v
Зеркало из https://github.com/vlang/v
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.
4module 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]
8pub fn little_endian_u16(b []u8) u16 {
9_ = b[1] // bounds check
10return 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]
15pub fn little_endian_u16_at(b []u8, o int) u16 {
16_ = b[o] // bounds check
17_ = b[o + 1] // bounds check
18return 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]
23pub fn little_endian_u16_end(b []u8) u16 {
24return 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]
29pub fn little_endian_put_u16(mut b []u8, v u16) {
30_ = b[1] // bounds check
31b[0] = u8(v)
32b[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]
37pub fn little_endian_put_u16_at(mut b []u8, v u16, o int) {
38_ = b[o] // bounds check
39_ = b[o + 1] // bounds check
40b[o] = u8(v)
41b[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]
46pub fn little_endian_put_u16_end(mut b []u8, v u16) {
47little_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.
51pub fn little_endian_get_u16(v u16) []u8 {
52mut b := []u8{cap: 2}
53b << u8(v)
54b << u8(v >> u16(8))
55return 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]
60pub fn little_endian_u32(b []u8) u32 {
61_ = b[3] // bounds check
62return 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]
67pub fn little_endian_u32_at(b []u8, o int) u32 {
68_ = b[o] // bounds check
69_ = b[o + 3] // bounds check
70return 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]
75pub fn little_endian_u32_end(b []u8) u32 {
76return 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]
81pub fn little_endian_put_u32(mut b []u8, v u32) {
82_ = b[3] // bounds check
83b[0] = u8(v)
84b[1] = u8(v >> u32(8))
85b[2] = u8(v >> u32(16))
86b[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]
91pub fn little_endian_put_u32_at(mut b []u8, v u32, o int) {
92_ = b[o] // bounds check
93_ = b[o + 3] // bounds check
94b[o] = u8(v)
95b[o + 1] = u8(v >> u32(8))
96b[o + 2] = u8(v >> u32(16))
97b[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]
102pub fn little_endian_put_u32_end(mut b []u8, v u32) {
103little_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.
107pub fn little_endian_get_u32(v u32) []u8 {
108mut b := []u8{cap: 4}
109b << u8(v)
110b << u8(v >> u32(8))
111b << u8(v >> u32(16))
112b << u8(v >> u32(24))
113return 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]
118pub fn little_endian_u64(b []u8) u64 {
119_ = b[7] // bounds check
120return 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]
125pub fn little_endian_u64_at(b []u8, o int) u64 {
126_ = b[o] // bounds check
127_ = b[o + 7] // bounds check
128return u64(b[o]) | (u64(b[o + 1]) << u64(8)) | (u64(b[o + 2]) << u64(16)) | (u64(b[o + 3]) << u64(24)) | (u64(b[
129o + 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]
134pub fn little_endian_u64_end(b []u8) u64 {
135return 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]
140pub fn little_endian_put_u64(mut b []u8, v u64) {
141_ = b[7] // bounds check
142b[0] = u8(v)
143b[1] = u8(v >> u64(8))
144b[2] = u8(v >> u64(16))
145b[3] = u8(v >> u64(24))
146b[4] = u8(v >> u64(32))
147b[5] = u8(v >> u64(40))
148b[6] = u8(v >> u64(48))
149b[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]
154pub fn little_endian_put_u64_at(mut b []u8, v u64, o int) {
155_ = b[o] // bounds check
156_ = b[o + 7] // bounds check
157b[o] = u8(v)
158b[o + 1] = u8(v >> u64(8))
159b[o + 2] = u8(v >> u64(16))
160b[o + 3] = u8(v >> u64(24))
161b[o + 4] = u8(v >> u64(32))
162b[o + 5] = u8(v >> u64(40))
163b[o + 6] = u8(v >> u64(48))
164b[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]
169pub fn little_endian_put_u64_end(mut b []u8, v u64) {
170little_endian_put_u64_at(mut b, v, b.len - 8)
171}
172
173@[direct_array_access; inline]
174pub fn little_endian_f32_at(b []u8, o int) f32 {
175_ = b[o] // bounds check
176_ = b[o + 3] // bounds check
177u := u32(b[o]) | (u32(b[o + 1]) << u32(8)) | (u32(b[o + 2]) << u32(16)) | (u32(b[o + 3]) << u32(24))
178unsafe {
179return *(&f32(&u))
180}
181}
182
183// little_endian_get_u64 creates u8 array from the unsigned 64-bit integer v in little endian order.
184pub fn little_endian_get_u64(v u64) []u8 {
185mut b := []u8{cap: 8}
186b << u8(v)
187b << u8(v >> u64(8))
188b << u8(v >> u64(16))
189b << u8(v >> u64(24))
190b << u8(v >> u64(32))
191b << u8(v >> u64(40))
192b << u8(v >> u64(48))
193b << u8(v >> u64(56))
194return b
195}
196