commit 1c1287bd183027172b7d7ccebd5cb5c55c4309b6 parent 62eee8d4f453be39abe95e6a59232d7930923012 Author: Drew DeVault <sir@cmpwn.com> Date: Fri, 26 Feb 2021 12:28:40 -0500 endian: new module (WIP) Diffstat:
A | endian/big.ha | | | 31 | +++++++++++++++++++++++++++++++ |
1 file changed, 31 insertions(+), 0 deletions(-)
diff --git a/endian/big.ha b/endian/big.ha @@ -0,0 +1,31 @@ +// Writes a u32 into a buffer in big-endian order. +export fn beputu32(buf: []u8, in: u32) void = { + buf[3] = (in): u8; + buf[2] = (in >> 8): u8; + buf[1] = (in >> 16): u8; + buf[0] = (in >> 24): u8; +}; + +// Writes a u64 into a buffer in big-endian order. +export fn beputu64(buf: []u8, in: u64) void = { + buf[7] = (in >> 0): u8; + buf[6] = (in >> 8): u8; + buf[5] = (in >> 16): u8; + buf[4] = (in >> 24): u8; + buf[3] = (in >> 32): u8; + buf[2] = (in >> 40): u8; + buf[1] = (in >> 48): u8; + buf[0] = (in >> 56): u8; +}; + +@test fn big() void = { + let buf: [8]u8 = [0...]; + beputu32(buf, 0x12345678); + assert(buf[0] == 0x12 && buf[1] == 0x34 + && buf[2] == 0x56 && buf[3] == 0x78); + 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); +};