quadmath-imp.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /* GCC Quad-Precision Math Library
  2. Copyright (C) 2010, 2011 Free Software Foundation, Inc.
  3. Written by Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>
  4. This file is part of the libquadmath library.
  5. Libquadmath is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. Libquadmath is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with libquadmath; see the file COPYING.LIB. If
  15. not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  16. Boston, MA 02110-1301, USA. */
  17. #ifndef QUADMATH_IMP_H
  18. #define QUADMATH_IMP_H
  19. #include <errno.h>
  20. #include <limits.h>
  21. #include <stdbool.h>
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include "quadmath.h"
  25. #include "config.h"
  26. #ifdef HAVE_FENV_H
  27. # include <fenv.h>
  28. #endif
  29. /* Under IEEE 754, an architecture may determine tininess of
  30. floating-point results either "before rounding" or "after
  31. rounding", but must do so in the same way for all operations
  32. returning binary results. Define TININESS_AFTER_ROUNDING to 1 for
  33. "after rounding" architectures, 0 for "before rounding"
  34. architectures. */
  35. #define TININESS_AFTER_ROUNDING 1
  36. #define HIGH_ORDER_BIT_IS_SET_FOR_SNAN 0
  37. #define FIX_FLT128_LONG_CONVERT_OVERFLOW 0
  38. #define FIX_FLT128_LLONG_CONVERT_OVERFLOW 0
  39. /* Prototypes for internal functions. */
  40. extern int32_t __quadmath_rem_pio2q (__float128, __float128 *);
  41. extern void __quadmath_kernel_sincosq (__float128, __float128, __float128 *,
  42. __float128 *, int);
  43. extern __float128 __quadmath_kernel_sinq (__float128, __float128, int);
  44. extern __float128 __quadmath_kernel_cosq (__float128, __float128);
  45. extern __float128 __quadmath_kernel_tanq (__float128, __float128, int);
  46. extern __float128 __quadmath_gamma_productq (__float128, __float128, int,
  47. __float128 *);
  48. extern __float128 __quadmath_gammaq_r (__float128, int *);
  49. extern __float128 __quadmath_lgamma_negq (__float128, int *);
  50. extern __float128 __quadmath_lgamma_productq (__float128, __float128,
  51. __float128, int);
  52. extern __float128 __quadmath_lgammaq_r (__float128, int *);
  53. extern __float128 __quadmath_x2y2m1q (__float128 x, __float128 y);
  54. extern __complex128 __quadmath_kernel_casinhq (__complex128, int);
  55. static inline void
  56. mul_splitq (__float128 *hi, __float128 *lo, __float128 x, __float128 y)
  57. {
  58. /* Fast built-in fused multiply-add. */
  59. *hi = x * y;
  60. *lo = fmaq (x, y, -*hi);
  61. }
  62. /* Frankly, if you have __float128, you have 64-bit integers, right? */
  63. #ifndef UINT64_C
  64. # error "No way!"
  65. #endif
  66. /* Main union type we use to manipulate the floating-point type. */
  67. typedef union
  68. {
  69. __float128 value;
  70. struct
  71. #ifdef __MINGW32__
  72. /* On mingw targets the ms-bitfields option is active by default.
  73. Therefore enforce gnu-bitfield style. */
  74. __attribute__ ((gcc_struct))
  75. #endif
  76. {
  77. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  78. unsigned negative:1;
  79. unsigned exponent:15;
  80. unsigned mantissa0:16;
  81. unsigned mantissa1:32;
  82. unsigned mantissa2:32;
  83. unsigned mantissa3:32;
  84. #else
  85. unsigned mantissa3:32;
  86. unsigned mantissa2:32;
  87. unsigned mantissa1:32;
  88. unsigned mantissa0:16;
  89. unsigned exponent:15;
  90. unsigned negative:1;
  91. #endif
  92. } ieee;
  93. struct
  94. {
  95. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  96. uint64_t high;
  97. uint64_t low;
  98. #else
  99. uint64_t low;
  100. uint64_t high;
  101. #endif
  102. } words64;
  103. struct
  104. {
  105. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  106. uint32_t w0;
  107. uint32_t w1;
  108. uint32_t w2;
  109. uint32_t w3;
  110. #else
  111. uint32_t w3;
  112. uint32_t w2;
  113. uint32_t w1;
  114. uint32_t w0;
  115. #endif
  116. } words32;
  117. struct
  118. #ifdef __MINGW32__
  119. /* Make sure we are using gnu-style bitfield handling. */
  120. __attribute__ ((gcc_struct))
  121. #endif
  122. {
  123. #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  124. unsigned negative:1;
  125. unsigned exponent:15;
  126. unsigned quiet_nan:1;
  127. unsigned mantissa0:15;
  128. unsigned mantissa1:32;
  129. unsigned mantissa2:32;
  130. unsigned mantissa3:32;
  131. #else
  132. unsigned mantissa3:32;
  133. unsigned mantissa2:32;
  134. unsigned mantissa1:32;
  135. unsigned mantissa0:15;
  136. unsigned quiet_nan:1;
  137. unsigned exponent:15;
  138. unsigned negative:1;
  139. #endif
  140. } ieee_nan;
  141. } ieee854_float128;
  142. /* Get two 64 bit ints from a long double. */
  143. #define GET_FLT128_WORDS64(ix0,ix1,d) \
  144. do { \
  145. ieee854_float128 u; \
  146. u.value = (d); \
  147. (ix0) = u.words64.high; \
  148. (ix1) = u.words64.low; \
  149. } while (0)
  150. /* Set a long double from two 64 bit ints. */
  151. #define SET_FLT128_WORDS64(d,ix0,ix1) \
  152. do { \
  153. ieee854_float128 u; \
  154. u.words64.high = (ix0); \
  155. u.words64.low = (ix1); \
  156. (d) = u.value; \
  157. } while (0)
  158. /* Get the more significant 64 bits of a long double mantissa. */
  159. #define GET_FLT128_MSW64(v,d) \
  160. do { \
  161. ieee854_float128 u; \
  162. u.value = (d); \
  163. (v) = u.words64.high; \
  164. } while (0)
  165. /* Set the more significant 64 bits of a long double mantissa from an int. */
  166. #define SET_FLT128_MSW64(d,v) \
  167. do { \
  168. ieee854_float128 u; \
  169. u.value = (d); \
  170. u.words64.high = (v); \
  171. (d) = u.value; \
  172. } while (0)
  173. /* Get the least significant 64 bits of a long double mantissa. */
  174. #define GET_FLT128_LSW64(v,d) \
  175. do { \
  176. ieee854_float128 u; \
  177. u.value = (d); \
  178. (v) = u.words64.low; \
  179. } while (0)
  180. #define IEEE854_FLOAT128_BIAS 0x3fff
  181. #define QUADFP_NAN 0
  182. #define QUADFP_INFINITE 1
  183. #define QUADFP_ZERO 2
  184. #define QUADFP_SUBNORMAL 3
  185. #define QUADFP_NORMAL 4
  186. #define fpclassifyq(x) \
  187. __builtin_fpclassify (QUADFP_NAN, QUADFP_INFINITE, QUADFP_NORMAL, \
  188. QUADFP_SUBNORMAL, QUADFP_ZERO, x)
  189. #ifndef math_opt_barrier
  190. # define math_opt_barrier(x) \
  191. ({ __typeof (x) __x = (x); __asm ("" : "+m" (__x)); __x; })
  192. # define math_force_eval(x) \
  193. ({ __typeof (x) __x = (x); __asm __volatile__ ("" : : "m" (__x)); })
  194. #endif
  195. /* math_narrow_eval reduces its floating-point argument to the range
  196. and precision of its semantic type. (The original evaluation may
  197. still occur with excess range and precision, so the result may be
  198. affected by double rounding.) */
  199. #define math_narrow_eval(x) (x)
  200. /* If X (which is not a NaN) is subnormal, force an underflow
  201. exception. */
  202. #define math_check_force_underflow(x) \
  203. do \
  204. { \
  205. __float128 force_underflow_tmp = (x); \
  206. if (fabsq (force_underflow_tmp) < FLT128_MIN) \
  207. { \
  208. __float128 force_underflow_tmp2 \
  209. = force_underflow_tmp * force_underflow_tmp; \
  210. math_force_eval (force_underflow_tmp2); \
  211. } \
  212. } \
  213. while (0)
  214. /* Likewise, but X is also known to be nonnegative. */
  215. #define math_check_force_underflow_nonneg(x) \
  216. do \
  217. { \
  218. __float128 force_underflow_tmp = (x); \
  219. if (force_underflow_tmp < FLT128_MIN) \
  220. { \
  221. __float128 force_underflow_tmp2 \
  222. = force_underflow_tmp * force_underflow_tmp; \
  223. math_force_eval (force_underflow_tmp2); \
  224. } \
  225. } \
  226. while (0)
  227. /* Likewise, for both real and imaginary parts of a complex
  228. result. */
  229. #define math_check_force_underflow_complex(x) \
  230. do \
  231. { \
  232. __typeof (x) force_underflow_complex_tmp = (x); \
  233. math_check_force_underflow (__real__ force_underflow_complex_tmp); \
  234. math_check_force_underflow (__imag__ force_underflow_complex_tmp); \
  235. } \
  236. while (0)
  237. #ifndef HAVE_FENV_H
  238. # define feraiseexcept(arg) ((void) 0)
  239. typedef int fenv_t;
  240. # define feholdexcept(arg) ((void) 0)
  241. # define fesetround(arg) ((void) 0)
  242. # define feupdateenv(arg) ((void) (arg))
  243. # define fesetenv(arg) ((void) (arg))
  244. # define fetestexcept(arg) 0
  245. # define feclearexcept(arg) ((void) 0)
  246. #else
  247. # ifndef HAVE_FEHOLDEXCEPT
  248. # define feholdexcept(arg) ((void) 0)
  249. # endif
  250. # ifndef HAVE_FESETROUND
  251. # define fesetround(arg) ((void) 0)
  252. # endif
  253. # ifndef HAVE_FEUPDATEENV
  254. # define feupdateenv(arg) ((void) (arg))
  255. # endif
  256. # ifndef HAVE_FESETENV
  257. # define fesetenv(arg) ((void) (arg))
  258. # endif
  259. # ifndef HAVE_FETESTEXCEPT
  260. # define fetestexcept(arg) 0
  261. # endif
  262. #endif
  263. #ifndef __glibc_likely
  264. # define __glibc_likely(cond) __builtin_expect ((cond), 1)
  265. #endif
  266. #ifndef __glibc_unlikely
  267. # define __glibc_unlikely(cond) __builtin_expect ((cond), 0)
  268. #endif
  269. #if defined HAVE_FENV_H && defined HAVE_FESETROUND && defined HAVE_FEUPDATEENV
  270. struct rm_ctx
  271. {
  272. fenv_t env;
  273. bool updated_status;
  274. };
  275. # define SET_RESTORE_ROUNDF128(RM) \
  276. struct rm_ctx ctx __attribute__((cleanup (libc_feresetround_ctx))); \
  277. libc_feholdsetround_ctx (&ctx, (RM))
  278. static inline __attribute__ ((always_inline)) void
  279. libc_feholdsetround_ctx (struct rm_ctx *ctx, int round)
  280. {
  281. ctx->updated_status = false;
  282. /* Update rounding mode only if different. */
  283. if (__glibc_unlikely (round != fegetround ()))
  284. {
  285. ctx->updated_status = true;
  286. fegetenv (&ctx->env);
  287. fesetround (round);
  288. }
  289. }
  290. static inline __attribute__ ((always_inline)) void
  291. libc_feresetround_ctx (struct rm_ctx *ctx)
  292. {
  293. /* Restore the rounding mode if updated. */
  294. if (__glibc_unlikely (ctx->updated_status))
  295. feupdateenv (&ctx->env);
  296. }
  297. #else
  298. # define SET_RESTORE_ROUNDF128(RM) ((void) 0)
  299. #endif
  300. #endif