X86 80-bit floating point type in Java

I want to emulate an extended x86 precision type and do arithmetic and drop other types in Java.

I could try to implement it with BigDecimal, but covering all the special cases around NaNs, infinity, and castings would probably be a tedious task. I know of some libraries that provide other floating types with higher precision than double, but I want to have the same precision as the 80-bit x86 float.

Is there a Java library that provides this type of floating point? If not, can you provide other recommendations that would allow you to implement this type of data with less effort than coming up with a special BigDecimal solution?

+7
source share
3 answers

An 80-bit value is best kept as a combination of long (for the mantissa) and int for the exponent and sign. For many operations, it will probably be most appropriate to place the upper and lower halves of the length into separate β€œlong” values, so the code for adding two numbers with the corresponding signs and indicators is likely to be something like this:

 long resultLo = (num1.mant & 0xFFFFFFFFL)+(num2.mant & 0xFFFFFFFFL); long resultHi = (num1.mant >>> 32)+(num2.mant >>> 32)+(resultLo >>> 32); result.exp = num1.exp; // Should match num2.exp if (resultHi > 0xFFFFFFFFL) { exponent++; resultHi = (resultHi + ((resultHi & 2)>>>1)) >>> 1; // Round the result } rest.mant = (resultHi << 32) + resultLo; 

A bit of trouble around, but not completely inoperative. The key is to break the numbers into pieces small enough so that you can do all your math as you type "long".

By the way, note that if one of the numbers initially did not have the same indicator, it will be necessary to monitor whether any bits stopped when moving it left or right to correspond to the indicator of the first number, so that after that properly round the result.

+3
source

If you know that your Java code will actually run on an x86 processor, implement 80-bit arithmetic in the assembly (or C if the C compiler supports this) and is invoked with JNI.

If you are targeting a specific platform other than x86, check out the qemu code. There must be some way to rip out only the part that performs 80-bit floating point operations. (Edit: qemu SoftFloat implementation.). Call JNI.

If you really need 80-bit arithmetic with a clean platform with a clean Java environment, you can probably compare it with the C implementation in open source emulators to make sure that you are accessing the right angular cases.

+4
source

This is slightly the opposite of the java strictfp option , which limits the computation to 8 bytes, where 80 bits are executed.

So, my answer runs on the JVM on a 64-bit machine, possibly on some / OS VM hypervisors, so you have a development platform.

-3
source

All Articles