From 3caa1b7297e2959633adb39f1422765aba8a4d50 Mon Sep 17 00:00:00 2001 From: JalonSolov Date: Sun, 30 Nov 2025 13:17:10 -0500 Subject: [PATCH] encoding.binary: use unions for small speed increase and readability (#25867) --- vlib/encoding/binary/big_endian.v | 201 ++++++++++++++------ vlib/encoding/binary/big_endian_fixed.v | 74 ++++++-- vlib/encoding/binary/little_endian.v | 207 +++++++++++++++------ vlib/encoding/binary/little_endian_fixed.v | 74 ++++++-- vlib/encoding/binary/unions.v | 30 +++ 5 files changed, 440 insertions(+), 146 deletions(-) create mode 100644 vlib/encoding/binary/unions.v diff --git a/vlib/encoding/binary/big_endian.v b/vlib/encoding/binary/big_endian.v index 9978da06f..a9b5d50f8 100644 --- a/vlib/encoding/binary/big_endian.v +++ b/vlib/encoding/binary/big_endian.v @@ -7,7 +7,15 @@ module binary @[direct_array_access; inline] pub fn big_endian_u16(b []u8) u16 { _ = b[1] // bounds check - return u16(b[1]) | (u16(b[0]) << u16(8)) + unsafe { + mut u := U16{} + $if big_endian { + u.b[0], u.b[1] = b[0], b[1] + } $else { + u.b[0], u.b[1] = b[1], b[0] + } + return u.u + } } // big_endian_u16_at creates a u16 from two bytes in the array b at the specified offset in big endian order. @@ -15,7 +23,15 @@ pub fn big_endian_u16(b []u8) u16 { pub fn big_endian_u16_at(b []u8, o int) u16 { _ = b[o] // bounds check _ = b[o + 1] // bounds check - return u16(b[o + 1]) | (u16(b[o]) << u16(8)) + unsafe { + mut u := U16{} + $if big_endian { + u.b[0], u.b[1] = b[o], b[o + 1] + } $else { + u.b[0], u.b[1] = b[o + 1], b[o] + } + return u.u + } } // big_endian_u16_end creates a u16 from two bytes in the array b at the specified offset in big endian order. @@ -28,8 +44,16 @@ pub fn big_endian_u16_end(b []u8) u16 { @[direct_array_access; inline] pub fn big_endian_put_u16(mut b []u8, v u16) { _ = b[1] // bounds check - b[0] = u8(v >> u16(8)) - b[1] = u8(v) + unsafe { + mut u := U16{ + u: v + } + $if big_endian { + b[0], b[1] = u.b[0], u.b[1] + } $else { + b[0], b[1] = u.b[1], u.b[0] + } + } } // big_endian_put_u16_at writes a u16 to the two bytes in the array b at the specified offset in big endian order. @@ -37,8 +61,16 @@ pub fn big_endian_put_u16(mut b []u8, v u16) { pub fn big_endian_put_u16_at(mut b []u8, v u16, o int) { _ = b[o] // bounds check _ = b[o + 1] // bounds check - b[o] = u8(v >> u16(8)) - b[o + 1] = u8(v) + unsafe { + mut u := U16{ + u: v + } + $if big_endian { + b[o], b[o + 1] = u.b[0], u.b[1] + } $else { + b[o], b[o + 1] = u.b[1], u.b[0] + } + } } // big_endian_put_u16_end writes a u16 to the last two bytes in the array b in big endian order. @@ -48,18 +80,32 @@ pub fn big_endian_put_u16_end(mut b []u8, v u16) { } // big_endian_get_u16 creates u8 array from the unsigned 16-bit integer v in big endian order. +@[direct_array_access; inline] pub fn big_endian_get_u16(v u16) []u8 { - mut b := []u8{len: 2} - b[0] = u8(v >> u16(8)) - b[1] = u8(v) - return b + unsafe { + mut u := U16{ + u: v + } + $if little_endian { + u.b[0], u.b[1] = u.b[1], u.b[0] + } + return u.b[..] + } } // big_endian_u32 creates a u32 from four bytes in the array b in big endian order. @[direct_array_access; inline] pub fn big_endian_u32(b []u8) u32 { _ = b[3] // bounds check - return u32(b[3]) | (u32(b[2]) << u32(8)) | (u32(b[1]) << u32(16)) | (u32(b[0]) << u32(24)) + unsafe { + mut u := U32{} + $if big_endian { + u.b[0], u.b[1], u.b[2], u.b[3] = b[0], b[1], b[2], b[3] + } $else { + u.b[0], u.b[1], u.b[2], u.b[3] = b[3], b[2], b[1], b[0] + } + return u.u + } } // big_endian_u32_at creates a u32 from four bytes in the array b at the specified offset in big endian order. @@ -67,7 +113,15 @@ pub fn big_endian_u32(b []u8) u32 { pub fn big_endian_u32_at(b []u8, o int) u32 { _ = b[o] // bounds check _ = b[o + 3] // bounds check - return u32(b[o + 3]) | (u32(b[o + 2]) << u32(8)) | (u32(b[o + 1]) << u32(16)) | (u32(b[o]) << u32(24)) + unsafe { + mut u := U32{} + $if big_endian { + u.b[0], u.b[1], u.b[2], u.b[3] = b[o], b[o + 1], b[o + 2], b[o + 3] + } $else { + u.b[0], u.b[1], u.b[2], u.b[3] = b[o + 3], b[o + 2], b[o + 1], b[o] + } + return u.u + } } // big_endian_u32_end creates a u32 from the last four bytes in the array b in big endian order. @@ -80,10 +134,16 @@ pub fn big_endian_u32_end(b []u8) u32 { @[direct_array_access; inline] pub fn big_endian_put_u32(mut b []u8, v u32) { _ = b[3] // bounds check - b[0] = u8(v >> u32(24)) - b[1] = u8(v >> u32(16)) - b[2] = u8(v >> u32(8)) - b[3] = u8(v) + unsafe { + mut u := U32{ + u: v + } + $if big_endian { + b[0], b[1], b[2], b[3] = u.b[0], u.b[1], u.b[2], u.b[3] + } $else { + b[0], b[1], b[2], b[3] = u.b[3], u.b[2], u.b[1], u.b[0] + } + } } // big_endian_put_u32_at writes a u32 to four bytes in the array b at the specified offset in big endian order. @@ -91,10 +151,16 @@ pub fn big_endian_put_u32(mut b []u8, v u32) { pub fn big_endian_put_u32_at(mut b []u8, v u32, o int) { _ = b[o] // bounds check _ = b[o + 3] // bounds check - b[o] = u8(v >> u32(24)) - b[o + 1] = u8(v >> u32(16)) - b[o + 2] = u8(v >> u32(8)) - b[o + 3] = u8(v) + unsafe { + mut u := U32{ + u: v + } + $if big_endian { + b[o], b[o + 1], b[o + 2], b[o + 3] = u.b[0], u.b[1], u.b[2], u.b[3] + } $else { + b[o], b[o + 1], b[o + 2], b[o + 3] = u.b[3], u.b[2], u.b[1], u.b[0] + } + } } // big_endian_put_u32_end writes a u32 to the last four bytes in the array b in big endian order. @@ -104,20 +170,32 @@ pub fn big_endian_put_u32_end(mut b []u8, v u32) { } // big_endian_get_u32 creates u8 array from the unsigned 32-bit integer v in big endian order. +@[direct_array_access; inline] pub fn big_endian_get_u32(v u32) []u8 { - mut b := []u8{len: 4} - b[0] = u8(v >> u32(24)) - b[1] = u8(v >> u32(16)) - b[2] = u8(v >> u32(8)) - b[3] = u8(v) - return b + unsafe { + mut u := U32{ + u: v + } + $if little_endian { + u.b[0], u.b[1], u.b[2], u.b[3] = u.b[3], u.b[2], u.b[1], u.b[0] + } + return u.b[..] + } } // big_endian_u64 creates a u64 from the first eight bytes in the array b in big endian order. @[direct_array_access; inline] pub fn big_endian_u64(b []u8) u64 { _ = b[7] // bounds check - return u64(b[7]) | (u64(b[6]) << u64(8)) | (u64(b[5]) << u64(16)) | (u64(b[4]) << u64(24)) | (u64(b[3]) << u64(32)) | (u64(b[2]) << u64(40)) | (u64(b[1]) << u64(48)) | (u64(b[0]) << u64(56)) + unsafe { + mut u := U64{} + $if big_endian { + u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] = b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7] + } $else { + u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] = b[7], b[6], b[5], b[4], b[3], b[2], b[1], b[0] + } + return u.u + } } // big_endian_u64_at creates a u64 from eight bytes in the array b at the specified offset in big endian order. @@ -125,8 +203,17 @@ pub fn big_endian_u64(b []u8) u64 { pub fn big_endian_u64_at(b []u8, o int) u64 { _ = b[o] // bounds check _ = b[o + 7] // bounds check - return u64(b[o + 7]) | (u64(b[o + 6]) << u64(8)) | (u64(b[o + 5]) << u64(16)) | (u64(b[o + 4]) << u64(24)) | (u64(b[ - o + 3]) << u64(32)) | (u64(b[o + 2]) << u64(40)) | (u64(b[o + 1]) << u64(48)) | (u64(b[o]) << u64(56)) + unsafe { + mut u := U64{} + $if big_endian { + u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] = b[o], b[o + 1], b[o + 2], b[ + o + 3], b[o + 4], b[o + 5], b[o + 6], b[o + 7] + } $else { + u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] = b[o + 7], b[o + 6], b[ + o + 5], b[o + 4], b[o + 3], b[o + 2], b[o + 1], b[o] + } + return u.u + } } // big_endian_u64_end creates a u64 from the last eight bytes in the array b in big endian order. @@ -139,14 +226,16 @@ pub fn big_endian_u64_end(b []u8) u64 { @[direct_array_access; inline] pub fn big_endian_put_u64(mut b []u8, v u64) { _ = b[7] // bounds check - b[0] = u8(v >> u64(56)) - b[1] = u8(v >> u64(48)) - b[2] = u8(v >> u64(40)) - b[3] = u8(v >> u64(32)) - b[4] = u8(v >> u64(24)) - b[5] = u8(v >> u64(16)) - b[6] = u8(v >> u64(8)) - b[7] = u8(v) + unsafe { + mut u := U64{ + u: v + } + $if big_endian { + b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7] = u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] + } $else { + b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7] = u.b[7], u.b[6], u.b[5], u.b[4], u.b[3], u.b[2], u.b[1], u.b[0] + } + } } // big_endian_put_u64_at writes a u64 to eight bytes in the array b at the specified offset in big endian order. @@ -154,14 +243,16 @@ pub fn big_endian_put_u64(mut b []u8, v u64) { pub fn big_endian_put_u64_at(mut b []u8, v u64, o int) { _ = b[o] // bounds check _ = b[o + 7] // bounds check - b[o] = u8(v >> u64(56)) - b[o + 1] = u8(v >> u64(48)) - b[o + 2] = u8(v >> u64(40)) - b[o + 3] = u8(v >> u64(32)) - b[o + 4] = u8(v >> u64(24)) - b[o + 5] = u8(v >> u64(16)) - b[o + 6] = u8(v >> u64(8)) - b[o + 7] = u8(v) + unsafe { + mut u := U64{ + u: v + } + $if big_endian { + b[o], b[o + 1], b[o + 2], b[o + 3], b[o + 4], b[o + 5], b[o + 6], b[o + 7] = u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] + } $else { + b[o], b[o + 1], b[o + 2], b[o + 3], b[o + 4], b[o + 5], b[o + 6], b[o + 7] = u.b[7], u.b[6], u.b[5], u.b[4], u.b[3], u.b[2], u.b[1], u.b[0] + } + } } // big_endian_put_u64_end writes a u64 to the last eight bytes in the array b at the specified offset in big endian order. @@ -171,15 +262,15 @@ pub fn big_endian_put_u64_end(mut b []u8, v u64) { } // big_endian_get_u64 creates u8 array from the unsigned 64-bit integer v in big endian order. +@[direct_array_access; inline] pub fn big_endian_get_u64(v u64) []u8 { - mut b := []u8{len: 8} - b[0] = u8(v >> u64(56)) - b[1] = u8(v >> u64(48)) - b[2] = u8(v >> u64(40)) - b[3] = u8(v >> u64(32)) - b[4] = u8(v >> u64(24)) - b[5] = u8(v >> u64(16)) - b[6] = u8(v >> u64(8)) - b[7] = u8(v) - return b + unsafe { + mut u := U64{ + u: v + } + $if little_endian { + u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] = u.b[7], u.b[6], u.b[5], u.b[4], u.b[3], u.b[2], u.b[1], u.b[0] + } + return u.b[..] + } } diff --git a/vlib/encoding/binary/big_endian_fixed.v b/vlib/encoding/binary/big_endian_fixed.v index 7ca8b46bb..e4849bb07 100644 --- a/vlib/encoding/binary/big_endian_fixed.v +++ b/vlib/encoding/binary/big_endian_fixed.v @@ -6,46 +6,86 @@ module binary // big_endian_u16_fixed creates a u16 from the first two bytes in the fixed array b in big endian order. @[direct_array_access; inline] pub fn big_endian_u16_fixed(b [2]u8) u16 { - return u16(b[1]) | (u16(b[0]) << u16(8)) + unsafe { + mut u := U16{} + $if big_endian { + u.b[0], u.b[1] = b[0], b[1] + } $else { + u.b[0], u.b[1] = b[1], b[0] + } + return u.u + } } // big_endian_put_u16_fixed writes a u16 to the fixed array b in big endian order. @[direct_array_access; inline] pub fn big_endian_put_u16_fixed(mut b [2]u8, v u16) { - b[0] = u8(v >> u16(8)) - b[1] = u8(v) + unsafe { + mut u := U16{ + u: v + } + $if big_endian { + b[0], b[1] = u.b[0], u.b[1] + } $else { + b[0], b[1] = u.b[1], u.b[0] + } + } } // big_endian_u32_fixed creates a u32 from four bytes in the fixed array b in big endian order. @[direct_array_access; inline] pub fn big_endian_u32_fixed(b [4]u8) u32 { - return u32(b[3]) | (u32(b[2]) << u32(8)) | (u32(b[1]) << u32(16)) | (u32(b[0]) << u32(24)) + unsafe { + mut u := U32{} + $if big_endian { + u.b[0], u.b[1], u.b[2], u.b[3] = b[0], b[1], b[2], b[3] + } $else { + u.b[0], u.b[1], u.b[2], u.b[3] = b[3], b[2], b[1], b[0] + } + return u.u + } } // big_endian_put_u32_fixed writes a u32 to the fixed array b in big endian order. @[direct_array_access; inline] pub fn big_endian_put_u32_fixed(mut b [4]u8, v u32) { - b[0] = u8(v >> u32(24)) - b[1] = u8(v >> u32(16)) - b[2] = u8(v >> u32(8)) - b[3] = u8(v) + unsafe { + mut u := U32{ + u: v + } + $if big_endian { + b[0], b[1], b[2], b[3] = u.b[0], u.b[1], u.b[2], u.b[3] + } $else { + b[0], b[1], b[2], b[3] = u.b[3], u.b[2], u.b[1], u.b[0] + } + } } // big_endian_u64_fixed creates a u64 from the fixed array b in big endian order. @[direct_array_access; inline] pub fn big_endian_u64_fixed(b [8]u8) u64 { - return u64(b[7]) | (u64(b[6]) << u64(8)) | (u64(b[5]) << u64(16)) | (u64(b[4]) << u64(24)) | (u64(b[3]) << u64(32)) | (u64(b[2]) << u64(40)) | (u64(b[1]) << u64(48)) | (u64(b[0]) << u64(56)) + unsafe { + mut u := U64{} + $if big_endian { + u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] = b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7] + } $else { + u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] = b[7], b[6], b[5], b[4], b[3], b[2], b[1], b[0] + } + return u.u + } } // big_endian_put_u64_fixed writes a u64 to the fixed array b in big endian order. @[direct_array_access; inline] pub fn big_endian_put_u64_fixed(mut b [8]u8, v u64) { - b[0] = u8(v >> u64(56)) - b[1] = u8(v >> u64(48)) - b[2] = u8(v >> u64(40)) - b[3] = u8(v >> u64(32)) - b[4] = u8(v >> u64(24)) - b[5] = u8(v >> u64(16)) - b[6] = u8(v >> u64(8)) - b[7] = u8(v) + unsafe { + mut u := U64{ + u: v + } + $if big_endian { + b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7] = u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] + } $else { + b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7] = u.b[7], u.b[6], u.b[5], u.b[4], u.b[3], u.b[2], u.b[1], u.b[0] + } + } } diff --git a/vlib/encoding/binary/little_endian.v b/vlib/encoding/binary/little_endian.v index db8a144d3..d45943bef 100644 --- a/vlib/encoding/binary/little_endian.v +++ b/vlib/encoding/binary/little_endian.v @@ -7,7 +7,15 @@ module binary @[direct_array_access; inline] pub fn little_endian_u16(b []u8) u16 { _ = b[1] // bounds check - return u16(b[0]) | (u16(b[1]) << u16(8)) + unsafe { + mut u := U16{} + $if little_endian { + u.b[0], u.b[1] = b[0], b[1] + } $else { + u.b[0], u.b[1] = b[1], b[0] + } + return u.u + } } // little_endian_u16_at creates a u16 from two bytes in the array b at the specified offset in little endian order. @@ -15,7 +23,15 @@ pub fn little_endian_u16(b []u8) u16 { pub fn little_endian_u16_at(b []u8, o int) u16 { _ = b[o] // bounds check _ = b[o + 1] // bounds check - return u16(b[o]) | (u16(b[o + 1]) << u16(8)) + unsafe { + mut u := U16{} + $if little_endian { + u.b[0], u.b[1] = b[o], b[o + 1] + } $else { + u.b[0], u.b[1] = b[o + 1], b[o] + } + return u.u + } } // little_endian_u16_end creates a u16 from the last two bytes of the array b in little endian order. @@ -28,8 +44,16 @@ pub fn little_endian_u16_end(b []u8) u16 { @[direct_array_access; inline] pub fn little_endian_put_u16(mut b []u8, v u16) { _ = b[1] // bounds check - b[0] = u8(v) - b[1] = u8(v >> u16(8)) + unsafe { + mut u := U16{ + u: v + } + $if little_endian { + b[0], b[1] = u.b[0], u.b[1] + } $else { + b[0], b[1] = u.b[1], u.b[0] + } + } } // little_endian_put_u16_at writes a u16 to the two bytes in the array b at the specified offset in little endian order. @@ -37,8 +61,16 @@ pub fn little_endian_put_u16(mut b []u8, v u16) { pub fn little_endian_put_u16_at(mut b []u8, v u16, o int) { _ = b[o] // bounds check _ = b[o + 1] // bounds check - b[o] = u8(v) - b[o + 1] = u8(v >> u16(8)) + unsafe { + mut u := U16{ + u: v + } + $if little_endian { + b[o], b[o + 1] = u.b[0], u.b[1] + } $else { + b[o], b[o + 1] = u.b[1], u.b[0] + } + } } // little_endian_put_u16_end writes a u16 to the last two bytes of the array b in little endian order. @@ -49,17 +81,30 @@ pub fn little_endian_put_u16_end(mut b []u8, v u16) { // little_endian_get_u16 creates u8 array from the unsigned 16-bit integer v in little endian order. pub fn little_endian_get_u16(v u16) []u8 { - mut b := []u8{len: 2} - b[0] = u8(v) - b[1] = u8(v >> u16(8)) - return b + unsafe { + mut u := U16{ + u: v + } + $if big_endian { + u.b[0], u.b[1] = u.b[1], u.b[0] + } + return u.b[..] + } } // little_endian_u32 creates a u32 from the first four bytes in the array b in little endian order. @[direct_array_access; inline] pub fn little_endian_u32(b []u8) u32 { _ = b[3] // bounds check - return u32(b[0]) | (u32(b[1]) << u32(8)) | (u32(b[2]) << u32(16)) | (u32(b[3]) << u32(24)) + unsafe { + mut u := U32{} + $if little_endian { + u.b[0], u.b[1], u.b[2], u.b[3] = b[0], b[1], b[2], b[3] + } $else { + u.b[0], u.b[1], u.b[2], u.b[3] = b[3], b[2], b[1], b[0] + } + return u.u + } } // little_endian_u32_at creates a u32 from four bytes in the array b at the specified offset in little endian order. @@ -67,7 +112,15 @@ pub fn little_endian_u32(b []u8) u32 { pub fn little_endian_u32_at(b []u8, o int) u32 { _ = b[o] // bounds check _ = b[o + 3] // bounds check - return u32(b[o]) | (u32(b[o + 1]) << u32(8)) | (u32(b[o + 2]) << u32(16)) | (u32(b[o + 3]) << u32(24)) + unsafe { + mut u := U32{} + $if little_endian { + u.b[0], u.b[1], u.b[2], u.b[3] = b[o], b[o + 1], b[o + 2], b[o + 3] + } $else { + u.b[0], u.b[1], u.b[2], u.b[3] = b[o + 3], b[o + 2], b[o + 1], b[o] + } + return u.u + } } // little_endian_u32_end creates a u32 from the last four bytes in the array b in little endian order. @@ -80,10 +133,16 @@ pub fn little_endian_u32_end(b []u8) u32 { @[direct_array_access; inline] pub fn little_endian_put_u32(mut b []u8, v u32) { _ = b[3] // bounds check - b[0] = u8(v) - b[1] = u8(v >> u32(8)) - b[2] = u8(v >> u32(16)) - b[3] = u8(v >> u32(24)) + unsafe { + mut u := U32{ + u: v + } + $if little_endian { + b[0], b[1], b[2], b[3] = u.b[0], u.b[1], u.b[2], u.b[3] + } $else { + b[0], b[1], b[2], b[3] = u.b[3], u.b[2], u.b[1], u.b[0] + } + } } // little_endian_put_u32_at writes a u32 to the four bytes in the array b at the specified offset in little endian order. @@ -91,10 +150,16 @@ pub fn little_endian_put_u32(mut b []u8, v u32) { pub fn little_endian_put_u32_at(mut b []u8, v u32, o int) { _ = b[o] // bounds check _ = b[o + 3] // bounds check - b[o] = u8(v) - b[o + 1] = u8(v >> u32(8)) - b[o + 2] = u8(v >> u32(16)) - b[o + 3] = u8(v >> u32(24)) + unsafe { + mut u := U32{ + u: v + } + $if little_endian { + b[o], b[o + 1], b[o + 2], b[o + 3] = u.b[0], u.b[1], u.b[2], u.b[3] + } $else { + b[o], b[o + 1], b[o + 2], b[o + 3] = u.b[3], u.b[2], u.b[1], u.b[0] + } + } } // little_endian_put_u32_end writes a u32 to the last four bytes in the array b in little endian order. @@ -105,19 +170,30 @@ pub fn little_endian_put_u32_end(mut b []u8, v u32) { // little_endian_get_u32 creates u8 array from the unsigned 32-bit integer v in little endian order. pub fn little_endian_get_u32(v u32) []u8 { - mut b := []u8{len: 4} - b[0] = u8(v) - b[1] = u8(v >> u32(8)) - b[2] = u8(v >> u32(16)) - b[3] = u8(v >> u32(24)) - return b + unsafe { + mut u := U32{ + u: v + } + $if big_endian { + u.b[0], u.b[1], u.b[2], u.b[3] = u.b[3], u.b[2], u.b[1], u.b[0] + } + return u.b[..] + } } // little_endian_u64 creates a u64 from the first eight bytes in the array b in little endian order. @[direct_array_access; inline] pub fn little_endian_u64(b []u8) u64 { _ = b[7] // bounds check - return u64(b[0]) | (u64(b[1]) << u64(8)) | (u64(b[2]) << u64(16)) | (u64(b[3]) << u64(24)) | (u64(b[4]) << u64(32)) | (u64(b[5]) << u64(40)) | (u64(b[6]) << u64(48)) | (u64(b[7]) << u64(56)) + unsafe { + mut u := U64{} + $if little_endian { + u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] = b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7] + } $else { + u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] = b[7], b[6], b[5], b[4], b[3], b[2], b[1], b[0] + } + return u.u + } } // little_endian_u64_at creates a u64 from eight bytes in the array b at the specified offset in little endian order. @@ -125,8 +201,17 @@ pub fn little_endian_u64(b []u8) u64 { pub fn little_endian_u64_at(b []u8, o int) u64 { _ = b[o] // bounds check _ = b[o + 7] // bounds check - return u64(b[o]) | (u64(b[o + 1]) << u64(8)) | (u64(b[o + 2]) << u64(16)) | (u64(b[o + 3]) << u64(24)) | (u64(b[ - o + 4]) << u64(32)) | (u64(b[o + 5]) << u64(40)) | (u64(b[o + 6]) << u64(48)) | (u64(b[o + 7]) << u64(56)) + unsafe { + mut u := U64{} + $if little_endian { + u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] = b[o], b[o + 1], b[o + 2], b[ + o + 3], b[o + 4], b[o + 5], b[o + 6], b[o + 7] + } $else { + u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] = b[o + 7], b[o + 6], b[ + o + 5], b[o + 4], b[o + 3], b[o + 2], b[o + 1], b[o] + } + return u.u + } } // little_endian_u64_end creates a u64 from the last eight bytes in the array b in little endian order. @@ -139,14 +224,16 @@ pub fn little_endian_u64_end(b []u8) u64 { @[direct_array_access; inline] pub fn little_endian_put_u64(mut b []u8, v u64) { _ = b[7] // bounds check - b[0] = u8(v) - b[1] = u8(v >> u64(8)) - b[2] = u8(v >> u64(16)) - b[3] = u8(v >> u64(24)) - b[4] = u8(v >> u64(32)) - b[5] = u8(v >> u64(40)) - b[6] = u8(v >> u64(48)) - b[7] = u8(v >> u64(56)) + unsafe { + mut u := U64{ + u: v + } + $if little_endian { + b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7] = u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] + } $else { + b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7] = u.b[7], u.b[6], u.b[5], u.b[4], u.b[3], u.b[2], u.b[1], u.b[0] + } + } } // little_endian_put_u64_at writes a u64 to the eight bytes in the array b at the specified offset in little endian order. @@ -154,14 +241,16 @@ pub fn little_endian_put_u64(mut b []u8, v u64) { pub fn little_endian_put_u64_at(mut b []u8, v u64, o int) { _ = b[o] // bounds check _ = b[o + 7] // bounds check - b[o] = u8(v) - b[o + 1] = u8(v >> u64(8)) - b[o + 2] = u8(v >> u64(16)) - b[o + 3] = u8(v >> u64(24)) - b[o + 4] = u8(v >> u64(32)) - b[o + 5] = u8(v >> u64(40)) - b[o + 6] = u8(v >> u64(48)) - b[o + 7] = u8(v >> u64(56)) + unsafe { + mut u := U64{ + u: v + } + $if little_endian { + b[o], b[o + 1], b[o + 2], b[o + 3], b[o + 4], b[o + 5], b[o + 6], b[o + 7] = u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] + } $else { + b[o], b[o + 1], b[o + 2], b[o + 3], b[o + 4], b[o + 5], b[o + 6], b[o + 7] = u.b[7], u.b[6], u.b[5], u.b[4], u.b[3], u.b[2], u.b[1], u.b[0] + } + } } // little_endian_put_u64_end writes a u64 to the last eight bytes in the array b at in little endian order. @@ -174,22 +263,26 @@ pub fn little_endian_put_u64_end(mut b []u8, v u64) { pub fn little_endian_f32_at(b []u8, o int) f32 { _ = b[o] // bounds check _ = b[o + 3] // bounds check - u := u32(b[o]) | (u32(b[o + 1]) << u32(8)) | (u32(b[o + 2]) << u32(16)) | (u32(b[o + 3]) << u32(24)) unsafe { - return *(&f32(&u)) + mut u := F32{} + $if little_endian { + u.b[0], u.b[1], u.b[2], u.b[3] = b[o], b[o + 1], b[o + 2], b[o + 3] + } $else { + u.b[0], u.b[1], u.b[2], u.b[3] = b[o + 3], b[o + 2], b[o + 1], b[o] + } + return u.u } } // little_endian_get_u64 creates u8 array from the unsigned 64-bit integer v in little endian order. pub fn little_endian_get_u64(v u64) []u8 { - mut b := []u8{len: 8} - b[0] = u8(v) - b[1] = u8(v >> u64(8)) - b[2] = u8(v >> u64(16)) - b[3] = u8(v >> u64(24)) - b[4] = u8(v >> u64(32)) - b[5] = u8(v >> u64(40)) - b[6] = u8(v >> u64(48)) - b[7] = u8(v >> u64(56)) - return b + unsafe { + mut u := U64{ + u: v + } + $if big_endian { + u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] = u.b[7], u.b[6], u.b[5], u.b[4], u.b[3], u.b[2], u.b[1], u.b[0] + } + return u.b[..] + } } diff --git a/vlib/encoding/binary/little_endian_fixed.v b/vlib/encoding/binary/little_endian_fixed.v index 5074f0a0a..498a0c165 100644 --- a/vlib/encoding/binary/little_endian_fixed.v +++ b/vlib/encoding/binary/little_endian_fixed.v @@ -6,46 +6,86 @@ module binary // little_endian_u16_fixed creates a u16 from the fixed array b in little endian order. @[direct_array_access; inline] pub fn little_endian_u16_fixed(b [2]u8) u16 { - return u16(b[0]) | (u16(b[1]) << u16(8)) + unsafe { + mut u := U16{} + $if little_endian { + u.b[0], u.b[1] = b[0], b[1] + } $else { + u.b[0], u.b[1] = b[1], b[0] + } + return u.u + } } // little_endian_put_u16_fixed writes a u16 to the fixed array b in little endian order. @[direct_array_access; inline] pub fn little_endian_put_u16_fixed(mut b [2]u8, v u16) { - b[0] = u8(v) - b[1] = u8(v >> u16(8)) + unsafe { + mut u := U16{ + u: v + } + $if little_endian { + b[0], b[1] = u.b[0], u.b[1] + } $else { + b[0], b[1] = u.b[1], u.b[0] + } + } } // little_endian_u32_fixed creates a u32 from the fixed array b in little endian order. @[direct_array_access; inline] pub fn little_endian_u32_fixed(b [4]u8) u32 { - return u32(b[0]) | (u32(b[1]) << u32(8)) | (u32(b[2]) << u32(16)) | (u32(b[3]) << u32(24)) + unsafe { + mut u := U32{} + $if little_endian { + u.b[0], u.b[1], u.b[2], u.b[3] = b[0], b[1], b[2], b[3] + } $else { + u.b[0], u.b[1], u.b[2], u.b[3] = b[3], b[2], b[1], b[0] + } + return u.u + } } // little_endian_put_u32_fixed writes a u32 to the fixed array b in little endian order. @[direct_array_access; inline] pub fn little_endian_put_u32_fixed(mut b [4]u8, v u32) { - b[0] = u8(v) - b[1] = u8(v >> u32(8)) - b[2] = u8(v >> u32(16)) - b[3] = u8(v >> u32(24)) + unsafe { + mut u := U32{ + u: v + } + $if little_endian { + b[0], b[1], b[2], b[3] = u.b[0], u.b[1], u.b[2], u.b[3] + } $else { + b[0], b[1], b[2], b[3] = u.b[3], u.b[2], u.b[1], u.b[0] + } + } } // little_endian_u64_fixed creates a u64 from the fixed array b in little endian order. @[direct_array_access; inline] pub fn little_endian_u64_fixed(b [8]u8) u64 { - return u64(b[0]) | (u64(b[1]) << u64(8)) | (u64(b[2]) << u64(16)) | (u64(b[3]) << u64(24)) | (u64(b[4]) << u64(32)) | (u64(b[5]) << u64(40)) | (u64(b[6]) << u64(48)) | (u64(b[7]) << u64(56)) + unsafe { + mut u := U64{} + $if little_endian { + u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] = b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7] + } $else { + u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] = b[7], b[6], b[5], b[4], b[3], b[2], b[1], b[0] + } + return u.u + } } // little_endian_put_u64_fixed writes a u64 to the fixed array b in little endian order. @[direct_array_access; inline] pub fn little_endian_put_u64_fixed(mut b [8]u8, v u64) { - b[0] = u8(v) - b[1] = u8(v >> u64(8)) - b[2] = u8(v >> u64(16)) - b[3] = u8(v >> u64(24)) - b[4] = u8(v >> u64(32)) - b[5] = u8(v >> u64(40)) - b[6] = u8(v >> u64(48)) - b[7] = u8(v >> u64(56)) + unsafe { + mut u := U64{ + u: v + } + $if little_endian { + b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7] = u.b[0], u.b[1], u.b[2], u.b[3], u.b[4], u.b[5], u.b[6], u.b[7] + } $else { + b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7] = u.b[7], u.b[6], u.b[5], u.b[4], u.b[3], u.b[2], u.b[1], u.b[0] + } + } } diff --git a/vlib/encoding/binary/unions.v b/vlib/encoding/binary/unions.v new file mode 100644 index 000000000..520864ea8 --- /dev/null +++ b/vlib/encoding/binary/unions.v @@ -0,0 +1,30 @@ +// Copyright (c) 2025 Alexander Medvednikov. All rights reserved. +// Use of this source code is governed by an MIT license +// that can be found in the LICENSE file. +module binary + +// Unions used across the little endian/big endian conversion routines. + +union U16 { +mut: + b [2]u8 + u u16 +} + +union U32 { +mut: + b [4]u8 + u u32 +} + +union U64 { +mut: + b [8]u8 + u u64 +} + +union F32 { +mut: + b [4]u8 + u f32 +} -- 2.39.5