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
This commit is contained in:
Ed Schouten 2012-01-05 10:46:22 +00:00
parent f8fd121ca4
commit 069a43171f
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=229575
2 changed files with 81 additions and 51 deletions

View File

@ -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)

View File

@ -24,7 +24,7 @@
.\"
.\" $FreeBSD$
.\"
.Dd August 14, 2004
.Dd January 4, 2012
.Dt TGMATH 3
.Os
.Sh NAME
@ -139,15 +139,24 @@ The header
.In tgmath.h
first appeared in
.Fx 5.3 .
.Sh BUGS
The header
.Sh COMPILER SUPPORT
Before
.St -isoC-11 ,
the header
.In tgmath.h
cannot be implemented with strictly conforming C code and needs
could not be implemented with strictly conforming C code and needed
special compiler support.
The current implementation only works for GCC.
.Pp
As of
.St -isoC-11 ,
this header file can be implemented using the
.Fn _Generic
language keyword.
In addition to compilers that support this keyword, this header file
works with GCC.
.Sh BUGS
Many of the functions mentioned here are not prototyped in
.In math.h
or
.In complex.h
as they are not yet implemented.
This prevents the corresponding type-generic macro from working at all.