OSDN Git Service

b642c83f5e5476da8ede298feaa1021195dc1388
[uclinux-h8/uClibc.git] / libc / sysdeps / linux / sparc / bits / mathinline.h
1 /* Inline math functions for SPARC.
2    Copyright (C) 1999, 2000, 2001, 2002, 2004, 2006
3    Free Software Foundation, Inc.
4    This file is part of the GNU C Library.
5    Contributed by Jakub Jelinek <jakub@redhat.com>.
6
7    The GNU C Library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Lesser General Public
9    License as published by the Free Software Foundation; either
10    version 2.1 of the License, or (at your option) any later version.
11
12    The GNU C Library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Lesser General Public License for more details.
16
17    You should have received a copy of the GNU Lesser General Public
18    License along with the GNU C Library; if not, see
19    <http://www.gnu.org/licenses/>.  */
20
21 #ifndef _MATH_H
22 # error "Never use <bits/mathinline.h> directly; include <math.h> instead."
23 #endif
24
25 #include <bits/wordsize.h>
26
27 #ifdef __GNUC__
28
29 #if defined __USE_ISOC99 && !__GNUC_PREREQ (3, 0)
30 # undef isgreater
31 # undef isgreaterequal
32 # undef isless
33 # undef islessequal
34 # undef islessgreater
35 # undef isunordered
36
37 # if __WORDSIZE == 32
38
39 #  ifndef __NO_LONG_DOUBLE_MATH
40
41 #   define __unordered_cmp(x, y) \
42   (__extension__                                                              \
43    ({ unsigned __r;                                                           \
44       if (sizeof (x) == 4 && sizeof (y) == 4)                                 \
45         {                                                                     \
46           float __x = (x); float __y = (y);                                   \
47           __asm__ ("fcmps %1,%2; st %%fsr, %0" : "=m" (__r) : "f" (__x),      \
48                    "f" (__y) : "cc");                                         \
49         }                                                                     \
50       else if (sizeof (x) <= 8 && sizeof (y) <= 8)                            \
51         {                                                                     \
52           double __x = (x); double __y = (y);                                 \
53           __asm__ ("fcmpd\t%1,%2\n\tst\t%%fsr,%0" : "=m" (__r) : "f" (__x),   \
54                    "f" (__y) : "cc");                                         \
55         }                                                                     \
56       else                                                                    \
57         {                                                                     \
58           long double __x = (x); long double __y = (y);                       \
59           extern int _Q_cmp (const long double a, const long double b);       \
60           __r = _Q_cmp (__x, __y) << 10;                                      \
61         }                                                                     \
62       __r; }))
63
64 #  else
65
66 #   define __unordered_cmp(x, y) \
67   (__extension__                                                              \
68    ({ unsigned __r;                                                           \
69       if (sizeof (x) == 4 && sizeof (y) == 4)                                 \
70         {                                                                     \
71           float __x = (x); float __y = (y);                                   \
72           __asm__ ("fcmps %1,%2; st %%fsr, %0" : "=m" (__r) : "f" (__x),      \
73                    "f" (__y) : "cc");                                         \
74         }                                                                     \
75       else                                                                    \
76         {                                                                     \
77           double __x = (x); double __y = (y);                                 \
78           __asm__ ("fcmpd\t%1,%2\n\tst\t%%fsr,%0" : "=m" (__r) : "f" (__x),   \
79                    "f" (__y) : "cc");                                         \
80         }                                                                     \
81       __r; }))
82
83 #  endif
84
85 #  define isgreater(x, y) ((__unordered_cmp (x, y) & (3 << 10)) == (2 << 10))
86 #  define isgreaterequal(x, y) ((__unordered_cmp (x, y) & (1 << 10)) == 0)
87 #  define isless(x, y) ((__unordered_cmp (x, y) & (3 << 10)) == (1 << 10))
88 #  define islessequal(x, y) ((__unordered_cmp (x, y) & (2 << 10)) == 0)
89 #  define islessgreater(x, y) (((__unordered_cmp (x, y) + (1 << 10)) & (2 << 10)) != 0)
90 #  define isunordered(x, y) ((__unordered_cmp (x, y) & (3 << 10)) == (3 << 10))
91
92 # else /* sparc64 */
93
94 #  define __unordered_v9cmp(x, y, op, qop) \
95   (__extension__                                                              \
96    ({ unsigned __r;                                                           \
97       if (sizeof (x) == 4 && sizeof (y) == 4)                                 \
98         {                                                                     \
99           float __x = (x); float __y = (y);                                   \
100           __asm__ ("fcmps\t%%fcc3,%1,%2\n\tmov" op "\t%%fcc3,1,%0"            \
101                    : "=r" (__r) : "f" (__x), "f" (__y), "0" (0) : "cc");      \
102         }                                                                     \
103       else if (sizeof (x) <= 8 && sizeof (y) <= 8)                            \
104         {                                                                     \
105           double __x = (x); double __y = (y);                                 \
106           __asm__ ("fcmpd\t%%fcc3,%1,%2\n\tmov" op "\t%%fcc3,1,%0"            \
107                    : "=r" (__r) : "f" (__x), "f" (__y), "0" (0) : "cc");      \
108         }                                                                     \
109       else                                                                    \
110         {                                                                     \
111           long double __x = (x); long double __y = (y);                       \
112           extern int _Qp_cmp (const long double *a, const long double *b);    \
113           __r = qop;                                                          \
114         }                                                                     \
115       __r; }))
116
117 #  define isgreater(x, y) __unordered_v9cmp(x, y, "g", _Qp_cmp (&__x, &__y) == 2)
118 #  define isgreaterequal(x, y) __unordered_v9cmp(x, y, "ge", (_Qp_cmp (&__x, &__y) & 1) == 0)
119 #  define isless(x, y) __unordered_v9cmp(x, y, "l", _Qp_cmp (&__x, &__y) == 1)
120 #  define islessequal(x, y) __unordered_v9cmp(x, y, "le", (_Qp_cmp (&__x, &__y) & 2) == 0)
121 #  define islessgreater(x, y) __unordered_v9cmp(x, y, "lg", ((_Qp_cmp (&__x, &__y) + 1) & 2) != 0)
122 #  define isunordered(x, y) __unordered_v9cmp(x, y, "u", _Qp_cmp (&__x, &__y) == 3)
123
124 # endif /* sparc64 */
125
126 #endif /* __USE_ISOC99 */
127
128 #if (!defined __NO_MATH_INLINES || defined __LIBC_INTERNAL_MATH_INLINES) && defined __OPTIMIZE__
129
130 # ifdef __cplusplus
131 #  define __MATH_INLINE __inline
132 # else
133 #  define __MATH_INLINE extern __inline
134 # endif  /* __cplusplus */
135
136 /* The gcc, version 2.7 or below, has problems with all this inlining
137    code.  So disable it for this version of the compiler.  */
138 # if __GNUC_PREREQ (2, 8)
139
140 #  ifdef __USE_ISOC99
141
142 /* Test for negative number.  Used in the signbit() macro.  */
143 __MATH_INLINE int
144 __NTH (__signbitf (float __x))
145 {
146   __extension__ union { float __f; int __i; } __u = { __f: __x };
147   return __u.__i < 0;
148 }
149
150 #   if __WORDSIZE == 32
151
152 __MATH_INLINE int
153 __NTH (__signbit (double __x))
154 {
155   __extension__ union { double __d; int __i[2]; } __u = { __d: __x };
156   return __u.__i[0] < 0;
157 }
158
159 #    ifndef __NO_LONG_DOUBLE_MATH
160 __MATH_INLINE int
161 __NTH (__signbitl (long double __x))
162 {
163   __extension__ union { long double __l; int __i[4]; } __u = { __l: __x };
164   return __u.__i[0] < 0;
165 }
166 #    else
167 __MATH_INLINE int
168 __NTH (__signbitl (long double __x))
169 {
170   return __signbit ((double)__x);
171 }
172 #    endif
173
174 #   else /* sparc64 */
175
176 __MATH_INLINE int
177 __NTH (__signbit (double __x))
178 {
179   __extension__ union { double __d; long int __i; } __u = { __d: __x };
180   return __u.__i < 0;
181 }
182
183 __MATH_INLINE int
184 __NTH (__signbitl (long double __x))
185 {
186   __extension__ union { long double __l; long int __i[2]; } __u = { __l: __x };
187   return __u.__i[0] < 0;
188 }
189
190 #   endif /* sparc64 */
191
192 #  endif /* __USE_ISOC99 */
193
194 #  if !defined __NO_MATH_INLINES && !__GNUC_PREREQ (3, 2)
195
196 __MATH_INLINE double
197 __NTH (sqrt (double __x))
198 {
199   register double __r;
200   __asm__ ("fsqrtd %1,%0" : "=f" (__r) : "f" (__x));
201   return __r;
202 }
203
204 __MATH_INLINE float
205 __NTH (sqrtf (float __x))
206 {
207   register float __r;
208   __asm__ ("fsqrts %1,%0" : "=f" (__r) : "f" (__x));
209   return __r;
210 }
211
212 #   if __WORDSIZE == 64
213 __MATH_INLINE long double
214 __NTH (sqrtl (long double __x))
215 {
216   long double __r;
217   extern void _Qp_sqrt (long double *, __const__ long double *);
218   _Qp_sqrt (&__r, &__x);
219   return __r;
220 }
221 #   elif !defined __NO_LONG_DOUBLE_MATH
222 __MATH_INLINE long double
223 sqrtl (long double __x) __THROW
224 {
225   extern long double _Q_sqrt (__const__ long double);
226   return _Q_sqrt (__x);
227 }
228 #   endif /* sparc64 */
229
230 #  endif /* !__NO_MATH_INLINES && !GCC 3.2+ */
231
232 /* This code is used internally in the GNU libc.  */
233 #  ifdef __LIBC_INTERNAL_MATH_INLINES
234 __MATH_INLINE double
235 __ieee754_sqrt (double __x)
236 {
237   register double __r;
238   __asm__ ("fsqrtd %1,%0" : "=f" (__r) : "f" (__x));
239   return __r;
240 }
241
242 __MATH_INLINE float
243 __ieee754_sqrtf (float __x)
244 {
245   register float __r;
246   __asm__ ("fsqrts %1,%0" : "=f" (__r) : "f" (__x));
247   return __r;
248 }
249
250 #   if __WORDSIZE == 64
251 __MATH_INLINE long double
252 __ieee754_sqrtl (long double __x)
253 {
254   long double __r;
255   extern void _Qp_sqrt (long double *, __const__ long double *);
256   _Qp_sqrt(&__r, &__x);
257   return __r;
258 }
259 #   elif !defined __NO_LONG_DOUBLE_MATH
260 __MATH_INLINE long double
261 __ieee754_sqrtl (long double __x)
262 {
263   extern long double _Q_sqrt (__const__ long double);
264   return _Q_sqrt (__x);
265 }
266 #   endif /* sparc64 */
267 #  endif /* __LIBC_INTERNAL_MATH_INLINES */
268 # endif /* gcc 2.8+ */
269
270 # ifdef __USE_ISOC99
271
272 #  ifndef __NO_MATH_INLINES
273
274 __MATH_INLINE double __NTH (fdim (double __x, double __y));
275 __MATH_INLINE double
276 __NTH (fdim (double __x, double __y))
277 {
278   return __x <= __y ? 0 : __x - __y;
279 }
280
281 __MATH_INLINE float __NTH (fdimf (float __x, float __y));
282 __MATH_INLINE float
283 __NTH (fdimf (float __x, float __y))
284 {
285   return __x <= __y ? 0 : __x - __y;
286 }
287
288 #  endif /* !__NO_MATH_INLINES */
289 # endif /* __USE_ISOC99 */
290 #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
291 #endif /* __GNUC__ */