🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

[C++] Is it safe to assume IEEE-754 and have memset 0 of floating point always valid for 0.0?

Started by
5 comments, last by frob 4 years, 3 months ago

Is it safe to assume IEEE-754 and have memset 0 of floating point always valid for 0.0?

Advertisement

Memsetting the memory to zero gives a zero floating point value.

There are zero values which don't have all bits off. Said differently, there are bit patterns with some bits on which are also numerically zero.

frob said:

Memsetting the memory to zero gives a zero floating point value.

There are zero values which don't have all bits off. Said differently, there are bit patterns with some bits on which are also numerically zero.

Unless you are talking just about -0, I don't think this is right.

To answer OP's question, yes, it's safe to memset to zeroes.

Frob, can you show a bit pattern in IEEE 754 single precision float other than 0x0 and 0x80000000 that's also a zero?

The C Language Standard (ISO/IEC 9899) madates IEE 754 and that's included in the C++ Language Standard (ISO/IEC 14882) by inference.

So, yes, a memset of 0 should be OK, within platform-specific constraints. Remember alignment, conversions to register representations, and all that faffing around involved in floating-point at the lowest level.

Stephen M. Webb
Professional Free Software Developer

pcmaster said:

Frob, can you show a bit pattern … other than 0x0 and 0x80000000 that's also a zero?

Subnormal/denormal numbers when the DAZ flag (Denormals Are Zero) and FTZ (Flush To Zero) flags are set. Any bit pattern that represents one of those numbers gets evaluated to zero when the flags are set.

Most programs run with those flags on to avoid the cost of subnormal/denormal value exceptions and the dramatically slower processing they require.

Which is why I wrote, a memset to a zero bit pattern is a zero, but there are many other bit patterns which can be processed as numerically zero. There are many possible zeros.

At this stage, I think it's safe to assume that any sane computing device is now following IEEE 754 for floating point behavior. GPUs were really the last hold-outs, and while I think there are still a few mild caveats they are basically compliant. But frob correctly notes in this particular case this is unidirectional. (Not sure why you'd scan the bit pattern for a comparison to zero… but don't.)

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

This topic is closed to new replies.

Advertisement