builtin: move min/max integer values consts from math to builtin (#19809)

This commit is contained in:
Artem Yurchenko 2023-11-08 19:43:48 +01:00 committed by GitHub
parent 7e7e43866a
commit 97f7c3f609
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 90 additions and 27 deletions

View file

@ -87,8 +87,8 @@ fn new_aints(ovals []int, extreme_mins int, extreme_maxs int) Aints {
nmaxs: extreme_maxs
}
mut sum := i64(0)
mut imin := int(math.max_i32)
mut imax := int(-math.max_i32)
mut imin := int(max_i32)
mut imax := int(-max_i32)
// discard the extremes:
mut vals := []int{}
for x in ovals {

View file

@ -540,7 +540,7 @@ pub fn (mut a array) delete_last() {
// Alternative: Slices can also be made with [start..end] notation
// Alternative: `.slice_ni()` will always return an array.
fn (a array) slice(start int, _end int) array {
end := if _end == 2147483647 { a.len } else { _end } // max_int
end := if _end == max_int { a.len } else { _end } // max_int
$if !no_bounds_checking {
if start > end {
panic('array.slice: invalid slice index (${start} > ${end})')
@ -575,7 +575,7 @@ fn (a array) slice(start int, _end int) array {
// This function always return a valid array.
fn (a array) slice_ni(_start int, _end int) array {
// a.flags.clear(.noslices)
mut end := if _end == 2147483647 { a.len } else { _end } // max_int
mut end := if _end == max_int { a.len } else { _end } // max_int
mut start := _start
if start < 0 {

View file

@ -42,6 +42,38 @@ const (
digit_pairs = '00102030405060708090011121314151617181910212223242526272829203132333435363738393041424344454647484940515253545556575859506162636465666768696071727374757677787970818283848586878889809192939495969798999'
)
pub const (
min_i8 = i8(-128)
max_i8 = i8(127)
min_i16 = i16(-32768)
max_i16 = i16(32767)
min_i32 = i32(-2147483648)
max_i32 = i32(2147483647)
min_int = min_i32
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
min_i64 = i64(-9223372036854775807 - 1)
max_i64 = i64(9223372036854775807)
min_u8 = u8(0)
max_u8 = u8(255)
min_u16 = u16(0)
max_u16 = u16(65535)
min_u32 = u32(0)
max_u32 = u32(4294967295)
min_u64 = u64(0)
max_u64 = u64(18446744073709551615)
)
// This implementation is the quickest with gcc -O2
// str_l returns the string representation of the integer nn with max chars.
[direct_array_access; inline]
@ -194,8 +226,7 @@ pub fn (nn i64) str() string {
mut d := i64(0)
if n == 0 {
return '0'
} else if n == i64(-9223372036854775807 - 1) {
// math.min_i64
} else if n == min_i64 {
return '-9223372036854775808'
}
max := 20

View file

@ -1027,7 +1027,7 @@ pub fn (s string) split_into_lines() []string {
// Example: assert 'ABCD'.substr(1,3) == 'BC'
[direct_array_access]
pub fn (s string) substr(start int, _end int) string {
end := if _end == 2147483647 { s.len } else { _end } // max_int
end := if _end == max_int { s.len } else { _end } // max_int
$if !no_bounds_checking {
if start > end || start > s.len || end > s.len || start < 0 || end < 0 {
panic('substr(${start}, ${end}) out of bounds (len=${s.len})')
@ -1052,7 +1052,7 @@ pub fn (s string) substr(start int, _end int) string {
// return an error when the index is out of range
[direct_array_access]
pub fn (s string) substr_with_check(start int, _end int) !string {
end := if _end == 2147483647 { s.len } else { _end } // max_int
end := if _end == max_int { s.len } else { _end } // max_int
if start > end || start > s.len || end > s.len || start < 0 || end < 0 {
return error('substr(${start}, ${end}) out of bounds (len=${s.len})')
}
@ -1076,7 +1076,7 @@ pub fn (s string) substr_with_check(start int, _end int) !string {
[direct_array_access]
pub fn (s string) substr_ni(_start int, _end int) string {
mut start := _start
mut end := if _end == 2147483647 { s.len } else { _end } // max_int
mut end := if _end == max_int { s.len } else { _end } // max_int
// borders math
if start < 0 {
@ -2188,7 +2188,7 @@ pub fn (s string) trim_indent() string {
.filter(!it.is_blank())
.map(it.indent_width())
mut min_common_indent := int(2147483647) // max int
mut min_common_indent := int(max_int) // max int
for line_indent in lines_indents {
if line_indent < min_common_indent {
min_common_indent = line_indent

View file

@ -9,6 +9,38 @@ const (
digit_pairs = '00102030405060708090011121314151617181910212223242526272829203132333435363738393041424344454647484940515253545556575859506162636465666768696071727374757677787970818283848586878889809192939495969798999'
)
pub const (
min_i8 = i8(-128)
max_i8 = i8(127)
min_i16 = i16(-32768)
max_i16 = i16(32767)
min_i32 = i32(-2147483648)
max_i32 = i32(2147483647)
min_int = min_i32
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
min_i64 = i64(-9223372036854775807 - 1)
max_i64 = i64(9223372036854775807)
min_u8 = u8(0)
max_u8 = u8(255)
min_u16 = u16(0)
max_u16 = u16(65535)
min_u32 = u32(0)
max_u32 = u32(4294967295)
min_u64 = u64(0)
max_u64 = u64(18446744073709551615)
)
// This implementation is the quickest with gcc -O2
// str_l returns the string representation of the integer nn with max chars.
[direct_array_access; inline]
@ -142,7 +174,7 @@ pub fn (nn i64) str() string {
mut d := i64(0)
if n == 0 {
return '0'
} else if n == i64(-9223372036854775807 - 1) {
} else if n == min_i64 {
// math.min_i64
return '-9223372036854775808'
}

View file

@ -4,7 +4,6 @@ module x11
import time
import sync
import math
$if freebsd {
#flag -I/usr/local/include
@ -433,7 +432,7 @@ fn (cb &Clipboard) pick_target(prop Property) Atom {
mut to_be_requested := Atom(0)
// This is higher than the maximum priority.
mut priority := int(math.max_i32)
mut priority := int(max_i32)
for i in 0 .. prop.nitems {
// See if this data type is allowed and of higher priority (closer to zero)

View file

@ -97,8 +97,7 @@ fn subtract_align_last_byte_in_place(mut a []u32, b []u32) {
mut new_carry := u32(0)
offset := a.len - b.len
for index := a.len - b.len; index < a.len; index++ {
if a[index] < (b[index - offset] + carry)
|| (b[index - offset] == math.max_u32 && carry > 0) {
if a[index] < (b[index - offset] + carry) || (b[index - offset] == max_u32 && carry > 0) {
new_carry = 1
} else {
new_carry = 0

View file

@ -22,6 +22,7 @@ const (
m4 = u64(0x0000ffff0000ffff)
)
// TODO: this consts should be taken from int.v
const (
// save importing math mod just for these
max_u32 = u32(4294967295)

View file

@ -40,7 +40,8 @@ pub const (
smallest_non_zero_f64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2**(1023 - 1 + 52)
)
// Integer limit values
[deprecated: 'use built-in constants instead (e.g. min_i8 instead of math.min_i8)']
[deprecated_after: '2023-12-31']
pub const (
min_i8 = i8(-128)
max_i8 = i8(127)

View file

@ -84,7 +84,7 @@ pub fn approximate_with_eps(val f64, eps f64) Fraction {
if eps < 0.0 {
panic('Epsilon value cannot be negative.')
}
if math.abs(val) > math.max_i64 {
if math.abs(val) > max_i64 {
panic('Value out of range.')
}
// The integer part is separated first. Then we process the fractional

View file

@ -1766,33 +1766,33 @@ fn (mut c Checker) enum_decl(mut node ast.EnumDecl) {
senum_type := c.table.type_to_str(node.typ)
match node.typ {
ast.i8_type {
signed, enum_imin, enum_imax = true, -128, 0x7F
signed, enum_imin, enum_imax = true, min_i8, max_i8
}
ast.i16_type {
signed, enum_imin, enum_imax = true, -32_768, 0x7FFF
signed, enum_imin, enum_imax = true, min_i16, max_i16
}
ast.int_type {
signed, enum_imin, enum_imax = true, -2_147_483_648, 0x7FFF_FFFF
signed, enum_imin, enum_imax = true, min_i32, max_i32
}
ast.i64_type {
signed, enum_imin, enum_imax = true, i64(-9223372036854775807 - 1), i64(0x7FFF_FFFF_FFFF_FFFF)
signed, enum_imin, enum_imax = true, min_i64, max_i64
}
//
ast.u8_type {
signed, enum_umin, enum_umax = false, 0, 0xFF
signed, enum_umin, enum_umax = false, min_u8, max_u8
}
ast.u16_type {
signed, enum_umin, enum_umax = false, 0, 0xFFFF
signed, enum_umin, enum_umax = false, min_u16, max_u16
}
ast.u32_type {
signed, enum_umin, enum_umax = false, 0, 0xFFFF_FFFF
signed, enum_umin, enum_umax = false, min_u32, max_u32
}
ast.u64_type {
signed, enum_umin, enum_umax = false, 0, 0xFFFF_FFFF_FFFF_FFFF
signed, enum_umin, enum_umax = false, min_u64, max_u64
}
else {
if senum_type == 'i32' {
signed, enum_imin, enum_imax = true, -2_147_483_648, 0x7FFF_FFFF
signed, enum_imin, enum_imax = true, min_i32, max_i32
} else {
c.error('`${senum_type}` is not one of `i8`,`i16`,`i32`,`int`,`i64`,`u8`,`u16`,`u32`,`u64`',
node.typ_pos)

View file

@ -590,7 +590,7 @@ fn (e Eval) type_to_size(typ ast.Type) u64 {
32
})
}
ast.i8_type_idx, ast.i16_type_idx, ast.int_type_idx, ast.i64_type_idx {
ast.i8_type_idx, ast.i16_type_idx, ast.int_type_idx, ast.i32_type_idx, ast.i64_type_idx {
return u64(math.exp2(f64(typ - 2))) // this formula converts the type number to the bitsize
}
ast.u8_type_idx, ast.u16_type_idx, ast.u32_type_idx, ast.u64_type_idx {