mathinline.h 4.6 KB

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