xref: /minix3/include/tgmath.h (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras /*-
2*2fe8fb19SBen Gras  * Copyright (c) 2008 The NetBSD Foundation, Inc.
3*2fe8fb19SBen Gras  * All rights reserved.
4*2fe8fb19SBen Gras  *
5*2fe8fb19SBen Gras  * This code is derived from software contributed to The NetBSD Foundation
6*2fe8fb19SBen Gras  * by Matt Thomas <matt@3am-software.com>
7*2fe8fb19SBen Gras  *
8*2fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
9*2fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
10*2fe8fb19SBen Gras  * are met:
11*2fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
12*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
13*2fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
14*2fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
15*2fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
16*2fe8fb19SBen Gras  *
17*2fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
18*2fe8fb19SBen Gras  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19*2fe8fb19SBen Gras  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20*2fe8fb19SBen Gras  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
21*2fe8fb19SBen Gras  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22*2fe8fb19SBen Gras  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23*2fe8fb19SBen Gras  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*2fe8fb19SBen Gras  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25*2fe8fb19SBen Gras  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26*2fe8fb19SBen Gras  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27*2fe8fb19SBen Gras  * POSSIBILITY OF SUCH DAMAGE.
28*2fe8fb19SBen Gras  */
29*2fe8fb19SBen Gras 
30*2fe8fb19SBen Gras #ifndef _TGMATH_H_
31*2fe8fb19SBen Gras #define	_TGMATH_H_
32*2fe8fb19SBen Gras 
33*2fe8fb19SBen Gras #include <math.h>
34*2fe8fb19SBen Gras #include <complex.h>
35*2fe8fb19SBen Gras 
36*2fe8fb19SBen Gras /*
37*2fe8fb19SBen Gras  * C99 Type-generic math (7.22)
38*2fe8fb19SBen Gras  */
39*2fe8fb19SBen Gras #ifdef __GNUC__
40*2fe8fb19SBen Gras #define	__TG_CHOOSE(p, a, b)	__builtin_choose_expr((p), (a), (b))
41*2fe8fb19SBen Gras #define	__TG_IS_EQUIV_TYPE_P(v, t)	\
42*2fe8fb19SBen Gras 	 __builtin_types_compatible_p(__typeof__(v), t)
43*2fe8fb19SBen Gras #else
44*2fe8fb19SBen Gras #error how does this compler do type-generic macros?
45*2fe8fb19SBen Gras #endif
46*2fe8fb19SBen Gras 
47*2fe8fb19SBen Gras #define	__TG_IS_FCOMPLEX_P(t)	__TG_IS_EQUIV_TYPE_P(t, float complex)
48*2fe8fb19SBen Gras #define	__TG_IS_DCOMPLEX_P(t)	__TG_IS_EQUIV_TYPE_P(t, double complex)
49*2fe8fb19SBen Gras #define	__TG_IS_LCOMPLEX_P(t)	__TG_IS_EQUIV_TYPE_P(t, long double complex)
50*2fe8fb19SBen Gras #define	__TG_IS_FLOAT_P(t)	__TG_IS_EQUIV_TYPE_P(t, float)
51*2fe8fb19SBen Gras #define	__TG_IS_LDOUBLE_P(t)	__TG_IS_EQUIV_TYPE_P(t, long double)
52*2fe8fb19SBen Gras #define	__TG_IS_FREAL_P(t)	(__TG_IS_FLOAT_P(t) || __TG_IS_FCOMPLEX_P(t))
53*2fe8fb19SBen Gras #define	__TG_IS_LREAL_P(t)	(__TG_IS_LDOUBLE_P(t) || __TG_IS_LCOMPLEX_P(t))
54*2fe8fb19SBen Gras 
55*2fe8fb19SBen Gras #define	__TG_IS_COMPLEX_P(t)					\
56*2fe8fb19SBen Gras 	(__TG_IS_FCOMPLEX_P(t)					\
57*2fe8fb19SBen Gras 	 || __TG_IS_DCOMPLEX_P(t)				\
58*2fe8fb19SBen Gras 	 || __TG_IS_LCOMPLEX_P(t))
59*2fe8fb19SBen Gras 
60*2fe8fb19SBen Gras #define	__TG_GFN1(fn, a, ftype, ltype)				\
61*2fe8fb19SBen Gras 	__TG_CHOOSE(__TG_IS_##ftype##_P(a),			\
62*2fe8fb19SBen Gras 		    fn##f(a),					\
63*2fe8fb19SBen Gras 		    __TG_CHOOSE(__TG_IS_##ltype##_P(a),		\
64*2fe8fb19SBen Gras 				fn##l(a),			\
65*2fe8fb19SBen Gras 				fn(a)))
66*2fe8fb19SBen Gras 
67*2fe8fb19SBen Gras #define	__TG_GFN1x(fn, a, b, ftype, ltype)			\
68*2fe8fb19SBen Gras 	__TG_CHOOSE(__TG_IS_##ftype##_P(a),			\
69*2fe8fb19SBen Gras 		    fn##f((a), (b)),				\
70*2fe8fb19SBen Gras 		    __TG_CHOOSE(__TG_IS_##ltype##_P(a),		\
71*2fe8fb19SBen Gras 				fn##l((a), (b)),		\
72*2fe8fb19SBen Gras 				fn((a), (b))))
73*2fe8fb19SBen Gras 
74*2fe8fb19SBen Gras #define	__TG_GFN2(fn, a, b, ftype, ltype)			\
75*2fe8fb19SBen Gras 	__TG_CHOOSE(__TG_IS_##ftype##_P(a)			\
76*2fe8fb19SBen Gras 		    && __TG_IS_##ftype##_P(b),			\
77*2fe8fb19SBen Gras 		    fn##f((a), (b)),				\
78*2fe8fb19SBen Gras 		    __TG_CHOOSE(__TG_IS_##ltype##_P(a)		\
79*2fe8fb19SBen Gras 				|| __TG_IS_##ltype##_P(b),	\
80*2fe8fb19SBen Gras 				fn##l((a), (b)),		\
81*2fe8fb19SBen Gras 				fn((a), (b))))
82*2fe8fb19SBen Gras 
83*2fe8fb19SBen Gras #define	__TG_GFN2x(fn, a, b, c, ftype, ltype)			\
84*2fe8fb19SBen Gras 	__TG_CHOOSE(__TG_IS_##ftype##_P(a)			\
85*2fe8fb19SBen Gras 		    && __TG_IS_##ftype##_P(b),			\
86*2fe8fb19SBen Gras 		    fn##f((a), (b), (c)),			\
87*2fe8fb19SBen Gras 		    __TG_CHOOSE(__TG_IS_##ltype##_P(a)		\
88*2fe8fb19SBen Gras 				|| __TG_IS_##ltype##_P(b),	\
89*2fe8fb19SBen Gras 				fn##l((a), (b), (c)),		\
90*2fe8fb19SBen Gras 				fn((a), (b), (c))))
91*2fe8fb19SBen Gras 
92*2fe8fb19SBen Gras #define	__TG_GFN3(fn, a, b, c, ftype, ltype)			\
93*2fe8fb19SBen Gras 	__TG_CHOOSE(__TG_IS_##ftype##_P(a)			\
94*2fe8fb19SBen Gras 		    && __TG_IS_##ftype##_P(b)			\
95*2fe8fb19SBen Gras 		    && __TG_IS_##ftype##_P(c),			\
96*2fe8fb19SBen Gras 		    fn##f((a), (b), (c)),			\
97*2fe8fb19SBen Gras 		    __TG_CHOOSE(__TG_IS_##ltype##_P(a)		\
98*2fe8fb19SBen Gras 				|| __TG_IS_##ltype##_P(b)	\
99*2fe8fb19SBen Gras 				|| __TG_IS_##ltype##_P(c),	\
100*2fe8fb19SBen Gras 				fn##l((a), (b), (c)),		\
101*2fe8fb19SBen Gras 				fn((a), (b), (c))))
102*2fe8fb19SBen Gras 
103*2fe8fb19SBen Gras 
104*2fe8fb19SBen Gras #define	__TG_CFN1(cfn, a)	__TG_GFN1(cfn, a, FREAL, LREAL)
105*2fe8fb19SBen Gras #define	__TG_CFN2(cfn, a, b)	__TG_GFN2(cfn, a, b, FREAL, LREAL)
106*2fe8fb19SBen Gras 
107*2fe8fb19SBen Gras #define	__TG_FN1(fn, a)		__TG_GFN1(fn, a, FLOAT, LDOUBLE)
108*2fe8fb19SBen Gras #define	__TG_FN1x(fn, a, b)	__TG_GFN1x(fn, a, b, FLOAT, LDOUBLE)
109*2fe8fb19SBen Gras #define	__TG_FN2(fn, a, b)	__TG_GFN2(fn, a, b, FLOAT, LDOUBLE)
110*2fe8fb19SBen Gras #define	__TG_FN2x(fn, a, b, c)	__TG_GFN2x(fn, a, b, c, FLOAT, LDOUBLE)
111*2fe8fb19SBen Gras #define	__TG_FN3(fn, a, b, c)	__TG_GFN3(fn, a, b, c, FLOAT, LDOUBLE)
112*2fe8fb19SBen Gras 
113*2fe8fb19SBen Gras #define	__TG_COMPLEX(a, fn)			\
114*2fe8fb19SBen Gras 	__TG_CHOOSE(__TG_IS_COMPLEX_P(a),	\
115*2fe8fb19SBen Gras 		    __TG_CFN1(c##fn, (a)),	\
116*2fe8fb19SBen Gras 		    __TG_FN1(fn, (a)))
117*2fe8fb19SBen Gras 
118*2fe8fb19SBen Gras #define	__TG_COMPLEX1(a, cfn, fn)		\
119*2fe8fb19SBen Gras 	__TG_CHOOSE(__TG_IS_COMPLEX_P(a),	\
120*2fe8fb19SBen Gras 		    __TG_CFN1(cfn, (a)),	\
121*2fe8fb19SBen Gras 		    __TG_FN1(fn, (a)))
122*2fe8fb19SBen Gras 
123*2fe8fb19SBen Gras #define	__TG_COMPLEX2(a, b, fn)			\
124*2fe8fb19SBen Gras 	__TG_CHOOSE(__TG_IS_COMPLEX_P(a)	\
125*2fe8fb19SBen Gras 		    || __TG_IS_COMPLEX_P(b),	\
126*2fe8fb19SBen Gras 		    __TG_CFN2(c##fn, (a), (b)),	\
127*2fe8fb19SBen Gras 		    __TG_FN2(fn, (a), (b)))
128*2fe8fb19SBen Gras 
129*2fe8fb19SBen Gras #define	acos(a)		__TG_COMPLEX((a), acos)
130*2fe8fb19SBen Gras #define	asin(a)		__TG_COMPLEX((a), asin)
131*2fe8fb19SBen Gras #define	atan(a)		__TG_COMPLEX((a), atan)
132*2fe8fb19SBen Gras #define	acosh(a)	__TG_COMPLEX((a), acosh)
133*2fe8fb19SBen Gras #define	asinh(a)	__TG_COMPLEX((a), asinh)
134*2fe8fb19SBen Gras #define	atanh(a)	__TG_COMPLEX((a), atanh)
135*2fe8fb19SBen Gras #define	cos(a)		__TG_COMPLEX((a), cos)
136*2fe8fb19SBen Gras #define	sin(a)		__TG_COMPLEX((a), sin)
137*2fe8fb19SBen Gras #define	tan(a)		__TG_COMPLEX((a), tan)
138*2fe8fb19SBen Gras #define	cosh(a)		__TG_COMPLEX((a), cosh)
139*2fe8fb19SBen Gras #define	sinh(a)		__TG_COMPLEX((a), sinh)
140*2fe8fb19SBen Gras #define	tanh(a)		__TG_COMPLEX((a), tanh)
141*2fe8fb19SBen Gras #define	exp(a)		__TG_COMPLEX((a), exp)
142*2fe8fb19SBen Gras #define	log(a)		__TG_COMPLEX((a), log)
143*2fe8fb19SBen Gras #define	pow(a,b)	__TG_COMPLEX2((a), (b), pow)
144*2fe8fb19SBen Gras #define	sqrt(a)		__TG_COMPLEX((a), sqrt)
145*2fe8fb19SBen Gras #define	fabs(a)		__TG_COMPLEX1((a), cabs, fabs)
146*2fe8fb19SBen Gras 
147*2fe8fb19SBen Gras #define	atan2(a,b)	__TG_FN2(atan2, (a), (b))
148*2fe8fb19SBen Gras #define	cbrt(a)		__TG_FN1(cbrt, (a))
149*2fe8fb19SBen Gras #define	ceil(a)		__TG_FN1(ceil, (a))
150*2fe8fb19SBen Gras #define	copysign(a,b)	__TG_FN2(copysign, (a), (b))
151*2fe8fb19SBen Gras #define	erf(a)		__TG_FN1(erf, (a))
152*2fe8fb19SBen Gras #define	erfc(a)		__TG_FN1(erfc, (a))
153*2fe8fb19SBen Gras #define	exp2(a)		__TG_FN1(exp2, (a))
154*2fe8fb19SBen Gras #define	expm1(a)	__TG_FN1(expm1, (a))
155*2fe8fb19SBen Gras #define	fdim(a,b)	__TG_FN2(fdim, (a), (b))
156*2fe8fb19SBen Gras #define	floor(a)	__TG_FN1(floor, (a))
157*2fe8fb19SBen Gras #define	fma(a,b,c)	__TG_FN3(fma, (a), (b), (c))
158*2fe8fb19SBen Gras #define	fmax(a,b)	__TG_FN2(fmax, (a), (b))
159*2fe8fb19SBen Gras #define	fmin(a,b)	__TG_FN2(fmin, (a), (b))
160*2fe8fb19SBen Gras #define	fmod(a,b)	__TG_FN2(fmod, (a), (b))
161*2fe8fb19SBen Gras #define	frexp(a,b)	__TG_FN1x(frexp, (a), (b))
162*2fe8fb19SBen Gras #define	hypot(a,b)	__TG_FN2(hypot, (a), (b))
163*2fe8fb19SBen Gras #define	ilogb(a)	__TG_FN1(ilogb, (a))
164*2fe8fb19SBen Gras #define	ldexp(a,b)	__TG_FN1x(ldexp, (a), (b))
165*2fe8fb19SBen Gras #define	lgamma(a)	__TG_FN1(lgamma, (a))
166*2fe8fb19SBen Gras #define	llrint(a)	__TG_FN1(llrint, (a))
167*2fe8fb19SBen Gras #define	llround(a)	__TG_FN1(llround, (a))
168*2fe8fb19SBen Gras #define	log10(a)	__TG_FN1(log10, (a))
169*2fe8fb19SBen Gras #define	log1p(a)	__TG_FN1(log1p, (a))
170*2fe8fb19SBen Gras #define	log2(a)		__TG_FN1(log2, (a))
171*2fe8fb19SBen Gras #define	logb(a)		__TG_FN1(logb, (a))
172*2fe8fb19SBen Gras #define	lrint(a)	__TG_FN1(lrint, (a))
173*2fe8fb19SBen Gras #define	lround(a)	__TG_FN1(lround, (a))
174*2fe8fb19SBen Gras #define	nearbyint(a)	__TG_FN1(nearbyint, (a))
175*2fe8fb19SBen Gras #define	nextafter(a,b)	__TG_FN2(nextafter, (a), (b))
176*2fe8fb19SBen Gras #define	nexttoward(a,b)	__TG_FN2(nexttoward, (a), (b))
177*2fe8fb19SBen Gras #define	remainder(a,b)	__TG_FN2(remainder, (a), (b))
178*2fe8fb19SBen Gras #define	remquo(a,b,c)	__TG_FN2x(remquo, (a), (b), (c))
179*2fe8fb19SBen Gras #define	rint(a)		__TG_FN1(rint, (a))
180*2fe8fb19SBen Gras #define	round(a)	__TG_FN1(round, (a))
181*2fe8fb19SBen Gras #define	scalbn(a,b)	__TG_FN1x(scalbn, (a), (b))
182*2fe8fb19SBen Gras #define	scalb1n(a,b)	__TG_FN1x(scalb1n, (a), (b))
183*2fe8fb19SBen Gras #define	tgamma(a)	__TG_FN1(tgamma, (a))
184*2fe8fb19SBen Gras #define	trunc(a)	__TG_FN1(trunc, (a))
185*2fe8fb19SBen Gras 
186*2fe8fb19SBen Gras #define	carg(a)		__TG_CFN1(carg, (a))
187*2fe8fb19SBen Gras #define	cimag(a)	__TG_CFN1(cimag, (a))
188*2fe8fb19SBen Gras #define	conj(a)		__TG_CFN1(conj, (a))
189*2fe8fb19SBen Gras #define	cproj(a)	__TG_CFN1(cproj, (a))
190*2fe8fb19SBen Gras #define	creal(a)	__TG_CFN1(creal, (a))
191*2fe8fb19SBen Gras 
192*2fe8fb19SBen Gras #endif /* !_TGMATH_H_ */
193