|
|
|
Dealing with Floating-point Exceptions in MSVC7\8 : Page 4
Learn how to catch floating-point exceptions in C\C++ code.
|
|
|
![]() |
|
The Visual Studio Compiler Options
For both Debug and Release configurations, Visual Studio offers the "Code Generation" property page in "C/C++" property folder. Here, you can select the desired option for the "Enable Enhanced Instruction Set" property. The available options are:
- "Not Set"
- "Streaming SIMD Extensions (/arch:SSE):" The new set of instructions and registers on the Pentium III processor allows Single-Instruction Multiple-Data (SIMD) computations to be made on single-precision floating-point numbers. You may see the assembler code for both options above in a disassembly window during debugging that looks like:
c = a/b; //inside a __try block
100114A8 fld qword ptr [a]
100114AB fdiv qword ptr [b]
100114AE fstp qword ptr [c]
100114B1 wait
}
100114B2 wait
100114B3 mov dword ptr [ebp-4],0FFFFFFFEh
100114BA jmp $LN6+12h (100114EBh)
_except(EvaluateException(GetExceptionCode()))
Because the assembly code is the same for both options, no changes are needed for the source code in order to trap floating-point exceptions when the option is set to /arch:SSE.
- "Streaming SIMD Extensions 2 (/arch:SSE2)": The new set of instructions on the Pentium 4 and Intel Xeon processors operates on the XXM and MXCSR registers and performs SIMD computations on double-precision floating-point and integer numbers. The assembler code for this option looks like:
c = a/b; //inside a __try block
1001325A movsd xmm0,mmword ptr [a]
1001325F divsd xmm0,mmword ptr [b]
10013264 movsd mmword ptr [c],xmm0
10013269 wait
1001326A mov dword ptr [ebp-4],0FFFFFFFFh
10013271 jmp $L18835+12h (100132A2h)
}
_except(EvaluateException(GetExceptionCode()))
The SSE and SSE2 instructions can generate the same floating-point exceptions (mentioned above) that are generated with x87 floating-point instructions and are defined in IEEE Standard 754. There is also a Denormal operand exception that is not defined in the standard. Each of the exception conditions has a corresponding flag and mask in the MXCSR register. Control register CR4's OSXMMEXCEPT flag provides additional control over generation of SIMD floating-point exceptions.
Comparing VS 2005 with VS 2003
VS 2005 introduced some improvements in dealing with floating-point exceptions. To compare with VS 2003:
- The options for the Code Generation \Enable C++ Exceptions property in VS2003 are:
- No: Structured and C++ exceptions are captured, but C++ objects that go out –f-scope as a result of exception will not be destroyed. This is an equivalent to /EHsc-.
- Yes (/EHsc): Only C++ exceptions will be captured. This means that catch(. . .) will not catch C exceptions. The C++ objects that should be destroyed will be destroyed. During optimization, the Release configuration compiler may optimize away the catch clause if it does not see a throw nside the try block.
- Changes for VS2005:
- Yes (/EHsc): If a C exception is generated, the C++ objects that should be destroyed will not be destroyed. This means that C++ exception handling works correctly only for C++ exceptions; in other words, catch (. . .) does not catch C exceptions.
- Yes with SHE Exceptions (/EHa) catch (. . .) will catch C and C++ exceptions. Also, if you install a translator function, this option should be selected.
- The options for Code Generation\Enable Floating Point Exceptions property (Enable Floating Point Exception Semantics) in VS 2005 are:
- Changes for VS2005:
- This property has been removed. The new property is Code Generation\Floating Point Model with these options:
- Precise (fp:precise): Replaces the /Ops option.
- Strict (/fp:strict): Creates the strictest code.
- Fast (/fp:fast): The compiler creates the fastest code in the majority of cases.
- If you use _controlfp() in code compiled with VS 2005, you will get a C4996 warning that this function has been deprecated. Use _controlfp_s() instead.
|
Boris Eligulashvili is a Senior Engineer at Viewray, Inc. Prior to joining Viewray, he worked his way through different software development positions for Allen-Bradley, Saint-Gobain Crystals and Detectors, Philips Medical Systems, and Hitachi Medical Systems America, Inc. He has worked with the latest technologies as they have evolved throughout the yearsfrom raw machine code to .NET. His current focus is in developing software solutions for a novel MRI-guided radiotherapy device.
Email Author
|
|
|
|
|