Add a cvar to enable floating point exceptions
Floating point exceptions can be useful to detect bugs such as floating point division by zero. A cvar to turn that on would be a useful debugging tool. Preferably on a per-module basis (engine/cgame/sgame). To turn it on for only a gamelogic module you'll need to use a native exe since a DLL shares the global state with the engine and I doubt this functionality is available in NaCl.
On Windows the exceptions can be enabled like
unsigned dummy;
if (0 != _controlfp_s(&dummy, 0, _EM_INVALID | _EM_ZERODIVIDE /* | _EM_OVERFLOW */))
Log::Warn("error");
On Linux feenableexcept can be used
Why not just enabling them globally with a CMake variable to begin with? So a debug build can return to a debugger and dump a backtrace?
I managed to fix some issues by writing feenableexcept (FE_DIVBYZERO); at the beginning of the main function as this looks good enough to catch obvious errors:
- https://github.com/DaemonEngine/Daemon/pull/812
- https://github.com/Unvanquished/Unvanquished/pull/2533
Why would you make it a compile-time option when the code needed is very small and it can be easily enabled or disabled at runtime?
Is the produced bytecode for divisions the same once the feature is built?
Of course, it's just calling a function at runtime that sets a bit in the processor.
Can we have this implemented?
Also, is there a way to have a warning instead of a crash on Linux?
Also, is there a way to have a warning instead of a crash on Linux?
I was looking for this yesterday (how to resume execution after a floating point trap) but people asking on Stack Overflow were given negative responses. Meanwhile I found someone claiming to be able to do it on Windows...
What's the performance cost?
What's the performance cost?
Nothing. But this shouldn't be enabled by default anyway. Only for debugging.