]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - include/tgmath.h
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / include / tgmath.h
1 /*-
2  * Copyright (c) 2004 Stefan Farfeleder.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #ifndef _TGMATH_H_
30 #define _TGMATH_H_
31
32 #include <complex.h>
33 #include <math.h>
34
35 /*
36  * This implementation of <tgmath.h> requires two implementation-dependent
37  * macros to be defined:
38  * __tg_impl_simple(x, y, z, fn, fnf, fnl, ...)
39  *      Invokes fnl() if the corresponding real type of x, y or z is long
40  *      double, fn() if it is double or any has an integer type, and fnf()
41  *      otherwise.
42  * __tg_impl_full(x, y, z, fn, fnf, fnl, cfn, cfnf, cfnl, ...)
43  *      Invokes [c]fnl() if the corresponding real type of x, y or z is long
44  *      double, [c]fn() if it is double or any has an integer type, and
45  *      [c]fnf() otherwise.  The function with the 'c' prefix is called if
46  *      any of x, y or z is a complex number.
47  * Both macros call the chosen function with all additional arguments passed
48  * to them, as given by __VA_ARGS__.
49  *
50  * Note that these macros cannot be implemented with C's ?: operator,
51  * because the return type of the whole expression would incorrectly be long
52  * double complex regardless of the argument types.
53  */
54
55 #if __GNUC_PREREQ__(3, 1)
56 #define __tg_type(e, t) __builtin_types_compatible_p(__typeof__(e), t)
57 #define __tg_type3(e1, e2, e3, t)                                       \
58         (__tg_type(e1, t) || __tg_type(e2, t) || __tg_type(e3, t))
59 #define __tg_type_corr(e1, e2, e3, t)                                   \
60         (__tg_type3(e1, e2, e3, t) || __tg_type3(e1, e2, e3, t _Complex))
61 #define __tg_integer(e1, e2, e3)                                        \
62         (((__typeof__(e1))1.5 == 1) || ((__typeof__(e2))1.5 == 1) ||    \
63             ((__typeof__(e3))1.5 == 1))
64 #define __tg_is_complex(e1, e2, e3)                                     \
65         (__tg_type3(e1, e2, e3, float _Complex) ||                      \
66             __tg_type3(e1, e2, e3, double _Complex) ||                  \
67             __tg_type3(e1, e2, e3, long double _Complex) ||             \
68             __tg_type3(e1, e2, e3, __typeof__(_Complex_I)))
69
70 #define __tg_impl_simple(x, y, z, fn, fnf, fnl, ...)                    \
71         __builtin_choose_expr(__tg_type_corr(x, y, z, long double),     \
72             fnl(__VA_ARGS__), __builtin_choose_expr(                    \
73                 __tg_type_corr(x, y, z, double) || __tg_integer(x, y, z),\
74                 fn(__VA_ARGS__), fnf(__VA_ARGS__)))
75
76 #define __tg_impl_full(x, y, z, fn, fnf, fnl, cfn, cfnf, cfnl, ...)     \
77         __builtin_choose_expr(__tg_is_complex(x, y, z),                 \
78             __tg_impl_simple(x, y, z, cfn, cfnf, cfnl, __VA_ARGS__),    \
79             __tg_impl_simple(x, y, z, fn, fnf, fnl, __VA_ARGS__))
80
81 #else   /* __GNUC__ */
82 #error "<tgmath.h> not implemented for this compiler"
83 #endif  /* !__GNUC__ */
84
85 /* Macros to save lots of repetition below */
86 #define __tg_simple(x, fn)                                              \
87         __tg_impl_simple(x, x, x, fn, fn##f, fn##l, x)
88 #define __tg_simple2(x, y, fn)                                          \
89         __tg_impl_simple(x, x, y, fn, fn##f, fn##l, x, y)
90 #define __tg_simplev(x, fn, ...)                                        \
91         __tg_impl_simple(x, x, x, fn, fn##f, fn##l, __VA_ARGS__)
92 #define __tg_full(x, fn)                                                \
93         __tg_impl_full(x, x, x, fn, fn##f, fn##l, c##fn, c##fn##f, c##fn##l, x)
94
95 /* 7.22#4 -- These macros expand to real or complex functions, depending on
96  * the type of their arguments. */
97 #define acos(x)         __tg_full(x, acos)
98 #define asin(x)         __tg_full(x, asin)
99 #define atan(x)         __tg_full(x, atan)
100 #define acosh(x)        __tg_full(x, acosh)
101 #define asinh(x)        __tg_full(x, asinh)
102 #define atanh(x)        __tg_full(x, atanh)
103 #define cos(x)          __tg_full(x, cos)
104 #define sin(x)          __tg_full(x, sin)
105 #define tan(x)          __tg_full(x, tan)
106 #define cosh(x)         __tg_full(x, cosh)
107 #define sinh(x)         __tg_full(x, sinh)
108 #define tanh(x)         __tg_full(x, tanh)
109 #define exp(x)          __tg_full(x, exp)
110 #define log(x)          __tg_full(x, log)
111 #define pow(x, y)       __tg_impl_full(x, x, y, pow, powf, powl,        \
112                             cpow, cpowf, cpowl, x, y)
113 #define sqrt(x)         __tg_full(x, sqrt)
114
115 /* "The corresponding type-generic macro for fabs and cabs is fabs." */
116 #define fabs(x)         __tg_impl_full(x, x, x, fabs, fabsf, fabsl,     \
117                             cabs, cabsf, cabsl, x)
118
119 /* 7.22#5 -- These macros are only defined for arguments with real type. */
120 #define atan2(x, y)     __tg_simple2(x, y, atan2)
121 #define cbrt(x)         __tg_simple(x, cbrt)
122 #define ceil(x)         __tg_simple(x, ceil)
123 #define copysign(x, y)  __tg_simple2(x, y, copysign)
124 #define erf(x)          __tg_simple(x, erf)
125 #define erfc(x)         __tg_simple(x, erfc)
126 #define exp2(x)         __tg_simple(x, exp2)
127 #define expm1(x)        __tg_simple(x, expm1)
128 #define fdim(x, y)      __tg_simple2(x, y, fdim)
129 #define floor(x)        __tg_simple(x, floor)
130 #define fma(x, y, z)    __tg_impl_simple(x, y, z, fma, fmaf, fmal, x, y, z)
131 #define fmax(x, y)      __tg_simple2(x, y, fmax)
132 #define fmin(x, y)      __tg_simple2(x, y, fmin)
133 #define fmod(x, y)      __tg_simple2(x, y, fmod)
134 #define frexp(x, y)     __tg_simplev(x, frexp, x, y)
135 #define hypot(x, y)     __tg_simple2(x, y, hypot)
136 #define ilogb(x)        __tg_simple(x, ilogb)
137 #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_ */