News for hackers. Stuff that matters.
Re:C and C++ Prioritize Performance over Correctness by weinzierl on Friday August 18, 2023 @02:02PM (#37179645)
ARM actually kind of has that. The register file has an overflow flag that is not cleared on subsequent operations (sticky overflow). So instead of triggering an exception, which is prohibitively expensive, you can do a series of calculations and check afterwards if an overflow happened in any of them. A bit like NaN for floating point. From what I understand the flag alone is still costly, so we will have to see if it survives.
Re:C and C++ Prioritize Performance over Correctness by andersa on Friday August 18, 2023 @02:09PM (#37179760)
It doesn't matter if triggering the exception is expensive. At that point overflow has already occured, so your program state is now nonsense, and you might as well just let it crash unhandled. Much better outcome than reading memory at some mysterious offset.

If just having the ability for an exception to occur during an instruction causes overhead, that would be a big problem though.

Re:C and C++ Prioritize Performance over Correctness by weinzierl on Friday August 18, 2023 @03:07PM (#37180570)
Think about it like that. If you allow an exception you essentially create many branches with all their negative consequences. With the sticky bit you combine them to one branch (that is still expensive[1]).

[1] https://news.ycombinator.com/item?id=8766417

Re:C and C++ Prioritize Performance over Correctness by Findecanor on Friday August 18, 2023 @03:16PM (#37180687)
> At that point overflow has already occured, so your program state is now nonsense, and you might as well just let it crash unhandled.

The trick is to check and clear the flag before any instruction that would have a side-effect, that depends on the arithmetic result.

IEEE 754-compliant floating point units have a similar behaviour with NaN that is a bit more versatile: an arithmetic instruction results in NaN if any operand is NaN, but an instruction with side-effect (compare, convert or store) will raise an exception if given a NaN.

Re:C and C++ Prioritize Performance over Correctness by spookie on Friday August 18, 2023 @02:09PM (#37179753)
That's quite interesting and reasonable