mathinline.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. /* Inline math functions for i387.
  2. Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004
  3. Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by John C. Bowman <bowman@math.ualberta.ca>, 1995.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, see
  16. <http://www.gnu.org/licenses/>. */
  17. #ifndef _MATH_H
  18. # error "Never use <bits/mathinline.h> directly; include <math.h> instead."
  19. #endif
  20. #ifdef __cplusplus
  21. # define __MATH_INLINE __inline
  22. #else
  23. # define __MATH_INLINE __extern_inline
  24. #endif
  25. #if defined __USE_ISOC99 && defined __GNUC__ && __GNUC__ >= 2
  26. /* GCC 2.97 and up have builtins that actually can be used. */
  27. # if !__GNUC_PREREQ (2,97)
  28. /* ISO C99 defines some macros to perform unordered comparisons. The
  29. ix87 FPU supports this with special opcodes and we should use them.
  30. These must not be inline functions since we have to be able to handle
  31. all floating-point types. */
  32. # undef isgreater
  33. # undef isgreaterequal
  34. # undef isless
  35. # undef islessequal
  36. # undef islessgreater
  37. # undef isunordered
  38. # ifdef __i686__
  39. /* For the PentiumPro and more recent processors we can provide
  40. better code. */
  41. # define isgreater(x, y) \
  42. ({ register char __result; \
  43. __asm__ ("fucomip %%st(1), %%st; seta %%al" \
  44. : "=a" (__result) : "u" (y), "t" (x) : "cc", "st"); \
  45. __result; })
  46. # define isgreaterequal(x, y) \
  47. ({ register char __result; \
  48. __asm__ ("fucomip %%st(1), %%st; setae %%al" \
  49. : "=a" (__result) : "u" (y), "t" (x) : "cc", "st"); \
  50. __result; })
  51. # define isless(x, y) \
  52. ({ register char __result; \
  53. __asm__ ("fucomip %%st(1), %%st; seta %%al" \
  54. : "=a" (__result) : "u" (x), "t" (y) : "cc", "st"); \
  55. __result; })
  56. # define islessequal(x, y) \
  57. ({ register char __result; \
  58. __asm__ ("fucomip %%st(1), %%st; setae %%al" \
  59. : "=a" (__result) : "u" (x), "t" (y) : "cc", "st"); \
  60. __result; })
  61. # define islessgreater(x, y) \
  62. ({ register char __result; \
  63. __asm__ ("fucomip %%st(1), %%st; setne %%al" \
  64. : "=a" (__result) : "u" (y), "t" (x) : "cc", "st"); \
  65. __result; })
  66. # define isunordered(x, y) \
  67. ({ register char __result; \
  68. __asm__ ("fucomip %%st(1), %%st; setp %%al" \
  69. : "=a" (__result) : "u" (y), "t" (x) : "cc", "st"); \
  70. __result; })
  71. # else
  72. /* This is the dumb, portable code for i386 and above. */
  73. # define isgreater(x, y) \
  74. ({ register char __result; \
  75. __asm__ ("fucompp; fnstsw; testb $0x45, %%ah; setz %%al" \
  76. : "=a" (__result) : "u" (y), "t" (x) : "cc", "st", "st(1)"); \
  77. __result; })
  78. # define isgreaterequal(x, y) \
  79. ({ register char __result; \
  80. __asm__ ("fucompp; fnstsw; testb $0x05, %%ah; setz %%al" \
  81. : "=a" (__result) : "u" (y), "t" (x) : "cc", "st", "st(1)"); \
  82. __result; })
  83. # define isless(x, y) \
  84. ({ register char __result; \
  85. __asm__ ("fucompp; fnstsw; testb $0x45, %%ah; setz %%al" \
  86. : "=a" (__result) : "u" (x), "t" (y) : "cc", "st", "st(1)"); \
  87. __result; })
  88. # define islessequal(x, y) \
  89. ({ register char __result; \
  90. __asm__ ("fucompp; fnstsw; testb $0x05, %%ah; setz %%al" \
  91. : "=a" (__result) : "u" (x), "t" (y) : "cc", "st", "st(1)"); \
  92. __result; })
  93. # define islessgreater(x, y) \
  94. ({ register char __result; \
  95. __asm__ ("fucompp; fnstsw; testb $0x44, %%ah; setz %%al" \
  96. : "=a" (__result) : "u" (y), "t" (x) : "cc", "st", "st(1)"); \
  97. __result; })
  98. # define isunordered(x, y) \
  99. ({ register char __result; \
  100. __asm__ ("fucompp; fnstsw; sahf; setp %%al" \
  101. : "=a" (__result) : "u" (y), "t" (x) : "cc", "st", "st(1)"); \
  102. __result; })
  103. # endif /* __i686__ */
  104. # endif /* GCC 2.97 */
  105. /* The gcc, version 2.7 or below, has problems with all this inlining
  106. code. So disable it for this version of the compiler. */
  107. # if __GNUC_PREREQ (2, 8)
  108. /* Test for negative number. Used in the signbit() macro. */
  109. __MATH_INLINE int
  110. __NTH (__signbitf (float __x))
  111. {
  112. __extension__ union { float __f; int __i; } __u = { __f: __x };
  113. return __u.__i < 0;
  114. }
  115. __MATH_INLINE int
  116. __NTH (__signbit (double __x))
  117. {
  118. __extension__ union { double __d; int __i[2]; } __u = { __d: __x };
  119. return __u.__i[1] < 0;
  120. }
  121. __MATH_INLINE int
  122. __NTH (__signbitl (long double __x))
  123. {
  124. __extension__ union { long double __l; int __i[3]; } __u = { __l: __x };
  125. return (__u.__i[2] & 0x8000) != 0;
  126. }
  127. # endif
  128. #endif
  129. /* The gcc, version 2.7 or below, has problems with all this inlining
  130. code. So disable it for this version of the compiler. */
  131. #if __GNUC_PREREQ (2, 8)
  132. #if ((!defined __NO_MATH_INLINES || defined __LIBC_INTERNAL_MATH_INLINES) \
  133. && defined __OPTIMIZE__)
  134. /* A macro to define float, double, and long double versions of various
  135. math functions for the ix87 FPU. FUNC is the function name (which will
  136. be suffixed with f and l for the float and long double version,
  137. respectively). OP is the name of the FPU operation.
  138. We define two sets of macros. The set with the additional NP
  139. doesn't add a prototype declaration. */
  140. #if defined __USE_MISC || defined __USE_ISOC99
  141. # define __inline_mathop(func, op) \
  142. __inline_mathop_ (double, func, op) \
  143. __inline_mathop_ (float, __CONCAT(func,f), op) \
  144. __inline_mathop_ (long double, __CONCAT(func,l), op)
  145. # define __inline_mathopNP(func, op) \
  146. __inline_mathopNP_ (double, func, op) \
  147. __inline_mathopNP_ (float, __CONCAT(func,f), op) \
  148. __inline_mathopNP_ (long double, __CONCAT(func,l), op)
  149. #else
  150. # define __inline_mathop(func, op) \
  151. __inline_mathop_ (double, func, op)
  152. # define __inline_mathopNP(func, op) \
  153. __inline_mathopNP_ (double, func, op)
  154. #endif
  155. #define __inline_mathop_(float_type, func, op) \
  156. __inline_mathop_decl_ (float_type, func, op, "0" (__x))
  157. #define __inline_mathopNP_(float_type, func, op) \
  158. __inline_mathop_declNP_ (float_type, func, op, "0" (__x))
  159. #if defined __USE_MISC || defined __USE_ISOC99
  160. # define __inline_mathop_decl(func, op, params...) \
  161. __inline_mathop_decl_ (double, func, op, params) \
  162. __inline_mathop_decl_ (float, __CONCAT(func,f), op, params) \
  163. __inline_mathop_decl_ (long double, __CONCAT(func,l), op, params)
  164. # define __inline_mathop_declNP(func, op, params...) \
  165. __inline_mathop_declNP_ (double, func, op, params) \
  166. __inline_mathop_declNP_ (float, __CONCAT(func,f), op, params) \
  167. __inline_mathop_declNP_ (long double, __CONCAT(func,l), op, params)
  168. #else
  169. # define __inline_mathop_decl(func, op, params...) \
  170. __inline_mathop_decl_ (double, func, op, params)
  171. # define __inline_mathop_declNP(func, op, params...) \
  172. __inline_mathop_declNP_ (double, func, op, params)
  173. #endif
  174. #define __inline_mathop_decl_(float_type, func, op, params...) \
  175. __MATH_INLINE float_type func (float_type) __THROW; \
  176. __inline_mathop_declNP_ (float_type, func, op, params)
  177. #define __inline_mathop_declNP_(float_type, func, op, params...) \
  178. __MATH_INLINE float_type __NTH (func (float_type __x)) \
  179. { \
  180. register float_type __result; \
  181. __asm__ __volatile__ (op : "=t" (__result) : params); \
  182. return __result; \
  183. }
  184. #if defined __USE_MISC || defined __USE_ISOC99
  185. # define __inline_mathcode(func, arg, code) \
  186. __inline_mathcode_ (double, func, arg, code) \
  187. __inline_mathcode_ (float, __CONCAT(func,f), arg, code) \
  188. __inline_mathcode_ (long double, __CONCAT(func,l), arg, code)
  189. # define __inline_mathcodeNP(func, arg, code) \
  190. __inline_mathcodeNP_ (double, func, arg, code) \
  191. __inline_mathcodeNP_ (float, __CONCAT(func,f), arg, code) \
  192. __inline_mathcodeNP_ (long double, __CONCAT(func,l), arg, code)
  193. # define __inline_mathcode2(func, arg1, arg2, code) \
  194. __inline_mathcode2_ (double, func, arg1, arg2, code) \
  195. __inline_mathcode2_ (float, __CONCAT(func,f), arg1, arg2, code) \
  196. __inline_mathcode2_ (long double, __CONCAT(func,l), arg1, arg2, code)
  197. # define __inline_mathcodeNP2(func, arg1, arg2, code) \
  198. __inline_mathcodeNP2_ (double, func, arg1, arg2, code) \
  199. __inline_mathcodeNP2_ (float, __CONCAT(func,f), arg1, arg2, code) \
  200. __inline_mathcodeNP2_ (long double, __CONCAT(func,l), arg1, arg2, code)
  201. # define __inline_mathcode3(func, arg1, arg2, arg3, code) \
  202. __inline_mathcode3_ (double, func, arg1, arg2, arg3, code) \
  203. __inline_mathcode3_ (float, __CONCAT(func,f), arg1, arg2, arg3, code) \
  204. __inline_mathcode3_ (long double, __CONCAT(func,l), arg1, arg2, arg3, code)
  205. # define __inline_mathcodeNP3(func, arg1, arg2, arg3, code) \
  206. __inline_mathcodeNP3_ (double, func, arg1, arg2, arg3, code) \
  207. __inline_mathcodeNP3_ (float, __CONCAT(func,f), arg1, arg2, arg3, code) \
  208. __inline_mathcodeNP3_ (long double, __CONCAT(func,l), arg1, arg2, arg3, code)
  209. #else
  210. # define __inline_mathcode(func, arg, code) \
  211. __inline_mathcode_ (double, func, (arg), code)
  212. # define __inline_mathcodeNP(func, arg, code) \
  213. __inline_mathcodeNP_ (double, func, (arg), code)
  214. # define __inline_mathcode2(func, arg1, arg2, code) \
  215. __inline_mathcode2_ (double, func, arg1, arg2, code)
  216. # define __inline_mathcodeNP2(func, arg1, arg2, code) \
  217. __inline_mathcodeNP2_ (double, func, arg1, arg2, code)
  218. # define __inline_mathcode3(func, arg1, arg2, arg3, code) \
  219. __inline_mathcode3_ (double, func, arg1, arg2, arg3, code)
  220. # define __inline_mathcodeNP3(func, arg1, arg2, arg3, code) \
  221. __inline_mathcodeNP3_ (double, func, arg1, arg2, arg3, code)
  222. #endif
  223. #define __inline_mathcode_(float_type, func, arg, code) \
  224. __MATH_INLINE float_type func (float_type) __THROW; \
  225. __inline_mathcodeNP_(float_type, func, arg, code)
  226. #define __inline_mathcodeNP_(float_type, func, arg, code) \
  227. __MATH_INLINE float_type __NTH (func (float_type arg)) \
  228. { \
  229. code; \
  230. }
  231. #define __inline_mathcode2_(float_type, func, arg1, arg2, code) \
  232. __MATH_INLINE float_type func (float_type, float_type) __THROW; \
  233. __inline_mathcodeNP2_ (float_type, func, arg1, arg2, code)
  234. #define __inline_mathcodeNP2_(float_type, func, arg1, arg2, code) \
  235. __MATH_INLINE float_type __NTH (func (float_type arg1, float_type arg2)) \
  236. { \
  237. code; \
  238. }
  239. #define __inline_mathcode3_(float_type, func, arg1, arg2, arg3, code) \
  240. __MATH_INLINE float_type func (float_type, float_type, float_type) __THROW; \
  241. __inline_mathcodeNP3_(float_type, func, arg1, arg2, arg3, code)
  242. #define __inline_mathcodeNP3_(float_type, func, arg1, arg2, arg3, code) \
  243. __MATH_INLINE float_type __NTH (func (float_type arg1, float_type arg2, \
  244. float_type arg3)) \
  245. { \
  246. code; \
  247. }
  248. #endif
  249. #if !defined __NO_MATH_INLINES && defined __OPTIMIZE__
  250. /* Miscellaneous functions */
  251. __inline_mathcode (__sgn, __x, \
  252. return __x == 0.0 ? 0.0 : (__x > 0.0 ? 1.0 : -1.0))
  253. /* __FAST_MATH__ is defined by gcc -ffast-math. */
  254. #ifdef __FAST_MATH__
  255. __inline_mathcode (__pow2, __x, \
  256. register long double __value; \
  257. register long double __exponent; \
  258. __extension__ long long int __p = (long long int) __x; \
  259. if (__x == (long double) __p) \
  260. { \
  261. __asm__ __volatile__ \
  262. ("fscale" \
  263. : "=t" (__value) : "0" (1.0), "u" (__x)); \
  264. return __value; \
  265. } \
  266. __asm__ __volatile__ \
  267. ("fld %%st(0)\n\t" \
  268. "frndint # int(x)\n\t" \
  269. "fxch\n\t" \
  270. "fsub %%st(1) # fract(x)\n\t" \
  271. "f2xm1 # 2^(fract(x)) - 1\n\t" \
  272. : "=t" (__value), "=u" (__exponent) : "0" (__x)); \
  273. __value += 1.0; \
  274. __asm__ __volatile__ \
  275. ("fscale" \
  276. : "=t" (__value) : "0" (__value), "u" (__exponent)); \
  277. return __value)
  278. # ifdef __USE_GNU
  279. # define __sincos_code \
  280. register long double __cosr; \
  281. register long double __sinr; \
  282. __asm__ __volatile__ \
  283. ("fsincos\n\t" \
  284. "fnstsw %%ax\n\t" \
  285. "testl $0x400, %%eax\n\t" \
  286. "jz 1f\n\t" \
  287. "fldpi\n\t" \
  288. "fadd %%st(0)\n\t" \
  289. "fxch %%st(1)\n\t" \
  290. "2: fprem1\n\t" \
  291. "fnstsw %%ax\n\t" \
  292. "testl $0x400, %%eax\n\t" \
  293. "jnz 2b\n\t" \
  294. "fstp %%st(1)\n\t" \
  295. "fsincos\n\t" \
  296. "1:" \
  297. : "=t" (__cosr), "=u" (__sinr) : "0" (__x)); \
  298. *__sinx = __sinr; \
  299. *__cosx = __cosr
  300. __MATH_INLINE void
  301. __NTH (__sincos (double __x, double *__sinx, double *__cosx))
  302. {
  303. __sincos_code;
  304. }
  305. __MATH_INLINE void
  306. __NTH (__sincosf (float __x, float *__sinx, float *__cosx))
  307. {
  308. __sincos_code;
  309. }
  310. __MATH_INLINE void
  311. __NTH (__sincosl (long double __x, long double *__sinx, long double *__cosx))
  312. {
  313. __sincos_code;
  314. }
  315. # endif
  316. /* Optimized inline implementation, sometimes with reduced precision
  317. and/or argument range. */
  318. # if __GNUC_PREREQ (3, 5)
  319. # define __expm1_code \
  320. register long double __temp; \
  321. __temp = __builtin_expm1l (__x); \
  322. return __temp ? __temp : __x
  323. # else
  324. # define __expm1_code \
  325. register long double __value; \
  326. register long double __exponent; \
  327. register long double __temp; \
  328. __asm__ __volatile__ \
  329. ("fldl2e # e^x - 1 = 2^(x * log2(e)) - 1\n\t" \
  330. "fmul %%st(1) # x * log2(e)\n\t" \
  331. "fst %%st(1)\n\t" \
  332. "frndint # int(x * log2(e))\n\t" \
  333. "fxch\n\t" \
  334. "fsub %%st(1) # fract(x * log2(e))\n\t" \
  335. "f2xm1 # 2^(fract(x * log2(e))) - 1\n\t" \
  336. "fscale # 2^(x * log2(e)) - 2^(int(x * log2(e)))\n\t" \
  337. : "=t" (__value), "=u" (__exponent) : "0" (__x)); \
  338. __asm__ __volatile__ \
  339. ("fscale # 2^int(x * log2(e))\n\t" \
  340. : "=t" (__temp) : "0" (1.0), "u" (__exponent)); \
  341. __temp -= 1.0; \
  342. __temp += __value; \
  343. return __temp ? __temp : __x
  344. # endif
  345. __inline_mathcodeNP_ (long double, __expm1l, __x, __expm1_code)
  346. # if __GNUC_PREREQ (3, 4)
  347. __inline_mathcodeNP_ (long double, __expl, __x, return __builtin_expl (__x))
  348. # else
  349. # define __exp_code \
  350. register long double __value; \
  351. register long double __exponent; \
  352. __asm__ __volatile__ \
  353. ("fldl2e # e^x = 2^(x * log2(e))\n\t" \
  354. "fmul %%st(1) # x * log2(e)\n\t" \
  355. "fst %%st(1)\n\t" \
  356. "frndint # int(x * log2(e))\n\t" \
  357. "fxch\n\t" \
  358. "fsub %%st(1) # fract(x * log2(e))\n\t" \
  359. "f2xm1 # 2^(fract(x * log2(e))) - 1\n\t" \
  360. : "=t" (__value), "=u" (__exponent) : "0" (__x)); \
  361. __value += 1.0; \
  362. __asm__ __volatile__ \
  363. ("fscale" \
  364. : "=t" (__value) : "0" (__value), "u" (__exponent)); \
  365. return __value
  366. __inline_mathcodeNP (exp, __x, __exp_code)
  367. __inline_mathcodeNP_ (long double, __expl, __x, __exp_code)
  368. # endif
  369. # if !__GNUC_PREREQ (3, 5)
  370. __inline_mathcodeNP (tan, __x, \
  371. register long double __value; \
  372. register long double __value2 __attribute__ ((__unused__)); \
  373. __asm__ __volatile__ \
  374. ("fptan" \
  375. : "=t" (__value2), "=u" (__value) : "0" (__x)); \
  376. return __value)
  377. # endif
  378. #endif /* __FAST_MATH__ */
  379. #if __GNUC_PREREQ (3, 4)
  380. __inline_mathcodeNP2_ (long double, __atan2l, __y, __x,
  381. return __builtin_atan2l (__y, __x))
  382. #else
  383. # define __atan2_code \
  384. register long double __value; \
  385. __asm__ __volatile__ \
  386. ("fpatan" \
  387. : "=t" (__value) : "0" (__x), "u" (__y) : "st(1)"); \
  388. return __value
  389. # ifdef __FAST_MATH__
  390. __inline_mathcodeNP2 (atan2, __y, __x, __atan2_code)
  391. # endif
  392. __inline_mathcodeNP2_ (long double, __atan2l, __y, __x, __atan2_code)
  393. #endif
  394. #if defined __FAST_MATH__ && !__GNUC_PREREQ (3, 5)
  395. __inline_mathcodeNP2 (fmod, __x, __y, \
  396. register long double __value; \
  397. __asm__ __volatile__ \
  398. ("1: fprem\n\t" \
  399. "fnstsw %%ax\n\t" \
  400. "sahf\n\t" \
  401. "jp 1b" \
  402. : "=t" (__value) : "0" (__x), "u" (__y) : "ax", "cc"); \
  403. return __value)
  404. #endif
  405. #ifdef __FAST_MATH__
  406. # if !__GNUC_PREREQ (3,3)
  407. __inline_mathopNP (sqrt, "fsqrt")
  408. __inline_mathopNP_ (long double, __sqrtl, "fsqrt")
  409. # define __libc_sqrtl(n) __sqrtl (n)
  410. # else
  411. # define __libc_sqrtl(n) __builtin_sqrtl (n)
  412. # endif
  413. #endif
  414. #if __GNUC_PREREQ (2, 8)
  415. __inline_mathcodeNP_ (double, fabs, __x, return __builtin_fabs (__x))
  416. # if defined __USE_MISC || defined __USE_ISOC99
  417. __inline_mathcodeNP_ (float, fabsf, __x, return __builtin_fabsf (__x))
  418. __inline_mathcodeNP_ (long double, fabsl, __x, return __builtin_fabsl (__x))
  419. # endif
  420. __inline_mathcodeNP_ (long double, __fabsl, __x, return __builtin_fabsl (__x))
  421. #else
  422. __inline_mathop (fabs, "fabs")
  423. __inline_mathop_ (long double, __fabsl, "fabs")
  424. #endif
  425. #ifdef __FAST_MATH__
  426. # if !__GNUC_PREREQ (3, 4)
  427. /* The argument range of this inline version is reduced. */
  428. __inline_mathopNP (sin, "fsin")
  429. /* The argument range of this inline version is reduced. */
  430. __inline_mathopNP (cos, "fcos")
  431. __inline_mathop_declNP (log, "fldln2; fxch; fyl2x", "0" (__x) : "st(1)")
  432. # endif
  433. # if !__GNUC_PREREQ (3, 5)
  434. __inline_mathop_declNP (log10, "fldlg2; fxch; fyl2x", "0" (__x) : "st(1)")
  435. __inline_mathcodeNP (asin, __x, return __atan2l (__x, __libc_sqrtl (1.0 - __x * __x)))
  436. __inline_mathcodeNP (acos, __x, return __atan2l (__libc_sqrtl (1.0 - __x * __x), __x))
  437. # endif
  438. # if !__GNUC_PREREQ (3, 4)
  439. __inline_mathop_declNP (atan, "fld1; fpatan", "0" (__x) : "st(1)")
  440. # endif
  441. #endif /* __FAST_MATH__ */
  442. __inline_mathcode_ (long double, __sgn1l, __x, \
  443. __extension__ union { long double __xld; unsigned int __xi[3]; } __n = \
  444. { __xld: __x }; \
  445. __n.__xi[2] = (__n.__xi[2] & 0x8000) | 0x3fff; \
  446. __n.__xi[1] = 0x80000000; \
  447. __n.__xi[0] = 0; \
  448. return __n.__xld)
  449. #ifdef __FAST_MATH__
  450. /* The argument range of the inline version of sinhl is slightly reduced. */
  451. __inline_mathcodeNP (sinh, __x, \
  452. register long double __exm1 = __expm1l (__fabsl (__x)); \
  453. return 0.5 * (__exm1 / (__exm1 + 1.0) + __exm1) * __sgn1l (__x))
  454. __inline_mathcodeNP (cosh, __x, \
  455. register long double __ex = __expl (__x); \
  456. return 0.5 * (__ex + 1.0 / __ex))
  457. __inline_mathcodeNP (tanh, __x, \
  458. register long double __exm1 = __expm1l (-__fabsl (__x + __x)); \
  459. return __exm1 / (__exm1 + 2.0) * __sgn1l (-__x))
  460. #endif
  461. __inline_mathcodeNP (floor, __x, \
  462. register long double __value; \
  463. __volatile__ unsigned short int __cw; \
  464. __volatile__ unsigned short int __cwtmp; \
  465. __asm__ __volatile__ ("fnstcw %0" : "=m" (__cw)); \
  466. __cwtmp = (__cw & 0xf3ff) | 0x0400; /* rounding down */ \
  467. __asm__ __volatile__ ("fldcw %0" : : "m" (__cwtmp)); \
  468. __asm__ __volatile__ ("frndint" : "=t" (__value) : "0" (__x)); \
  469. __asm__ __volatile__ ("fldcw %0" : : "m" (__cw)); \
  470. return __value)
  471. __inline_mathcodeNP (ceil, __x, \
  472. register long double __value; \
  473. __volatile__ unsigned short int __cw; \
  474. __volatile__ unsigned short int __cwtmp; \
  475. __asm__ __volatile__ ("fnstcw %0" : "=m" (__cw)); \
  476. __cwtmp = (__cw & 0xf3ff) | 0x0800; /* rounding up */ \
  477. __asm__ __volatile__ ("fldcw %0" : : "m" (__cwtmp)); \
  478. __asm__ __volatile__ ("frndint" : "=t" (__value) : "0" (__x)); \
  479. __asm__ __volatile__ ("fldcw %0" : : "m" (__cw)); \
  480. return __value)
  481. #ifdef __FAST_MATH__
  482. # define __ldexp_code \
  483. register long double __value; \
  484. __asm__ __volatile__ \
  485. ("fscale" \
  486. : "=t" (__value) : "0" (__x), "u" ((long double) __y)); \
  487. return __value
  488. __MATH_INLINE double
  489. __NTH (ldexp (double __x, int __y))
  490. {
  491. __ldexp_code;
  492. }
  493. #endif
  494. /* Optimized versions for some non-standardized functions. */
  495. #if defined __USE_ISOC99 || defined __USE_MISC
  496. # ifdef __FAST_MATH__
  497. __inline_mathcodeNP (expm1, __x, __expm1_code)
  498. /* We cannot rely on M_SQRT being defined. So we do it for ourself
  499. here. */
  500. # define __M_SQRT2 1.41421356237309504880L /* sqrt(2) */
  501. # if !__GNUC_PREREQ (3, 5)
  502. __inline_mathcodeNP (log1p, __x, \
  503. register long double __value; \
  504. if (__fabsl (__x) >= 1.0 - 0.5 * __M_SQRT2) \
  505. __value = logl (1.0 + __x); \
  506. else \
  507. __asm__ __volatile__ \
  508. ("fldln2\n\t" \
  509. "fxch\n\t" \
  510. "fyl2xp1" \
  511. : "=t" (__value) : "0" (__x) : "st(1)"); \
  512. return __value)
  513. # endif
  514. /* The argument range of the inline version of asinhl is slightly reduced. */
  515. __inline_mathcodeNP (asinh, __x, \
  516. register long double __y = __fabsl (__x); \
  517. return (log1pl (__y * __y / (__libc_sqrtl (__y * __y + 1.0) + 1.0) + __y) \
  518. * __sgn1l (__x)))
  519. __inline_mathcodeNP (acosh, __x, \
  520. return logl (__x + __libc_sqrtl (__x - 1.0) * __libc_sqrtl (__x + 1.0)))
  521. __inline_mathcodeNP (atanh, __x, \
  522. register long double __y = __fabsl (__x); \
  523. return -0.5 * log1pl (-(__y + __y) / (1.0 + __y)) * __sgn1l (__x))
  524. /* The argument range of the inline version of hypotl is slightly reduced. */
  525. __inline_mathcodeNP2 (hypot, __x, __y,
  526. return __libc_sqrtl (__x * __x + __y * __y))
  527. # if !__GNUC_PREREQ (3, 5)
  528. __inline_mathcodeNP(logb, __x, \
  529. register long double __value; \
  530. register long double __junk; \
  531. __asm__ __volatile__ \
  532. ("fxtract\n\t" \
  533. : "=t" (__junk), "=u" (__value) : "0" (__x)); \
  534. return __value)
  535. # endif
  536. # endif
  537. #endif
  538. #ifdef __USE_ISOC99
  539. # ifdef __FAST_MATH__
  540. # if !__GNUC_PREREQ (3, 5)
  541. __inline_mathop_declNP (log2, "fld1; fxch; fyl2x", "0" (__x) : "st(1)")
  542. # endif
  543. __MATH_INLINE float
  544. __NTH (ldexpf (float __x, int __y))
  545. {
  546. __ldexp_code;
  547. }
  548. __MATH_INLINE long double
  549. __NTH (ldexpl (long double __x, int __y))
  550. {
  551. __ldexp_code;
  552. }
  553. __inline_mathcodeNP3 (fma, __x, __y, __z, return (__x * __y) + __z)
  554. __inline_mathopNP (rint, "frndint")
  555. # endif /* __FAST_MATH__ */
  556. # define __lrint_code \
  557. long int __lrintres; \
  558. __asm__ __volatile__ \
  559. ("fistpl %0" \
  560. : "=m" (__lrintres) : "t" (__x) : "st"); \
  561. return __lrintres
  562. __MATH_INLINE long int
  563. __NTH (lrintf (float __x))
  564. {
  565. __lrint_code;
  566. }
  567. __MATH_INLINE long int
  568. __NTH (lrint (double __x))
  569. {
  570. __lrint_code;
  571. }
  572. __MATH_INLINE long int
  573. __NTH (lrintl (long double __x))
  574. {
  575. __lrint_code;
  576. }
  577. # undef __lrint_code
  578. # define __llrint_code \
  579. long long int __llrintres; \
  580. __asm__ __volatile__ \
  581. ("fistpll %0" \
  582. : "=m" (__llrintres) : "t" (__x) : "st"); \
  583. return __llrintres
  584. __MATH_INLINE long long int
  585. __NTH (llrintf (float __x))
  586. {
  587. __llrint_code;
  588. }
  589. __MATH_INLINE long long int
  590. __NTH (llrint (double __x))
  591. {
  592. __llrint_code;
  593. }
  594. __MATH_INLINE long long int
  595. __NTH (llrintl (long double __x))
  596. {
  597. __llrint_code;
  598. }
  599. # undef __llrint_code
  600. #endif
  601. #ifdef __USE_MISC
  602. # if defined __FAST_MATH__ && !__GNUC_PREREQ (3, 5)
  603. __inline_mathcodeNP2 (drem, __x, __y, \
  604. register double __value; \
  605. register int __clobbered; \
  606. __asm__ __volatile__ \
  607. ("1: fprem1\n\t" \
  608. "fstsw %%ax\n\t" \
  609. "sahf\n\t" \
  610. "jp 1b" \
  611. : "=t" (__value), "=&a" (__clobbered) : "0" (__x), "u" (__y) : "cc"); \
  612. return __value)
  613. # endif
  614. /* This function is used in the `isfinite' macro. */
  615. __MATH_INLINE int
  616. __NTH (__finite (double __x))
  617. {
  618. union { double __d; int __i[2]; } u;
  619. u.__d = __x;
  620. /* Finite numbers have at least one zero bit in exponent. */
  621. /* All other numbers will result in 0xffffffff after OR: */
  622. return (u.__i[1] | 0x800fffff) != 0xffffffff;
  623. }
  624. __MATH_INLINE int
  625. __NTH (__finitef (float __x))
  626. {
  627. union { float __d; int __i; } u;
  628. u.__d = __x;
  629. return (u.__i | 0x807fffff) != 0xffffffff;
  630. }
  631. /* Miscellaneous functions */
  632. # ifdef __FAST_MATH__
  633. __inline_mathcode (__coshm1, __x, \
  634. register long double __exm1 = __expm1l (__fabsl (__x)); \
  635. return 0.5 * (__exm1 / (__exm1 + 1.0)) * __exm1)
  636. __inline_mathcode (__acosh1p, __x, \
  637. return log1pl (__x + __libc_sqrtl (__x) * __libc_sqrtl (__x + 2.0)))
  638. # endif /* __FAST_MATH__ */
  639. #endif /* __USE_MISC */
  640. /* Undefine some of the large macros which are not used anymore. */
  641. #undef __atan2_code
  642. #ifdef __FAST_MATH__
  643. # undef __expm1_code
  644. # undef __exp_code
  645. # undef __sincos_code
  646. #endif /* __FAST_MATH__ */
  647. #endif /* __NO_MATH_INLINES */
  648. /* This code is used internally in the GNU libc. */
  649. #ifdef __LIBC_INTERNAL_MATH_INLINES
  650. __inline_mathop (__ieee754_sqrt, "fsqrt")
  651. __inline_mathcode2 (__ieee754_atan2, __y, __x,
  652. register long double __value;
  653. __asm__ __volatile__ ("fpatan\n\t"
  654. : "=t" (__value)
  655. : "0" (__x), "u" (__y) : "st(1)");
  656. return __value;)
  657. #endif
  658. #endif /* __GNUC__ */