mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vlib: inline certain functions
This commit is contained in:
parent
695d4018ea
commit
f187c35fb2
@ -6,23 +6,26 @@ module binary
|
||||
|
||||
|
||||
// Little Endian
|
||||
[inline]
|
||||
pub fn little_endian_endian_u16(b []byte) u16 {
|
||||
_ := b[1] // bounds check
|
||||
return u16(b[0]) | u16(u16(b[1])<<u16(8))
|
||||
}
|
||||
|
||||
|
||||
[inline]
|
||||
pub fn little_endian_put_u16(b mut []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(u32(b[1])<<u32(8)) | u32(u32(b[2])<<u32(16)) | u32(u32(b[3])<<u32(24))
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn little_endian_put_u32(b mut []byte, v u32) {
|
||||
_ := b[3] // bounds check
|
||||
b[0] = byte(v)
|
||||
@ -31,12 +34,14 @@ pub fn little_endian_put_u32(b mut []byte, v u32) {
|
||||
b[3] = byte(v >> u32(24))
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn little_endian_u64(b []byte) u64 {
|
||||
_ := b[7] // bounds check
|
||||
return u64(b[0]) | u64(u64(b[1])<<u64(8)) | u64(u64(b[2])<<u64(16)) | u64(u64(b[3])<<u64(24)) |
|
||||
u64(u64(b[4])<<u64(32)) | u64(u64(b[5])<<u64(40)) | u64(u64(b[6])<<u64(48)) | u64(u64(b[7])<<u64(56))
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn little_endian_put_u64(b mut []byte, v u64) {
|
||||
_ := b[7] // bounds check
|
||||
b[0] = byte(v)
|
||||
@ -50,22 +55,26 @@ pub fn little_endian_put_u64(b mut []byte, v u64) {
|
||||
}
|
||||
|
||||
// Big Endian
|
||||
[inline]
|
||||
pub fn big_endian_u16(b []byte) u16 {
|
||||
_ := b[1] // bounds check
|
||||
return u16(b[1]) | u16(u16(b[0])<<u16(8))
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn big_endian_put_u16(b mut []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(u32(b[2])<<u32(8)) | u32(u32(b[1])<<u32(16)) | u32(u32(b[0])<<u32(24))
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn big_endian_put_u32(b mut []byte, v u32) {
|
||||
_ := b[3] // bounds check
|
||||
b[0] = byte(v >> u32(24))
|
||||
@ -74,12 +83,14 @@ pub fn big_endian_put_u32(b mut []byte, v u32) {
|
||||
b[3] = byte(v)
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn big_endian_u64(b []byte) u64 {
|
||||
_ := b[7] // bounds check
|
||||
return u64(b[7]) | u64(u64(b[6])<<u64(8)) | u64(u64(b[5])<<u64(16)) | u64(u64(b[4])<<u64(24)) |
|
||||
u64(u64(b[3])<<u64(32)) | u64(u64(b[2])<<u64(40)) | u64(u64(b[1])<<u64(48)) | u64(u64(b[0])<<u64(56))
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn big_endian_put_u64(b mut []byte, v u64) {
|
||||
_ := b[7] // bounds check
|
||||
b[0] = byte(v >> u64(56))
|
||||
|
@ -10,6 +10,7 @@ module bits
|
||||
// To rotate x right by k bits, call rotate_left_8(x, -k).
|
||||
//
|
||||
// This function's execution time does not depend on the inputs.
|
||||
[inline]
|
||||
pub fn rotate_left_8(x byte, k int) byte {
|
||||
n := byte(8)
|
||||
s := byte(k) & byte(n - byte(1))
|
||||
@ -20,6 +21,7 @@ pub fn rotate_left_8(x byte, k int) byte {
|
||||
// To rotate x right by k bits, call rotate_left_16(x, -k).
|
||||
//
|
||||
// This function's execution time does not depend on the inputs.
|
||||
[inline]
|
||||
pub fn rotate_left_16(x u16, k int) u16 {
|
||||
n := u16(16)
|
||||
s := u16(k) & (n - u16(1))
|
||||
@ -30,6 +32,7 @@ pub fn rotate_left_16(x u16, k int) u16 {
|
||||
// To rotate x right by k bits, call rotate_left_32(x, -k).
|
||||
//
|
||||
// This function's execution time does not depend on the inputs.
|
||||
[inline]
|
||||
pub fn rotate_left_32(x u32, k int) u32 {
|
||||
n := u32(32)
|
||||
s := u32(k) & (n - u32(1))
|
||||
@ -40,6 +43,7 @@ pub fn rotate_left_32(x u32, k int) u32 {
|
||||
// To rotate x right by k bits, call rotate_left_64(x, -k).
|
||||
//
|
||||
// This function's execution time does not depend on the inputs.
|
||||
[inline]
|
||||
pub fn rotate_left_64(x u64, k int) u64 {
|
||||
n := u64(64)
|
||||
s := u64(k) & (n - u64(1))
|
||||
|
Loading…
Reference in New Issue
Block a user