mathinline.h 26 KB

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