aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgingerBill <bill@gingerbill.org>2022-09-27 22:31:46 +0100
committergingerBill <bill@gingerbill.org>2022-09-27 22:31:46 +0100
commitc4d19dfa925ffe44c04fcda90543a272b7165274 (patch)
treec493697ab16154885febf04511f7f61e64b4bbbb
parent35e70f4be17f4123ec25108d664864f13b38087e (diff)
Use `uint` instead of `int` to improve code generation for bounds checking
-rw-r--r--core/bytes/buffer.odin7
-rw-r--r--core/bytes/bytes.odin2
-rw-r--r--core/runtime/error_checks.odin6
-rw-r--r--core/slice/slice.odin4
-rw-r--r--core/strings/strings.odin2
5 files changed, 9 insertions, 12 deletions
diff --git a/core/bytes/buffer.odin b/core/bytes/buffer.odin
index d9f195871..bba834f7e 100644
--- a/core/bytes/buffer.odin
+++ b/core/bytes/buffer.odin
@@ -240,14 +240,11 @@ buffer_read_ptr :: proc(b: ^Buffer, ptr: rawptr, size: int) -> (n: int, err: io.
buffer_read_at :: proc(b: ^Buffer, p: []byte, offset: int) -> (n: int, err: io.Error) {
b.last_read = .Invalid
- if offset < 0 || offset >= len(b.buf) {
+ if uint(offset) >= len(b.buf) {
err = .Invalid_Offset
return
}
-
- if 0 <= offset && offset < len(b.buf) {
- n = copy(p, b.buf[offset:])
- }
+ n = copy(p, b.buf[offset:])
if n > 0 {
b.last_read = .Read
}
diff --git a/core/bytes/bytes.odin b/core/bytes/bytes.odin
index f1737f3c5..d39f01b06 100644
--- a/core/bytes/bytes.odin
+++ b/core/bytes/bytes.odin
@@ -638,7 +638,7 @@ trim_left_proc :: proc(s: []byte, p: proc(rune) -> bool) -> []byte {
index_rune :: proc(s: []byte, r: rune) -> int {
switch {
- case 0 <= r && r < utf8.RUNE_SELF:
+ case u32(r) < utf8.RUNE_SELF:
return index_byte(s, byte(r))
case r == utf8.RUNE_ERROR:
diff --git a/core/runtime/error_checks.odin b/core/runtime/error_checks.odin
index 0d0b39072..8539c724d 100644
--- a/core/runtime/error_checks.odin
+++ b/core/runtime/error_checks.odin
@@ -18,7 +18,7 @@ type_assertion_trap :: proc "contextless" () -> ! {
bounds_check_error :: proc "contextless" (file: string, line, column: i32, index, count: int) {
- if 0 <= index && index < count {
+ if uint(index) < uint(count) {
return
}
@(cold)
@@ -99,8 +99,8 @@ dynamic_array_expr_error :: proc "contextless" (file: string, line, column: i32,
matrix_bounds_check_error :: proc "contextless" (file: string, line, column: i32, row_index, column_index, row_count, column_count: int) {
- if 0 <= row_index && row_index < row_count &&
- 0 <= column_index && column_index < column_count {
+ if uint(row_index) < uint(row_count) &&
+ uint(column_index) < uint(column_count) {
return
}
@(cold)
diff --git a/core/slice/slice.odin b/core/slice/slice.odin
index abc84787f..032a8ca6e 100644
--- a/core/slice/slice.odin
+++ b/core/slice/slice.odin
@@ -321,14 +321,14 @@ last_ptr :: proc(array: $T/[]$E) -> ^E {
}
get :: proc(array: $T/[]$E, index: int) -> (value: E, ok: bool) {
- if 0 <= index && index < len(array) {
+ if uint(index) < len(array) {
value = array[index]
ok = true
}
return
}
get_ptr :: proc(array: $T/[]$E, index: int) -> (value: ^E, ok: bool) {
- if 0 <= index && index < len(array) {
+ if uint(index) < len(array) {
value = &array[index]
ok = true
}
diff --git a/core/strings/strings.odin b/core/strings/strings.odin
index a0f553a68..ba357e027 100644
--- a/core/strings/strings.odin
+++ b/core/strings/strings.odin
@@ -760,7 +760,7 @@ last_index_byte :: proc(s: string, c: byte) -> int {
*/
index_rune :: proc(s: string, r: rune) -> int {
switch {
- case 0 <= r && r < utf8.RUNE_SELF:
+ case u32(r) < utf8.RUNE_SELF:
return index_byte(s, byte(r))
case r == utf8.RUNE_ERROR: