mathinline.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* Inline math functions for SPARC.
  2. Copyright (C) 1999, 2000, 2001, 2002, 2004, 2006
  3. Free Software Foundation, Inc.
  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, see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef _MATH_H
  17. # error "Never use <bits/mathinline.h> directly; include <math.h> instead."
  18. #endif
  19. #ifdef __GNUC__
  20. #if defined __USE_ISOC99 && !__GNUC_PREREQ (3, 0)
  21. # undef isgreater
  22. # undef isgreaterequal
  23. # undef isless
  24. # undef islessequal
  25. # undef islessgreater
  26. # undef isunordered
  27. # define __unordered_v9cmp(x, y, op, qop) \
  28. (__extension__ \
  29. ({ unsigned __r; \
  30. if (sizeof (x) == 4 && sizeof (y) == 4) \
  31. { \
  32. float __x = (x); float __y = (y); \
  33. __asm__ ("fcmps\t%%fcc3,%1,%2\n\tmov" op "\t%%fcc3,1,%0" \
  34. : "=r" (__r) : "f" (__x), "f" (__y), "0" (0) : "cc"); \
  35. } \
  36. else if (sizeof (x) <= 8 && sizeof (y) <= 8) \
  37. { \
  38. double __x = (x); double __y = (y); \
  39. __asm__ ("fcmpd\t%%fcc3,%1,%2\n\tmov" op "\t%%fcc3,1,%0" \
  40. : "=r" (__r) : "f" (__x), "f" (__y), "0" (0) : "cc"); \
  41. } \
  42. else \
  43. { \
  44. long double __x = (x); long double __y = (y); \
  45. extern int _Qp_cmp (const long double *a, const long double *b); \
  46. __r = qop; \
  47. } \
  48. __r; }))
  49. # define isgreater(x, y) __unordered_v9cmp(x, y, "g", _Qp_cmp (&__x, &__y) == 2)
  50. # define isgreaterequal(x, y) __unordered_v9cmp(x, y, "ge", (_Qp_cmp (&__x, &__y) & 1) == 0)
  51. # define isless(x, y) __unordered_v9cmp(x, y, "l", _Qp_cmp (&__x, &__y) == 1)
  52. # define islessequal(x, y) __unordered_v9cmp(x, y, "le", (_Qp_cmp (&__x, &__y) & 2) == 0)
  53. # define islessgreater(x, y) __unordered_v9cmp(x, y, "lg", ((_Qp_cmp (&__x, &__y) + 1) & 2) != 0)
  54. # define isunordered(x, y) __unordered_v9cmp(x, y, "u", _Qp_cmp (&__x, &__y) == 3)
  55. #endif /* __USE_ISOC99 */
  56. #if (!defined __NO_MATH_INLINES || defined __LIBC_INTERNAL_MATH_INLINES) && defined __OPTIMIZE__
  57. # ifdef __cplusplus
  58. # define __MATH_INLINE __inline
  59. # else
  60. # define __MATH_INLINE __extern_inline
  61. # endif /* __cplusplus */
  62. /* The gcc, version 2.7 or below, has problems with all this inlining
  63. code. So disable it for this version of the compiler. */
  64. # if __GNUC_PREREQ (2, 8)
  65. # ifdef __USE_ISOC99
  66. /* Test for negative number. Used in the signbit() macro. */
  67. __MATH_INLINE int
  68. __NTH (__signbitf (float __x))
  69. {
  70. __extension__ union { float __f; int __i; } __u = { __f: __x };
  71. return __u.__i < 0;
  72. }
  73. __MATH_INLINE int
  74. __NTH (__signbit (double __x))
  75. {
  76. __extension__ union { double __d; long int __i; } __u = { __d: __x };
  77. return __u.__i < 0;
  78. }
  79. __MATH_INLINE int
  80. __NTH (__signbitl (long double __x))
  81. {
  82. __extension__ union { long double __l; long int __i[2]; } __u = { __l: __x };
  83. return __u.__i[0] < 0;
  84. }
  85. # endif /* __USE_ISOC99 */
  86. # if !defined __NO_MATH_INLINES && !__GNUC_PREREQ (3, 2)
  87. __MATH_INLINE double
  88. __NTH (sqrt (double __x))
  89. {
  90. register double __r;
  91. __asm__ ("fsqrtd %1,%0" : "=f" (__r) : "f" (__x));
  92. return __r;
  93. }
  94. __MATH_INLINE float
  95. __NTH (sqrtf (float __x))
  96. {
  97. register float __r;
  98. __asm__ ("fsqrts %1,%0" : "=f" (__r) : "f" (__x));
  99. return __r;
  100. }
  101. __MATH_INLINE long double
  102. __NTH (sqrtl (long double __x))
  103. {
  104. long double __r;
  105. extern void _Qp_sqrt (long double *, __const__ long double *);
  106. _Qp_sqrt (&__r, &__x);
  107. return __r;
  108. }
  109. # elif !defined __NO_LONG_DOUBLE_MATH
  110. __MATH_INLINE long double
  111. sqrtl (long double __x) __THROW
  112. {
  113. extern long double _Q_sqrt (__const__ long double);
  114. return _Q_sqrt (__x);
  115. }
  116. # endif /* !__NO_MATH_INLINES && !GCC 3.2+ */
  117. /* This code is used internally in the GNU libc. */
  118. # ifdef __LIBC_INTERNAL_MATH_INLINES
  119. __MATH_INLINE double
  120. __ieee754_sqrt (double __x)
  121. {
  122. register double __r;
  123. __asm__ ("fsqrtd %1,%0" : "=f" (__r) : "f" (__x));
  124. return __r;
  125. }
  126. __MATH_INLINE float
  127. __ieee754_sqrtf (float __x)
  128. {
  129. register float __r;
  130. __asm__ ("fsqrts %1,%0" : "=f" (__r) : "f" (__x));
  131. return __r;
  132. }
  133. __MATH_INLINE long double
  134. __ieee754_sqrtl (long double __x)
  135. {
  136. long double __r;
  137. extern void _Qp_sqrt (long double *, __const__ long double *);
  138. _Qp_sqrt(&__r, &__x);
  139. return __r;
  140. }
  141. # elif !defined __NO_LONG_DOUBLE_MATH
  142. __MATH_INLINE long double
  143. __ieee754_sqrtl (long double __x)
  144. {
  145. extern long double _Q_sqrt (__const__ long double);
  146. return _Q_sqrt (__x);
  147. }
  148. # endif /* __LIBC_INTERNAL_MATH_INLINES */
  149. # endif /* gcc 2.8+ */
  150. # ifdef __USE_ISOC99
  151. # ifndef __NO_MATH_INLINES
  152. __MATH_INLINE double __NTH (fdim (double __x, double __y));
  153. __MATH_INLINE double
  154. __NTH (fdim (double __x, double __y))
  155. {
  156. return __x <= __y ? 0 : __x - __y;
  157. }
  158. __MATH_INLINE float __NTH (fdimf (float __x, float __y));
  159. __MATH_INLINE float
  160. __NTH (fdimf (float __x, float __y))
  161. {
  162. return __x <= __y ? 0 : __x - __y;
  163. }
  164. # endif /* !__NO_MATH_INLINES */
  165. # endif /* __USE_ISOC99 */
  166. #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
  167. #endif /* __GNUC__ */