[Cfp-interest] type-generic math

Joel C. Salomon joelcsalomon at gmail.com
Tue Jan 17 15:10:26 PST 2012


On 01/17/2012 05:13 PM, Jim Thomas wrote:
> On Jan 17, 2012, at 10:58 AM, Fred J. Tydeman wrote:
>> On Tue, 17 Jan 2012 10:13:37 -0800 Jim Thomas wrote:
>>>
>>> and the total order functions (F.10.12)
>>
>> Why are they excluded?
>>
> Hmm. The Functions document excludes them but doesn't say why. Does anyone remember? If not, I'll include them for now.
> 
> The issue Joel raised is about the difficulty in using the new _Generic mechanism to implement type-generic macros with more than one generic argument. This may be an issue for _Generic, but not for making such functions type-generic. As Fred pointed out, we already have 2 and 3 argument type-generic functions (which have been implemented, though, to my knowledge, without using _Generic).

Meanwhile, on 01/17/2012 04:42 PM, Fred J. Tydeman wrote:
> Seems to me that they would be the same as atan2() and pow()
> -- which also have two FP arguments.
> And easier than fma() -- which has three FP arguments.

Arithmetic functions of multiple arguments *can* be implemented via
_Generic.  Whatever C99 magical extensions implementations used for
<tgmath.h> are probably still there in the C11 versions of those
compilers, so perhaps implementors haven't updated the macros.  But
Clark Nelson defined _Generic to be powerful enough to define all of
<tgmath.h>.  Specifically, <bit.ly/c11-n1404> has example of nested
_Generic selection expressions for pow() and fma().

This works for the arithmetic functions because type promotion is
meaningful; e.g., `pow(2.3f, 3.4L)` becomes `powl(2.3f, 3.4L)`.  But (as
I was given to understand) the total order functions are entirely
meaningless when applied to mixed types, so I was looking into the
possibility of excluding those erroneous invocations.

[A thought occurs to me:  I might get very, very clever with _Generic
and define a type-generic totalorder() macro that gives meaningful error
messages if it's misused -- in C; what about when this standard is
adapted to C++?  But Google turns up
<http://code.google.com/p/mili/wiki/PromotionDisable>, which indicates
that template magic can provide the same protection (and probably with
cleaner error messages).]

--Joel


More information about the Cfp-interest mailing list