4

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
1
  • I think the answer is no. The floating-point mode is one of the many things that must be saved across context switches. Commented Apr 27, 2012 at 0:37

1 Answer 1

7

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.