This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Compile-time floating-point expressions and subsequent detection of possible overflows etc -- during the compile-time stage.
- From: leon zadorin <leonleon77 at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Wed, 2 Nov 2011 15:31:42 +1100
- Subject: Compile-time floating-point expressions and subsequent detection of possible overflows etc -- during the compile-time stage.
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.
For example in an oversimplified case like this:
float const x(::std::numeric_limits<float>::max());
float const y(x * 2);
It would be nice for GCC to emit a warning if it ended up setting 'y'
to 'Inf'... or similar.
Testing for such compile-time results during runtime would always be
less efficient (and at times, when not using individual
expression-guarding but rather FPU register reading, not possible...)
Forcing to always individually-guard may also be extremely defficient
(e.g. if 99% of cases are not overflowing then, statistically, it may
not be the best way of doing this). Expecting for the compile-time
'Inf', or similar, to be propagated eventually to some
runtime-detectable result is also not a solution as comparative (e.g.
x < y) math will not work either and not be detected (as 'trigger' for
y to overflow had happened at compile-time).
Best regards
Leon.