Skip to content

Conversation

@zhurvla
Copy link
Contributor

@zhurvla zhurvla commented Dec 17, 2025

No description provided.

@gbrail
Copy link
Collaborator

gbrail commented Dec 18, 2025

Looks like the right idea to me. Does anyone else want to do a code review first?

Copy link
Collaborator

@rbri rbri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if the bit shifting is worth the effort and it might decrease the readability. On the other hand we already do a lot of bit magic in this class ...

Outside of the minor suggestions this is another great step forward, thanks a lot for this PR.

return Float.valueOf(sign == 0 ? 0.0f : -0.0f);
} else {
// Denormalized number
float value = (float) (mantissa / 1024.0 * Math.pow(2, -14));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can precalculate some constant value

if (exponent == 0) {
if (mantissa == 0) {
// Zero
return Float.valueOf(sign == 0 ? 0.0f : -0.0f);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i guess the code looks simple if you do not use else after the return (same for the other if else cases)

}
} else {
// Normalized number
float value = (float) ((1.0 + mantissa / 1024.0) * Math.pow(2, exponent - 15));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this class does a lot of bit shifting, maybe we can should do this for / 1024.0 also?

// Overflow to infinity
doWriteInt16(buf, offset, (sign << 15) | 0x7c00, littleEndian);
return;
} else if (absVal < (float) Math.pow(2, -14)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again return/else but you use the other approach in the code above

} else if (exponent <= 0) {
// Denormalized
exponent = 0;
mantissa = (int) Math.round((absVal / Math.pow(2, -14)) * 1024.0) & 0x3ff;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

precalculation and bit shift?

@aardvark179
Copy link
Contributor

I'm a little nervous of the bit manipulation here so it would be really helpful to have a written explanation of what's going on. Also @zhurvla it's helpful to write a little bit more than a title in a PR, and it's worth taking care not to accidentally pull in the properties file from our fork, I got a bit of a shock at the amount of change in the first commit until I realised what had happened.

}
}

public static Float readFloat16(byte[] buf, int offset, boolean littleEndian) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the other functions in this file... could we implement it in a similar way to the existing readFloat32 and friends?

    public static Float readFloat32(byte[] buf, int offset, boolean littleEndian) {
        long base = readUint32Primitive(buf, offset, littleEndian);
        return Float.valueOf(Float.intBitsToFloat((int) base));
    }

This looks so much simpler than what you have implemented. Can we do something like it for float 16, relying on readInt16? (Ditto for the setter)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have a built in function for making single precision float from the bits of a half precision float.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants