mathinline.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /* Inline math functions for SPARC.
  2. Copyright (C) 1999, 2000 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. #if defined __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. /* Test for negative number. Used in the signbit() macro. */
  87. __MATH_INLINE int
  88. __signbitf (float __x) __THROW
  89. {
  90. __extension__ union { float __f; int __i; } __u = { __f: __x };
  91. return __u.__i < 0;
  92. }
  93. #if __WORDSIZE == 32
  94. __MATH_INLINE int
  95. __signbit (double __x) __THROW
  96. {
  97. __extension__ union { double __d; int __i[2]; } __u = { __d: __x };
  98. return __u.__i[0] < 0;
  99. }
  100. __MATH_INLINE int
  101. __signbitl (long double __x) __THROW
  102. {
  103. return __signbit ((double)__x);
  104. }
  105. #else /* sparc64 */
  106. __MATH_INLINE int
  107. __signbit (double __x) __THROW
  108. {
  109. __extension__ union { double __d; long __i; } __u = { __d: __x };
  110. return __u.__i < 0;
  111. }
  112. __MATH_INLINE int
  113. __signbitl (long double __x) __THROW
  114. {
  115. __extension__ union { long double __l; long __i[2]; } __u = { __l: __x };
  116. return __u.__i[0] < 0;
  117. }
  118. #endif /* sparc64 */
  119. #ifndef __NO_MATH_INLINES
  120. __MATH_INLINE double
  121. sqrt(double __x) __THROW
  122. {
  123. register double __r;
  124. __asm ("fsqrtd %1,%0" : "=f" (__r) : "f" (__x));
  125. return __r;
  126. }
  127. __MATH_INLINE float
  128. sqrtf(float __x) __THROW
  129. {
  130. register float __r;
  131. __asm ("fsqrts %1,%0" : "=f" (__r) : "f" (__x));
  132. return __r;
  133. }
  134. #if __WORDSIZE == 64
  135. __MATH_INLINE long double
  136. sqrtl(long double __x) __THROW
  137. {
  138. long double __r;
  139. extern void _Qp_sqrt(long double *, __const__ long double *);
  140. _Qp_sqrt(&__r, &__x);
  141. return __r;
  142. }
  143. #endif /* sparc64 */
  144. #endif
  145. /* This code is used internally in the GNU libc. */
  146. #ifdef __LIBC_INTERNAL_MATH_INLINES
  147. __MATH_INLINE double
  148. __ieee754_sqrt(double __x)
  149. {
  150. register double __r;
  151. __asm ("fsqrtd %1,%0" : "=f" (__r) : "f" (__x));
  152. return __r;
  153. }
  154. __MATH_INLINE float
  155. __ieee754_sqrtf(float __x)
  156. {
  157. register float __r;
  158. __asm ("fsqrts %1,%0" : "=f" (__r) : "f" (__x));
  159. return __r;
  160. }
  161. #if __WORDSIZE == 64
  162. __MATH_INLINE long double
  163. __ieee754_sqrtl(long double __x)
  164. {
  165. long double __r;
  166. extern void _Qp_sqrt(long double *, __const__ long double *);
  167. _Qp_sqrt(&__r, &__x);
  168. return __r;
  169. }
  170. #endif /* sparc64 */
  171. #endif /* __LIBC_INTERNAL_MATH_INLINES */
  172. #endif /* gcc 2.8+ */
  173. #ifdef __USE_ISOC99
  174. #ifndef __NO_MATH_INLINES
  175. __MATH_INLINE double fdim (double __x, double __y);
  176. __MATH_INLINE double
  177. fdim (double __x, double __y)
  178. {
  179. return __x < __y ? 0 : __x - __y;
  180. }
  181. __MATH_INLINE float fdimf (float __x, float __y);
  182. __MATH_INLINE float
  183. fdimf (float __x, float __y)
  184. {
  185. return __x < __y ? 0 : __x - __y;
  186. }
  187. #endif /* !__NO_MATH_INLINES */
  188. #endif /* __USE_ISOC99 */
  189. #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
  190. #endif /* __GNUC__ */