tgmath.h revision 134736
1160814Ssimon/*-
2160814Ssimon * Copyright (c) 2004 Stefan Farfeleder.
3160814Ssimon * All rights reserved.
4160814Ssimon *
5160814Ssimon * Redistribution and use in source and binary forms, with or without
6160814Ssimon * modification, are permitted provided that the following conditions
7160814Ssimon * are met:
8160814Ssimon * 1. Redistributions of source code must retain the above copyright
9160814Ssimon *    notice, this list of conditions and the following disclaimer.
10280304Sjkim * 2. Redistributions in binary form must reproduce the above copyright
11160814Ssimon *    notice, this list of conditions and the following disclaimer in the
12160814Ssimon *    documentation and/or other materials provided with the distribution.
13160814Ssimon *
14160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15160814Ssimon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17160814Ssimon * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18160814Ssimon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19160814Ssimon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20160814Ssimon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22160814Ssimon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23160814Ssimon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24160814Ssimon * SUCH DAMAGE.
25160814Ssimon *
26160814Ssimon * $FreeBSD: head/include/tgmath.h 134736 2004-09-03 23:44:09Z stefanf $
27160814Ssimon */
28160814Ssimon
29160814Ssimon#ifndef _TGMATH_H_
30160814Ssimon#define	_TGMATH_H_
31160814Ssimon
32160814Ssimon#include <complex.h>
33160814Ssimon#include <math.h>
34160814Ssimon
35160814Ssimon/*
36160814Ssimon * This implementation of <tgmath.h> requires two implementation-dependent
37160814Ssimon * macros to be defined:
38160814Ssimon * __tg_impl_simple(x, y, z, fn, fnf, fnl, ...)
39160814Ssimon *	Invokes fnl() if the corresponding real type of x, y or z is long
40160814Ssimon *	double, fn() if it is double or any has an integer type, and fnf()
41160814Ssimon *	otherwise.
42160814Ssimon * __tg_impl_full(x, y, z, fn, fnf, fnl, cfn, cfnf, cfnl, ...)
43160814Ssimon *	Invokes [c]fnl() if the corresponding real type of x, y or z is long
44160814Ssimon *	double, [c]fn() if it is double or any has an integer type, and
45160814Ssimon *	[c]fnf() otherwise.  The function with the 'c' prefix is called if
46160814Ssimon *	any of x, y or z is a complex number.
47160814Ssimon * Both macros call the chosen function with all additional arguments passed
48160814Ssimon * to them, as given by __VA_ARGS__.
49160814Ssimon *
50160814Ssimon * Note that these macros cannot be implemented with C's ?: operator,
51160814Ssimon * because the return type of the whole expression would incorrectly be long
52160814Ssimon * double complex regardless of the argument types.
53160814Ssimon */
54160814Ssimon
55160814Ssimon#if __GNUC_PREREQ__(3, 1)
56280304Sjkim#define	__tg_type(e, t)	__builtin_types_compatible_p(__typeof__(e), t)
57280304Sjkim#define	__tg_type3(e1, e2, e3, t)					\
58160814Ssimon	(__tg_type(e1, t) || __tg_type(e2, t) || __tg_type(e3, t))
59160814Ssimon#define	__tg_type_corr(e1, e2, e3, t)					\
60160814Ssimon	(__tg_type3(e1, e2, e3, t) || __tg_type3(e1, e2, e3, t _Complex))
61160814Ssimon#define	__tg_integer(e1, e2, e3)					\
62160814Ssimon	(((__typeof__(e1))1.5 == 1) || ((__typeof__(e2))1.5 == 1) ||	\
63160814Ssimon	    ((__typeof__(e3))1.5 == 1))
64160814Ssimon#define	__tg_is_complex(e1, e2, e3)					\
65160814Ssimon	(__tg_type3(e1, e2, e3, float _Complex) ||			\
66160814Ssimon	    __tg_type3(e1, e2, e3, double _Complex) ||			\
67160814Ssimon	    __tg_type3(e1, e2, e3, long double _Complex)) ||		\
68160814Ssimon	    __tg_type3(e1, e2, e3, __typeof__(_Complex_I))
69280304Sjkim
70280304Sjkim#define	__tg_impl_simple(x, y, z, fn, fnf, fnl, ...)			\
71160814Ssimon	__builtin_choose_expr(__tg_type_corr(x, y, z, long double),	\
72280304Sjkim	    fnl(__VA_ARGS__), __builtin_choose_expr(			\
73280304Sjkim		__tg_type_corr(x, y, z, double) || __tg_integer(x, y, z),\
74280304Sjkim		fn(__VA_ARGS__), fnf(__VA_ARGS__)))
75280304Sjkim
76280304Sjkim#define	__tg_impl_full(x, y, z, fn, fnf, fnl, cfn, cfnf, cfnl, ...)	\
77160814Ssimon	__builtin_choose_expr(__tg_is_complex(x, y, z),			\
78280304Sjkim	    __tg_impl_simple(x, y, z, cfn, cfnf, cfnl, __VA_ARGS__),	\
79280304Sjkim	    __tg_impl_simple(x, y, z, fn, fnf, fnl, __VA_ARGS__))
80280304Sjkim
81280304Sjkim#else	/* __GNUC__ */
82280304Sjkim#error "<tgmath.h> not implemented for this compiler"
83280304Sjkim#endif	/* !__GNUC__ */
84280304Sjkim
85160814Ssimon/* Macros to save lots of repetition below */
86160814Ssimon#define	__tg_simple(x, fn)						\
87160814Ssimon	__tg_impl_simple(x, x, x, fn, fn##f, fn##l, x)
88160814Ssimon#define	__tg_simple2(x, y, fn)						\
89280304Sjkim	__tg_impl_simple(x, x, y, fn, fn##f, fn##l, x, y)
90280304Sjkim#define	__tg_simplev(x, fn, ...)					\
91280304Sjkim	__tg_impl_simple(x, x, x, fn, fn##f, fn##l, __VA_ARGS__)
92280304Sjkim#define	__tg_full(x, fn)						\
93160814Ssimon	__tg_impl_full(x, x, x, fn, fn##f, fn##l, c##fn, c##fn##f, c##fn##l, x)
94160814Ssimon
95280304Sjkim/* 7.22#4 -- These macros expand to real or complex functions, depending on
96280304Sjkim * the type of their arguments. */
97160814Ssimon#define	acos(x)		__tg_full(x, acos)
98160814Ssimon#define	asin(x)		__tg_full(x, asin)
99280304Sjkim#define	atan(x)		__tg_full(x, atan)
100280304Sjkim#define	acosh(x)	__tg_full(x, acosh)
101280304Sjkim#define	asinh(x)	__tg_full(x, asinh)
102160814Ssimon#define	atanh(x)	__tg_full(x, atanh)
103280304Sjkim#define	cos(x)		__tg_full(x, cos)
104280304Sjkim#define	sin(x)		__tg_full(x, sin)
105160814Ssimon#define	tan(x)		__tg_full(x, tan)
106280304Sjkim#define	cosh(x)		__tg_full(x, cosh)
107280304Sjkim#define	sinh(x)		__tg_full(x, sinh)
108160814Ssimon#define	tanh(x)		__tg_full(x, tanh)
109160814Ssimon#define	exp(x)		__tg_full(x, exp)
110160814Ssimon#define	log(x)		__tg_full(x, log)
111280304Sjkim#define	pow(x, y)	__tg_impl_full(x, x, y, pow, powf, powl,	\
112280304Sjkim			    cpow, cpowf, cpowl, x, y)
113160814Ssimon#define	sqrt(x)		__tg_full(x, sqrt)
114280304Sjkim
115280304Sjkim/* "The corresponding type-generic macro for fabs and cabs is fabs." */
116160814Ssimon#define	fabs(x)		__tg_impl_full(x, x, x, fabs, fabsf, fabsl,	\
117160814Ssimon    			    cabs, cabsf, cabsl, x)
118280304Sjkim
119280304Sjkim/* 7.22#5 -- These macros are only defined for arguments with real type. */
120160814Ssimon#define	atan2(x, y)	__tg_simple2(x, y, atan2)
121280304Sjkim#define	cbrt(x)		__tg_simple(x, cbrt)
122280304Sjkim#define	ceil(x)		__tg_simple(x, ceil)
123160814Ssimon#define	copysign(x, y)	__tg_simple2(x, y, copysign)
124160814Ssimon#define	erf(x)		__tg_simple(x, erf)
125160814Ssimon#define	erfc(x)		__tg_simple(x, erfc)
126280304Sjkim#define	exp2(x)		__tg_simple(x, exp2)
127160814Ssimon#define	expm1(x)	__tg_simple(x, expm1)
128280304Sjkim#define	fdim(x, y)	__tg_simple2(x, y, fdim)
129280304Sjkim#define	floor(x)	__tg_simple(x, floor)
130280304Sjkim#define	fma(x, y, z)	__tg_impl_simple(x, y, z, fma, fmaf, fmal, x, y, z)
131160814Ssimon#define	fmax(x, y)	__tg_simple2(x, y, fmax)
132160814Ssimon#define	fmin(x, y)	__tg_simple2(x, y, fmin)
133280304Sjkim#define	fmod(x, y)	__tg_simple2(x, y, fmod)
134280304Sjkim#define	frexp(x, y)	__tg_simplev(x, frexp, x, y)
135280304Sjkim#define	hypot(x, y)	__tg_simple2(x, y, hypot)
136280304Sjkim#define	ilogb(x)	__tg_simple(x, ilogb)
137280304Sjkim#define	ldexp(x, y)	__tg_simplev(x, ldexp, x, y)
138#define	lgamma(x)	__tg_simple(x, lgamma)
139#define	llrint(x)	__tg_simple(x, llrint)
140#define	llround(x)	__tg_simple(x, llround)
141#define	log10(x)	__tg_simple(x, log10)
142#define	log1p(x)	__tg_simple(x, log1p)
143#define	log2(x)		__tg_simple(x, log2)
144#define	logb(x)		__tg_simple(x, logb)
145#define	lrint(x)	__tg_simple(x, lrint)
146#define	lround(x)	__tg_simple(x, lround)
147#define	nearbyint(x)	__tg_simple(x, nearbyint)
148#define	nextafter(x, y)	__tg_simple2(x, y, nextafter)
149#define	nexttoward(x, y) __tg_simplev(x, nexttoward, x, y)
150#define	remainder(x, y)	__tg_simple2(x, y, remainder)
151#define	remquo(x, y, z)	__tg_impl_simple(x, x, y, remquo, remquof,	\
152			    remquol, x, y, z)
153#define	rint(x)		__tg_simple(x, rint)
154#define	round(x)	__tg_simple(x, round)
155#define	scalbn(x, y)	__tg_simplev(x, scalbn, x, y)
156#define	scalbln(x, y)	__tg_simplev(x, scalbln, x, y)
157#define	tgamma(x)	__tg_simple(x, tgamma)
158#define	trunc(x)	__tg_simple(x, trunc)
159
160/* 7.22#6 -- These macros always expand to complex functions. */
161#define	carg(x)		__tg_simple(x, carg)
162#define	cimag(x)	__tg_simple(x, cimag)
163#define	conj(x)		__tg_simple(x, conj)
164#define	cproj(x)	__tg_simple(x, cproj)
165#define	creal(x)	__tg_simple(x, creal)
166
167#endif /* !_TGMATH_H_ */
168