commit 6ac28a0879780a7881e6d3e1eaee8f99e94aa2fc
parent 6049e2c788b9f6d1784d02fbe1433b43c5722f94
Author: Armin Weigl <tb46305@gmail.com>
Date: Fri, 26 Mar 2021 20:23:42 +0100
endian: *getu64
Diffstat:
3 files changed, 31 insertions(+), 1 deletion(-)
diff --git a/endian/big.ha b/endian/big.ha
@@ -28,6 +28,19 @@ export fn beputu32(buf: []u8, in: u32) void = {
buf[0] = (in >> 24): u8;
};
+// Reads a u64 from a buffer in big-endian order.
+export fn begetu64(buf: []u8) u64 = {
+ return
+ (buf[0] << 56u64) |
+ (buf[1] << 48u64) |
+ (buf[2] << 40u64) |
+ (buf[3] << 32u64) |
+ (buf[4] << 24u64) |
+ (buf[5] << 16u64) |
+ (buf[6] << 8u64) |
+ (buf[7] << 0);
+};
+
// Writes a u64 into a buffer in big-endian order.
export fn beputu64(buf: []u8, in: u64) void = {
buf[7] = (in >> 0): u8;
@@ -56,5 +69,5 @@ export fn beputu64(buf: []u8, in: u64) void = {
&& buf[2] == 0x56 && buf[3] == 0x78
&& buf[4] == 0x87 && buf[5] == 0x65
&& buf[6] == 0x43 && buf[7] == 0x21);
- // TODO: begetu64 (has some qbe issues?)
+ assert(begetu64(buf) == 0x1234567887654321);
};
diff --git a/endian/endian.ha b/endian/endian.ha
@@ -4,6 +4,7 @@ export type endian = struct {
putu16: *fn(buf: []u8, in: u16) void,
getu32: *fn(buf: []u8) u32,
putu32: *fn(buf: []u8, in: u32) void,
+ getu64: *fn(buf: []u8) u64,
putu64: *fn(buf: []u8, in: u64) void,
};
@@ -13,6 +14,7 @@ export const big: endian = endian {
putu16 = &beputu16,
getu32 = &begetu32,
putu32 = &beputu32,
+ getu64 = &begetu64,
putu64 = &beputu64,
};
@@ -22,5 +24,6 @@ export const little: endian = endian {
putu16 = &leputu16,
getu32 = &legetu32,
putu32 = &leputu32,
+ getu64 = &legetu64,
putu64 = &leputu64,
};
diff --git a/endian/little.ha b/endian/little.ha
@@ -28,6 +28,19 @@ export fn leputu32(buf: []u8, in: u32) void = {
buf[3] = (in >> 24): u8;
};
+// Reads a u64 from a buffer in little-endian order.
+export fn legetu64(buf: []u8) u64 = {
+ return
+ (buf[7] << 56u64) |
+ (buf[6] << 48u64) |
+ (buf[5] << 40u64) |
+ (buf[4] << 32u64) |
+ (buf[3] << 24u64) |
+ (buf[2] << 16u64) |
+ (buf[1] << 8u64) |
+ (buf[0] << 0);
+};
+
// Writes a u64 into a buffer in little-endian order.
export fn leputu64(buf: []u8, in: u64) void = {
buf[0] = (in >> 0): u8;
@@ -56,4 +69,5 @@ export fn leputu64(buf: []u8, in: u64) void = {
&& buf[2] == 0x65 && buf[3] == 0x87
&& buf[4] == 0x78 && buf[5] == 0x56
&& buf[6] == 0x34 && buf[7] == 0x12);
+ assert(legetu64(buf) == 0x1234567887654321);
};