2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2019-07-15 18:49:01 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module binary
|
2021-05-08 13:32:29 +03:00
|
|
|
|
2019-07-15 18:49:01 +03:00
|
|
|
// Little Endian
|
2022-05-30 21:56:39 +03:00
|
|
|
[direct_array_access; inline]
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn little_endian_u16(b []u8) u16 {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = b[1] // bounds check
|
2021-05-08 13:32:29 +03:00
|
|
|
return u16(b[0]) | (u16(b[1]) << u16(8))
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2022-05-30 21:56:39 +03:00
|
|
|
[direct_array_access; inline]
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn little_endian_put_u16(mut b []u8, v u16) {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = b[1] // bounds check
|
2022-04-15 14:58:56 +03:00
|
|
|
b[0] = u8(v)
|
|
|
|
b[1] = u8(v >> u16(8))
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2022-05-30 21:56:39 +03:00
|
|
|
[direct_array_access; inline]
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn little_endian_u32(b []u8) u32 {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = b[3] // bounds check
|
2021-05-08 13:32:29 +03:00
|
|
|
return u32(b[0]) | (u32(b[1]) << u32(8)) | (u32(b[2]) << u32(16)) | (u32(b[3]) << u32(24))
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2022-05-30 21:56:39 +03:00
|
|
|
[direct_array_access; inline]
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn little_endian_put_u32(mut b []u8, v u32) {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = b[3] // bounds check
|
2022-04-15 14:58:56 +03:00
|
|
|
b[0] = u8(v)
|
|
|
|
b[1] = u8(v >> u32(8))
|
|
|
|
b[2] = u8(v >> u32(16))
|
|
|
|
b[3] = u8(v >> u32(24))
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2022-05-30 21:56:39 +03:00
|
|
|
[direct_array_access; inline]
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn little_endian_u64(b []u8) u64 {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = b[7] // bounds check
|
2021-05-08 13:32:29 +03:00
|
|
|
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))
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2022-05-30 21:56:39 +03:00
|
|
|
[direct_array_access; inline]
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn little_endian_put_u64(mut b []u8, v u64) {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = b[7] // bounds check
|
2022-04-15 14:58:56 +03:00
|
|
|
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))
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Big Endian
|
2022-05-30 21:56:39 +03:00
|
|
|
[direct_array_access; inline]
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn big_endian_u16(b []u8) u16 {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = b[1] // bounds check
|
2021-05-08 13:32:29 +03:00
|
|
|
return u16(b[1]) | (u16(b[0]) << u16(8))
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2022-05-30 21:56:39 +03:00
|
|
|
[direct_array_access; inline]
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn big_endian_put_u16(mut b []u8, v u16) {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = b[1] // bounds check
|
2022-04-15 14:58:56 +03:00
|
|
|
b[0] = u8(v >> u16(8))
|
|
|
|
b[1] = u8(v)
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2022-05-30 21:56:39 +03:00
|
|
|
[direct_array_access; inline]
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn big_endian_u32(b []u8) u32 {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = b[3] // bounds check
|
2021-05-08 13:32:29 +03:00
|
|
|
return u32(b[3]) | (u32(b[2]) << u32(8)) | (u32(b[1]) << u32(16)) | (u32(b[0]) << u32(24))
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2022-05-30 21:56:39 +03:00
|
|
|
[direct_array_access; inline]
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn big_endian_put_u32(mut b []u8, v u32) {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = b[3] // bounds check
|
2022-04-15 14:58:56 +03:00
|
|
|
b[0] = u8(v >> u32(24))
|
|
|
|
b[1] = u8(v >> u32(16))
|
|
|
|
b[2] = u8(v >> u32(8))
|
|
|
|
b[3] = u8(v)
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2022-05-30 21:56:39 +03:00
|
|
|
[direct_array_access; inline]
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn big_endian_u64(b []u8) u64 {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = b[7] // bounds check
|
2021-05-08 13:32:29 +03:00
|
|
|
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))
|
2019-07-15 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
2022-05-30 21:56:39 +03:00
|
|
|
[direct_array_access; inline]
|
2022-04-15 15:35:35 +03:00
|
|
|
pub fn big_endian_put_u64(mut b []u8, v u64) {
|
2019-09-25 15:10:45 +03:00
|
|
|
_ = b[7] // bounds check
|
2022-04-15 14:58:56 +03:00
|
|
|
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)
|
2019-07-16 18:59:07 +03:00
|
|
|
}
|