Skip to content

Conversation

@miantalha45
Copy link

Summary

Adds bfloat16 and bfloat16_array support to the Fory JavaScript runtime and codegen, following the same structure as the existing float16 implementation.

Fixes #3288

Changes

Runtime

  • BFloat16 class with fromBits() / toBits() and fromFloat32() / toFloat32() (IEEE 754 compliant, round-to-nearest ties-to-even for float32→bfloat16).
  • BFloat16Array backed by Uint16Array for packed bfloat16 arrays; supports iteration and fromRaw() for deserialization.
  • Wire format: 2 bytes per value (little-endian), matching existing float32/float64 behavior. NaN/Inf/±0/subnormals round-trip correctly.

Type system & serialization

  • Type.bfloat16() and Type.bfloat16Array().
  • Serializers registered in type resolver; reader returns BFloat16 (no raw uint16 in public API).
  • Codegen: gen/builder.ts, gen/number.ts, and gen/typedArray.ts updated for bfloat16 and bfloat16_array.

API

  • BFloat16 and BFloat16Array exported from the package.
  • Writer accepts BFloat16 | number for scalar bfloat16; arrays accept BFloat16[] or BFloat16Array.

Tests

  • Scalar: normal values, NaN, Infinity, ±0 round-trip.
  • Array: bfloat16Array with number array and with BFloat16Array.

Checklist

  • BFloat16 type with to_bits/from_bits and float32 conversions
  • BFloat16Array with packed Uint16Array storage
  • Wire format 2 bytes, endianness aligned with float32/float64
  • Type.bfloat16() and Type.bfloat16Array(); resolver and codegen
  • Tests for scalar and array serialization/round-trip

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.

[JavaScript] add bfloat16 and bfloat16_array

1 participant