Question

In quite some articles they suggest using the fistp instruction for converting float->integer in a fast way. This is dependent on the current rounding mode of the CPU, so you'll have to set it.

But can this rounding mode get changed during the runtime of the program by other programs? By the OS?

Even worse, is this a possible scenario (pseudocode)?

set_rounding_mode(ROUND_TRUNCATE);
/* process gets switched out and other process sets the rounding mode to round */
int x = round_with_fistp(0.6);
printf("%d\n", x); // prints 1
Was it helpful?

Solution

No sane general-purpose OS would share the rounding mode across processes and threads. It should only change when a thread requests it and the change should be local to that thread only. You may, however, encounter that some libraries (especially, 3rd party) change it and sometimes (or always) fail to restore it and force you to implement workarounds.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top