1 /* Definitions of inline math functions implemented by the m68881/2.
2 Copyright (C) 1991, 92, 93, 94, 96, 97 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. */
22 #include <sys/cdefs.h>
24 #ifdef __LIBC_M81_MATH_INLINES
25 /* This is used when defining the functions themselves. Define them with
26 __ names, and with `static inline' instead of `extern inline' so the
27 bodies will always be used, never an external function call. */
28 #define __m81_u(x) __CONCAT(__,x)
29 #define __m81_inline static __inline
32 #define __m81_inline extern __inline
33 #define __M81_MATH_INLINES 1
36 /* Define a const math function. */
37 #define __m81_defun(rettype, func, args) \
38 __m81_inline rettype __attribute__((__const__)) \
41 /* Define the three variants of a math function that has a direct
42 implementation in the m68k fpu. FUNC is the name for C (which will be
43 suffixed with f and l for the float and long double version, resp). OP
44 is the name of the fpu operation (without leading f). */
47 #define __inline_mathop(func, op) \
48 __inline_mathop1(double, func, op) \
49 __inline_mathop1(float, __CONCAT(func,f), op) \
50 __inline_mathop1(long double, __CONCAT(func,l), op)
52 #define __inline_mathop(func, op) \
53 __inline_mathop1(double, func, op)
56 #define __inline_mathop1(float_type,func, op) \
57 __m81_defun (float_type, func, (float_type __mathop_x)) \
59 float_type __result; \
60 __asm("f" __STRING(op) "%.x %1, %0" : "=f" (__result) : "f" (__mathop_x));\
64 #ifdef __LIBC_M81_MATH_INLINES
65 /* ieee style elementary functions */
66 /* These are internal to the implementation of libm. */
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
)
78 __inline_mathop(__atan
, atan
)
79 __inline_mathop(__cos
, cos
)
80 __inline_mathop(__sin
, sin
)
81 __inline_mathop(__tan
, tan
)
82 __inline_mathop(__tanh
, tanh
)
83 __inline_mathop(__fabs
, abs
)
84 __inline_mathop(__sqrt
, sqrt
)
86 __inline_mathop(__rint
, int)
87 __inline_mathop(__expm1
, etoxm1
)
88 __inline_mathop(__log1p
, lognp1
)
89 __inline_mathop(__logb
, log2
)
90 __inline_mathop(__significand
, getman
)
92 #if !defined __NO_MATH_INLINES && defined __OPTIMIZE__
94 __inline_mathop(atan
, atan
)
95 __inline_mathop(cos
, cos
)
96 __inline_mathop(sin
, sin
)
97 __inline_mathop(tan
, tan
)
98 __inline_mathop(tanh
, tanh
)
99 __inline_mathop(fabs
, abs
)
100 __inline_mathop(sqrt
, sqrt
)
102 #if defined(__USE_MISC) || defined(__USE_XOPEN_EXTENDED)
103 __inline_mathop(rint
, int)
104 __inline_mathop(expm1
, etoxm1
)
105 __inline_mathop(log1p
, lognp1
)
106 __inline_mathop(logb
, log2
)
110 __inline_mathop(significand
, getman
)
113 #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */
115 /* This macro contains the definition for the rest of the inline
116 functions, using __FLOAT_TYPE as the domain type and __S as the suffix
117 for the function names. */
119 #ifdef __LIBC_M81_MATH_INLINES
120 /* Internally used functions. */
121 #define __internal_inline_functions(float_type, s) \
122 __m81_defun (float_type, __CONCAT(__ieee754_remainder,s), \
123 (float_type __x, float_type __y)) \
125 float_type __result; \
126 __asm("frem%.x %1, %0" : "=f" (__result) : "f" (__y), "0" (__x)); \
130 __m81_defun (float_type, __CONCAT(__ieee754_fmod,s), \
131 (float_type __x, float_type __y)) \
133 float_type __result; \
134 __asm("fmod%.x %1, %0" : "=f" (__result) : "f" (__y), "0" (__x)); \
138 __m81_defun (float_type, __CONCAT(__ieee754_atan2,s), \
139 (float_type __y, float_type __x)) \
141 float_type __pi, __pi_2; \
143 __asm ("fmovecr%.x %#0, %0" : "=f" (__pi)); \
144 __asm ("fscale%.w %#-1, %0" : "=f" (__pi_2) : "0" (__pi)); \
150 return __m81_u(__CONCAT(__atan,s)) (__y / __x); \
152 return __pi_2 - __m81_u(__CONCAT(__atan,s)) (__x / __y); \
157 return __m81_u(__CONCAT(__atan,s)) (__y / __x); \
159 return -__pi_2 - __m81_u(__CONCAT(__atan,s)) (__x / __y); \
167 return __pi + __m81_u(__CONCAT(__atan,s)) (__y / __x); \
169 return __pi_2 - __m81_u(__CONCAT(__atan,s)) (__x / __y); \
174 return -__pi + __m81_u(__CONCAT(__atan,s)) (__y / __x); \
176 return -__pi_2 - __m81_u(__CONCAT(__atan,s)) (__x / __y); \
181 __m81_defun (float_type, __CONCAT(__ieee754_pow,s), \
182 (float_type __x, float_type __y)) \
184 float_type __result; \
188 __result = 0.0 / 0.0; \
192 else if (__y == 0.0 || __x == 1.0) \
194 else if (__y == 1.0) \
196 else if (__y == 2.0) \
197 __result = __x * __x; \
198 else if (__x == 10.0) \
199 __asm("ftentox%.x %1, %0" : "=f" (__result) : "f" (__y)); \
200 else if (__x == 2.0) \
201 __asm("ftwotox%.x %1, %0" : "=f" (__result) : "f" (__y)); \
202 else if (__x < 0.0) \
204 float_type __temp = __m81_u (__CONCAT(__rint,s)) (__y); \
207 int __i = (int) __y; \
208 __result = (__m81_u(__CONCAT(__ieee754_exp,s)) \
209 (__y * __m81_u(__CONCAT(__ieee754_log,s)) (-__x))); \
211 __result = -__result; \
214 __result = 0.0 / 0.0; \
217 __result = (__m81_u(__CONCAT(__ieee754_exp,s)) \
218 (__y * __m81_u(__CONCAT(__ieee754_log,s)) (__x))); \
222 __m81_defun (float_type, __CONCAT(__ieee754_scalb,s), \
223 (float_type __x, float_type __n)) \
225 float_type __result; \
226 __asm ("fscale%.x %1, %0" : "=f" (__result) : "f" (__n), "0" (__x)); \
230 __internal_inline_functions (double,)
231 __internal_inline_functions (float,f
)
232 __internal_inline_functions (long double,l
)
233 #undef __internal_inline_functions
235 #endif /* __LIBC_M81_MATH_INLINES */
237 /* The rest of the functions are available to the user. */
239 #define __inline_functions(float_type, s) \
240 __m81_inline float_type \
241 __m81_u(__CONCAT(__frexp,s))(float_type __value, int *__expptr) \
243 float_type __mantissa, __exponent; \
245 if (__value == 0.0) \
250 __asm("fgetexp%.x %1, %0" : "=f" (__exponent) : "f" (__value)); \
251 __iexponent = (int) __exponent + 1; \
252 *__expptr = __iexponent; \
253 __asm("fscale%.l %2, %0" : "=f" (__mantissa) \
254 : "0" (__value), "dmi" (-__iexponent)); \
258 __m81_defun (float_type, __CONCAT(__floor,s), (float_type __x)) \
260 float_type __result; \
261 unsigned long int __ctrl_reg; \
262 __asm __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
263 /* Set rounding towards negative infinity. */ \
264 __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
265 : "dmi" ((__ctrl_reg & ~0x10) | 0x20)); \
266 /* Convert X to an integer, using -Inf rounding. */ \
267 __asm __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
268 /* Restore the previous rounding mode. */ \
269 __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
270 : "dmi" (__ctrl_reg)); \
274 __m81_defun (float_type, __CONCAT(__ceil,s), (float_type __x)) \
276 float_type __result; \
277 unsigned long int __ctrl_reg; \
278 __asm __volatile__ ("fmove%.l %!, %0" : "=dm" (__ctrl_reg)); \
279 /* Set rounding towards positive infinity. */ \
280 __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
281 : "dmi" (__ctrl_reg | 0x30)); \
282 /* Convert X to an integer, using +Inf rounding. */ \
283 __asm __volatile__ ("fint%.x %1, %0" : "=f" (__result) : "f" (__x)); \
284 /* Restore the previous rounding mode. */ \
285 __asm __volatile__ ("fmove%.l %0, %!" : /* No outputs. */ \
286 : "dmi" (__ctrl_reg)); \
290 __m81_inline float_type \
291 __m81_u(__CONCAT(__modf,s))(float_type __value, float_type *__iptr) \
293 float_type __modf_int; \
294 __asm ("fintrz%.x %1, %0" : "=f" (__modf_int) : "f" (__value)); \
295 *__iptr = __modf_int; \
296 return __value - __modf_int; \
299 __m81_defun (int, __CONCAT(__isinf,s), (float_type __value)) \
301 /* There is no branch-condition for infinity, \
302 so we must extract and examine the condition codes manually. */ \
303 unsigned long int __fpsr; \
304 __asm("ftst%.x %1\n" \
305 "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
306 return (__fpsr & (2 << 24)) ? (__fpsr & (8 << 24) ? -1 : 1) : 0; \
309 __m81_defun (int, __CONCAT(__isnan,s), (float_type __value)) \
312 __asm("ftst%.x %1\n" \
313 "fsun %0" : "=dm" (__result) : "f" (__value)); \
317 __m81_defun (int, __CONCAT(__finite,s), (float_type __value)) \
319 /* There is no branch-condition for infinity, so we must extract and \
320 examine the condition codes manually. */ \
321 unsigned long int __fpsr; \
322 __asm ("ftst%.x %1\n" \
323 "fmove%.l %/fpsr, %0" : "=dm" (__fpsr) : "f" (__value)); \
324 return (__fpsr & (3 << 24)) == 0; \
327 __m81_defun (int, __CONCAT(__ilogb,s), (float_type __x)) \
329 float_type __result; \
332 __asm("fgetexp%.x %1, %0" : "=f" (__result) : "f" (__x)); \
333 return (int) __result; \
336 __m81_defun (float_type, __CONCAT(__scalbn,s), (float_type __x, int __n)) \
338 float_type __result; \
339 __asm ("fscale%.l %1, %0" : "=f" (__result) : "dmi" (__n), "0" (__x)); \
343 /* This defines the three variants of the inline functions. */
344 __inline_functions (double,)
345 __inline_functions (float,f
)
346 __inline_functions (long double,l
)
347 #undef __inline_functions
349 #if !defined __NO_MATH_INLINES && defined __OPTIMIZE__
351 /* Define inline versions of the user visible functions. */
353 #define __inline_forward_c(rettype, name, args1, args2) \
354 extern __inline rettype __attribute__((__const__)) \
357 return __CONCAT(__,name) args2; \
360 #define __inline_forward(rettype, name, args1, args2) \
361 extern __inline rettype name args1 \
363 return __CONCAT(__,name) args2; \
366 __inline_forward(double,frexp
, (double __value
, int *__expptr
),
368 __inline_forward_c(double,floor
, (double __x
), (__x
))
369 __inline_forward_c(double,ceil
, (double __x
), (__x
))
370 __inline_forward(double,modf
, (double __value
, double *__iptr
),
373 __inline_forward_c(int,isinf
, (double __value
), (__value
))
374 __inline_forward_c(int,finite
, (double __value
), (__value
))
375 __inline_forward_c(double,scalbn
, (double __x
, int __n
), (__x
, __n
))
377 #if defined __USE_MISC || defined __USE_XOPEN
378 __inline_forward_c(int,isnan
, (double __value
), (__value
))
379 __inline_forward_c(int,ilogb
, (double __value
), (__value
))
384 __inline_forward(float,frexpf
, (float __value
, int *__expptr
),
386 __inline_forward_c(float,floorf
, (float __x
), (__x
))
387 __inline_forward_c(float,ceilf
, (float __x
), (__x
))
388 __inline_forward(float,modff
, (float __value
, float *__iptr
),
390 __inline_forward_c(int,isinff
, (float __value
), (__value
))
391 __inline_forward_c(int,finitef
, (float __value
), (__value
))
392 __inline_forward_c(float,scalbnf
, (float __x
, int __n
), (__x
, __n
))
393 __inline_forward_c(int,isnanf
, (float __value
), (__value
))
394 __inline_forward_c(int,ilogbf
, (float __value
), (__value
))
396 __inline_forward(long double,frexpl
, (long double __value
, int *__expptr
),
398 __inline_forward_c(long double,floorl
, (long double __x
), (__x
))
399 __inline_forward_c(long double,ceill
, (long double __x
), (__x
))
400 __inline_forward(long double,modfl
,
401 (long double __value
, long double *__iptr
),
403 __inline_forward_c(int,isinfl
, (long double __value
), (__value
))
404 __inline_forward_c(int,finitel
, (long double __value
), (__value
))
405 __inline_forward_c(long double,scalbnl
, (long double __x
, int __n
),
407 __inline_forward_c(int,isnanl
, (long double __value
), (__value
))
408 __inline_forward_c(int,ilogbl
, (long double __value
), (__value
))
410 #endif /* __USE_MISC */
412 #undef __inline_forward
413 #undef __inline_forward_c
415 #endif /* !__NO_MATH_INLINES && __OPTIMIZE__ */