Skip to content

Commit

Permalink
Faster base conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
gherynos committed May 16, 2020
1 parent 0d78c6a commit c73e16f
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,11 @@ namespace saltpack {
num.add_word(data.at(i));
}

Num bA((int) alphabet.length());
Num mod;
for (int i = 0; i < c; i++) {

mod = num % bA;
out.insert(0, 1, alphabet.at(mod.to_double()));
num /= bA;
Num::word remainder;
Num::div_mod_half_word(num, alphabet.length(), num, remainder);
out.insert(0, 1, alphabet.at(remainder));
}

return out;
Expand All @@ -111,14 +109,10 @@ namespace saltpack {
auto b = (size_t) floor((double) c * log2(a) / 8);

Num num(0);
Num bA((int) a);
Num pow;
for (int i = (int) c - 1; i >= 0; i--) {

Num digit((unsigned char) alphabet.find(data.at(c - i - 1)));
pow = bA.pow(i);
digit *= pow;
num += digit;
num.mul_word(alphabet.length());
num.add_word(alphabet.find(data.at(c - i - 1)));
}

BYTE_ARRAY out;
Expand Down

0 comments on commit c73e16f

Please sign in to comment.