Implementing Constant_Float.
Hi there,
I've been experiencing some difficulties in implementing the Constant_Float of a classfile in my C++ bytecode library.
While I am able to read and write the two 32-bit values correctly to and from the classfile, I am unable to represent the value as a C++ 'float'.
According to 4.4.4 of the ClassFile spec, (http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html), this following code should be converting the IEEE float to a float value:
int s = ((value >> 31) == 0) ? 1 : -1;
int e = ((value >> 23) & 0xff);
int m = (e == 0) ?
(value & 0x7fffff) << 1 :
(value & 0x7fffff) | 0x800000;
return (float)(s * m * pow(2, e - 150));
Where 'value' is an 'int'.
However, I have been unable to successfully implement this.
The float value in the classfile is equal to 1234.567f (0x449A5225), and this is the value I have stored in 'value'.
However, the procedure specified in the spec. is not giving me valid results, any assistance?
Thanks.
[1259 byte] By [
regecksa] at [2007-11-27 6:32:17]

# 6
Hi,
I did take a look at the JVM source, and was able to solve my problem, thanks :).
It involved creating a float/int union, casting my 32-bit value to a long and placing it in the int field, and then my float ended up being correctly stored in the union's float.
I'm not sure why this approach has worked but not others.
Thanks anyway :).
# 7
This approach has worked because your platform uses IEEE754 floats so it doesn't need the bits moved around.
The code you quoted from the JVM spec works in Java (modulo FP accuracy, bearing in mind that 1234.567f doesn't have an exact FP representation):
public static voidmain(String[] args)
{
intbits = Float.floatToIntBits(1234.567f);
System.out.println("0x"+Integer.toHexString(bits));
int s = ((bits >> 31) == 0) ? 1 : -1;
int e = ((bits >> 23) & 0xff);
int m = (e == 0) ?
(bits & 0x7fffff) << 1 :
(bits & 0x7fffff) | 0x800000;
floatf = (float)(s*m*Math.pow(2.0, e-150));
System.out.printf("%f\n", f);
}
ejpa at 2007-7-12 17:57:39 >
