mathinline.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /* Inline math functions for i387 and SSE.
  2. Copyright (C) 1995-2016 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef _MATH_H
  16. # error "Never use <bits/mathinline.h> directly; include <math.h> instead."
  17. #endif
  18. #ifndef __extern_always_inline
  19. # define __MATH_INLINE __inline
  20. #else
  21. # define __MATH_INLINE __extern_always_inline
  22. #endif
  23. #if defined __USE_ISOC99 && defined __GNUC__ && __GNUC__ >= 2
  24. /* GCC 2.97 and up have builtins that actually can be used. */
  25. # if !__GNUC_PREREQ (2,97)
  26. /* ISO C99 defines some macros to perform unordered comparisons. The
  27. ix87 FPU supports this with special opcodes and we should use them.
  28. These must not be inline functions since we have to be able to handle
  29. all floating-point types. */
  30. # undef isgreater
  31. # undef isgreaterequal
  32. # undef isless
  33. # undef islessequal
  34. # undef islessgreater
  35. # undef isunordered
  36. # ifdef __i686__
  37. /* For the PentiumPro and more recent processors we can provide
  38. better code. */
  39. # define isgreater(x, y) \
  40. ({ register char __result; \
  41. __asm__ ("fucomip %%st(1), %%st; seta %%al" \
  42. : "=a" (__result) : "u" (y), "t" (x) : "cc", "st"); \
  43. __result; })
  44. # define isgreaterequal(x, y) \
  45. ({ register char __result; \
  46. __asm__ ("fucomip %%st(1), %%st; setae %%al" \
  47. : "=a" (__result) : "u" (y), "t" (x) : "cc", "st"); \
  48. __result; })
  49. # define isless(x, y) \
  50. ({ register char __result; \
  51. __asm__ ("fucomip %%st(1), %%st; seta %%al" \
  52. : "=a" (__result) : "u" (x), "t" (y) : "cc", "st"); \
  53. __result; })
  54. # define islessequal(x, y) \
  55. ({ register char __result; \
  56. __asm__ ("fucomip %%st(1), %%st; setae %%al" \
  57. : "=a" (__result) : "u" (x), "t" (y) : "cc", "st"); \
  58. __result; })
  59. # define islessgreater(x, y) \
  60. ({ register char __result; \
  61. __asm__ ("fucomip %%st(1), %%st; setne %%al" \
  62. : "=a" (__result) : "u" (y), "t" (x) : "cc", "st"); \
  63. __result; })
  64. # define isunordered(x, y) \
  65. ({ register char __result; \
  66. __asm__ ("fucomip %%st(1), %%st; setp %%al" \
  67. : "=a" (__result) : "u" (y), "t" (x) : "cc", "st"); \
  68. __result; })
  69. # else
  70. /* This is the dumb, portable code for i386 and above. */
  71. # define isgreater(x, y) \
  72. ({ register char __result; \
  73. __asm__ ("fucompp; fnstsw; testb $0x45, %%ah; setz %%al" \
  74. : "=a" (__result) : "u" (y), "t" (x) : "cc", "st", "st(1)"); \
  75. __result; })
  76. # define isgreaterequal(x, y) \
  77. ({ register char __result; \
  78. __asm__ ("fucompp; fnstsw; testb $0x05, %%ah; setz %%al" \
  79. : "=a" (__result) : "u" (y), "t" (x) : "cc", "st", "st(1)"); \
  80. __result; })
  81. # define isless(x, y) \
  82. ({ register char __result; \
  83. __asm__ ("fucompp; fnstsw; testb $0x45, %%ah; setz %%al" \
  84. : "=a" (__result) : "u" (x), "t" (y) : "cc", "st", "st(1)"); \
  85. __result; })
  86. # define islessequal(x, y) \
  87. ({ register char __result; \
  88. __asm__ ("fucompp; fnstsw; testb $0x05, %%ah; setz %%al" \
  89. : "=a" (__result) : "u" (x), "t" (y) : "cc", "st", "st(1)"); \
  90. __result; })
  91. # define islessgreater(x, y) \
  92. ({ register char __result; \
  93. __asm__ ("fucompp; fnstsw; testb $0x44, %%ah; setz %%al" \
  94. : "=a" (__result) : "u" (y), "t" (x) : "cc", "st", "st(1)"); \
  95. __result; })
  96. # define isunordered(x, y) \
  97. ({ register char __result; \
  98. __asm__ ("fucompp; fnstsw; sahf; setp %%al" \
  99. : "=a" (__result) : "u" (y), "t" (x) : "cc", "st", "st(1)"); \
  100. __result; })
  101. # endif /* __i686__ */
  102. # endif /* GCC 2.97 */
  103. /* The gcc, version 2.7 or below, has problems with all this inlining
  104. code. So disable it for this version of the compiler. */
  105. # if __GNUC_PREREQ (2, 8)
  106. __BEGIN_NAMESPACE_C99
  107. /* Test for negative number. Used in the signbit() macro. */
  108. __MATH_INLINE int
  109. __NTH (__signbitf (float __x))
  110. {
  111. # ifdef __SSE2_MATH__
  112. int __m;
  113. __asm ("pmovmskb %1, %0" : "=r" (__m) : "x" (__x));
  114. return (__m & 0x8) != 0;
  115. # else
  116. __extension__ union { float __f; int __i; } __u = { __f: __x };
  117. return __u.__i < 0;
  118. # endif
  119. }
  120. __MATH_INLINE int
  121. __NTH (__signbit (double __x))
  122. {
  123. # ifdef __SSE2_MATH__
  124. int __m;
  125. __asm ("pmovmskb %1, %0" : "=r" (__m) : "x" (__x));
  126. return (__m & 0x80) != 0;
  127. # else
  128. __extension__ union { double __d; int __i[2]; } __u = { __d: __x };
  129. return __u.__i[1] < 0;
  130. # endif
  131. }
  132. __MATH_INLINE int
  133. __NTH (__signbitl (long double __x))
  134. {
  135. __extension__ union { long double __l; int __i[3]; } __u = { __l: __x };
  136. return (__u.__i[2] & 0x8000) != 0;
  137. }
  138. __END_NAMESPACE_C99
  139. # endif
  140. #endif
  141. /* The gcc, version 2.7 or below, has problems with all this inlining
  142. code. So disable it for this version of the compiler. */
  143. #if __GNUC_PREREQ (2, 8)
  144. # if !__GNUC_PREREQ (3, 4) && !defined __NO_MATH_INLINES \
  145. && defined __OPTIMIZE__
  146. /* GCC 3.4 introduced builtins for all functions below, so
  147. there's no need to define any of these inline functions. */
  148. # ifdef __USE_ISOC99
  149. __BEGIN_NAMESPACE_C99
  150. /* Round to nearest integer. */
  151. # ifdef __SSE_MATH__
  152. __MATH_INLINE long int
  153. __NTH (lrintf (float __x))
  154. {
  155. long int __res;
  156. /* Mark as volatile since the result is dependent on the state of
  157. the SSE control register (the rounding mode). Otherwise GCC might
  158. remove these assembler instructions since it does not know about
  159. the rounding mode change and cannot currently be told. */
  160. __asm __volatile__ ("cvtss2si %1, %0" : "=r" (__res) : "xm" (__x));
  161. return __res;
  162. }
  163. # endif
  164. # ifdef __SSE2_MATH__
  165. __MATH_INLINE long int
  166. __NTH (lrint (double __x))
  167. {
  168. long int __res;
  169. /* Mark as volatile since the result is dependent on the state of
  170. the SSE control register (the rounding mode). Otherwise GCC might
  171. remove these assembler instructions since it does not know about
  172. the rounding mode change and cannot currently be told. */
  173. __asm __volatile__ ("cvtsd2si %1, %0" : "=r" (__res) : "xm" (__x));
  174. return __res;
  175. }
  176. # endif
  177. # ifdef __x86_64__
  178. __extension__
  179. __MATH_INLINE long long int
  180. __NTH (llrintf (float __x))
  181. {
  182. long long int __res;
  183. /* Mark as volatile since the result is dependent on the state of
  184. the SSE control register (the rounding mode). Otherwise GCC might
  185. remove these assembler instructions since it does not know about
  186. the rounding mode change and cannot currently be told. */
  187. __asm __volatile__ ("cvtss2si %1, %0" : "=r" (__res) : "xm" (__x));
  188. return __res;
  189. }
  190. __extension__
  191. __MATH_INLINE long long int
  192. __NTH (llrint (double __x))
  193. {
  194. long long int __res;
  195. /* Mark as volatile since the result is dependent on the state of
  196. the SSE control register (the rounding mode). Otherwise GCC might
  197. remove these assembler instructions since it does not know about
  198. the rounding mode change and cannot currently be told. */
  199. __asm __volatile__ ("cvtsd2si %1, %0" : "=r" (__res) : "xm" (__x));
  200. return __res;
  201. }
  202. # endif
  203. # if defined __FINITE_MATH_ONLY__ && __FINITE_MATH_ONLY__ > 0 \
  204. && defined __SSE2_MATH__
  205. /* Determine maximum of two values. */
  206. __MATH_INLINE float
  207. __NTH (fmaxf (float __x, float __y))
  208. {
  209. # ifdef __AVX__
  210. float __res;
  211. __asm ("vmaxss %2, %1, %0" : "=x" (__res) : "x" (x), "xm" (__y));
  212. return __res;
  213. # else
  214. __asm ("maxss %1, %0" : "+x" (__x) : "xm" (__y));
  215. return __x;
  216. # endif
  217. }
  218. __MATH_INLINE double
  219. __NTH (fmax (double __x, double __y))
  220. {
  221. # ifdef __AVX__
  222. float __res;
  223. __asm ("vmaxsd %2, %1, %0" : "=x" (__res) : "x" (x), "xm" (__y));
  224. return __res;
  225. # else
  226. __asm ("maxsd %1, %0" : "+x" (__x) : "xm" (__y));
  227. return __x;
  228. # endif
  229. }
  230. /* Determine minimum of two values. */
  231. __MATH_INLINE float
  232. __NTH (fminf (float __x, float __y))
  233. {
  234. # ifdef __AVX__
  235. float __res;
  236. __asm ("vminss %2, %1, %0" : "=x" (__res) : "x" (x), "xm" (__y));
  237. return __res;
  238. # else
  239. __asm ("minss %1, %0" : "+x" (__x) : "xm" (__y));
  240. return __x;
  241. # endif
  242. }
  243. __MATH_INLINE double
  244. __NTH (fmin (double __x, double __y))
  245. {
  246. # ifdef __AVX__
  247. float __res;
  248. __asm ("vminsd %2, %1, %0" : "=x" (__res) : "x" (x), "xm" (__y));
  249. return __res;
  250. # else
  251. __asm ("minsd %1, %0" : "+x" (__x) : "xm" (__y));
  252. return __x;
  253. # endif
  254. }
  255. # endif
  256. __END_NAMESPACE_C99
  257. # endif
  258. # if defined __SSE4_1__ && defined __SSE2_MATH__
  259. # if defined __USE_XOPEN_EXTENDED || defined __USE_ISOC99
  260. __BEGIN_NAMESPACE_C99
  261. /* Round to nearest integer. */
  262. __MATH_INLINE double
  263. __NTH (rint (double __x))
  264. {
  265. double __res;
  266. /* Mark as volatile since the result is dependent on the state of
  267. the SSE control register (the rounding mode). Otherwise GCC might
  268. remove these assembler instructions since it does not know about
  269. the rounding mode change and cannot currently be told. */
  270. __asm __volatile__ ("roundsd $4, %1, %0" : "=x" (__res) : "xm" (__x));
  271. return __res;
  272. }
  273. __MATH_INLINE float
  274. __NTH (rintf (float __x))
  275. {
  276. float __res;
  277. /* Mark as volatile since the result is dependent on the state of
  278. the SSE control register (the rounding mode). Otherwise GCC might
  279. remove these assembler instructions since it does not know about
  280. the rounding mode change and cannot currently be told. */
  281. __asm __volatile__ ("roundss $4, %1, %0" : "=x" (__res) : "xm" (__x));
  282. return __res;
  283. }
  284. # ifdef __USE_ISOC99
  285. /* Round to nearest integer without raising inexact exception. */
  286. __MATH_INLINE double
  287. __NTH (nearbyint (double __x))
  288. {
  289. double __res;
  290. /* Mark as volatile since the result is dependent on the state of
  291. the SSE control register (the rounding mode). Otherwise GCC might
  292. remove these assembler instructions since it does not know about
  293. the rounding mode change and cannot currently be told. */
  294. __asm __volatile__ ("roundsd $0xc, %1, %0" : "=x" (__res) : "xm" (__x));
  295. return __res;
  296. }
  297. __MATH_INLINE float
  298. __NTH (nearbyintf (float __x))
  299. {
  300. float __res;
  301. /* Mark as volatile since the result is dependent on the state of
  302. the SSE control register (the rounding mode). Otherwise GCC might
  303. remove these assembler instructions since it does not know about
  304. the rounding mode change and cannot currently be told. */
  305. __asm __volatile__ ("roundss $0xc, %1, %0" : "=x" (__res) : "xm" (__x));
  306. return __res;
  307. }
  308. # endif
  309. __END_NAMESPACE_C99
  310. # endif
  311. __BEGIN_NAMESPACE_STD
  312. /* Smallest integral value not less than X. */
  313. __MATH_INLINE double
  314. __NTH (ceil (double __x))
  315. {
  316. double __res;
  317. __asm ("roundsd $2, %1, %0" : "=x" (__res) : "xm" (__x));
  318. return __res;
  319. }
  320. __END_NAMESPACE_STD
  321. __BEGIN_NAMESPACE_C99
  322. __MATH_INLINE float
  323. __NTH (ceilf (float __x))
  324. {
  325. float __res;
  326. __asm ("roundss $2, %1, %0" : "=x" (__res) : "xm" (__x));
  327. return __res;
  328. }
  329. __END_NAMESPACE_C99
  330. __BEGIN_NAMESPACE_STD
  331. /* Largest integer not greater than X. */
  332. __MATH_INLINE double
  333. __NTH (floor (double __x))
  334. {
  335. double __res;
  336. __asm ("roundsd $1, %1, %0" : "=x" (__res) : "xm" (__x));
  337. return __res;
  338. }
  339. __END_NAMESPACE_STD
  340. __BEGIN_NAMESPACE_C99
  341. __MATH_INLINE float
  342. __NTH (floorf (float __x))
  343. {
  344. float __res;
  345. __asm ("roundss $1, %1, %0" : "=x" (__res) : "xm" (__x));
  346. return __res;
  347. }
  348. __END_NAMESPACE_C99
  349. # endif
  350. # endif
  351. #endif
  352. /* Disable x87 inlines when -fpmath=sse is passed and also when we're building
  353. on x86_64. Older gcc (gcc-3.2 for example) does not define __SSE2_MATH__
  354. for x86_64. */
  355. #if !defined __SSE2_MATH__ && !defined __x86_64__
  356. # if ((!defined __NO_MATH_INLINES || defined __LIBC_INTERNAL_MATH_INLINES) \
  357. && defined __OPTIMIZE__)
  358. /* The inline functions do not set errno or raise necessarily the
  359. correct exceptions. */
  360. # undef math_errhandling
  361. /* A macro to define float, double, and long double versions of various
  362. math functions for the ix87 FPU. FUNC is the function name (which will
  363. be suffixed with f and l for the float and long double version,
  364. respectively). OP is the name of the FPU operation.
  365. We define two sets of macros. The set with the additional NP
  366. doesn't add a prototype declaration. */
  367. # ifdef __USE_ISOC99
  368. # define __inline_mathop(func, op) \
  369. __inline_mathop_ (double, func, op) \
  370. __inline_mathop_ (float, __CONCAT(func,f), op) \
  371. __inline_mathop_ (long double, __CONCAT(func,l), op)
  372. # define __inline_mathopNP(func, op) \
  373. __inline_mathopNP_ (double, func, op) \
  374. __inline_mathopNP_ (float, __CONCAT(func,f), op) \
  375. __inline_mathopNP_ (long double, __CONCAT(func,l), op)
  376. # else
  377. # define __inline_mathop(func, op) \
  378. __inline_mathop_ (double, func, op)
  379. # define __inline_mathopNP(func, op) \
  380. __inline_mathopNP_ (double, func, op)
  381. # endif
  382. # define __inline_mathop_(float_type, func, op) \
  383. __inline_mathop_decl_ (float_type, func, op, "0" (__x))
  384. # define __inline_mathopNP_(float_type, func, op) \
  385. __inline_mathop_declNP_ (float_type, func, op, "0" (__x))
  386. # ifdef __USE_ISOC99
  387. # define __inline_mathop_decl(func, op, params...) \
  388. __inline_mathop_decl_ (double, func, op, params) \
  389. __inline_mathop_decl_ (float, __CONCAT(func,f), op, params) \
  390. __inline_mathop_decl_ (long double, __CONCAT(func,l), op, params)
  391. # define __inline_mathop_declNP(func, op, params...) \
  392. __inline_mathop_declNP_ (double, func, op, params) \
  393. __inline_mathop_declNP_ (float, __CONCAT(func,f), op, params) \
  394. __inline_mathop_declNP_ (long double, __CONCAT(func,l), op, params)
  395. # else
  396. # define __inline_mathop_decl(func, op, params...) \
  397. __inline_mathop_decl_ (double, func, op, params)
  398. # define __inline_mathop_declNP(func, op, params...) \
  399. __inline_mathop_declNP_ (double, func, op, params)
  400. # endif
  401. # define __inline_mathop_decl_(float_type, func, op, params...) \
  402. __MATH_INLINE float_type func (float_type) __THROW; \
  403. __inline_mathop_declNP_ (float_type, func, op, params)
  404. # define __inline_mathop_declNP_(float_type, func, op, params...) \
  405. __MATH_INLINE float_type __NTH (func (float_type __x)) \
  406. { \
  407. register float_type __result; \
  408. __asm __volatile__ (op : "=t" (__result) : params); \
  409. return __result; \
  410. }
  411. # ifdef __USE_ISOC99
  412. # define __inline_mathcode(func, arg, code) \
  413. __inline_mathcode_ (double, func, arg, code) \
  414. __inline_mathcode_ (float, __CONCAT(func,f), arg, code) \
  415. __inline_mathcode_ (long double, __CONCAT(func,l), arg, code)
  416. # define __inline_mathcodeNP(func, arg, code) \
  417. __inline_mathcodeNP_ (double, func, arg, code) \
  418. __inline_mathcodeNP_ (float, __CONCAT(func,f), arg, code) \
  419. __inline_mathcodeNP_ (long double, __CONCAT(func,l), arg, code)
  420. # define __inline_mathcode2(func, arg1, arg2, code) \
  421. __inline_mathcode2_ (double, func, arg1, arg2, code) \
  422. __inline_mathcode2_ (float, __CONCAT(func,f), arg1, arg2, code) \
  423. __inline_mathcode2_ (long double, __CONCAT(func,l), arg1, arg2, code)
  424. # define __inline_mathcodeNP2(func, arg1, arg2, code) \
  425. __inline_mathcodeNP2_ (double, func, arg1, arg2, code) \
  426. __inline_mathcodeNP2_ (float, __CONCAT(func,f), arg1, arg2, code) \
  427. __inline_mathcodeNP2_ (long double, __CONCAT(func,l), arg1, arg2, code)
  428. # define __inline_mathcode3(func, arg1, arg2, arg3, code) \
  429. __inline_mathcode3_ (double, func, arg1, arg2, arg3, code) \
  430. __inline_mathcode3_ (float, __CONCAT(func,f), arg1, arg2, arg3, code) \
  431. __inline_mathcode3_ (long double, __CONCAT(func,l), arg1, arg2, arg3, code)
  432. # define __inline_mathcodeNP3(func, arg1, arg2, arg3, code) \
  433. __inline_mathcodeNP3_ (double, func, arg1, arg2, arg3, code) \
  434. __inline_mathcodeNP3_ (float, __CONCAT(func,f), arg1, arg2, arg3, code) \
  435. __inline_mathcodeNP3_ (long double, __CONCAT(func,l), arg1, arg2, arg3, code)
  436. # else
  437. # define __inline_mathcode(func, arg, code) \
  438. __inline_mathcode_ (double, func, (arg), code)
  439. # define __inline_mathcodeNP(func, arg, code) \
  440. __inline_mathcodeNP_ (double, func, (arg), code)
  441. # define __inline_mathcode2(func, arg1, arg2, code) \
  442. __inline_mathcode2_ (double, func, arg1, arg2, code)
  443. # define __inline_mathcodeNP2(func, arg1, arg2, code) \
  444. __inline_mathcodeNP2_ (double, func, arg1, arg2, code)
  445. # define __inline_mathcode3(func, arg1, arg2, arg3, code) \
  446. __inline_mathcode3_ (double, func, arg1, arg2, arg3, code)
  447. # define __inline_mathcodeNP3(func, arg1, arg2, arg3, code) \
  448. __inline_mathcodeNP3_ (double, func, arg1, arg2, arg3, code)
  449. # endif
  450. # define __inline_mathcode_(float_type, func, arg, code) \
  451. __MATH_INLINE float_type func (float_type) __THROW; \
  452. __inline_mathcodeNP_(float_type, func, arg, code)
  453. # define __inline_mathcodeNP_(float_type, func, arg, code) \
  454. __MATH_INLINE float_type __NTH (func (float_type arg)) \
  455. { \
  456. code; \
  457. }
  458. # define __inline_mathcode2_(float_type, func, arg1, arg2, code) \
  459. __MATH_INLINE float_type func (float_type, float_type) __THROW; \
  460. __inline_mathcodeNP2_ (float_type, func, arg1, arg2, code)
  461. # define __inline_mathcodeNP2_(float_type, func, arg1, arg2, code) \
  462. __MATH_INLINE float_type __NTH (func (float_type arg1, float_type arg2)) \
  463. { \
  464. code; \
  465. }
  466. # define __inline_mathcode3_(float_type, func, arg1, arg2, arg3, code) \
  467. __MATH_INLINE float_type func (float_type, float_type, float_type) __THROW; \
  468. __inline_mathcodeNP3_(float_type, func, arg1, arg2, arg3, code)
  469. # define __inline_mathcodeNP3_(float_type, func, arg1, arg2, arg3, code) \
  470. __MATH_INLINE float_type __NTH (func (float_type arg1, float_type arg2, \
  471. float_type arg3)) \
  472. { \
  473. code; \
  474. }
  475. # endif
  476. # if !defined __NO_MATH_INLINES && defined __OPTIMIZE__
  477. /* Miscellaneous functions */
  478. /* __FAST_MATH__ is defined by gcc -ffast-math. */
  479. # ifdef __FAST_MATH__
  480. # ifdef __USE_GNU
  481. # define __sincos_code \
  482. register long double __cosr; \
  483. register long double __sinr; \
  484. register unsigned int __swtmp; \
  485. __asm __volatile__ \
  486. ("fsincos\n\t" \
  487. "fnstsw %w2\n\t" \
  488. "testl $0x400, %2\n\t" \
  489. "jz 1f\n\t" \
  490. "fldpi\n\t" \
  491. "fadd %%st(0)\n\t" \
  492. "fxch %%st(1)\n\t" \
  493. "2: fprem1\n\t" \
  494. "fnstsw %w2\n\t" \
  495. "testl $0x400, %2\n\t" \
  496. "jnz 2b\n\t" \
  497. "fstp %%st(1)\n\t" \
  498. "fsincos\n\t" \
  499. "1:" \
  500. : "=t" (__cosr), "=u" (__sinr), "=a" (__swtmp) : "0" (__x)); \
  501. *__sinx = __sinr; \
  502. *__cosx = __cosr
  503. __MATH_INLINE void
  504. __NTH (__sincos (double __x, double *__sinx, double *__cosx))
  505. {
  506. __sincos_code;
  507. }
  508. __MATH_INLINE void
  509. __NTH (__sincosf (float __x, float *__sinx, float *__cosx))
  510. {
  511. __sincos_code;
  512. }
  513. __MATH_INLINE void
  514. __NTH (__sincosl (long double __x, long double *__sinx, long double *__cosx))
  515. {
  516. __sincos_code;
  517. }
  518. # endif
  519. /* Optimized inline implementation, sometimes with reduced precision
  520. and/or argument range. */
  521. # if __GNUC_PREREQ (3, 5)
  522. # define __expm1_code \
  523. register long double __temp; \
  524. __temp = __builtin_expm1l (__x); \
  525. return __temp ? __temp : __x
  526. # else
  527. # define __expm1_code \
  528. register long double __value; \
  529. register long double __exponent; \
  530. register long double __temp; \
  531. __asm __volatile__ \
  532. ("fldl2e # e^x - 1 = 2^(x * log2(e)) - 1\n\t" \
  533. "fmul %%st(1) # x * log2(e)\n\t" \
  534. "fst %%st(1)\n\t" \
  535. "frndint # int(x * log2(e))\n\t" \
  536. "fxch\n\t" \
  537. "fsub %%st(1) # fract(x * log2(e))\n\t" \
  538. "f2xm1 # 2^(fract(x * log2(e))) - 1\n\t" \
  539. "fscale # 2^(x * log2(e)) - 2^(int(x * log2(e)))\n\t" \
  540. : "=t" (__value), "=u" (__exponent) : "0" (__x)); \
  541. __asm __volatile__ \
  542. ("fscale # 2^int(x * log2(e))\n\t" \
  543. : "=t" (__temp) : "0" (1.0), "u" (__exponent)); \
  544. __temp -= 1.0; \
  545. __temp += __value; \
  546. return __temp ? __temp : __x
  547. # endif
  548. __inline_mathcodeNP_ (long double, __expm1l, __x, __expm1_code)
  549. # if __GNUC_PREREQ (3, 4)
  550. __inline_mathcodeNP_ (long double, __expl, __x, return __builtin_expl (__x))
  551. # else
  552. # define __exp_code \
  553. register long double __value; \
  554. register long double __exponent; \
  555. __asm __volatile__ \
  556. ("fldl2e # e^x = 2^(x * log2(e))\n\t" \
  557. "fmul %%st(1) # x * log2(e)\n\t" \
  558. "fst %%st(1)\n\t" \
  559. "frndint # int(x * log2(e))\n\t" \
  560. "fxch\n\t" \
  561. "fsub %%st(1) # fract(x * log2(e))\n\t" \
  562. "f2xm1 # 2^(fract(x * log2(e))) - 1\n\t" \
  563. : "=t" (__value), "=u" (__exponent) : "0" (__x)); \
  564. __value += 1.0; \
  565. __asm __volatile__ \
  566. ("fscale" \
  567. : "=t" (__value) : "0" (__value), "u" (__exponent)); \
  568. return __value
  569. __inline_mathcodeNP (exp, __x, __exp_code)
  570. __inline_mathcodeNP_ (long double, __expl, __x, __exp_code)
  571. # endif
  572. # if !__GNUC_PREREQ (3, 5)
  573. __inline_mathcodeNP (tan, __x, \
  574. register long double __value; \
  575. register long double __value2 __attribute__ ((__unused__)); \
  576. __asm __volatile__ \
  577. ("fptan" \
  578. : "=t" (__value2), "=u" (__value) : "0" (__x)); \
  579. return __value)
  580. # endif
  581. # endif /* __FAST_MATH__ */
  582. # if __GNUC_PREREQ (3, 4)
  583. __inline_mathcodeNP2_ (long double, __atan2l, __y, __x,
  584. return __builtin_atan2l (__y, __x))
  585. # else
  586. # define __atan2_code \
  587. register long double __value; \
  588. __asm __volatile__ \
  589. ("fpatan" \
  590. : "=t" (__value) : "0" (__x), "u" (__y) : "st(1)"); \
  591. return __value
  592. # ifdef __FAST_MATH__
  593. __inline_mathcodeNP2 (atan2, __y, __x, __atan2_code)
  594. # endif
  595. __inline_mathcodeNP2_ (long double, __atan2l, __y, __x, __atan2_code)
  596. # endif
  597. # if defined __FAST_MATH__ && !__GNUC_PREREQ (3, 5)
  598. __inline_mathcodeNP2 (fmod, __x, __y, \
  599. register long double __value; \
  600. __asm __volatile__ \
  601. ("1: fprem\n\t" \
  602. "fnstsw %%ax\n\t" \
  603. "sahf\n\t" \
  604. "jp 1b" \
  605. : "=t" (__value) : "0" (__x), "u" (__y) : "ax", "cc"); \
  606. return __value)
  607. # endif
  608. # ifdef __FAST_MATH__
  609. # if !__GNUC_PREREQ (3,3)
  610. __inline_mathopNP (sqrt, "fsqrt")
  611. __inline_mathopNP_ (long double, __sqrtl, "fsqrt")
  612. # define __libc_sqrtl(n) __sqrtl (n)
  613. # else
  614. # define __libc_sqrtl(n) __builtin_sqrtl (n)
  615. # endif
  616. # endif
  617. # if __GNUC_PREREQ (2, 8)
  618. __inline_mathcodeNP_ (double, fabs, __x, return __builtin_fabs (__x))
  619. # ifdef __USE_ISOC99
  620. __inline_mathcodeNP_ (float, fabsf, __x, return __builtin_fabsf (__x))
  621. __inline_mathcodeNP_ (long double, fabsl, __x, return __builtin_fabsl (__x))
  622. # endif
  623. __inline_mathcodeNP_ (long double, __fabsl, __x, return __builtin_fabsl (__x))
  624. # else
  625. __inline_mathop (fabs, "fabs")
  626. __inline_mathop_ (long double, __fabsl, "fabs")
  627. # endif
  628. # ifdef __FAST_MATH__
  629. # if !__GNUC_PREREQ (3, 4)
  630. /* The argument range of this inline version is reduced. */
  631. __inline_mathopNP (sin, "fsin")
  632. /* The argument range of this inline version is reduced. */
  633. __inline_mathopNP (cos, "fcos")
  634. __inline_mathop_declNP (log, "fldln2; fxch; fyl2x", "0" (__x) : "st(1)")
  635. # endif
  636. # if !__GNUC_PREREQ (3, 5)
  637. __inline_mathop_declNP (log10, "fldlg2; fxch; fyl2x", "0" (__x) : "st(1)")
  638. __inline_mathcodeNP (asin, __x, return __atan2l (__x, __libc_sqrtl (1.0 - __x * __x)))
  639. __inline_mathcodeNP (acos, __x, return __atan2l (__libc_sqrtl (1.0 - __x * __x), __x))
  640. # endif
  641. # if !__GNUC_PREREQ (3, 4)
  642. __inline_mathop_declNP (atan, "fld1; fpatan", "0" (__x) : "st(1)")
  643. # endif
  644. # endif /* __FAST_MATH__ */
  645. __inline_mathcode_ (long double, __sgn1l, __x, \
  646. __extension__ union { long double __xld; unsigned int __xi[3]; } __n = \
  647. { __xld: __x }; \
  648. __n.__xi[2] = (__n.__xi[2] & 0x8000) | 0x3fff; \
  649. __n.__xi[1] = 0x80000000; \
  650. __n.__xi[0] = 0; \
  651. return __n.__xld)
  652. # ifdef __FAST_MATH__
  653. /* The argument range of the inline version of sinhl is slightly reduced. */
  654. __inline_mathcodeNP (sinh, __x, \
  655. register long double __exm1 = __expm1l (__fabsl (__x)); \
  656. return 0.5 * (__exm1 / (__exm1 + 1.0) + __exm1) * __sgn1l (__x))
  657. __inline_mathcodeNP (cosh, __x, \
  658. register long double __ex = __expl (__x); \
  659. return 0.5 * (__ex + 1.0 / __ex))
  660. __inline_mathcodeNP (tanh, __x, \
  661. register long double __exm1 = __expm1l (-__fabsl (__x + __x)); \
  662. return __exm1 / (__exm1 + 2.0) * __sgn1l (-__x))
  663. # endif
  664. __inline_mathcodeNP (floor, __x, \
  665. register long double __value; \
  666. register int __ignore; \
  667. unsigned short int __cw; \
  668. unsigned short int __cwtmp; \
  669. __asm __volatile ("fnstcw %3\n\t" \
  670. "movzwl %3, %1\n\t" \
  671. "andl $0xf3ff, %1\n\t" \
  672. "orl $0x0400, %1\n\t" /* rounding down */ \
  673. "movw %w1, %2\n\t" \
  674. "fldcw %2\n\t" \
  675. "frndint\n\t" \
  676. "fldcw %3" \
  677. : "=t" (__value), "=&q" (__ignore), "=m" (__cwtmp), \
  678. "=m" (__cw) \
  679. : "0" (__x)); \
  680. return __value)
  681. __inline_mathcodeNP (ceil, __x, \
  682. register long double __value; \
  683. register int __ignore; \
  684. unsigned short int __cw; \
  685. unsigned short int __cwtmp; \
  686. __asm __volatile ("fnstcw %3\n\t" \
  687. "movzwl %3, %1\n\t" \
  688. "andl $0xf3ff, %1\n\t" \
  689. "orl $0x0800, %1\n\t" /* rounding up */ \
  690. "movw %w1, %2\n\t" \
  691. "fldcw %2\n\t" \
  692. "frndint\n\t" \
  693. "fldcw %3" \
  694. : "=t" (__value), "=&q" (__ignore), "=m" (__cwtmp), \
  695. "=m" (__cw) \
  696. : "0" (__x)); \
  697. return __value)
  698. # ifdef __FAST_MATH__
  699. # define __ldexp_code \
  700. register long double __value; \
  701. __asm __volatile__ \
  702. ("fscale" \
  703. : "=t" (__value) : "0" (__x), "u" ((long double) __y)); \
  704. return __value
  705. __MATH_INLINE double
  706. __NTH (ldexp (double __x, int __y))
  707. {
  708. __ldexp_code;
  709. }
  710. # endif
  711. /* Optimized versions for some non-standardized functions. */
  712. # ifdef __USE_ISOC99
  713. # ifdef __FAST_MATH__
  714. __inline_mathcodeNP (expm1, __x, __expm1_code)
  715. /* We cannot rely on M_SQRT being defined. So we do it for ourself
  716. here. */
  717. # define __M_SQRT2 1.41421356237309504880L /* sqrt(2) */
  718. # if !__GNUC_PREREQ (3, 5)
  719. __inline_mathcodeNP (log1p, __x, \
  720. register long double __value; \
  721. if (__fabsl (__x) >= 1.0 - 0.5 * __M_SQRT2) \
  722. __value = logl (1.0 + __x); \
  723. else \
  724. __asm __volatile__ \
  725. ("fldln2\n\t" \
  726. "fxch\n\t" \
  727. "fyl2xp1" \
  728. : "=t" (__value) : "0" (__x) : "st(1)"); \
  729. return __value)
  730. # endif
  731. /* The argument range of the inline version of asinhl is slightly reduced. */
  732. __inline_mathcodeNP (asinh, __x, \
  733. register long double __y = __fabsl (__x); \
  734. return (log1pl (__y * __y / (__libc_sqrtl (__y * __y + 1.0) + 1.0) + __y) \
  735. * __sgn1l (__x)))
  736. __inline_mathcodeNP (acosh, __x, \
  737. return logl (__x + __libc_sqrtl (__x - 1.0) * __libc_sqrtl (__x + 1.0)))
  738. __inline_mathcodeNP (atanh, __x, \
  739. register long double __y = __fabsl (__x); \
  740. return -0.5 * log1pl (-(__y + __y) / (1.0 + __y)) * __sgn1l (__x))
  741. /* The argument range of the inline version of hypotl is slightly reduced. */
  742. __inline_mathcodeNP2 (hypot, __x, __y,
  743. return __libc_sqrtl (__x * __x + __y * __y))
  744. # if !__GNUC_PREREQ (3, 5)
  745. __inline_mathcodeNP(logb, __x, \
  746. register long double __value; \
  747. register long double __junk; \
  748. __asm __volatile__ \
  749. ("fxtract\n\t" \
  750. : "=t" (__junk), "=u" (__value) : "0" (__x)); \
  751. return __value)
  752. # endif
  753. # endif
  754. # endif
  755. # ifdef __USE_ISOC99
  756. # ifdef __FAST_MATH__
  757. # if !__GNUC_PREREQ (3, 5)
  758. __inline_mathop_declNP (log2, "fld1; fxch; fyl2x", "0" (__x) : "st(1)")
  759. # endif
  760. __MATH_INLINE float
  761. __NTH (ldexpf (float __x, int __y))
  762. {
  763. __ldexp_code;
  764. }
  765. __MATH_INLINE long double
  766. __NTH (ldexpl (long double __x, int __y))
  767. {
  768. __ldexp_code;
  769. }
  770. __inline_mathopNP (rint, "frndint")
  771. # endif /* __FAST_MATH__ */
  772. # define __lrint_code \
  773. long int __lrintres; \
  774. __asm__ __volatile__ \
  775. ("fistpl %0" \
  776. : "=m" (__lrintres) : "t" (__x) : "st"); \
  777. return __lrintres
  778. __MATH_INLINE long int
  779. __NTH (lrintf (float __x))
  780. {
  781. __lrint_code;
  782. }
  783. __MATH_INLINE long int
  784. __NTH (lrint (double __x))
  785. {
  786. __lrint_code;
  787. }
  788. __MATH_INLINE long int
  789. __NTH (lrintl (long double __x))
  790. {
  791. __lrint_code;
  792. }
  793. # undef __lrint_code
  794. # define __llrint_code \
  795. long long int __llrintres; \
  796. __asm__ __volatile__ \
  797. ("fistpll %0" \
  798. : "=m" (__llrintres) : "t" (__x) : "st"); \
  799. return __llrintres
  800. __extension__
  801. __MATH_INLINE long long int
  802. __NTH (llrintf (float __x))
  803. {
  804. __llrint_code;
  805. }
  806. __extension__
  807. __MATH_INLINE long long int
  808. __NTH (llrint (double __x))
  809. {
  810. __llrint_code;
  811. }
  812. __extension__
  813. __MATH_INLINE long long int
  814. __NTH (llrintl (long double __x))
  815. {
  816. __llrint_code;
  817. }
  818. # undef __llrint_code
  819. # endif
  820. # ifdef __USE_MISC
  821. # if defined __FAST_MATH__ && !__GNUC_PREREQ (3, 5)
  822. __inline_mathcodeNP2 (drem, __x, __y, \
  823. register double __value; \
  824. register int __clobbered; \
  825. __asm __volatile__ \
  826. ("1: fprem1\n\t" \
  827. "fstsw %%ax\n\t" \
  828. "sahf\n\t" \
  829. "jp 1b" \
  830. : "=t" (__value), "=&a" (__clobbered) : "0" (__x), "u" (__y) : "cc"); \
  831. return __value)
  832. # endif
  833. /* This function is used in the `isfinite' macro. */
  834. __MATH_INLINE int
  835. __NTH (__finite (double __x))
  836. {
  837. return (__extension__
  838. (((((union { double __d; int __i[2]; }) {__d: __x}).__i[1]
  839. | 0x800fffffu) + 1) >> 31));
  840. }
  841. # endif /* __USE_MISC */
  842. /* Undefine some of the large macros which are not used anymore. */
  843. # undef __atan2_code
  844. # ifdef __FAST_MATH__
  845. # undef __expm1_code
  846. # undef __exp_code
  847. # undef __sincos_code
  848. # endif /* __FAST_MATH__ */
  849. # endif /* __NO_MATH_INLINES */
  850. /* This code is used internally in the GNU libc. */
  851. # ifdef __LIBC_INTERNAL_MATH_INLINES
  852. __inline_mathop (__ieee754_sqrt, "fsqrt")
  853. __inline_mathcode2_ (long double, __ieee754_atan2l, __y, __x,
  854. register long double __value;
  855. __asm __volatile__ ("fpatan\n\t"
  856. : "=t" (__value)
  857. : "0" (__x), "u" (__y) : "st(1)");
  858. return __value;)
  859. # endif
  860. #endif /* !__SSE2_MATH__ && !__x86_64__ */