Compile-time floating-point expressions and subsequent detection of possible overflows etc -- during the compile-time stage.

Vincent Lefevre vincent+gcc@vinc17.org
Wed Nov 2 09:01:00 GMT 2011


On 2011-11-02 15:31:42 +1100, leon zadorin wrote:
> Under certain conditions GCC is able to perform compile-time folding
> of floating point expressions. This is great.
> 
> Sometimes, however, the resulting (or indeed intermediate) expressions
> may evaluate to a non-finite value (e.g. NaN, Inf) -- at compile-time.
> 
> During a traditional run-time execution of floating point arithmetic
> there are various ways to detect the case of values going into
> something like an overflow (individual guarding of each
> possibly-violating expression via 'isfinite()'; or via reading the FPU
> status register, which hardware itself may flag appropriately; or via
> the 'fetestexcept' et al wrappers).
> 
> I was wondering if there was a way to ask GCC to optionally emit some
> kind of a diagnostic (e.g. a warning of some kind) when it does
> floating point calculations at compile-time and when such compile-time
> calculations yield in a non-finite number.

In ISO C99, I don't think the compiler is allowed to replace a
floating expression with an underflow or overflow by its result
at compile time. 6.6#4 of ISO C99 says: "Each constant expression
shall evaluate to a constant that is in the range of representable
values for its type." and an underflow or overflow means that the
result is not in the range of representable values, so that the
rules for constant expressions must not be applied.

Now, I don't know what C++ allows (in your case) and I haven't
tested how GCC behaves (there may be a bug...).

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <http://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / Arénaire project (LIP, ENS-Lyon)



More information about the Gcc-help mailing list