Skip to content

Commit be38ba5

Browse files
committed
Another little bit faster.
1 parent a682002 commit be38ba5

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

core/math/big/radix.odin

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,15 +616,15 @@ _itoa_raw_full :: proc(a: ^Int, radix: i8, buffer: []u8, zero_terminate := false
616616
context.allocator = allocator
617617

618618
// Calculate largest radix^n that fits within _DIGIT_BITS
619-
divisor := ITOA_DIVISOR
619+
divisor := _WORD(ITOA_DIVISOR)
620620
digit_count := ITOA_COUNT
621621
_radix := DIGIT(radix)
622622

623623
if radix != 10 {
624624
i := _WORD(1)
625625
digit_count = -1
626626
for i < _WORD(1 << _DIGIT_BITS) {
627-
divisor = DIGIT(i)
627+
divisor = _WORD(i)
628628
i *= _WORD(radix)
629629
digit_count += 1
630630
}
@@ -644,11 +644,30 @@ _itoa_raw_full :: proc(a: ^Int, radix: i8, buffer: []u8, zero_terminate := false
644644
temp.sign = .Zero_or_Positive
645645
}
646646

647+
q := &Int{}
648+
defer internal_destroy(q)
649+
647650
remainder: DIGIT
648651
for {
649-
if remainder, err = internal_divmod(temp, temp, divisor); err != nil {
650-
return len(buffer) - available, err
652+
internal_grow(q, temp.used) or_return
653+
q.used = temp.used
654+
q.sign = temp.sign
655+
656+
w := _WORD(0)
657+
658+
for ix := temp.used - 1; ix >= 0; ix -= 1 {
659+
t := DIGIT(0)
660+
w = (w << _WORD(_DIGIT_BITS) | _WORD(temp.digit[ix]))
661+
if w >= divisor {
662+
t = DIGIT(w / divisor)
663+
w -= _WORD(t) * divisor
664+
}
665+
q.digit[ix] = t
651666
}
667+
remainder = DIGIT(w)
668+
669+
internal_clamp(q)
670+
q, temp = temp, q
652671

653672
count := digit_count
654673
for available > 0 && count > 0 {

0 commit comments

Comments
 (0)