Skip to content

Commit 58011e9

Browse files
MaxGraeydcodeIO
authored andcommitted
Optimize pow10 helper of strtod (#1018)
1 parent 686b48d commit 58011e9

File tree

4 files changed

+3153
-2933
lines changed

4 files changed

+3153
-2933
lines changed

std/assembly/util/string.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,12 @@ export const enum CharCode {
6969
z = 0x7A
7070
}
7171

72-
// 9 * 8 = 72 bytes
72+
// 23 * 8 = 184 bytes
7373
// @ts-ignore: decorator
74-
@lazy
75-
const Powers10Hi: f64[] = [1, 1e32, 1e64, 1e96, 1e128, 1e160, 1e192, 1e224, 1e256, 1e288];
76-
// 32 * 8 = 256 bytes
77-
// @ts-ignore: decorator
78-
@lazy
79-
const Powers10Lo: f64[] = [
74+
@lazy const Powers10: f64[] = [
8075
1e00, 1e01, 1e02, 1e03, 1e04, 1e05, 1e06, 1e07, 1e08, 1e09,
8176
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
82-
1e20, 1e21, 1e22, 1e23, 1e24, 1e25, 1e26, 1e27, 1e28, 1e29,
83-
1e30, 1e31
77+
1e20, 1e21, 1e22
8478
];
8579

8680
export function compareImpl(str1: string, index1: usize, str2: string, index2: usize, len: usize): i32 {
@@ -600,15 +594,11 @@ function fixmul(a: u64, b: u32): u64 {
600594
}
601595

602596
// @ts-ignore: decorator
597+
@inline
603598
function pow10(n: i32): f64 {
604-
// @ts-ignore: type
605-
const hi = Powers10Hi.dataStart as usize;
606-
// @ts-ignore: type
607-
const lo = Powers10Lo.dataStart as usize;
608-
return (
609-
load<f64>(hi + ((n >> 5) << alignof<f64>())) *
610-
load<f64>(lo + ((n & 31) << alignof<f64>()))
611-
);
599+
// argument `n` should bounds in [0, 22] range
600+
// @ts-ignore: cast
601+
return load<f64>(Powers10.dataStart as usize + (n << alignof<f64>()));
612602
}
613603

614604
// @ts-ignore: decorator

0 commit comments

Comments
 (0)