mathinline.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /* Inline math functions for Alpha.
  2. Copyright (C) 1996, 1997, 1999-2001, 2004 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by David Mosberger-Tang.
  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. #ifdef __cplusplus
  21. # define __MATH_INLINE __inline
  22. #else
  23. # define __MATH_INLINE extern __inline
  24. #endif
  25. #if defined __USE_ISOC99 && defined __GNUC__ && !__GNUC_PREREQ(3,0)
  26. # undef isgreater
  27. # undef isgreaterequal
  28. # undef isless
  29. # undef islessequal
  30. # undef islessgreater
  31. # undef isunordered
  32. # define isunordered(u, v) \
  33. (__extension__ \
  34. ({ double __r, __u = (u), __v = (v); \
  35. __asm ("cmptun/su %1,%2,%0\n\ttrapb" \
  36. : "=&f" (__r) : "f" (__u), "f"(__v)); \
  37. __r != 0; }))
  38. #endif /* ISO C99 */
  39. #if (!defined __NO_MATH_INLINES || defined __LIBC_INTERNAL_MATH_INLINES) \
  40. && defined __OPTIMIZE__
  41. #if !__GNUC_PREREQ (4, 0)
  42. # define __inline_copysign(NAME, TYPE) \
  43. __MATH_INLINE TYPE \
  44. __NTH (NAME (TYPE __x, TYPE __y)) \
  45. { \
  46. TYPE __z; \
  47. __asm ("cpys %1, %2, %0" : "=f" (__z) : "f" (__y), "f" (__x)); \
  48. return __z; \
  49. }
  50. __inline_copysign (__copysignf, float)
  51. __inline_copysign (copysignf, float)
  52. __inline_copysign (__copysign, double)
  53. __inline_copysign (copysign, double)
  54. # undef __inline_copysign
  55. #endif
  56. #if !__GNUC_PREREQ (2, 8)
  57. # define __inline_fabs(NAME, TYPE) \
  58. __MATH_INLINE TYPE \
  59. __NTH (NAME (TYPE __x)) \
  60. { \
  61. TYPE __z; \
  62. __asm ("cpys $f31, %1, %0" : "=f" (__z) : "f" (__x)); \
  63. return __z; \
  64. }
  65. __inline_fabs (__fabsf, float)
  66. __inline_fabs (fabsf, float)
  67. __inline_fabs (__fabs, double)
  68. __inline_fabs (fabs, double)
  69. # undef __inline_fabs
  70. #endif
  71. /* Use the -inf rounding mode conversion instructions to implement
  72. floor. We note when the exponent is large enough that the value
  73. must be integral, as this avoids unpleasant integer overflows. */
  74. __MATH_INLINE float
  75. __NTH (__floorf (float __x))
  76. {
  77. /* Check not zero since floor(-0) == -0. */
  78. if (__x != 0 && fabsf (__x) < 16777216.0f) /* 1 << FLT_MANT_DIG */
  79. {
  80. /* Note that Alpha S_Floating is stored in registers in a
  81. restricted T_Floating format, so we don't even need to
  82. convert back to S_Floating in the end. The initial
  83. conversion to T_Floating is needed to handle denormals. */
  84. float __tmp1, __tmp2;
  85. __asm ("cvtst/s %3,%2\n\t"
  86. #ifdef _IEEE_FP_INEXACT
  87. "cvttq/svim %2,%1\n\t"
  88. #else
  89. "cvttq/svm %2,%1\n\t"
  90. #endif
  91. "cvtqt/m %1,%0\n\t"
  92. : "=f"(__x), "=&f"(__tmp1), "=&f"(__tmp2)
  93. : "f"(__x));
  94. }
  95. return __x;
  96. }
  97. __MATH_INLINE double
  98. __NTH (__floor (double __x))
  99. {
  100. if (__x != 0 && fabs (__x) < 9007199254740992.0) /* 1 << DBL_MANT_DIG */
  101. {
  102. double __tmp1;
  103. __asm (
  104. #ifdef _IEEE_FP_INEXACT
  105. "cvttq/svim %2,%1\n\t"
  106. #else
  107. "cvttq/svm %2,%1\n\t"
  108. #endif
  109. "cvtqt/m %1,%0\n\t"
  110. : "=f"(__x), "=&f"(__tmp1)
  111. : "f"(__x));
  112. }
  113. return __x;
  114. }
  115. __MATH_INLINE float __NTH (floorf (float __x)) { return __floorf(__x); }
  116. __MATH_INLINE double __NTH (floor (double __x)) { return __floor(__x); }
  117. #ifdef __USE_ISOC99
  118. __MATH_INLINE float
  119. __NTH (__fdimf (float __x, float __y))
  120. {
  121. return __x <= __y ? 0.0f : __x - __y;
  122. }
  123. __MATH_INLINE float
  124. __NTH (fdimf (float __x, float __y))
  125. {
  126. return __x <= __y ? 0.0f : __x - __y;
  127. }
  128. __MATH_INLINE double
  129. __NTH (__fdim (double __x, double __y))
  130. {
  131. return __x <= __y ? 0.0 : __x - __y;
  132. }
  133. __MATH_INLINE double
  134. __NTH (fdim (double __x, double __y))
  135. {
  136. return __x <= __y ? 0.0 : __x - __y;
  137. }
  138. /* Test for negative number. Used in the signbit() macro. */
  139. __MATH_INLINE int
  140. __NTH (__signbitf (float __x))
  141. {
  142. __extension__ union { float __f; int __i; } __u = { __f: __x };
  143. return __u.__i < 0;
  144. }
  145. __MATH_INLINE int
  146. __NTH (__signbit (double __x))
  147. {
  148. __extension__ union { double __d; long __i; } __u = { __d: __x };
  149. return __u.__i < 0;
  150. }
  151. #endif /* C99 */
  152. #endif /* __NO_MATH_INLINES */