zend_float.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Christian Seiler <chris_se@gmx.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifndef ZEND_FLOAT_H
  19. #define ZEND_FLOAT_H
  20. BEGIN_EXTERN_C()
  21. /*
  22. Define functions for FP initialization and de-initialization.
  23. */
  24. extern ZEND_API void zend_init_fpu(void);
  25. extern ZEND_API void zend_shutdown_fpu(void);
  26. extern ZEND_API void zend_ensure_fpu_mode(void);
  27. END_EXTERN_C()
  28. /* Copy of the contents of xpfpa.h (which is under public domain)
  29. See http://wiki.php.net/rfc/rounding for details.
  30. Cross Platform Floating Point Arithmetics
  31. This header file defines several platform-dependent macros that ensure
  32. equal and deterministic floating point behaviour across several platforms,
  33. compilers and architectures.
  34. The current macros are currently only used on x86 and x86_64 architectures,
  35. on every other architecture, these macros expand to NOPs. This assumes that
  36. other architectures do not have an internal precision and the operhand types
  37. define the computational precision of floating point operations. This
  38. assumption may be false, in that case, the author is interested in further
  39. details on the other platform.
  40. For further details, please visit:
  41. http://www.christian-seiler.de/projekte/fpmath/
  42. Version: 20090317 */
  43. /*
  44. Implementation notes:
  45. x86_64:
  46. - Since all x86_64 compilers use SSE by default, we do not define these
  47. macros there. We ignore the compiler option -mfpmath=i387, because there is
  48. no reason to use it on x86_64.
  49. General:
  50. - It would be nice if one could detect whether SSE if used for math via some
  51. funky compiler defines and if so, make the macros go to NOPs. Any ideas
  52. on how to do that?
  53. MS Visual C:
  54. - Since MSVC users typically don't use autoconf or CMake, we will detect
  55. MSVC via compile time define.
  56. */
  57. /* MSVC detection (MSVC people usually don't use autoconf) */
  58. #if defined(_MSC_VER) && !defined(_WIN64)
  59. # define HAVE__CONTROLFP_S
  60. #endif /* _MSC_VER */
  61. #if defined(HAVE__CONTROLFP_S) && !defined(__x86_64__)
  62. /* float.h defines _controlfp_s */
  63. # include <float.h>
  64. # define XPFPA_HAVE_CW 1
  65. # define XPFPA_CW_DATATYPE \
  66. unsigned int
  67. # define XPFPA_STORE_CW(vptr) do { \
  68. _controlfp_s((unsigned int *)(vptr), 0, 0); \
  69. } while (0)
  70. # define XPFPA_RESTORE_CW(vptr) do { \
  71. unsigned int _xpfpa_fpu_cw; \
  72. _controlfp_s(&_xpfpa_fpu_cw, *((unsigned int *)(vptr)), _MCW_PC); \
  73. } while (0)
  74. # define XPFPA_DECLARE \
  75. unsigned int _xpfpa_fpu_oldcw, _xpfpa_fpu_cw;
  76. # define XPFPA_SWITCH_DOUBLE() do { \
  77. _controlfp_s(&_xpfpa_fpu_cw, 0, 0); \
  78. _xpfpa_fpu_oldcw = _xpfpa_fpu_cw; \
  79. _controlfp_s(&_xpfpa_fpu_cw, _PC_53, _MCW_PC); \
  80. } while (0)
  81. # define XPFPA_SWITCH_SINGLE() do { \
  82. _controlfp_s(&_xpfpa_fpu_cw, 0, 0); \
  83. _xpfpa_fpu_oldcw = _xpfpa_fpu_cw; \
  84. _controlfp_s(&_xpfpa_fpu_cw, _PC_24, _MCW_PC); \
  85. } while (0)
  86. /* NOTE: This only sets internal precision. MSVC does NOT support double-
  87. extended precision! */
  88. # define XPFPA_SWITCH_DOUBLE_EXTENDED() do { \
  89. _controlfp_s(&_xpfpa_fpu_cw, 0, 0); \
  90. _xpfpa_fpu_oldcw = _xpfpa_fpu_cw; \
  91. _controlfp_s(&_xpfpa_fpu_cw, _PC_64, _MCW_PC); \
  92. } while (0)
  93. # define XPFPA_RESTORE() \
  94. _controlfp_s(&_xpfpa_fpu_cw, _xpfpa_fpu_oldcw, _MCW_PC)
  95. /* We do NOT use the volatile return trick since _controlfp_s is a function
  96. call and thus FP registers are saved in memory anyway. However, we do use
  97. a variable to ensure that the expression passed into val will be evaluated
  98. *before* switching back contexts. */
  99. # define XPFPA_RETURN_DOUBLE(val) \
  100. do { \
  101. double _xpfpa_result = (val); \
  102. XPFPA_RESTORE(); \
  103. return _xpfpa_result; \
  104. } while (0)
  105. # define XPFPA_RETURN_SINGLE(val) \
  106. do { \
  107. float _xpfpa_result = (val); \
  108. XPFPA_RESTORE(); \
  109. return _xpfpa_result; \
  110. } while (0)
  111. /* This won't work, but we add a macro for it anyway. */
  112. # define XPFPA_RETURN_DOUBLE_EXTENDED(val) \
  113. do { \
  114. long double _xpfpa_result = (val); \
  115. XPFPA_RESTORE(); \
  116. return _xpfpa_result; \
  117. } while (0)
  118. #elif defined(HAVE__CONTROLFP) && !defined(__x86_64__)
  119. /* float.h defines _controlfp */
  120. # include <float.h>
  121. # define XPFPA_DECLARE \
  122. unsigned int _xpfpa_fpu_oldcw;
  123. # define XPFPA_HAVE_CW 1
  124. # define XPFPA_CW_DATATYPE \
  125. unsigned int
  126. # define XPFPA_STORE_CW(vptr) do { \
  127. *((unsigned int *)(vptr)) = _controlfp(0, 0); \
  128. } while (0)
  129. # define XPFPA_RESTORE_CW(vptr) do { \
  130. _controlfp(*((unsigned int *)(vptr)), _MCW_PC); \
  131. } while (0)
  132. # define XPFPA_SWITCH_DOUBLE() do { \
  133. _xpfpa_fpu_oldcw = _controlfp(0, 0); \
  134. _controlfp(_PC_53, _MCW_PC); \
  135. } while (0)
  136. # define XPFPA_SWITCH_SINGLE() do { \
  137. _xpfpa_fpu_oldcw = _controlfp(0, 0); \
  138. _controlfp(_PC_24, _MCW_PC); \
  139. } while (0)
  140. /* NOTE: This will only work as expected on MinGW. */
  141. # define XPFPA_SWITCH_DOUBLE_EXTENDED() do { \
  142. _xpfpa_fpu_oldcw = _controlfp(0, 0); \
  143. _controlfp(_PC_64, _MCW_PC); \
  144. } while (0)
  145. # define XPFPA_RESTORE() \
  146. _controlfp(_xpfpa_fpu_oldcw, _MCW_PC)
  147. /* We do NOT use the volatile return trick since _controlfp is a function
  148. call and thus FP registers are saved in memory anyway. However, we do use
  149. a variable to ensure that the expression passed into val will be evaluated
  150. *before* switching back contexts. */
  151. # define XPFPA_RETURN_DOUBLE(val) \
  152. do { \
  153. double _xpfpa_result = (val); \
  154. XPFPA_RESTORE(); \
  155. return _xpfpa_result; \
  156. } while (0)
  157. # define XPFPA_RETURN_SINGLE(val) \
  158. do { \
  159. float _xpfpa_result = (val); \
  160. XPFPA_RESTORE(); \
  161. return _xpfpa_result; \
  162. } while (0)
  163. /* This will only work on MinGW */
  164. # define XPFPA_RETURN_DOUBLE_EXTENDED(val) \
  165. do { \
  166. long double _xpfpa_result = (val); \
  167. XPFPA_RESTORE(); \
  168. return _xpfpa_result; \
  169. } while (0)
  170. #elif defined(HAVE__FPU_SETCW) && !defined(__x86_64__) /* glibc systems */
  171. /* fpu_control.h defines _FPU_[GS]ETCW */
  172. # include <fpu_control.h>
  173. # define XPFPA_DECLARE \
  174. fpu_control_t _xpfpa_fpu_oldcw, _xpfpa_fpu_cw;
  175. # define XPFPA_HAVE_CW 1
  176. # define XPFPA_CW_DATATYPE \
  177. fpu_control_t
  178. # define XPFPA_STORE_CW(vptr) do { \
  179. _FPU_GETCW((*((fpu_control_t *)(vptr)))); \
  180. } while (0)
  181. # define XPFPA_RESTORE_CW(vptr) do { \
  182. _FPU_SETCW((*((fpu_control_t *)(vptr)))); \
  183. } while (0)
  184. # define XPFPA_SWITCH_DOUBLE() do { \
  185. _FPU_GETCW(_xpfpa_fpu_oldcw); \
  186. _xpfpa_fpu_cw = (_xpfpa_fpu_oldcw & ~_FPU_EXTENDED & ~_FPU_SINGLE) | _FPU_DOUBLE; \
  187. _FPU_SETCW(_xpfpa_fpu_cw); \
  188. } while (0)
  189. # define XPFPA_SWITCH_SINGLE() do { \
  190. _FPU_GETCW(_xpfpa_fpu_oldcw); \
  191. _xpfpa_fpu_cw = (_xpfpa_fpu_oldcw & ~_FPU_EXTENDED & ~_FPU_DOUBLE) | _FPU_SINGLE; \
  192. _FPU_SETCW(_xpfpa_fpu_cw); \
  193. } while (0)
  194. # define XPFPA_SWITCH_DOUBLE_EXTENDED() do { \
  195. _FPU_GETCW(_xpfpa_fpu_oldcw); \
  196. _xpfpa_fpu_cw = (_xpfpa_fpu_oldcw & ~_FPU_SINGLE & ~_FPU_DOUBLE) | _FPU_EXTENDED; \
  197. _FPU_SETCW(_xpfpa_fpu_cw); \
  198. } while (0)
  199. # define XPFPA_RESTORE() \
  200. _FPU_SETCW(_xpfpa_fpu_oldcw)
  201. /* We use a temporary volatile variable (in a new block) in order to ensure
  202. that the optimizer does not mis-optimize the instructions. Also, a volatile
  203. variable ensures truncation to correct precision. */
  204. # define XPFPA_RETURN_DOUBLE(val) \
  205. do { \
  206. volatile double _xpfpa_result = (val); \
  207. XPFPA_RESTORE(); \
  208. return _xpfpa_result; \
  209. } while (0)
  210. # define XPFPA_RETURN_SINGLE(val) \
  211. do { \
  212. volatile float _xpfpa_result = (val); \
  213. XPFPA_RESTORE(); \
  214. return _xpfpa_result; \
  215. } while (0)
  216. # define XPFPA_RETURN_DOUBLE_EXTENDED(val) \
  217. do { \
  218. volatile long double _xpfpa_result = (val); \
  219. XPFPA_RESTORE(); \
  220. return _xpfpa_result; \
  221. } while (0)
  222. #elif defined(HAVE_FPSETPREC) && !defined(__x86_64__) /* FreeBSD */
  223. /* fpu_control.h defines _FPU_[GS]ETCW */
  224. # include <machine/ieeefp.h>
  225. # define XPFPA_DECLARE \
  226. fp_prec_t _xpfpa_fpu_oldprec;
  227. # define XPFPA_HAVE_CW 1
  228. # define XPFPA_CW_DATATYPE \
  229. fp_prec_t
  230. # define XPFPA_STORE_CW(vptr) do { \
  231. *((fp_prec_t *)(vptr)) = fpgetprec(); \
  232. } while (0)
  233. # define XPFPA_RESTORE_CW(vptr) do { \
  234. fpsetprec(*((fp_prec_t *)(vptr))); \
  235. } while (0)
  236. # define XPFPA_SWITCH_DOUBLE() do { \
  237. _xpfpa_fpu_oldprec = fpgetprec(); \
  238. fpsetprec(FP_PD); \
  239. } while (0)
  240. # define XPFPA_SWITCH_SINGLE() do { \
  241. _xpfpa_fpu_oldprec = fpgetprec(); \
  242. fpsetprec(FP_PS); \
  243. } while (0)
  244. # define XPFPA_SWITCH_DOUBLE_EXTENDED() do { \
  245. _xpfpa_fpu_oldprec = fpgetprec(); \
  246. fpsetprec(FP_PE); \
  247. } while (0)
  248. # define XPFPA_RESTORE() \
  249. fpsetprec(_xpfpa_fpu_oldprec)
  250. /* We use a temporary volatile variable (in a new block) in order to ensure
  251. that the optimizer does not mis-optimize the instructions. Also, a volatile
  252. variable ensures truncation to correct precision. */
  253. # define XPFPA_RETURN_DOUBLE(val) \
  254. do { \
  255. volatile double _xpfpa_result = (val); \
  256. XPFPA_RESTORE(); \
  257. return _xpfpa_result; \
  258. } while (0)
  259. # define XPFPA_RETURN_SINGLE(val) \
  260. do { \
  261. volatile float _xpfpa_result = (val); \
  262. XPFPA_RESTORE(); \
  263. return _xpfpa_result; \
  264. } while (0)
  265. # define XPFPA_RETURN_DOUBLE_EXTENDED(val) \
  266. do { \
  267. volatile long double _xpfpa_result = (val); \
  268. XPFPA_RESTORE(); \
  269. return _xpfpa_result; \
  270. } while (0)
  271. #elif defined(HAVE_FPU_INLINE_ASM_X86) && !defined(__x86_64__)
  272. /*
  273. Custom x86 inline assembler implementation.
  274. This implementation does not use predefined wrappers of the OS / compiler
  275. but rather uses x86/x87 inline assembler directly. Basic instructions:
  276. fnstcw - Store the FPU control word in a variable
  277. fldcw - Load the FPU control word from a variable
  278. Bits (only bits 8 and 9 are relevant, bits 0 to 7 are for other things):
  279. 0x0yy: Single precision
  280. 0x1yy: Reserved
  281. 0x2yy: Double precision
  282. 0x3yy: Double-extended precision
  283. We use an unsigned int for the datatype. glibc sources add __mode__ (__HI__)
  284. attribute to it (HI stands for half-integer according to docs). It is unclear
  285. what the does exactly and how portable it is.
  286. The assembly syntax works with GNU CC, Intel CC and Sun CC.
  287. */
  288. # define XPFPA_DECLARE \
  289. unsigned int _xpfpa_fpu_oldcw, _xpfpa_fpu_cw;
  290. # define XPFPA_HAVE_CW 1
  291. # define XPFPA_CW_DATATYPE \
  292. unsigned int
  293. # define XPFPA_STORE_CW(vptr) do { \
  294. __asm__ __volatile__ ("fnstcw %0" : "=m" (*((unsigned int *)(vptr)))); \
  295. } while (0)
  296. # define XPFPA_RESTORE_CW(vptr) do { \
  297. __asm__ __volatile__ ("fldcw %0" : : "m" (*((unsigned int *)(vptr)))); \
  298. } while (0)
  299. # define XPFPA_SWITCH_DOUBLE() do { \
  300. __asm__ __volatile__ ("fnstcw %0" : "=m" (*&_xpfpa_fpu_oldcw)); \
  301. _xpfpa_fpu_cw = (_xpfpa_fpu_oldcw & ~0x100) | 0x200; \
  302. __asm__ __volatile__ ("fldcw %0" : : "m" (*&_xpfpa_fpu_cw)); \
  303. } while (0)
  304. # define XPFPA_SWITCH_SINGLE() do { \
  305. __asm__ __volatile__ ("fnstcw %0" : "=m" (*&_xpfpa_fpu_oldcw)); \
  306. _xpfpa_fpu_cw = (_xpfpa_fpu_oldcw & ~0x300); \
  307. __asm__ __volatile__ ("fldcw %0" : : "m" (*&_xpfpa_fpu_cw)); \
  308. } while (0)
  309. # define XPFPA_SWITCH_DOUBLE_EXTENDED() do { \
  310. __asm__ __volatile__ ("fnstcw %0" : "=m" (*&_xpfpa_fpu_oldcw)); \
  311. _xpfpa_fpu_cw = _xpfpa_fpu_oldcw | 0x300; \
  312. __asm__ __volatile__ ("fldcw %0" : : "m" (*&_xpfpa_fpu_cw)); \
  313. } while (0)
  314. # define XPFPA_RESTORE() \
  315. __asm__ __volatile__ ("fldcw %0" : : "m" (*&_xpfpa_fpu_oldcw))
  316. /* We use a temporary volatile variable (in a new block) in order to ensure
  317. that the optimizer does not mis-optimize the instructions. Also, a volatile
  318. variable ensures truncation to correct precision. */
  319. # define XPFPA_RETURN_DOUBLE(val) \
  320. do { \
  321. volatile double _xpfpa_result = (val); \
  322. XPFPA_RESTORE(); \
  323. return _xpfpa_result; \
  324. } while (0)
  325. # define XPFPA_RETURN_SINGLE(val) \
  326. do { \
  327. volatile float _xpfpa_result = (val); \
  328. XPFPA_RESTORE(); \
  329. return _xpfpa_result; \
  330. } while (0)
  331. # define XPFPA_RETURN_DOUBLE_EXTENDED(val) \
  332. do { \
  333. volatile long double _xpfpa_result = (val); \
  334. XPFPA_RESTORE(); \
  335. return _xpfpa_result; \
  336. } while (0)
  337. #else /* FPU CONTROL */
  338. /*
  339. This is either not an x87 FPU or the inline assembly syntax was not
  340. recognized. In any case, default to NOPs for the macros and hope the
  341. generated code will behave as planned.
  342. */
  343. # define XPFPA_DECLARE /* NOP */
  344. # define XPFPA_HAVE_CW 0
  345. # define XPFPA_CW_DATATYPE unsigned int
  346. # define XPFPA_STORE_CW(variable) /* NOP */
  347. # define XPFPA_RESTORE_CW(variable) /* NOP */
  348. # define XPFPA_SWITCH_DOUBLE() /* NOP */
  349. # define XPFPA_SWITCH_SINGLE() /* NOP */
  350. # define XPFPA_SWITCH_DOUBLE_EXTENDED() /* NOP */
  351. # define XPFPA_RESTORE() /* NOP */
  352. # define XPFPA_RETURN_DOUBLE(val) return (val)
  353. # define XPFPA_RETURN_SINGLE(val) return (val)
  354. # define XPFPA_RETURN_DOUBLE_EXTENDED(val) return (val)
  355. #endif /* FPU CONTROL */
  356. #endif