xgbtxy opened a new pull request, #1682:
URL: https://github.com/apache/arrow-go/pull/1682

   ### Rationale for this change
   
   `arrow/float16` currently has four IEEE 754 binary16 correctness bugs:
   
   1. `Num.IsInf()` checks `(n.bits & 0x7c00) == 0x7c00` without verifying that 
the 10-bit significand is zero, so `IsInf()` returns `true` for every NaN 
encoding (`NaN().IsInf() == true`).
   2. `New(f float32)` sets `res = 0` when `exp == 0` without clearing `fc`, 
turning float32 subnormals (e.g. `math.Float32frombits(0x00400000)`) into 
non-zero float16 subnormals (`0x0200`) instead of signed zero.
   3. `New(f float32)` drops the lower 13 significand bits when `exp == 0xff`, 
converting float32 NaNs with payloads only in those bits (e.g. 
`math.Float32frombits(0x7f800001)`) into `+/-Inf` instead of NaN.
   4. `Float32()` decodes binary16 subnormals (`exp == 0 && fc != 0`) directly 
with float32 exponent `0`, producing float32 subnormals off by 2^112 (e.g. 
`0x0001` yields `2^-136` instead of `2^-24`), while `New(f float32)` flushes 
the entire binary16 subnormal range `[2^-24, 2^-14)` to zero.
   
   ### What changes are included in this PR?
   
   - `arrow/float16/float16.go`:
     - Fix `Num.IsInf()` to mask with `0x7fff` (`(n.bits & 0x7fff) == 0x7c00`).
     - In `New(f float32)`: clear `fc` when `exp == 0`, preserve NaN (`fc = 
0x200`) when a float32 NaN payload truncates to zero, and encode float32 values 
in the binary16 subnormal range by shifting the implicit leading significand 
bit into the 10-bit subnormal field.
     - In `Num.Float32()`: normalize non-zero binary16 subnormals (`exp == 0 && 
fc != 0`) into exact normal `float32` values.
   - `arrow/float16/float16_test.go`:
     - Add `TestIsInf` covering `+/-Inf`, NaN encodings, zeros, subnormals, and 
finite bounds.
     - Add `TestSubnormalAndSpecialFloat32Conversions` exhaustively verifying 
all 65,536 binary16 encodings (`IsNaN`, `IsInf`, subnormal `Float32()` values, 
and `Float32() -> New()` round-trip) plus float32 subnormal underflow and 
low-payload NaN preservation.
   
   ### Are these changes tested?
   
   Yes. Added unit tests in `arrow/float16/float16_test.go` and verified with 
`go test -race -count=1 -tags "assert,test"` and `-tags "noasm,assert,test"` 
across `./arrow/float16/...`, `./arrow/array/...`, `./arrow/scalar/...`, and 
`./arrow/compute/...`.
   
   ### Are there any user-facing changes?
   
   Yes: `float16.Num.IsInf()` now returns `false` for NaN values, and 
`float16.New` / `float16.Num.Float32` now conform to IEEE 754 binary16 
semantics for subnormal and NaN conversions.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to