How to set double precision in C++ on MacOSX?

僤鯓⒐⒋嵵緔 提交于 2019-12-10 19:45:25

问题


I'm trying to port _controlfp( _CW_DEFAULT, 0xffffffff ); from WIN32 to Mac OS X / Intel. I have absolutely no idea how to port this instruction... And you? Thanks!


回答1:


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.




回答2:


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:

  1. Control the floating point chip on the Mac yourself
  2. Or use a Microsoft compiler for Mac
  3. 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.



来源:https://stackoverflow.com/questions/2900614/how-to-set-double-precision-in-c-on-macosx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!