Skip to content

Commit

Permalink
refactor: Rename dtoa/itoa_stream to dtoa/itoa_buffered & simplify th…
Browse files Browse the repository at this point in the history
…em (#1339)

BREAKING CHANGE: The internal `dtoa/itoa_stream` helpers (unsafe `Number#toString`) in `util/number` have been renamed to `dtoa/itoa_buffered`, don't take a redundant offset argument anymore, and the underlying core implementation helpers are no longer exposed. Usage now is: `dtoa_buffered(buffer + byteOffset, value)`
  • Loading branch information
MaxGraey authored Jun 14, 2020
1 parent e5bfe15 commit 0e398c4
Show file tree
Hide file tree
Showing 9 changed files with 851 additions and 847 deletions.
16 changes: 7 additions & 9 deletions std/assembly/util/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export function utoa32_dec_core(buffer: usize, num: u32, offset: usize): void {

// @ts-ignore: decorator
@inline
export function utoa32_hex_core(buffer: usize, num: u32, offset: usize): void {
function utoa32_hex_core(buffer: usize, num: u32, offset: usize): void {
if (ASC_SHRINK_LEVEL >= 1) {
utoa_hex_simple<u32>(buffer, num, offset);
} else {
Expand All @@ -300,7 +300,7 @@ export function utoa32_hex_core(buffer: usize, num: u32, offset: usize): void {

// @ts-ignore: decorator
@inline
export function utoa64_dec_core(buffer: usize, num: u64, offset: usize): void {
function utoa64_dec_core(buffer: usize, num: u64, offset: usize): void {
if (ASC_SHRINK_LEVEL >= 1) {
utoa_dec_simple<u64>(buffer, num, offset);
} else {
Expand All @@ -310,15 +310,15 @@ export function utoa64_dec_core(buffer: usize, num: u64, offset: usize): void {

// @ts-ignore: decorator
@inline
export function utoa64_hex_core(buffer: usize, num: u64, offset: usize): void {
function utoa64_hex_core(buffer: usize, num: u64, offset: usize): void {
if (ASC_SHRINK_LEVEL >= 1) {
utoa_hex_simple<u64>(buffer, num, offset);
} else {
utoa_hex_lut(buffer, num, offset);
}
}

export function utoa64_any_core(buffer: usize, num: u64, offset: usize, radix: i32): void {
function utoa64_any_core(buffer: usize, num: u64, offset: usize, radix: i32): void {
const lut = changetype<usize>(ANY_DIGITS);
var base = u64(radix);
if ((radix & (radix - 1)) == 0) { // for radix which pow of two
Expand Down Expand Up @@ -710,7 +710,7 @@ function prettify(buffer: usize, length: i32, k: i32): i32 {
}
}

export function dtoa_core(buffer: usize, value: f64): i32 {
function dtoa_core(buffer: usize, value: f64): i32 {
var sign = i32(value < 0);
if (sign) {
value = -value;
Expand All @@ -736,8 +736,7 @@ export function dtoa(value: f64): String {
return result;
}

export function itoa_stream<T extends number>(buffer: usize, offset: usize, value: T): u32 {
buffer += offset << 1;
export function itoa_buffered<T extends number>(buffer: usize, value: T): u32 {
var sign: u32 = 0;
if (isSigned<T>()) {
sign = u32(value < 0);
Expand Down Expand Up @@ -783,8 +782,7 @@ export function itoa_stream<T extends number>(buffer: usize, offset: usize, valu
return decimals;
}

export function dtoa_stream(buffer: usize, offset: usize, value: f64): u32 {
buffer += offset << 1;
export function dtoa_buffered(buffer: usize, value: f64): u32 {
if (value == 0) {
store<u16>(buffer, CharCode._0);
store<u16>(buffer, CharCode.DOT, 2);
Expand Down
18 changes: 7 additions & 11 deletions std/assembly/util/string.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { itoa32, utoa32, itoa64, utoa64, dtoa, itoa_stream, dtoa_stream, MAX_DOUBLE_LENGTH } from "./number";
import { itoa32, utoa32, itoa64, utoa64, dtoa, itoa_buffered, dtoa_buffered, MAX_DOUBLE_LENGTH } from "./number";
import { ipow32 } from "../math";

// All tables are stored as two staged lookup tables (static tries)
Expand Down Expand Up @@ -889,7 +889,7 @@ export function joinIntegerArray<T>(dataStart: usize, length: i32, separator: st
for (let i = 0; i < lastIndex; ++i) {
value = load<T>(dataStart + (<usize>i << alignof<T>()));
// @ts-ignore: type
offset += itoa_stream<T>(changetype<usize>(result), offset, value);
offset += itoa_buffered<T>(changetype<usize>(result) + (<usize>offset << 1), value);
if (sepLen) {
memory.copy(
changetype<usize>(result) + (<usize>offset << 1),
Expand All @@ -901,7 +901,7 @@ export function joinIntegerArray<T>(dataStart: usize, length: i32, separator: st
}
value = load<T>(dataStart + (<usize>lastIndex << alignof<T>()));
// @ts-ignore: type
offset += itoa_stream<T>(changetype<usize>(result), offset, value);
offset += itoa_buffered<T>(changetype<usize>(result) + (<usize>offset << 1), value);
if (estLen > offset) return result.substring(0, offset);
return result;
}
Expand All @@ -924,10 +924,8 @@ export function joinFloatArray<T>(dataStart: usize, length: i32, separator: stri
var value: T;
for (let i = 0; i < lastIndex; ++i) {
value = load<T>(dataStart + (<usize>i << alignof<T>()));
offset += dtoa_stream(changetype<usize>(result), offset,
// @ts-ignore: type
value
);
// @ts-ignore: type
offset += dtoa_buffered(changetype<usize>(result) + (<usize>offset << 1), value);
if (sepLen) {
memory.copy(
changetype<usize>(result) + (<usize>offset << 1),
Expand All @@ -938,10 +936,8 @@ export function joinFloatArray<T>(dataStart: usize, length: i32, separator: stri
}
}
value = load<T>(dataStart + (<usize>lastIndex << alignof<T>()));
offset += dtoa_stream(changetype<usize>(result), offset,
// @ts-ignore: type
value
);
// @ts-ignore: type
offset += dtoa_buffered(changetype<usize>(result) + (<usize>offset << 1), value);
if (estLen > offset) return result.substring(0, offset);
return result;
}
Expand Down
12 changes: 6 additions & 6 deletions std/assembly/wasi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
import {
MAX_DOUBLE_LENGTH,
decimalCount32,
dtoa_stream
dtoa_buffered
} from "util/number";

// @ts-ignore: decorator
Expand Down Expand Up @@ -81,19 +81,19 @@ function trace( // eslint-disable-line @typescript-eslint/no-unused-vars
fd_write(2, iovPtr, 1, lenPtr);
if (n) {
store<u8>(bufPtr++, 0x20); // space
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_stream(bufPtr, 0, a0), bufPtr);
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_buffered(bufPtr, a0), bufPtr);
fd_write(2, iovPtr, 1, lenPtr);
if (n > 1) {
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_stream(bufPtr, 0, a1), bufPtr);
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_buffered(bufPtr, a1), bufPtr);
fd_write(2, iovPtr, 1, lenPtr);
if (n > 2) {
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_stream(bufPtr, 0, a2), bufPtr);
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_buffered(bufPtr, a2), bufPtr);
fd_write(2, iovPtr, 1, lenPtr);
if (n > 3) {
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_stream(bufPtr, 0, a3), bufPtr);
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_buffered(bufPtr, a3), bufPtr);
fd_write(2, iovPtr, 1, lenPtr);
if (n > 4) {
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_stream(bufPtr, 0, a4), bufPtr);
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_buffered(bufPtr, a4), bufPtr);
fd_write(2, iovPtr, 1, lenPtr);
}
}
Expand Down
Loading

0 comments on commit 0e398c4

Please sign in to comment.