Daemon icon indicating copy to clipboard operation
Daemon copied to clipboard

Add a cvar to enable floating point exceptions

Open slipher opened this issue 3 years ago • 8 comments

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

slipher avatar Dec 12 '22 03:12 slipher

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

illwieckz avatar Mar 22 '23 07:03 illwieckz

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?

slipher avatar Mar 22 '23 15:03 slipher

Is the produced bytecode for divisions the same once the feature is built?

illwieckz avatar Mar 22 '23 15:03 illwieckz

Of course, it's just calling a function at runtime that sets a bit in the processor.

slipher avatar Mar 24 '23 04:03 slipher

Can we have this implemented?

Also, is there a way to have a warning instead of a crash on Linux?

illwieckz avatar May 07 '23 16:05 illwieckz

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...

slipher avatar May 07 '23 17:05 slipher

What's the performance cost?

ghost avatar May 07 '23 17:05 ghost

What's the performance cost?

Nothing. But this shouldn't be enabled by default anyway. Only for debugging.

slipher avatar May 07 '23 17:05 slipher