os: fix swap_bytes_u64 (#24033)

This commit is contained in:
Mike 2025-03-25 15:34:57 +02:00 committed by GitHub
parent 2b7690254d
commit d6c3e6fe5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 39 additions and 14 deletions

View file

@ -74,17 +74,34 @@ pub fn (mut f File) read_u8() !u8 {
@[inline]
fn swap_bytes_u16(x u16) u16 {
return ((x >> 8) & 0x00FF) | ((x << 8) & 0xFF00)
// vfmt off
return ((x >> 8) & 0x00FF) |
((x << 8) & 0xFF00)
// vfmt on
}
@[inline]
fn swap_bytes_u32(x u32) u32 {
return ((x >> 24) & 0x0000_00FF) | ((x >> 8) & 0x0000_FF00) | ((x << 8) & 0x00FF_0000) | ((x << 24) & 0xFF00_0000)
// vfmt off
return ((x >> 24) & 0x0000_00FF) |
((x >> 8) & 0x0000_FF00) |
((x << 8) & 0x00FF_0000) |
((x << 24) & 0xFF00_0000)
// vfmt on
}
@[inline]
fn swap_bytes_u64(x u64) u64 {
return ((x >> 40) & 0x00000000_0000FF00) | ((x >> 24) & 0x00000000_00FF0000) | ((x >> 8) & 0x00000000_FF000000) | ((x << 8) & 0x000000FF_00000000) | ((x << 24) & 0x0000FF00_00000000) | ((x << 40) & 0x00FF0000_00000000) | ((x << 56) & 0xFF000000_00000000)
// vfmt off
return ((x >> 56) & 0x00000000_000000FF) |
((x >> 40) & 0x00000000_0000FF00) |
((x >> 24) & 0x00000000_00FF0000) |
((x >> 8) & 0x00000000_FF000000) |
((x << 8) & 0x000000FF_00000000) |
((x << 24) & 0x0000FF00_00000000) |
((x << 40) & 0x00FF0000_00000000) |
((x << 56) & 0xFF000000_00000000)
// vfmt on
}
fn swap_bytes[T](input T) T {

View file

@ -19,8 +19,8 @@ fn test_write_be_read_be() {
mut f := os.open_file(fname, 'wb')!
f.write_be[u8](0x08)!
f.write_be[u16](0x1617)!
f.write_be[u32](0x3233)!
f.write_be[u64](0x6465)!
f.write_be[u32](0x30313233)!
f.write_be[u64](0x5859606162636465)!
f.write_u8(123)!
f.write_u8(42)!
f.close()
@ -28,11 +28,19 @@ fn test_write_be_read_be() {
assert os.read_bytes(fname)! == [
u8(0x08),
0x16, 0x17,
0x00, 0x00, 0x32, 0x33,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x64, 0x65,
0x30, 0x31, 0x32, 0x33,
0x58, 0x59, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65,
123, 42
]
// vfmt on
mut r := os.open_file(fname, 'rb')!
assert r.read_be[u8]()! == 0x08
assert r.read_be[u16]()! == 0x1617
assert r.read_be[u32]()! == 0x30313233
assert r.read_be[u64]()! == 0x5859606162636465
assert r.read_u8()! == 123
assert r.read_u8()! == 42
r.close()
}
fn test_write_le_read_le() {
@ -40,25 +48,25 @@ fn test_write_le_read_le() {
mut f := os.open_file(fname, 'wb')!
f.write_le[u8](0x08)!
f.write_le[u16](0x1617)!
f.write_le[u32](0x3233)!
f.write_le[u64](0x6465)!
f.write_le[u32](0x30313233)!
f.write_le[u64](0x5859606162636465)!
f.write_u8(12)!
f.write_u8(34)!
f.close()
// vfmt off
assert os.read_bytes('f_le')! == [
assert os.read_bytes(fname)! == [
u8(0x08),
0x17, 0x16,
0x33, 0x32, 0x00, 0x00,
0x65, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x33, 0x32, 0x31, 0x30,
0x65, 0x64, 0x63, 0x62, 0x61, 0x60, 0x59, 0x58,
12, 34
]
// vfmt on
mut r := os.open_file(fname, 'rb')!
assert r.read_le[u8]()! == 0x08
assert r.read_le[u16]()! == 0x1617
assert r.read_le[u32]()! == 0x3233
assert r.read_le[u64]()! == 0x6465
assert r.read_le[u32]()! == 0x30313233
assert r.read_le[u64]()! == 0x5859606162636465
assert r.read_u8()! == 12
assert r.read_u8()! == 34
r.close()