mathinline.h 5.5 KB

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