hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit 4b4db93c39dd5376aa662931864c2d8018ae728e
parent 88bda176d9875340a95c1c5acb156d8ff35e298d
Author: Drew DeVault <sir@cmpwn.com>
Date:   Fri, 26 Mar 2021 09:29:26 -0400

endian: expand big endian support

Diffstat:
Mendian/big.ha | 31++++++++++++++++++++++++++++++-
1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/endian/big.ha b/endian/big.ha @@ -1,6 +1,28 @@ +// Reads a u16 from a buffer in big-endian order. +export fn begetu16(buf: []u8) u16 = { + return + (buf[0] << 8u16) | + (buf[1] << 0); +}; + +// Writes a u16 into a buffer in big-endian order. +export fn beputu16(buf: []u8, in: u16) void = { + buf[1] = (in >> 0): u8; + buf[0] = (in >> 8): u8; +}; + +// Reads a u32 from a buffer in big-endian order. +export fn begetu32(buf: []u8) u32 = { + return + (buf[0] << 24u32) | + (buf[1] << 16u32) | + (buf[2] << 8u32) | + (buf[3] << 0); +}; + // Writes a u32 into a buffer in big-endian order. export fn beputu32(buf: []u8, in: u32) void = { - buf[3] = (in): u8; + buf[3] = (in >> 0): u8; buf[2] = (in >> 8): u8; buf[1] = (in >> 16): u8; buf[0] = (in >> 24): u8; @@ -20,12 +42,19 @@ export fn beputu64(buf: []u8, in: u64) void = { @test fn big() void = { let buf: [8]u8 = [0...]; + beputu16(buf, 0x1234); + assert(buf[0] == 0x12 && buf[1] == 0x34); + assert(begetu16(buf) == 0x1234); + beputu32(buf, 0x12345678); assert(buf[0] == 0x12 && buf[1] == 0x34 && buf[2] == 0x56 && buf[3] == 0x78); + assert(begetu32(buf) == 0x12345678); + beputu64(buf, 0x1234567887654321); assert(buf[0] == 0x12 && buf[1] == 0x34 && buf[2] == 0x56 && buf[3] == 0x78 && buf[4] == 0x87 && buf[5] == 0x65 && buf[6] == 0x43 && buf[7] == 0x21); + // TODO: begetu64 (has some qbe issues?) };