aboutsummaryrefslogtreecommitdiff
path: root/core/math
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2021-08-07 23:07:22 +0200
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2021-08-11 20:59:53 +0200
commit53bf66ce1ec8708284d2dcf726240fcbbcd90d05 (patch)
treeca38f80111036f3dafa3abe7e3937280578b6c25 /core/math
parentfd95f50c560550a361b109d1f47c5d31c7621fe4 (diff)
big: Prettify `internal_cmp_digit`.
Diffstat (limited to 'core/math')
-rw-r--r--core/math/big/build.bat6
-rw-r--r--core/math/big/compare.odin1
-rw-r--r--core/math/big/internal.odin36
3 files changed, 19 insertions, 24 deletions
diff --git a/core/math/big/build.bat b/core/math/big/build.bat
index a12e3262a..1921803b5 100644
--- a/core/math/big/build.bat
+++ b/core/math/big/build.bat
@@ -1,10 +1,10 @@
@echo off
-:odin run . -vet
+odin run . -vet
: -o:size
:odin build . -build-mode:shared -show-timings -o:minimal -no-bounds-check
:odin build . -build-mode:shared -show-timings -o:size -no-bounds-check
:odin build . -build-mode:shared -show-timings -o:size
-odin build . -build-mode:shared -show-timings -o:speed -no-bounds-check
+:odin build . -build-mode:shared -show-timings -o:speed -no-bounds-check
:odin build . -build-mode:shared -show-timings -o:speed
-python test.py \ No newline at end of file
+:python test.py \ No newline at end of file
diff --git a/core/math/big/compare.odin b/core/math/big/compare.odin
index 1b0aa6877..2581a3c38 100644
--- a/core/math/big/compare.odin
+++ b/core/math/big/compare.odin
@@ -89,6 +89,7 @@ int_compare_digit :: proc(a: ^Int, b: DIGIT) -> (comparison: int, err: Error) {
return #force_inline internal_cmp_digit(a, b), nil;
}
+int_cmp_digit :: int_compare_digit;
/*
Compare the magnitude of two `Int`s, unsigned.
diff --git a/core/math/big/internal.odin b/core/math/big/internal.odin
index 1ba1c7fa3..93fa04a8d 100644
--- a/core/math/big/internal.odin
+++ b/core/math/big/internal.odin
@@ -1046,40 +1046,34 @@ internal_compare :: proc { internal_int_compare, internal_int_compare_digit, };
internal_cmp :: internal_compare;
/*
- Compare an `Int` to an unsigned number upto the size of the backing type.
+ Compare an `Int` to an unsigned number upto `DIGIT & _MASK`.
+ Returns -1 if `a` < `b`, 0 if `a` == `b` and 1 if `b` > `a`.
- Returns -1 if `a` < `b`, 0 if `a` == `b` and 1 if `b` > `a`.
-
- Expects `a` and `b` both to be valid `Int`s, i.e. initialized and not `nil`.
+ Expects: `a` and `b` both to be valid `Int`s, i.e. initialized and not `nil`.
*/
internal_int_compare_digit :: #force_inline proc(a: ^Int, b: DIGIT) -> (comparison: int) {
+ a_is_negative := #force_inline internal_is_negative(a);
+
+ switch {
/*
- Compare based on sign.
+ Compare based on sign first.
*/
-
- if #force_inline internal_is_negative(a) { return -1; }
-
+ case a_is_negative: return -1;
/*
- Compare based on magnitude.
+ Then compare on magnitude.
*/
- if a.used > 1 { return +1; }
-
+ case a.used > 1: return +1;
/*
- Compare the only digit in `a` to `b`.
+ We have only one digit. Compare it against `b`.
*/
- switch {
- case a.digit[0] < b:
- return -1;
- case a.digit[0] == b:
- return 0;
- case a.digit[0] > b:
- return +1;
- case:
+ case a.digit[0] < b: return -1;
+ case a.digit[0] == b: return 0;
+ case a.digit[0] > b: return +1;
/*
Unreachable.
Just here because Odin complains about a missing return value at the bottom of the proc otherwise.
*/
- return;
+ case: return;
}
}
internal_compare_digit :: proc { internal_int_compare_digit, };