Optimizing with dmd disables floating point exceptions

Walter Bright newshound2 at digitalmars.com
Thu Apr 28 23:27:19 PDT 2011


On 4/28/2011 3:02 PM, Jens Mueller wrote:
> Hi,
>
> checking whether a floating point exception occurred is good coding
> practice for me.
>
> I discovered a strange behavior using dmd.
> divByZero.d:
> import std.math;
> unittest {
>      auto a = 1.0L;
>      assert(!ieeeFlags.divByZero);
>      a /= 0.0L;
>      assert(ieeeFlags.divByZero);
> }
>
> $ rdmd --main -unittest divByZero.d
> passes, but
> $ rdmd -O --main -unittest divByZero.d
> makes the second assert fail.
>
> Similar behavior if I enable floating point exceptions
>      FloatingPointControl fpctrl;
>      fpctrl.enableExceptions(FloatingPointControl.severeExceptions);
>
> Without optimization I get a floating point exception. With
> optimization the code passes even though it shouldn't.
> In sum neither floating point exceptions nor status flags are working
> when optimizations are turned on.
> $ dmd | head -1
> Digital Mars D Compiler v2.052

That's because the a/=1.0L; is a dead assignment and is removed by the optimizer.



More information about the Digitalmars-d mailing list