mathinline.h 4.8 KB

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