v

Зеркало из https://github.com/vlang/v
Форк
0
/
little_endian.v 
195 строк · 6.7 Кб
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
// little_endian_u16 creates a u16 from the first two bytes in the array b in little endian order.
7
@[direct_array_access; inline]
8
pub fn little_endian_u16(b []u8) u16 {
9
	_ = b[1] // bounds check
10
	return u16(b[0]) | (u16(b[1]) << u16(8))
11
}
12

13
// little_endian_u16_at creates a u16 from two bytes in the array b at the specified offset in little endian order.
14
@[direct_array_access; inline]
15
pub fn little_endian_u16_at(b []u8, o int) u16 {
16
	_ = b[o] // bounds check
17
	_ = b[o + 1] // bounds check
18
	return u16(b[o]) | (u16(b[o + 1]) << u16(8))
19
}
20

21
// little_endian_u16_end creates a u16 from the last two bytes of the array b in little endian order.
22
@[direct_array_access; inline]
23
pub fn little_endian_u16_end(b []u8) u16 {
24
	return little_endian_u16_at(b, b.len - 2)
25
}
26

27
// little_endian_put_u16 writes a u16 to the first two bytes in the array b in little endian order.
28
@[direct_array_access; inline]
29
pub fn little_endian_put_u16(mut b []u8, v u16) {
30
	_ = b[1] // bounds check
31
	b[0] = u8(v)
32
	b[1] = u8(v >> u16(8))
33
}
34

35
// little_endian_put_u16_at writes a u16 to the two bytes in the array b at the specified offset in little endian order.
36
@[direct_array_access; inline]
37
pub fn little_endian_put_u16_at(mut b []u8, v u16, o int) {
38
	_ = b[o] // bounds check
39
	_ = b[o + 1] // bounds check
40
	b[o] = u8(v)
41
	b[o + 1] = u8(v >> u16(8))
42
}
43

44
// little_endian_put_u16_end writes a u16 to the last two bytes of the array b in little endian order.
45
@[direct_array_access; inline]
46
pub fn little_endian_put_u16_end(mut b []u8, v u16) {
47
	little_endian_put_u16_at(mut b, v, b.len - 2)
48
}
49

50
// little_endian_get_u16 creates u8 array from the unsigned 16-bit integer v in little endian order.
51
pub fn little_endian_get_u16(v u16) []u8 {
52
	mut b := []u8{cap: 2}
53
	b << u8(v)
54
	b << u8(v >> u16(8))
55
	return b
56
}
57

58
// little_endian_u32 creates a u32 from the first four bytes in the array b in little endian order.
59
@[direct_array_access; inline]
60
pub fn little_endian_u32(b []u8) u32 {
61
	_ = b[3] // bounds check
62
	return u32(b[0]) | (u32(b[1]) << u32(8)) | (u32(b[2]) << u32(16)) | (u32(b[3]) << u32(24))
63
}
64

65
// little_endian_u32_at creates a u32 from four bytes in the array b at the specified offset in little endian order.
66
@[direct_array_access; inline]
67
pub fn little_endian_u32_at(b []u8, o int) u32 {
68
	_ = b[o] // bounds check
69
	_ = b[o + 3] // bounds check
70
	return u32(b[o]) | (u32(b[o + 1]) << u32(8)) | (u32(b[o + 2]) << u32(16)) | (u32(b[o + 3]) << u32(24))
71
}
72

73
// little_endian_u32_end creates a u32 from the last four bytes in the array b in little endian order.
74
@[direct_array_access; inline]
75
pub fn little_endian_u32_end(b []u8) u32 {
76
	return little_endian_u32_at(b, b.len - 4)
77
}
78

79
// little_endian_put_u32 writes a u32 to the first four bytes in the array b in little endian order.
80
@[direct_array_access; inline]
81
pub fn little_endian_put_u32(mut b []u8, v u32) {
82
	_ = b[3] // bounds check
83
	b[0] = u8(v)
84
	b[1] = u8(v >> u32(8))
85
	b[2] = u8(v >> u32(16))
86
	b[3] = u8(v >> u32(24))
87
}
88

89
// little_endian_put_u32_at writes a u32 to the four bytes in the array b at the specified offset in little endian order.
90
@[direct_array_access; inline]
91
pub fn little_endian_put_u32_at(mut b []u8, v u32, o int) {
92
	_ = b[o] // bounds check
93
	_ = b[o + 3] // bounds check
94
	b[o] = u8(v)
95
	b[o + 1] = u8(v >> u32(8))
96
	b[o + 2] = u8(v >> u32(16))
97
	b[o + 3] = u8(v >> u32(24))
98
}
99

100
// little_endian_put_u32_end writes a u32 to the last four bytes in the array b in little endian order.
101
@[direct_array_access; inline]
102
pub fn little_endian_put_u32_end(mut b []u8, v u32) {
103
	little_endian_put_u32_at(mut b, v, b.len - 4)
104
}
105

106
// little_endian_get_u32 creates u8 array from the unsigned 32-bit integer v in little endian order.
107
pub fn little_endian_get_u32(v u32) []u8 {
108
	mut b := []u8{cap: 4}
109
	b << u8(v)
110
	b << u8(v >> u32(8))
111
	b << u8(v >> u32(16))
112
	b << u8(v >> u32(24))
113
	return b
114
}
115

116
// little_endian_u64 creates a u64 from the first eight bytes in the array b in little endian order.
117
@[direct_array_access; inline]
118
pub fn little_endian_u64(b []u8) u64 {
119
	_ = b[7] // bounds check
120
	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))
121
}
122

123
// little_endian_u64_at creates a u64 from eight bytes in the array b at the specified offset in little endian order.
124
@[direct_array_access; inline]
125
pub fn little_endian_u64_at(b []u8, o int) u64 {
126
	_ = b[o] // bounds check
127
	_ = b[o + 7] // bounds check
128
	return u64(b[o]) | (u64(b[o + 1]) << u64(8)) | (u64(b[o + 2]) << u64(16)) | (u64(b[o + 3]) << u64(24)) | (u64(b[
129
		o + 4]) << u64(32)) | (u64(b[o + 5]) << u64(40)) | (u64(b[o + 6]) << u64(48)) | (u64(b[o + 7]) << u64(56))
130
}
131

132
// little_endian_u64_end creates a u64 from the last eight bytes in the array b in little endian order.
133
@[direct_array_access; inline]
134
pub fn little_endian_u64_end(b []u8) u64 {
135
	return little_endian_u64_at(b, b.len - 8)
136
}
137

138
// little_endian_put_u64 writes a u64 to the first eight bytes in the array b in little endian order.
139
@[direct_array_access; inline]
140
pub fn little_endian_put_u64(mut b []u8, v u64) {
141
	_ = b[7] // bounds check
142
	b[0] = u8(v)
143
	b[1] = u8(v >> u64(8))
144
	b[2] = u8(v >> u64(16))
145
	b[3] = u8(v >> u64(24))
146
	b[4] = u8(v >> u64(32))
147
	b[5] = u8(v >> u64(40))
148
	b[6] = u8(v >> u64(48))
149
	b[7] = u8(v >> u64(56))
150
}
151

152
// little_endian_put_u64_at writes a u64 to the eight bytes in the array b at the specified offset in little endian order.
153
@[direct_array_access; inline]
154
pub fn little_endian_put_u64_at(mut b []u8, v u64, o int) {
155
	_ = b[o] // bounds check
156
	_ = b[o + 7] // bounds check
157
	b[o] = u8(v)
158
	b[o + 1] = u8(v >> u64(8))
159
	b[o + 2] = u8(v >> u64(16))
160
	b[o + 3] = u8(v >> u64(24))
161
	b[o + 4] = u8(v >> u64(32))
162
	b[o + 5] = u8(v >> u64(40))
163
	b[o + 6] = u8(v >> u64(48))
164
	b[o + 7] = u8(v >> u64(56))
165
}
166

167
// little_endian_put_u64_end writes a u64 to the last eight bytes in the array b at in little endian order.
168
@[direct_array_access; inline]
169
pub fn little_endian_put_u64_end(mut b []u8, v u64) {
170
	little_endian_put_u64_at(mut b, v, b.len - 8)
171
}
172

173
@[direct_array_access; inline]
174
pub fn little_endian_f32_at(b []u8, o int) f32 {
175
	_ = b[o] // bounds check
176
	_ = b[o + 3] // bounds check
177
	u := u32(b[o]) | (u32(b[o + 1]) << u32(8)) | (u32(b[o + 2]) << u32(16)) | (u32(b[o + 3]) << u32(24))
178
	unsafe {
179
		return *(&f32(&u))
180
	}
181
}
182

183
// little_endian_get_u64 creates u8 array from the unsigned 64-bit integer v in little endian order.
184
pub fn little_endian_get_u64(v u64) []u8 {
185
	mut b := []u8{cap: 8}
186
	b << u8(v)
187
	b << u8(v >> u64(8))
188
	b << u8(v >> u64(16))
189
	b << u8(v >> u64(24))
190
	b << u8(v >> u64(32))
191
	b << u8(v >> u64(40))
192
	b << u8(v >> u64(48))
193
	b << u8(v >> u64(56))
194
	return b
195
}
196

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.