‹ BackHN Continuity

Thread

Subnormal floating-point numbers are expensive on Intel processors

69 points · 54 comments · zdw

  1. gwbas1c · · focus · HN ↗
    I'm still trying to understand what a subnormal number is; IE, I'm looking for the TLDR so I know just enough to know if I'm using them and need to learn more.

    Unfortunately, the Wikipedia article, while probably being accurate, doesn't give a clear and concise answer.

    IE, is 0.0001 a subnormal? Or is it 0.000000000000000000001?

    1. account42 · · focus · HN ↗
      Floating point numbers are usually interpreted as

          sign * 1.mantissa * 2 ^ exponent
      
      where sign, mantissa and exponent are fixed bit width integers. The 1. before the number is normally implicit because it would be a waste of a bit to encode it when you could just use a diferent exponent to represent such a number.

      However with this simple scheme the number zero and a relatively large gap around it cannot be represented (relatively large to the gap between the smallest and next smalles number that can be represented).

      So there is a special case where for the smallest encodeable exponent the mantissa must also specify that 1. or 0. prefix. Because its a special case it needs special handling that clever silicon engineers might think is unimportant enough to handle in microcode instead of dedicated silicon.

      x86 has a mode to assume that all such small numbers are actually equal to zero which can then be handle without microcode fallback. Technically its even a bit more complicated because x86 has two different float implementations and for at least SSE floats you can control the denormals-are-zero and flush-(denormals)-to-zero-(when writing) modes independently. GCC -ffast-math actual enables that mode for the entire main thread.

      AFAIK ARM NEON always works in that mode so the Gravion and Apple benchmarks might be unfair here undless you compare with DAZ and FTZ enabled on Intel. No idea if the AMD benchmarks might have used different modes. Because the flags are global per thread you can easily have unrelated loaded libraries messing the benchmark up.

      1. ack_complete · · focus · HN ↗
        > AFAIK ARM NEON always works in that mode

        This was only true for ARMv7 NEON (32-bit). ARMv8 / AArch64 NEON is IEEE compliant.

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.