bigint: itoa support for arbitrary precision if is_power_of_two(radix)

This commit is contained in:
Jeroen van Rijn
2021-08-11 20:59:50 +02:00
parent e600e5947b
commit 5af85aed3d
4 changed files with 82 additions and 16 deletions
+33
View File
@@ -102,6 +102,39 @@ set_integer :: proc(a: ^Int, n: $T, minimize := false, loc := #caller_location)
set :: proc{set_integer};
/*
Helpers to extract values from the `Int`.
*/
extract_bit :: proc(a: ^Int, bit_offset: int) -> (bit: DIGIT, err: Error) {
limb := bit_offset / _DIGIT_BITS;
if limb < 0 || limb >= a.used {
return 0, .Invalid_Input;
}
i := DIGIT(1 << DIGIT((bit_offset % _DIGIT_BITS)));
return 1 if ((a.digit[limb] & i) != 0) else 0, .OK;
}
extract_bits :: proc(a: ^Int, offset, count: int) -> (res: _WORD, err: Error) {
if count > _WORD_BITS || count < 1 {
return 0, .Invalid_Input;
}
v: DIGIT;
e: Error;
for shift := 0; shift < count; shift += 1 {
o := offset + shift;
v, e = extract_bit(a, o);
if e != .OK {
break;
}
res = res + _WORD(v) << uint(shift);
}
return res, e;
}
/*
Resize backing store.
*/