math.bits: remove deprecated max32 and max64 const (#20277)

This commit is contained in:
Swastik Baranwal 2023-12-27 17:25:55 +05:30 committed by GitHub
parent 426e471988
commit 6a04febbf8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 17 deletions

View file

@ -2,6 +2,36 @@ module builtin
type byte = u8 type byte = u8
pub const min_i8 = i8(-128)
pub const max_i8 = i8(127)
pub const min_i16 = i16(-32768)
pub const max_i16 = i16(32767)
pub const min_i32 = i32(-2147483648)
pub const max_i32 = i32(2147483647)
pub const min_int = min_i32
pub const max_int = max_i32
// -9223372036854775808 is wrong, because C compilers parse literal values
// without sign first, and 9223372036854775808 overflows i64, hence the
// consecutive subtraction by 1
pub const min_i64 = i64(-9223372036854775807 - 1)
pub const max_i64 = i64(9223372036854775807)
pub const min_u8 = u8(0)
pub const max_u8 = u8(255)
pub const min_u16 = u16(0)
pub const max_u16 = u16(65535)
pub const min_u32 = u32(0)
pub const max_u32 = u32(4294967295)
pub const min_u64 = u64(0)
pub const max_u64 = u64(18446744073709551615)
pub fn (i i8) str() string { pub fn (i i8) str() string {
mut res := '' mut res := ''
#res.str = i.val.toString() #res.str = i.val.toString()

View file

@ -22,11 +22,6 @@ const m3 = u64(0x00ff00ff00ff00ff) // etc.
const m4 = u64(0x0000ffff0000ffff) const m4 = u64(0x0000ffff0000ffff)
// TODO: this consts should be taken from int.v
// save importing math mod just for these
const max_u32 = u32(4294967295)
const max_u64 = u64(18446744073709551615)
// --- LeadingZeros --- // --- LeadingZeros ---
// leading_zeros_8 returns the number of leading zero bits in x; the result is 8 for x == 0. // leading_zeros_8 returns the number of leading zero bits in x; the result is 8 for x == 0.
pub fn leading_zeros_8(x u8) int { pub fn leading_zeros_8(x u8) int {
@ -129,9 +124,9 @@ pub fn ones_count_64(x u64) int {
// Per "Hacker's Delight", the first line can be simplified // Per "Hacker's Delight", the first line can be simplified
// more, but it saves at best one instruction, so we leave // more, but it saves at best one instruction, so we leave
// it alone for clarity. // it alone for clarity.
mut y := (x >> u64(1) & (bits.m0 & bits.max_u64)) + (x & (bits.m0 & bits.max_u64)) mut y := (x >> u64(1) & (bits.m0 & max_u64)) + (x & (bits.m0 & max_u64))
y = (y >> u64(2) & (bits.m1 & bits.max_u64)) + (y & (bits.m1 & bits.max_u64)) y = (y >> u64(2) & (bits.m1 & max_u64)) + (y & (bits.m1 & max_u64))
y = ((y >> 4) + y) & (bits.m2 & bits.max_u64) y = ((y >> 4) + y) & (bits.m2 & max_u64)
y += y >> 8 y += y >> 8
y += y >> 16 y += y >> 16
y += y >> 32 y += y >> 32
@ -200,18 +195,18 @@ pub fn reverse_16(x u16) u16 {
// reverse_32 returns the value of x with its bits in reversed order. // reverse_32 returns the value of x with its bits in reversed order.
@[inline] @[inline]
pub fn reverse_32(x u32) u32 { pub fn reverse_32(x u32) u32 {
mut y := ((x >> u32(1) & (bits.m0 & bits.max_u32)) | ((x & (bits.m0 & bits.max_u32)) << 1)) mut y := ((x >> u32(1) & (bits.m0 & max_u32)) | ((x & (bits.m0 & max_u32)) << 1))
y = ((y >> u32(2) & (bits.m1 & bits.max_u32)) | ((y & (bits.m1 & bits.max_u32)) << u32(2))) y = ((y >> u32(2) & (bits.m1 & max_u32)) | ((y & (bits.m1 & max_u32)) << u32(2)))
y = ((y >> u32(4) & (bits.m2 & bits.max_u32)) | ((y & (bits.m2 & bits.max_u32)) << u32(4))) y = ((y >> u32(4) & (bits.m2 & max_u32)) | ((y & (bits.m2 & max_u32)) << u32(4)))
return reverse_bytes_32(u32(y)) return reverse_bytes_32(u32(y))
} }
// reverse_64 returns the value of x with its bits in reversed order. // reverse_64 returns the value of x with its bits in reversed order.
@[inline] @[inline]
pub fn reverse_64(x u64) u64 { pub fn reverse_64(x u64) u64 {
mut y := ((x >> u64(1) & (bits.m0 & bits.max_u64)) | ((x & (bits.m0 & bits.max_u64)) << 1)) mut y := ((x >> u64(1) & (bits.m0 & max_u64)) | ((x & (bits.m0 & max_u64)) << 1))
y = ((y >> u64(2) & (bits.m1 & bits.max_u64)) | ((y & (bits.m1 & bits.max_u64)) << 2)) y = ((y >> u64(2) & (bits.m1 & max_u64)) | ((y & (bits.m1 & max_u64)) << 2))
y = ((y >> u64(4) & (bits.m2 & bits.max_u64)) | ((y & (bits.m2 & bits.max_u64)) << 4)) y = ((y >> u64(4) & (bits.m2 & max_u64)) | ((y & (bits.m2 & max_u64)) << 4))
return reverse_bytes_64(y) return reverse_bytes_64(y)
} }
@ -229,7 +224,7 @@ pub fn reverse_bytes_16(x u16) u16 {
// This function's execution time does not depend on the inputs. // This function's execution time does not depend on the inputs.
@[inline] @[inline]
pub fn reverse_bytes_32(x u32) u32 { pub fn reverse_bytes_32(x u32) u32 {
y := ((x >> u32(8) & (bits.m3 & bits.max_u32)) | ((x & (bits.m3 & bits.max_u32)) << u32(8))) y := ((x >> u32(8) & (bits.m3 & max_u32)) | ((x & (bits.m3 & max_u32)) << u32(8)))
return u32((y >> 16) | (y << 16)) return u32((y >> 16) | (y << 16))
} }
@ -238,8 +233,8 @@ pub fn reverse_bytes_32(x u32) u32 {
// This function's execution time does not depend on the inputs. // This function's execution time does not depend on the inputs.
@[inline] @[inline]
pub fn reverse_bytes_64(x u64) u64 { pub fn reverse_bytes_64(x u64) u64 {
mut y := ((x >> u64(8) & (bits.m3 & bits.max_u64)) | ((x & (bits.m3 & bits.max_u64)) << u64(8))) mut y := ((x >> u64(8) & (bits.m3 & max_u64)) | ((x & (bits.m3 & max_u64)) << u64(8)))
y = ((y >> u64(16) & (bits.m4 & bits.max_u64)) | ((y & (bits.m4 & bits.max_u64)) << u64(16))) y = ((y >> u64(16) & (bits.m4 & max_u64)) | ((y & (bits.m4 & max_u64)) << u64(16)))
return (y >> 32) | (y << 32) return (y >> 32) | (y << 32)
} }