tgmath.h revision 229575
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: head/include/tgmath.h 229575 2012-01-05 10:46:22Z ed $
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 _Complex_I distinction should not be needed, but due to a bug in
58 * GCC 4.2, _Complex_I is not of type float _Complex.
59 */
60
61#ifndef __generic
62#error "<tgmath.h> not implemented for this compiler"
63#endif
64
65#define	__tg_generic_simple(x, fnl, fn, fnf)				\
66	__generic(x, long double _Complex, fnl,				\
67	    __generic(x, double _Complex, fn,				\
68	        __generic(x, float _Complex, fnf,			\
69	            __generic(x, __typeof(_Complex_I), fnf,		\
70	                __generic(x, long double, fnl,			\
71	                    __generic(x, float, fnf, fn))))))
72#define	__tg_impl_simple(x, y, z, fnl, fn, fnf, ...)			\
73	__tg_generic_simple(x,						\
74	    __tg_generic_simple(y,					\
75	        __tg_generic_simple(z, fnl, fnl, fnl),			\
76	        __tg_generic_simple(z, fnl, fnl, fnl),			\
77	        __tg_generic_simple(z, fnl, fnl, fnl)),			\
78	    __tg_generic_simple(y,					\
79	        __tg_generic_simple(z, fnl, fnl, fnl),			\
80	        __tg_generic_simple(z, fnl, fn , fn ),			\
81	        __tg_generic_simple(z, fnl, fn , fn )),			\
82	    __tg_generic_simple(y,					\
83	        __tg_generic_simple(z, fnl, fnl, fnl),			\
84	        __tg_generic_simple(z, fnl, fn , fn ),			\
85	        __tg_generic_simple(z, fnl, fn , fnf)))(__VA_ARGS__)
86#define	__tg_generic_full(x, cfnl, cfn, cfnf, fnl, fn, fnf)		\
87	__generic(x, long double _Complex, cfnl,			\
88	    __generic(x, double _Complex, cfn,				\
89	        __generic(x, float _Complex, cfnf,			\
90	            __generic(x, __typeof(_Complex_I), cfnf,		\
91	                __generic(x, long double, fnl,			\
92	                    __generic(x, float, fnf, fn))))))
93#define	__tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...)	\
94	__tg_generic_full(x,						\
95	    __tg_generic_full(y, cfnl, cfnl, cfnl, cfnl, cfnl, cfnl),	\
96	    __tg_generic_full(y, cfnl, cfn , cfn , cfnl, cfn , cfn ),	\
97	    __tg_generic_full(y, cfnl, cfn , cfnf, cfnl, cfn , cfnf),	\
98	    __tg_generic_full(y, cfnl, cfnl, cfnl, fnl , fnl , fnl ),	\
99	    __tg_generic_full(y, cfnl, cfn , cfn , fnl , fn  , fn  ),	\
100	    __tg_generic_full(y, cfnl, cfn , cfnf, fnl , fn  , fnf ))	\
101	    (__VA_ARGS__)
102
103/* Macros to save lots of repetition below */
104#define	__tg_simple(x, fn)						\
105	__tg_impl_simple(x, x, x, fn##l, fn, fn##f, x)
106#define	__tg_simple2(x, y, fn)						\
107	__tg_impl_simple(x, x, y, fn##l, fn, fn##f, x, y)
108#define	__tg_simple3(x, y, z, fn)					\
109	__tg_impl_simple(x, y, z, fn##l, fn, fn##f, x, y, z)
110#define	__tg_simplev(x, fn, ...)					\
111	__tg_impl_simple(x, x, x, fn##l, fn, fn##f, __VA_ARGS__)
112#define	__tg_full(x, fn)						\
113	__tg_impl_full(x, x, c##fn##l, c##fn, c##fn##f, fn##l, fn, fn##f, x)
114#define	__tg_full2(x, y, fn)						\
115	__tg_impl_full(x, y, c##fn##l, c##fn, c##fn##f, fn##l, fn, fn##f, x, y)
116
117/* 7.22#4 -- These macros expand to real or complex functions, depending on
118 * the type of their arguments. */
119#define	acos(x)		__tg_full(x, acos)
120#define	asin(x)		__tg_full(x, asin)
121#define	atan(x)		__tg_full(x, atan)
122#define	acosh(x)	__tg_full(x, acosh)
123#define	asinh(x)	__tg_full(x, asinh)
124#define	atanh(x)	__tg_full(x, atanh)
125#define	cos(x)		__tg_full(x, cos)
126#define	sin(x)		__tg_full(x, sin)
127#define	tan(x)		__tg_full(x, tan)
128#define	cosh(x)		__tg_full(x, cosh)
129#define	sinh(x)		__tg_full(x, sinh)
130#define	tanh(x)		__tg_full(x, tanh)
131#define	exp(x)		__tg_full(x, exp)
132#define	log(x)		__tg_full(x, log)
133#define	pow(x, y)	__tg_full2(x, y, pow)
134#define	sqrt(x)		__tg_full(x, sqrt)
135
136/* "The corresponding type-generic macro for fabs and cabs is fabs." */
137#define	fabs(x)		__tg_impl_full(x, x, cabsl, cabs, cabsf,	\
138    			    fabsl, fabs, fabsf, x)
139
140/* 7.22#5 -- These macros are only defined for arguments with real type. */
141#define	atan2(x, y)	__tg_simple2(x, y, atan2)
142#define	cbrt(x)		__tg_simple(x, cbrt)
143#define	ceil(x)		__tg_simple(x, ceil)
144#define	copysign(x, y)	__tg_simple2(x, y, copysign)
145#define	erf(x)		__tg_simple(x, erf)
146#define	erfc(x)		__tg_simple(x, erfc)
147#define	exp2(x)		__tg_simple(x, exp2)
148#define	expm1(x)	__tg_simple(x, expm1)
149#define	fdim(x, y)	__tg_simple2(x, y, fdim)
150#define	floor(x)	__tg_simple(x, floor)
151#define	fma(x, y, z)	__tg_simple3(x, y, z, fma)
152#define	fmax(x, y)	__tg_simple2(x, y, fmax)
153#define	fmin(x, y)	__tg_simple2(x, y, fmin)
154#define	fmod(x, y)	__tg_simple2(x, y, fmod)
155#define	frexp(x, y)	__tg_simplev(x, frexp, x, y)
156#define	hypot(x, y)	__tg_simple2(x, y, hypot)
157#define	ilogb(x)	__tg_simple(x, ilogb)
158#define	ldexp(x, y)	__tg_simplev(x, ldexp, x, y)
159#define	lgamma(x)	__tg_simple(x, lgamma)
160#define	llrint(x)	__tg_simple(x, llrint)
161#define	llround(x)	__tg_simple(x, llround)
162#define	log10(x)	__tg_simple(x, log10)
163#define	log1p(x)	__tg_simple(x, log1p)
164#define	log2(x)		__tg_simple(x, log2)
165#define	logb(x)		__tg_simple(x, logb)
166#define	lrint(x)	__tg_simple(x, lrint)
167#define	lround(x)	__tg_simple(x, lround)
168#define	nearbyint(x)	__tg_simple(x, nearbyint)
169#define	nextafter(x, y)	__tg_simple2(x, y, nextafter)
170#define	nexttoward(x, y) __tg_simplev(x, nexttoward, x, y)
171#define	remainder(x, y)	__tg_simple2(x, y, remainder)
172#define	remquo(x, y, z)	__tg_impl_simple(x, x, y, remquol, remquo,	\
173			    remquof, x, y, z)
174#define	rint(x)		__tg_simple(x, rint)
175#define	round(x)	__tg_simple(x, round)
176#define	scalbn(x, y)	__tg_simplev(x, scalbn, x, y)
177#define	scalbln(x, y)	__tg_simplev(x, scalbln, x, y)
178#define	tgamma(x)	__tg_simple(x, tgamma)
179#define	trunc(x)	__tg_simple(x, trunc)
180
181/* 7.22#6 -- These macros always expand to complex functions. */
182#define	carg(x)		__tg_simple(x, carg)
183#define	cimag(x)	__tg_simple(x, cimag)
184#define	conj(x)		__tg_simple(x, conj)
185#define	cproj(x)	__tg_simple(x, cproj)
186#define	creal(x)	__tg_simple(x, creal)
187
188#endif /* !_TGMATH_H_ */
189