[Cfp-interest] implementing tgmath for constant rounding modes
Joel C. Salomon
joelcsalomon at gmail.com
Thu Jun 7 16:22:20 PDT 2012
On 06/07/2012 03:48 PM, Fred J. Tydeman wrote:
>
> On Sat, 2 Jun 2012 08:22:55 -0700 Jim Thomas wrote:
>>
>> <tgmath.h> aside, the math functions that can be affected by constant rounding modes must be
>> sensitive to whether a constant rounding mode is in effect.
>> Take the example of cbrt. <math.h> could have
>>
>> #define cbrt(x) __roundwise_cbrt(x)
>>
>> Then the translator would have to keep track of whether a constant rounding mode is in effect at
>> each call site for __roundwise_cbrt and, if so, what the rounding direction is.
>>
>> Compilers already keep track of the state of other FP pragmas, which have the same sort of
>> scoping as the constant rounding mode pragmas.
>
> As far as I recall, neither gcc nor clang keep track of FENV_ACCESS. So, I am not
> sure how easy it is to track state of FP pragmas.
>
> So, in effect, __roundwise_cbrt becomes a special keyword/token/symbol to the compiler.
> And the same for all the other math functions (that is a lot of names to treat special).
>
> So, if another compiler did it as
> #define cbrt(x) __RoundWise_cbrt(x)
> it will be hard for 3rd party library/header implementors
> (such as P.J.Plauger's Dinkumware) to create a header that
> works with all compilers.
>
> Would it be better to add just one new attribute, eg,
> #define cbrt(x) [[ROUNDWISE]] cbrt(x)
> that would be the same for all compilers?
Except that attributes were not accepted into C11.
Also, looking at <http://open-std.org/jtc1/sc22/wg14/www/docs/n1403.pdf>
where attributes for C++ are described, it does not seem to me that they
can be used in this way, only for declarations and listed control
constructs.
So you'd be left with including [[roundwise]] in the function
declaration, e.g.:
double cbrt [[roundwise]] (double);
(which cannot be evaded by macro suppression), or with a cast, e.g.:
typedef double __roundwise_dbl [[roundwise]] (double);
#define cbrt(x) ((__roundwise)cbrt)(x)
but does anyone know if such a cast does what I'm trying for in C++?
--Joel
More information about the Cfp-interest
mailing list