1/* APPLE LOCAL file mainline 2007-06-12 2872232 */
2/* Copyright (C) 2004
3   Free Software Foundation, Inc.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING.  If not, write to
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA.  */
21
22/* As a special exception, if you include this header file into source
23   files compiled by GCC, this header file does not by itself cause
24   the resulting executable to be covered by the GNU General Public
25   License.  This exception does not however invalidate any other
26   reasons why the executable file might be covered by the GNU General
27   Public License.  */
28
29/*
30 * ISO C Standard:  7.22  Type-generic math <tgmath.h>
31 */
32
33#ifndef _TGMATH_H
34#define _TGMATH_H
35
36#include <math.h>
37
38#ifndef __cplusplus
39#include <complex.h>
40
41/* Naming convention: generic macros are defining using
42   __TGMATH_CPLX*, __TGMATH_REAL*, and __TGMATH_CPLX_ONLY.  _CPLX
43   means the generic argument(s) may be real or complex, _REAL means
44   real only, _CPLX means complex only.  If there is no suffix, we are
45   defining a function of one generic argument.  If the suffix is _n
46   it is a function of n generic arguments.  If the suffix is _m_n it
47   is a function of n arguments, the first m of which are generic.  We
48   only define these macros for values of n and/or m that are needed. */
49
50/* The general rules for generic macros are given in 7.22 paragraphs 1 and 2.
51   If any generic parameter is complex, we use a complex version.  Otherwise
52   we use a real version.  If the real part of any generic parameter is long
53   double, we use the long double version.  Otherwise if the real part of any
54   generic paramter is double or of integer type, we use the double version.
55   Otherwise we use the float version. */
56
57#define __tg_cplx(expr) \
58  __builtin_classify_type(expr) == 9
59
60#define __tg_ldbl(expr) \
61  __builtin_types_compatible_p(__typeof__(expr), long double)
62
63#define __tg_dbl(expr)                                       \
64  (__builtin_types_compatible_p(__typeof__(expr), double)    \
65   || __builtin_classify_type(expr) == 1)
66
67#define __tg_choose(x,f,d,l)                                  \
68  __builtin_choose_expr(__tg_ldbl(x), l,                      \
69                        __builtin_choose_expr(__tg_dbl(x), d, \
70                                              f))
71
72#define __tg_choose_2(x,y,f,d,l)                                             \
73  __builtin_choose_expr(__tg_ldbl(x) || __tg_ldbl(y), l,                     \
74                        __builtin_choose_expr(__tg_dbl(x) || __tg_dbl(y), d, \
75                                              f))
76
77#define __tg_choose_3(x,y,z,f,d,l)                                        \
78   __builtin_choose_expr(__tg_ldbl(x) || __tg_ldbl(y) || __tg_ldbl(z), l, \
79                        __builtin_choose_expr(__tg_dbl(x) || __tg_dbl(y)  \
80                                              || __tg_dbl(z), d,          \
81                                              f))
82
83#define __TGMATH_CPLX(z,R,C)                                                  \
84  __builtin_choose_expr (__tg_cplx(z),                                        \
85                         __tg_choose (__real__(z), C##f(z), (C)(z), C##l(z)), \
86			 /* APPLE LOCAL shorten-64-to-32 4604239 */	      \
87                         __tg_choose (z, R##f((float)(z)), (R)(z), R##l(z)))
88
89#define __TGMATH_CPLX_2(z1,z2,R,C)                                             \
90  __builtin_choose_expr (__tg_cplx(z1) || __tg_cplx(z2),                       \
91                         __tg_choose_2 (__real__(z1), __real__(z2),            \
92                                        C##f(z1,z2), (C)(z1,z2), C##l(z1,z2)), \
93                         __tg_choose_2 (z1, z2,                                \
94			 /* APPLE LOCAL shorten-64-to-32 5909621 */	       \
95                                        R##f((float)(z1),(float)(z2)), (R)(z1,z2), R##l(z1,z2)))
96
97#define __TGMATH_REAL(x,R) \
98  /* APPLE LOCAL shorten-64-to-32 5909621 */	      \
99  __tg_choose (x, R##f((float)(x)), (R)(x), R##l(x))
100#define __TGMATH_REAL_2(x,y,R) \
101  /* APPLE LOCAL shorten-64-to-32 4604239 */	      \
102  __tg_choose_2 (x, y, R##f((float)(x),(float)(y)), (R)(x,y), R##l(x,y))
103#define __TGMATH_REAL_3(x,y,z,R) \
104  /* APPLE LOCAL shorten-64-to-32 5909621 */	       \
105  __tg_choose_3 (x, y, z, R##f((float)(x),(float)(y),(float)(z)), (R)(x,y,z), R##l(x,y,z))
106#define __TGMATH_REAL_1_2(x,y,R) \
107  /* APPLE LOCAL shorten-64-to-32 5909621 */	       \
108  __tg_choose (x, R##f((float)(x),y), (R)(x,y), R##l(x,y))
109#define __TGMATH_REAL_2_3(x,y,z,R) \
110  /* APPLE LOCAL shorten-64-to-32 5909621 */	       \
111  __tg_choose_2 (x, y, R##f((float)(x),(float)(y),z), (R)(x,y,z), R##l(x,y,z))
112#define __TGMATH_CPLX_ONLY(z,C) \
113  __tg_choose (__real__(z), C##f(z), (C)(z), C##l(z))
114
115/* Functions defined in both <math.h> and <complex.h> (7.22p4) */
116#define acos(z)          __TGMATH_CPLX(z, acos, cacos)
117#define asin(z)          __TGMATH_CPLX(z, asin, casin)
118#define atan(z)          __TGMATH_CPLX(z, atan, catan)
119#define acosh(z)         __TGMATH_CPLX(z, acosh, cacosh)
120#define asinh(z)         __TGMATH_CPLX(z, asinh, casinh)
121#define atanh(z)         __TGMATH_CPLX(z, atanh, catanh)
122#define cos(z)           __TGMATH_CPLX(z, cos, ccos)
123#define sin(z)           __TGMATH_CPLX(z, sin, csin)
124#define tan(z)           __TGMATH_CPLX(z, tan, ctan)
125#define cosh(z)          __TGMATH_CPLX(z, cosh, ccosh)
126#define sinh(z)          __TGMATH_CPLX(z, sinh, csinh)
127#define tanh(z)          __TGMATH_CPLX(z, tanh, ctanh)
128#define exp(z)           __TGMATH_CPLX(z, exp, cexp)
129#define log(z)           __TGMATH_CPLX(z, log, clog)
130#define pow(z1,z2)       __TGMATH_CPLX_2(z1, z2, pow, cpow)
131#define sqrt(z)          __TGMATH_CPLX(z, sqrt, csqrt)
132#define fabs(z)          __TGMATH_CPLX(z, fabs, cabs)
133
134/* Functions defined in <math.h> only (7.22p5) */
135#define atan2(x,y)       __TGMATH_REAL_2(x, y, atan2)
136#define cbrt(x)          __TGMATH_REAL(x, cbrt)
137#define ceil(x)          __TGMATH_REAL(x, ceil)
138#define copysign(x,y)    __TGMATH_REAL_2(x, y, copysign)
139#define erf(x)           __TGMATH_REAL(x, erf)
140#define erfc(x)          __TGMATH_REAL(x, erfc)
141#define exp2(x)          __TGMATH_REAL(x, exp2)
142#define expm1(x)         __TGMATH_REAL(x, expm1)
143#define fdim(x,y)        __TGMATH_REAL_2(x, y, fdim)
144#define floor(x)         __TGMATH_REAL(x, floor)
145#define fma(x,y,z)       __TGMATH_REAL_3(x, y, z, fma)
146#define fmax(x,y)        __TGMATH_REAL_2(x, y, fmax)
147#define fmin(x,y)        __TGMATH_REAL_2(x, y, fmin)
148#define fmod(x,y)        __TGMATH_REAL_2(x, y, fmod)
149#define frexp(x,y)       __TGMATH_REAL_1_2(x, y, frexp)
150#define hypot(x,y)       __TGMATH_REAL_2(x, y, hypot)
151#define ilogb(x)         __TGMATH_REAL(x, ilogb)
152#define ldexp(x,y)       __TGMATH_REAL_1_2(x, y, ldexp)
153#define lgamma(x)        __TGMATH_REAL(x, lgamma)
154#define llrint(x)        __TGMATH_REAL(x, llrint)
155#define llround(x)       __TGMATH_REAL(x, llround)
156#define log10(x)         __TGMATH_REAL(x, log10)
157#define log1p(x)         __TGMATH_REAL(x, log1p)
158#define log2(x)          __TGMATH_REAL(x, log2)
159#define logb(x)          __TGMATH_REAL(x, logb)
160#define lrint(x)         __TGMATH_REAL(x, lrint)
161#define lround(x)        __TGMATH_REAL(x, lround)
162#define nearbyint(x)     __TGMATH_REAL(x, nearbyint)
163#define nextafter(x,y)   __TGMATH_REAL_2(x, y, nextafter)
164#define nexttoward(x,y)  __TGMATH_REAL_1_2(x, y, nexttoward)
165#define remainder(x,y)   __TGMATH_REAL_2(x, y, remainder)
166#define remquo(x,y,z)    __TGMATH_REAL_2_3(x, y, z, remquo)
167#define rint(x)          __TGMATH_REAL(x, rint)
168#define round(x)         __TGMATH_REAL(x, round)
169#define scalbn(x,y)      __TGMATH_REAL_1_2(x, y, scalbn)
170#define scalbln(x,y)     __TGMATH_REAL_1_2(x, y, scalbln)
171#define tgamma(x)        __TGMATH_REAL(x, tgamma)
172#define trunc(x)         __TGMATH_REAL(x, trunc)
173
174/* Functions defined in <complex.h> only (7.22p6) */
175#define carg(z)          __TGMATH_CPLX_ONLY(z, carg)
176#define cimag(z)         __TGMATH_CPLX_ONLY(z, cimag)
177#define conj(z)          __TGMATH_CPLX_ONLY(z, conj)
178#define cproj(z)         __TGMATH_CPLX_ONLY(z, cproj)
179#define creal(z)         __TGMATH_CPLX_ONLY(z, creal)
180
181#endif /* __cplusplus */
182#endif /* _TGMATH_H */
183