Skip to content

Commit fc90317

Browse files
committed
branchless dateFromEpoch
1 parent 9893245 commit fc90317

3 files changed

Lines changed: 44 additions & 53 deletions

File tree

std/assembly/date.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,17 +332,15 @@ function invalidDate(millis: i64): bool {
332332
// Paper: https://arxiv.org/pdf/2102.06959.pdf
333333
function dateFromEpoch(ms: i64): i32 {
334334
let da = (<i32>floorDiv(ms, MILLIS_PER_DAY) * 4 + EPOCH_OFFSET * 4) | 3;
335-
let q0 = floorDiv(da, DAYS_PER_EPOCH); // [0, 146096]
335+
let q0 = floorDiv(da, DAYS_PER_EPOCH); // [0, 146096]
336336
let r1 = <u32>da - q0 * DAYS_PER_EPOCH;
337337
let u1 = u64(r1 | 3) * 2939745;
338338
let dm1 = <u32>u1 / 11758980;
339339
let n1 = 2141 * dm1 + 197913;
340340
let year = 100 * q0 + i32(u1 >>> 32);
341-
let mo = n1 >>> 16;
342-
_day = (n1 & 0xFFFF) / 2141 + 1; // [1, 31]
343-
if (dm1 >= 306) { mo -= 12; ++year; }
344-
_month = mo; // [1, 12]
345-
return year;
341+
_day = (n1 & 0xFFFF) / 2141 + 1; // [1, 31]
342+
_month = (n1 >>> 16) - (dm1 >= 306 ? 12 : 0); // [1, 12]
343+
return year + (dm1 >= 306 ? 1 : 0);
346344
}
347345

348346
// http://howardhinnant.github.io/date_algorithms.html#days_from_civil

tests/compiler/std/date.debug.wat

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@
309309
(local $dm1 i32)
310310
(local $n1 i32)
311311
(local $year i32)
312-
(local $mo i32)
313312
block $~lib/date/floorDiv<i64>|inlined.0 (result i64)
314313
local.get $ms
315314
local.set $a
@@ -398,33 +397,36 @@
398397
i32.add
399398
local.set $year
400399
local.get $n1
401-
i32.const 16
402-
i32.shr_u
403-
local.set $mo
404-
local.get $n1
405400
i32.const 65535
406401
i32.and
407402
i32.const 2141
408403
i32.div_s
409404
i32.const 1
410405
i32.add
411406
global.set $~lib/date/_day
407+
local.get $n1
408+
i32.const 16
409+
i32.shr_u
412410
local.get $dm1
413411
i32.const 306
414412
i32.ge_u
415-
if
416-
local.get $mo
413+
if (result i32)
417414
i32.const 12
418-
i32.sub
419-
local.set $mo
420-
local.get $year
421-
i32.const 1
422-
i32.add
423-
local.set $year
415+
else
416+
i32.const 0
424417
end
425-
local.get $mo
418+
i32.sub
426419
global.set $~lib/date/_month
427420
local.get $year
421+
local.get $dm1
422+
i32.const 306
423+
i32.ge_u
424+
if (result i32)
425+
i32.const 1
426+
else
427+
i32.const 0
428+
end
429+
i32.add
428430
return
429431
)
430432
(func $~lib/date/Date#set:year (param $this i32) (param $year i32)

tests/compiler/std/date.release.wat

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5395,7 +5395,6 @@
53955395
(local $1 i32)
53965396
(local $2 i32)
53975397
(local $3 i32)
5398-
(local $4 i32)
53995398
local.get $0
54005399
i64.const 86399999
54015400
i64.const 0
@@ -5413,19 +5412,19 @@
54135412
i32.add
54145413
i32.const 3
54155414
i32.or
5416-
local.tee $1
5415+
local.tee $2
54175416
i32.const 146096
54185417
i32.const 0
5419-
local.get $1
5418+
local.get $2
54205419
i32.const 0
54215420
i32.lt_s
54225421
select
54235422
i32.sub
54245423
i32.const 146097
54255424
i32.div_s
5426-
local.set $2
5427-
local.get $1
5425+
local.set $1
54285426
local.get $2
5427+
local.get $1
54295428
i32.const 146097
54305429
i32.mul
54315430
i32.sub
@@ -5438,49 +5437,41 @@
54385437
i32.wrap_i64
54395438
i32.const 11758980
54405439
i32.div_u
5441-
local.tee $4
5440+
local.tee $2
54425441
i32.const 2141
54435442
i32.mul
54445443
i32.const 197913
54455444
i32.add
5446-
local.set $3
5447-
local.get $0
5448-
i64.const 32
5449-
i64.shr_u
5450-
i32.wrap_i64
5451-
local.get $2
5452-
i32.const 100
5453-
i32.mul
5454-
i32.add
5455-
local.set $1
5456-
local.get $3
5457-
i32.const 16
5458-
i32.shr_u
5459-
local.set $2
5460-
local.get $3
5445+
local.tee $3
54615446
i32.const 65535
54625447
i32.and
54635448
i32.const 2141
54645449
i32.div_u
54655450
i32.const 1
54665451
i32.add
54675452
global.set $~lib/date/_day
5468-
local.get $4
5453+
local.get $3
5454+
i32.const 16
5455+
i32.shr_u
5456+
i32.const 12
5457+
i32.const 0
5458+
local.get $2
54695459
i32.const 306
54705460
i32.ge_u
5471-
if
5472-
local.get $2
5473-
i32.const 12
5474-
i32.sub
5475-
local.set $2
5476-
local.get $1
5477-
i32.const 1
5478-
i32.add
5479-
local.set $1
5480-
end
5481-
local.get $2
5461+
local.tee $2
5462+
select
5463+
i32.sub
54825464
global.set $~lib/date/_month
5465+
local.get $0
5466+
i64.const 32
5467+
i64.shr_u
5468+
i32.wrap_i64
54835469
local.get $1
5470+
i32.const 100
5471+
i32.mul
5472+
i32.add
5473+
local.get $2
5474+
i32.add
54845475
)
54855476
(func $~lib/date/Date#toTimeString (param $0 i32) (result i32)
54865477
(local $1 i32)

0 commit comments

Comments
 (0)