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 #ifdef __GNUC__
26 
27 #if defined __USE_ISOC99 && !__GNUC_PREREQ (3, 0)
28 # undef isgreater
29 # undef isgreaterequal
30 # undef isless
31 # undef islessequal
32 # undef islessgreater
33 # undef isunordered
34 
35 #  ifndef __NO_LONG_DOUBLE_MATH
36 
37 #   define __unordered_cmp(x, y) \
38   (__extension__							      \
39    ({ unsigned __r;							      \
40       if (sizeof (x) == 4 && sizeof (y) == 4)				      \
41 	{								      \
42 	  float __x = (x); float __y = (y);				      \
43 	  __asm__ ("fcmps %1,%2; st %%fsr, %0" : "=m" (__r) : "f" (__x),      \
44 		   "f" (__y) : "cc");					      \
45 	}								      \
46       else if (sizeof (x) <= 8 && sizeof (y) <= 8)			      \
47 	{								      \
48 	  double __x = (x); double __y = (y);				      \
49 	  __asm__ ("fcmpd\t%1,%2\n\tst\t%%fsr,%0" : "=m" (__r) : "f" (__x),   \
50 		   "f" (__y) : "cc");					      \
51 	}								      \
52       else								      \
53 	{								      \
54 	  long double __x = (x); long double __y = (y);			      \
55 	  extern int _Q_cmp (const long double a, const long double b);	      \
56 	  __r = _Q_cmp (__x, __y) << 10;				      \
57 	}								      \
58       __r; }))
59 
60 #  else
61 
62 #   define __unordered_cmp(x, y) \
63   (__extension__							      \
64    ({ unsigned __r;							      \
65       if (sizeof (x) == 4 && sizeof (y) == 4)				      \
66 	{								      \
67 	  float __x = (x); float __y = (y);				      \
68 	  __asm__ ("fcmps %1,%2; st %%fsr, %0" : "=m" (__r) : "f" (__x),      \
69 		   "f" (__y) : "cc");					      \
70 	}								      \
71       else								      \
72 	{								      \
73 	  double __x = (x); double __y = (y);				      \
74 	  __asm__ ("fcmpd\t%1,%2\n\tst\t%%fsr,%0" : "=m" (__r) : "f" (__x),   \
75 		   "f" (__y) : "cc");					      \
76 	}								      \
77       __r; }))
78 
79 #  endif
80 
81 #  define isgreater(x, y) ((__unordered_cmp (x, y) & (3 << 10)) == (2 << 10))
82 #  define isgreaterequal(x, y) ((__unordered_cmp (x, y) & (1 << 10)) == 0)
83 #  define isless(x, y) ((__unordered_cmp (x, y) & (3 << 10)) == (1 << 10))
84 #  define islessequal(x, y) ((__unordered_cmp (x, y) & (2 << 10)) == 0)
85 #  define islessgreater(x, y) (((__unordered_cmp (x, y) + (1 << 10)) & (2 << 10)) != 0)
86 #  define isunordered(x, y) ((__unordered_cmp (x, y) & (3 << 10)) == (3 << 10))
87 
88 #endif /* __USE_ISOC99 */
89 
90 #if (!defined __NO_MATH_INLINES || defined __LIBC_INTERNAL_MATH_INLINES) && defined __OPTIMIZE__
91 
92 # ifdef __cplusplus
93 #  define __MATH_INLINE __inline
94 # else
95 #  define __MATH_INLINE __extern_inline
96 # endif  /* __cplusplus */
97 
98 /* The gcc, version 2.7 or below, has problems with all this inlining
99    code.  So disable it for this version of the compiler.  */
100 # if __GNUC_PREREQ (2, 8)
101 
102 #  ifdef __USE_ISOC99
103 
104 /* Test for negative number.  Used in the signbit() macro.  */
105 __MATH_INLINE int
__NTH(__signbitf (float __x))106 __NTH (__signbitf (float __x))
107 {
108   __extension__ union { float __f; int __i; } __u = { __f: __x };
109   return __u.__i < 0;
110 }
111 
112 __MATH_INLINE int
__NTH(__signbit (double __x))113 __NTH (__signbit (double __x))
114 {
115   __extension__ union { double __d; int __i[2]; } __u = { __d: __x };
116   return __u.__i[0] < 0;
117 }
118 
119 #    ifndef __NO_LONG_DOUBLE_MATH
120 __MATH_INLINE int
__NTH(__signbitl (long double __x))121 __NTH (__signbitl (long double __x))
122 {
123   __extension__ union { long double __l; int __i[4]; } __u = { __l: __x };
124   return __u.__i[0] < 0;
125 }
126 #    else
127 __MATH_INLINE int
__NTH(__signbitl (long double __x))128 __NTH (__signbitl (long double __x))
129 {
130   return __signbit ((double)__x);
131 }
132 #    endif
133 
134 #  endif /* __USE_ISOC99 */
135 
136 #  if !defined __NO_MATH_INLINES && !__GNUC_PREREQ (3, 2)
137 
138 __MATH_INLINE double
__NTH(sqrt (double __x))139 __NTH (sqrt (double __x))
140 {
141   register double __r;
142   __asm__ ("fsqrtd %1,%0" : "=f" (__r) : "f" (__x));
143   return __r;
144 }
145 
146 __MATH_INLINE float
__NTH(sqrtf (float __x))147 __NTH (sqrtf (float __x))
148 {
149   register float __r;
150   __asm__ ("fsqrts %1,%0" : "=f" (__r) : "f" (__x));
151   return __r;
152 }
153 
154 #  endif /* !__NO_MATH_INLINES && !GCC 3.2+ */
155 
156 /* This code is used internally in the GNU libc.  */
157 #  ifdef __LIBC_INTERNAL_MATH_INLINES
158 __MATH_INLINE double
__ieee754_sqrt(double __x)159 __ieee754_sqrt (double __x)
160 {
161   register double __r;
162   __asm__ ("fsqrtd %1,%0" : "=f" (__r) : "f" (__x));
163   return __r;
164 }
165 
166 __MATH_INLINE float
__ieee754_sqrtf(float __x)167 __ieee754_sqrtf (float __x)
168 {
169   register float __r;
170   __asm__ ("fsqrts %1,%0" : "=f" (__r) : "f" (__x));
171   return __r;
172 }
173 
174 #  endif /* __LIBC_INTERNAL_MATH_INLINES */
175 # endif /* gcc 2.8+ */
176 
177 # ifdef __USE_ISOC99
178 
179 #  ifndef __NO_MATH_INLINES
180 
181 __MATH_INLINE double __NTH (fdim (double __x, double __y));
182 __MATH_INLINE double
__NTH(fdim (double __x,double __y))183 __NTH (fdim (double __x, double __y))
184 {
185   return __x <= __y ? 0 : __x - __y;
186 }
187 
188 __MATH_INLINE float __NTH (fdimf (float __x, float __y));
189 __MATH_INLINE float
__NTH(fdimf (float __x,float __y))190 __NTH (fdimf (float __x, float __y))
191 {
192   return __x <= __y ? 0 : __x - __y;
193 }
194 
195 #  endif /* !__NO_MATH_INLINES */
196 # endif /* __USE_ISOC99 */
197 #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
198 #endif /* __GNUC__ */
199