mathinline.h 15 KB

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