number: Float64.bits(), ofBits(_) and the same for Float32 #348

Closed
opened 2026-10-12 21:07:57 +00:00 by orion · 4 comments
Collaborator

Context

std/binary computes the IEEE 754 bit patterns of floats in TorbScript from exact powers of two (std/binary/src/number.trb, float64Pattern, float32Pattern, ...), which is slow and loses a nan's payload (BINARY.md decision 3). A native bit cast keeps both. This is slice 2 of the Float32 work (slice 1: arithmetic, #35).

Source

Acceptance criteria

  • Float64.bits(): UInt64, Float64.ofBits(_ bits: UInt64): Float64, Float32.bits(): UInt32, Float32.ofBits(_ bits: UInt32): Float32 in std/number, a bit cast (intrinsic or a memcpy-style runtime function) in the C back end and the VM, identical in both.
  • Round trips: ofBits(x.bits()) has the bits of x for every pattern tried, including -0.0, subnormals, infinities and nans with a payload (0x7ff0000000000001, 0x7fa00000); 1.0.bits() == 0x3ff0000000000000, Float32 1.0 is 0x3f800000.
  • Conformance programs in tests/conformance/ for both widths and both back ends; the constant evaluator does not fold a bit cast of a nan into something a Float cannot hold.
  • Docs: std/number doc comments, docs/design/BINARY.md section 10 marks the natives built, skills/ regenerated.

Design record point

BINARY.md section 10 (names bits()/ofBits(_) and the four types).

Seed impact

Yes: four new natives, one or two commits. std/binary does not use them in this slice (that is the migration slice, after the seed refresh).

Dependencies

Blocked by slice 1 (#35): Float32 has to compute before its bits are worth reading.

## Context `std/binary` computes the IEEE 754 bit patterns of floats in TorbScript from exact powers of two (`std/binary/src/number.trb`, `float64Pattern`, `float32Pattern`, ...), which is slow and loses a `nan`'s payload (BINARY.md decision 3). A native bit cast keeps both. This is slice 2 of the `Float32` work (slice 1: arithmetic, #35). ## Source - [BINARY.md section 10](https://git.torb.dev/torbscript/language/blob/393ef2608e4a60b8ff76d8b6b408951b822d887d/docs/design/BINARY.md#L237), "`Float32` and the bit patterns" - [BINARY.md decision 3](https://git.torb.dev/torbscript/language/blob/393ef2608e4a60b8ff76d8b6b408951b822d887d/docs/design/BINARY.md#L208) ## Acceptance criteria - [ ] `Float64.bits(): UInt64`, `Float64.ofBits(_ bits: UInt64): Float64`, `Float32.bits(): UInt32`, `Float32.ofBits(_ bits: UInt32): Float32` in `std/number`, a bit cast (intrinsic or a `memcpy`-style runtime function) in the C back end and the VM, identical in both. - [ ] Round trips: `ofBits(x.bits())` has the bits of `x` for every pattern tried, including `-0.0`, subnormals, infinities and `nan`s with a payload (`0x7ff0000000000001`, `0x7fa00000`); `1.0.bits() == 0x3ff0000000000000`, `Float32` `1.0` is `0x3f800000`. - [ ] Conformance programs in `tests/conformance/` for both widths and both back ends; the constant evaluator does not fold a bit cast of a `nan` into something a `Float` cannot hold. - [ ] Docs: `std/number` doc comments, `docs/design/BINARY.md` section 10 marks the natives built, `skills/` regenerated. ## Design record point BINARY.md section 10 (names `bits()`/`ofBits(_)` and the four types). ## Seed impact Yes: four new natives, one or two commits. `std/binary` does not use them in this slice (that is the migration slice, after the seed refresh). ## Dependencies Blocked by slice 1 (#35): `Float32` has to compute before its bits are worth reading.
Author
Collaborator

[loop:torbscript] decided by the loop: slice 2 of #35, names bits()/ofBits(_) and the result types UInt64/UInt32 follow BINARY.md section 10 and the width types of std/number. Not ready until #35 is merged (dependency set in the forge). next: waiting (blocked by #35)

[loop:torbscript] decided by the loop: slice 2 of #35, names `bits()`/`ofBits(_)` and the result types `UInt64`/`UInt32` follow BINARY.md section 10 and the width types of `std/number`. Not `ready` until #35 is merged (dependency set in the forge). next: waiting (blocked by #35)
Author
Collaborator

[loop:torbscript] refine: dependency #35 landed (PR #352), label ready set, next: implement

[loop:torbscript] refine: dependency #35 landed (PR #352), label `ready` set, next: implement
Author
Collaborator

[loop:torbscript] claim implement by session:loop-torbscript-20261013 until 2026-10-13T09:46Z

[loop:torbscript] claim implement by session:loop-torbscript-20261013 until 2026-10-13T09:46Z
Author
Collaborator

[loop:torbscript] claim fix by session:loop-torbscript-20261013 until 2026-10-13T13:04Z

[loop:torbscript] claim fix by session:loop-torbscript-20261013 until 2026-10-13T13:04Z
argus closed this issue 2026-10-13 07:53:14 +00:00
Sign in to join this conversation.
No labels
loop
ready
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
torbscript/language#348
No description provided.