zend_float.h 16 KB

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