summaryrefslogtreecommitdiffstats
path: root/include/tgmath.h
diff options
context:
space:
mode:
authored <ed@FreeBSD.org>2012-01-05 10:46:22 +0000
committered <ed@FreeBSD.org>2012-01-05 10:46:22 +0000
commit3e678d58aa7b47e823702aead47cf69982d3d270 (patch)
tree0eed688597265ddb650f4370eac630aae7c6c4c7 /include/tgmath.h
parentcbdad2302c0b444c8793270a01a683dff62d2397 (diff)
downloadFreeBSD-src-3e678d58aa7b47e823702aead47cf69982d3d270.zip
FreeBSD-src-3e678d58aa7b47e823702aead47cf69982d3d270.tar.gz
Reimplement <tgmath.h> on top of __generic().
The macro construction used now, is almost identical to the code provided in C11 proposal N1404. This new version doesn't seem to introduce any regressions according to the regression test in tools/, but still seems to malfunction with Clang on certain aspects. The new code does work successfully with GCC 4.2, 4.6 and 4.7. With 4.7, it also works when __generic() is implemented on top of _Generic(). Discussed with: stefanf
Diffstat (limited to 'include/tgmath.h')
-rw-r--r--include/tgmath.h111
1 files changed, 66 insertions, 45 deletions
diff --git a/include/tgmath.h b/include/tgmath.h
index 7ff8dd4..9673dd4 100644
--- a/include/tgmath.h
+++ b/include/tgmath.h
@@ -2,6 +2,9 @@
* Copyright (c) 2004 Stefan Farfeleder.
* All rights reserved.
*
+ * Copyright (c) 2012 Ed Schouten <ed@FreeBSD.org>
+ * All rights reserved.
+ *
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
@@ -33,64 +36,83 @@
#include <math.h>
/*
- * This implementation of <tgmath.h> requires two implementation-dependent
- * macros to be defined:
- * __tg_impl_simple(x, y, z, fn, fnf, fnl, ...)
+ * This implementation of <tgmath.h> uses the two following macros,
+ * which are based on the macros described in C11 proposal N1404:
+ * __tg_impl_simple(x, y, z, fnl, fn, fnf, ...)
* Invokes fnl() if the corresponding real type of x, y or z is long
* double, fn() if it is double or any has an integer type, and fnf()
* otherwise.
- * __tg_impl_full(x, y, z, fn, fnf, fnl, cfn, cfnf, cfnl, ...)
- * Invokes [c]fnl() if the corresponding real type of x, y or z is long
+ * __tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...)
+ * Invokes [c]fnl() if the corresponding real type of x or y is long
* double, [c]fn() if it is double or any has an integer type, and
* [c]fnf() otherwise. The function with the 'c' prefix is called if
- * any of x, y or z is a complex number.
+ * any of x or y is a complex number.
* Both macros call the chosen function with all additional arguments passed
* to them, as given by __VA_ARGS__.
*
* Note that these macros cannot be implemented with C's ?: operator,
* because the return type of the whole expression would incorrectly be long
* double complex regardless of the argument types.
+ *
+ * The _Complex_I distinction should not be needed, but due to a bug in
+ * GCC 4.2, _Complex_I is not of type float _Complex.
*/
-#if __GNUC_PREREQ__(3, 1)
-#define __tg_type(e, t) __builtin_types_compatible_p(__typeof__(e), t)
-#define __tg_type3(e1, e2, e3, t) \
- (__tg_type(e1, t) || __tg_type(e2, t) || __tg_type(e3, t))
-#define __tg_type_corr(e1, e2, e3, t) \
- (__tg_type3(e1, e2, e3, t) || __tg_type3(e1, e2, e3, t _Complex))
-#define __tg_integer(e1, e2, e3) \
- (((__typeof__(e1))1.5 == 1) || ((__typeof__(e2))1.5 == 1) || \
- ((__typeof__(e3))1.5 == 1))
-#define __tg_is_complex(e1, e2, e3) \
- (__tg_type3(e1, e2, e3, float _Complex) || \
- __tg_type3(e1, e2, e3, double _Complex) || \
- __tg_type3(e1, e2, e3, long double _Complex) || \
- __tg_type3(e1, e2, e3, __typeof__(_Complex_I)))
-
-#define __tg_impl_simple(x, y, z, fn, fnf, fnl, ...) \
- __builtin_choose_expr(__tg_type_corr(x, y, z, long double), \
- fnl(__VA_ARGS__), __builtin_choose_expr( \
- __tg_type_corr(x, y, z, double) || __tg_integer(x, y, z),\
- fn(__VA_ARGS__), fnf(__VA_ARGS__)))
-
-#define __tg_impl_full(x, y, z, fn, fnf, fnl, cfn, cfnf, cfnl, ...) \
- __builtin_choose_expr(__tg_is_complex(x, y, z), \
- __tg_impl_simple(x, y, z, cfn, cfnf, cfnl, __VA_ARGS__), \
- __tg_impl_simple(x, y, z, fn, fnf, fnl, __VA_ARGS__))
-
-#else /* __GNUC__ */
+#ifndef __generic
#error "<tgmath.h> not implemented for this compiler"
-#endif /* !__GNUC__ */
+#endif
+
+#define __tg_generic_simple(x, fnl, fn, fnf) \
+ __generic(x, long double _Complex, fnl, \
+ __generic(x, double _Complex, fn, \
+ __generic(x, float _Complex, fnf, \
+ __generic(x, __typeof(_Complex_I), fnf, \
+ __generic(x, long double, fnl, \
+ __generic(x, float, fnf, fn))))))
+#define __tg_impl_simple(x, y, z, fnl, fn, fnf, ...) \
+ __tg_generic_simple(x, \
+ __tg_generic_simple(y, \
+ __tg_generic_simple(z, fnl, fnl, fnl), \
+ __tg_generic_simple(z, fnl, fnl, fnl), \
+ __tg_generic_simple(z, fnl, fnl, fnl)), \
+ __tg_generic_simple(y, \
+ __tg_generic_simple(z, fnl, fnl, fnl), \
+ __tg_generic_simple(z, fnl, fn , fn ), \
+ __tg_generic_simple(z, fnl, fn , fn )), \
+ __tg_generic_simple(y, \
+ __tg_generic_simple(z, fnl, fnl, fnl), \
+ __tg_generic_simple(z, fnl, fn , fn ), \
+ __tg_generic_simple(z, fnl, fn , fnf)))(__VA_ARGS__)
+#define __tg_generic_full(x, cfnl, cfn, cfnf, fnl, fn, fnf) \
+ __generic(x, long double _Complex, cfnl, \
+ __generic(x, double _Complex, cfn, \
+ __generic(x, float _Complex, cfnf, \
+ __generic(x, __typeof(_Complex_I), cfnf, \
+ __generic(x, long double, fnl, \
+ __generic(x, float, fnf, fn))))))
+#define __tg_impl_full(x, y, cfnl, cfn, cfnf, fnl, fn, fnf, ...) \
+ __tg_generic_full(x, \
+ __tg_generic_full(y, cfnl, cfnl, cfnl, cfnl, cfnl, cfnl), \
+ __tg_generic_full(y, cfnl, cfn , cfn , cfnl, cfn , cfn ), \
+ __tg_generic_full(y, cfnl, cfn , cfnf, cfnl, cfn , cfnf), \
+ __tg_generic_full(y, cfnl, cfnl, cfnl, fnl , fnl , fnl ), \
+ __tg_generic_full(y, cfnl, cfn , cfn , fnl , fn , fn ), \
+ __tg_generic_full(y, cfnl, cfn , cfnf, fnl , fn , fnf )) \
+ (__VA_ARGS__)
/* Macros to save lots of repetition below */
#define __tg_simple(x, fn) \
- __tg_impl_simple(x, x, x, fn, fn##f, fn##l, x)
+ __tg_impl_simple(x, x, x, fn##l, fn, fn##f, x)
#define __tg_simple2(x, y, fn) \
- __tg_impl_simple(x, x, y, fn, fn##f, fn##l, x, y)
+ __tg_impl_simple(x, x, y, fn##l, fn, fn##f, x, y)
+#define __tg_simple3(x, y, z, fn) \
+ __tg_impl_simple(x, y, z, fn##l, fn, fn##f, x, y, z)
#define __tg_simplev(x, fn, ...) \
- __tg_impl_simple(x, x, x, fn, fn##f, fn##l, __VA_ARGS__)
+ __tg_impl_simple(x, x, x, fn##l, fn, fn##f, __VA_ARGS__)
#define __tg_full(x, fn) \
- __tg_impl_full(x, x, x, fn, fn##f, fn##l, c##fn, c##fn##f, c##fn##l, x)
+ __tg_impl_full(x, x, c##fn##l, c##fn, c##fn##f, fn##l, fn, fn##f, x)
+#define __tg_full2(x, y, fn) \
+ __tg_impl_full(x, y, c##fn##l, c##fn, c##fn##f, fn##l, fn, fn##f, x, y)
/* 7.22#4 -- These macros expand to real or complex functions, depending on
* the type of their arguments. */
@@ -108,13 +130,12 @@
#define tanh(x) __tg_full(x, tanh)
#define exp(x) __tg_full(x, exp)
#define log(x) __tg_full(x, log)
-#define pow(x, y) __tg_impl_full(x, x, y, pow, powf, powl, \
- cpow, cpowf, cpowl, x, y)
+#define pow(x, y) __tg_full2(x, y, pow)
#define sqrt(x) __tg_full(x, sqrt)
/* "The corresponding type-generic macro for fabs and cabs is fabs." */
-#define fabs(x) __tg_impl_full(x, x, x, fabs, fabsf, fabsl, \
- cabs, cabsf, cabsl, x)
+#define fabs(x) __tg_impl_full(x, x, cabsl, cabs, cabsf, \
+ fabsl, fabs, fabsf, x)
/* 7.22#5 -- These macros are only defined for arguments with real type. */
#define atan2(x, y) __tg_simple2(x, y, atan2)
@@ -127,7 +148,7 @@
#define expm1(x) __tg_simple(x, expm1)
#define fdim(x, y) __tg_simple2(x, y, fdim)
#define floor(x) __tg_simple(x, floor)
-#define fma(x, y, z) __tg_impl_simple(x, y, z, fma, fmaf, fmal, x, y, z)
+#define fma(x, y, z) __tg_simple3(x, y, z, fma)
#define fmax(x, y) __tg_simple2(x, y, fmax)
#define fmin(x, y) __tg_simple2(x, y, fmin)
#define fmod(x, y) __tg_simple2(x, y, fmod)
@@ -148,8 +169,8 @@
#define nextafter(x, y) __tg_simple2(x, y, nextafter)
#define nexttoward(x, y) __tg_simplev(x, nexttoward, x, y)
#define remainder(x, y) __tg_simple2(x, y, remainder)
-#define remquo(x, y, z) __tg_impl_simple(x, x, y, remquo, remquof, \
- remquol, x, y, z)
+#define remquo(x, y, z) __tg_impl_simple(x, x, y, remquol, remquo, \
+ remquof, x, y, z)
#define rint(x) __tg_simple(x, rint)
#define round(x) __tg_simple(x, round)
#define scalbn(x, y) __tg_simplev(x, scalbn, x, y)
OpenPOWER on IntegriCloud