mathinline.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /* Inline math functions for i387.
  2. Copyright (C) 1995,96,97,98,99,2000,2001 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by John C. Bowman <bowman@math.ualberta.ca>, 1995.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library 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. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  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. # if __GNUC_PREREQ (2,97)
  27. /* GCC 2.97 and up have builtins that actually can be used. */
  28. # define isgreater(x, y) __builtin_isgreater (x, y)
  29. # define isgreaterequal(x, y) __builtin_isgreaterequal (x, y)
  30. # define isless(x, y) __builtin_isless (x, y)
  31. # define islessequal(x, y) __builtin_islessequal (x, y)
  32. # define islessgreater(x, y) __builtin_islessgreater (x, y)
  33. # define isunordered(x, y) __builtin_isunordered (x, y)
  34. # else
  35. /* ISO C99 defines some macros to perform unordered comparisons. The
  36. ix87 FPU supports this with special opcodes and we should use them.
  37. These must not be inline functions since we have to be able to handle
  38. all floating-point types. */
  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. __signbitf (float __x) __THROW
  112. {
  113. __extension__ union { float __f; int __i; } __u = { __f: __x };
  114. return __u.__i < 0;
  115. }
  116. __MATH_INLINE int
  117. __signbit (double __x) __THROW
  118. {
  119. __extension__ union { double __d; int __i[2]; } __u = { __d: __x };
  120. return __u.__i[1] < 0;
  121. }
  122. __MATH_INLINE int
  123. __signbitl (long double __x) __THROW
  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 func (float_type __x) __THROW \
  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 func (float_type arg) __THROW \
  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 func (float_type arg1, float_type arg2) __THROW \
  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 func (float_type arg1, float_type arg2, \
  245. float_type arg3) __THROW \
  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. __sincos (double __x, double *__sinx, double *__cosx) __THROW
  303. {
  304. __sincos_code;
  305. }
  306. __MATH_INLINE void
  307. __sincosf (float __x, float *__sinx, float *__cosx) __THROW
  308. {
  309. __sincos_code;
  310. }
  311. __MATH_INLINE void
  312. __sincosl (long double __x, long double *__sinx, long double *__cosx) __THROW
  313. {
  314. __sincos_code;
  315. }
  316. # endif
  317. /* Optimized inline implementation, sometimes with reduced precision
  318. and/or argument range. */
  319. # define __expm1_code \
  320. register long double __value; \
  321. register long double __exponent; \
  322. register long double __temp; \
  323. __asm __volatile__ \
  324. ("fldl2e # e^x - 1 = 2^(x * log2(e)) - 1\n\t" \
  325. "fmul %%st(1) # x * log2(e)\n\t" \
  326. "fst %%st(1)\n\t" \
  327. "frndint # int(x * log2(e))\n\t" \
  328. "fxch\n\t" \
  329. "fsub %%st(1) # fract(x * log2(e))\n\t" \
  330. "f2xm1 # 2^(fract(x * log2(e))) - 1\n\t" \
  331. "fscale # 2^(x * log2(e)) - 2^(int(x * log2(e)))\n\t" \
  332. : "=t" (__value), "=u" (__exponent) : "0" (__x)); \
  333. __asm __volatile__ \
  334. ("fscale # 2^int(x * log2(e))\n\t" \
  335. : "=t" (__temp) : "0" (1.0), "u" (__exponent)); \
  336. __temp -= 1.0; \
  337. return __temp + __value ? __temp + __value : __x
  338. __inline_mathcodeNP_ (long double, __expm1l, __x, __expm1_code)
  339. # define __exp_code \
  340. register long double __value; \
  341. register long double __exponent; \
  342. __asm __volatile__ \
  343. ("fldl2e # e^x = 2^(x * log2(e))\n\t" \
  344. "fmul %%st(1) # x * log2(e)\n\t" \
  345. "fst %%st(1)\n\t" \
  346. "frndint # int(x * log2(e))\n\t" \
  347. "fxch\n\t" \
  348. "fsub %%st(1) # fract(x * log2(e))\n\t" \
  349. "f2xm1 # 2^(fract(x * log2(e))) - 1\n\t" \
  350. : "=t" (__value), "=u" (__exponent) : "0" (__x)); \
  351. __value += 1.0; \
  352. __asm __volatile__ \
  353. ("fscale" \
  354. : "=t" (__value) : "0" (__value), "u" (__exponent)); \
  355. return __value
  356. __inline_mathcodeNP (exp, __x, __exp_code)
  357. __inline_mathcodeNP_ (long double, __expl, __x, __exp_code)
  358. __inline_mathcodeNP (tan, __x, \
  359. register long double __value; \
  360. register long double __value2 __attribute_used__; \
  361. __asm __volatile__ \
  362. ("fptan" \
  363. : "=t" (__value2), "=u" (__value) : "0" (__x)); \
  364. return __value)
  365. #endif /* __FAST_MATH__ */
  366. #define __atan2_code \
  367. register long double __value; \
  368. __asm __volatile__ \
  369. ("fpatan" \
  370. : "=t" (__value) : "0" (__x), "u" (__y) : "st(1)"); \
  371. return __value
  372. __inline_mathcodeNP2 (atan2, __y, __x, __atan2_code)
  373. __inline_mathcodeNP2_ (long double, __atan2l, __y, __x, __atan2_code)
  374. __inline_mathcodeNP2 (fmod, __x, __y, \
  375. register long double __value; \
  376. __asm __volatile__ \
  377. ("1: fprem\n\t" \
  378. "fnstsw %%ax\n\t" \
  379. "sahf\n\t" \
  380. "jp 1b" \
  381. : "=t" (__value) : "0" (__x), "u" (__y) : "ax", "cc"); \
  382. return __value)
  383. __inline_mathopNP (sqrt, "fsqrt")
  384. __inline_mathopNP_ (long double, __sqrtl, "fsqrt")
  385. #if __GNUC_PREREQ (2, 8)
  386. __inline_mathcodeNP_ (double, fabs, __x, return __builtin_fabs (__x))
  387. __inline_mathcodeNP_ (float, fabsf, __x, return __builtin_fabsf (__x))
  388. __inline_mathcodeNP_ (long double, fabsl, __x, return __builtin_fabsl (__x))
  389. __inline_mathcodeNP_ (long double, __fabsl, __x, return __builtin_fabsl (__x))
  390. #else
  391. __inline_mathop (fabs, "fabs")
  392. __inline_mathop_ (long double, __fabsl, "fabs")
  393. #endif
  394. #ifdef __FAST_MATH__
  395. /* The argument range of this inline version is reduced. */
  396. __inline_mathopNP (sin, "fsin")
  397. /* The argument range of this inline version is reduced. */
  398. __inline_mathopNP (cos, "fcos")
  399. __inline_mathop_declNP (log, "fldln2; fxch; fyl2x", "0" (__x) : "st(1)")
  400. __inline_mathop_declNP (log10, "fldlg2; fxch; fyl2x", "0" (__x) : "st(1)")
  401. __inline_mathcodeNP (asin, __x, return __atan2l (__x, __sqrtl (1.0 - __x * __x)))
  402. __inline_mathcodeNP (acos, __x, return __atan2l (__sqrtl (1.0 - __x * __x), __x))
  403. #endif /* __FAST_MATH__ */
  404. __inline_mathop_declNP (atan, "fld1; fpatan", "0" (__x) : "st(1)")
  405. __inline_mathcode_ (long double, __sgn1l, __x, \
  406. __extension__ union { long double __xld; unsigned int __xi[3]; } __n = \
  407. { __xld: __x }; \
  408. __n.__xi[2] = (__n.__xi[2] & 0x8000) | 0x3fff; \
  409. __n.__xi[1] = 0x80000000; \
  410. __n.__xi[0] = 0; \
  411. return __n.__xld)
  412. #ifdef __FAST_MATH__
  413. /* The argument range of the inline version of sinhl is slightly reduced. */
  414. __inline_mathcodeNP (sinh, __x, \
  415. register long double __exm1 = __expm1l (__fabsl (__x)); \
  416. return 0.5 * (__exm1 / (__exm1 + 1.0) + __exm1) * __sgn1l (__x))
  417. __inline_mathcodeNP (cosh, __x, \
  418. register long double __ex = __expl (__x); \
  419. return 0.5 * (__ex + 1.0 / __ex))
  420. __inline_mathcodeNP (tanh, __x, \
  421. register long double __exm1 = __expm1l (-__fabsl (__x + __x)); \
  422. return __exm1 / (__exm1 + 2.0) * __sgn1l (-__x))
  423. #endif
  424. __inline_mathcodeNP (floor, __x, \
  425. register long double __value; \
  426. __volatile unsigned short int __cw; \
  427. __volatile unsigned short int __cwtmp; \
  428. __asm __volatile ("fnstcw %0" : "=m" (__cw)); \
  429. __cwtmp = (__cw & 0xf3ff) | 0x0400; /* rounding down */ \
  430. __asm __volatile ("fldcw %0" : : "m" (__cwtmp)); \
  431. __asm __volatile ("frndint" : "=t" (__value) : "0" (__x)); \
  432. __asm __volatile ("fldcw %0" : : "m" (__cw)); \
  433. return __value)
  434. __inline_mathcodeNP (ceil, __x, \
  435. register long double __value; \
  436. __volatile unsigned short int __cw; \
  437. __volatile unsigned short int __cwtmp; \
  438. __asm __volatile ("fnstcw %0" : "=m" (__cw)); \
  439. __cwtmp = (__cw & 0xf3ff) | 0x0800; /* rounding up */ \
  440. __asm __volatile ("fldcw %0" : : "m" (__cwtmp)); \
  441. __asm __volatile ("frndint" : "=t" (__value) : "0" (__x)); \
  442. __asm __volatile ("fldcw %0" : : "m" (__cw)); \
  443. return __value)
  444. #define __ldexp_code \
  445. register long double __value; \
  446. __asm __volatile__ \
  447. ("fscale" \
  448. : "=t" (__value) : "0" (__x), "u" ((long double) __y)); \
  449. return __value
  450. __MATH_INLINE double
  451. ldexp (double __x, int __y) __THROW
  452. {
  453. __ldexp_code;
  454. }
  455. /* Optimized versions for some non-standardized functions. */
  456. #if defined __USE_ISOC99 || defined __USE_MISC
  457. # ifdef __FAST_MATH__
  458. __inline_mathcodeNP (expm1, __x, __expm1_code)
  459. # endif
  460. /* We cannot rely on M_SQRT being defined. So we do it for ourself
  461. here. */
  462. # define __M_SQRT2 1.41421356237309504880L /* sqrt(2) */
  463. __inline_mathcodeNP (log1p, __x, \
  464. register long double __value; \
  465. if (__fabsl (__x) >= 1.0 - 0.5 * __M_SQRT2) \
  466. __value = logl (1.0 + __x); \
  467. else \
  468. __asm __volatile__ \
  469. ("fldln2\n\t" \
  470. "fxch\n\t" \
  471. "fyl2xp1" \
  472. : "=t" (__value) : "0" (__x) : "st(1)"); \
  473. return __value)
  474. /* The argument range of the inline version of asinhl is slightly reduced. */
  475. __inline_mathcodeNP (asinh, __x, \
  476. register long double __y = __fabsl (__x); \
  477. return (log1pl (__y * __y / (__sqrtl (__y * __y + 1.0) + 1.0) + __y) \
  478. * __sgn1l (__x)))
  479. __inline_mathcodeNP (acosh, __x, \
  480. return logl (__x + __sqrtl (__x - 1.0) * __sqrtl (__x + 1.0)))
  481. __inline_mathcodeNP (atanh, __x, \
  482. register long double __y = __fabsl (__x); \
  483. return -0.5 * log1pl (-(__y + __y) / (1.0 + __y)) * __sgn1l (__x))
  484. /* The argument range of the inline version of hypotl is slightly reduced. */
  485. __inline_mathcodeNP2 (hypot, __x, __y, return __sqrtl (__x * __x + __y * __y))
  486. __inline_mathcodeNP(logb, __x, \
  487. register long double __value; \
  488. register long double __junk; \
  489. __asm __volatile__ \
  490. ("fxtract\n\t" \
  491. : "=t" (__junk), "=u" (__value) : "0" (__x)); \
  492. return __value)
  493. #endif
  494. #ifdef __USE_ISOC99
  495. #ifdef __FAST_MATH__
  496. __inline_mathop_declNP (log2, "fld1; fxch; fyl2x", "0" (__x) : "st(1)")
  497. #endif /* __FAST_MATH__ */
  498. __MATH_INLINE float
  499. ldexpf (float __x, int __y) __THROW
  500. {
  501. __ldexp_code;
  502. }
  503. __MATH_INLINE long double
  504. ldexpl (long double __x, int __y) __THROW
  505. {
  506. __ldexp_code;
  507. }
  508. #ifdef __FAST_MATH__
  509. __inline_mathcodeNP3 (fma, __x, __y, __z, return (__x * __y) + __z)
  510. __inline_mathopNP (rint, "frndint")
  511. #endif /* __FAST_MATH__ */
  512. #define __lrint_code \
  513. long int __lrintres; \
  514. __asm__ __volatile__ \
  515. ("fistpl %0" \
  516. : "=m" (__lrintres) : "t" (__x) : "st"); \
  517. return __lrintres
  518. __MATH_INLINE long int
  519. lrintf (float __x) __THROW
  520. {
  521. __lrint_code;
  522. }
  523. __MATH_INLINE long int
  524. lrint (double __x) __THROW
  525. {
  526. __lrint_code;
  527. }
  528. __MATH_INLINE long int
  529. lrintl (long double __x) __THROW
  530. {
  531. __lrint_code;
  532. }
  533. #undef __lrint_code
  534. #define __llrint_code \
  535. long long int __llrintres; \
  536. __asm__ __volatile__ \
  537. ("fistpll %0" \
  538. : "=m" (__llrintres) : "t" (__x) : "st"); \
  539. return __llrintres
  540. __MATH_INLINE long long int
  541. llrintf (float __x) __THROW
  542. {
  543. __llrint_code;
  544. }
  545. __MATH_INLINE long long int
  546. llrint (double __x) __THROW
  547. {
  548. __llrint_code;
  549. }
  550. __MATH_INLINE long long int
  551. llrintl (long double __x) __THROW
  552. {
  553. __llrint_code;
  554. }
  555. #undef __llrint_code
  556. #endif
  557. #ifdef __USE_MISC
  558. __inline_mathcodeNP2 (drem, __x, __y, \
  559. register double __value; \
  560. register int __clobbered; \
  561. __asm __volatile__ \
  562. ("1: fprem1\n\t" \
  563. "fstsw %%ax\n\t" \
  564. "sahf\n\t" \
  565. "jp 1b" \
  566. : "=t" (__value), "=&a" (__clobbered) : "0" (__x), "u" (__y) : "cc"); \
  567. return __value)
  568. /* This function is used in the `isfinite' macro. */
  569. __MATH_INLINE int
  570. __finite (double __x) __THROW
  571. {
  572. return (__extension__
  573. (((((union { double __d; int __i[2]; }) {__d: __x}).__i[1]
  574. | 0x800fffffu) + 1) >> 31));
  575. }
  576. /* Miscellaneous functions */
  577. #ifdef __FAST_MATH__
  578. __inline_mathcode (__coshm1, __x, \
  579. register long double __exm1 = __expm1l (__fabsl (__x)); \
  580. return 0.5 * (__exm1 / (__exm1 + 1.0)) * __exm1)
  581. __inline_mathcode (__acosh1p, __x, \
  582. return log1pl (__x + __sqrtl (__x) * __sqrtl (__x + 2.0)))
  583. #endif /* __FAST_MATH__ */
  584. #endif /* __USE_MISC */
  585. /* Undefine some of the large macros which are not used anymore. */
  586. #undef __atan2_code
  587. #ifdef __FAST_MATH__
  588. # undef __expm1_code
  589. # undef __exp_code
  590. # undef __sincos_code
  591. #endif /* __FAST_MATH__ */
  592. #endif /* __NO_MATH_INLINES */
  593. /* This code is used internally in the GNU libc. */
  594. #ifdef __LIBC_INTERNAL_MATH_INLINES
  595. __inline_mathop (__ieee754_sqrt, "fsqrt")
  596. __inline_mathcode2 (__ieee754_atan2, __y, __x,
  597. register long double __value;
  598. __asm __volatile__ ("fpatan\n\t"
  599. : "=t" (__value)
  600. : "0" (__x), "u" (__y) : "st(1)");
  601. return __value;)
  602. #endif
  603. #endif /* __GNUC__ */