mathinline.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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, write to the Free
  16. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  17. 02111-1307 USA. */
  18. #ifndef _MATH_H
  19. # error "Never use <bits/mathinline.h> directly; include <math.h> instead."
  20. #endif
  21. #include <bits/wordsize.h>
  22. #ifdef __GNUC__
  23. #if defined __USE_ISOC99 && !__GNUC_PREREQ (3, 0)
  24. # undef isgreater
  25. # undef isgreaterequal
  26. # undef isless
  27. # undef islessequal
  28. # undef islessgreater
  29. # undef isunordered
  30. # if __WORDSIZE == 32
  31. # ifndef __NO_LONG_DOUBLE_MATH
  32. # define __unordered_cmp(x, y) \
  33. (__extension__ \
  34. ({ unsigned __r; \
  35. if (sizeof (x) == 4 && sizeof (y) == 4) \
  36. { \
  37. float __x = (x); float __y = (y); \
  38. __asm__ ("fcmps %1,%2; st %%fsr, %0" : "=m" (__r) : "f" (__x), \
  39. "f" (__y) : "cc"); \
  40. } \
  41. else if (sizeof (x) <= 8 && sizeof (y) <= 8) \
  42. { \
  43. double __x = (x); double __y = (y); \
  44. __asm__ ("fcmpd\t%1,%2\n\tst\t%%fsr,%0" : "=m" (__r) : "f" (__x), \
  45. "f" (__y) : "cc"); \
  46. } \
  47. else \
  48. { \
  49. long double __x = (x); long double __y = (y); \
  50. extern int _Q_cmp (const long double a, const long double b); \
  51. __r = _Q_cmp (__x, __y) << 10; \
  52. } \
  53. __r; }))
  54. # else
  55. # define __unordered_cmp(x, y) \
  56. (__extension__ \
  57. ({ unsigned __r; \
  58. if (sizeof (x) == 4 && sizeof (y) == 4) \
  59. { \
  60. float __x = (x); float __y = (y); \
  61. __asm__ ("fcmps %1,%2; st %%fsr, %0" : "=m" (__r) : "f" (__x), \
  62. "f" (__y) : "cc"); \
  63. } \
  64. else \
  65. { \
  66. double __x = (x); double __y = (y); \
  67. __asm__ ("fcmpd\t%1,%2\n\tst\t%%fsr,%0" : "=m" (__r) : "f" (__x), \
  68. "f" (__y) : "cc"); \
  69. } \
  70. __r; }))
  71. # endif
  72. # define isgreater(x, y) ((__unordered_cmp (x, y) & (3 << 10)) == (2 << 10))
  73. # define isgreaterequal(x, y) ((__unordered_cmp (x, y) & (1 << 10)) == 0)
  74. # define isless(x, y) ((__unordered_cmp (x, y) & (3 << 10)) == (1 << 10))
  75. # define islessequal(x, y) ((__unordered_cmp (x, y) & (2 << 10)) == 0)
  76. # define islessgreater(x, y) (((__unordered_cmp (x, y) + (1 << 10)) & (2 << 10)) != 0)
  77. # define isunordered(x, y) ((__unordered_cmp (x, y) & (3 << 10)) == (3 << 10))
  78. # else /* sparc64 */
  79. # define __unordered_v9cmp(x, y, op, qop) \
  80. (__extension__ \
  81. ({ unsigned __r; \
  82. if (sizeof (x) == 4 && sizeof (y) == 4) \
  83. { \
  84. float __x = (x); float __y = (y); \
  85. __asm__ ("fcmps\t%%fcc3,%1,%2\n\tmov" op "\t%%fcc3,1,%0" \
  86. : "=r" (__r) : "f" (__x), "f" (__y), "0" (0) : "cc"); \
  87. } \
  88. else if (sizeof (x) <= 8 && sizeof (y) <= 8) \
  89. { \
  90. double __x = (x); double __y = (y); \
  91. __asm__ ("fcmpd\t%%fcc3,%1,%2\n\tmov" op "\t%%fcc3,1,%0" \
  92. : "=r" (__r) : "f" (__x), "f" (__y), "0" (0) : "cc"); \
  93. } \
  94. else \
  95. { \
  96. long double __x = (x); long double __y = (y); \
  97. extern int _Qp_cmp (const long double *a, const long double *b); \
  98. __r = qop; \
  99. } \
  100. __r; }))
  101. # define isgreater(x, y) __unordered_v9cmp(x, y, "g", _Qp_cmp (&__x, &__y) == 2)
  102. # define isgreaterequal(x, y) __unordered_v9cmp(x, y, "ge", (_Qp_cmp (&__x, &__y) & 1) == 0)
  103. # define isless(x, y) __unordered_v9cmp(x, y, "l", _Qp_cmp (&__x, &__y) == 1)
  104. # define islessequal(x, y) __unordered_v9cmp(x, y, "le", (_Qp_cmp (&__x, &__y) & 2) == 0)
  105. # define islessgreater(x, y) __unordered_v9cmp(x, y, "lg", ((_Qp_cmp (&__x, &__y) + 1) & 2) != 0)
  106. # define isunordered(x, y) __unordered_v9cmp(x, y, "u", _Qp_cmp (&__x, &__y) == 3)
  107. # endif /* sparc64 */
  108. #endif /* __USE_ISOC99 */
  109. #if (!defined __NO_MATH_INLINES || defined __LIBC_INTERNAL_MATH_INLINES) && defined __OPTIMIZE__
  110. # ifdef __cplusplus
  111. # define __MATH_INLINE __inline
  112. # else
  113. # define __MATH_INLINE extern __inline
  114. # endif /* __cplusplus */
  115. /* The gcc, version 2.7 or below, has problems with all this inlining
  116. code. So disable it for this version of the compiler. */
  117. # if __GNUC_PREREQ (2, 8)
  118. # ifdef __USE_ISOC99
  119. /* Test for negative number. Used in the signbit() macro. */
  120. __MATH_INLINE int
  121. __NTH (__signbitf (float __x))
  122. {
  123. __extension__ union { float __f; int __i; } __u = { __f: __x };
  124. return __u.__i < 0;
  125. }
  126. # if __WORDSIZE == 32
  127. __MATH_INLINE int
  128. __NTH (__signbit (double __x))
  129. {
  130. __extension__ union { double __d; int __i[2]; } __u = { __d: __x };
  131. return __u.__i[0] < 0;
  132. }
  133. # ifndef __NO_LONG_DOUBLE_MATH
  134. __MATH_INLINE int
  135. __NTH (__signbitl (long double __x))
  136. {
  137. __extension__ union { long double __l; int __i[4]; } __u = { __l: __x };
  138. return __u.__i[0] < 0;
  139. }
  140. # else
  141. __MATH_INLINE int
  142. __NTH (__signbitl (long double __x))
  143. {
  144. return __signbit ((double)__x);
  145. }
  146. # endif
  147. # else /* sparc64 */
  148. __MATH_INLINE int
  149. __NTH (__signbit (double __x))
  150. {
  151. __extension__ union { double __d; long int __i; } __u = { __d: __x };
  152. return __u.__i < 0;
  153. }
  154. __MATH_INLINE int
  155. __NTH (__signbitl (long double __x))
  156. {
  157. __extension__ union { long double __l; long int __i[2]; } __u = { __l: __x };
  158. return __u.__i[0] < 0;
  159. }
  160. # endif /* sparc64 */
  161. # endif /* __USE_ISOC99 */
  162. # if !defined __NO_MATH_INLINES && !__GNUC_PREREQ (3, 2)
  163. __MATH_INLINE double
  164. __NTH (sqrt (double __x))
  165. {
  166. register double __r;
  167. __asm__ ("fsqrtd %1,%0" : "=f" (__r) : "f" (__x));
  168. return __r;
  169. }
  170. __MATH_INLINE float
  171. __NTH (sqrtf (float __x))
  172. {
  173. register float __r;
  174. __asm__ ("fsqrts %1,%0" : "=f" (__r) : "f" (__x));
  175. return __r;
  176. }
  177. # if __WORDSIZE == 64
  178. __MATH_INLINE long double
  179. __NTH (sqrtl (long double __x))
  180. {
  181. long double __r;
  182. extern void _Qp_sqrt (long double *, __const__ long double *);
  183. _Qp_sqrt (&__r, &__x);
  184. return __r;
  185. }
  186. # elif !defined __NO_LONG_DOUBLE_MATH
  187. __MATH_INLINE long double
  188. sqrtl (long double __x) __THROW
  189. {
  190. extern long double _Q_sqrt (__const__ long double);
  191. return _Q_sqrt (__x);
  192. }
  193. # endif /* sparc64 */
  194. # endif /* !__NO_MATH_INLINES && !GCC 3.2+ */
  195. /* This code is used internally in the GNU libc. */
  196. # ifdef __LIBC_INTERNAL_MATH_INLINES
  197. __MATH_INLINE double
  198. __ieee754_sqrt (double __x)
  199. {
  200. register double __r;
  201. __asm__ ("fsqrtd %1,%0" : "=f" (__r) : "f" (__x));
  202. return __r;
  203. }
  204. __MATH_INLINE float
  205. __ieee754_sqrtf (float __x)
  206. {
  207. register float __r;
  208. __asm__ ("fsqrts %1,%0" : "=f" (__r) : "f" (__x));
  209. return __r;
  210. }
  211. # if __WORDSIZE == 64
  212. __MATH_INLINE long double
  213. __ieee754_sqrtl (long double __x)
  214. {
  215. long double __r;
  216. extern void _Qp_sqrt (long double *, __const__ long double *);
  217. _Qp_sqrt(&__r, &__x);
  218. return __r;
  219. }
  220. # elif !defined __NO_LONG_DOUBLE_MATH
  221. __MATH_INLINE long double
  222. __ieee754_sqrtl (long double __x)
  223. {
  224. extern long double _Q_sqrt (__const__ long double);
  225. return _Q_sqrt (__x);
  226. }
  227. # endif /* sparc64 */
  228. # endif /* __LIBC_INTERNAL_MATH_INLINES */
  229. # endif /* gcc 2.8+ */
  230. # ifdef __USE_ISOC99
  231. # ifndef __NO_MATH_INLINES
  232. __MATH_INLINE double __NTH (fdim (double __x, double __y));
  233. __MATH_INLINE double
  234. __NTH (fdim (double __x, double __y))
  235. {
  236. return __x <= __y ? 0 : __x - __y;
  237. }
  238. __MATH_INLINE float __NTH (fdimf (float __x, float __y));
  239. __MATH_INLINE float
  240. __NTH (fdimf (float __x, float __y))
  241. {
  242. return __x <= __y ? 0 : __x - __y;
  243. }
  244. # endif /* !__NO_MATH_INLINES */
  245. # endif /* __USE_ISOC99 */
  246. #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
  247. #endif /* __GNUC__ */