Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: buffer.readbits/writebits #18

Merged
merged 2 commits into from
Dec 14, 2024
Merged

RFC: buffer.readbits/writebits #18

merged 2 commits into from
Dec 14, 2024

Conversation

vegorov-rbx
Copy link
Collaborator

@vegorov-rbx vegorov-rbx commented Dec 23, 2023

Rendered: link

Copy link

@ccuser44 ccuser44 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bump, this would be extremely helpful.

@vegorov-rbx
Copy link
Collaborator Author

Yeah, it took a while in the pipeline with other priorities, but we still remember about this one and it's likely to be accepted.

@aatxe aatxe self-assigned this Dec 14, 2024
@vegorov-rbx vegorov-rbx merged commit 2766e04 into master Dec 14, 2024
@zeux
Copy link
Contributor

zeux commented Dec 19, 2024

Operations are always preformed in little-endian byte order and starting from least significant bits.

This has a typo (performed), but more importantly the spec doesn't specifically have an example for fractional bit offsets. My understanding is that the way this works is:

  • Bytes are numbered from 0 to buffer size - 1
  • Bits within bytes are numbered from 0 (least significant) to 7 (most significant)
  • Bits inside the buffer are numbered from 0 to 8 * buffer size - 1
  • Reads/writes across a byte boundary will write most significant bits of an earlier byte and least significant bits of a later byte, for example:
buffer.writebits(buf, 5, 17, value)

Will take 17 least-significant bits of value, split them in groups of 3, 8, 6 (in least significant order), and overwrite 3 most significant bits to byte 0 (keeping the 5 least significant bits as is), the entire byte 1, and overwrite 6 least significant bits of byte 2 (keeping the 2 most significant bits as is).

... There's probably a cleaner way to describe this but at least one example of boundary straddling would be helpful.

@vegorov-rbx
Copy link
Collaborator Author

That is correct, in a code form example:

  local buf0 = buffer.create(16)
  buffer.writebits(buf0, 5, 17, 0b11111111_11111111_11111111_11111111)

  assert(buffer.readu8(buf0, 0) == 0b11100000)
  assert(buffer.readu8(buf0, 1) == 0b11111111)
  assert(buffer.readu8(buf0, 2) == 0b00111111)
  
  local buf1 = buffer.create(16)
  buffer.fill(buf1, 0, 0xff)
  buffer.writebits(buf1, 5, 17, 0b00000000_00000000_00000000_00000000)

  assert(buffer.readu8(buf1, 0) == 0b00011111)
  assert(buffer.readu8(buf1, 1) == 0b00000000)
  assert(buffer.readu8(buf1, 2) == 0b11000000)

hgoldstein added a commit to luau-lang/luau that referenced this pull request Jan 10, 2025
# General

All code has been re-formatted by `clang-format`; this is not
mechanically enforced, so Luau may go out-of-sync over the course of the
year.

# New Solver

* Track free types interior to a block of code on `Scope`, which should
reduce the number of free types that remain un-generalized after type
checking is complete (e.g.: less errors like `'a <: number is
incompatible with number`).

# Autocomplete

* Fragment autocomplete now does *not* provide suggestions within
comments (matching non-fragment autocomplete behavior).
* Autocomplete now respects iteration and recursion limits (some hangs
will now early exit with a "unification too complex error," some crashes
will now become internal complier exceptions).

# Runtime

* Add a limit to how many Luau codegen slot nodes addresses can be in
use at the same time (fixes #1605, fixes #1558).
* Added constant folding for vector arithmetic (fixes #1553).
* Added support for `buffer.readbits` and `buffer.writebits` (see:
luau-lang/rfcs#18).

---

Co-authored-by: Aaron Weiss <aaronweiss@roblox.com>
Co-authored-by: David Cope <dcope@roblox.com>
Co-authored-by: Hunter Goldstein <hgoldstein@roblox.com>
Co-authored-by: Vighnesh Vijay <vvijay@roblox.com>
Co-authored-by: Vyacheslav Egorov <vegorov@roblox.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants