News for hackers. Stuff that matters.
Re:Float Self-Tagging by rtpg on Wednesday November 27, 2024 @09:13PM (#42261761)
Do all float operations need to reconfirm those bits afterwards though? I suppose if you have some sort of JIT you can end up with a bunch of unboxed floats and would only pay the cost on boundaries though
Re:Float Self-Tagging by pansa2 on Wednesday November 27, 2024 @10:06PM (#42262058)
> reconfirm those bits afterwards

Thanks - I hadn't thought about that but it seems to be the main downside of this approach. The benefit of NaN-boxing is that it reassigns values that are otherwise unused - floating-point calculations will never generate NaNs with those bit patterns.

Re:Float Self-Tagging by AlotOfReading on Thursday November 28, 2024 @02:23AM (#42263237)
An additional wrinkle is that NaNs are a bit unstable and can have large performance penalties. You can't let the NaNs ever escape into arithmetic and you may even have issues even storing them in a register.
Re:Float Self-Tagging by lifthrasiir on Wednesday November 27, 2024 @09:54PM (#42261994)
Only when they have to be boxed, but yes if you are talking about that.
Re:Float Self-Tagging by ndjdjddjsjj on Thursday November 28, 2024 @01:57AM (#42263056)
You need to check after the floating point operation though just in case. Or after the boundary where you pass the float to something else expecting this scheme.
Re:Float Self-Tagging by phire on Thursday November 28, 2024 @02:09AM (#42263129)
Yes, but there should be some optimisation opportunities.

Off the top of my head: Any multiply by a constant less than 1.0 will never overflow the unboxed range (but might underflow) and there should be times when it's provably better to check the inputs are inside a range, rather than checking the outputs.

It's worth pointing out that these overflow/underflow checks will be very cold (on typical code). They won't waste much in the way of branch-prediction resources.

I wonder if it's worth taking advantage of floating point overflow/underflow exceptions. I think a multiplication by 2^767 will trigger an exception if the value would overflow, and the corresponding multiply by 2^-765 will catch underflows.

It's tempting to allocate two more tags for floats (001 and 010), covering the entire range from -2^257 to +2^257. It will be rare to actually see those small floats near zero, but it could be worth eliminating the possibility of underflows.

Re:Float Self-Tagging by ithkuil on Thursday November 28, 2024 @06:32AM (#42264475)
You check the tag before doing float operations
Re:Float Self-Tagging by pansa2 on Thursday November 28, 2024 @08:56AM (#42265279)
And afterwards, because floating-point arithmetic can change the value of the tag. This isn't necessary with NaN-boxing, because it uses NaN bit patterns that the hardware never generates.