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

Levente Uzonyi leves at caesar.elte.hu
Wed May 23 17:23:27 UTC 2018


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/questions/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
>


More information about the Vm-dev mailing list