xref: /freebsd/include/tgmath.h (revision ca27f0cef04fe67c812aae3568211798f52f28ee)
1 /*-
2  * Copyright (c) 2004 Stefan Farfeleder.
3  * All rights reserved.
4  *
5  * Copyright (c) 2012 Ed Schouten <ed@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 
32 #ifndef _TGMATH_H_
33 #define	_TGMATH_H_
34 
35 #include <complex.h>
36 #include <math.h>
37 
38 /*
39  * This implementation of <tgmath.h> uses the two following macros,
40  * which are based on the macros described in C11 proposal N1404:
41  * __tg_impl_simple(x, y, z, fnl, fn, fnf, ...)
42  *	Invokes fnl() if the corresponding real type of x, y or z is long
43  *	double, fn() if it is double or any has an integer type, and fnf()
44  *	otherwise.
45  * __tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...)
46  *	Invokes [c]fnl() if the corresponding real type of x or y is long
47  *	double, [c]fn() if it is double or any has an integer type, and
48  *	[c]fnf() otherwise.  The function with the 'c' prefix is called if
49  *	any of x or y is a complex number.
50  * Both macros call the chosen function with all additional arguments passed
51  * to them, as given by __VA_ARGS__.
52  *
53  * Note that these macros cannot be implemented with C's ?: operator,
54  * because the return type of the whole expression would incorrectly be long
55  * double complex regardless of the argument types.
56  *
57  * The structure of the C11 implementation of these macros can in
58  * principle be reused for non-C11 compilers, but due to an integer
59  * promotion bug for complex types in GCC 4.2, simply let non-C11
60  * compilers use an inefficient yet reliable version.
61  */
62 
63 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L
64 #define	__tg_generic(x, cfnl, cfn, cfnf, fnl, fn, fnf)			\
65 	_Generic(x,							\
66 		long double _Complex: cfnl,				\
67 		double _Complex: cfn,					\
68 		float _Complex: cfnf,					\
69 		long double: fnl,					\
70 		default: fn,						\
71 		float: fnf						\
72 	)
73 #define	__tg_type(x)							\
74 	__tg_generic(x, (long double _Complex)0, (double _Complex)0,	\
75 	    (float _Complex)0, (long double)0, (double)0, (float)0)
76 #define	__tg_impl_simple(x, y, z, fnl, fn, fnf, ...)			\
77 	__tg_generic(							\
78 	    __tg_type(x) + __tg_type(y) + __tg_type(z),			\
79 	    fnl, fn, fnf, fnl, fn, fnf)(__VA_ARGS__)
80 #define	__tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...)	\
81 	__tg_generic(							\
82 	    __tg_type(x) + __tg_type(y),				\
83 	    cfnl, cfn, cfnf, fnl, fn, fnf)(__VA_ARGS__)
84 #elif defined(__generic)
85 #define	__tg_generic_simple(x, fnl, fn, fnf)				\
86 	__generic(x, long double _Complex, fnl,				\
87 	    __generic(x, double _Complex, fn,				\
88 	        __generic(x, float _Complex, fnf,			\
89 	            __generic(x, long double, fnl,			\
90 	                __generic(x, float, fnf, fn)))))
91 #define	__tg_impl_simple(x, y, z, fnl, fn, fnf, ...)			\
92 	__tg_generic_simple(x,						\
93 	    __tg_generic_simple(y,					\
94 	        __tg_generic_simple(z, fnl, fnl, fnl),			\
95 	        __tg_generic_simple(z, fnl, fnl, fnl),			\
96 	        __tg_generic_simple(z, fnl, fnl, fnl)),			\
97 	    __tg_generic_simple(y,					\
98 	        __tg_generic_simple(z, fnl, fnl, fnl),			\
99 	        __tg_generic_simple(z, fnl, fn , fn ),			\
100 	        __tg_generic_simple(z, fnl, fn , fn )),			\
101 	    __tg_generic_simple(y,					\
102 	        __tg_generic_simple(z, fnl, fnl, fnl),			\
103 	        __tg_generic_simple(z, fnl, fn , fn ),			\
104 	        __tg_generic_simple(z, fnl, fn , fnf)))(__VA_ARGS__)
105 #define	__tg_generic_full(x, cfnl, cfn, cfnf, fnl, fn, fnf)		\
106 	__generic(x, long double _Complex, cfnl,			\
107 	    __generic(x, double _Complex, cfn,				\
108 	        __generic(x, float _Complex, cfnf,			\
109 	            __generic(x, long double, fnl,			\
110 	                __generic(x, float, fnf, fn)))))
111 #define	__tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...)	\
112 	__tg_generic_full(x,						\
113 	    __tg_generic_full(y, cfnl, cfnl, cfnl, cfnl, cfnl, cfnl),	\
114 	    __tg_generic_full(y, cfnl, cfn , cfn , cfnl, cfn , cfn ),	\
115 	    __tg_generic_full(y, cfnl, cfn , cfnf, cfnl, cfn , cfnf),	\
116 	    __tg_generic_full(y, cfnl, cfnl, cfnl, fnl , fnl , fnl ),	\
117 	    __tg_generic_full(y, cfnl, cfn , cfn , fnl , fn  , fn  ),	\
118 	    __tg_generic_full(y, cfnl, cfn , cfnf, fnl , fn  , fnf ))	\
119 	    (__VA_ARGS__)
120 #else
121 #error "<tgmath.h> not implemented for this compiler"
122 #endif
123 
124 /* Macros to save lots of repetition below */
125 #define	__tg_simple(x, fn)						\
126 	__tg_impl_simple(x, x, x, fn##l, fn, fn##f, x)
127 #define	__tg_simple2(x, y, fn)						\
128 	__tg_impl_simple(x, x, y, fn##l, fn, fn##f, x, y)
129 #define	__tg_simple3(x, y, z, fn)					\
130 	__tg_impl_simple(x, y, z, fn##l, fn, fn##f, x, y, z)
131 #define	__tg_simplev(x, fn, ...)					\
132 	__tg_impl_simple(x, x, x, fn##l, fn, fn##f, __VA_ARGS__)
133 #define	__tg_full(x, fn)						\
134 	__tg_impl_full(x, x, c##fn##l, c##fn, c##fn##f, fn##l, fn, fn##f, x)
135 #define	__tg_full2(x, y, fn)						\
136 	__tg_impl_full(x, y, c##fn##l, c##fn, c##fn##f, fn##l, fn, fn##f, x, y)
137 
138 /* 7.22#4 -- These macros expand to real or complex functions, depending on
139  * the type of their arguments. */
140 #define	acos(x)		__tg_full(x, acos)
141 #define	asin(x)		__tg_full(x, asin)
142 #define	atan(x)		__tg_full(x, atan)
143 #define	acosh(x)	__tg_full(x, acosh)
144 #define	asinh(x)	__tg_full(x, asinh)
145 #define	atanh(x)	__tg_full(x, atanh)
146 #define	cos(x)		__tg_full(x, cos)
147 #define	sin(x)		__tg_full(x, sin)
148 #define	tan(x)		__tg_full(x, tan)
149 #define	cosh(x)		__tg_full(x, cosh)
150 #define	sinh(x)		__tg_full(x, sinh)
151 #define	tanh(x)		__tg_full(x, tanh)
152 #define	exp(x)		__tg_full(x, exp)
153 #define	log(x)		__tg_full(x, log)
154 #define	pow(x, y)	__tg_full2(x, y, pow)
155 #define	sqrt(x)		__tg_full(x, sqrt)
156 
157 /* "The corresponding type-generic macro for fabs and cabs is fabs." */
158 #define	fabs(x)		__tg_impl_full(x, x, cabsl, cabs, cabsf,	\
159     			    fabsl, fabs, fabsf, x)
160 
161 /* 7.22#5 -- These macros are only defined for arguments with real type. */
162 #define	atan2(x, y)	__tg_simple2(x, y, atan2)
163 #define	cbrt(x)		__tg_simple(x, cbrt)
164 #define	ceil(x)		__tg_simple(x, ceil)
165 #define	copysign(x, y)	__tg_simple2(x, y, copysign)
166 #define	erf(x)		__tg_simple(x, erf)
167 #define	erfc(x)		__tg_simple(x, erfc)
168 #define	exp2(x)		__tg_simple(x, exp2)
169 #define	expm1(x)	__tg_simple(x, expm1)
170 #define	fdim(x, y)	__tg_simple2(x, y, fdim)
171 #define	floor(x)	__tg_simple(x, floor)
172 #define	fma(x, y, z)	__tg_simple3(x, y, z, fma)
173 #define	fmax(x, y)	__tg_simple2(x, y, fmax)
174 #define	fmin(x, y)	__tg_simple2(x, y, fmin)
175 #define	fmod(x, y)	__tg_simple2(x, y, fmod)
176 #define	frexp(x, y)	__tg_simplev(x, frexp, x, y)
177 #define	hypot(x, y)	__tg_simple2(x, y, hypot)
178 #define	ilogb(x)	__tg_simple(x, ilogb)
179 #define	ldexp(x, y)	__tg_simplev(x, ldexp, x, y)
180 #define	lgamma(x)	__tg_simple(x, lgamma)
181 #define	llrint(x)	__tg_simple(x, llrint)
182 #define	llround(x)	__tg_simple(x, llround)
183 #define	log10(x)	__tg_simple(x, log10)
184 #define	log1p(x)	__tg_simple(x, log1p)
185 #define	log2(x)		__tg_simple(x, log2)
186 #define	logb(x)		__tg_simple(x, logb)
187 #define	lrint(x)	__tg_simple(x, lrint)
188 #define	lround(x)	__tg_simple(x, lround)
189 #define	nearbyint(x)	__tg_simple(x, nearbyint)
190 #define	nextafter(x, y)	__tg_simple2(x, y, nextafter)
191 #define	nexttoward(x, y) __tg_simplev(x, nexttoward, x, y)
192 #define	remainder(x, y)	__tg_simple2(x, y, remainder)
193 #define	remquo(x, y, z)	__tg_impl_simple(x, x, y, remquol, remquo,	\
194 			    remquof, x, y, z)
195 #define	rint(x)		__tg_simple(x, rint)
196 #define	round(x)	__tg_simple(x, round)
197 #define	scalbn(x, y)	__tg_simplev(x, scalbn, x, y)
198 #define	scalbln(x, y)	__tg_simplev(x, scalbln, x, y)
199 #define	tgamma(x)	__tg_simple(x, tgamma)
200 #define	trunc(x)	__tg_simple(x, trunc)
201 
202 /* 7.22#6 -- These macros always expand to complex functions. */
203 #define	carg(x)		__tg_simple(x, carg)
204 #define	cimag(x)	__tg_simple(x, cimag)
205 #define	conj(x)		__tg_simple(x, conj)
206 #define	cproj(x)	__tg_simple(x, cproj)
207 #define	creal(x)	__tg_simple(x, creal)
208 
209 #endif /* !_TGMATH_H_ */
210