How does the operating system avoid performance overhead of triggering floating-point exceptions frequently?

Viewed 32

The IEEE floating point standard defines several exceptions that occur when the result of a floating point operation is unclear or undesirable: underflow, overflow, inexact, invalid, and divide-by-zero.

As you can see, such exceptions could occur quite frequently: even 0.2+0.1 should trigger the inexact exception. For a piece of numerical code involving N floating-point instructions, N/2 or more might trigger at least one of these exceptions. So I wonder how the OS works to avoid the performance overhead of constantly triggering these exceptions?

1 Answers

Floating-point exceptions are not operating-system or process exceptions. The common default mode is that floating-point exceptions raise a flag in the floating-point status register and deliver a default result for the operation but do not trigger a change of program control. No trap routine is called. The operating system is not informed and does not notice. The floating-point instruction performs much like an integer arithmetic instruction. (However, the need to update a global status register can cause serialization in some processor architectures. Some architectures are designed to avoid this unless the register is read.)

If a process does change the mode so that floating-point exceptions do cause traps, then performance may be affected, especially with the inexact exception.

Related