Explorar o código

big: Continuing to refactor.

Jeroen van Rijn %!s(int64=4) %!d(string=hai) anos
pai
achega
4eadd0867d

+ 155 - 0
core/math/big/api.odin

@@ -0,0 +1,155 @@
+package big
+
+/*
+	Copyright 2021 Jeroen van Rijn <[email protected]>.
+	Made available under Odin's BSD-2 license.
+
+	An arbitrary precision mathematics implementation in Odin.
+	For the theoretical underpinnings, see Knuth's The Art of Computer Programming, Volume 2, section 4.3.
+	The code started out as an idiomatic source port of libTomMath, which is in the public domain, with thanks.
+
+	This file collects public procs into proc maps, as well as any of their aliases.
+/*
+
+*/
+	=== === === === === === === === === === === === === === === === === === === === === === === ===
+	                                    Basic arithmetic.
+	                                    See `basic.odin`.
+	=== === === === === === === === === === === === === === === === === === === === === === === ===
+*/
+
+/*
+	err = add(dest, a, b);
+*/
+add :: proc {
+	/*
+		int_add :: proc(dest, a, b: ^Int) -> (err: Error)
+	*/
+	int_add,
+	/*
+		int_add_digit :: proc(dest, a: ^Int, digit: DIGIT) -> (err: Error)
+	*/
+	int_add_digit,
+};
+
+/*
+	err = sub(dest, a, b);
+*/
+sub :: proc {
+	/*
+		int_sub :: proc(dest, a, b: ^Int) -> (err: Error)
+	*/
+	int_sub,
+	/*
+		int_sub_digit :: proc(dest, a: ^Int, digit: DIGIT) -> (err: Error)
+	*/
+	int_sub_digit,
+};
+
+/*
+	=== === === === === === === === === === === === === === === === === === === === === === === ===
+	                                        Comparisons.
+	                                    See `compare.odin`.
+	=== === === === === === === === === === === === === === === === === === === === === === === ===
+*/
+
+is_initialized :: proc {
+	/*
+		int_is_initialized :: proc(a: ^Int) -> bool
+	*/
+	int_is_initialized,
+};
+
+is_zero :: proc {
+	/*
+		int_is_zero :: proc(a: ^Int) -> bool
+	*/
+	int_is_zero,
+};
+
+is_positive :: proc {
+	/*
+		int_is_positive :: proc(a: ^Int) -> bool
+	*/
+	int_is_positive,
+};
+is_pos :: is_positive;
+
+is_negative :: proc {
+	/*
+		int_is_negative :: proc(a: ^Int) -> bool
+	*/
+	int_is_negative,
+};
+is_neg :: is_negative;
+
+is_even :: proc {
+	/*
+		int_is_even :: proc(a: ^Int) -> bool
+	*/
+	int_is_even,
+};
+
+is_odd :: proc {
+	/*
+		int_is_odd :: proc(a: ^Int) -> bool
+	*/
+	int_is_odd,
+};
+
+is_power_of_two :: proc {
+	/*
+		platform_int_is_power_of_two :: proc(a: int) -> bool
+	*/
+	platform_int_is_power_of_two,
+	/*
+		int_is_power_of_two :: proc(a: ^Int) -> (res: bool)
+	*/
+	int_is_power_of_two,
+};
+
+compare :: proc {
+	/*
+		Compare two `Int`s, signed.
+
+		int_compare :: proc(a, b: ^Int) -> Comparison_Flag
+	*/
+	int_compare,
+	/*
+		Compare an `Int` to an unsigned number upto the size of the backing type.
+
+		int_compare_digit :: proc(a: ^Int, u: DIGIT) -> Comparison_Flag
+	*/
+	int_compare_digit,
+};
+cmp :: compare;
+
+compare_magnitude :: proc {
+	/*
+		Compare the magnitude of two `Int`s, unsigned.
+	*/
+	int_compare_magnitude,
+};
+cmp_mag :: compare_magnitude;
+
+/*
+	=== === === === === === === === === === === === === === === === === === === === === === === ===
+	                              Initialization and other helpers.
+	                                    See `helpers.odin`.
+	=== === === === === === === === === === === === === === === === === === === === === === === ===
+*/
+
+destroy :: proc {
+	/*
+		Deallocates the backing memory of an `Int` and resets it.
+
+		int_destroy :: proc(a: ^Int, allocator_zeroes := false, allocator := context.allocator)
+	*/
+	int_destroy,
+};
+
+init :: proc{
+	int_init_sized,
+	int_init_from_integer,
+	int_init_copy,
+};

+ 11 - 15
core/math/big/basic.odin

@@ -4,7 +4,7 @@ package big
 	Copyright 2021 Jeroen van Rijn <[email protected]>.
 	Made available under Odin's BSD-2 license.
 
-	A BigInt implementation in Odin.
+	An arbitrary precision mathematics implementation in Odin.
 	For the theoretical underpinnings, see Knuth's The Art of Computer Programming, Volume 2, section 4.3.
 	The code started out as an idiomatic source port of libTomMath, which is in the public domain, with thanks.
 
@@ -23,7 +23,7 @@ import "core:intrinsics"
 /*
 	High-level addition. Handles sign.
 */
-add_two_ints :: proc(dest, a, b: ^Int) -> (err: Error) {
+int_add :: proc(dest, a, b: ^Int) -> (err: Error) {
 	dest := dest; x := a; y := b;
 	assert_initialized(dest); assert_initialized(a); assert_initialized(b);
 
@@ -32,7 +32,7 @@ add_two_ints :: proc(dest, a, b: ^Int) -> (err: Error) {
 	*/
 	if x.sign == y.sign {
 		dest.sign = x.sign;
-		return _add(dest, x, y);
+		return _int_add(dest, x, y);
 	}
 
 	/*
@@ -45,7 +45,7 @@ add_two_ints :: proc(dest, a, b: ^Int) -> (err: Error) {
 	}
 
 	dest.sign = x.sign;
-	return _sub(dest, x, y);
+	return _int_sub(dest, x, y);
 }
 
 /*
@@ -54,7 +54,7 @@ add_two_ints :: proc(dest, a, b: ^Int) -> (err: Error) {
 
 	dest = a + digit;
 */
-add_digit :: proc(dest, a: ^Int, digit: DIGIT) -> (err: Error) {
+int_add_digit :: proc(dest, a: ^Int, digit: DIGIT) -> (err: Error) {
 	dest := dest; digit := digit;
 	assert_initialized(dest); assert_initialized(a);
 
@@ -165,12 +165,10 @@ add_digit :: proc(dest, a: ^Int, digit: DIGIT) -> (err: Error) {
 	return .OK;
 }
 
-add :: proc{add_two_ints, add_digit};
-
 /*
 	High-level subtraction, dest = number - decrease. Handles signs.
 */
-sub_two_ints :: proc(dest, number, decrease: ^Int) -> (err: Error) {
+int_sub :: proc(dest, number, decrease: ^Int) -> (err: Error) {
 	dest := dest; x := number; y := decrease;
 	assert_initialized(number); assert_initialized(decrease); assert_initialized(dest);
 
@@ -180,7 +178,7 @@ sub_two_ints :: proc(dest, number, decrease: ^Int) -> (err: Error) {
 			In either case, ADD their magnitudes and use the sign of the first number.
 		*/
 		dest.sign = x.sign;
-		return _add(dest, x, y);
+		return _int_add(dest, x, y);
 	}
 
 	/*
@@ -201,7 +199,7 @@ sub_two_ints :: proc(dest, number, decrease: ^Int) -> (err: Error) {
 		*/
 		dest.sign = x.sign;
 	}
-	return _sub(dest, x, y);
+	return _int_sub(dest, x, y);
 }
 
 /*
@@ -210,7 +208,7 @@ sub_two_ints :: proc(dest, number, decrease: ^Int) -> (err: Error) {
 
 	dest = a - digit;
 */
-sub_digit :: proc(dest, a: ^Int, digit: DIGIT) -> (err: Error) {
+int_sub_digit :: proc(dest, a: ^Int, digit: DIGIT) -> (err: Error) {
 	dest := dest; digit := digit;
 	assert_initialized(dest); assert_initialized(a);
 
@@ -296,8 +294,6 @@ sub_digit :: proc(dest, a: ^Int, digit: DIGIT) -> (err: Error) {
 	return .OK;
 }
 
-sub :: proc{sub_two_ints, sub_digit};
-
 /*
 	==========================
 		Low-level routines    
@@ -308,7 +304,7 @@ sub :: proc{sub_two_ints, sub_digit};
 	Low-level addition, unsigned.
 	Handbook of Applied Cryptography, algorithm 14.7.
 */
-_add :: proc(dest, a, b: ^Int) -> (err: Error) {
+_int_add :: proc(dest, a, b: ^Int) -> (err: Error) {
 	dest := dest; x := a; y := b;
 	assert_initialized(a); assert_initialized(b); assert_initialized(dest);
 
@@ -389,7 +385,7 @@ _add :: proc(dest, a, b: ^Int) -> (err: Error) {
 	Low-level subtraction, dest = number - decrease. Assumes |number| > |decrease|.
 	Handbook of Applied Cryptography, algorithm 14.9.
 */
-_sub :: proc(dest, number, decrease: ^Int) -> (err: Error) {
+_int_sub :: proc(dest, number, decrease: ^Int) -> (err: Error) {
 	dest := dest; x := number; y := decrease;
 	assert_initialized(number); assert_initialized(decrease); assert_initialized(dest);
 

+ 1 - 1
core/math/big/bigint.odin

@@ -4,7 +4,7 @@ package big
 	Copyright 2021 Jeroen van Rijn <[email protected]>.
 	Made available under Odin's BSD-2 license.
 
-	A BigInt implementation in Odin.
+	An arbitrary precision mathematics implementation in Odin.
 	For the theoretical underpinnings, see Knuth's The Art of Computer Programming, Volume 2, section 4.3.
 	The code started out as an idiomatic source port of libTomMath, which is in the public domain, with thanks.
 */

+ 36 - 39
core/math/big/compare.odin

@@ -4,32 +4,32 @@ package big
 	Copyright 2021 Jeroen van Rijn <[email protected]>.
 	Made available under Odin's BSD-2 license.
 
-	A BigInt implementation in Odin.
+	An arbitrary precision mathematics implementation in Odin.
 	For the theoretical underpinnings, see Knuth's The Art of Computer Programming, Volume 2, section 4.3.
 	The code started out as an idiomatic source port of libTomMath, which is in the public domain, with thanks.
+
+	This file contains various comparison routines.
 */
 
 import "core:intrinsics"
 
-is_initialized :: proc(a: ^Int) -> bool {
+int_is_initialized :: proc(a: ^Int) -> bool {
 	return a != rawptr(uintptr(0));
 }
 
-is_zero :: proc(a: ^Int) -> bool {
+int_is_zero :: proc(a: ^Int) -> bool {
 	return is_initialized(a) && a.used == 0;
 }
 
-is_positive :: proc(a: ^Int) -> bool {
+int_is_positive :: proc(a: ^Int) -> bool {
 	return is_initialized(a) && a.sign == .Zero_or_Positive;
 }
-is_pos :: is_positive;;
 
-is_negative :: proc(a: ^Int) -> bool {
+int_is_negative :: proc(a: ^Int) -> bool {
 	return is_initialized(a) && a.sign == .Negative;
 }
-is_neg :: is_negative;
 
-is_even :: proc(a: ^Int) -> bool {
+int_is_even :: proc(a: ^Int) -> bool {
 	if is_initialized(a) {
 		if is_zero(a) {
 			return true;
@@ -41,18 +41,18 @@ is_even :: proc(a: ^Int) -> bool {
 	return false;
 }
 
-is_odd :: proc(a: ^Int) -> bool {
+int_is_odd :: proc(a: ^Int) -> bool {
 	if is_initialized(a) {
 		return !is_even(a);
 	}
 	return false;
 }
 
-is_power_of_two_small :: proc(a: int) -> bool {
+platform_int_is_power_of_two :: proc(a: int) -> bool {
 	return ((a) != 0) && (((a) & ((a) - 1)) == 0);
 }
 
-is_power_of_two_large :: proc(a: ^Int) -> (res: bool) {
+int_is_power_of_two :: proc(a: ^Int) -> (res: bool) {
 	/*
 		Early out for Int == 0.
 	*/
@@ -63,7 +63,7 @@ is_power_of_two_large :: proc(a: ^Int) -> (res: bool) {
 	/*
 		For an `Int` to be a power of two, its top limb has to be a power of two.
 	*/
-	if !is_power_of_two_small(int(a.digit[a.used - 1])) {
+	if !platform_int_is_power_of_two(int(a.digit[a.used - 1])) {
 		return false;
 	}
 
@@ -84,12 +84,11 @@ is_power_of_two_large :: proc(a: ^Int) -> (res: bool) {
 	}
 	return true;
 }
-is_power_of_two :: proc{is_power_of_two_small, is_power_of_two_large};
 
 /*
 	Compare two `Int`s, signed.
 */
-compare :: proc(a, b: ^Int) -> Comparison_Flag {
+int_compare :: proc(a, b: ^Int) -> Comparison_Flag {
 	if !is_initialized(a) { return .Uninitialized; }
 	if !is_initialized(b) { return .Uninitialized; }
 
@@ -105,35 +104,11 @@ compare :: proc(a, b: ^Int) -> Comparison_Flag {
 	}
 	return cmp_mag(x, y);
 }
-cmp :: compare;
-
-/*
-	Compare the magnitude of two `Int`s, unsigned.
-*/
-compare_magnitude :: proc(a, b: ^Int) -> Comparison_Flag {
-	if !is_initialized(a) { return .Uninitialized; }
-	if !is_initialized(b) { return .Uninitialized; }
-
-	/* Compare based on used digits */
-	if a.used != b.used {
-		return .Greater_Than if a.used > b.used else .Less_Than;
-	}
-
-	/* Same number of used digits, compare based on their value */
-	for n := a.used - 1; n >= 0; n -= 1 {
-		if a.digit[n] != b.digit[n] {
-			return .Greater_Than if a.digit[n] > b.digit[n] else .Less_Than;
-		}
-	}
-
-   	return .Equal;
-}
-cmp_mag :: compare_magnitude;
 
 /*
 	Compare an `Int` to an unsigned number upto the size of the backing type.
 */
-compare_digit :: proc(a: ^Int, u: DIGIT) -> Comparison_Flag {
+int_compare_digit :: proc(a: ^Int, u: DIGIT) -> Comparison_Flag {
 	if !is_initialized(a) { return .Uninitialized; }
 
 	/* Compare based on sign */
@@ -152,4 +127,26 @@ compare_digit :: proc(a: ^Int, u: DIGIT) -> Comparison_Flag {
 	}
 
 	return .Equal;
+}
+
+/*
+	Compare the magnitude of two `Int`s, unsigned.
+*/
+int_compare_magnitude :: proc(a, b: ^Int) -> Comparison_Flag {
+	if !is_initialized(a) { return .Uninitialized; }
+	if !is_initialized(b) { return .Uninitialized; }
+
+	/* Compare based on used digits */
+	if a.used != b.used {
+		return .Greater_Than if a.used > b.used else .Less_Than;
+	}
+
+	/* Same number of used digits, compare based on their value */
+	for n := a.used - 1; n >= 0; n -= 1 {
+		if a.digit[n] != b.digit[n] {
+			return .Greater_Than if a.digit[n] > b.digit[n] else .Less_Than;
+		}
+	}
+
+   	return .Equal;
 }

+ 28 - 36
core/math/big/helpers.odin

@@ -4,7 +4,7 @@ package big
 	Copyright 2021 Jeroen van Rijn <[email protected]>.
 	Made available under Odin's BSD-2 license.
 
-	A BigInt implementation in Odin.
+	An arbitrary precision mathematics implementation in Odin.
 	For the theoretical underpinnings, see Knuth's The Art of Computer Programming, Volume 2, section 4.3.
 	The code started out as an idiomatic source port of libTomMath, which is in the public domain, with thanks.
 */
@@ -13,13 +13,10 @@ import "core:mem"
 import "core:intrinsics"
 
 /*
-	Deallocates the backing memory of an Int.
+	Deallocates the backing memory of an `Int`.
 */
-destroy :: proc(a: ^Int, allocator_zeroes := false, free_int := true, loc := #caller_location) {
-	if !is_initialized(a) {
-		// Nothing to do.
-		return;
-	}
+int_destroy :: proc(a: ^Int, allocator_zeroes := false, allocator := context.allocator) {
+	if !is_initialized(a) { return; }
 
 	if !allocator_zeroes {
 		mem.zero_slice(a.digit[:]);
@@ -27,15 +24,15 @@ destroy :: proc(a: ^Int, allocator_zeroes := false, free_int := true, loc := #ca
 	free(&a.digit[0]);
 	a.used      = 0;
 	a.allocated = 0;
-	if free_int {
-		free(a);
-	}
+	free(a);
 }
 
 /*
 	Creates and returns a new `Int`.
+	Size is the last parameter so it doesn't conflict with `int_init_from_integer`,
+	and we can say `init(512)` to init & set to 512.
 */
-init_new :: proc(allocator_zeroes := true, allocator := context.allocator, size := _DEFAULT_DIGIT_COUNT) -> (a: ^Int, err: Error) {
+int_init_sized :: proc(allocator_zeroes := true, allocator := context.allocator, size := _DEFAULT_DIGIT_COUNT) -> (a: ^Int, err: Error) {
 	/*
 		Allocating a new variable.
 	*/
@@ -46,10 +43,10 @@ init_new :: proc(allocator_zeroes := true, allocator := context.allocator, size
 	a.used      = 0;
 	a.sign      = .Zero_or_Positive;
 
-	if len(a.digit) != size {
+	if len(a.digit) != int(size) {
 		return a, .Out_of_Memory;
 	}
-	a.allocated = size;
+	a.allocated = int(size);
 
 	if !allocator_zeroes {
 		_zero_unused(a);
@@ -58,17 +55,17 @@ init_new :: proc(allocator_zeroes := true, allocator := context.allocator, size
 }
 
 /*
-	Initialize from a signed or unsigned integer.
+	Initialize from a signed or unsigned platform integer.
 	Inits a new `Int` and then calls the appropriate `set` routine.
 */
-init_from_integer :: proc(src: $T, minimize := false, allocator_zeroes := true, allocator := context.allocator) -> (a: ^Int, err: Error) where intrinsics.type_is_integer(T) {
+int_init_from_integer :: proc(src: $T, minimize := false, allocator_zeroes := true, allocator := context.allocator) -> (a: ^Int, err: Error) where intrinsics.type_is_integer(T) {
 
 	n := _DEFAULT_DIGIT_COUNT;
 	if minimize {
 		n = _MIN_DIGIT_COUNT;
 	}
 
-	a, err = init_new(allocator_zeroes, allocator, n);
+	a, err = init(allocator_zeroes, allocator, n);
 	if err == .OK {
 		set(a, src, minimize);
 	}
@@ -78,43 +75,41 @@ init_from_integer :: proc(src: $T, minimize := false, allocator_zeroes := true,
 /*
 	Initialize an `Int` as a copy from another `Int`.
 */
-init_copy :: proc(src: ^Int, minimize := false, allocator_zeroes := true, allocator := context.allocator) -> (a: ^Int, err: Error) {
+int_init_copy :: proc(src: ^Int, minimize := false, allocator_zeroes := true, allocator := context.allocator) -> (a: ^Int, err: Error) {
 	if !is_initialized(src) {
 		return nil, .Invalid_Input;
 	}
 
-	a, err = init_new(allocator_zeroes, allocator, src.used);
+	a, err = init(allocator_zeroes, allocator, src.used);
 	if err == .OK {
 		copy(a, src);
 	}
 	return;
 }
 
-init :: proc{init_new, init_from_integer, init_copy};
-
 /*
 	Helpers to set an `Int` to a specific value.
 */
-set_integer :: proc(a: ^Int, n: $T, minimize := false, loc := #caller_location) where intrinsics.type_is_integer(T) {
-	n := n;
-	assert_initialized(a, loc);
+int_set_from_integer :: proc(dest: ^Int, src: $T, minimize := false, loc := #caller_location) where intrinsics.type_is_integer(T) {
+	src := src;
+	assert_initialized(dest, loc);
 
-	a.used = 0;
-	a.sign = .Zero_or_Positive if n >= 0 else .Negative;
-	n = abs(n);
+	dest.used = 0;
+	dest.sign = .Zero_or_Positive if src >= 0 else .Negative;
+	src = abs(src);
 
-	for n != 0 {
-		a.digit[a.used] = DIGIT(n) & _MASK;
-		a.used += 1;
-		n >>= _DIGIT_BITS;
+	for src != 0 {
+		dest.digit[dest.used] = DIGIT(src) & _MASK;
+		dest.used += 1;
+		src >>= _DIGIT_BITS;
 	}
 	if minimize {
-		shrink(a);
+		shrink(dest);
 	}
-	_zero_unused(a);
+	_zero_unused(dest);
 }
 
-set :: proc{set_integer};
+set :: proc{int_set_from_integer};
 
 /*
 	Copy one `Int` to another.
@@ -375,9 +370,6 @@ minus_one :: proc(a: ^Int, minimize := false) -> (err: Error) {
 power_of_two :: proc(a: ^Int, power: int) -> (err: Error) {
 	assert_initialized(a);
 
-	/*
-
-	*/
 	if power < 0 || power > _MAX_BIT_COUNT {
 		return .Invalid_Input;
 	}

+ 1 - 1
core/math/big/log.odin

@@ -4,7 +4,7 @@ package big
 	Copyright 2021 Jeroen van Rijn <[email protected]>.
 	Made available under Odin's BSD-2 license.
 
-	A BigInt implementation in Odin.
+	An arbitrary precision mathematics implementation in Odin.
 	For the theoretical underpinnings, see Knuth's The Art of Computer Programming, Volume 2, section 4.3.
 	The code started out as an idiomatic source port of libTomMath, which is in the public domain, with thanks.
 */

+ 1 - 1
core/math/big/logical.odin

@@ -4,7 +4,7 @@ package big
 	Copyright 2021 Jeroen van Rijn <[email protected]>.
 	Made available under Odin's BSD-2 license.
 
-	A BigInt implementation in Odin.
+	An arbitrary precision mathematics implementation in Odin.
 	For the theoretical underpinnings, see Knuth's The Art of Computer Programming, Volume 2, section 4.3.
 	The code started out as an idiomatic source port of libTomMath, which is in the public domain, with thanks.
 

+ 1 - 1
core/math/big/radix.odin

@@ -4,7 +4,7 @@ package big
 	Copyright 2021 Jeroen van Rijn <[email protected]>.
 	Made available under Odin's BSD-2 license.
 
-	A BigInt implementation in Odin.
+	An arbitrary precision mathematics implementation in Odin.
 	For the theoretical underpinnings, see Knuth's The Art of Computer Programming, Volume 2, section 4.3.
 	The code started out as an idiomatic source port of libTomMath, which is in the public domain, with thanks.