mathinline.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* Inline math functions for SPARC.
  2. Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Jakub Jelinek <jakub@redhat.com>.
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  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. #ifdef __USE_ISOC99
  23. # if __WORDSIZE == 32
  24. # define __unordered_cmp(x, y) \
  25. (__extension__ \
  26. ({ unsigned __r; \
  27. if (sizeof(x) == 4 && sizeof(y) == 4) \
  28. { \
  29. float __x = (x); float __y = (y); \
  30. __asm__("fcmps %1,%2; st %%fsr, %0" : "=m" (__r) : "f" (__x), \
  31. "f" (__y) : "cc"); \
  32. } \
  33. else \
  34. { \
  35. double __x = (x); double __y = (y); \
  36. __asm__("fcmpd\t%1,%2\n\tst\t%%fsr,%0" : "=m" (__r) : "f" (__x), \
  37. "f" (__y) : "cc"); \
  38. } \
  39. __r; }))
  40. # define isgreater(x, y) ((__unordered_cmp (x, y) & (3 << 10)) == (2 << 10))
  41. # define isgreaterequal(x, y) ((__unordered_cmp (x, y) & (1 << 10)) == 0)
  42. # define isless(x, y) ((__unordered_cmp (x, y) & (3 << 10)) == (1 << 10))
  43. # define islessequal(x, y) ((__unordered_cmp (x, y) & (2 << 10)) == 0)
  44. # define islessgreater(x, y) (((__unordered_cmp (x, y) + (1 << 10)) & (2 << 10)) != 0)
  45. # define isunordered(x, y) ((__unordered_cmp (x, y) & (3 << 10)) == (3 << 10))
  46. # else /* sparc64 */
  47. # define __unordered_v9cmp(x, y, op, qop) \
  48. (__extension__ \
  49. ({ unsigned __r; \
  50. if (sizeof(x) == 4 && sizeof(y) == 4) \
  51. { \
  52. float __x = (x); float __y = (y); \
  53. __asm__("fcmps\t%%fcc3,%1,%2\n\tmov" op "\t%%fcc3,1,%0" \
  54. : "=r" (__r) : "f" (__x), "f" (__y), "0" (0) : "cc"); \
  55. } \
  56. else if (sizeof(x) <= 8 && sizeof(y) <= 8) \
  57. { \
  58. double __x = (x); double __y = (y); \
  59. __asm__("fcmpd\t%%fcc3,%1,%2\n\tmov" op "\t%%fcc3,1,%0" \
  60. : "=r" (__r) : "f" (__x), "f" (__y), "0" (0) : "cc"); \
  61. } \
  62. else \
  63. { \
  64. long double __x = (x); long double __y = (y); \
  65. extern int _Qp_cmp(const long double *a, const long double *b); \
  66. __r = qop; \
  67. } \
  68. __r; }))
  69. # define isgreater(x, y) __unordered_v9cmp(x, y, "g", _Qp_cmp (&__x, &__y) == 2)
  70. # define isgreaterequal(x, y) __unordered_v9cmp(x, y, "ge", (_Qp_cmp (&__x, &__y) & 1) == 0)
  71. # define isless(x, y) __unordered_v9cmp(x, y, "l", _Qp_cmp (&__x, &__y) == 1)
  72. # define islessequal(x, y) __unordered_v9cmp(x, y, "le", (_Qp_cmp (&__x, &__y) & 2) == 0)
  73. # define islessgreater(x, y) __unordered_v9cmp(x, y, "lg", ((_Qp_cmp (&__x, &__y) + 1) & 2) != 0)
  74. # define isunordered(x, y) __unordered_v9cmp(x, y, "u", _Qp_cmp (&__x, &__y) == 3)
  75. # endif /* sparc64 */
  76. #endif /* __USE_ISOC99 */
  77. #if (!defined __NO_MATH_INLINES || defined __LIBC_INTERNAL_MATH_INLINES) && defined __OPTIMIZE__
  78. # ifdef __cplusplus
  79. # define __MATH_INLINE __inline
  80. # else
  81. # define __MATH_INLINE extern __inline
  82. # endif /* __cplusplus */
  83. /* The gcc, version 2.7 or below, has problems with all this inlining
  84. code. So disable it for this version of the compiler. */
  85. # if __GNUC_PREREQ (2, 8)
  86. # ifdef __USE_ISOC99
  87. /* Test for negative number. Used in the signbit() macro. */
  88. __MATH_INLINE int
  89. __signbitf (float __x) __THROW
  90. {
  91. __extension__ union { float __f; int __i; } __u = { __f: __x };
  92. return __u.__i < 0;
  93. }
  94. # if __WORDSIZE == 32
  95. __MATH_INLINE int
  96. __signbit (double __x) __THROW
  97. {
  98. __extension__ union { double __d; int __i[2]; } __u = { __d: __x };
  99. return __u.__i[0] < 0;
  100. }
  101. __MATH_INLINE int
  102. __signbitl (long double __x) __THROW
  103. {
  104. return __signbit ((double)__x);
  105. }
  106. # else /* sparc64 */
  107. __MATH_INLINE int
  108. __signbit (double __x) __THROW
  109. {
  110. __extension__ union { double __d; long int __i; } __u = { __d: __x };
  111. return __u.__i < 0;
  112. }
  113. __MATH_INLINE int
  114. __signbitl (long double __x) __THROW
  115. {
  116. __extension__ union { long double __l; long int __i[2]; } __u = { __l: __x };
  117. return __u.__i[0] < 0;
  118. }
  119. # endif /* sparc64 */
  120. # endif /* __USE_ISOC99 */
  121. # ifndef __NO_MATH_INLINES
  122. __MATH_INLINE double
  123. sqrt (double __x) __THROW
  124. {
  125. register double __r;
  126. __asm ("fsqrtd %1,%0" : "=f" (__r) : "f" (__x));
  127. return __r;
  128. }
  129. __MATH_INLINE float
  130. sqrtf (float __x) __THROW
  131. {
  132. register float __r;
  133. __asm ("fsqrts %1,%0" : "=f" (__r) : "f" (__x));
  134. return __r;
  135. }
  136. # if __WORDSIZE == 64
  137. __MATH_INLINE long double
  138. sqrtl (long double __x) __THROW
  139. {
  140. long double __r;
  141. extern void _Qp_sqrt (long double *, __const__ long double *);
  142. _Qp_sqrt (&__r, &__x);
  143. return __r;
  144. }
  145. # endif /* sparc64 */
  146. # endif /* !__NO_MATH_INLINES */
  147. /* This code is used internally in the GNU libc. */
  148. # ifdef __LIBC_INTERNAL_MATH_INLINES
  149. __MATH_INLINE double
  150. __ieee754_sqrt (double __x)
  151. {
  152. register double __r;
  153. __asm ("fsqrtd %1,%0" : "=f" (__r) : "f" (__x));
  154. return __r;
  155. }
  156. __MATH_INLINE float
  157. __ieee754_sqrtf (float __x)
  158. {
  159. register float __r;
  160. __asm ("fsqrts %1,%0" : "=f" (__r) : "f" (__x));
  161. return __r;
  162. }
  163. # if __WORDSIZE == 64
  164. __MATH_INLINE long double
  165. __ieee754_sqrtl (long double __x)
  166. {
  167. long double __r;
  168. extern void _Qp_sqrt (long double *, __const__ long double *);
  169. _Qp_sqrt(&__r, &__x);
  170. return __r;
  171. }
  172. # endif /* sparc64 */
  173. # endif /* __LIBC_INTERNAL_MATH_INLINES */
  174. # endif /* gcc 2.8+ */
  175. # ifdef __USE_ISOC99
  176. # ifndef __NO_MATH_INLINES
  177. __MATH_INLINE double fdim (double __x, double __y);
  178. __MATH_INLINE double
  179. fdim (double __x, double __y)
  180. {
  181. return __x < __y ? 0 : __x - __y;
  182. }
  183. __MATH_INLINE float fdimf (float __x, float __y);
  184. __MATH_INLINE float
  185. fdimf (float __x, float __y)
  186. {
  187. return __x < __y ? 0 : __x - __y;
  188. }
  189. # endif /* !__NO_MATH_INLINES */
  190. # endif /* __USE_ISOC99 */
  191. #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
  192. #endif /* __GNUC__ */