mathinline.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* Inline math functions for SPARC.
  2. Copyright (C) 1999, 2000, 2001, 2002, 2004, 2006
  3. Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. Contributed by Jakub Jelinek <jakub@redhat.com>.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Lesser General Public
  8. License as published by the Free Software Foundation; either
  9. version 2.1 of the License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public
  15. License along with the GNU C Library; if not, see
  16. <http://www.gnu.org/licenses/>. */
  17. #ifndef _MATH_H
  18. # error "Never use <bits/mathinline.h> directly; include <math.h> instead."
  19. #endif
  20. #include <bits/wordsize.h>
  21. #ifdef __GNUC__
  22. #if defined __USE_ISOC99 && !__GNUC_PREREQ (3, 0)
  23. # undef isgreater
  24. # undef isgreaterequal
  25. # undef isless
  26. # undef islessequal
  27. # undef islessgreater
  28. # undef isunordered
  29. # if __WORDSIZE == 32
  30. # ifndef __NO_LONG_DOUBLE_MATH
  31. # define __unordered_cmp(x, y) \
  32. (__extension__ \
  33. ({ unsigned __r; \
  34. if (sizeof (x) == 4 && sizeof (y) == 4) \
  35. { \
  36. float __x = (x); float __y = (y); \
  37. __asm__ ("fcmps %1,%2; st %%fsr, %0" : "=m" (__r) : "f" (__x), \
  38. "f" (__y) : "cc"); \
  39. } \
  40. else if (sizeof (x) <= 8 && sizeof (y) <= 8) \
  41. { \
  42. double __x = (x); double __y = (y); \
  43. __asm__ ("fcmpd\t%1,%2\n\tst\t%%fsr,%0" : "=m" (__r) : "f" (__x), \
  44. "f" (__y) : "cc"); \
  45. } \
  46. else \
  47. { \
  48. long double __x = (x); long double __y = (y); \
  49. extern int _Q_cmp (const long double a, const long double b); \
  50. __r = _Q_cmp (__x, __y) << 10; \
  51. } \
  52. __r; }))
  53. # else
  54. # define __unordered_cmp(x, y) \
  55. (__extension__ \
  56. ({ unsigned __r; \
  57. if (sizeof (x) == 4 && sizeof (y) == 4) \
  58. { \
  59. float __x = (x); float __y = (y); \
  60. __asm__ ("fcmps %1,%2; st %%fsr, %0" : "=m" (__r) : "f" (__x), \
  61. "f" (__y) : "cc"); \
  62. } \
  63. else \
  64. { \
  65. double __x = (x); double __y = (y); \
  66. __asm__ ("fcmpd\t%1,%2\n\tst\t%%fsr,%0" : "=m" (__r) : "f" (__x), \
  67. "f" (__y) : "cc"); \
  68. } \
  69. __r; }))
  70. # endif
  71. # define isgreater(x, y) ((__unordered_cmp (x, y) & (3 << 10)) == (2 << 10))
  72. # define isgreaterequal(x, y) ((__unordered_cmp (x, y) & (1 << 10)) == 0)
  73. # define isless(x, y) ((__unordered_cmp (x, y) & (3 << 10)) == (1 << 10))
  74. # define islessequal(x, y) ((__unordered_cmp (x, y) & (2 << 10)) == 0)
  75. # define islessgreater(x, y) (((__unordered_cmp (x, y) + (1 << 10)) & (2 << 10)) != 0)
  76. # define isunordered(x, y) ((__unordered_cmp (x, y) & (3 << 10)) == (3 << 10))
  77. # else /* sparc64 */
  78. # define __unordered_v9cmp(x, y, op, qop) \
  79. (__extension__ \
  80. ({ unsigned __r; \
  81. if (sizeof (x) == 4 && sizeof (y) == 4) \
  82. { \
  83. float __x = (x); float __y = (y); \
  84. __asm__ ("fcmps\t%%fcc3,%1,%2\n\tmov" op "\t%%fcc3,1,%0" \
  85. : "=r" (__r) : "f" (__x), "f" (__y), "0" (0) : "cc"); \
  86. } \
  87. else if (sizeof (x) <= 8 && sizeof (y) <= 8) \
  88. { \
  89. double __x = (x); double __y = (y); \
  90. __asm__ ("fcmpd\t%%fcc3,%1,%2\n\tmov" op "\t%%fcc3,1,%0" \
  91. : "=r" (__r) : "f" (__x), "f" (__y), "0" (0) : "cc"); \
  92. } \
  93. else \
  94. { \
  95. long double __x = (x); long double __y = (y); \
  96. extern int _Qp_cmp (const long double *a, const long double *b); \
  97. __r = qop; \
  98. } \
  99. __r; }))
  100. # define isgreater(x, y) __unordered_v9cmp(x, y, "g", _Qp_cmp (&__x, &__y) == 2)
  101. # define isgreaterequal(x, y) __unordered_v9cmp(x, y, "ge", (_Qp_cmp (&__x, &__y) & 1) == 0)
  102. # define isless(x, y) __unordered_v9cmp(x, y, "l", _Qp_cmp (&__x, &__y) == 1)
  103. # define islessequal(x, y) __unordered_v9cmp(x, y, "le", (_Qp_cmp (&__x, &__y) & 2) == 0)
  104. # define islessgreater(x, y) __unordered_v9cmp(x, y, "lg", ((_Qp_cmp (&__x, &__y) + 1) & 2) != 0)
  105. # define isunordered(x, y) __unordered_v9cmp(x, y, "u", _Qp_cmp (&__x, &__y) == 3)
  106. # endif /* sparc64 */
  107. #endif /* __USE_ISOC99 */
  108. #if (!defined __NO_MATH_INLINES || defined __LIBC_INTERNAL_MATH_INLINES) && defined __OPTIMIZE__
  109. # ifdef __cplusplus
  110. # define __MATH_INLINE __inline
  111. # else
  112. # define __MATH_INLINE __extern_inline
  113. # endif /* __cplusplus */
  114. /* The gcc, version 2.7 or below, has problems with all this inlining
  115. code. So disable it for this version of the compiler. */
  116. # if __GNUC_PREREQ (2, 8)
  117. # ifdef __USE_ISOC99
  118. /* Test for negative number. Used in the signbit() macro. */
  119. __MATH_INLINE int
  120. __NTH (__signbitf (float __x))
  121. {
  122. __extension__ union { float __f; int __i; } __u = { __f: __x };
  123. return __u.__i < 0;
  124. }
  125. # if __WORDSIZE == 32
  126. __MATH_INLINE int
  127. __NTH (__signbit (double __x))
  128. {
  129. __extension__ union { double __d; int __i[2]; } __u = { __d: __x };
  130. return __u.__i[0] < 0;
  131. }
  132. # ifndef __NO_LONG_DOUBLE_MATH
  133. __MATH_INLINE int
  134. __NTH (__signbitl (long double __x))
  135. {
  136. __extension__ union { long double __l; int __i[4]; } __u = { __l: __x };
  137. return __u.__i[0] < 0;
  138. }
  139. # else
  140. __MATH_INLINE int
  141. __NTH (__signbitl (long double __x))
  142. {
  143. return __signbit ((double)__x);
  144. }
  145. # endif
  146. # else /* sparc64 */
  147. __MATH_INLINE int
  148. __NTH (__signbit (double __x))
  149. {
  150. __extension__ union { double __d; long int __i; } __u = { __d: __x };
  151. return __u.__i < 0;
  152. }
  153. __MATH_INLINE int
  154. __NTH (__signbitl (long double __x))
  155. {
  156. __extension__ union { long double __l; long int __i[2]; } __u = { __l: __x };
  157. return __u.__i[0] < 0;
  158. }
  159. # endif /* sparc64 */
  160. # endif /* __USE_ISOC99 */
  161. # if !defined __NO_MATH_INLINES && !__GNUC_PREREQ (3, 2)
  162. __MATH_INLINE double
  163. __NTH (sqrt (double __x))
  164. {
  165. register double __r;
  166. __asm__ ("fsqrtd %1,%0" : "=f" (__r) : "f" (__x));
  167. return __r;
  168. }
  169. __MATH_INLINE float
  170. __NTH (sqrtf (float __x))
  171. {
  172. register float __r;
  173. __asm__ ("fsqrts %1,%0" : "=f" (__r) : "f" (__x));
  174. return __r;
  175. }
  176. # if __WORDSIZE == 64
  177. __MATH_INLINE long double
  178. __NTH (sqrtl (long double __x))
  179. {
  180. long double __r;
  181. extern void _Qp_sqrt (long double *, __const__ long double *);
  182. _Qp_sqrt (&__r, &__x);
  183. return __r;
  184. }
  185. # elif !defined __NO_LONG_DOUBLE_MATH
  186. __MATH_INLINE long double
  187. sqrtl (long double __x) __THROW
  188. {
  189. extern long double _Q_sqrt (__const__ long double);
  190. return _Q_sqrt (__x);
  191. }
  192. # endif /* sparc64 */
  193. # endif /* !__NO_MATH_INLINES && !GCC 3.2+ */
  194. /* This code is used internally in the GNU libc. */
  195. # ifdef __LIBC_INTERNAL_MATH_INLINES
  196. __MATH_INLINE double
  197. __ieee754_sqrt (double __x)
  198. {
  199. register double __r;
  200. __asm__ ("fsqrtd %1,%0" : "=f" (__r) : "f" (__x));
  201. return __r;
  202. }
  203. __MATH_INLINE float
  204. __ieee754_sqrtf (float __x)
  205. {
  206. register float __r;
  207. __asm__ ("fsqrts %1,%0" : "=f" (__r) : "f" (__x));
  208. return __r;
  209. }
  210. # if __WORDSIZE == 64
  211. __MATH_INLINE long double
  212. __ieee754_sqrtl (long double __x)
  213. {
  214. long double __r;
  215. extern void _Qp_sqrt (long double *, __const__ long double *);
  216. _Qp_sqrt(&__r, &__x);
  217. return __r;
  218. }
  219. # elif !defined __NO_LONG_DOUBLE_MATH
  220. __MATH_INLINE long double
  221. __ieee754_sqrtl (long double __x)
  222. {
  223. extern long double _Q_sqrt (__const__ long double);
  224. return _Q_sqrt (__x);
  225. }
  226. # endif /* sparc64 */
  227. # endif /* __LIBC_INTERNAL_MATH_INLINES */
  228. # endif /* gcc 2.8+ */
  229. # ifdef __USE_ISOC99
  230. # ifndef __NO_MATH_INLINES
  231. __MATH_INLINE double __NTH (fdim (double __x, double __y));
  232. __MATH_INLINE double
  233. __NTH (fdim (double __x, double __y))
  234. {
  235. return __x <= __y ? 0 : __x - __y;
  236. }
  237. __MATH_INLINE float __NTH (fdimf (float __x, float __y));
  238. __MATH_INLINE float
  239. __NTH (fdimf (float __x, float __y))
  240. {
  241. return __x <= __y ? 0 : __x - __y;
  242. }
  243. # endif /* !__NO_MATH_INLINES */
  244. # endif /* __USE_ISOC99 */
  245. #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
  246. #endif /* __GNUC__ */