Archived

This topic is now archived and is closed to further replies.

Debugging and Floating Point exceptions

This topic is 5626 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just a little debugging tip I have found out recently - the hard way :/ As you might know, when you perform an illegal floating point operation in your game ( divide by 0, sqrt(-1), that sort of a thing), this result in an illegal numbers shown in the VC debugger as INF or QNAN. Unfortunately by default the processor just gobbles them up and just happily carries on. Even worse, if you use one of those illegal numbers in an other expression, it will make the result an illegal number as well. Eventually these QNANs spread like a disease across all your data (including saved games!), your objects will being disappearing in mid-air and everything will go crazy. At least that''s how it was for our game. If you have experienced anything like this, read on. Turns out there''s a simple way to remedy this - just enable floating point exceptions. They are for some reason disabled by default, even in Debug builds. this is how you do it: #include <float.h> ... WORD wSave; _clearfp(); //Clear all existing FPU exceptions __asm { fstcw wSave mov ax, wSave or ax, 3fh ;; disable all exceptions and ax, not 4Dh ;; enable Inval Op, Zero Division, ;; Denormal, Overflow, Stack fault mov wSave, ax fldcw wTemp Then, as soon as an illegal operation occurs, the program will break into the debugger. The message that VC gives is somewhat misleading, but it''s not hard to figure out what exactly went wrong. NOTE: there IS a high-level C functions _control87 and _controlfp that do roughly the same thing as the Asm code above but for some reason they doen''t allow to catch FPU stack faults (a fairly common bug if you write asm code).

Share this post


Link to post
Share on other sites