aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/encoding/binary/binary.v
blob: d7fe298347cce9986198d572f0837b72cdd47222 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) 2019-2021 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

// Little Endian
[inline]
pub fn little_endian_u16(b []byte) u16 {
	_ = b[1] // bounds check
	return u16(b[0]) | (u16(b[1]) << u16(8))
}

[inline]
pub fn little_endian_put_u16(mut b []byte, v u16) {
	_ = b[1] // bounds check
	b[0] = byte(v)
	b[1] = byte(v >> u16(8))
}

[inline]
pub fn little_endian_u32(b []byte) u32 {
	_ = b[3] // bounds check
	return u32(b[0]) | (u32(b[1]) << u32(8)) | (u32(b[2]) << u32(16)) | (u32(b[3]) << u32(24))
}

[inline]
pub fn little_endian_put_u32(mut b []byte, v u32) {
	_ = b[3] // bounds check
	b[0] = byte(v)
	b[1] = byte(v >> u32(8))
	b[2] = byte(v >> u32(16))
	b[3] = byte(v >> u32(24))
}

[inline]
pub fn little_endian_u64(b []byte) 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))
}

[inline]
pub fn little_endian_put_u64(mut b []byte, v u64) {
	_ = b[7] // bounds check
	b[0] = byte(v)
	b[1] = byte(v >> u64(8))
	b[2] = byte(v >> u64(16))
	b[3] = byte(v >> u64(24))
	b[4] = byte(v >> u64(32))
	b[5] = byte(v >> u64(40))
	b[6] = byte(v >> u64(48))
	b[7] = byte(v >> u64(56))
}

// Big Endian
[inline]
pub fn big_endian_u16(b []byte) u16 {
	_ = b[1] // bounds check
	return u16(b[1]) | (u16(b[0]) << u16(8))
}

[inline]
pub fn big_endian_put_u16(mut b []byte, v u16) {
	_ = b[1] // bounds check
	b[0] = byte(v >> u16(8))
	b[1] = byte(v)
}

[inline]
pub fn big_endian_u32(b []byte) u32 {
	_ = b[3] // bounds check
	return u32(b[3]) | (u32(b[2]) << u32(8)) | (u32(b[1]) << u32(16)) | (u32(b[0]) << u32(24))
}

[inline]
pub fn big_endian_put_u32(mut b []byte, v u32) {
	_ = b[3] // bounds check
	b[0] = byte(v >> u32(24))
	b[1] = byte(v >> u32(16))
	b[2] = byte(v >> u32(8))
	b[3] = byte(v)
}

[inline]
pub fn big_endian_u64(b []byte) 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))
}

[inline]
pub fn big_endian_put_u64(mut b []byte, v u64) {
	_ = b[7] // bounds check
	b[0] = byte(v >> u64(56))
	b[1] = byte(v >> u64(48))
	b[2] = byte(v >> u64(40))
	b[3] = byte(v >> u64(32))
	b[4] = byte(v >> u64(24))
	b[5] = byte(v >> u64(16))
	b[6] = byte(v >> u64(8))
	b[7] = byte(v)
}