1 /* Definitions of inline math functions implemented by the m68881/2.
2 Copyright (C) 1991,92,93,94,96,97,98,99,2000 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
24 /* ISO C99 defines some macros to perform unordered comparisons. The
25 m68k FPU supports this with special opcodes and we should use them.
26 These must not be inline functions since we have to be able to handle
27 all floating-point types. */
28 # define isgreater(x, y) \
31 __asm__ ("fcmp%.x %2,%1; fsogt %0" \
32 : "=dm" (__result) : "f" (x), "f" (y)); \
35 # define isgreaterequal(x, y) \
38 __asm__ ("fcmp%.x %2,%1; fsoge %0" \
39 : "=dm" (__result) : "f" (x), "f" (y)); \
42 # define isless(x, y) \
45 __asm__ ("fcmp%.x %2,%1; fsolt %0" \
46 : "=dm" (__result) : "f" (x), "f" (y)); \
49 # define islessequal(x, y) \
52 __asm__ ("fcmp%.x %2,%1; fsole %0" \
53 : "=dm" (__result) : "f" (x), "f" (y)); \
56 # define islessgreater(x, y) \
59 __asm__ ("fcmp%.x %2,%1; fsogl %0" \
60 : "=dm" (__result) : "f" (x), "f" (y)); \
63 # define isunordered(x, y) \
66 __asm__ ("fcmp%.x %2,%1; fsun %0" \
67 : "=dm" (__result) : "f" (x), "f" (y)); \
72 #if (!defined __NO_MATH_INLINES && defined __OPTIMIZE__) \
73 || defined __LIBC_INTERNAL_MATH_INLINES
75 #ifdef __LIBC_INTERNAL_MATH_INLINES
76 /* This is used when defining the functions themselves. Define them with
77 __ names, and with `static inline' instead of `extern inline' so the
78 bodies will always be used, never an external function call. */
79 # define __m81_u(x) __CONCAT(__,x)
80 # define __m81_inline static __inline
84 # define __m81_inline __inline
86 # define __m81_inline extern __inline
88 # define __M81_MATH_INLINES 1
91 /* Define a const math function. */
92 #define __m81_defun(rettype, func, args) \
93 __m81_inline rettype __attribute__((__const__)) \
96 /* Define the three variants of a math function that has a direct
97 implementation in the m68k fpu. FUNC is the name for C (which will be
98 suffixed with f and l for the float and long double version, resp). OP
99 is the name of the fpu operation (without leading f). */
101 #if defined __USE_MISC || defined __USE_ISOC99
102 # define __inline_mathop(func, op) \
103 __inline_mathop1(double, func, op) \
104 __inline_mathop1(float, __CONCAT(func,f), op) \
105 __inline_mathop1(long double, __CONCAT(func,l), op)
107 # define __inline_mathop(func, op) \
108 __inline_mathop1(double, func, op)
111 #define __inline_mathop1(float_type,func, op) \
112 __m81_defun (float_type, func, (float_type __mathop_x)) __THROW \
114 float_type __result; \
115 __asm("f" __STRING(op) "%.x %1, %0" : "=f" (__result) : "f" (__mathop_x));\
119 __inline_mathop(__atan
, atan
)
120 __inline_mathop(__cos
, cos
)
121 __inline_mathop(__sin
, sin
)
122 __inline_mathop(__tan
, tan
)
123 __inline_mathop(__tanh
, tanh
)
124 __inline_mathop(__fabs
, abs
)
126 #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED || defined __USE_ISOC99
127 __inline_mathop(__rint
, int)
128 __inline_mathop(__expm1
, etoxm1
)
129 __inline_mathop(__log1p
, lognp1
)
133 __inline_mathop(__significand
, getman
)
137 __inline_mathop(__trunc
, intrz
)
140 #if !defined __NO_MATH_INLINES && defined __OPTIMIZE__
142 __inline_mathop(atan
, atan
)
143 __inline_mathop(cos
, cos
)
144 __inline_mathop(sin
, sin
)
145 __inline_mathop(tan
, tan
)
146 __inline_mathop(tanh
, tanh
)
148 # if defined __USE_MISC || defined __USE_XOPEN_EXTENDED || defined __USE_ISOC99
149 __inline_mathop(rint
, int)
150 __inline_mathop(expm1
, etoxm1
)
151 __inline_mathop(log1p
, lognp1
)
155 __inline_mathop(significand
, getman
)
159 __inline_mathop(trunc
, intrz
)
162 #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
164 /* This macro contains the definition for the rest of the inline
165 functions, using FLOAT_TYPE as the domain type and S as the suffix
166 for the function names. */
168 #define __inline_functions(float_type, s) \
169 __m81_inline float_type \
170 __m81_u(__CONCAT(__frexp,s))(float_type __value, int *__expptr) __THROW \
172 float_type __mantissa, __exponent; \
174 unsigned long __fpsr; \
175 __asm("ftst%.x %1\n" \
176 "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
177 if (__fpsr & (7 << 24)) \
179 /* Not finite or zero. */ \
183 __asm("fgetexp%.x %1, %0" : "=f" (__exponent) : "f" (__value)); \
184 __iexponent = (int) __exponent + 1; \
185 *__expptr = __iexponent; \
186 __asm("fscale%.l %2, %0" : "=f" (__mantissa) \
187 : "0" (__value), "dmi" (-__iexponent)); \
191 __m81_defun (float_type, __CONCAT(__floor,s), (float_type __x)) __THROW \
193 float_type __result; \
194 unsigned long int __ctrl_reg; \
195 __asm __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
196 /* Set rounding towards negative infinity. */ \
197 __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
198 : "dmi" ((__ctrl_reg & ~0x10) | 0x20)); \
199 /* Convert X to an integer, using -Inf rounding. */ \
200 __asm __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
201 /* Restore the previous rounding mode. */ \
202 __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
203 : "dmi" (__ctrl_reg)); \
207 __m81_defun (float_type, __CONCAT(__ceil,s), (float_type __x)) __THROW \
209 float_type __result; \
210 unsigned long int __ctrl_reg; \
211 __asm __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
212 /* Set rounding towards positive infinity. */ \
213 __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
214 : "dmi" (__ctrl_reg | 0x30)); \
215 /* Convert X to an integer, using +Inf rounding. */ \
216 __asm __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
217 /* Restore the previous rounding mode. */ \
218 __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
219 : "dmi" (__ctrl_reg)); \
223 __inline_functions(double,)
224 #if defined __USE_MISC || defined __USE_ISOC99
225 __inline_functions(float,f
)
226 __inline_functions(long double,l
)
228 #undef __inline_functions
232 # define __inline_functions(float_type, s) \
233 __m81_defun (int, __CONCAT(__isinf,s), (float_type __value)) __THROW \
235 /* There is no branch-condition for infinity, \
236 so we must extract and examine the condition codes manually. */ \
237 unsigned long int __fpsr; \
238 __asm("ftst%.x %1\n" \
239 "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
240 return (__fpsr & (2 << 24)) ? (__fpsr & (8 << 24) ? -1 : 1) : 0; \
243 __m81_defun (int, __CONCAT(__finite,s), (float_type __value)) __THROW \
245 /* There is no branch-condition for infinity, so we must extract and \
246 examine the condition codes manually. */ \
247 unsigned long int __fpsr; \
248 __asm ("ftst%.x %1\n" \
249 "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
250 return (__fpsr & (3 << 24)) == 0; \
253 __m81_defun (float_type, __CONCAT(__scalbn,s), \
254 (float_type __x, int __n)) __THROW \
256 float_type __result; \
257 __asm ("fscale%.l %1, %0" : "=f" (__result) : "dmi" (__n), "0" (__x)); \
261 __inline_functions(double,)
262 __inline_functions(float,f
)
263 __inline_functions(long double,l
)
264 # undef __inline_functions
266 #endif /* Use misc. */
268 #if defined __USE_MISC || defined __USE_XOPEN
270 # define __inline_functions(float_type, s) \
271 __m81_defun (int, __CONCAT(__isnan,s), (float_type __value)) __THROW \
274 __asm("ftst%.x %1\n" \
275 "fsun %0" : "=dm" (__result) : "f" (__value)); \
279 __inline_functions(double,)
281 __inline_functions(float,f
)
282 __inline_functions(long double,l
)
284 # undef __inline_functions
290 # define __inline_functions(float_type, s) \
291 __m81_defun (int, __CONCAT(__signbit,s), (float_type __value)) __THROW \
293 /* There is no branch-condition for the sign bit, so we must extract \
294 and examine the condition codes manually. */ \
295 unsigned long int __fpsr; \
296 __asm ("ftst%.x %1\n" \
297 "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
298 return (__fpsr >> 27) & 1; \
301 __m81_defun (float_type, __CONCAT(__scalbln,s), \
302 (float_type __x, long int __n)) __THROW \
304 return __CONCAT(__scalbn,s) (__x, __n); \
307 __m81_defun (float_type, __CONCAT(__nearbyint,s), (float_type __x)) __THROW \
309 float_type __result; \
310 unsigned long int __ctrl_reg; \
311 __asm __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
312 /* Temporarily disable the inexact exception. */ \
313 __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
314 : "dmi" (__ctrl_reg & ~0x200)); \
315 __asm __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
316 __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
317 : "dmi" (__ctrl_reg)); \
321 __m81_defun (long int, __CONCAT(__lrint,s), (float_type __x)) __THROW \
324 __asm ("fmove%.l %1, %0" : "=dm" (__result) : "f" (__x)); \
328 __m81_inline float_type \
329 __m81_u(__CONCAT(__fma,s))(float_type __x, float_type __y, \
330 float_type __z) __THROW \
332 return (__x * __y) + __z; \
335 __inline_functions (double,)
336 __inline_functions (float,f
)
337 __inline_functions (long double,l
)
338 # undef __inline_functions
340 #endif /* Use ISO C9x */
344 # define __inline_functions(float_type, s) \
346 __m81_u(__CONCAT(__sincos,s))(float_type __x, float_type *__sinx, \
347 float_type *__cosx) __THROW \
349 __asm ("fsincos%.x %2,%1:%0" \
350 : "=f" (*__sinx), "=f" (*__cosx) : "f" (__x)); \
353 __inline_functions (double,)
354 __inline_functions (float,f
)
355 __inline_functions (long double,l
)
356 # undef __inline_functions
360 #if !defined __NO_MATH_INLINES && defined __OPTIMIZE__
362 /* Define inline versions of the user visible functions. */
364 /* Note that there must be no whitespace before the argument passed for
365 NAME, to make token pasting work correctly with -traditional. */
366 # define __inline_forward_c(rettype, name, args1, args2) \
367 extern __inline rettype __attribute__((__const__)) \
370 return __CONCAT(__,name) args2; \
373 # define __inline_forward(rettype, name, args1, args2) \
374 extern __inline rettype name args1 __THROW \
376 return __CONCAT(__,name) args2; \
379 __inline_forward(double,frexp
, (double __value
, int *__expptr
),
381 __inline_forward_c(double,floor
, (double __x
), (__x
))
382 __inline_forward_c(double,ceil
, (double __x
), (__x
))
384 # ifndef __USE_ISOC99 /* Conflict with macro of same name. */
385 __inline_forward_c(int,isinf
, (double __value
), (__value
))
387 __inline_forward_c(int,finite
, (double __value
), (__value
))
388 __inline_forward_c(double,scalbn
, (double __x
, int __n
), (__x
, __n
))
390 # if defined __USE_MISC || defined __USE_XOPEN
391 # ifndef __USE_ISOC99 /* Conflict with macro of same name. */
392 __inline_forward_c(int,isnan
, (double __value
), (__value
))
396 __inline_forward_c(double,scalbln
, (double __x
, long int __n
), (__x
, __n
))
397 __inline_forward_c(double,nearbyint
, (double __value
), (__value
))
398 __inline_forward_c(long int,lrint
, (double __value
), (__value
))
399 __inline_forward_c(double,fma
, (double __x
, double __y
, double __z
),
403 __inline_forward(void,sincos
, (double __x
, double *__sinx
, double *__cosx
),
404 (__x
, __sinx
, __cosx
))
407 # if defined __USE_MISC || defined __USE_ISOC99
409 __inline_forward(float,frexpf
, (float __value
, int *__expptr
),
411 __inline_forward_c(float,floorf
, (float __x
), (__x
))
412 __inline_forward_c(float,ceilf
, (float __x
), (__x
))
414 __inline_forward_c(int,isinff
, (float __value
), (__value
))
415 __inline_forward_c(int,finitef
, (float __value
), (__value
))
416 __inline_forward_c(float,scalbnf
, (float __x
, int __n
), (__x
, __n
))
417 __inline_forward_c(int,isnanf
, (float __value
), (__value
))
420 __inline_forward_c(float,scalblnf
, (float __x
, long int __n
), (__x
, __n
))
421 __inline_forward_c(float,nearbyintf
, (float __value
), (__value
))
422 __inline_forward_c(long int,lrintf
, (float __value
), (__value
))
423 __inline_forward_c(float,fmaf
, (float __x
, float __y
, float __z
),
427 __inline_forward(void,sincosf
, (float __x
, float *__sinx
, float *__cosx
),
428 (__x
, __sinx
, __cosx
))
431 __inline_forward(long double,frexpl
, (long double __value
, int *__expptr
),
433 __inline_forward_c(long double,floorl
, (long double __x
), (__x
))
434 __inline_forward_c(long double,ceill
, (long double __x
), (__x
))
436 __inline_forward_c(int,isinfl
, (long double __value
), (__value
))
437 __inline_forward_c(int,finitel
, (long double __value
), (__value
))
438 __inline_forward_c(long double,scalbnl
, (long double __x
, int __n
), (__x
, __n
))
439 __inline_forward_c(int,isnanl
, (long double __value
), (__value
))
442 __inline_forward_c(long double,scalblnl
, (long double __x
, long int __n
),
444 __inline_forward_c(long double,nearbyintl
, (long double __value
), (__value
))
445 __inline_forward_c(long int,lrintl
, (long double __value
), (__value
))
446 __inline_forward_c(long double,fmal
,
447 (long double __x
, long double __y
, long double __z
),
451 __inline_forward(void,sincosl
,
452 (long double __x
, long double *__sinx
, long double *__cosx
),
453 (__x
, __sinx
, __cosx
))
456 #endif /* Use misc or ISO C99 */
458 #undef __inline_forward
459 #undef __inline_forward_c
461 #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */