Proposal: fixing the 'pure' floating point problem.

Don nospam at nospam.com
Mon Mar 16 06:48:42 PDT 2009


Michel Fortin wrote:
> On 2009-03-16 08:27:28 -0400, Don <nospam at nospam.com> said:
> 
>> That requires a new keyord, four new calling conventions, a new name 
>> mangling scheme, compiler insertion of special code, nasty issues with 
>> function pointers, ...
> 
> Which isn't much different as adding a new extern(x) option, for which 
> all these problems have been solved.

Adding an extern(x) is a colossal language change!


>> for a feature that almost nobody will ever use. And it doesn't deal 
>> with    dynamic rounding mode.
> 
> Well, isn't it dynamic rounding mode? I ask because you can change the 
> mode dynamically by calling a function. With floatmode(neutral) you tell 
> which function support any rounding mode, and with floatmode(x) you 
> choose which rounding mode to use within a function. It just makes sure 
> those changes are scoped and limit them to function boundaries.

You have to be able to SET the rounding mode. In the dynamic case, you 
can't get the compiler to do it automatically for you, without saying 
which one you want!

> 
> If you want to evaluate the same function with two rounding modes, just 
> create a template:
> 
>     R roundUp(alias a, R)(float arg) floatmode(round_up)
>     {
>         return a(arg);
>     }
>     R roundDown(alias a, R)(float arg) floatmode(round_down)
>     {
>         return a(arg);
>     }
> 
> then call it:
> 
>     roundUp!(sin)(8);
>     roundDown!(sin)(8);
> 
> 
>> And it doesn't solve the problem of the sticky flags.
> 
> As for sticky flags, couldn't they be returned by the template when you 
> care about them? Something like:
> 
>     struct FloatAndStickyFlags {
>         this(float, int);
>         float value;
>         int sticky_flags;
>     }
>     FloatAndStickyFlags roundDownGetStickyFlags(alias a, R)(float arg) 
> floatmode(round_down)
>     {
>         return FloatAndStickyFlags(a(arg), getStickyFlags());
>     }

No, you can't do that without a guarantee that the sticky flags are 
preserved. The sticky flags depend on the state of the sticky flags on 
entry to the function, and on the behaviour of the function. If you only 
cache the return value of the function, the sticky flags will be wrong.
AND even if you did solve that, you'd still need to duplicate every 
function in your code, according to whether it return the sticky flags 
or not!

I've already posted two elegant, very simple solutions which solve all 
the problems.

SOLUTION 1: use the module statement to mark specific modules as 
containing functions which need to use the rounding mode and/or sticky 
flags, preventing pure functions in those modules from being cached when 
called from other similarly marked modules;

SOLUTION 2: provide a call into the runtime to turn caching of pure 
functions on and off.

There's absolutely no need for complicated stuff.



More information about the Digitalmars-d mailing list