How to set double precision in C++ on MacOSX?
I'm trying to port _controlfp( _CW_DEF开发者_运维知识库AULT, 0xffffffff ); from WIN32 to Mac OS X / Intel. I have absolutely no idea how to port this instruction... And you? Thanks!
Try section 8.6 of Gough's Introduction to GCC, which demonstrates the x86 FLDCW instruction. But it helps if you tell us why you need it — if you want your doubles to be IEEE-754 64-bit doubles, the easiest way is to compile with -msse -mfpmath=sse.
What precision elements are you controlling?
According to Microsoft's Website:
The
_control87
function gets and sets the floating-point control word. The floating-point control word allows the program to change the precision, rounding, and infinity modes in the floating-point math package. You can also mask or unmask floating-point exceptions using_control87
. If the value for mask is equal to 0,_control87
gets the floating-point control word. If mask is nonzero, a new value for the control word is set: For any bit that is on (equal to 1) in mask, the corresponding bit in new is used to update the control word. In other words,fpcntrl = ((fpcntrl & ~mask) | (new & mask))
where fpcntrl is the floating-point control word.
Note the keyword "library". This function is manipulating the Microsoft library, which may not exist on the Mac.
I suggest the following:
- Control the floating point chip on the Mac yourself
- Or use a Microsoft compiler for Mac
- Or find a method to move the floating point controls to a more generic spot in your program.
The best advice I can give is to keep as much code inline with the standard and platform or library specific issues to minimum. For those functions involving platform specific features, move them into their own source files / translation units. Create copies of these functions, one for each platform. Let the linker decide with ones to use.
Good Luck.
精彩评论