1 /* Definitions of inline math functions implemented by the m68881/2.
2 Copyright (C) 1991,92,93,94,96,97,98,99,2000,2002, 2003, 2004
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
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.
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.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
23 /* GCC 3.1 and up have builtins that actually can be used. */
24 # if !__GNUC_PREREQ (3,1)
25 /* ISO C99 defines some macros to perform unordered comparisons. The
26 m68k FPU supports this with special opcodes and we should use them.
27 These must not be inline functions since we have to be able to handle
28 all floating-point types. */
30 # undef isgreaterequal
35 # define isgreater(x, y) \
38 __asm__ ("fcmp%.x %2,%1; fsogt %0" \
39 : "=dm" (__result) : "f" (x), "f" (y)); \
42 # define isgreaterequal(x, y) \
45 __asm__ ("fcmp%.x %2,%1; fsoge %0" \
46 : "=dm" (__result) : "f" (x), "f" (y)); \
49 # define isless(x, y) \
52 __asm__ ("fcmp%.x %2,%1; fsolt %0" \
53 : "=dm" (__result) : "f" (x), "f" (y)); \
56 # define islessequal(x, y) \
59 __asm__ ("fcmp%.x %2,%1; fsole %0" \
60 : "=dm" (__result) : "f" (x), "f" (y)); \
63 # define islessgreater(x, y) \
66 __asm__ ("fcmp%.x %2,%1; fsogl %0" \
67 : "=dm" (__result) : "f" (x), "f" (y)); \
70 # define isunordered(x, y) \
73 __asm__ ("fcmp%.x %2,%1; fsun %0" \
74 : "=dm" (__result) : "f" (x), "f" (y)); \
80 #if (!defined __NO_MATH_INLINES && defined __OPTIMIZE__) \
81 || defined __LIBC_INTERNAL_MATH_INLINES
83 #ifdef __LIBC_INTERNAL_MATH_INLINES
84 /* This is used when defining the functions themselves. Define them with
85 __ names, and with `static inline' instead of `extern inline' so the
86 bodies will always be used, never an external function call. */
87 # define __m81_u(x) __CONCAT(__,x)
88 # define __m81_inline static __inline
92 # define __m81_inline __inline
94 # define __m81_inline extern __inline
96 # define __M81_MATH_INLINES 1
99 /* Define a const math function. */
100 #define __m81_defun(rettype, func, args) \
101 __m81_inline rettype __attribute__((__const__)) \
104 /* Define the three variants of a math function that has a direct
105 implementation in the m68k fpu. FUNC is the name for C (which will be
106 suffixed with f and l for the float and long double version, resp). OP
107 is the name of the fpu operation (without leading f). */
109 #if defined __USE_MISC || defined __USE_ISOC99
110 # define __inline_mathop(func, op) \
111 __inline_mathop1(double, func, op) \
112 __inline_mathop1(float, __CONCAT(func,f), op) \
113 __inline_mathop1(long double, __CONCAT(func,l), op)
115 # define __inline_mathop(func, op) \
116 __inline_mathop1(double, func, op)
119 #define __inline_mathop1(float_type,func, op) \
120 __m81_defun (float_type, func, (float_type __mathop_x)) \
122 float_type __result; \
123 __asm__("f" __STRING(op) "%.x %1, %0" : "=f" (__result) : "f" (__mathop_x));\
127 __inline_mathop(__atan, atan)
128 __inline_mathop(__cos, cos)
129 __inline_mathop(__sin, sin)
130 __inline_mathop(__tan, tan)
131 __inline_mathop(__tanh, tanh)
132 __inline_mathop(__fabs, abs)
134 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED || defined __USE_ISOC99
135 __inline_mathop(__rint, int)
136 __inline_mathop(__expm1, etoxm1)
137 __inline_mathop(__log1p, lognp1)
141 __inline_mathop(__significand, getman)
145 __inline_mathop(__trunc, intrz)
148 #if !defined __NO_MATH_INLINES && defined __OPTIMIZE__
150 __inline_mathop(atan, atan)
151 __inline_mathop(cos, cos)
152 __inline_mathop(sin, sin)
153 __inline_mathop(tan, tan)
154 __inline_mathop(tanh, tanh)
156 # if defined __USE_MISC || defined __USE_XOPEN_EXTENDED || defined __USE_ISOC99
157 __inline_mathop(rint, int)
158 __inline_mathop(expm1, etoxm1)
159 __inline_mathop(log1p, lognp1)
163 __inline_mathop(significand, getman)
167 __inline_mathop(trunc, intrz)
170 #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
172 /* This macro contains the definition for the rest of the inline
173 functions, using FLOAT_TYPE as the domain type and S as the suffix
174 for the function names. */
176 #define __inline_functions(float_type, s) \
177 __m81_defun (float_type, __CONCAT(__floor,s), (float_type __x)) \
179 float_type __result; \
180 unsigned long int __ctrl_reg; \
181 __asm__ __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
182 /* Set rounding towards negative infinity. */ \
183 __asm__ __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
184 : "dmi" ((__ctrl_reg & ~0x10) | 0x20)); \
185 /* Convert X to an integer, using -Inf rounding. */ \
186 __asm__ __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
187 /* Restore the previous rounding mode. */ \
188 __asm__ __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
189 : "dmi" (__ctrl_reg)); \
193 __m81_defun (float_type, __CONCAT(__ceil,s), (float_type __x)) \
195 float_type __result; \
196 unsigned long int __ctrl_reg; \
197 __asm__ __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
198 /* Set rounding towards positive infinity. */ \
199 __asm__ __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
200 : "dmi" (__ctrl_reg | 0x30)); \
201 /* Convert X to an integer, using +Inf rounding. */ \
202 __asm__ __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
203 /* Restore the previous rounding mode. */ \
204 __asm__ __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
205 : "dmi" (__ctrl_reg)); \
209 __inline_functions(double,)
210 #if defined __USE_MISC || defined __USE_ISOC99
211 __inline_functions(float,f)
212 __inline_functions(long double,l)
214 #undef __inline_functions
218 # define __inline_functions(float_type, s) \
219 __m81_defun (int, __CONCAT(__isinf,s), (float_type __value)) \
221 /* There is no branch-condition for infinity, \
222 so we must extract and examine the condition codes manually. */ \
223 unsigned long int __fpsr; \
224 __asm__("ftst%.x %1\n" \
225 "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
226 return (__fpsr & (2 << 24)) ? (__fpsr & (8 << 24) ? -1 : 1) : 0; \
229 __m81_defun (int, __CONCAT(__finite,s), (float_type __value)) \
231 /* There is no branch-condition for infinity, so we must extract and \
232 examine the condition codes manually. */ \
233 unsigned long int __fpsr; \
234 __asm__ ("ftst%.x %1\n" \
235 "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
236 return (__fpsr & (3 << 24)) == 0; \
239 __m81_defun (float_type, __CONCAT(__scalbn,s), \
240 (float_type __x, int __n)) \
242 float_type __result; \
243 __asm__ ("fscale%.l %1, %0" : "=f" (__result) : "dmi" (__n), "0" (__x)); \
247 __inline_functions(double,)
248 __inline_functions(float,f)
249 __inline_functions(long double,l)
250 # undef __inline_functions
252 #endif /* Use misc. */
254 #if defined __USE_MISC || defined __USE_XOPEN
256 # define __inline_functions(float_type, s) \
257 __m81_defun (int, __CONCAT(__isnan,s), (float_type __value)) \
260 __asm__("ftst%.x %1\n" \
261 "fsun %0" : "=dm" (__result) : "f" (__value)); \
265 __inline_functions(double,)
267 __inline_functions(float,f)
268 __inline_functions(long double,l)
270 # undef __inline_functions
276 # define __inline_functions(float_type, s) \
277 __m81_defun (int, __CONCAT(__signbit,s), (float_type __value)) \
279 /* There is no branch-condition for the sign bit, so we must extract \
280 and examine the condition codes manually. */ \
281 unsigned long int __fpsr; \
282 __asm__ ("ftst%.x %1\n" \
283 "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
284 return (__fpsr >> 27) & 1; \
287 __m81_defun (float_type, __CONCAT(__scalbln,s), \
288 (float_type __x, long int __n)) \
290 return __CONCAT(__scalbn,s) (__x, __n); \
293 __m81_defun (float_type, __CONCAT(__nearbyint,s), (float_type __x)) \
295 float_type __result; \
296 unsigned long int __ctrl_reg; \
297 __asm__ __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
298 /* Temporarily disable the inexact exception. */ \
299 __asm__ __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
300 : "dmi" (__ctrl_reg & ~0x200)); \
301 __asm__ __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
302 __asm__ __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
303 : "dmi" (__ctrl_reg)); \
307 __m81_defun (long int, __CONCAT(__lrint,s), (float_type __x)) \
310 __asm__ ("fmove%.l %1, %0" : "=dm" (__result) : "f" (__x)); \
314 __m81_inline float_type \
315 __m81_u(__CONCAT(__fma,s))(float_type __x, float_type __y, \
318 return (__x * __y) + __z; \
321 __inline_functions (double,)
322 __inline_functions (float,f)
323 __inline_functions (long double,l)
324 # undef __inline_functions
326 #endif /* Use ISO C9x */
330 # define __inline_functions(float_type, s) \
332 __m81_u(__CONCAT(__sincos,s))(float_type __x, float_type *__sinx, \
333 float_type *__cosx) \
335 __asm__ ("fsincos%.x %2,%1:%0" \
336 : "=f" (*__sinx), "=f" (*__cosx) : "f" (__x)); \
339 __inline_functions (double,)
340 __inline_functions (float,f)
341 __inline_functions (long double,l)
342 # undef __inline_functions
346 #if !defined __NO_MATH_INLINES && defined __OPTIMIZE__
348 /* Define inline versions of the user visible functions. */
350 /* Note that there must be no whitespace before the argument passed for
351 NAME, to make token pasting work correctly with -traditional. */
352 # define __inline_forward_c(rettype, name, args1, args2) \
353 extern __inline rettype __attribute__((__const__)) \
356 return __CONCAT(__,name) args2; \
359 # define __inline_forward(rettype, name, args1, args2) \
360 extern __inline rettype name args1 \
362 return __CONCAT(__,name) args2; \
365 __inline_forward_c(double,floor, (double __x), (__x))
366 __inline_forward_c(double,ceil, (double __x), (__x))
368 # ifndef __USE_ISOC99 /* Conflict with macro of same name. */
369 __inline_forward_c(int,isinf, (double __value), (__value))
371 __inline_forward_c(int,finite, (double __value), (__value))
372 __inline_forward_c(double,scalbn, (double __x, int __n), (__x, __n))
374 # if defined __USE_MISC || defined __USE_XOPEN
375 # ifndef __USE_ISOC99 /* Conflict with macro of same name. */
376 __inline_forward_c(int,isnan, (double __value), (__value))
380 __inline_forward_c(double,scalbln, (double __x, long int __n), (__x, __n))
381 __inline_forward_c(double,nearbyint, (double __value), (__value))
382 __inline_forward_c(long int,lrint, (double __value), (__value))
383 __inline_forward_c(double,fma, (double __x, double __y, double __z),
387 __inline_forward(void,sincos, (double __x, double *__sinx, double *__cosx),
388 (__x, __sinx, __cosx))
391 # if defined __USE_MISC || defined __USE_ISOC99
393 __inline_forward_c(float,floorf, (float __x), (__x))
394 __inline_forward_c(float,ceilf, (float __x), (__x))
396 __inline_forward_c(int,isinff, (float __value), (__value))
397 __inline_forward_c(int,finitef, (float __value), (__value))
398 __inline_forward_c(float,scalbnf, (float __x, int __n), (__x, __n))
399 __inline_forward_c(int,isnanf, (float __value), (__value))
402 __inline_forward_c(float,scalblnf, (float __x, long int __n), (__x, __n))
403 __inline_forward_c(float,nearbyintf, (float __value), (__value))
404 __inline_forward_c(long int,lrintf, (float __value), (__value))
405 __inline_forward_c(float,fmaf, (float __x, float __y, float __z),
409 __inline_forward(void,sincosf, (float __x, float *__sinx, float *__cosx),
410 (__x, __sinx, __cosx))
413 __inline_forward_c(long double,floorl, (long double __x), (__x))
414 __inline_forward_c(long double,ceill, (long double __x), (__x))
416 __inline_forward_c(int,isinfl, (long double __value), (__value))
417 __inline_forward_c(int,finitel, (long double __value), (__value))
418 __inline_forward_c(long double,scalbnl, (long double __x, int __n), (__x, __n))
419 __inline_forward_c(int,isnanl, (long double __value), (__value))
422 __inline_forward_c(long double,scalblnl, (long double __x, long int __n),
424 __inline_forward_c(long double,nearbyintl, (long double __value), (__value))
425 __inline_forward_c(long int,lrintl, (long double __value), (__value))
426 __inline_forward_c(long double,fmal,
427 (long double __x, long double __y, long double __z),
431 __inline_forward(void,sincosl,
432 (long double __x, long double *__sinx, long double *__cosx),
433 (__x, __sinx, __cosx))
436 #endif /* Use misc or ISO C99 */
438 #undef __inline_forward
439 #undef __inline_forward_c
441 #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */