| 1 | // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module binary |
| 5 | |
| 6 | // big_endian_u16_fixed creates a u16 from the first two bytes in the fixed array b in big endian order. |
| 7 | @[direct_array_access; inline] |
| 8 | pub fn big_endian_u16_fixed(b [2]u8) u16 { |
| 9 | unsafe { |
| 10 | mut u := U16{} |
| 11 | $if big_endian { |
| 12 | u.b[0], u.b[1] = b[0], b[1] |
| 13 | } $else { |
| 14 | u.b[0], u.b[1] = b[1], b[0] |
| 15 | } |
| 16 | return u.u |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | // big_endian_put_u16_fixed writes a u16 to the fixed array b in big endian order. |
| 21 | @[direct_array_access; inline] |
| 22 | pub fn big_endian_put_u16_fixed(mut b [2]u8, v u16) { |
| 23 | unsafe { |
| 24 | mut u := U16{ |
| 25 | u: v |
| 26 | } |
| 27 | $if big_endian { |
| 28 | b[0], b[1] = u.b[0], u.b[1] |
| 29 | } $else { |
| 30 | b[0], b[1] = u.b[1], u.b[0] |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // big_endian_u32_fixed creates a u32 from four bytes in the fixed array b in big endian order. |
| 36 | @[direct_array_access; inline] |
| 37 | pub fn big_endian_u32_fixed(b [4]u8) u32 { |
| 38 | unsafe { |
| 39 | mut u := U32{} |
| 40 | $if big_endian { |
| 41 | u.b[0], u.b[1], u.b[2], u.b[3] = b[0], b[1], b[2], b[3] |
| 42 | } $else { |
| 43 | u.b[0], u.b[1], u.b[2], u.b[3] = b[3], b[2], b[1], b[0] |
| 44 | } |
| 45 | return u.u |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // big_endian_put_u32_fixed writes a u32 to the fixed array b in big endian order. |
| 50 | @[direct_array_access; inline] |
| 51 | pub fn big_endian_put_u32_fixed(mut b [4]u8, v u32) { |
| 52 | unsafe { |
| 53 | mut u := U32{ |
| 54 | u: v |
| 55 | } |
| 56 | $if big_endian { |
| 57 | b[0], b[1], b[2], b[3] = u.b[0], u.b[1], u.b[2], u.b[3] |
| 58 | } $else { |
| 59 | b[0], b[1], b[2], b[3] = u.b[3], u.b[2], u.b[1], u.b[0] |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // big_endian_u64_fixed creates a u64 from the fixed array b in big endian order. |
| 65 | @[direct_array_access; inline] |
| 66 | pub fn big_endian_u64_fixed(b [8]u8) u64 { |
| 67 | unsafe { |
| 68 | mut u := U64{} |
| 69 | $if big_endian { |
| 70 | 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] |
| 71 | } $else { |
| 72 | 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] |
| 73 | } |
| 74 | return u.u |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // big_endian_put_u64_fixed writes a u64 to the fixed array b in big endian order. |
| 79 | @[direct_array_access; inline] |
| 80 | pub fn big_endian_put_u64_fixed(mut b [8]u8, v u64) { |
| 81 | unsafe { |
| 82 | mut u := U64{ |
| 83 | u: v |
| 84 | } |
| 85 | $if big_endian { |
| 86 | 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] |
| 87 | } $else { |
| 88 | 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] |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |