-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
4868 lines (4196 loc) · 118 KB
/
main.js
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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// UMD pattern
// see https://github.com/umdjs/umd/blob/master/templates/returnExports.js
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.myAsmjsModule = factory();
}
}(this, function () {
'use strict';
function myAsmjsModule(stdlib, foreign, heap) {
'use asm';
var Infinity = stdlib.Infinity;
var NaN = stdlib.NaN;
var abs = stdlib.Math.abs;
var acos = stdlib.Math.acos;
var asin = stdlib.Math.asin;
var atan = stdlib.Math.atan;
var atan2 = stdlib.Math.atan2;
var ceil = stdlib.Math.ceil;
var cos = stdlib.Math.cos;
var exp = stdlib.Math.exp;
var floor = stdlib.Math.floor;
var fround = stdlib.Math.fround;
var imul = stdlib.Math.imul;
var log = stdlib.Math.log;
var max = stdlib.Math.max;
var min = stdlib.Math.min;
var pow = stdlib.Math.pow;
var sin = stdlib.Math.sin;
var sqrt = stdlib.Math.sqrt;
var tan = stdlib.Math.tan;
var I1 = new stdlib.Int8Array(heap);
var I2 = new stdlib.Int16Array(heap);
var I4 = new stdlib.Int32Array(heap);
var U1 = new stdlib.Uint8Array(heap);
var U2 = new stdlib.Uint16Array(heap);
var U4 = new stdlib.Uint32Array(heap);
var F4 = new stdlib.Float32Array(heap);
var F8 = new stdlib.Float64Array(heap);
/**
* Returns the number of 1s in a 32-bit integer and
* writes out the actual indices of the 1s (0 <= i < 32) into <code>outP</code>.
* Each index occupies one byte, so at most 32 bytes (32 uint8 integers) will
* be written into <code>outP</code>.
*
* Before using this function, precompute a table at <code>tableP</code>
* by <code>deBruijnSelectInit</code>.
*
* @param {int} tableP - byte offset to the precomputed table
* @param {int} n - 32-bit integer to be examined
* @param {int} outP - byte offset into which the results are to be written
* @returns {signed} - number of 1s found in a word
*
* @see Peter Wegner. 1960. A Technique for Counting Ones in a Binary Computer.
* Communications of the ACM, 3(5):322, May.
* @see Charles E. Leiserson, Harald Prokop, and Keith H. Randall. 1998. Using
* de Bruijn Sequences to Index a 1 in a Computer Word. Technical report.
*/
function deBruijnSelect(tableP, n, outP) {
/*
* Type annotations
*/
tableP = tableP | 0;
n = n | 0;
outP = outP | 0;
/*
* Local variables
*/
var i = 0;
var t = 0;
var offset = 0;
/*
* Main
*/
while ((n | 0) != 0) {
// Since 2147483648 & -2147483648 returns -2147483648 in ECMAScript,
// we need type casting (>>> 0) to unsigned.
t = (n & -n) >>> 0;
// 0x077cb531 is a de Bruijn sequence 00000111011111001011010100110001
offset = imul(t, 0x077cb531) >>> 27;
U1[(outP + i) >> 0] = U1[(tableP + offset) >> 0];
n = (n - t) | 0;
i = (i + 1) | 0;
}
return i | 0;
}
/**
* Initializes a table used in <code>deBruijnSelect</code>.
* Exactly 32 bytes will be written into outP.
*/
function deBruijnSelectInit(outP) {
/*
* Type annotations
*/
outP = outP | 0;
/*
* Local variables
*/
var i = 0;
var offset = 0;
/*
* Main
*/
for (i = 0; (i | 0) < 32; i = (i + 1) | 0) {
// 0x077cb531 is a de Bruijn sequence 00000111011111001011010100110001
offset = (0x077cb531 << i) >>> 27;
U1[(outP + offset) >> 0] = i;
}
}
/**
* Returns the number of 1s in a 32-bit integer and
* writes out the actual indices of the 1s (0 <= i < 32) into <code>outP</code>.
* Each index occupies one byte, so at most 32 bytes (32 uint8 integers) will
* be written into <code>outP</code>.
*
* This function can be slower than the version using pre-computed table
* in some environments (10% slower in Firefox 45) but equally fast in other
* cases.
*
* @param {int} n - 32-bit integer to be examined
* @param {int} outP - byte offset into which the results are to be written
* @returns {signed} - number of 1s found in a word
*
* @see Peter Wegner. 1960. A Technique for Counting Ones in a Binary Computer.
* Communications of the ACM, 3(5):322, May.
* @see Charles E. Leiserson, Harald Prokop, and Keith H. Randall. 1998. Using
* de Bruijn Sequences to Index a 1 in a Computer Word. Technical report.
*/
function deBruijnSelectNoTable(n, outP) {
/*
* Type annotations
*/
n = n | 0;
outP = outP | 0;
/*
* Local variables
*/
var i = 0;
var t = 0;
var offset = 0;
var v = 0;
/*
* Main
*/
while (n | 0) {
// Since 2147483648 & -2147483648 returns -2147483648 in ECMAScript,
// we need type casting (>>> 0) to unsigned.
t = (n & -n) >>> 0;
// 0x077cb531 is a de Bruijn sequence 00000111011111001011010100110001
offset = imul(t, 0x077cb531) >>> 27;
switch (offset | 0) {
case 0:
v = 0;
break;
case 1:
v = 1;
break;
case 3:
v = 2;
break;
case 7:
v = 3;
break;
case 14:
v = 4;
break;
case 29:
v = 5;
break;
case 27:
v = 6;
break;
case 23:
v = 7;
break;
case 15:
v = 8;
break;
case 31:
v = 9;
break;
case 30:
v = 10;
break;
case 28:
v = 11;
break;
case 25:
v = 12;
break;
case 18:
v = 13;
break;
case 5:
v = 14;
break;
case 11:
v = 15;
break;
case 22:
v = 16;
break;
case 13:
v = 17;
break;
case 26:
v = 18;
break;
case 21:
v = 19;
break;
case 10:
v = 20;
break;
case 20:
v = 21;
break;
case 9:
v = 22;
break;
case 19:
v = 23;
break;
case 6:
v = 24;
break;
case 12:
v = 25;
break;
case 24:
v = 26;
break;
case 17:
v = 27;
break;
case 2:
v = 28;
break;
case 4:
v = 29;
break;
case 8:
v = 30;
break;
case 16:
v = 31;
break;
}
U1[(outP + i) >> 0] = v | 0;
n = (n - t) | 0;
i = (i + 1) | 0;
}
return i | 0;
}
/*
script to create the switch-case statement
(function () {
console.log('switch (offset) {');
for (i = 0; (i | 0) < 32; i = (i + 1) | 0) {
offset = (0x077cb531 << i) >>> 27;
console.log(' case ' + offset + ':')
console.log(' v = ' + i + ';');
console.log(' break;');
}
console.log('}');
})();
*/
/**
* Returns the next highest power of 2 for a positive unsigned 32-bit integer
* in [1, 2^31]. The returned value will be signed due to asm.js constraints,
* so use <code>>></code> for unsigned type cast. (Specifically, this returns
* -2147483648 if the input is in (2^30, 2^31]).
*
* If the given value is already a power of 2, this function returns the same
* value. If the given value is 0 or more than 2^31, this function returns 0.
*
* This algorithm was first devised by Pete Hart and William Lewis in February
* of 1997, and later independetly discovered by Sean Anderson in
* Semptember 14, 2001.
* See http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
*
* @param {int} v - unsigned 32-bit integer
* @returns {signed} - next highest power of 2
*/
function nextPow2(v) {
/*
* Type annotations
*/
v = v | 0;
/*
* Main
*/
v = v >>> 0;
v = (v - 1) >>> 0;
v = v | (v >>> 1);
v = v | (v >>> 2);
v = v | (v >>> 4);
v = v | (v >>> 8);
v = v | (v >>> 16);
v = (v + 1) >>> 0;
return v | 0;
}
/**
* Writes (bitLength) bits into a heap.
*
* <code>p</code> must be a multiple of 4. <code>value</code> must be less
* than <code>2^bitLength</code>. Destination must be initialized to 0
* beforehand. If these conditions are violated, the behavior is undefined.
*
* @param {number} p - base index in bytes which must be aligned to 4 bytes
* @param {number} bitIndex - relative index in bits
* @param {number} bitLength - length in bits (<= 32)
* @param {number} value - values to be written
*/
function writeBits(p, bitIndex, bitLength, value) {
/*
* Type annotations
*/
p = p | 0;
bitIndex = bitIndex | 0;
bitLength = bitLength | 0;
value = value | 0;
/*
* Local variables
*/
var byteOffset = 0;
var bitOffset = 0;
var mask = 0;
/*
* Main
*/
byteOffset = (p + (bitIndex >>> 3)) | 0;
bitOffset = bitIndex & 0x1f;
// When we need some bits from the next, mask becomes 0xffffffff, otherwise 0.
mask = -((bitOffset + bitLength - 1) >>> 5) | 0;
U4[byteOffset >> 2] = U4[byteOffset >> 2] | (value << bitOffset);
byteOffset = (byteOffset + 4) | 0;
U4[byteOffset >> 2] = U4[byteOffset >> 2] |
(mask & (value >>> (32 - bitOffset)));
}
/*
* Implementation note:
* Currently
&
* +---+---+---+---+
* |LEN|LIM|LBS|FLG| (more-->)
* +---+---+---+---+
*
* +==================+==================+
* |... LOWER_BITS ...|... HIGHER_BITS...|
* +==================+==================+
*
* Header
* LEN: number of items ("number of 1s" if regarded as a rank-select dict.)
* LIM: max value + 1 ("size of a bitmap" if regarded as a ranke-select dict.)
* LBS: lower bits size, or the number of bits per item in lower bits
* FLG: reserved space for future expansions
*/
/**
* Creates an Elias-Fano structure for a sequence of unsigned integers.
*
* The structure is useful for compressing a sparse set of unsigned integers
* with several primitive operations retained (e.g., rank, select, and succ).
*
* Before using this function, perform deBruijnSelectInit to precompute a table
* at <code>deBruijnTableP</code>.
* Also, destination of outP must be initialized to 0 beforehand.
*
* Although Elias-Fano can be more efficient by using an auxiliary "rank-select"
* strucure, currently this code does not implement the strategy.
*
* This data structure uses n log (m / n) + O(n) bits where n is the number
* of items and m is the maximum value of in a sequence.
* The exact size in bytes can be estimated by using eliasFanoByteSize.
*
* @param {int} p - byte offset to 32-bit unsigned integers
* @param {int} len - length of the input
* @param {int} deBruijnTableP - byte offset to the sequence of de Bruijn consts
* @param {int} outP - byte offset into which the result will be written
* @returns {int} - 0 if successfully created, otherwise a positive value
*/
function eliasFano(p, len, deBruijnTableP, outP) {
/*
* Type annotations
*/
p = p | 0;
len = len | 0;
deBruijnTableP = deBruijnTableP | 0;
outP = outP | 0;
/*
* Local variables
*/
var lim = 0.0;
var lowerBitsSize = 0; // size in bits per item in the sequence of lower bits
var lowerBitsSizePow2 = 0;
var numberOfBuckets = 0;
// total size of the sequence of lower bits, aligned to 4 bytes
var lowerBitsByteSize = 0;
var t = 0.0;
var t2 = 0;
/*
* Main
*/
// TODO: initialize to zero for outP
// TODO: sort and unique here
lim = (+(U4[(p + ((len - 1) << 2)) >> 2] >>> 0)) + 1.0;
t = lim / (+(len | 0));
// ceil(log2(t)) == log2(nextPow2(ceil(t))) for [1, 2^31]
t2 = (nextPow2(~~ceil(t)) | 0) >>> 0;
t2 = deBruijnSelect(deBruijnTableP, t2, outP) | 0;
// if ((t2 | 0) != 1) { // asserts t2 is a power of 2
// throw new Error('Assertion failed: not power of 2');
// }
lowerBitsSize = U1[outP >> 0] | 0;
lowerBitsSizePow2 = (1 << lowerBitsSize) | 0;
numberOfBuckets = ~~ceil(lim / +(lowerBitsSizePow2 | 0));
U4[outP >> 2] = len;
U4[(outP + 4) >> 2] = ~~lim;
U4[(outP + 8) >> 2] = lowerBitsSize;
U4[(outP + 12) >> 2] = 0;
outP = (outP + 16) | 0; // header size
// aligned to 4 bytes
lowerBitsByteSize = (((imul(lowerBitsSize, len) - 1) >>> 5) + 1) << 2;
createLowerBits(p, len, lowerBitsSize, outP);
outP = (outP + lowerBitsByteSize) | 0;
createHigherBits(p, len, lowerBitsSize, outP);
return 0;
}
function createLowerBits(p, len, lowerBitsSize, outP) {
/*
* Type annotations
*/
p = p | 0;
len = len | 0;
lowerBitsSize = lowerBitsSize | 0;
outP = outP | 0;
/*
* Local variables
*/
var end = 0;
var mask = 0;
var v = 0;
var bitIndex = 0;
/*
* Main
*/
if ((lowerBitsSize | 0) == 0) {
return;
}
end = (p + (len << 2)) | 0;
mask = ((1 << lowerBitsSize) - 1) | 0;
for (; (p | 0) < (end | 0); p = (p + 4) | 0) {
v = U4[p >> 2] & mask;
writeBits(outP, bitIndex, lowerBitsSize, v);
bitIndex = (bitIndex + lowerBitsSize) | 0;
}
}
// Beware that Vigna 2013 and Golynski 2014 have slightly different
// formations for creating higher bits; here we use Golynski 2014
function createHigherBits(p, len, lowerBitsSize, outP) {
/*
* Type annotations
*/
p = p | 0;
len = len | 0;
lowerBitsSize = lowerBitsSize | 0;
outP = outP | 0;
/*
* Local variables
*/
var end = 0;
var higherBits = 0;
var previousHigherBits = 0;
var unarySize = 0;
var bitIndex = 0;
/*
* Main
*/
end = (p + (len << 2)) | 0;
bitIndex = 1;
for (; (p | 0) < (end | 0); p = (p + 4) | 0) {
higherBits = U4[p >> 2] >>> lowerBitsSize;
unarySize = (higherBits - previousHigherBits) | 0;
writeBits(outP, bitIndex, (unarySize + 1) | 0, 1 << unarySize);
previousHigherBits = higherBits;
bitIndex = (bitIndex + unarySize + 1) | 0;
}
}
/**
* Calculates the exact byte size required by the Elias-Fano structure for a
* sequence of unique 32-bit unsigned integers.
*
* This code uses 4 bytes at tmpP.
*
* In terms of space complexity, this structure uses B(m, n) + O(n) bits,
* where B(m, n) = log2(ceil(binomial_coefficient(m, n))).
*/
function eliasFanoByteSize(maxValue, len, deBruijnTableP, tmpP) {
maxValue = maxValue | 0;
len = len | 0;
deBruijnTableP = deBruijnTableP | 0;
tmpP = tmpP | 0;
/*
* Local variables
*/
var headerByteSize = 0;
var lowerBitsByteSize = 0;
var higherBitsByteSize = 0;
var lowerBitsSize = 0;
var lowerBitsSizePow2 = 0;
var numberOfBuckets = 0;
var t = 0.0;
var t2 = 0;
/*
* Main
*/
headerByteSize = 16;
t = (+(maxValue | 0)) + 1.0;
t = t / (+(len | 0));
t2 = (nextPow2(~~ceil(t)) | 0) >>> 0;
t2 = deBruijnSelect(deBruijnTableP, t2, tmpP) | 0;
lowerBitsSize = U1[tmpP >> 0] | 0;
lowerBitsSizePow2 = (1 << lowerBitsSize) | 0;
numberOfBuckets = ~~ceil(t / +(lowerBitsSizePow2 | 0));
// conversion from bit size to byte size
// TODO: imul(len, beta) must be in [1, 2^32 - 1]. Check this.
lowerBitsByteSize = (((imul(lowerBitsSize, len) - 1) >>> 5) + 1) << 2;
// conversion from bit size to byte size
// TODO: len + numberOfBuckets must be in [1, 2^32 - 1]. Check this.
higherBitsByteSize = ((((len + numberOfBuckets) - 1) >>> 5) + 1) << 2;
return (headerByteSize + lowerBitsByteSize + higherBitsByteSize) | 0;
}
/**
* Fast <code>popcount</code> (also known as sideways addition)
* for 32-bit integers, that is, counting non-zero bits in an integer.
*
* See {@link
* http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel}
* or {@link http://stackoverflow.com/a/15979139/3211373}.
*
* @param {int} n - 32-bit integer
* @return {signed} number of non-zero bits in <code>n</code>
*/
function popcount(n) {
/*
* Type annotations
*/
n = n |0;
/*
* Main
*/
n = (n - ((n >>> 1) & 0x55555555)) | 0;
n = (n & 0x33333333) + ((n >>> 2) & 0x33333333) | 0;
return (imul(((n + (n >>> 4)) & 0x0F0F0F0F), 0x01010101) >>> 24) | 0;
}
/**
* Retrieves (bitLength) bits from a heap.
*
* <code>p</code> must be a multiple of 4. If this condition is violated,
* the behavior is undefined.
*
* @param {number} p - base index in bytes which must be aligned to 4 bytes
* @param {number} bitIndex - relative index in bits
* @param {number} bitLength - length in bits (<= 32)
* @returns {number} retrieved bits as a signed 32-bit integer (not unsigned)
*/
function readBits(p, bitIndex, bitLength) {
/*
* Type annotations
*/
p = p | 0;
bitIndex = bitIndex | 0;
bitLength = bitLength | 0;
/*
* Local variables
*/
var byteOffset = 0;
var bitOffset = 0;
var mask = 0;
var result = 0;
var bitsCurrent = 0;
var bitsNext = 0;
/*
* Main
*/
byteOffset = (p + (bitIndex >>> 3)) | 0;
bitOffset = bitIndex & 0x1f;
// When we need some bits from the next, mask becomes 0xffffffff, otherwise 0.
mask = -((bitOffset + bitLength - 1) >>> 5) | 0;
bitsCurrent = U4[byteOffset >> 2] | 0;
bitsNext = U4[(byteOffset + 4) >> 2] | 0;
result = result | (bitsCurrent >>> bitOffset);
// Mask is needed since (a << 32) does not give 0
result = result | (mask & (bitsNext << (32 - bitOffset)));
return (result & (0xffffffff >>> (32 - bitLength))) | 0;
}
/**
* Returns the smallest value in an Elias-Fano set which is equal to or more
* than a given value <code>n</code>.
*
* If n is higher than maxValue, this code returns 0xffffffff.
* (It is ok because in situations where 0xffffffff is a valid value,
* that is, if maxValue = 0xffffffff, n is never higher than maxValue).
*/
function eliasFanoSucc(n, eliasFanoP, deBruijnTableP, tmpP) {
/*
* Type annotations
*/
n = n | 0;
eliasFanoP = eliasFanoP | 0;
deBruijnTableP = deBruijnTableP | 0;
tmpP = tmpP | 0;
/*
* Local variables
*/
var lowerBitsSize = 0;
var len = 0;
var maxValue = 0;
var lowerBitsP = 0;
var lowerBitsByteSize = 0;
var higherBitsP = 0;
var bitPosition = 0;
var bucketId = 0;
var itemId = 0;
var numberOfZeros = 0;
var previousNumberOfZeros = 0;
var bitBlock = 0;
var t = 0;
var t2 = 0;
var bit = 0;
var v = 0;
var lowBits = 0;
/*
* Main
*/
n = n >>> 0;
len = U4[eliasFanoP >> 2] | 0;
maxValue = ((U4[(eliasFanoP + 4) >> 2] | 0) - 1) | 0;
lowerBitsSize = U4[(eliasFanoP + 8) >> 2] | 0;
if ((n | 0) > (maxValue | 0)) {
return 0xffffffff | 0;
}
lowerBitsP = (eliasFanoP + 16) | 0;
lowerBitsByteSize = (((imul(lowerBitsSize, len) - 1) >>> 5) + 1) << 2;
higherBitsP = (lowerBitsP + lowerBitsByteSize) | 0;
//
// Step 1: rank0 on upperBits
// the position of the n-th bucket can be retrived by finding the n-th 0.
//
// If n is in Elias-Fano, it must be in the (n >> lowerBitsSize)-th bucket,
// where indexing is 0-based.
// For example, if lowerBitsSize is 2, then the bucket range is
// 4 (= 2^lowerBitsSize), and the first bucket represents [0, 4),
// the second [4, 8), etc. In this case, 0 is in (0 >> 2) = 0-th bucket,
// 3 is in (3 >> 2) = 0-th bucket, and 4 is in (4 >> 2) = 1st bucket.
bucketId = n >> lowerBitsSize;
// Since the starting position of a bucket is marked with a 0 in the
// upper-bits, we can retrieve the position of a bucket by using
// rank0(bucketId), that is, finding the (bucketId)-th 0.
// Note that due to 0-based indexing, if bucketId = 4, we need to see 5 zeros.
// We therefore use <= (rather than <) in the following while loop.
while ((numberOfZeros | 0) <= (bucketId | 0)) {
// TODO: speed comparison
// popcount is generally faster than de Bruijn
// but in extremely sparse cases de Bruijn might be better
previousNumberOfZeros = numberOfZeros;
bitBlock = U4[higherBitsP >> 2] | 0;
numberOfZeros = (numberOfZeros + (popcount(~bitBlock) | 0)) | 0;
bitPosition = (bitPosition + 32) | 0;
higherBitsP = (higherBitsP + 4) | 0;
if ((bitPosition | 0) > (maxValue | 0)) {
// error - prevent infinite loops
return 0xffffffff | 0;
}
}
// unread one bit block
bitPosition = (bitPosition - 32) | 0; // ?
numberOfZeros = previousNumberOfZeros;
// re-read the block by using de Bruijn
t = deBruijnSelect(deBruijnTableP, ~bitBlock, tmpP) | 0;
// the t2-th 0 in the bit block is our target ("t2-th" is also 0-based)
t2 = (bucketId - numberOfZeros) | 0;
t = U1[(tmpP + t2) >> 0] | 0; // now t contains the inner bit index
bitPosition = (bitPosition + t) | 0;
// There are (bucketId + 1) 0s in the first (bitPosition + 1) bits,
// indicating that (bitPosition - bucketId) 1s so far. So the next 1
// represents (bitPosition - bucketId)-th item (0-based) in the set.
itemId = (bitPosition - bucketId) | 0;
//
// Step 2: searching
// Search the value we want
// TODO: iteratively applying select1 may be faster than
// the linear search implemented here.
// Note that since we have already treated the invalid case (n > maxValue)
// before, there is at least one 1 after the position we retrieved,
// demanding that this loop should terminate if inputs are valid.
// If bit position exceeds maxValue, something wrong happened.
while (1) {
bitPosition = (bitPosition + 1) | 0;
t = (t + 1) | 0;
if ((bitPosition | 0) > (maxValue | 0)) {
// error
v = 0xffffffff;
break;
}
if ((t | 0) >= 32) {
higherBitsP = (higherBitsP + 4) | 0;
bitBlock = U4[higherBitsP >> 2] | 0;
t = 0;
}
bit = (bitBlock >>> t) & 1;
if (bit) {
lowBits = readBits(lowerBitsP, imul(itemId, lowerBitsSize),
lowerBitsSize) | 0;
v = ((bucketId << lowerBitsSize) + lowBits) | 0;
if ((v | 0) >= (n | 0)) {
break;
}
itemId = (itemId + 1) | 0;
} else {
bucketId = (bucketId + 1) | 0;
}
}
return v | 0;
}
/**
* Returns the number of 1s in a 32-bit integer and
* writes out the actual indices of the 1s (0 <= i < 32) into <code>outP</code>.
* Each index occupies one byte, so at most 32 bytes (32 uint8 integers) will
* be written into <code>outP</code>.
*
* Faster than the de Bruijn version for dense cases
*/
function select(n, outP) {
/*
* Type annotations
*/
n = n | 0;
outP = outP | 0;
/*
* Local variables
*/
var i = 0;
var bit = 0;
var result = 0;
/*
* Main
*/
for (; (i | 0) < 32; i = (i + 1) | 0) {
bit = n & 1;
if ((bit | 0) == 1) {
U1[(outP + result) >> 0] = i | 0;
}
result = (result + bit) | 0;
n = n >>> 1;
}
return result | 0;
}
/**
* Returns the largest number of one or more 32-bit floats.
* If the specified length is less than 1, the behavior is undefined.
*
* @param {int} p - byte offset
* @param {int} len - length
* @returns {double} - max value
*/
function maxFloat32(p, len) {
/*
* Type annotations
*/
p = p | 0;
len = len | 0;
/*
* Local variables
*/
var end = 0;
var v = 0.0;
var result = 0.0;
/*
* Main
*/
end = (p + (len << 2)) | 0;
result = +F4[p >> 2];
p = (p + 4) | 0;
for (; (p | 0) < (end | 0); p = (p + 4) | 0) {
v = +F4[p >> 2];
if (v >= result) {
result = v;
}
}
return +result;
}
/**
* Returns the sum of 32-bit floats.
* 0.0 if the specified length is less than 1.
*
* @param {int} p - byte offset
* @param {int} len - length
* @returns {double} - sum
*/
function sumFloat32(p, len) {
/*
* Type annotations
*/
p = p | 0;
len = len | 0;
/*
* Local variables
*/
var end = 0;
var v = 0.0;
var result = 0.0;
/*
* Main
*/
if ((len | 0) < 0) {
return 0.0;
}
end = (p + (len << 2)) | 0;
for (; (p | 0) < (end | 0); p = (p + 4) | 0) {
v = +F4[p >> 2];
result = result + v;
}
return +result;
}
/**
* Returns the sum of 32-bit signed integers.
* 0 if the specified length is less than 1.
*
* @param {int} p - byte offset
* @param {int} len - length
* @returns {signed} - sum
*/
function sumInt32(p, len) {
/*
* Type annotations
*/
p = p | 0;
len = len | 0;
/*
* Local variables
*/
var end = 0;
var v = 0;
var result = 0;
/*
* Main
*/
if ((len | 0) < 0) {
return 0;
}
end = (p + (len << 2)) | 0;
for (; (p | 0) < (end | 0); p = (p + 4) | 0) {
v = I4[p >> 2] | 0;
result = (result + v) | 0;
}
return result | 0;
}
/**
* Returns the logsumexp of one or more 32-bit floats.
* Always 0.0 if the specified length is less than 1.
*
* @param {int} p - byte offset
* @param {int} len - length
* @returns {double} - result of logsumexp
*/
function logsumexpFloat32(p, len) {
/*
* Type annotations
*/
p = p | 0;
len = len | 0;