Skip to content

Conversation

@pavpanchekha
Copy link
Collaborator

@pavpanchekha pavpanchekha commented Jan 19, 2026

Racket 8.15 added flbit-field, which can extract specific bits of a floating-point number as an integer. This allocates less than going through a byte-vector—it doesn't need to allocate at all, depending on how many bits you need—so the math library should use it. Specifically, this PR adds:

  • Use of flbit-field in flonum->ordinal and flonum->bit-field, which avoids allocating an intermediate byte-vector and going straight to an integer
  • Use of flbit-field in flonum->fields, which means that flonum->sig+exp no longer allocates at all
  • A two-chunk computation of flonums-between that avoids any allocation if the output is a fixnum

@pavpanchekha pavpanchekha force-pushed the faster-flonum-operations branch from f7002cd to 75ef5da Compare January 19, 2026 20:34
@pavpanchekha
Copy link
Collaborator Author

You might wonder why we need to micro-optimize this stuff and the answer is that this is something like 2% of all Herbie allocations. Working on the PR.

@pavpanchekha pavpanchekha marked this pull request as draft January 19, 2026 20:36
@pavpanchekha pavpanchekha force-pushed the faster-flonum-operations branch from 75ef5da to cd459c6 Compare January 19, 2026 20:37
@samth
Copy link
Member

samth commented Jan 19, 2026

The require/typed will be pretty slow here, we should just fix that in TR directly.

@pavpanchekha
Copy link
Collaborator Author

pavpanchekha commented Jan 19, 2026

How does one do that? In theory there's an unsound version of flbit-field which the compiler could produce that would be super fast.

@pavpanchekha pavpanchekha marked this pull request as ready for review January 19, 2026 21:23
@samth
Copy link
Member

samth commented Jan 19, 2026

@pavpanchekha
Copy link
Collaborator Author

Done in racket/typed-racket#1484. How do we typically do this? Do we merge that and then remove the require/typed here and just expect it all to go in at once?

@samth
Copy link
Member

samth commented Jan 19, 2026

We should just merge the TR PR (possibly with a version bump) and then merge this one without the require/typed.

We could also add an optimization to transform flbit-field to unsafe-flbit-field when we know that the two fixnum arguments are in [0,63] (because they're usually going to be constants).

@pavpanchekha
Copy link
Collaborator Author

Yeah, I couldn't (in a cursory look) figure out how the optimization stuff worked so I didn't touch that, but yes, in practice they'll always be constants so one could detect the results and optimize. In theory you could also detect constants and use that for type checking (to assert that it's an Index if the bounds are tight enough), dunno if that gets you anything but maybe.

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.

3 participants