mathinline.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /* Definitions of inline math functions implemented by the m68881/2.
  2. Copyright (C) 1991, 92, 93, 94, 96, 97, 98 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 Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. 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. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #ifdef __GNUC__
  17. #ifdef __USE_ISOC9X
  18. /* ISO C 9X defines some macros to perform unordered comparisons. The
  19. m68k FPU supports this with special opcodes and we should use them.
  20. These must not be inline functions since we have to be able to handle
  21. all floating-point types. */
  22. # define isgreater(x, y) \
  23. __extension__ \
  24. ({ char __result; \
  25. __asm__ ("fcmp%.x %2,%1; fsogt %0" \
  26. : "=dm" (__result) : "f" (x), "f" (y)); \
  27. __result != 0; })
  28. # define isgreaterequal(x, y) \
  29. __extension__ \
  30. ({ char __result; \
  31. __asm__ ("fcmp%.x %2,%1; fsoge %0" \
  32. : "=dm" (__result) : "f" (x), "f" (y)); \
  33. __result != 0; })
  34. # define isless(x, y) \
  35. __extension__ \
  36. ({ char __result; \
  37. __asm__ ("fcmp%.x %2,%1; fsolt %0" \
  38. : "=dm" (__result) : "f" (x), "f" (y)); \
  39. __result != 0; })
  40. # define islessequal(x, y) \
  41. __extension__ \
  42. ({ char __result; \
  43. __asm__ ("fcmp%.x %2,%1; fsole %0" \
  44. : "=dm" (__result) : "f" (x), "f" (y)); \
  45. __result != 0; })
  46. # define islessgreater(x, y) \
  47. __extension__ \
  48. ({ char __result; \
  49. __asm__ ("fcmp%.x %2,%1; fsogl %0" \
  50. : "=dm" (__result) : "f" (x), "f" (y)); \
  51. __result != 0; })
  52. # define isunordered(x, y) \
  53. __extension__ \
  54. ({ char __result; \
  55. __asm__ ("fcmp%.x %2,%1; fsun %0" \
  56. : "=dm" (__result) : "f" (x), "f" (y)); \
  57. __result != 0; })
  58. #endif
  59. #if (!defined __NO_MATH_INLINES && defined __OPTIMIZE__) \
  60. || defined __LIBC_INTERNAL_MATH_INLINES
  61. #ifdef __LIBC_INTERNAL_MATH_INLINES
  62. /* This is used when defining the functions themselves. Define them with
  63. __ names, and with `static inline' instead of `extern inline' so the
  64. bodies will always be used, never an external function call. */
  65. # define __m81_u(x) __CONCAT(__,x)
  66. # define __m81_inline static __inline
  67. #else
  68. # define __m81_u(x) x
  69. # ifdef __cplusplus
  70. # define __m81_inline __inline
  71. # else
  72. # define __m81_inline extern __inline
  73. # endif
  74. # define __M81_MATH_INLINES 1
  75. #endif
  76. /* Define a const math function. */
  77. #define __m81_defun(rettype, func, args) \
  78. __m81_inline rettype __attribute__((__const__)) \
  79. __m81_u(func) args
  80. /* Define the three variants of a math function that has a direct
  81. implementation in the m68k fpu. FUNC is the name for C (which will be
  82. suffixed with f and l for the float and long double version, resp). OP
  83. is the name of the fpu operation (without leading f). */
  84. #if defined __USE_MISC || defined __USE_ISOC9X
  85. # define __inline_mathop(func, op) \
  86. __inline_mathop1(double, func, op) \
  87. __inline_mathop1(float, __CONCAT(func,f), op) \
  88. __inline_mathop1(long double, __CONCAT(func,l), op)
  89. #else
  90. # define __inline_mathop(func, op) \
  91. __inline_mathop1(double, func, op)
  92. #endif
  93. #define __inline_mathop1(float_type,func, op) \
  94. __m81_defun (float_type, func, (float_type __mathop_x)) \
  95. { \
  96. float_type __result; \
  97. __asm("f" __STRING(op) "%.x %1, %0" : "=f" (__result) : "f" (__mathop_x));\
  98. return __result; \
  99. }
  100. #ifdef __LIBC_INTERNAL_MATH_INLINES
  101. /* ieee style elementary functions */
  102. /* These are internal to the implementation of libm. */
  103. __inline_mathop(__ieee754_acos, acos)
  104. __inline_mathop(__ieee754_asin, asin)
  105. __inline_mathop(__ieee754_cosh, cosh)
  106. __inline_mathop(__ieee754_sinh, sinh)
  107. __inline_mathop(__ieee754_exp, etox)
  108. __inline_mathop(__ieee754_exp2, twotox)
  109. __inline_mathop(__ieee754_exp10, tentox)
  110. __inline_mathop(__ieee754_log10, log10)
  111. __inline_mathop(__ieee754_log, logn)
  112. __inline_mathop(__ieee754_sqrt, sqrt)
  113. __inline_mathop(__ieee754_atanh, atanh)
  114. #endif
  115. __inline_mathop(__atan, atan)
  116. __inline_mathop(__cos, cos)
  117. __inline_mathop(__sin, sin)
  118. __inline_mathop(__tan, tan)
  119. __inline_mathop(__tanh, tanh)
  120. __inline_mathop(__fabs, abs)
  121. #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED || defined __USE_ISOC9X
  122. __inline_mathop(__rint, int)
  123. __inline_mathop(__expm1, etoxm1)
  124. __inline_mathop(__log1p, lognp1)
  125. #endif
  126. #ifdef __USE_MISC
  127. __inline_mathop(__significand, getman)
  128. #endif
  129. #ifdef __USE_ISOC9X
  130. __inline_mathop(__log2, log2)
  131. __inline_mathop(__trunc, intrz)
  132. #endif
  133. #if !defined __NO_MATH_INLINES && defined __OPTIMIZE__
  134. __inline_mathop(atan, atan)
  135. __inline_mathop(cos, cos)
  136. __inline_mathop(sin, sin)
  137. __inline_mathop(tan, tan)
  138. __inline_mathop(tanh, tanh)
  139. # if defined __USE_MISC || defined __USE_XOPEN_EXTENDED || defined __USE_ISOC9X
  140. __inline_mathop(rint, int)
  141. __inline_mathop(expm1, etoxm1)
  142. __inline_mathop(log1p, lognp1)
  143. # endif
  144. # ifdef __USE_MISC
  145. __inline_mathop(significand, getman)
  146. # endif
  147. # ifdef __USE_ISOC9X
  148. __inline_mathop(log2, log2)
  149. __inline_mathop(trunc, intrz)
  150. # endif
  151. #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
  152. /* This macro contains the definition for the rest of the inline
  153. functions, using __FLOAT_TYPE as the domain type and __S as the suffix
  154. for the function names. */
  155. #ifdef __LIBC_INTERNAL_MATH_INLINES
  156. /* Internally used functions. */
  157. # define __internal_inline_functions(float_type, s) \
  158. __m81_defun (float_type, __CONCAT(__ieee754_remainder,s), \
  159. (float_type __x, float_type __y)) \
  160. { \
  161. float_type __result; \
  162. __asm("frem%.x %1, %0" : "=f" (__result) : "f" (__y), "0" (__x)); \
  163. return __result; \
  164. } \
  165. \
  166. __m81_defun (float_type, __CONCAT(__ieee754_fmod,s), \
  167. (float_type __x, float_type __y)) \
  168. { \
  169. float_type __result; \
  170. __asm("fmod%.x %1, %0" : "=f" (__result) : "f" (__y), "0" (__x)); \
  171. return __result; \
  172. }
  173. __internal_inline_functions (double,)
  174. __internal_inline_functions (float,f)
  175. __internal_inline_functions (long double,l)
  176. # undef __internal_inline_functions
  177. /* Get the m68881 condition codes, to quickly check multiple conditions. */
  178. static __inline__ unsigned long
  179. __m81_test (long double __val)
  180. {
  181. unsigned long __fpsr;
  182. __asm ("ftst%.x %1; fmove%.l %/fpsr,%0" : "=dm" (__fpsr) : "f" (__val));
  183. return __fpsr;
  184. }
  185. /* Bit values returned by __m81_test. */
  186. # define __M81_COND_NAN (1 << 24)
  187. # define __M81_COND_INF (2 << 24)
  188. # define __M81_COND_ZERO (4 << 24)
  189. # define __M81_COND_NEG (8 << 24)
  190. #endif /* __LIBC_INTENRAL_MATH_INLINES */
  191. /* The rest of the functions are available to the user. */
  192. #define __inline_functions(float_type, s) \
  193. __m81_inline float_type \
  194. __m81_u(__CONCAT(__frexp,s))(float_type __value, int *__expptr) \
  195. { \
  196. float_type __mantissa, __exponent; \
  197. int __iexponent; \
  198. unsigned long __fpsr; \
  199. __asm("ftst%.x %1\n" \
  200. "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
  201. if (__fpsr & (7 << 24)) \
  202. { \
  203. /* Not finite or zero. */ \
  204. *__expptr = 0; \
  205. return __value; \
  206. } \
  207. __asm("fgetexp%.x %1, %0" : "=f" (__exponent) : "f" (__value)); \
  208. __iexponent = (int) __exponent + 1; \
  209. *__expptr = __iexponent; \
  210. __asm("fscale%.l %2, %0" : "=f" (__mantissa) \
  211. : "0" (__value), "dmi" (-__iexponent)); \
  212. return __mantissa; \
  213. } \
  214. \
  215. __m81_defun (float_type, __CONCAT(__floor,s), (float_type __x)) \
  216. { \
  217. float_type __result; \
  218. unsigned long int __ctrl_reg; \
  219. __asm __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
  220. /* Set rounding towards negative infinity. */ \
  221. __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  222. : "dmi" ((__ctrl_reg & ~0x10) | 0x20)); \
  223. /* Convert X to an integer, using -Inf rounding. */ \
  224. __asm __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
  225. /* Restore the previous rounding mode. */ \
  226. __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  227. : "dmi" (__ctrl_reg)); \
  228. return __result; \
  229. } \
  230. \
  231. __m81_defun (float_type, __CONCAT(__ceil,s), (float_type __x)) \
  232. { \
  233. float_type __result; \
  234. unsigned long int __ctrl_reg; \
  235. __asm __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
  236. /* Set rounding towards positive infinity. */ \
  237. __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  238. : "dmi" (__ctrl_reg | 0x30)); \
  239. /* Convert X to an integer, using +Inf rounding. */ \
  240. __asm __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
  241. /* Restore the previous rounding mode. */ \
  242. __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  243. : "dmi" (__ctrl_reg)); \
  244. return __result; \
  245. }
  246. __inline_functions(double,)
  247. #if defined __USE_MISC || defined __USE_ISOC9X
  248. __inline_functions(float,f)
  249. __inline_functions(long double,l)
  250. #endif
  251. #undef __inline_functions
  252. #ifdef __USE_MISC
  253. # define __inline_functions(float_type, s) \
  254. __m81_defun (int, __CONCAT(__isinf,s), (float_type __value)) \
  255. { \
  256. /* There is no branch-condition for infinity, \
  257. so we must extract and examine the condition codes manually. */ \
  258. unsigned long int __fpsr; \
  259. __asm("ftst%.x %1\n" \
  260. "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
  261. return (__fpsr & (2 << 24)) ? (__fpsr & (8 << 24) ? -1 : 1) : 0; \
  262. } \
  263. \
  264. __m81_defun (int, __CONCAT(__finite,s), (float_type __value)) \
  265. { \
  266. /* There is no branch-condition for infinity, so we must extract and \
  267. examine the condition codes manually. */ \
  268. unsigned long int __fpsr; \
  269. __asm ("ftst%.x %1\n" \
  270. "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
  271. return (__fpsr & (3 << 24)) == 0; \
  272. } \
  273. \
  274. __m81_defun (float_type, __CONCAT(__scalbn,s), \
  275. (float_type __x, int __n)) \
  276. { \
  277. float_type __result; \
  278. __asm ("fscale%.l %1, %0" : "=f" (__result) : "dmi" (__n), "0" (__x)); \
  279. return __result; \
  280. }
  281. __inline_functions(double,)
  282. __inline_functions(float,f)
  283. __inline_functions(long double,l)
  284. # undef __inline_functions
  285. #endif /* Use misc. */
  286. #if defined __USE_MISC || defined __USE_XOPEN
  287. # define __inline_functions(float_type, s) \
  288. __m81_defun (int, __CONCAT(__isnan,s), (float_type __value)) \
  289. { \
  290. char __result; \
  291. __asm("ftst%.x %1\n" \
  292. "fsun %0" : "=dm" (__result) : "f" (__value)); \
  293. return __result; \
  294. }
  295. __inline_functions(double,)
  296. # ifdef __USE_MISC
  297. __inline_functions(float,f)
  298. __inline_functions(long double,l)
  299. # endif
  300. # undef __inline_functions
  301. #endif
  302. #ifdef __USE_ISOC9X
  303. # define __inline_functions(float_type, s) \
  304. __m81_defun (int, __CONCAT(__signbit,s), (float_type __value)) \
  305. { \
  306. /* There is no branch-condition for the sign bit, so we must extract \
  307. and examine the condition codes manually. */ \
  308. unsigned long int __fpsr; \
  309. __asm ("ftst%.x %1\n" \
  310. "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
  311. return (__fpsr >> 27) & 1; \
  312. } \
  313. \
  314. __m81_defun (float_type, __CONCAT(__scalbln,s), \
  315. (float_type __x, long int __n)) \
  316. { \
  317. return __CONCAT(__scalbn,s) (__x, __n); \
  318. } \
  319. \
  320. __m81_defun (float_type, __CONCAT(__nearbyint,s), (float_type __x)) \
  321. { \
  322. float_type __result; \
  323. unsigned long int __ctrl_reg; \
  324. __asm __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
  325. /* Temporarily disable the inexact exception. */ \
  326. __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  327. : "dmi" (__ctrl_reg & ~0x200)); \
  328. __asm __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
  329. __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  330. : "dmi" (__ctrl_reg)); \
  331. return __result; \
  332. } \
  333. \
  334. __m81_defun (long int, __CONCAT(__lrint,s), (float_type __x)) \
  335. { \
  336. long int __result; \
  337. __asm ("fmove%.l %1, %0" : "=dm" (__result) : "f" (__x)); \
  338. return __result; \
  339. } \
  340. \
  341. __m81_inline float_type \
  342. __m81_u(__CONCAT(__fma,s))(float_type __x, float_type __y, \
  343. float_type __z) \
  344. { \
  345. return (__x * __y) + __z; \
  346. }
  347. __inline_functions (double,)
  348. __inline_functions (float,f)
  349. __inline_functions (long double,l)
  350. # undef __inline_functions
  351. #endif /* Use ISO C9x */
  352. #ifdef __USE_GNU
  353. # define __inline_functions(float_type, s) \
  354. __m81_inline void \
  355. __m81_u(__CONCAT(__sincos,s))(float_type __x, float_type *__sinx, \
  356. float_type *__cosx) \
  357. { \
  358. __asm ("fsincos%.x %2,%1:%0" \
  359. : "=f" (*__sinx), "=f" (*__cosx) : "f" (__x)); \
  360. }
  361. __inline_functions (double,)
  362. __inline_functions (float,f)
  363. __inline_functions (long double,l)
  364. # undef __inline_functions
  365. #endif
  366. #if !defined __NO_MATH_INLINES && defined __OPTIMIZE__
  367. /* Define inline versions of the user visible functions. */
  368. /* Note that there must be no whitespace before the argument passed for
  369. NAME, to make token pasting work correctly with -traditional. */
  370. # define __inline_forward_c(rettype, name, args1, args2) \
  371. extern __inline rettype __attribute__((__const__)) \
  372. name args1 \
  373. { \
  374. return __CONCAT(__,name) args2; \
  375. }
  376. # define __inline_forward(rettype, name, args1, args2) \
  377. extern __inline rettype name args1 \
  378. { \
  379. return __CONCAT(__,name) args2; \
  380. }
  381. __inline_forward(double,frexp, (double __value, int *__expptr),
  382. (__value, __expptr))
  383. __inline_forward_c(double,floor, (double __x), (__x))
  384. __inline_forward_c(double,ceil, (double __x), (__x))
  385. # ifdef __USE_MISC
  386. # ifndef __USE_ISOC9X /* Conflict with macro of same name. */
  387. __inline_forward_c(int,isinf, (double __value), (__value))
  388. # endif
  389. __inline_forward_c(int,finite, (double __value), (__value))
  390. __inline_forward_c(double,scalbn, (double __x, int __n), (__x, __n))
  391. # endif
  392. # if defined __USE_MISC || defined __USE_XOPEN
  393. # ifndef __USE_ISOC9X /* Conflict with macro of same name. */
  394. __inline_forward_c(int,isnan, (double __value), (__value))
  395. # endif
  396. # endif
  397. # ifdef __USE_ISOC9X
  398. __inline_forward_c(double,scalbln, (double __x, long int __n), (__x, __n))
  399. __inline_forward_c(double,nearbyint, (double __value), (__value))
  400. __inline_forward_c(long int,lrint, (double __value), (__value))
  401. __inline_forward_c(double,fma, (double __x, double __y, double __z),
  402. (__x, __y, __z))
  403. # endif
  404. # ifdef __USE_GNU
  405. __inline_forward(void,sincos, (double __x, double *__sinx, double *__cosx),
  406. (__x, __sinx, __cosx))
  407. # endif
  408. # if defined __USE_MISC || defined __USE_ISOC9X
  409. __inline_forward(float,frexpf, (float __value, int *__expptr),
  410. (__value, __expptr))
  411. __inline_forward_c(float,floorf, (float __x), (__x))
  412. __inline_forward_c(float,ceilf, (float __x), (__x))
  413. # ifdef __USE_MISC
  414. __inline_forward_c(int,isinff, (float __value), (__value))
  415. __inline_forward_c(int,finitef, (float __value), (__value))
  416. __inline_forward_c(float,scalbnf, (float __x, int __n), (__x, __n))
  417. __inline_forward_c(int,isnanf, (float __value), (__value))
  418. # endif
  419. # ifdef __USE_ISOC9X
  420. __inline_forward_c(float,scalblnf, (float __x, long int __n), (__x, __n))
  421. __inline_forward_c(float,nearbyintf, (float __value), (__value))
  422. __inline_forward_c(long int,lrintf, (float __value), (__value))
  423. __inline_forward_c(float,fmaf, (float __x, float __y, float __z),
  424. (__x, __y, __z))
  425. # endif
  426. # ifdef __USE_GNU
  427. __inline_forward(void,sincosf, (float __x, float *__sinx, float *__cosx),
  428. (__x, __sinx, __cosx))
  429. # endif
  430. __inline_forward(long double,frexpl, (long double __value, int *__expptr),
  431. (__value, __expptr))
  432. __inline_forward_c(long double,floorl, (long double __x), (__x))
  433. __inline_forward_c(long double,ceill, (long double __x), (__x))
  434. # ifdef __USE_MISC
  435. __inline_forward_c(int,isinfl, (long double __value), (__value))
  436. __inline_forward_c(int,finitel, (long double __value), (__value))
  437. __inline_forward_c(long double,scalbnl, (long double __x, int __n), (__x, __n))
  438. __inline_forward_c(int,isnanl, (long double __value), (__value))
  439. # endif
  440. # ifdef __USE_ISOC9X
  441. __inline_forward_c(long double,scalblnl, (long double __x, long int __n),
  442. (__x, __n))
  443. __inline_forward_c(long double,nearbyintl, (long double __value), (__value))
  444. __inline_forward_c(long int,lrintl, (long double __value), (__value))
  445. __inline_forward_c(long double,fmal,
  446. (long double __x, long double __y, long double __z),
  447. (__x, __y, __z))
  448. # endif
  449. # ifdef __USE_GNU
  450. __inline_forward(void,sincosl,
  451. (long double __x, long double *__sinx, long double *__cosx),
  452. (__x, __sinx, __cosx))
  453. # endif
  454. #endif /* Use misc or ISO C9X */
  455. #undef __inline_forward
  456. #undef __inline_forward_c
  457. #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
  458. #endif
  459. #endif /* GCC. */