mathinline.h 15 KB

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