mathinline.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Inline math functions for powerpc.
  2. Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2004, 2006
  3. Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  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. #include <features.h>
  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 /* __cplusplus */
  25. #if defined __GNUC__ && !defined _SOFT_FLOAT
  26. #ifdef __USE_ISOC99
  27. # if !__GNUC_PREREQ (2,97)
  28. # define __unordered_cmp(x, y) \
  29. (__extension__ \
  30. ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y); \
  31. unsigned __r; \
  32. __asm__("fcmpu 7,%1,%2 ; mfcr %0" : "=r" (__r) : "f" (__x), "f"(__y) \
  33. : "cr7"); \
  34. __r; }))
  35. # undef isgreater
  36. # undef isgreaterequal
  37. # undef isless
  38. # undef islessequal
  39. # undef islessgreater
  40. # undef isunordered
  41. # define isgreater(x, y) (__unordered_cmp (x, y) >> 2 & 1)
  42. # define isgreaterequal(x, y) ((__unordered_cmp (x, y) & 6) != 0)
  43. # define isless(x, y) (__unordered_cmp (x, y) >> 3 & 1)
  44. # define islessequal(x, y) ((__unordered_cmp (x, y) & 0xA) != 0)
  45. # define islessgreater(x, y) ((__unordered_cmp (x, y) & 0xC) != 0)
  46. # define isunordered(x, y) (__unordered_cmp (x, y) & 1)
  47. # endif /* __GNUC_PREREQ (2,97) */
  48. /* The gcc, version 2.7 or below, has problems with all this inlining
  49. code. So disable it for this version of the compiler. */
  50. # if __GNUC_PREREQ (2, 8)
  51. /* Test for negative number. Used in the signbit() macro. */
  52. __MATH_INLINE int
  53. __NTH (__signbitf (float __x))
  54. {
  55. __extension__ union { float __f; int __i; } __u = { __f: __x };
  56. return __u.__i < 0;
  57. }
  58. __MATH_INLINE int
  59. __NTH (__signbit (double __x))
  60. {
  61. __extension__ union { double __d; int __i[2]; } __u = { __d: __x };
  62. return __u.__i[0] < 0;
  63. }
  64. # endif
  65. #endif /* __USE_ISOC99 */
  66. #if !defined __NO_MATH_INLINES && defined __OPTIMIZE__
  67. #ifdef __USE_ISOC99
  68. # ifndef __powerpc64__
  69. __MATH_INLINE long int lrint (double __x) __THROW;
  70. __MATH_INLINE long int
  71. __NTH (lrint (double __x))
  72. {
  73. union {
  74. double __d;
  75. int __ll[2];
  76. } __u;
  77. __asm__ ("fctiw %0,%1" : "=f"(__u.__d) : "f"(__x));
  78. return __u.__ll[1];
  79. }
  80. __MATH_INLINE long int lrintf (float __x) __THROW;
  81. __MATH_INLINE long int
  82. __NTH (lrintf (float __x))
  83. {
  84. union {
  85. double __d;
  86. int __ll[2];
  87. } __u;
  88. __asm__ ("fctiw %0,%1" : "=f"(__u.__d) : "f"(__x));
  89. return __u.__ll[1];
  90. }
  91. # endif
  92. __MATH_INLINE double fdim (double __x, double __y) __THROW;
  93. __MATH_INLINE double
  94. __NTH (fdim (double __x, double __y))
  95. {
  96. return __x <= __y ? 0 : __x - __y;
  97. }
  98. __MATH_INLINE float fdimf (float __x, float __y) __THROW;
  99. __MATH_INLINE float
  100. __NTH (fdimf (float __x, float __y))
  101. {
  102. return __x <= __y ? 0 : __x - __y;
  103. }
  104. #endif /* __USE_ISOC99 */
  105. #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
  106. /* This code is used internally in the GNU libc. */
  107. #if 0 /*def __LIBC_INTERNAL_MATH_INLINES*/
  108. #include <sysdep.h>
  109. #include <ldsodefs.h>
  110. #include <dl-procinfo.h>
  111. # if __WORDSIZE == 64 || defined _ARCH_PWR4
  112. # define __CPU_HAS_FSQRT 1
  113. # else
  114. # define __CPU_HAS_FSQRT ((GLRO(dl_hwcap) & PPC_FEATURE_64) != 0)
  115. # endif
  116. extern double __slow_ieee754_sqrt (double);
  117. __MATH_INLINE double
  118. __NTH (__ieee754_sqrt (double __x))
  119. {
  120. double __z;
  121. /* If the CPU is 64-bit we can use the optional FP instructions. */
  122. if (__CPU_HAS_FSQRT)
  123. {
  124. /* Volatile is required to prevent the compiler from moving the
  125. fsqrt instruction above the branch. */
  126. __asm__ __volatile__ (
  127. " fsqrt %0,%1\n"
  128. : "=f" (__z)
  129. : "f" (__x));
  130. }
  131. else
  132. __z = __slow_ieee754_sqrt(__x);
  133. return __z;
  134. }
  135. extern float __slow_ieee754_sqrtf (float);
  136. __MATH_INLINE float
  137. __NTH (__ieee754_sqrtf (float __x))
  138. {
  139. float __z;
  140. /* If the CPU is 64-bit we can use the optional FP instructions. */
  141. if (__CPU_HAS_FSQRT)
  142. {
  143. /* Volatile is required to prevent the compiler from moving the
  144. fsqrts instruction above the branch. */
  145. __asm__ __volatile__ (
  146. " fsqrts %0,%1\n"
  147. : "=f" (__z)
  148. : "f" (__x));
  149. }
  150. else
  151. __z = __slow_ieee754_sqrtf(__x);
  152. return __z;
  153. }
  154. #endif /* __LIBC_INTERNAL_MATH_INLINES */
  155. #endif /* __GNUC__ && !_SOFT_FLOAT */