You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

422 lines
15 KiB

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