bigint: Add prototypes for immediate add+sub.

This commit is contained in:
Jeroen van Rijn
2021-08-11 20:59:50 +02:00
parent d57e1be89f
commit c5cbd3260a
2 changed files with 40 additions and 14 deletions
+40 -14
View File
@@ -14,14 +14,14 @@ import "core:intrinsics"
/* /*
=========================== ===========================
User-level routines User-level routines
=========================== ===========================
*/ */
/* /*
High-level addition. Handles sign. High-level addition. Handles sign.
*/ */
add :: proc(dest, a, b: ^Int) -> (err: Error) { add_two_ints :: proc(dest, a, b: ^Int) -> (err: Error) {
dest := dest; x := a; y := b; dest := dest; x := a; y := b;
_panic_if_uninitialized(a); _panic_if_uninitialized(b); _panic_if_uninitialized(dest); _panic_if_uninitialized(a); _panic_if_uninitialized(b); _panic_if_uninitialized(dest);
@@ -34,22 +34,35 @@ add :: proc(dest, a, b: ^Int) -> (err: Error) {
} }
/* /*
One positive, the other negative. One positive, the other negative.
Subtract the one with the greater magnitude from the other. Subtract the one with the greater magnitude from the other.
The result gets the sign of the one with the greater magnitude. The result gets the sign of the one with the greater magnitude.
*/ */
if cmp_mag(x, y) == .Less_Than { if cmp_mag(x, y) == .Less_Than {
x, y = y, x; x, y = y, x;
} }
dest.sign = x.sign; dest.sign = x.sign;
return _sub(dest, x, y); return _sub(dest, x, y);
} }
/*
Adds the unsigned `DIGIT` immediate to an `Int`,
such that the `DIGIT` doesn't have to be turned into an `Int` first.
dest = a + digit;
*/
add_digit :: proc(dest, a: ^int, digit: DIGIT) -> (err: Error) {
return .Unimplemented;
}
add :: proc{add_two_ints, add_digit};
/* /*
High-level subtraction, dest = number - decrease. Handles signs. High-level subtraction, dest = number - decrease. Handles signs.
*/ */
sub :: proc(dest, number, decrease: ^Int) -> (err: Error) { sub_two_ints :: proc(dest, number, decrease: ^Int) -> (err: Error) {
dest := dest; x := number; y := decrease; dest := dest; x := number; y := decrease;
_panic_if_uninitialized(number); _panic_if_uninitialized(decrease); _panic_if_uninitialized(dest); _panic_if_uninitialized(number); _panic_if_uninitialized(decrease); _panic_if_uninitialized(dest);
@@ -83,9 +96,22 @@ sub :: proc(dest, number, decrease: ^Int) -> (err: Error) {
return _sub(dest, x, y); return _sub(dest, x, y);
} }
/*
Adds the unsigned `DIGIT` immediate to an `Int`,
such that the `DIGIT` doesn't have to be turned into an `Int` first.
dest = number - decrease;
*/
sub_digit :: proc(dest, number: ^int, decrease: DIGIT) -> (err: Error) {
return .Unimplemented;
}
sub :: proc{sub_two_ints, sub_digit};
/* /*
========================== ==========================
Low-level routines Low-level routines
========================== ==========================
*/ */
@@ -235,5 +261,5 @@ _sub :: proc(dest, number, decrease: ^Int) -> (err: Error) {
Adjust dest.used based on leading zeroes. Adjust dest.used based on leading zeroes.
*/ */
clamp(dest); clamp(dest);
return .OK; return .OK;
} }