[Vm-dev] Fwd: [Pharo-users] Set Rounding mode for IEEE floating point operations

Nicolas Cellier nicolas.cellier.aka.nice at gmail.com
Wed May 23 20:17:18 UTC 2018


Interesting!
Are you after Interval arithmetic or something more complex?

Don't forget that the activeProcess can be preempted
- if a higher priority Process gets ready
- if it waits for a Semaphore
- if it explicitely yields and a process with same priority is ready
So Double roundToMinusInfWhile: [... code goes here ...] could have side
effects on concurrent Process...
Unless the rounding mode is made a Process specific variable and that the
ProcessorScheduler cares to restore appropriate rounding mode on Process
switch if they differ... If the switch is too expensive, it could be
differed until a floating point primitive is invoked (unfortunately FFI and
plugins would have to be protected too).

Note that I have played with computation of residual errors on main
operations (+ - * / sqrt) with fma or Kahan-sum-like technics.
This code could also be used for fully emulating some rounding mode at
image side.
If it's only for Interval arithmetic, an image side solution could scale
(more expensive than native hardware, but it's to be tested on
macro-benchmark).
Tell me if your are interested.

Also note that unless you use some correctly rounded libm for the
sin/cos/exp/ln... primitives (CRLIBM for example), then there is no much
guaranty on the behaviour of these functions. The algorithm for
Float>>raisedTo: and even raisedToInteger: is quite naive too, and will
cumulate several ulp errors that a good libm/pow would not.

Please keep us informed on your progress :)

Nicolas

2018-05-23 19:23 GMT+02:00 Levente Uzonyi <leves at caesar.elte.hu>:

>
> Hi Steffen,
>
> As always, there are two ways to do this: via FFI or by writing a plugin
> (which would implement the primitive).
>
> For this case, FFI is probably easier to use. On POSIX platforms, you can
> call the fegetround/fesetround C99 functions to get/set the rounding mode.
> I have got no idea how to use FFI in Pharo, but in Squeak it would be as
> simple as writing these two methods:
>
> currentRoundingMode
>
>         <cdecl: long 'fegetround' (void) module: 'libm'>
>         ^self externalCallFailed
>
> setRoundingModeTo: newMode
>         "Set the rounding mode to newMode. Return zero on success, nonzero
> on failure/unsupported mode.
>         Modes:
>                 0      Rounding is toward 0.
>                 1      Rounding is toward nearest number.
>                 2      Rounding is toward positive infinity.
>                 3      Rounding is toward negative infinity.
>         "
>
>         <cdecl: long 'fesetround' (long) module: 'libm'>
>         ^self externalCallFailed
>
> Note that your platform may not support some rounding modes, and changing
> the rounding mode may affect other parts of the VM.
>
> Here's a quote from the GNU documentation about rounding modes:
> "You should avoid changing the rounding mode if possible. It can be an
> expensive operation; also, some hardware requires you to compile your
> program differently for it to work. The resulting code may run slower. See
> your compiler documentation for details."
> So, you might have to tweak compiler flags and compile your own VM if
> things don't work out of the box.
>
> You might also find this answer helpful: https://stackoverflow.com/ques
> tions/6867693/change-floating-point-rounding-mode#6867722
>
> Levente
>
> On Wed, 23 May 2018, Steffen Märcker wrote:
>
>
>> Dear VM experts,
>>
>> I originally asked the question below on pharo-users. Basically I am
>> looking for a way to set the IEEE founding mode for fp-operations at
>> runtime from the image.
>>
>> Best, Steffen
>>
>>
>> Original mail to pharo-users:
>>
>> Hi,
>>
>> is there any way to set the rounding mode for IEEE floating point
>> operations? Maybe something like
>>
>> Double roundToMinusInfWhile: [... code goes here ...]
>>> Double roundToZeroWhile: [... more code here ...]
>>>
>>
>> If not, is it possible to add this behavior, e.g., via a custom primitive?
>>
>> Best, Steffen
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/vm-dev/attachments/20180523/6e5ccba2/attachment.html>


More information about the Vm-dev mailing list