Skip to content

Commit

Permalink
Refactor, simplify and optimize u128toa10 & u256toa10 helpers (#76)
Browse files Browse the repository at this point in the history
* refactor, simplify and optimize u128toa10 & u256toa10

* refactor
  • Loading branch information
MaxGraey authored Jul 17, 2022
1 parent 39d5a83 commit 7910485
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
6 changes: 3 additions & 3 deletions assembly/integer/u128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
__divmod_rem_hi,
} from '../globals';

import { atou128, u128toa10 } from '../utils';
import { atou128, u128toDecimalString } from '../utils';

@lazy const HEX_CHARS = '0123456789abcdef';

Expand Down Expand Up @@ -838,7 +838,7 @@ export class u128 {
*/
@inline
as<T>(): T {
var dummy: T;
var dummy!: T;
if (dummy instanceof bool) return <T>this.toBool();
else if (dummy instanceof i8) return <T>this.toI64();
else if (dummy instanceof u8) return <T>this.toU64();
Expand Down Expand Up @@ -939,6 +939,6 @@ export class u128 {
}
return result;
}
return u128toa10(this);
return u128toDecimalString(this);
}
}
6 changes: 3 additions & 3 deletions assembly/integer/u256.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { i128 } from './i128';
import { u128 } from './u128';
import { u256toa10 } from "../utils";
import { u256toDecimalString } from "../utils";

@lazy const HEX_CHARS = '0123456789abcdef';

Expand Down Expand Up @@ -567,7 +567,7 @@ export class u256 {
*/
@inline
as<T>(): T {
var dummy: T;
var dummy!: T;
if (dummy instanceof bool) return <T>this.toBool();
else if (dummy instanceof i8) return <T>this.toI64();
else if (dummy instanceof u8) return <T>this.toU64();
Expand Down Expand Up @@ -643,6 +643,6 @@ export class u256 {
}
return result;
}
return u256toa10(this);
return u256toDecimalString(this);
}
}
52 changes: 29 additions & 23 deletions assembly/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,51 +80,57 @@ export function isPowerOverflow128(base: u128, exponent: i32): bool {
}

// helper function for utoa
function processU64(digits: Int8Array, value: u64): void {
function processU64(digits: Uint8Array, value: u64): void {
var length = digits.length - 1;
for (let i = 63; i != -1; i--) {
for (let j = 0; j <= length; j++) {
digits[j] = digits[j] + (i8(digits[j] >= 5) * 3);
for (let i = 63; i != -1; --i) {
for (let j = 0; j <= length; ++j) {
unchecked(digits[j] += (u8(digits[j] >= 5) * 3));
}
for (let j = length; j != -1; j--) {
digits[j] = digits[j] << 1;
if (j < length) digits[j + 1] = (digits[j+1] | i8(digits[j] > 15));
digits[j] = digits[j] & 15;
for (let j = length; j != -1; --j) {
let d = unchecked(digits[j]) << 1;
if (j < length) unchecked(digits[j + 1] |= u8(d > 15));
unchecked(digits[j] = d & 15);
}
digits[0] = digits[0] + i8((value & (1 << i)) != 0);
unchecked(digits[0] += u8((value & (1 << i)) != 0));
}
}

export function u128toa10(value: u128): string {
export function u128toDecimalString(value: u128): string {
var length = 40;
var digits = new Int8Array(length);
var digits = new Uint8Array(length);
var result = "", start = false;

processU64(digits, value.hi);
processU64(digits, value.lo);

var result = "";
var start = false;
for (let i = length - 1; i != -1; i--) {
if (!start && digits[i] > 0) start = true;
if (start) result = result.concat(HEX_CHARS.charAt(digits[i]));
for (let i = length - 1; i != -1; --i) {
let d = unchecked(digits[i]);
if (!start && d != 0) start = true;
if (start) {
assert(<u32>d <= 9);
result += String.fromCharCode(0x30 + d);
}
}
return result;
}

export function u256toa10(value: u256): string {
export function u256toDecimalString(value: u256): string {
var length = 78;
var digits = new Int8Array(length);
var digits = new Uint8Array(length);
var result = "", start = false;

processU64(digits, value.hi2);
processU64(digits, value.hi1);
processU64(digits, value.lo2);
processU64(digits, value.lo1);

var result = "";
var start = false;
for (let i = length - 1; i != -1; i--) {
if (!start && digits[i] > 0) start = true;
if (start) result = result.concat(HEX_CHARS.charAt(digits[i]));
for (let i = length - 1; i != -1; --i) {
let d = unchecked(digits[i]);
if (!start && d != 0) start = true;
if (start) {
assert(<u32>d <= 9);
result += String.fromCharCode(0x30 + d);
}
}
return result;
}
Expand Down

0 comments on commit 7910485

Please sign in to comment.