← Back to context

Comment by maplant

15 hours ago

It can't do these things because it's not wanted. Say you have the following:

  enum Value {
      Float(f64),
      Ptr(*const T),
  }

Do you want the compiler to disallow certain bit patterns in the Float variant simply so that it can implement NanBoxing?

Probably with an annotation around a NanBoxable(f64) type that tells it to do that.

That being said, the optimization is complex that may be insufficient:

> For my boxing scheme, I picked a bias value such that the lowest two bits end up being 10. That 1 in bit index 1 indicates that doubles can't be directly compared for equality. Amazingly, we only lose two bits of exponent, and we keep the full precision of the mantissa, meaning we lose no significant digits in the flonum representation.

This suggests the optimization needs more information about specifically how you want to box the float. There probably is some primitives worth considering standardizing to make this kind of optimization possible so that the tunable parameters are passed as const generic values.

Or actually doing what they did here where it changes it to a

    enum Value {
         SimpleFloat(64bit value),
         ComplexNan(Heap pointer)
         Ptr(*const T)
    }

You lose out on performance if you use the bit patterns in the float that most people don't use very much, but you keep the correctness.

I'd be very unhappy if a compiler silently did this to me - it would make performance extremely hard to reason about. But it's not quite as bad as changing the semantics.