floating point division on multiple computers - same architecture

floating point division on multiple computers - same architecture

Hi,
the question is, whether the floating point division of two integers like this:

int i, j;
//init
float x = ((float) i)/j;

ends up with the exactly same result on any computer within one CPU architecture, one operating system and one compiler. I don't really care what the exact number of the result is, i just need it to be exactly the same on multiple computers with the same architecture(in particular i ask about x86_64).

An example:

I have two computers. Both of them are have x64 Debian. One of them has Intel C2D processor and the other one say AMD Phenom II. Now on C2D i compile a source code stated above. The question is: Will the x variable contain exactly the same result on both of the computers for every possible i and j values(same on both computers of course)? Now, do not consider any compiler optimization - let us say the value of the two variables is not known during compile time... Again, I don't care about the precision of the computation, i just need the values to be the same.

6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Quoting aros_prince
Hi,
the question is, whether the floating point division of two integers like this:

int i, j;
//init
float x = ((float) i)/j;

ends up with the exactly same result on any computer within one CPU
architecture, one operating system and one compiler. I don't really care
what the exact number of the result is, i just need it to be exactly
the same on multiple computers with the same architecture(in particular i
ask about x86_64).

An example:

I have two computers. Both of them are have x64 Debian. One of them has
Intel C2D processor and the other one say AMD Phenom II. Now on C2D i
compile a source code stated above. The question is: Will the x variable
contain exactly the same result on both of the computers for every
possible i and j values(same on both computers of course)? Now, do not
consider any compiler optimization - let us say the value of the two
variables is not known during compile time... Again, I don't care about
the precision of the computation, i just need the values to be the same.

Then you should use your compiler options to specify IEEE accurate division (icc -prec-div or one of the options which implies it, such as -fp-model source). For gcc, you would avoid the reciprocal-math option. Accuracy of the approximate reciprocal instruction on AMD64 is less than it was on the earlier AMD32 CPUs, and both AMD versions differ from the Intel version.
Needless to say, in more realistic context, other issues may come in. We can't guess how you are using this.

I am using gcc. The purpose of the question was rather to find out, whether it is possible to ensure the same result (whatever it is...). I need it to be the same if compiled as x86 or x64 (configurable by CMake), not at one time. What i want to say is that if compiled as x86, then all the x86 computers that runs the program has to agree on the same result. If compiled as x64, then all x64 computers has to agree on the same result. So what do i need to specify to gcc for it to reach my goal. I hear I can also insert instructions like _controlfp() to set it in the program.

If you use x87 code (the default for gcc 32-bit) you will need to make sure the precision mode is set the same in all cases (e.g. by controlfp()). You would avoid this and get the same code as the x64 default by setting an option which supports sse; e.g. -march=pentium4 -mfpmath=sse
In either case, as I mentioned before, you would avoid the reciprocal-math option.
If you have questions about gcc, the more appropriate place is the gcc-help mail list (sign up at gcc.gnu.org).

Well i think the answer i got is clear enough. Thank you very much.

>>...I don't care about the precision of the computation, i just need the values to be the same...

Tim answered all your questions. I'd like to add that calls like:
...
uiControlWordx87 = CrtControl87( _RTFPU_CW_DEFAULT, _RTFPU_MCW_PC );
or
uiControlWordx87 = CrtControl87( _RTFPU_PC_24, _RTFPU_MCW_PC );
or
uiControlWordx87 = CrtControl87( _RTFPU_PC_53, _RTFPU_MCW_PC );
...
at the beginning of your processing will guarantee that FPU settings for all platforms and computers are consistent. Only in that case your results will be identical for some C/C++ compiler but could be different (!) with another C/C++ compiler.

Leave a Comment

Please sign in to add a comment. Not a member? Join today