(__need_error_t): New macro, before including <errno.h>. [!__const] (__const):...
[glibc.git] / sysdeps / m68k / fpu / __math.h
blob0e3e2a3d8c6e0afc8584b1ff4b7512b1bb300a26
1 /* Copyright (C) 1991, 92, 93, 94, 96 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #ifdef __GNUC__
21 #include <sys/cdefs.h>
23 #ifdef __NO_M81_MATH_INLINES
24 /* This is used when defining the functions themselves. Define them with
25 __ names, and with `static inline' instead of `extern inline' so the
26 bodies will always be used, never an external function call. */
27 #define __m81_u(x) __CONCAT(__,x)
28 #define __m81_inline static __inline
29 #else
30 #define __m81_u(x) x
31 #define __m81_inline extern __inline
32 #define __M81_MATH_INLINES 1
33 #endif
35 /* Define a const math function. */
36 #define __m81_defun(rettype, func, args) \
37 __m81_inline rettype \
38 __m81_u(func) args __attribute__((__const__)); \
39 __m81_inline rettype \
40 __m81_u(func) args
42 /* Define the three variants of a math function that has a direct
43 implementation in the m68k fpu. FUNC is the name for C (which will be
44 suffixed with f and l for the float and long double version, resp). OP
45 is the name of the fpu operation (without leading f). */
46 #define __inline_mathop(func, op) \
47 __m81_defun (double, func, (double __mathop_x)) \
48 { \
49 double __result; \
50 __asm("f" __STRING(op) "%.x %1, %0" : "=f" (__result) : "f" (__mathop_x));\
51 return __result; \
52 } \
53 __m81_defun (float, func##f, (float __mathop_x)) \
54 { \
55 float __result; \
56 __asm("f" __STRING(op) "%.x %1, %0" : "=f" (__result) : "f" (__mathop_x));\
57 return __result; \
58 } \
59 __m81_defun (long double, func##l, (long double __mathop_x)) \
60 { \
61 long double __result; \
62 __asm("f" __STRING(op) "%.x %1, %0" : "=f" (__result) : "f" (__mathop_x));\
63 return __result; \
66 /* ieee style elementary functions */
67 __inline_mathop(__ieee754_acos, acos)
68 __inline_mathop(__ieee754_asin, asin)
69 __inline_mathop(__ieee754_cosh, cosh)
70 __inline_mathop(__ieee754_sinh, sinh)
71 __inline_mathop(__ieee754_exp, etox)
72 __inline_mathop(__ieee754_log10, log10)
73 __inline_mathop(__ieee754_log, logn)
74 __inline_mathop(__ieee754_sqrt, sqrt)
75 __inline_mathop(__ieee754_atanh, atanh)
77 __inline_mathop(__atan, atan)
78 __inline_mathop(__cos, cos)
79 __inline_mathop(__sin, sin)
80 __inline_mathop(__tan, tan)
81 __inline_mathop(__tanh, tanh)
82 __inline_mathop(__fabs, abs)
83 __inline_mathop(__sqrt, sqrt)
85 __inline_mathop(__rint, int)
86 __inline_mathop(__expm1, etoxm1)
87 __inline_mathop(__log1p, lognp1)
88 __inline_mathop(__logb, log2)
89 __inline_mathop(__significand, getman)
91 /* This macro contains the definition for the rest of the inline
92 functions, using __FLOAT_TYPE as the domain type and __S as the suffix
93 for the function names. */
95 #define __inline_functions(__float_type, __s) \
96 __m81_defun (__float_type, \
97 __ieee754_remainder##__s, (__float_type __x, __float_type __y)) \
98 { \
99 __float_type __result; \
100 __asm("frem%.x %1, %0" : "=f" (__result) : "f" (__y), "0" (__x)); \
101 return __result; \
104 __m81_defun (__float_type, \
105 __ieee754_fmod##__s, (__float_type __x, __float_type __y)) \
107 __float_type __result; \
108 __asm("fmod%.x %1, %0" : "=f" (__result) : "f" (__y), "0" (__x)); \
109 return __result; \
112 __m81_defun (__float_type, \
113 __ieee754_atan2##__s, (__float_type __y, __float_type __x)) \
115 __float_type __pi, __pi_2; \
117 __asm ("fmovecr%.x %#0, %0" : "=f" (__pi)); \
118 __asm ("fscale%.w %#-1, %0" : "=f" (__pi_2) : "0" (__pi)); \
119 if (__x > 0) \
121 if (__y > 0) \
123 if (__x > __y) \
124 return __m81_u(__atan##__s) (__y / __x); \
125 else \
126 return __pi_2 - __m81_u(__atan##__s) (__x / __y); \
128 else \
130 if (__x > -__y) \
131 return __m81_u(__atan##__s) (__y / __x); \
132 else \
133 return -__pi_2 - __m81_u(__atan##__s) (__x / __y); \
136 else \
138 if (__y > 0) \
140 if (-__x < __y) \
141 return __pi + __m81_u(__atan##__s) (__y / __x); \
142 else \
143 return __pi_2 - __m81_u(__atan##__s) (__x / __y); \
145 else \
147 if (-__x > -__y) \
148 return -__pi + __m81_u(__atan##__s) (__y / __x); \
149 else \
150 return -__pi_2 - __m81_u(__atan##__s) (__x / __y); \
155 __m81_inline __float_type \
156 __m81_u(__frexp##__s)(__float_type __value, int *__expptr) \
158 __float_type __mantissa, __exponent; \
159 int __iexponent; \
160 if (__value == 0.0) \
162 *__expptr = 0; \
163 return __value; \
165 __asm("fgetexp%.x %1, %0" : "=f" (__exponent) : "f" (__value)); \
166 __iexponent = (int) __exponent + 1; \
167 *__expptr = __iexponent; \
168 __asm("fscale%.l %2, %0" : "=f" (__mantissa) \
169 : "0" (__value), "dmi" (-__iexponent)); \
170 return __mantissa; \
173 __m81_defun (__float_type, __floor##__s, (__float_type __x)) \
175 __float_type __result; \
176 unsigned long int __ctrl_reg; \
177 __asm __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
178 /* Set rounding towards negative infinity. */ \
179 __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
180 : "dmi" ((__ctrl_reg & ~0x10) | 0x20)); \
181 /* Convert X to an integer, using -Inf rounding. */ \
182 __asm __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
183 /* Restore the previous rounding mode. */ \
184 __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
185 : "dmi" (__ctrl_reg)); \
186 return __result; \
189 __m81_defun (__float_type, \
190 __ieee754_pow##__s, (__float_type __x, __float_type __y)) \
192 __float_type __result; \
193 if (__x == 0.0) \
195 if (__y <= 0.0) \
196 __result = 0.0 / 0.0; \
197 else \
198 __result = 0.0; \
200 else if (__y == 0.0 || __x == 1.0) \
201 __result = 1.0; \
202 else if (__y == 1.0) \
203 __result = __x; \
204 else if (__y == 2.0) \
205 __result = __x * __x; \
206 else if (__x == 10.0) \
207 __asm("ftentox%.x %1, %0" : "=f" (__result) : "f" (__y)); \
208 else if (__x == 2.0) \
209 __asm("ftwotox%.x %1, %0" : "=f" (__result) : "f" (__y)); \
210 else if (__x < 0.0) \
212 __float_type __temp = __m81_u (__rint##__s) (__y); \
213 if (__y == __temp) \
215 int __i = (int) __y; \
216 __result = (__m81_u(__ieee754_exp##__s) \
217 (__y * __m81_u(__ieee754_log##__s) (-__x))); \
218 if (__i & 1) \
219 __result = -__result; \
221 else \
222 __result = 0.0 / 0.0; \
224 else \
225 __result = (__m81_u(__ieee754_exp##__s) \
226 (__y * __m81_u(__ieee754_log##__s) (__x))); \
227 return __result; \
230 __m81_defun (__float_type, __ceil##__s, (__float_type __x)) \
232 __float_type __result; \
233 unsigned long int __ctrl_reg; \
234 __asm __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
235 /* Set rounding towards positive infinity. */ \
236 __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
237 : "dmi" (__ctrl_reg | 0x30)); \
238 /* Convert X to an integer, using +Inf rounding. */ \
239 __asm __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
240 /* Restore the previous rounding mode. */ \
241 __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
242 : "dmi" (__ctrl_reg)); \
243 return __result; \
246 __m81_inline __float_type \
247 __m81_u(__modf##__s)(__float_type __value, __float_type *__iptr) \
249 __float_type __modf_int; \
250 __asm ("fintrz%.x %1, %0" : "=f" (__modf_int) : "f" (__value)); \
251 *__iptr = __modf_int; \
252 return __value - __modf_int; \
255 __m81_defun (int, __isinf##__s, (__float_type __value)) \
257 /* There is no branch-condition for infinity, \
258 so we must extract and examine the condition codes manually. */ \
259 unsigned long int __fpsr; \
260 __asm("ftst%.x %1\n" \
261 "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
262 return (__fpsr & (2 << 24)) ? (__fpsr & (8 << 24) ? -1 : 1) : 0; \
265 __m81_defun (int, __isnan##__s, (__float_type __value)) \
267 char __result; \
268 __asm("ftst%.x %1\n" \
269 "fsun %0" : "=dm" (__result) : "f" (__value)); \
270 return __result; \
273 __m81_defun (int, __finite##__s, (__float_type __value)) \
275 /* There is no branch-condition for infinity, so we must extract and \
276 examine the condition codes manually. */ \
277 unsigned long int __fpsr; \
278 __asm ("ftst%.x %1\n" \
279 "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
280 return (__fpsr & (3 << 24)) == 0; \
283 __m81_defun (int, __ilogb##__s, (__float_type __x)) \
285 __float_type __result; \
286 if (__x == 0.0) \
287 return 0x80000001; \
288 __asm("fgetexp%.x %1, %0" : "=f" (__result) : "f" (__x)); \
289 return (int) __result; \
292 __m81_defun (__float_type, \
293 __ieee754_scalb##__s, (__float_type __x, __float_type __n)) \
295 __float_type __result; \
296 __asm ("fscale%.x %1, %0" : "=f" (__result) : "f" (__n), "0" (__x)); \
297 return __result; \
300 __m81_defun (__float_type, __scalbn##__s, (__float_type __x, int __n)) \
302 __float_type __result; \
303 __asm ("fscale%.l %1, %0" : "=f" (__result) : "dmi" (__n), "0" (__x)); \
304 return __result; \
307 /* This defines the three variants of the inline functions. */
308 __inline_functions (double, )
309 __inline_functions (float, f)
310 __inline_functions (long double, l)
311 #undef __inline_functions
313 #endif /* GCC. */