mathinline.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /* Definitions of inline math functions implemented by the m68881/2.
  2. Copyright (C) 1991,92,93,94,96,97,98,99,2000 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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifdef __GNUC__
  17. #ifdef __USE_ISOC99
  18. /* ISO C99 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_ISOC99
  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)) __THROW \
  95. { \
  96. float_type __result; \
  97. __asm("f" __STRING(op) "%.x %1, %0" : "=f" (__result) : "f" (__mathop_x));\
  98. return __result; \
  99. }
  100. __inline_mathop(__atan, atan)
  101. __inline_mathop(__cos, cos)
  102. __inline_mathop(__sin, sin)
  103. __inline_mathop(__tan, tan)
  104. __inline_mathop(__tanh, tanh)
  105. __inline_mathop(__fabs, abs)
  106. #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED || defined __USE_ISOC99
  107. __inline_mathop(__rint, int)
  108. __inline_mathop(__expm1, etoxm1)
  109. __inline_mathop(__log1p, lognp1)
  110. #endif
  111. #ifdef __USE_MISC
  112. __inline_mathop(__significand, getman)
  113. #endif
  114. #ifdef __USE_ISOC99
  115. __inline_mathop(__trunc, intrz)
  116. #endif
  117. #if !defined __NO_MATH_INLINES && defined __OPTIMIZE__
  118. __inline_mathop(atan, atan)
  119. __inline_mathop(cos, cos)
  120. __inline_mathop(sin, sin)
  121. __inline_mathop(tan, tan)
  122. __inline_mathop(tanh, tanh)
  123. # if defined __USE_MISC || defined __USE_XOPEN_EXTENDED || defined __USE_ISOC99
  124. __inline_mathop(rint, int)
  125. __inline_mathop(expm1, etoxm1)
  126. __inline_mathop(log1p, lognp1)
  127. # endif
  128. # ifdef __USE_MISC
  129. __inline_mathop(significand, getman)
  130. # endif
  131. # ifdef __USE_ISOC99
  132. __inline_mathop(trunc, intrz)
  133. # endif
  134. #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
  135. /* This macro contains the definition for the rest of the inline
  136. functions, using FLOAT_TYPE as the domain type and S as the suffix
  137. for the function names. */
  138. #define __inline_functions(float_type, s) \
  139. __m81_inline float_type \
  140. __m81_u(__CONCAT(__frexp,s))(float_type __value, int *__expptr) __THROW \
  141. { \
  142. float_type __mantissa, __exponent; \
  143. int __iexponent; \
  144. unsigned long __fpsr; \
  145. __asm("ftst%.x %1\n" \
  146. "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
  147. if (__fpsr & (7 << 24)) \
  148. { \
  149. /* Not finite or zero. */ \
  150. *__expptr = 0; \
  151. return __value; \
  152. } \
  153. __asm("fgetexp%.x %1, %0" : "=f" (__exponent) : "f" (__value)); \
  154. __iexponent = (int) __exponent + 1; \
  155. *__expptr = __iexponent; \
  156. __asm("fscale%.l %2, %0" : "=f" (__mantissa) \
  157. : "0" (__value), "dmi" (-__iexponent)); \
  158. return __mantissa; \
  159. } \
  160. \
  161. __m81_defun (float_type, __CONCAT(__floor,s), (float_type __x)) __THROW \
  162. { \
  163. float_type __result; \
  164. unsigned long int __ctrl_reg; \
  165. __asm __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
  166. /* Set rounding towards negative infinity. */ \
  167. __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  168. : "dmi" ((__ctrl_reg & ~0x10) | 0x20)); \
  169. /* Convert X to an integer, using -Inf rounding. */ \
  170. __asm __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
  171. /* Restore the previous rounding mode. */ \
  172. __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  173. : "dmi" (__ctrl_reg)); \
  174. return __result; \
  175. } \
  176. \
  177. __m81_defun (float_type, __CONCAT(__ceil,s), (float_type __x)) __THROW \
  178. { \
  179. float_type __result; \
  180. unsigned long int __ctrl_reg; \
  181. __asm __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
  182. /* Set rounding towards positive infinity. */ \
  183. __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  184. : "dmi" (__ctrl_reg | 0x30)); \
  185. /* Convert X to an integer, using +Inf rounding. */ \
  186. __asm __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
  187. /* Restore the previous rounding mode. */ \
  188. __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  189. : "dmi" (__ctrl_reg)); \
  190. return __result; \
  191. }
  192. __inline_functions(double,)
  193. #if defined __USE_MISC || defined __USE_ISOC99
  194. __inline_functions(float,f)
  195. __inline_functions(long double,l)
  196. #endif
  197. #undef __inline_functions
  198. #ifdef __USE_MISC
  199. # define __inline_functions(float_type, s) \
  200. __m81_defun (int, __CONCAT(__isinf,s), (float_type __value)) __THROW \
  201. { \
  202. /* There is no branch-condition for infinity, \
  203. so we must extract and examine the condition codes manually. */ \
  204. unsigned long int __fpsr; \
  205. __asm("ftst%.x %1\n" \
  206. "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
  207. return (__fpsr & (2 << 24)) ? (__fpsr & (8 << 24) ? -1 : 1) : 0; \
  208. } \
  209. \
  210. __m81_defun (int, __CONCAT(__finite,s), (float_type __value)) __THROW \
  211. { \
  212. /* There is no branch-condition for infinity, so we must extract and \
  213. examine the condition codes manually. */ \
  214. unsigned long int __fpsr; \
  215. __asm ("ftst%.x %1\n" \
  216. "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
  217. return (__fpsr & (3 << 24)) == 0; \
  218. } \
  219. \
  220. __m81_defun (float_type, __CONCAT(__scalbn,s), \
  221. (float_type __x, int __n)) __THROW \
  222. { \
  223. float_type __result; \
  224. __asm ("fscale%.l %1, %0" : "=f" (__result) : "dmi" (__n), "0" (__x)); \
  225. return __result; \
  226. }
  227. __inline_functions(double,)
  228. __inline_functions(float,f)
  229. __inline_functions(long double,l)
  230. # undef __inline_functions
  231. #endif /* Use misc. */
  232. #if defined __USE_MISC || defined __USE_XOPEN
  233. # define __inline_functions(float_type, s) \
  234. __m81_defun (int, __CONCAT(__isnan,s), (float_type __value)) __THROW \
  235. { \
  236. char __result; \
  237. __asm("ftst%.x %1\n" \
  238. "fsun %0" : "=dm" (__result) : "f" (__value)); \
  239. return __result; \
  240. }
  241. __inline_functions(double,)
  242. # ifdef __USE_MISC
  243. __inline_functions(float,f)
  244. __inline_functions(long double,l)
  245. # endif
  246. # undef __inline_functions
  247. #endif
  248. #ifdef __USE_ISOC99
  249. # define __inline_functions(float_type, s) \
  250. __m81_defun (int, __CONCAT(__signbit,s), (float_type __value)) __THROW \
  251. { \
  252. /* There is no branch-condition for the sign bit, so we must extract \
  253. and examine the condition codes manually. */ \
  254. unsigned long int __fpsr; \
  255. __asm ("ftst%.x %1\n" \
  256. "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
  257. return (__fpsr >> 27) & 1; \
  258. } \
  259. \
  260. __m81_defun (float_type, __CONCAT(__scalbln,s), \
  261. (float_type __x, long int __n)) __THROW \
  262. { \
  263. return __CONCAT(__scalbn,s) (__x, __n); \
  264. } \
  265. \
  266. __m81_defun (float_type, __CONCAT(__nearbyint,s), (float_type __x)) __THROW \
  267. { \
  268. float_type __result; \
  269. unsigned long int __ctrl_reg; \
  270. __asm __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
  271. /* Temporarily disable the inexact exception. */ \
  272. __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  273. : "dmi" (__ctrl_reg & ~0x200)); \
  274. __asm __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
  275. __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  276. : "dmi" (__ctrl_reg)); \
  277. return __result; \
  278. } \
  279. \
  280. __m81_defun (long int, __CONCAT(__lrint,s), (float_type __x)) __THROW \
  281. { \
  282. long int __result; \
  283. __asm ("fmove%.l %1, %0" : "=dm" (__result) : "f" (__x)); \
  284. return __result; \
  285. } \
  286. \
  287. __m81_inline float_type \
  288. __m81_u(__CONCAT(__fma,s))(float_type __x, float_type __y, \
  289. float_type __z) __THROW \
  290. { \
  291. return (__x * __y) + __z; \
  292. }
  293. __inline_functions (double,)
  294. __inline_functions (float,f)
  295. __inline_functions (long double,l)
  296. # undef __inline_functions
  297. #endif /* Use ISO C9x */
  298. #ifdef __USE_GNU
  299. # define __inline_functions(float_type, s) \
  300. __m81_inline void \
  301. __m81_u(__CONCAT(__sincos,s))(float_type __x, float_type *__sinx, \
  302. float_type *__cosx) __THROW \
  303. { \
  304. __asm ("fsincos%.x %2,%1:%0" \
  305. : "=f" (*__sinx), "=f" (*__cosx) : "f" (__x)); \
  306. }
  307. __inline_functions (double,)
  308. __inline_functions (float,f)
  309. __inline_functions (long double,l)
  310. # undef __inline_functions
  311. #endif
  312. #if !defined __NO_MATH_INLINES && defined __OPTIMIZE__
  313. /* Define inline versions of the user visible functions. */
  314. /* Note that there must be no whitespace before the argument passed for
  315. NAME, to make token pasting work correctly with -traditional. */
  316. # define __inline_forward_c(rettype, name, args1, args2) \
  317. extern __inline rettype __attribute__((__const__)) \
  318. name args1 __THROW \
  319. { \
  320. return __CONCAT(__,name) args2; \
  321. }
  322. # define __inline_forward(rettype, name, args1, args2) \
  323. extern __inline rettype name args1 __THROW \
  324. { \
  325. return __CONCAT(__,name) args2; \
  326. }
  327. __inline_forward(double,frexp, (double __value, int *__expptr),
  328. (__value, __expptr))
  329. __inline_forward_c(double,floor, (double __x), (__x))
  330. __inline_forward_c(double,ceil, (double __x), (__x))
  331. # ifdef __USE_MISC
  332. # ifndef __USE_ISOC99 /* Conflict with macro of same name. */
  333. __inline_forward_c(int,isinf, (double __value), (__value))
  334. # endif
  335. __inline_forward_c(int,finite, (double __value), (__value))
  336. __inline_forward_c(double,scalbn, (double __x, int __n), (__x, __n))
  337. # endif
  338. # if defined __USE_MISC || defined __USE_XOPEN
  339. # ifndef __USE_ISOC99 /* Conflict with macro of same name. */
  340. __inline_forward_c(int,isnan, (double __value), (__value))
  341. # endif
  342. # endif
  343. # ifdef __USE_ISOC99
  344. __inline_forward_c(double,scalbln, (double __x, long int __n), (__x, __n))
  345. __inline_forward_c(double,nearbyint, (double __value), (__value))
  346. __inline_forward_c(long int,lrint, (double __value), (__value))
  347. __inline_forward_c(double,fma, (double __x, double __y, double __z),
  348. (__x, __y, __z))
  349. # endif
  350. # ifdef __USE_GNU
  351. __inline_forward(void,sincos, (double __x, double *__sinx, double *__cosx),
  352. (__x, __sinx, __cosx))
  353. # endif
  354. # if defined __USE_MISC || defined __USE_ISOC99
  355. __inline_forward(float,frexpf, (float __value, int *__expptr),
  356. (__value, __expptr))
  357. __inline_forward_c(float,floorf, (float __x), (__x))
  358. __inline_forward_c(float,ceilf, (float __x), (__x))
  359. # ifdef __USE_MISC
  360. __inline_forward_c(int,isinff, (float __value), (__value))
  361. __inline_forward_c(int,finitef, (float __value), (__value))
  362. __inline_forward_c(float,scalbnf, (float __x, int __n), (__x, __n))
  363. __inline_forward_c(int,isnanf, (float __value), (__value))
  364. # endif
  365. # ifdef __USE_ISOC99
  366. __inline_forward_c(float,scalblnf, (float __x, long int __n), (__x, __n))
  367. __inline_forward_c(float,nearbyintf, (float __value), (__value))
  368. __inline_forward_c(long int,lrintf, (float __value), (__value))
  369. __inline_forward_c(float,fmaf, (float __x, float __y, float __z),
  370. (__x, __y, __z))
  371. # endif
  372. # ifdef __USE_GNU
  373. __inline_forward(void,sincosf, (float __x, float *__sinx, float *__cosx),
  374. (__x, __sinx, __cosx))
  375. # endif
  376. __inline_forward(long double,frexpl, (long double __value, int *__expptr),
  377. (__value, __expptr))
  378. __inline_forward_c(long double,floorl, (long double __x), (__x))
  379. __inline_forward_c(long double,ceill, (long double __x), (__x))
  380. # ifdef __USE_MISC
  381. __inline_forward_c(int,isinfl, (long double __value), (__value))
  382. __inline_forward_c(int,finitel, (long double __value), (__value))
  383. __inline_forward_c(long double,scalbnl, (long double __x, int __n), (__x, __n))
  384. __inline_forward_c(int,isnanl, (long double __value), (__value))
  385. # endif
  386. # ifdef __USE_ISOC99
  387. __inline_forward_c(long double,scalblnl, (long double __x, long int __n),
  388. (__x, __n))
  389. __inline_forward_c(long double,nearbyintl, (long double __value), (__value))
  390. __inline_forward_c(long int,lrintl, (long double __value), (__value))
  391. __inline_forward_c(long double,fmal,
  392. (long double __x, long double __y, long double __z),
  393. (__x, __y, __z))
  394. # endif
  395. # ifdef __USE_GNU
  396. __inline_forward(void,sincosl,
  397. (long double __x, long double *__sinx, long double *__cosx),
  398. (__x, __sinx, __cosx))
  399. # endif
  400. #endif /* Use misc or ISO C99 */
  401. #undef __inline_forward
  402. #undef __inline_forward_c
  403. #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
  404. #endif
  405. #endif /* GCC. */