OSDN Git Service

d1b05f3881b475e71de9255da228be35cc57af6e
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / powerpc / bits / mathinline.h
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
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <features.h>
22
23 #ifndef _MATH_H
24 # error "Never use <bits/mathinline.h> directly; include <math.h> instead."
25 #endif
26
27 #ifdef __cplusplus
28 # define __MATH_INLINE __inline
29 #else
30 # define __MATH_INLINE extern __inline
31 #endif  /* __cplusplus */
32
33 #if defined __GNUC__ && !defined _SOFT_FLOAT
34
35 #ifdef __USE_ISOC99
36 # if !__GNUC_PREREQ (2,97)
37 #  define __unordered_cmp(x, y) \
38   (__extension__                                                              \
39    ({ __typeof__(x) __x = (x); __typeof__(y) __y = (y);                       \
40       unsigned __r;                                                           \
41       __asm__("fcmpu 7,%1,%2 ; mfcr %0" : "=r" (__r) : "f" (__x), "f"(__y)    \
42               : "cr7");  \
43       __r; }))
44
45 #  undef isgreater
46 #  undef isgreaterequal
47 #  undef isless
48 #  undef islessequal
49 #  undef islessgreater
50 #  undef isunordered
51
52 #  define isgreater(x, y) (__unordered_cmp (x, y) >> 2 & 1)
53 #  define isgreaterequal(x, y) ((__unordered_cmp (x, y) & 6) != 0)
54 #  define isless(x, y) (__unordered_cmp (x, y) >> 3 & 1)
55 #  define islessequal(x, y) ((__unordered_cmp (x, y) & 0xA) != 0)
56 #  define islessgreater(x, y) ((__unordered_cmp (x, y) & 0xC) != 0)
57 #  define isunordered(x, y) (__unordered_cmp (x, y) & 1)
58
59 # endif /* __GNUC_PREREQ (2,97) */
60
61 /* The gcc, version 2.7 or below, has problems with all this inlining
62    code.  So disable it for this version of the compiler.  */
63 # if __GNUC_PREREQ (2, 8)
64 /* Test for negative number.  Used in the signbit() macro.  */
65 __MATH_INLINE int
66 __NTH (__signbitf (float __x))
67 {
68   __extension__ union { float __f; int __i; } __u = { __f: __x };
69   return __u.__i < 0;
70 }
71 __MATH_INLINE int
72 __NTH (__signbit (double __x))
73 {
74   __extension__ union { double __d; int __i[2]; } __u = { __d: __x };
75   return __u.__i[0] < 0;
76 }
77 # endif
78 #endif /* __USE_ISOC99 */
79
80 #if !defined __NO_MATH_INLINES && defined __OPTIMIZE__
81
82 #ifdef __USE_ISOC99
83
84 # ifndef __powerpc64__
85 __MATH_INLINE long int lrint (double __x) __THROW;
86 __MATH_INLINE long int
87 __NTH (lrint (double __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
97 __MATH_INLINE long int lrintf (float __x) __THROW;
98 __MATH_INLINE long int
99 __NTH (lrintf (float __x))
100 {
101   union {
102     double __d;
103     int __ll[2];
104   } __u;
105   __asm__ ("fctiw %0,%1" : "=f"(__u.__d) : "f"(__x));
106   return __u.__ll[1];
107 }
108 # endif
109
110 __MATH_INLINE double fdim (double __x, double __y) __THROW;
111 __MATH_INLINE double
112 __NTH (fdim (double __x, double __y))
113 {
114   return __x <= __y ? 0 : __x - __y;
115 }
116
117 __MATH_INLINE float fdimf (float __x, float __y) __THROW;
118 __MATH_INLINE float
119 __NTH (fdimf (float __x, float __y))
120 {
121   return __x <= __y ? 0 : __x - __y;
122 }
123
124 #endif /* __USE_ISOC99 */
125 #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
126
127 /* This code is used internally in the GNU libc.  */
128 #if 0 /*def __LIBC_INTERNAL_MATH_INLINES*/
129
130 #include <sysdep.h>
131 #include <ldsodefs.h>
132 #include <dl-procinfo.h>
133
134 # if __WORDSIZE == 64 || defined _ARCH_PWR4
135 #  define __CPU_HAS_FSQRT 1
136 # else
137 #  define __CPU_HAS_FSQRT ((GLRO(dl_hwcap) & PPC_FEATURE_64) != 0)
138 # endif
139
140 extern double __slow_ieee754_sqrt (double);
141 __MATH_INLINE double
142 __NTH (__ieee754_sqrt (double __x))
143 {
144   double __z;
145
146   /* If the CPU is 64-bit we can use the optional FP instructions.  */
147   if (__CPU_HAS_FSQRT)
148   {
149     /* Volatile is required to prevent the compiler from moving the
150        fsqrt instruction above the branch.  */
151      __asm__ __volatile__ (
152         "       fsqrt   %0,%1\n"
153                 : "=f" (__z)
154                 : "f" (__x));
155   }
156   else
157      __z = __slow_ieee754_sqrt(__x);
158
159   return __z;
160 }
161
162 extern float __slow_ieee754_sqrtf (float);
163 __MATH_INLINE float
164 __NTH (__ieee754_sqrtf (float __x))
165 {
166   float __z;
167
168   /* If the CPU is 64-bit we can use the optional FP instructions.  */
169   if (__CPU_HAS_FSQRT)
170   {
171     /* Volatile is required to prevent the compiler from moving the
172        fsqrts instruction above the branch.  */
173      __asm__ __volatile__ (
174         "       fsqrts  %0,%1\n"
175                 : "=f" (__z)
176                 : "f" (__x));
177   }
178   else
179      __z = __slow_ieee754_sqrtf(__x);
180
181   return __z;
182 }
183 #endif /* __LIBC_INTERNAL_MATH_INLINES */
184 #endif /* __GNUC__ && !_SOFT_FLOAT */
185