aboutsummaryrefslogtreecommitdiff
path: root/gcc/ginclude
diff options
context:
space:
mode:
authorJoseph Myers <joseph@codesourcery.com>2017-11-15 01:53:45 +0000
committerJoseph Myers <jsm28@gcc.gnu.org>2017-11-15 01:53:45 +0000
commit3ca0dc603213b2ad5ffbcf1a443a102bef96053a (patch)
tree3c0c2c2688d3a9286523a577c507e343650af5ce /gcc/ginclude
parentf60bea11ada835a0b007469816bd1130a2f9a77a (diff)
downloadgcc-3ca0dc603213b2ad5ffbcf1a443a102bef96053a.zip
gcc-3ca0dc603213b2ad5ffbcf1a443a102bef96053a.tar.gz
gcc-3ca0dc603213b2ad5ffbcf1a443a102bef96053a.tar.bz2
Add __builtin_tgmath for better tgmath.h implementation (bug 81156).
Various implementations of C99/C11 <tgmath.h> have the property that their macro expansions contain many copies of the macro arguments, so resulting in exponential blowup of the size of macro expansions where a call to such a macro contains other such calls in the macro arguments. This patch adds a (C-only) language feature __builtin_tgmath designed to avoid this problem by implementing the <tgmath.h> function selection rules directly in the compiler. The effect is that type-generic macros can be defined simply as #define pow(a, b) __builtin_tgmath (powf, pow, powl, \ cpowf, cpow, cpowl, a, b) as in the example added to the manual, with each macro argument expanded exactly once. The details of __builtin_tgmath are as described in the manual. This is C-only since C++ uses function overloading and just defines <ctgmath> to include <ccomplex> and <cmath>. __builtin_tgmath handles C99/C11 type-generic macros, and _FloatN, _FloatNx and decimal floating-point types (following the proposed resolution to the floating-point TS DR#9 that makes the rules for finding a common type from arguments to a type-generic macro follow the usual arithmetic conversions after adjustment of integer arguments to _Decimal64 or double - or to _Complex double in the case of GNU complex integer arguments). Type-generic macros for functions from TS 18661 that round their results to a narrower type are handled, but there are still some unresolved questions regarding such macros so further changes in that regard may be needed in future. The current implementation follows an older version of the DR#13 resolution (allowing a function for a wide-enough argument type to be selected if no exactly-matching function is available), but with appropriate calls to __builtin_tgmath is still fully compatible with the latest version of the resolution (not yet in the DR log), and allowing such not-exactly-matching argument types to be chosen in that case avoids needing another special case to treat integers as _Float64 instead of double in certain cases. Regarding other possible language/library features, not currently implemented in GCC: * Imaginary types could be naturally supported by allowing cases where the type-generic type is an imaginary type T and arguments or return types may be T (as at present), or the corresponding real type to T (as at present), or (new) the corresponding real type if T is real or imaginary but T if T is complex. (tgmath.h would need a series of functions such as static inline _Imaginary double __sin_imag (_Imaginary double __x) { return _Imaginary_I * sinh (__imag__ __x); } to be used in __builtin_tgmath calls.) * __builtin_tgmath would use the constant rounding direction in the presence of support for the FENV_ROUND / FENV_DEC_ROUND pragmas. Support for those would also require a new __builtin_<something> to cause a non-type-generic call to use the constant rounding direction (it seems cleaner to add a new __builtin_<something> when required than to make __builtin_tgmath handle a non-type-generic case with only one function argument). * TS 18661-5 __STDC_TGMATH_OPERATOR_EVALUATION__ would require new __builtin_<something> that evaluates with excess range and precision like arithmetic operators do. * The proposed C bindings for IEEE 754-2018 augmented arithmetic operations involve struct return types. As currently implemented __builtin_tgmath does not handle those, but support could be added. There are many error cases that the implementation diagnoses. I've tried to ensure reasonable error messages for erroneous uses of __builtin_tgmath, but the errors for erroneous uses of the resulting type-generic macros (that is, when the non-function arguments have inappropriate types) are more important as they are more likely to be seen by users. GCC's own tgmath.h, as used for some targets, is updated in this patch. I've tested those changes minimally, via adjusting gcc.dg/c99-tgmath-* locally to use that tgmath.h version. I've also run the glibc testsuite (which has much more thorough tests of correctness of tgmath.h function selection) with a glibc patch to use __builtin_tgmath in glibc's tgmath.h. Bootstrapped with no regressions on x86_64-pc-linux-gnu. PR c/81156 gcc: * doc/extend.texi (Other Builtins): Document __builtin_tgmath. * ginclude/tgmath.h (__tg_cplx, __tg_ldbl, __tg_dbl, __tg_choose) (__tg_choose_2, __tg_choose_3, __TGMATH_REAL_1_2) (__TGMATH_REAL_2_3): Remove macros. (__TGMATH_CPLX, __TGMATH_CPLX_2, __TGMATH_REAL, __TGMATH_REAL_2) (__TGMATH_REAL_3, __TGMATH_CPLX_ONLY): Define using __builtin_tgmath. (frexp, ldexp, nexttoward, scalbn, scalbln): Define using __TGMATH_REAL_2. (remquo): Define using __TGMATH_REAL_3. gcc/c: * c-parser.c (check_tgmath_function): New function. (enum tgmath_parm_kind): New enum. (c_parser_postfix_expression): Handle __builtin_tgmath. gcc/c-family: * c-common.c (c_common_reswords): Add __builtin_tgmath. * c-common.h (enum rid): Add RID_BUILTIN_TGMATH. gcc/testsuite: * gcc.dg/builtin-tgmath-1.c, gcc.dg/builtin-tgmath-2.c, gcc.dg/builtin-tgmath-err-1.c, gcc.dg/builtin-tgmath-err-2.c, gcc.dg/dfp/builtin-tgmath-dfp-err.c, gcc.dg/dfp/builtin-tgmath-dfp.c: New tests. From-SVN: r254749
Diffstat (limited to 'gcc/ginclude')
-rw-r--r--gcc/ginclude/tgmath.h82
1 files changed, 19 insertions, 63 deletions
diff --git a/gcc/ginclude/tgmath.h b/gcc/ginclude/tgmath.h
index be3f5be..97968ad 100644
--- a/gcc/ginclude/tgmath.h
+++ b/gcc/ginclude/tgmath.h
@@ -38,68 +38,24 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
__TGMATH_CPLX*, __TGMATH_REAL*, and __TGMATH_CPLX_ONLY. _CPLX
means the generic argument(s) may be real or complex, _REAL means
real only, _CPLX means complex only. If there is no suffix, we are
- defining a function of one generic argument. If the suffix is _n
- it is a function of n generic arguments. If the suffix is _m_n it
- is a function of n arguments, the first m of which are generic. We
- only define these macros for values of n and/or m that are needed. */
-
-/* The general rules for generic macros are given in 7.22 paragraphs 1 and 2.
- If any generic parameter is complex, we use a complex version. Otherwise
- we use a real version. If the real part of any generic parameter is long
- double, we use the long double version. Otherwise if the real part of any
- generic parameter is double or of integer type, we use the double version.
- Otherwise we use the float version. */
-
-#define __tg_cplx(expr) \
- __builtin_classify_type(expr) == 9
-
-#define __tg_ldbl(expr) \
- __builtin_types_compatible_p(__typeof__(expr), long double)
-
-#define __tg_dbl(expr) \
- (__builtin_types_compatible_p(__typeof__(expr), double) \
- || __builtin_classify_type(expr) == 1)
-
-#define __tg_choose(x,f,d,l) \
- __builtin_choose_expr(__tg_ldbl(x), l, \
- __builtin_choose_expr(__tg_dbl(x), d, \
- f))
-
-#define __tg_choose_2(x,y,f,d,l) \
- __builtin_choose_expr(__tg_ldbl(x) || __tg_ldbl(y), l, \
- __builtin_choose_expr(__tg_dbl(x) || __tg_dbl(y), d, \
- f))
-
-#define __tg_choose_3(x,y,z,f,d,l) \
- __builtin_choose_expr(__tg_ldbl(x) || __tg_ldbl(y) || __tg_ldbl(z), l, \
- __builtin_choose_expr(__tg_dbl(x) || __tg_dbl(y) \
- || __tg_dbl(z), d, \
- f))
-
-#define __TGMATH_CPLX(z,R,C) \
- __builtin_choose_expr (__tg_cplx(z), \
- __tg_choose (__real__(z), C##f(z), (C)(z), C##l(z)), \
- __tg_choose (z, R##f(z), (R)(z), R##l(z)))
-
-#define __TGMATH_CPLX_2(z1,z2,R,C) \
- __builtin_choose_expr (__tg_cplx(z1) || __tg_cplx(z2), \
- __tg_choose_2 (__real__(z1), __real__(z2), \
- C##f(z1,z2), (C)(z1,z2), C##l(z1,z2)), \
- __tg_choose_2 (z1, z2, \
- R##f(z1,z2), (R)(z1,z2), R##l(z1,z2)))
+ defining a function of one argument. If the suffix is _n
+ it is a function of n arguments. We only define these macros for
+ values of n that are needed. */
+
+#define __TGMATH_CPLX(z,R,C) \
+ __builtin_tgmath (R##f, R, R##l, C##f, C, C##l, (z))
+
+#define __TGMATH_CPLX_2(z1,z2,R,C) \
+ __builtin_tgmath (R##f, R, R##l, C##f, C, C##l, (z1), (z2))
#define __TGMATH_REAL(x,R) \
- __tg_choose (x, R##f(x), (R)(x), R##l(x))
+ __builtin_tgmath (R##f, R, R##l, (x))
#define __TGMATH_REAL_2(x,y,R) \
- __tg_choose_2 (x, y, R##f(x,y), (R)(x,y), R##l(x,y))
+ __builtin_tgmath (R##f, R, R##l, (x), (y))
#define __TGMATH_REAL_3(x,y,z,R) \
- __tg_choose_3 (x, y, z, R##f(x,y,z), (R)(x,y,z), R##l(x,y,z))
-#define __TGMATH_REAL_1_2(x,y,R) \
- __tg_choose (x, R##f(x,y), (R)(x,y), R##l(x,y))
-#define __TGMATH_REAL_2_3(x,y,z,R) \
- __tg_choose_2 (x, y, R##f(x,y,z), (R)(x,y,z), R##l(x,y,z))
+ __builtin_tgmath (R##f, R, R##l, (x), (y), (z))
#define __TGMATH_CPLX_ONLY(z,C) \
- __tg_choose (__real__(z), C##f(z), (C)(z), C##l(z))
+ __builtin_tgmath (C##f, C, C##l, (z))
/* Functions defined in both <math.h> and <complex.h> (7.22p4) */
#define acos(z) __TGMATH_CPLX(z, acos, cacos)
@@ -135,10 +91,10 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
#define fmax(x,y) __TGMATH_REAL_2(x, y, fmax)
#define fmin(x,y) __TGMATH_REAL_2(x, y, fmin)
#define fmod(x,y) __TGMATH_REAL_2(x, y, fmod)
-#define frexp(x,y) __TGMATH_REAL_1_2(x, y, frexp)
+#define frexp(x,y) __TGMATH_REAL_2(x, y, frexp)
#define hypot(x,y) __TGMATH_REAL_2(x, y, hypot)
#define ilogb(x) __TGMATH_REAL(x, ilogb)
-#define ldexp(x,y) __TGMATH_REAL_1_2(x, y, ldexp)
+#define ldexp(x,y) __TGMATH_REAL_2(x, y, ldexp)
#define lgamma(x) __TGMATH_REAL(x, lgamma)
#define llrint(x) __TGMATH_REAL(x, llrint)
#define llround(x) __TGMATH_REAL(x, llround)
@@ -150,13 +106,13 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
#define lround(x) __TGMATH_REAL(x, lround)
#define nearbyint(x) __TGMATH_REAL(x, nearbyint)
#define nextafter(x,y) __TGMATH_REAL_2(x, y, nextafter)
-#define nexttoward(x,y) __TGMATH_REAL_1_2(x, y, nexttoward)
+#define nexttoward(x,y) __TGMATH_REAL_2(x, y, nexttoward)
#define remainder(x,y) __TGMATH_REAL_2(x, y, remainder)
-#define remquo(x,y,z) __TGMATH_REAL_2_3(x, y, z, remquo)
+#define remquo(x,y,z) __TGMATH_REAL_3(x, y, z, remquo)
#define rint(x) __TGMATH_REAL(x, rint)
#define round(x) __TGMATH_REAL(x, round)
-#define scalbn(x,y) __TGMATH_REAL_1_2(x, y, scalbn)
-#define scalbln(x,y) __TGMATH_REAL_1_2(x, y, scalbln)
+#define scalbn(x,y) __TGMATH_REAL_2(x, y, scalbn)
+#define scalbln(x,y) __TGMATH_REAL_2(x, y, scalbln)
#define tgamma(x) __TGMATH_REAL(x, tgamma)
#define trunc(x) __TGMATH_REAL(x, trunc)