aboutsummaryrefslogtreecommitdiff
path: root/math
diff options
context:
space:
mode:
authorJoseph Myers <joseph@codesourcery.com>2017-08-22 17:55:42 +0000
committerJoseph Myers <joseph@codesourcery.com>2017-08-22 17:55:42 +0000
commitd9bef9c0a41bc3ca4036542cbf2a7d2d0cf60237 (patch)
treeda821dc6e3782dfeed9df3151577b492aaa2afa2 /math
parent988f991b5069f09d793c887618e84a196b100416 (diff)
downloadglibc-d9bef9c0a41bc3ca4036542cbf2a7d2d0cf60237.tar
glibc-d9bef9c0a41bc3ca4036542cbf2a7d2d0cf60237.tar.gz
glibc-d9bef9c0a41bc3ca4036542cbf2a7d2d0cf60237.tar.bz2
glibc-d9bef9c0a41bc3ca4036542cbf2a7d2d0cf60237.zip
Fix tgmath.h handling of complex integers (bug 21684).
The tgmath.h macros return a real type not a complex type when an argument is of complex integer type (a GNU extension) and there are no arguments of complex floating type. It seems clear that just as real integers are mapped to double for tgmath.h, so complex integers should be mapped to _Complex double. This patch implements such a mapping. The main complication in fixing this bug is that the tgmath.h macros expand their arguments a large number of times, resulting in exponential blowup of the size of the expansion when calls to tgmath.h macros are used in the arguments of such macros; it would be unfortunate for fixing a bug with a fairly obscure extension to make the macros expand their arguments even more times. Thus, this patch optimizes the definitions of the relevant macros. __tgmath_real_type previously expanded its argument 7 times and now expands it 3 times. __tgmath_complex_type, used in place of __tgmath_real_type only for functions that might return either real or complex types, not for complex functions that always return real types or always return complex types, expands its argument 5 times. So the sizes of the macro expansions from nested macro calls are correspondingly reduced (remembering that each tgmath.h macro expands __tgmath_real_type, or sometimes now __tgmath_complex_type, several times). Sometimes the real return type resulted from calling a complex function and converting the result to a real type; sometimes it resulted from calling a real function, because the logic for determining whether arguments were real or complex, based on sizeof, was confused by integer promotions applying to e.g. short int but not _Complex short int. The relevant tests are converted to use a new macro __expr_is_real, which, by calling __builtin_classify_type rather than comparing the results of two calls to sizeof, also reduces the number of times macros expand their arguments. Although there are reductions in the number of times macros expand their arguments, I do not consider this to fix bug 21660, since a proper fix means each macro expanding its arguments only once (via using new compiler features designed for that purpose). Tested for x86_64. [BZ #21684] * math/tgmath.h (__floating_type): Simplify definitions. (__real_integer_type): New macro. (__complex_integer_type): Likewise. (__expr_is_real): Likewise. (__tgmath_real_type_sub): Update comment to describe handling of complex types. (__tgmath_complex_type_sub): New macro. (__tgmath_complex_type): Likewise. [__HAVE_FLOAT128 && __GLIBC_USE (IEC_60559_TYPES_EXT)] (__TGMATH_CF128): Use __expr_is_real. (__TGMATH_UNARY_REAL_IMAG): Use __tgmath_complex_type and __expr_is_real. (__TGMATH_BINARY_REAL_IMAG): Likewise. (__TGMATH_UNARY_REAL_IMAG_RET_REAL): Use __expr_is_real. * math/gen-tgmath-tests.py (Type.create_type): Create complex integer types.
Diffstat (limited to 'math')
-rwxr-xr-xmath/gen-tgmath-tests.py4
-rw-r--r--math/tgmath.h135
2 files changed, 90 insertions, 49 deletions
diff --git a/math/gen-tgmath-tests.py b/math/gen-tgmath-tests.py
index 9044670326..88307a7841 100755
--- a/math/gen-tgmath-tests.py
+++ b/math/gen-tgmath-tests.py
@@ -128,9 +128,7 @@ class Type(object):
real_type = Type(name, suffix=suffix, mant_dig=mant_dig,
condition=condition, order=order, integer=integer,
complex=False)
- # Complex integer types currently disabled because of problems
- # in tgmath.h.
- if complex_ok and not integer:
+ if complex_ok:
if complex_name is None:
complex_name = '_Complex %s' % name
complex_type = Type(complex_name, condition=condition,
diff --git a/math/tgmath.h b/math/tgmath.h
index 019f072e3e..68cee645f3 100644
--- a/math/tgmath.h
+++ b/math/tgmath.h
@@ -48,19 +48,43 @@
/* This is ugly but unless gcc gets appropriate builtins we have to do
something like this. Don't ask how it works. */
-/* 1 if 'type' is a floating type, 0 if 'type' is an integer type.
- Allows for _Bool. Expands to an integer constant expression. */
+/* __floating_type expands to 1 if TYPE is a floating type (including
+ complex floating types), 0 if TYPE is an integer type (including
+ complex integer types). __real_integer_type expands to 1 if TYPE
+ is a real integer type. __complex_integer_type expands to 1 if
+ TYPE is a complex integer type. All these macros expand to integer
+ constant expressions. All these macros can assume their argument
+ has an arithmetic type (not vector, decimal floating-point or
+ fixed-point), valid to pass to tgmath.h macros. */
# if __GNUC_PREREQ (3, 1)
-# define __floating_type(type) \
- (__builtin_classify_type ((type) 0) == 8 \
- || (__builtin_classify_type ((type) 0) == 9 \
- && __builtin_classify_type (__real__ ((type) 0)) == 8))
+/* __builtin_classify_type expands to an integer constant expression
+ in GCC 3.1 and later. Default conversions applied to the argument
+ of __builtin_classify_type mean it always returns 1 for real
+ integer types rather than ever returning different values for
+ character, boolean or enumerated types. */
+# define __floating_type(type) \
+ (__builtin_classify_type (__real__ ((type) 0)) == 8)
+# define __real_integer_type(type) \
+ (__builtin_classify_type ((type) 0) == 1)
+# define __complex_integer_type(type) \
+ (__builtin_classify_type ((type) 0) == 9 \
+ && __builtin_classify_type (__real__ ((type) 0)) == 1)
# else
-# define __floating_type(type) (((type) 0.25) && ((type) 0.25 - 1))
+/* GCC versions predating __builtin_classify_type are also looser on
+ what counts as an integer constant expression. */
+# define __floating_type(type) (((type) 1.25) != 1)
+# define __real_integer_type(type) (((type) (1.25 + _Complex_I)) == 1)
+# define __complex_integer_type(type) \
+ (((type) (1.25 + _Complex_I)) == (1 + _Complex_I))
# endif
-/* The tgmath real type for T, where E is 0 if T is an integer type and
- 1 for a floating type. */
+/* Whether an expression (of arithmetic type) has a real type. */
+# define __expr_is_real(E) (__builtin_classify_type (E) != 9)
+
+/* The tgmath real type for T, where E is 0 if T is an integer type
+ and 1 for a floating type. If T has a complex type, it is
+ unspecified whether the return type is real or complex (but it has
+ the correct corresponding real type). */
# define __tgmath_real_type_sub(T, E) \
__typeof__ (*(0 ? (__typeof__ (0 ? (double *) 0 : (void *) (E))) 0 \
: (__typeof__ (0 ? (T *) 0 : (void *) (!(E)))) 0))
@@ -70,6 +94,27 @@
__tgmath_real_type_sub (__typeof__ ((__typeof__ (+(expr))) 0), \
__floating_type (__typeof__ (+(expr))))
+/* The tgmath complex type for T, where E1 is 1 if T has a floating
+ type and 0 otherwise, E2 is 1 if T has a real integer type and 0
+ otherwise, and E3 is 1 if T has a complex type and 0 otherwise. */
+# define __tgmath_complex_type_sub(T, E1, E2, E3) \
+ __typeof__ (*(0 \
+ ? (__typeof__ (0 ? (T *) 0 : (void *) (!(E1)))) 0 \
+ : (__typeof__ (0 \
+ ? (__typeof__ (0 \
+ ? (double *) 0 \
+ : (void *) (!(E2)))) 0 \
+ : (__typeof__ (0 \
+ ? (_Complex double *) 0 \
+ : (void *) (!(E3)))) 0)) 0))
+
+/* The tgmath complex type of EXPR. */
+# define __tgmath_complex_type(expr) \
+ __tgmath_complex_type_sub (__typeof__ ((__typeof__ (+(expr))) 0), \
+ __floating_type (__typeof__ (+(expr))), \
+ __real_integer_type (__typeof__ (+(expr))), \
+ __complex_integer_type (__typeof__ (+(expr))))
+
/* Expand to text that checks if ARG_COMB has type _Float128, and if
so calls the appropriately suffixed FCT (which may include a cast),
or FCT and CFCT for complex functions, with arguments ARG_CALL. */
@@ -79,7 +124,7 @@
? fct ## f128 arg_call :
# define __TGMATH_CF128(arg_comb, fct, cfct, arg_call) \
__builtin_types_compatible_p (__typeof (+__real__ (arg_comb)), _Float128) \
- ? (sizeof (+__real__ (arg_comb)) == sizeof (+(arg_comb)) \
+ ? (__expr_is_real (arg_comb) \
? fct ## f128 arg_call \
: cfct ## f128 arg_call) :
# else
@@ -244,19 +289,20 @@
# define __TGMATH_UNARY_REAL_IMAG(Val, Fct, Cfct) \
(__extension__ ((sizeof (+__real__ (Val)) == sizeof (double) \
|| __builtin_classify_type (__real__ (Val)) != 8) \
- ? ((sizeof (+__real__ (Val)) == sizeof (+(Val))) \
- ? (__tgmath_real_type (Val)) Fct (Val) \
- : (__tgmath_real_type (Val)) Cfct (Val)) \
+ ? (__expr_is_real (Val) \
+ ? (__tgmath_complex_type (Val)) Fct (Val) \
+ : (__tgmath_complex_type (Val)) Cfct (Val)) \
: (sizeof (+__real__ (Val)) == sizeof (float)) \
- ? ((sizeof (+__real__ (Val)) == sizeof (+(Val))) \
- ? (__tgmath_real_type (Val)) Fct##f (Val) \
- : (__tgmath_real_type (Val)) Cfct##f (Val)) \
- : __TGMATH_CF128 ((Val), (__tgmath_real_type (Val)) Fct, \
- (__tgmath_real_type (Val)) Cfct, \
+ ? (__expr_is_real (Val) \
+ ? (__tgmath_complex_type (Val)) Fct##f (Val) \
+ : (__tgmath_complex_type (Val)) Cfct##f (Val)) \
+ : __TGMATH_CF128 ((Val), \
+ (__tgmath_complex_type (Val)) Fct, \
+ (__tgmath_complex_type (Val)) Cfct, \
(Val)) \
- ((sizeof (+__real__ (Val)) == sizeof (+(Val))) \
- ? (__tgmath_real_type (Val)) __tgml(Fct) (Val) \
- : (__tgmath_real_type (Val)) __tgml(Cfct) (Val))))
+ (__expr_is_real (Val) \
+ ? (__tgmath_complex_type (Val)) __tgml(Fct) (Val) \
+ : (__tgmath_complex_type (Val)) __tgml(Cfct) (Val))))
# define __TGMATH_UNARY_IMAG(Val, Cfct) \
(__extension__ ((sizeof (+__real__ (Val)) == sizeof (double) \
@@ -278,13 +324,13 @@
# define __TGMATH_UNARY_REAL_IMAG_RET_REAL(Val, Fct, Cfct) \
(__extension__ ((sizeof (+__real__ (Val)) == sizeof (double) \
|| __builtin_classify_type (__real__ (Val)) != 8) \
- ? ((sizeof (+__real__ (Val)) == sizeof (+(Val))) \
+ ? (__expr_is_real (Val) \
? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
Fct (Val) \
: (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
Cfct (Val)) \
: (sizeof (+__real__ (Val)) == sizeof (float)) \
- ? ((sizeof (+__real__ (Val)) == sizeof (+(Val))) \
+ ? (__expr_is_real (Val) \
? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
Fct##f (Val) \
: (__typeof__ (__real__ (__tgmath_real_type (Val)) 0))\
@@ -297,7 +343,7 @@
(__real__ \
(__tgmath_real_type (Val)) 0)) Cfct, \
(Val)) \
- ((sizeof (+__real__ (Val)) == sizeof (+(Val))) \
+ (__expr_is_real (Val) \
? (__typeof__ (__real__ (__tgmath_real_type (Val)) 0)) \
__tgml(Fct) (Val) \
: (__typeof__ (__real__ (__tgmath_real_type (Val)) 0)) \
@@ -312,41 +358,38 @@
+ __real__ (Val2)) == 8) \
? __TGMATH_CF128 ((Val1) + (Val2), \
(__typeof \
- ((__tgmath_real_type (Val1)) 0 \
- + (__tgmath_real_type (Val2)) 0)) \
+ ((__tgmath_complex_type (Val1)) 0 \
+ + (__tgmath_complex_type (Val2)) 0)) \
Fct, \
(__typeof \
- ((__tgmath_real_type (Val1)) 0 \
- + (__tgmath_real_type (Val2)) 0)) \
+ ((__tgmath_complex_type (Val1)) 0 \
+ + (__tgmath_complex_type (Val2)) 0)) \
Cfct, \
(Val1, Val2)) \
- ((sizeof (+__real__ (Val1)) == sizeof (+(Val1)) \
- && sizeof (+__real__ (Val2)) == sizeof (+(Val2))) \
- ? (__typeof ((__tgmath_real_type (Val1)) 0 \
- + (__tgmath_real_type (Val2)) 0)) \
+ (__expr_is_real ((Val1) + (Val2)) \
+ ? (__typeof ((__tgmath_complex_type (Val1)) 0 \
+ + (__tgmath_complex_type (Val2)) 0)) \
__tgml(Fct) (Val1, Val2) \
- : (__typeof ((__tgmath_real_type (Val1)) 0 \
- + (__tgmath_real_type (Val2)) 0)) \
+ : (__typeof ((__tgmath_complex_type (Val1)) 0 \
+ + (__tgmath_complex_type (Val2)) 0)) \
__tgml(Cfct) (Val1, Val2)) \
: (sizeof (+__real__ (Val1)) == sizeof (double) \
|| sizeof (+__real__ (Val2)) == sizeof (double) \
|| __builtin_classify_type (__real__ (Val1)) != 8 \
|| __builtin_classify_type (__real__ (Val2)) != 8) \
- ? ((sizeof (+__real__ (Val1)) == sizeof (+(Val1)) \
- && sizeof (+__real__ (Val2)) == sizeof (+(Val2))) \
- ? (__typeof ((__tgmath_real_type (Val1)) 0 \
- + (__tgmath_real_type (Val2)) 0)) \
+ ? (__expr_is_real ((Val1) + (Val2)) \
+ ? (__typeof ((__tgmath_complex_type (Val1)) 0 \
+ + (__tgmath_complex_type (Val2)) 0)) \
Fct (Val1, Val2) \
- : (__typeof ((__tgmath_real_type (Val1)) 0 \
- + (__tgmath_real_type (Val2)) 0)) \
+ : (__typeof ((__tgmath_complex_type (Val1)) 0 \
+ + (__tgmath_complex_type (Val2)) 0)) \
Cfct (Val1, Val2)) \
- : ((sizeof (+__real__ (Val1)) == sizeof (+(Val1)) \
- && sizeof (+__real__ (Val2)) == sizeof (+(Val2))) \
- ? (__typeof ((__tgmath_real_type (Val1)) 0 \
- + (__tgmath_real_type (Val2)) 0)) \
+ : (__expr_is_real ((Val1) + (Val2)) \
+ ? (__typeof ((__tgmath_complex_type (Val1)) 0 \
+ + (__tgmath_complex_type (Val2)) 0)) \
Fct##f (Val1, Val2) \
- : (__typeof ((__tgmath_real_type (Val1)) 0 \
- + (__tgmath_real_type (Val2)) 0)) \
+ : (__typeof ((__tgmath_complex_type (Val1)) 0 \
+ + (__tgmath_complex_type (Val2)) 0)) \
Cfct##f (Val1, Val2))))
#else
# error "Unsupported compiler; you cannot use <tgmath.h>"