<div dir="ltr"><div>Interesting!</div><div>Are you after Interval arithmetic or something more complex?<br></div><div><br></div><div>Don't forget that the activeProcess can be preempted</div><div>- if a higher priority Process gets ready</div><div>- if it waits for a Semaphore</div><div>- if it explicitely yields and a process with same priority is ready</div><div>So Double roundToMinusInfWhile: [... code goes here ...] could have side effects on concurrent Process...</div><div>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).<br></div><div><br></div><div>Note that I have played with computation of residual errors on main operations (+ - * / sqrt) with fma or Kahan-sum-like technics.</div><div>This code could also be used for fully emulating some rounding mode at image side.<br></div><div>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).</div><div>Tell me if your are interested.</div><div><br></div><div>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.<br></div><div><br></div><div>Please keep us informed on your progress :)</div><div><br></div><div>Nicolas<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">2018-05-23 19:23 GMT+02:00 Levente Uzonyi <span dir="ltr"><<a href="mailto:leves@caesar.elte.hu" target="_blank">leves@caesar.elte.hu</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> <br>Hi Steffen,<br>
<br>
As always, there are two ways to do this: via FFI or by writing a plugin (which would implement the primitive).<br>
<br>
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.<br>
I have got no idea how to use FFI in Pharo, but in Squeak it would be as simple as writing these two methods:<br>
<br>
currentRoundingMode<br>
<br>
        <cdecl: long 'fegetround' (void) module: 'libm'><br>
        ^self externalCallFailed<br>
<br>
setRoundingModeTo: newMode<br>
        "Set the rounding mode to newMode. Return zero on success, nonzero on failure/unsupported mode.<br>
        Modes:<br>
                0      Rounding is toward 0.<br>
                1      Rounding is toward nearest number.<br>
                2      Rounding is toward positive infinity.<br>
                3      Rounding is toward negative infinity.<br>
        "<br>
<br>
        <cdecl: long 'fesetround' (long) module: 'libm'><br>
        ^self externalCallFailed<br>
<br>
Note that your platform may not support some rounding modes, and changing the rounding mode may affect other parts of the VM.<br>
<br>
Here's a quote from the GNU documentation about rounding modes:<br>
"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."<br>
So, you might have to tweak compiler flags and compile your own VM if things don't work out of the box.<br>
<br>
You might also find this answer helpful: <a href="https://stackoverflow.com/questions/6867693/change-floating-point-rounding-mode#6867722" rel="noreferrer" target="_blank">https://stackoverflow.com/ques<wbr>tions/6867693/change-floating-<wbr>point-rounding-mode#6867722</a><br>
<br>
Levente<br>
<br>
On Wed, 23 May 2018, Steffen Märcker wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Dear VM experts,<br>
<br>
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.<br>
<br>
Best, Steffen<br>
<br>
<br>
Original mail to pharo-users:<br>
<br>
Hi,<br>
<br>
is there any way to set the rounding mode for IEEE floating point<br>
operations? Maybe something like<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Double roundToMinusInfWhile: [... code goes here ...]<br>
Double roundToZeroWhile: [... more code here ...]<br>
</blockquote>
<br>
If not, is it possible to add this behavior, e.g., via a custom primitive?<br>
<br>
Best, Steffen<br>
</blockquote>
<br></blockquote></div><br></div>