Skip to content

Instantly share code, notes, and snippets.

@ArvidSilverlock
Last active November 10, 2024 01:35
Show Gist options
  • Select an option

  • Save ArvidSilverlock/e7accb4e0b2092b3536c555156fc5ed6 to your computer and use it in GitHub Desktop.

Select an option

Save ArvidSilverlock/e7accb4e0b2092b3536c555156fc5ed6 to your computer and use it in GitHub Desktop.
Possibly useful `read` and `write` functions for `buffer`s that aren't inluded in the normal API
--[[
Copyright 2024 Arvid
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
]]
-- Possibly useful `read` and `write` functions for `buffer`s that aren't inluded in the normal API
local function writef16(b: buffer, offset: number, value: number)
if value > 65504 then
buffer.writeu16(b, offset, 0b0_11111_0000000000)
elseif value < -65504 then
buffer.writeu16(b, offset, 0b1_11111_0000000000)
elseif value ~= value then
buffer.writeu16(b, offset, 0b1_11111_0000000001)
elseif value == 0 then
buffer.writeu16(b, offset, 0)
else
local absValue = math.abs(value)
local interval = math.ldexp(1, math.floor(math.log(absValue, 2)) - 10)
local roundedValue = math.floor(absValue / interval) * interval
local fraction, unbiasedExponent = math.frexp(roundedValue)
local exponent = unbiasedExponent + 14
-- stylua: ignore start
local mantissa = math.round(if exponent <= 0
then fraction * 0x400 / math.ldexp(1, math.abs(exponent))
else fraction * 0x800) % 0x400
buffer.writeu16(b, offset, mantissa
+ math.max(exponent, 0) * 0x400
+ if value < 0 then 0x8000 else 0)
-- stylua: ignore end
end
end
local function readf16(b: buffer, offset: number): number
local uintForm = buffer.readu16(b, offset)
local mantissa_exponent = uintForm % 0x8000
if mantissa_exponent == 0b11111_0000000000 then
return if uintForm // 0x8000 == 1 then -math.huge else math.huge
elseif mantissa_exponent == 0b11111_0000000001 then
return 0 / 0
elseif mantissa_exponent == 0b00000_0000000000 then
return 0
else
local mantissa = mantissa_exponent % 0x400
local exponent = mantissa_exponent // 0x400
-- stylua: ignore
local fraction = if exponent == 0
then mantissa / 0x400
else mantissa / 0x800 + 0.5
local value = math.ldexp(fraction, exponent - 14)
return if uintForm // 0x8000 == 1 then -value else value
end
end
local function writeu24(b: buffer, offset: number, value: number)
buffer.writeu8(b, offset, value)
buffer.writeu16(b, offset + 1, value // 0x100)
end
local function readu24(b: buffer, offset: number): number
return buffer.readu8(b, offset) + buffer.readu16(b, offset + 1) * 0x100
end
local function writei24(b: buffer, offset: number, value: number)
local uint = (value + 0x1000000) % 0x1000000
buffer.writeu8(b, offset, uint)
buffer.writeu16(b, offset + 1, uint // 0x100)
end
local function readi24(b: buffer, offset: number): number
local uint = buffer.readu8(b, offset) + buffer.readu16(b, offset + 1) * 0x100
return (uint + 0x800000) % 0x1000000 - 0x800000
end
local function writeu48(b: buffer, offset: number, value: number)
buffer.writeu32(b, offset, value)
buffer.writeu16(b, offset + 4, value // 0x100000000)
end
local function readu48(b: buffer, offset: number): number
return buffer.readu32(b, offset) + buffer.readu16(b, offset + 4) * 0x100000000
end
local function writei48(b: buffer, offset: number, value: number)
local uint = (value + 0x1000000000000) % 0x1000000000000
buffer.writeu32(b, offset, uint)
buffer.writeu16(b, offset + 4, uint // 0x100000000)
end
local function readi48(b: buffer, offset: number): number
local uint = buffer.readu32(b, offset) + buffer.readu16(b, offset + 4) * 0x100000000
return (uint + 0x800000000000) % 0x1000000000000 - 0x800000000000
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment