[LLVMdev] Floating-Point Overflow check

51 views
Skip to first unread message

Steffen Geißinger

unread,
Jul 23, 2010, 10:20:26 AM7/23/10
to LLV...@cs.uiuc.edu
Hi,
 
i need to check if an overflow of an floating-point arithmetic operation occured.
Currently I'm doing something like this (for addition):
 
(LHS > 0 && RHS > 0 && sum <= 0)  || (LHS < 0 && RHS < 0 && sum >= 0)
 
This is checked for every addition.
Is there a more efficient way like the intrisic for int overflow?
How is it possible to raise a signal like SIGFPE?
 
Thanks!
 
Regards,
Steffen
 
 

John McCall

unread,
Jul 23, 2010, 1:36:37 PM7/23/10
to Steffen Geißinger, LLV...@cs.uiuc.edu
On Jul 23, 2010, at 7:20 AM, Steffen Geißinger wrote:
i need to check if an overflow of an floating-point arithmetic operation occured.
Currently I'm doing something like this (for addition):
 
(LHS > 0 && RHS > 0 && sum <= 0)  || (LHS < 0 && RHS < 0 && sum >= 0)

IEEE floating-point doesn't overflow like this;  if the magnitude of a result is too
large to represent, it becomes an infinity rather than wrapping around like integer
arithmetic.  You want to check whether the result is infinite.

I think the easiest way of doing that is to check whether (x - x) != (x - x).

John.

me22

unread,
Jul 23, 2010, 1:37:06 PM7/23/10
to Steffen Geißinger, LLV...@cs.uiuc.edu
On 23 July 2010 07:20, Steffen Geißinger

<steffen.g...@googlemail.com> wrote:
>
> i need to check if an overflow of an floating-point arithmetic operation
> occured.
> Currently I'm doing something like this (for addition):
>
> (LHS > 0 && RHS > 0 && sum <= 0)  || (LHS < 0 && RHS < 0 && sum >= 0)
>

Two things:
1) Have you tried adding 2 float-max values together to see what you
get? It's not a negative (on most platforms, at least). IEEE
floating-point has "infinities".
2) That method doesn't even work for integers, since overflow in
signed addition is undefined, and compilers will, correctly, remove
the check as impossible.

_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

Dale Johannesen

unread,
Jul 23, 2010, 1:41:10 PM7/23/10
to Steffen Geißinger, LLV...@cs.uiuc.edu
If you're talking about compile-time arithmetic, you should be using the APFloat library, which figures this out for you.

Steffen Geißinger

unread,
Jul 26, 2010, 7:12:07 AM7/26/10
to John McCall, LLV...@cs.uiuc.edu
 
Hello,
 
i know that the result is infinte, but there are sill flags in the FPU which indicate that an overflow, underflow or div by zero occured.
So isn't there an easy way to check if one of those flags is set?

 
2010/7/23 John McCall <rjmc...@apple.com>


 - If you're talking about compile-time arithmetic, you should be using the APFloat library, which figures this out for you.

No I'm talking about run-time.
 
-- Steffen

John McCall

unread,
Jul 26, 2010, 1:04:16 PM7/26/10
to Steffen Geißinger, LLV...@cs.uiuc.edu
On Jul 26, 2010, at 4:12 AM, Steffen Geißinger wrote:
> i know that the result is infinte, but there are sill flags in the FPU which indicate that an overflow, underflow or div by zero occured.
> So isn't there an easy way to check if one of those flags is set?

I don't think so; we probably don't select those instructions at all. I'm not sure what the best way to select them would be, to be honest; maybe a processor-specific intrinsic. I'd suggest filing a bug.

John.

Reply all
Reply to author
Forward
0 new messages