mathinline.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /* Definitions of inline math functions implemented by the m68881/2.
  2. Copyright (C) 1991,92,93,94,96,97,98,99,2000,2002, 2003, 2004
  3. Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  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, see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifdef __GNUC__
  17. #ifdef __USE_ISOC99
  18. /* GCC 3.1 and up have builtins that actually can be used. */
  19. # if !__GNUC_PREREQ (3,1)
  20. /* ISO C99 defines some macros to perform unordered comparisons. The
  21. m68k FPU supports this with special opcodes and we should use them.
  22. These must not be inline functions since we have to be able to handle
  23. all floating-point types. */
  24. # undef isgreater
  25. # undef isgreaterequal
  26. # undef isless
  27. # undef islessequal
  28. # undef islessgreater
  29. # undef isunordered
  30. # define isgreater(x, y) \
  31. __extension__ \
  32. ({ char __result; \
  33. __asm__ ("fcmp%.x %2,%1; fsogt %0" \
  34. : "=dm" (__result) : "f" (x), "f" (y)); \
  35. __result != 0; })
  36. # define isgreaterequal(x, y) \
  37. __extension__ \
  38. ({ char __result; \
  39. __asm__ ("fcmp%.x %2,%1; fsoge %0" \
  40. : "=dm" (__result) : "f" (x), "f" (y)); \
  41. __result != 0; })
  42. # define isless(x, y) \
  43. __extension__ \
  44. ({ char __result; \
  45. __asm__ ("fcmp%.x %2,%1; fsolt %0" \
  46. : "=dm" (__result) : "f" (x), "f" (y)); \
  47. __result != 0; })
  48. # define islessequal(x, y) \
  49. __extension__ \
  50. ({ char __result; \
  51. __asm__ ("fcmp%.x %2,%1; fsole %0" \
  52. : "=dm" (__result) : "f" (x), "f" (y)); \
  53. __result != 0; })
  54. # define islessgreater(x, y) \
  55. __extension__ \
  56. ({ char __result; \
  57. __asm__ ("fcmp%.x %2,%1; fsogl %0" \
  58. : "=dm" (__result) : "f" (x), "f" (y)); \
  59. __result != 0; })
  60. # define isunordered(x, y) \
  61. __extension__ \
  62. ({ char __result; \
  63. __asm__ ("fcmp%.x %2,%1; fsun %0" \
  64. : "=dm" (__result) : "f" (x), "f" (y)); \
  65. __result != 0; })
  66. # endif /* GCC 3.1 */
  67. #endif
  68. #if (!defined __NO_MATH_INLINES && defined __OPTIMIZE__) \
  69. || defined __LIBC_INTERNAL_MATH_INLINES
  70. #ifdef __LIBC_INTERNAL_MATH_INLINES
  71. /* This is used when defining the functions themselves. Define them with
  72. __ names, and with `static inline' instead of `extern inline' so the
  73. bodies will always be used, never an external function call. */
  74. # define __m81_u(x) __CONCAT(__,x)
  75. # define __m81_inline static __inline
  76. #else
  77. # define __m81_u(x) x
  78. # ifdef __cplusplus
  79. # define __m81_inline __inline
  80. # else
  81. # define __m81_inline __extern_inline
  82. # endif
  83. # define __M81_MATH_INLINES 1
  84. #endif
  85. /* Define a const math function. */
  86. #define __m81_defun(rettype, func, args) \
  87. __m81_inline rettype __attribute__((__const__)) \
  88. __m81_u(func) args
  89. /* Define the three variants of a math function that has a direct
  90. implementation in the m68k fpu. FUNC is the name for C (which will be
  91. suffixed with f and l for the float and long double version, resp). OP
  92. is the name of the fpu operation (without leading f). */
  93. #if defined __USE_MISC || defined __USE_ISOC99
  94. # define __inline_mathop(func, op) \
  95. __inline_mathop1(double, func, op) \
  96. __inline_mathop1(float, __CONCAT(func,f), op) \
  97. __inline_mathop1(long double, __CONCAT(func,l), op)
  98. #else
  99. # define __inline_mathop(func, op) \
  100. __inline_mathop1(double, func, op)
  101. #endif
  102. #define __inline_mathop1(float_type,func, op) \
  103. __m81_defun (float_type, func, (float_type __mathop_x)) \
  104. { \
  105. float_type __result; \
  106. __asm__("f" __STRING(op) "%.x %1, %0" : "=f" (__result) : "f" (__mathop_x));\
  107. return __result; \
  108. }
  109. __inline_mathop(__atan, atan)
  110. __inline_mathop(__cos, cos)
  111. __inline_mathop(__sin, sin)
  112. __inline_mathop(__tan, tan)
  113. __inline_mathop(__tanh, tanh)
  114. __inline_mathop(__fabs, abs)
  115. #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED || defined __USE_ISOC99
  116. __inline_mathop(__rint, int)
  117. __inline_mathop(__expm1, etoxm1)
  118. __inline_mathop(__log1p, lognp1)
  119. #endif
  120. #ifdef __USE_MISC
  121. __inline_mathop(__significand, getman)
  122. #endif
  123. #ifdef __USE_ISOC99
  124. __inline_mathop(__trunc, intrz)
  125. #endif
  126. #if !defined __NO_MATH_INLINES && defined __OPTIMIZE__
  127. __inline_mathop(atan, atan)
  128. __inline_mathop(cos, cos)
  129. __inline_mathop(sin, sin)
  130. __inline_mathop(tan, tan)
  131. __inline_mathop(tanh, tanh)
  132. # if defined __USE_MISC || defined __USE_XOPEN_EXTENDED || defined __USE_ISOC99
  133. __inline_mathop(rint, int)
  134. __inline_mathop(expm1, etoxm1)
  135. __inline_mathop(log1p, lognp1)
  136. # endif
  137. # ifdef __USE_MISC
  138. __inline_mathop(significand, getman)
  139. # endif
  140. # ifdef __USE_ISOC99
  141. __inline_mathop(trunc, intrz)
  142. # endif
  143. #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
  144. /* This macro contains the definition for the rest of the inline
  145. functions, using FLOAT_TYPE as the domain type and S as the suffix
  146. for the function names. */
  147. #define __inline_functions(float_type, s) \
  148. __m81_defun (float_type, __CONCAT(__floor,s), (float_type __x)) \
  149. { \
  150. float_type __result; \
  151. unsigned long int __ctrl_reg; \
  152. __asm__ __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
  153. /* Set rounding towards negative infinity. */ \
  154. __asm__ __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  155. : "dmi" ((__ctrl_reg & ~0x10) | 0x20)); \
  156. /* Convert X to an integer, using -Inf rounding. */ \
  157. __asm__ __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
  158. /* Restore the previous rounding mode. */ \
  159. __asm__ __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  160. : "dmi" (__ctrl_reg)); \
  161. return __result; \
  162. } \
  163. \
  164. __m81_defun (float_type, __CONCAT(__ceil,s), (float_type __x)) \
  165. { \
  166. float_type __result; \
  167. unsigned long int __ctrl_reg; \
  168. __asm__ __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
  169. /* Set rounding towards positive infinity. */ \
  170. __asm__ __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  171. : "dmi" (__ctrl_reg | 0x30)); \
  172. /* Convert X to an integer, using +Inf rounding. */ \
  173. __asm__ __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
  174. /* Restore the previous rounding mode. */ \
  175. __asm__ __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  176. : "dmi" (__ctrl_reg)); \
  177. return __result; \
  178. }
  179. __inline_functions(double,)
  180. #if defined __USE_MISC || defined __USE_ISOC99
  181. __inline_functions(float,f)
  182. __inline_functions(long double,l)
  183. #endif
  184. #undef __inline_functions
  185. #ifdef __USE_MISC
  186. # define __inline_functions(float_type, s) \
  187. __m81_defun (int, __CONCAT(__isinf,s), (float_type __value)) \
  188. { \
  189. /* There is no branch-condition for infinity, \
  190. so we must extract and examine the condition codes manually. */ \
  191. unsigned long int __fpsr; \
  192. __asm__("ftst%.x %1\n" \
  193. "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
  194. return (__fpsr & (2 << 24)) ? (__fpsr & (8 << 24) ? -1 : 1) : 0; \
  195. } \
  196. \
  197. __m81_defun (int, __CONCAT(__finite,s), (float_type __value)) \
  198. { \
  199. /* There is no branch-condition for infinity, so we must extract and \
  200. examine the condition codes manually. */ \
  201. unsigned long int __fpsr; \
  202. __asm__ ("ftst%.x %1\n" \
  203. "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
  204. return (__fpsr & (3 << 24)) == 0; \
  205. } \
  206. \
  207. __m81_defun (float_type, __CONCAT(__scalbn,s), \
  208. (float_type __x, int __n)) \
  209. { \
  210. float_type __result; \
  211. __asm__ ("fscale%.l %1, %0" : "=f" (__result) : "dmi" (__n), "0" (__x)); \
  212. return __result; \
  213. }
  214. __inline_functions(double,)
  215. __inline_functions(float,f)
  216. __inline_functions(long double,l)
  217. # undef __inline_functions
  218. #endif /* Use misc. */
  219. #if defined __USE_MISC || defined __USE_XOPEN
  220. # define __inline_functions(float_type, s) \
  221. __m81_defun (int, __CONCAT(__isnan,s), (float_type __value)) \
  222. { \
  223. char __result; \
  224. __asm__("ftst%.x %1\n" \
  225. "fsun %0" : "=dm" (__result) : "f" (__value)); \
  226. return __result; \
  227. }
  228. __inline_functions(double,)
  229. # ifdef __USE_MISC
  230. __inline_functions(float,f)
  231. __inline_functions(long double,l)
  232. # endif
  233. # undef __inline_functions
  234. #endif
  235. #ifdef __USE_ISOC99
  236. # define __inline_functions(float_type, s) \
  237. __m81_defun (int, __CONCAT(__signbit,s), (float_type __value)) \
  238. { \
  239. /* There is no branch-condition for the sign bit, so we must extract \
  240. and examine the condition codes manually. */ \
  241. unsigned long int __fpsr; \
  242. __asm__ ("ftst%.x %1\n" \
  243. "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
  244. return (__fpsr >> 27) & 1; \
  245. } \
  246. \
  247. __m81_defun (float_type, __CONCAT(__scalbln,s), \
  248. (float_type __x, long int __n)) \
  249. { \
  250. return __CONCAT(__scalbn,s) (__x, __n); \
  251. } \
  252. \
  253. __m81_defun (float_type, __CONCAT(__nearbyint,s), (float_type __x)) \
  254. { \
  255. float_type __result; \
  256. unsigned long int __ctrl_reg; \
  257. __asm__ __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
  258. /* Temporarily disable the inexact exception. */ \
  259. __asm__ __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  260. : "dmi" (__ctrl_reg & ~0x200)); \
  261. __asm__ __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
  262. __asm__ __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
  263. : "dmi" (__ctrl_reg)); \
  264. return __result; \
  265. } \
  266. \
  267. __m81_defun (long int, __CONCAT(__lrint,s), (float_type __x)) \
  268. { \
  269. long int __result; \
  270. __asm__ ("fmove%.l %1, %0" : "=dm" (__result) : "f" (__x)); \
  271. return __result; \
  272. } \
  273. \
  274. __m81_inline float_type \
  275. __m81_u(__CONCAT(__fma,s))(float_type __x, float_type __y, \
  276. float_type __z) \
  277. { \
  278. return (__x * __y) + __z; \
  279. }
  280. __inline_functions (double,)
  281. __inline_functions (float,f)
  282. __inline_functions (long double,l)
  283. # undef __inline_functions
  284. #endif /* Use ISO C9x */
  285. #ifdef __USE_GNU
  286. # define __inline_functions(float_type, s) \
  287. __m81_inline void \
  288. __m81_u(__CONCAT(__sincos,s))(float_type __x, float_type *__sinx, \
  289. float_type *__cosx) \
  290. { \
  291. __asm__ ("fsincos%.x %2,%1:%0" \
  292. : "=f" (*__sinx), "=f" (*__cosx) : "f" (__x)); \
  293. }
  294. __inline_functions (double,)
  295. __inline_functions (float,f)
  296. __inline_functions (long double,l)
  297. # undef __inline_functions
  298. #endif
  299. #if !defined __NO_MATH_INLINES && defined __OPTIMIZE__
  300. /* Define inline versions of the user visible functions. */
  301. /* Note that there must be no whitespace before the argument passed for
  302. NAME, to make token pasting work correctly with -traditional. */
  303. # define __inline_forward_c(rettype, name, args1, args2) \
  304. __extern_inline rettype __attribute__((__const__)) \
  305. name args1 \
  306. { \
  307. return __CONCAT(__,name) args2; \
  308. }
  309. # define __inline_forward(rettype, name, args1, args2) \
  310. __extern_inline rettype name args1 \
  311. { \
  312. return __CONCAT(__,name) args2; \
  313. }
  314. __inline_forward_c(double,floor, (double __x), (__x))
  315. __inline_forward_c(double,ceil, (double __x), (__x))
  316. # ifdef __USE_MISC
  317. # ifndef __USE_ISOC99 /* Conflict with macro of same name. */
  318. __inline_forward_c(int,isinf, (double __value), (__value))
  319. # endif
  320. __inline_forward_c(int,finite, (double __value), (__value))
  321. __inline_forward_c(double,scalbn, (double __x, int __n), (__x, __n))
  322. # endif
  323. # if defined __USE_MISC || defined __USE_XOPEN
  324. # ifndef __USE_ISOC99 /* Conflict with macro of same name. */
  325. __inline_forward_c(int,isnan, (double __value), (__value))
  326. # endif
  327. # endif
  328. # ifdef __USE_ISOC99
  329. __inline_forward_c(double,scalbln, (double __x, long int __n), (__x, __n))
  330. __inline_forward_c(double,nearbyint, (double __value), (__value))
  331. __inline_forward_c(long int,lrint, (double __value), (__value))
  332. __inline_forward_c(double,fma, (double __x, double __y, double __z),
  333. (__x, __y, __z))
  334. # endif
  335. # ifdef __USE_GNU
  336. __inline_forward(void,sincos, (double __x, double *__sinx, double *__cosx),
  337. (__x, __sinx, __cosx))
  338. # endif
  339. # if defined __USE_MISC || defined __USE_ISOC99
  340. __inline_forward_c(float,floorf, (float __x), (__x))
  341. __inline_forward_c(float,ceilf, (float __x), (__x))
  342. # ifdef __USE_MISC
  343. __inline_forward_c(int,isinff, (float __value), (__value))
  344. __inline_forward_c(int,finitef, (float __value), (__value))
  345. __inline_forward_c(float,scalbnf, (float __x, int __n), (__x, __n))
  346. __inline_forward_c(int,isnanf, (float __value), (__value))
  347. # endif
  348. # ifdef __USE_ISOC99
  349. __inline_forward_c(float,scalblnf, (float __x, long int __n), (__x, __n))
  350. __inline_forward_c(float,nearbyintf, (float __value), (__value))
  351. __inline_forward_c(long int,lrintf, (float __value), (__value))
  352. __inline_forward_c(float,fmaf, (float __x, float __y, float __z),
  353. (__x, __y, __z))
  354. # endif
  355. # ifdef __USE_GNU
  356. __inline_forward(void,sincosf, (float __x, float *__sinx, float *__cosx),
  357. (__x, __sinx, __cosx))
  358. # endif
  359. __inline_forward_c(long double,floorl, (long double __x), (__x))
  360. __inline_forward_c(long double,ceill, (long double __x), (__x))
  361. # ifdef __USE_MISC
  362. __inline_forward_c(int,isinfl, (long double __value), (__value))
  363. __inline_forward_c(int,finitel, (long double __value), (__value))
  364. __inline_forward_c(long double,scalbnl, (long double __x, int __n), (__x, __n))
  365. __inline_forward_c(int,isnanl, (long double __value), (__value))
  366. # endif
  367. # ifdef __USE_ISOC99
  368. __inline_forward_c(long double,scalblnl, (long double __x, long int __n),
  369. (__x, __n))
  370. __inline_forward_c(long double,nearbyintl, (long double __value), (__value))
  371. __inline_forward_c(long int,lrintl, (long double __value), (__value))
  372. __inline_forward_c(long double,fmal,
  373. (long double __x, long double __y, long double __z),
  374. (__x, __y, __z))
  375. # endif
  376. # ifdef __USE_GNU
  377. __inline_forward(void,sincosl,
  378. (long double __x, long double *__sinx, long double *__cosx),
  379. (__x, __sinx, __cosx))
  380. # endif
  381. #endif /* Use misc or ISO C99 */
  382. #undef __inline_forward
  383. #undef __inline_forward_c
  384. #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
  385. #endif
  386. #endif /* GCC. */