-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
parse.gr
588 lines (546 loc) 路 18.1 KB
/
parse.gr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// This module was based on Rust's dec2flt
// https://github.com/rust-lang/rust/blob/1cbc45942d5c0f6eb5d94e3b10762ba541958035/library/core/src/num/dec2flt/parse.rs
// Rust's MIT license is provided below:
/*
* Permission is hereby granted, free of charge, to any
* person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the
* Software without restriction, including without
* limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software
* is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice
* shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
* ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
* IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
module Parse
from "runtime/unsafe/wasmi32" include WasmI32
from "runtime/unsafe/wasmi64" include WasmI64
from "runtime/unsafe/wasmf64" include WasmF64
from "runtime/dataStructures" include DataStructures
use DataStructures.{ newInt32, newInt64, newFloat64 }
from "runtime/atof/common" include Common
use Common.{
_CHAR_CODE_UNDERSCORE,
_CHAR_CODE_PLUS,
_CHAR_CODE_MINUS,
_CHAR_CODE_DOT,
_CHAR_CODE_0,
_CHAR_CODE_E,
_CHAR_CODE_e,
_CHAR_CODE_A,
_CHAR_CODE_Z,
_CHAR_CODE_a,
_CHAR_CODE_f,
_CHAR_CODE_i,
_CHAR_CODE_n,
_CHAR_CODE_t,
_CHAR_CODE_y,
_MIN_EXPONENT_FAST_PATH,
_MAX_EXPONENT_DISGUISED_FAST_PATH,
_MAX_EXPONENT_FAST_PATH,
_MAX_MANTISSA_FAST_PATH,
is8Digits,
getPowers10,
getPowers10FastPath,
biasedFpToNumber,
fpNan,
fpInf,
}
from "runtime/atof/lemire" include Lemire
use Lemire.{ computeFloat }
from "runtime/atof/slow" include Slow
use Slow.{ parseLongMantissa }
// Try to parse 8 digits at a time:
// https://johnnylee-sde.github.io/Fast-numeric-string-to-int/
@unsafe
let parse8Digits = (digits: WasmI64) => {
use WasmI64.{ (+), (-), (*), (>>>), (&), (|), (==) }
let _MASK = 0x0000_00FF_0000_00FFN
let a = digits - 0x3030_3030_3030_3030N
let b = a * 10N + (a >>> 8N)
let c = (b & _MASK) * 0x000F_4240_0000_0064N
let d = (b >>> 16N & _MASK) * 0x0000_2710_0000_0001N
(c + d) >>> 32N
}
@unsafe
let parseScientificExponent = (ptr: WasmI32, offset: WasmI32, len: WasmI32) => {
use WasmI32.{ (+), (-), (*), geU as (>=), ltU as (<), (==), (!=) }
let mut offset = offset
let mut exponent = 0n
let mut negative = false
// Parse the optional leading sign
match (WasmI32.load8U(ptr, offset)) {
fst when fst == _CHAR_CODE_PLUS => {
offset += 1n
negative = false
},
fst when fst == _CHAR_CODE_MINUS => {
offset += 1n
negative = true
},
_ => void,
}
if (offset >= len) {
Err("Invalid exponent")
} else {
// Parse digits '0'-'9' until we hit a non-digit byte or reach the end of the string
while (offset < len) {
let char = WasmI32.load8U(ptr, offset)
let digit = char - 0x30n
if (digit >= 0n && digit < 10n) {
use WasmI32.{ (<) }
if (exponent < 0x10000n) {
exponent = digit + 10n * exponent
}
offset += 1n
} else if (char == _CHAR_CODE_UNDERSCORE) {
offset += 1n
} else {
break
}
}
if (offset != len) {
Err("Invalid exponent")
} else if (negative) {
Ok(WasmI32.toGrain(newInt32(0n - exponent)): Int32)
} else {
Ok(WasmI32.toGrain(newInt32(exponent)): Int32)
}
}
}
@unsafe
let parseFloatToParts = (string: String) => {
use WasmI32.{ (+), (-), gtU as (>), geU as (>=), ltU as (<), (==), (!=) }
use WasmI64.{ (+) as addWasmI64, (*) }
let ptr = WasmI32.fromGrain(string)
match (WasmI32.load(ptr, 4n)) {
// Invalid string
0n => Err("Invalid string"),
// Continue to parse the string
len => {
let ptr = ptr + 8n
let mut i = 0n
let mut mantissa = 0N
let mut exponent = 0n
let mut numDigits = 0n
let mut numDigitsAfterDot = 0n
let mut manyDigits = false
// Parse the optional leading sign
let mut negative = false
match (WasmI32.load8U(ptr, i)) {
fst when fst == _CHAR_CODE_PLUS => {
i += 1n
negative = false
},
fst when fst == _CHAR_CODE_MINUS => {
i += 1n
negative = true
},
_ => void,
}
// Parse digits '0'-'9' until we hit a non-digit byte or reach the end of the string
while (i < len) {
let char = WasmI32.load8U(ptr, i)
let digit = char - _CHAR_CODE_0
if (digit >= 0n && digit < 10n) {
mantissa = addWasmI64(mantissa * 10N, WasmI64.extendI32U(digit))
numDigits += 1n
i += 1n
} else if (char == _CHAR_CODE_UNDERSCORE) {
i += 1n
} else {
break
}
}
let digitsBeforeDecimal = numDigits
// Handle the dot
match (WasmI32.load8U(ptr, i)) {
c when c == _CHAR_CODE_DOT => {
i += 1n
let dotStartIndex = i
// Parse chunks of 8 digits
while (WasmI32.leU(i + 8n, len)) {
let digits = WasmI64.load(ptr, i)
if (is8Digits(digits)) {
mantissa = addWasmI64(parse8Digits(digits), mantissa * 100000000N)
i += 8n
numDigitsAfterDot += 8n
} else {
break
}
}
// Parse digits '0'-'9' until we hit a non-digit byte or reach the end of the string
while (i < len) {
let char = WasmI32.load8U(ptr, i)
let digit = char - _CHAR_CODE_0
if (digit >= 0n && digit < 10n) {
mantissa = addWasmI64(WasmI64.extendI32U(digit), mantissa * 10N)
i += 1n
numDigitsAfterDot += 1n
} else if (char == _CHAR_CODE_UNDERSCORE) {
i += 1n
} else {
break
}
}
exponent = 0n - numDigitsAfterDot
},
_ => void,
}
numDigits += numDigitsAfterDot
match (numDigits) {
0n => Err("Invalid float"),
_ => {
let _MAX_MANTISSA_DIGITS = 19n // 10^19 fits in uint64
let _MIN_19DIGIT_INT = 100_0000_0000_0000_0000N
// Parse scientific notation
let exponentResult = match (WasmI32.load8U(ptr, i)) {
c when c == _CHAR_CODE_E || c == _CHAR_CODE_e => {
i += 1n
parseScientificExponent(ptr, i, len)
},
_ => {
if (i != len) {
Err("Invalid float")
} else {
Ok(0l)
}
},
}
match (exponentResult) {
Ok(exponentNumber) => {
let exponentNumber = WasmI32.load(
WasmI32.fromGrain(exponentNumber),
4n
)
exponent += exponentNumber
// Check to see if we need to truncate
if (numDigits > _MAX_MANTISSA_DIGITS) {
manyDigits = false
numDigits -= _MAX_MANTISSA_DIGITS
let mut i = 0n
while (i < len) {
let c = WasmI32.load8U(ptr, i)
if (c == _CHAR_CODE_DOT || c == _CHAR_CODE_0) {
use WasmI32.{ (<), (>) }
let n = c - (_CHAR_CODE_0 - 1n)
numDigits -= if (n < 0n) {
0n
} else if (n > 255n) {
255n
} else {
n
}
i += 1n
} else if (c == _CHAR_CODE_UNDERSCORE) {
continue
} else {
break
}
}
// Attempt to parse 19 digits, the most that can fit in an i64
let mut digitsParsed = 0n
use WasmI32.{ (>) }
if (numDigits > 0n) {
manyDigits = true
mantissa = 0N
let mut i = 0n
while (WasmI64.ltU(mantissa, _MIN_19DIGIT_INT)) {
if (i < len) {
let char = WasmI32.load8U(ptr, i)
let digit = char - _CHAR_CODE_0
if (digit >= 0n && digit < 10n) {
mantissa = addWasmI64(
mantissa * 10N,
WasmI64.extendI32U(digit)
)
i += 1n
digitsParsed += 1n
} else if (char == _CHAR_CODE_UNDERSCORE) {
i += 1n
} else {
break
}
} else {
break
}
}
exponent = if (WasmI64.geU(mantissa, _MIN_19DIGIT_INT)) {
digitsBeforeDecimal - digitsParsed
} else {
// From Rust: https://github.com/rust-lang/rust/blob/e960b5e7749e95c6a6b2fdec7250a48105664efb/library/core/src/num/dec2flt/parse.rs#L179
// the next byte must be present and be '.'
// We know this is true because we had more than 19
// digits previously, so we overflowed a 64-bit integer,
// but parsing only the integral digits produced less
// than 19 digits. That means we must have a decimal
// point, and at least 1 fractional digit.
i += 1n
let mut fractionalDigitsParsed = 0n
while (WasmI64.ltU(mantissa, _MIN_19DIGIT_INT)) {
if (i < len) {
let char = WasmI32.load8U(ptr, i)
let digit = char - _CHAR_CODE_0
if (digit < 10n) {
mantissa = addWasmI64(
mantissa * 10N,
WasmI64.extendI32U(digit)
)
i += 1n
fractionalDigitsParsed += 1n
} else if (char == _CHAR_CODE_UNDERSCORE) {
i += 1n
} else {
break
}
} else {
break
}
}
0n - fractionalDigitsParsed
}
// Add back the explicit part
exponent += exponentNumber
}
Ok(
(
WasmI32.toGrain(newInt32(exponent)): Int32,
WasmI32.toGrain(newInt64(mantissa)): Int64,
negative,
manyDigits,
),
)
} else {
Ok(
(
WasmI32.toGrain(newInt32(exponent)): Int32,
WasmI32.toGrain(newInt64(mantissa)): Int64,
negative,
manyDigits,
),
)
}
},
Err(err) => Err(err),
}
},
}
},
}
}
@unsafe
let parseInfNan = s => {
use WasmI32.{ (+), (-), (<), (>=), (<=), (==) }
let ptr = WasmI32.fromGrain(s)
match (WasmI32.load(ptr, 4n)) {
// Invalid string
0n => Err("Invalid string"),
// Continue to parse
len => {
let ptr = ptr + 8n
let mut i = 0n
let mut mantissa = 0N
let mut exponent = 0n
let mut numDigits = 0n
let mut numDigitsAfterDot = 0n
let mut manyDigits = false
// Parse the optional leading sign
let mut negative = false
match (WasmI32.load8U(ptr, i)) {
fst when fst == _CHAR_CODE_PLUS => {
i += 1n
negative = false
},
fst when fst == _CHAR_CODE_MINUS => {
i += 1n
negative = true
},
_ => void,
}
if (len - i < 3n) {
Err("Invalid string")
} else {
let mut c1 = WasmI32.load8U(ptr + i, 0n)
if (c1 >= _CHAR_CODE_A && c1 <= _CHAR_CODE_Z) {
c1 += 0x20n
}
let mut c2 = WasmI32.load8U(ptr + i, 1n)
if (c2 >= _CHAR_CODE_A && c2 <= _CHAR_CODE_Z) {
c2 += 0x20n
}
let mut c3 = WasmI32.load8U(ptr + i, 2n)
if (c3 >= _CHAR_CODE_A && c3 <= _CHAR_CODE_Z) {
c3 += 0x20n
}
if (
len - i == 3n &&
c1 == _CHAR_CODE_n &&
c2 == _CHAR_CODE_a &&
c3 == _CHAR_CODE_n
) {
Ok((fpNan(), negative))
} else if (
c1 == _CHAR_CODE_i &&
c2 == _CHAR_CODE_n &&
c3 == _CHAR_CODE_f
) {
if (len - i == 3n) {
Ok((fpInf(), negative))
} else if (len - i == 8n) {
let mut c4 = WasmI32.load8U(ptr + i, 3n)
if (c4 >= _CHAR_CODE_A && c4 <= _CHAR_CODE_Z) {
c4 += 0x20n
}
let mut c5 = WasmI32.load8U(ptr + i, 4n)
if (c5 >= _CHAR_CODE_A && c5 <= _CHAR_CODE_Z) {
c5 += 0x20n
}
let mut c6 = WasmI32.load8U(ptr + i, 5n)
if (c6 >= _CHAR_CODE_A && c6 <= _CHAR_CODE_Z) {
c6 += 0x20n
}
let mut c7 = WasmI32.load8U(ptr + i, 6n)
if (c7 >= _CHAR_CODE_A && c7 <= _CHAR_CODE_Z) {
c7 += 0x20n
}
let mut c8 = WasmI32.load8U(ptr + i, 7n)
if (c8 >= _CHAR_CODE_A && c8 <= _CHAR_CODE_Z) {
c8 += 0x20n
}
if (
c4 == _CHAR_CODE_i &&
c5 == _CHAR_CODE_n &&
c6 == _CHAR_CODE_i &&
c7 == _CHAR_CODE_t &&
c8 == _CHAR_CODE_y
) {
Ok((fpInf(), negative))
} else {
Err("Invalid string")
}
} else {
Err("Invalid string")
}
} else {
Err("Invalid string")
}
}
},
}
}
@unsafe
provide let isFastPath = (
exponent: WasmI32,
mantissa: WasmI64,
negative: Bool,
manyDigits: Bool,
) => {
use WasmI32.{ (<=) }
use WasmI64.{ (<<) }
_MIN_EXPONENT_FAST_PATH <= exponent &&
exponent <= _MAX_EXPONENT_DISGUISED_FAST_PATH &&
WasmI64.leU(mantissa, _MAX_MANTISSA_FAST_PATH) &&
!manyDigits
}
@unsafe
provide let parseFloat = (string: String) => {
use WasmI32.{ (!=), (*) }
match (parseFloatToParts(string)) {
Ok((exponent, mantissa, negative, manyDigits)) => {
use WasmI64.{ (+), (>) }
let exponent = WasmI32.load(WasmI32.fromGrain(exponent), 4n)
let mantissa = WasmI64.load(WasmI32.fromGrain(mantissa), 8n)
let floatOpt = if (isFastPath(exponent, mantissa, negative, manyDigits)) {
use WasmI32.{ (<=) }
if (exponent <= _MAX_EXPONENT_FAST_PATH) {
// normal fast path
use WasmF64.{ (/), (*) }
use WasmI32.{ (<) }
let mantissa = WasmF64.convertI64U(mantissa)
let n = if (exponent < 0n) {
use WasmI32.{ (*) }
mantissa / getPowers10FastPath(exponent * -1n)
} else {
mantissa * getPowers10FastPath(exponent)
}
if (negative) {
Some(WasmI32.toGrain(newFloat64(n * -1.0W)): Number)
} else {
Some(WasmI32.toGrain(newFloat64(n)): Number)
}
} else {
// disguised fast path
use WasmI32.{ (-) }
use WasmI64.{ (*), (<=) }
let shift = exponent - _MAX_EXPONENT_FAST_PATH
let mantissa = mantissa * WasmI64.extendI32U(getPowers10(shift))
if (mantissa > _MAX_MANTISSA_FAST_PATH) {
None
} else {
use WasmF64.{ (*) }
let mantissa = WasmF64.convertI64U(mantissa)
let n = mantissa * getPowers10FastPath(_MAX_EXPONENT_FAST_PATH)
if (negative) {
Some(WasmI32.toGrain(newFloat64(n * -1.0W)): Number)
} else {
Some(WasmI32.toGrain(newFloat64(n)): Number)
}
}
}
} else {
None
}
// Check if fast path worked
match (floatOpt) {
Some(n) => Ok(n),
None => {
use WasmI32.{ (<), (>=) }
// From Rust:
// If significant digits were truncated, then we can have rounding error
// only if `mantissa + 1` produces a different result. We also avoid
// redundantly using the Eisel-Lemire algorithm if it was unable to
// correctly round on the first pass.
let mut fp = computeFloat(WasmI64.extendI32S(exponent), mantissa)
let f = WasmI64.load(WasmI32.fromGrain(fp.f), 8n)
let e = WasmI32.load(WasmI32.fromGrain(fp.e), 4n)
if (manyDigits && e >= 0n) {
use WasmI64.{ (!=) as neWasmI64 }
let fp2 = computeFloat(WasmI64.extendI32S(exponent), mantissa + 1N)
let f2 = WasmI64.load(WasmI32.fromGrain(fp2.f), 8n)
let e2 = WasmI32.load(WasmI32.fromGrain(fp2.e), 4n)
if (e != e2 || neWasmI64(f, f2)) {
fp.e = -1l
}
}
// Unable to correctly round the float using the Eisel-Lemire algorithm.
// Fallback to a slower, but always correct algorithm.
let e = WasmI32.load(WasmI32.fromGrain(fp.e), 4n)
if (e < 0n) {
fp = parseLongMantissa(string)
}
Ok(biasedFpToNumber(fp, negative))
},
}
},
Err(str) => {
match (parseInfNan(string)) {
Ok((fp, negative)) => Ok(biasedFpToNumber(fp, negative)),
_ => Err(str),
}
},
}
}