Skip to content

Conversation

@derek-miller
Copy link
Contributor

This pull request introduces raw_* methods for bitwise operations in the bit32 and bit64 modules, allowing zero-overhead access to native bit library functions across different Lua versions. The addition includes:

  • New functions for bit32: raw_band, raw_bor, raw_bxor, raw_bnot, raw_lshift, raw_rshift, raw_arshift, raw_rol, raw_ror, and raw_add.
  • New functions for bit64: raw_band, raw_bor, raw_bxor, raw_bnot, raw_lshift, raw_rshift, raw_arshift, raw_rol, raw_ror, and raw_add.

Implementation details:

  • On LuaJIT: Direct references to bit.* for zero overhead.
  • On Lua 5.3+: Native operators with 32-bit masking for shifts.
  • On Lua 5.2: Direct references to bit32.*.
  • On Pure Lua: Falls back to safe implementations.

Note: Behavior for shift amounts beyond bounds (e.g., >=32 or >=64) varies across Lua implementations. It is the caller's responsibility to ensure valid ranges for such amounts.

@derek-miller derek-miller force-pushed the feature/raw-operations branch 4 times, most recently from 4590584 to 79feab7 Compare January 29, 2026 04:22
…erations

This adds raw_* variants of all bit operations that bypass the to_unsigned()
wrapper on LuaJIT, providing direct access to native bit library functions.
For XOR, AND, OR, and rotate operations, the signedness doesn't matter since
bit patterns are identical.

New functions in bit32:
- raw_band, raw_bor, raw_bxor, raw_bnot
- raw_lshift, raw_rshift, raw_arshift
- raw_rol, raw_ror
- raw_add

New functions in bit64:
- raw_band, raw_bor, raw_bxor, raw_bnot
- raw_lshift, raw_rshift, raw_arshift
- raw_rol, raw_ror
- raw_add

Key implementation details:
- On LuaJIT: raw_* are direct references to bit.* functions (zero overhead)
- On Lua 5.3+: raw_* use native operators with 32-bit masking for shifts
- On Lua 5.2: raw_* are direct references to bit32.* (already unsigned)
- On Pure Lua: raw_* fall back to safe implementations (no native library)
- raw_rol/raw_ror fall back to computed versions on non-LuaJIT (not nil)

Note: Shift amounts >= 32 (or >= 64 for bit64) have platform-specific behavior.
LuaJIT wraps (n % 32), Lua 5.3+ returns 0. Callers should keep n in valid range.
@derek-miller derek-miller force-pushed the feature/raw-operations branch from 79feab7 to 4fa60a0 Compare January 29, 2026 04:28
@derek-miller derek-miller merged commit ea30c87 into main Jan 29, 2026
8 checks passed
@derek-miller derek-miller deleted the feature/raw-operations branch January 29, 2026 17:49
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.

2 participants