mathinline.h 5.0 KB

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