Skip to content

Commit 6228233

Browse files
authored
Minor itoa improvements (AssemblyScript#168)
1 parent d7c1c60 commit 6228233

File tree

3 files changed

+168
-248
lines changed

3 files changed

+168
-248
lines changed

std/assembly/internal/itoa.ts

Lines changed: 51 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11

22
import {
33
CharCode,
4-
allocate,
4+
allocate as allocateString,
55
HEADER_SIZE as STRING_HEADER_SIZE
66
} from "./string";
77

8-
import { loadUnsafe } from "./arraybuffer";
8+
import {
9+
loadUnsafe
10+
} from "./arraybuffer";
911

1012
@inline
11-
function getPowers10Table(): u32[] {
12-
return <u32[]>[
13+
function POWERS10(): u32[] {
14+
const table: u32[] = [
1315
1,
1416
10,
1517
100,
@@ -21,6 +23,7 @@ function getPowers10Table(): u32[] {
2123
100000000,
2224
1000000000
2325
];
26+
return table; // inlines to a constant memory offset
2427
}
2528

2629
/*
@@ -38,8 +41,8 @@ function getPowers10Table(): u32[] {
3841
"90", "91", "92", "93", "94", "95", "96", "97", "98", "99"
3942
*/
4043
@inline
41-
function getDigitsTable(): u32[] {
42-
return <u32[]>[
44+
function DIGITS(): u32[] {
45+
const table: u32[] = [
4346
0x00300030, 0x00310030, 0x00320030, 0x00330030, 0x00340030,
4447
0x00350030, 0x00360030, 0x00370030, 0x00380030, 0x00390030,
4548
0x00300031, 0x00310031, 0x00320031, 0x00330031, 0x00340031,
@@ -61,42 +64,40 @@ function getDigitsTable(): u32[] {
6164
0x00300039, 0x00310039, 0x00320039, 0x00330039, 0x00340039,
6265
0x00350039, 0x00360039, 0x00370039, 0x00380039, 0x00390039
6366
];
67+
return table; // inlines to a constant memory offset
6468
}
6569

6670
// Count number of decimals in value
67-
function decimalCount<T>(value: T): i32 {
68-
// make value abs
69-
var sign = value >> (8 * sizeof<T>() - 1);
70-
var v = (value ^ sign) - sign;
71-
var l = 8 * sizeof<T>() - <i32>clz<T>(v | 10); // log2
72-
var t = l * 1233 >>> 12; // log10
73-
74-
var lutbuf = changetype<ArrayBuffer>(getPowers10Table().buffer_);
71+
export function decimalCount<T>(value: T): i32 {
72+
var v = abs<T>(value); // NOP if value is unsigned anyway
73+
var l: usize = 8 * sizeof<T>() - <usize>clz<T>(v | 10); // log2
74+
var t = l * 1233 >>> 12; // log10
75+
76+
var lutbuf = <ArrayBuffer>POWERS10().buffer_;
7577
if (sizeof<T>() <= 4) {
7678
let power = loadUnsafe<u32,T>(lutbuf, t);
77-
t -= <i32>(v < power);
79+
t -= <usize>(v < power);
7880
} else { // sizeof<T>() == 8
7981
let le10 = t <= 10;
80-
let offset = select<i32>(0, 10, le10); // offset = t <= 10 ? 0 : 10
82+
let offset = select<usize>(0, 10, le10); // offset = t <= 10 ? 0 : 10
8183
let factor = select< T >(1, 10000000000, le10); // factor = t <= 10 ? 1 : 10 ^ 10
8284
let power = loadUnsafe<u32,T>(lutbuf, t - offset);
83-
t -= <i32>(v < factor * power);
85+
t -= <usize>(v < factor * power);
8486
}
8587
return t + 1;
8688
}
8789

88-
function utoa32_lut(buffer: usize, num: u32, offset: u32): void {
89-
var r: u32, t: u32, d1: u32, d2: u32;
90-
var lutbuf = changetype<ArrayBuffer>(getDigitsTable().buffer_);
90+
function utoa32_lut(buffer: usize, num: u32, offset: usize): void {
91+
var lutbuf = <ArrayBuffer>DIGITS().buffer_;
9192

9293
while (num >= 10000) {
9394
// in most VMs i32/u32 div and modulo by constant can be shared and simplificate
94-
t = num / 10000;
95-
r = num % 10000;
95+
let t = num / 10000;
96+
let r = num % 10000;
9697
num = t;
9798

98-
d1 = r / 100;
99-
d2 = r % 100;
99+
let d1 = r / 100;
100+
let d2 = r % 100;
100101

101102
let digits1 = loadUnsafe<u32,u64>(lutbuf, d1);
102103
let digits2 = loadUnsafe<u32,u64>(lutbuf, d2);
@@ -106,8 +107,8 @@ function utoa32_lut(buffer: usize, num: u32, offset: u32): void {
106107
}
107108

108109
if (num >= 100) {
109-
t = num / 100;
110-
d1 = num % 100;
110+
let t = num / 100;
111+
let d1 = num % 100;
111112
num = t;
112113
offset -= 2;
113114
let digits = loadUnsafe<u32,u32>(lutbuf, d1);
@@ -125,24 +126,21 @@ function utoa32_lut(buffer: usize, num: u32, offset: u32): void {
125126
}
126127
}
127128

128-
function utoa64_lut(buffer: usize, num: u64, offset: u32): void {
129-
var t: u64, r: u32, b: u32, c: u32;
130-
var b1: u32, b2: u32, c1: u32, c2: u32;
131-
132-
var lutbuf = changetype<ArrayBuffer>(getDigitsTable().buffer_);
129+
function utoa64_lut(buffer: usize, num: u64, offset: usize): void {
130+
var lutbuf = <ArrayBuffer>DIGITS().buffer_;
133131

134132
while (num >= 100000000) {
135-
t = num / 100000000;
136-
r = <u32>(num - t * 100000000);
133+
let t = num / 100000000;
134+
let r = <usize>(num - t * 100000000);
137135
num = t;
138136

139-
b = r / 10000;
140-
c = r % 10000;
137+
let b = r / 10000;
138+
let c = r % 10000;
141139

142-
b1 = b / 100;
143-
b2 = b % 100;
144-
c1 = c / 100;
145-
c2 = c % 100;
140+
let b1 = b / 100;
141+
let b2 = b % 100;
142+
let c1 = c / 100;
143+
let c2 = c % 100;
146144

147145
let digits1 = loadUnsafe<u32,u64>(lutbuf, c1);
148146
let digits2 = loadUnsafe<u32,u64>(lutbuf, c2);
@@ -157,15 +155,13 @@ function utoa64_lut(buffer: usize, num: u64, offset: u32): void {
157155
store<u64>(buffer + (offset << 1), digits1 | (digits2 << 32), STRING_HEADER_SIZE);
158156
}
159157

160-
r = <u32>num;
161-
if (r) utoa32_lut(buffer, r, offset);
158+
utoa32_lut(buffer, <u32>num, offset);
162159
}
163160

164-
function utoa_simple<T>(buffer: usize, num: T, offset: u32): void {
165-
var t: T, r: u32;
161+
function utoa_simple<T>(buffer: usize, num: T, offset: usize): void {
166162
do {
167-
t = num / 10;
168-
r = <u32>(num % 10);
163+
let t = num / 10;
164+
let r = <u32>(num % 10);
169165
num = t;
170166
offset -= 1;
171167
store<u16>(buffer + (offset << 1), CharCode._0 + r, STRING_HEADER_SIZE);
@@ -175,26 +171,26 @@ function utoa_simple<T>(buffer: usize, num: T, offset: u32): void {
175171
@inline
176172
export function utoa32_core(buffer: usize, num: u32, offset: u32): void {
177173
if (ASC_SHRINK_LEVEL >= 1) {
178-
utoa_simple(buffer, num, offset);
174+
utoa_simple(buffer, num, <usize>offset);
179175
} else {
180-
utoa32_lut(buffer, num, offset);
176+
utoa32_lut(buffer, num, <usize>offset);
181177
}
182178
}
183179

184180
@inline
185181
export function utoa64_core(buffer: usize, num: u64, offset: u32): void {
186182
if (ASC_SHRINK_LEVEL >= 1) {
187-
utoa_simple(buffer, num, offset);
183+
utoa_simple(buffer, num, <usize>offset);
188184
} else {
189-
utoa64_lut(buffer, num, offset);
185+
utoa64_lut(buffer, num, <usize>offset);
190186
}
191187
}
192188

193189
export function utoa32(value: u32): string {
194190
if (!value) return "0";
195191

196192
var decimals = decimalCount<u32>(value);
197-
var buffer = allocate(decimals);
193+
var buffer = allocateString(decimals);
198194

199195
utoa32_core(changetype<usize>(buffer), value, decimals);
200196
return changetype<string>(buffer);
@@ -207,7 +203,7 @@ export function itoa32(value: i32): string {
207203
if (isneg) value = -value;
208204

209205
var decimals = decimalCount<u32>(value) + <i32>isneg;
210-
var buffer = allocate(decimals);
206+
var buffer = allocateString(decimals);
211207

212208
utoa32_core(changetype<usize>(buffer), value, decimals);
213209
if (isneg) store<u16>(changetype<usize>(buffer), CharCode.MINUS, STRING_HEADER_SIZE);
@@ -222,11 +218,11 @@ export function utoa64(value: u64): string {
222218
if (value <= u32.MAX_VALUE) {
223219
let value32 = <u32>value;
224220
let decimals = decimalCount<u32>(value32);
225-
buffer = allocate(decimals);
221+
buffer = allocateString(decimals);
226222
utoa32_core(changetype<usize>(buffer), value32, decimals);
227223
} else {
228224
let decimals = decimalCount<u64>(value);
229-
buffer = allocate(decimals);
225+
buffer = allocateString(decimals);
230226
utoa64_core(changetype<usize>(buffer), value, decimals);
231227
}
232228

@@ -243,11 +239,11 @@ export function itoa64(value: i64): string {
243239
if (<u64>value <= <u64>u32.MAX_VALUE) {
244240
let value32 = <u32>value;
245241
let decimals = decimalCount<u32>(value32) + <i32>isneg;
246-
buffer = allocate(decimals);
242+
buffer = allocateString(decimals);
247243
utoa32_core(changetype<usize>(buffer), value32, decimals);
248244
} else {
249245
let decimals = decimalCount<u64>(value) + <i32>isneg;
250-
buffer = allocate(decimals);
246+
buffer = allocateString(decimals);
251247
utoa64_core(changetype<usize>(buffer), value, decimals);
252248
}
253249
if (isneg) store<u16>(changetype<usize>(buffer), CharCode.MINUS, STRING_HEADER_SIZE);

tests/compiler/std/string.optimized.wat

Lines changed: 20 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3744,27 +3744,14 @@
37443744
(i32.const 552)
37453745
)
37463746
(i32.shl
3747-
(tee_local $0
3747+
(tee_local $1
37483748
(i32.shr_u
37493749
(i32.mul
37503750
(i32.sub
37513751
(i32.const 32)
37523752
(i32.clz
37533753
(i32.or
3754-
(tee_local $1
3755-
(i32.sub
3756-
(i32.xor
3757-
(get_local $0)
3758-
(tee_local $0
3759-
(i32.shr_u
3760-
(get_local $0)
3761-
(i32.const 31)
3762-
)
3763-
)
3764-
)
3765-
(get_local $0)
3766-
)
3767-
)
3754+
(get_local $0)
37683755
(i32.const 10)
37693756
)
37703757
)
@@ -3781,9 +3768,9 @@
37813768
)
37823769
(i32.add
37833770
(i32.sub
3784-
(get_local $0)
3771+
(get_local $1)
37853772
(i32.lt_u
3786-
(get_local $1)
3773+
(get_local $0)
37873774
(get_local $2)
37883775
)
37893776
)
@@ -4032,7 +4019,7 @@
40324019
(local $1 i32)
40334020
(local $2 i32)
40344021
(set_local $2
4035-
(i32.le_s
4022+
(i32.le_u
40364023
(tee_local $1
40374024
(i32.shr_u
40384025
(i32.mul
@@ -4041,20 +4028,7 @@
40414028
(i32.wrap/i64
40424029
(i64.clz
40434030
(i64.or
4044-
(tee_local $0
4045-
(i64.sub
4046-
(i64.xor
4047-
(get_local $0)
4048-
(tee_local $0
4049-
(i64.shr_u
4050-
(get_local $0)
4051-
(i64.const 63)
4052-
)
4053-
)
4054-
)
4055-
(get_local $0)
4056-
)
4057-
)
4031+
(get_local $0)
40584032
(i64.const 10)
40594033
)
40604034
)
@@ -4108,7 +4082,7 @@
41084082
(local $4 i32)
41094083
(local $5 i32)
41104084
(local $6 i32)
4111-
(set_local $4
4085+
(set_local $3
41124086
(i32.load
41134087
(i32.const 1472)
41144088
)
@@ -4124,7 +4098,7 @@
41244098
(i32.div_u
41254099
(tee_local $5
41264100
(i32.div_u
4127-
(tee_local $3
4101+
(tee_local $4
41284102
(i32.wrap/i64
41294103
(i64.sub
41304104
(get_local $1)
@@ -4168,12 +4142,12 @@
41684142
(i64.or
41694143
(i64.load32_u offset=8
41704144
(i32.add
4171-
(get_local $4)
4145+
(get_local $3)
41724146
(i32.shl
41734147
(i32.div_u
4174-
(tee_local $3
4148+
(tee_local $4
41754149
(i32.rem_u
4176-
(get_local $3)
4150+
(get_local $4)
41774151
(i32.const 10000)
41784152
)
41794153
)
@@ -4186,10 +4160,10 @@
41864160
(i64.shl
41874161
(i64.load32_u offset=8
41884162
(i32.add
4189-
(get_local $4)
4163+
(get_local $3)
41904164
(i32.shl
41914165
(i32.rem_u
4192-
(get_local $3)
4166+
(get_local $4)
41934167
(i32.const 100)
41944168
)
41954169
(i32.const 2)
@@ -4216,7 +4190,7 @@
42164190
(i64.or
42174191
(i64.load32_u offset=8
42184192
(i32.add
4219-
(get_local $4)
4193+
(get_local $3)
42204194
(i32.shl
42214195
(get_local $6)
42224196
(i32.const 2)
@@ -4226,7 +4200,7 @@
42264200
(i64.shl
42274201
(i64.load32_u offset=8
42284202
(i32.add
4229-
(get_local $4)
4203+
(get_local $3)
42304204
(i32.shl
42314205
(get_local $5)
42324206
(i32.const 2)
@@ -4241,17 +4215,12 @@
42414215
)
42424216
)
42434217
)
4244-
(if
4245-
(tee_local $3
4246-
(i32.wrap/i64
4247-
(get_local $1)
4248-
)
4249-
)
4250-
(call $~lib/internal/itoa/utoa32_lut
4251-
(get_local $0)
4252-
(get_local $3)
4253-
(get_local $2)
4218+
(call $~lib/internal/itoa/utoa32_lut
4219+
(get_local $0)
4220+
(i32.wrap/i64
4221+
(get_local $1)
42544222
)
4223+
(get_local $2)
42554224
)
42564225
)
42574226
(func $~lib/internal/itoa/utoa64 (; 33 ;) (type $Ii) (param $0 i64) (result i32)

0 commit comments

Comments
 (0)