tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

compare-select-i32-i64.js (14039B)


      1 // Comprehensively test wasm compare-then-select, for all combinations of
      2 //
      3 //   compare     in  i32 i64
      4 //   select      in  i32 i64
      5 //   compare-op  in  eq ne lt_s lt_u gt_s gt_u le_s le_u ge_s ge_u
      6 //
      7 // and using comparison values from all 4 quadrant points of the range,
      8 // including at least one either side, so as to catch off-by-one comparison
      9 // errors.  In total 9000 tests.
     10 
     11 const cmpVals32
     12      = [ 0x00000000, 0x00000001, 0x00000002,
     13          0x3fffffff, 0x40000000, 0x40000001,
     14          0x7fffffff, 0x80000000, 0x80000001,
     15          0xbfffffff, 0xc0000000, 0xc0000001,
     16          0xfffffffd, 0xfffffffe, 0xffffffff ];
     17 
     18 const cmpVals64
     19      = [ 0x0000000000000000n, 0x0000000000000001n, 0x0000000000000002n,
     20          0x3fffffffffffffffn, 0x4000000000000000n, 0x4000000000000001n,
     21          0x7fffffffffffffffn, 0x8000000000000000n, 0x8000000000000001n,
     22          0xbfffffffffffffffn, 0xc000000000000000n, 0xc000000000000001n,
     23          0xfffffffffffffffdn, 0xfffffffffffffffen, 0xffffffffffffffffn ];
     24 
     25 const selVal32L = 0x55551234;
     26 const selVal32R = 0x55554321;
     27 
     28 const selVal64L = 0x5555555555554771n;
     29 const selVal64R = 0x5555555555551337n;
     30 
     31 function getCmpVals(cmpTy) {
     32    if (cmpTy === 'i32') {
     33        return cmpVals32;
     34    } else {
     35        assertEq(cmpTy, 'i64');
     36        return cmpVals64;
     37    }
     38 }
     39 
     40 function classifyResult(selTy, result) {
     41    if (selTy === 'i32') {
     42        if (result === selVal32L) {
     43            return '0';
     44        }
     45        if (result === selVal32R) {
     46            return '1';
     47        }
     48    } else {
     49        assertEq(selTy, 'i64');
     50        if (result === selVal64L) {
     51            return '2';
     52        }
     53        if (result === selVal64R) {
     54            return '3';
     55        }
     56    }
     57    // Anything that is not '0', '1', '2' or '3' will cause the final
     58    // summary-string comparison below to fail.
     59    return 'BAD';
     60 }
     61 
     62 let actualSummaryString = '';
     63 
     64 for ( cmpTy of [ 'i32', 'i64' ] ) {
     65    for ( selTy of [ 'i32', 'i64' ] ) {
     66        for ( cmpOp of [ 'eq', 'ne',
     67                         'lt_s', 'lt_u', 'gt_s', 'gt_u',
     68                         'le_s', 'le_u', 'ge_s', 'ge_u' ] ) {
     69            let t =
     70             `(module
     71                (func (export "f")
     72                      (param $p1 ${cmpTy}) (param $p2 ${cmpTy})
     73                      (param $p3 ${selTy}) (param $p4 ${selTy})
     74                      (result ${selTy})
     75                  (select (local.get $p3)
     76                          (local.get $p4)
     77                          (${cmpTy}.${cmpOp} (local.get $p1) (local.get $p2)))
     78                )
     79              )`;
     80            let i = new WebAssembly.Instance(
     81                    new WebAssembly.Module(wasmTextToBinary(t)));
     82            let cmpVals = getCmpVals(cmpTy);
     83            let selValL, selValR;
     84            if (selTy === 'i32') {
     85                selValL = selVal32L;
     86                selValR = selVal32R;
     87            } else {
     88                assertEq(selTy, 'i64');
     89                selValL = selVal64L;
     90                selValR = selVal64R;
     91            }
     92            for ( cmpValL of cmpVals ) {
     93                for ( cmpValR of cmpVals ) {
     94                    let res = i.exports.f(cmpValL, cmpValR, selValL, selValR);
     95                    // We expect res to be one of selVal<selTy>{L,R} values.
     96                    // Check that and add its summary to the running string.
     97                    let classified = classifyResult(selTy, res);
     98                    actualSummaryString += classified;
     99                }
    100            }
    101        }
    102    }
    103 }
    104 
    105 // We expect to run exactly 9000 tests:
    106 // 2 cmp types x 2 sel types x 10 conditions  (= 40 wasm functions)
    107 //             x 15 cmp argLs x 15 cmp argRs   = 9000
    108 assertEq(actualSummaryString.length, 9000);
    109 
    110 // The expected summary string, one char for each test, encoded per
    111 // function classifyResult above.  150 lines of 60 chars each.
    112 
    113 let expectedSummaryString
    114 =
    115  '011111111111111101111111111111110111111111111111011111111111' +
    116  '111101111111111111110111111111111111011111111111111101111111' +
    117  '111111110111111111111111011111111111111101111111111111110111' +
    118  '111111111111011111111111111101111111111111110100000000000000' +
    119  '010000000000000001000000000000000100000000000000010000000000' +
    120  '000001000000000000000100000000000000010000000000000001000000' +
    121  '000000000100000000000000010000000000000001000000000000000100' +
    122  '000000000000010000000000000001100000011111111110000011111111' +
    123  '111000011111111111100011111111111110011111111111111011111111' +
    124  '111111111111111000000010000000000000011000000000000011100000' +
    125  '000000011110000000000011111000000000011111100000000011111110' +
    126  '000000011111111100000000000000110000000000000111000000000000' +
    127  '111100000000000111110000000000111111000000000111111100000000' +
    128  '111111110000000111111111000000111111111100000111111111110000' +
    129  '111111111111000111111111111100111111111111110111111111111111' +
    130  '111111100000000011111100000000001111100000000000111100000000' +
    131  '000011100000000000001100000000000000100000000111111111111111' +
    132  '111111101111111111111100111111111111100011111111111100001111' +
    133  '111111100000111111111100000011111111100000001111111111111111' +
    134  '011111111111111001111111111111000111111111111000011111111111' +
    135  '000001111111111000000111111111000000011111111000000001111111' +
    136  '000000000111111000000000011111000000000001111000000000000111' +
    137  '000000000000011000000000000001000000011111111100000011111111' +
    138  '110000011111111111000011111111111100011111111111110011111111' +
    139  '111111011111111000000000000000000000010000000000000011000000' +
    140  '000000011100000000000011110000000000011111000000000011111100' +
    141  '000000011111110000000000000000100000000000000110000000000000' +
    142  '111000000000000111100000000000111110000000000111111000000000' +
    143  '111111100000000111111110000000111111111000000111111111100000' +
    144  '111111111110000111111111111000111111111111100111111111111110' +
    145  '011111100000000001111100000000000111100000000000011100000000' +
    146  '000001100000000000000100000000000000000000000111111101111111' +
    147  '111111100111111111111100011111111111100001111111111100000111' +
    148  '111111100000011111111100000001111111100000000011111111111111' +
    149  '001111111111111000111111111111000011111111111000001111111111' +
    150  '000000111111111000000011111111000000001111111000000000111111' +
    151  '000000000011111000000000001111000000000000111000000000000011' +
    152  '000000000000001000000000000000233333333333333323333333333333' +
    153  '332333333333333333233333333333333323333333333333332333333333' +
    154  '333333233333333333333323333333333333332333333333333333233333' +
    155  '333333333323333333333333332333333333333333233333333333333323' +
    156  '333333333333332322222222222222232222222222222223222222222222' +
    157  '222322222222222222232222222222222223222222222222222322222222' +
    158  '222222232222222222222223222222222222222322222222222222232222' +
    159  '222222222223222222222222222322222222222222232222222222222223' +
    160  '322222233333333332222233333333333222233333333333322233333333' +
    161  '333332233333333333333233333333333333333333333222222232222222' +
    162  '222222233222222222222233322222222222233332222222222233333222' +
    163  '222222233333322222222233333332222222233333333322222222222222' +
    164  '332222222222222333222222222222333322222222222333332222222222' +
    165  '333333222222222333333322222222333333332222222333333333222222' +
    166  '333333333322222333333333332222333333333333222333333333333322' +
    167  '333333333333332333333333333333333333322222222233333322222222' +
    168  '223333322222222222333322222222222233322222222222223322222222' +
    169  '222222322222222333333333333333333333323333333333333322333333' +
    170  '333333322233333333333322223333333333322222333333333322222233' +
    171  '333333322222223333333333333333233333333333333223333333333333' +
    172  '222333333333333222233333333333222223333333333222222333333333' +
    173  '222222233333333222222223333333222222222333333222222222233333' +
    174  '222222222223333222222222222333222222222222233222222222222223' +
    175  '222222233333333322222233333333332222233333333333222233333333' +
    176  '333322233333333333332233333333333333233333333222222222222222' +
    177  '222222232222222222222233222222222222233322222222222233332222' +
    178  '222222233333222222222233333322222222233333332222222222222222' +
    179  '322222222222222332222222222222333222222222222333322222222222' +
    180  '333332222222222333333222222222333333322222222333333332222222' +
    181  '333333333222222333333333322222333333333332222333333333333222' +
    182  '333333333333322333333333333332233333322222222223333322222222' +
    183  '222333322222222222233322222222222223322222222222222322222222' +
    184  '222222222222222333333323333333333333322333333333333322233333' +
    185  '333333322223333333333322222333333333322222233333333322222223' +
    186  '333333322222222233333333333333223333333333333222333333333333' +
    187  '222233333333333222223333333333222222333333333222222233333333' +
    188  '222222223333333222222222333333222222222233333222222222223333' +
    189  '222222222222333222222222222233222222222222223222222222222222' +
    190  '011111111111111101111111111111110111111111111111011111111111' +
    191  '111101111111111111110111111111111111011111111111111101111111' +
    192  '111111110111111111111111011111111111111101111111111111110111' +
    193  '111111111111011111111111111101111111111111110100000000000000' +
    194  '010000000000000001000000000000000100000000000000010000000000' +
    195  '000001000000000000000100000000000000010000000000000001000000' +
    196  '000000000100000000000000010000000000000001000000000000000100' +
    197  '000000000000010000000000000001100000011111111110000011111111' +
    198  '111000011111111111100011111111111110011111111111111011111111' +
    199  '111111111111111000000010000000000000011000000000000011100000' +
    200  '000000011110000000000011111000000000011111100000000011111110' +
    201  '000000011111111100000000000000110000000000000111000000000000' +
    202  '111100000000000111110000000000111111000000000111111100000000' +
    203  '111111110000000111111111000000111111111100000111111111110000' +
    204  '111111111111000111111111111100111111111111110111111111111111' +
    205  '111111100000000011111100000000001111100000000000111100000000' +
    206  '000011100000000000001100000000000000100000000111111111111111' +
    207  '111111101111111111111100111111111111100011111111111100001111' +
    208  '111111100000111111111100000011111111100000001111111111111111' +
    209  '011111111111111001111111111111000111111111111000011111111111' +
    210  '000001111111111000000111111111000000011111111000000001111111' +
    211  '000000000111111000000000011111000000000001111000000000000111' +
    212  '000000000000011000000000000001000000011111111100000011111111' +
    213  '110000011111111111000011111111111100011111111111110011111111' +
    214  '111111011111111000000000000000000000010000000000000011000000' +
    215  '000000011100000000000011110000000000011111000000000011111100' +
    216  '000000011111110000000000000000100000000000000110000000000000' +
    217  '111000000000000111100000000000111110000000000111111000000000' +
    218  '111111100000000111111110000000111111111000000111111111100000' +
    219  '111111111110000111111111111000111111111111100111111111111110' +
    220  '011111100000000001111100000000000111100000000000011100000000' +
    221  '000001100000000000000100000000000000000000000111111101111111' +
    222  '111111100111111111111100011111111111100001111111111100000111' +
    223  '111111100000011111111100000001111111100000000011111111111111' +
    224  '001111111111111000111111111111000011111111111000001111111111' +
    225  '000000111111111000000011111111000000001111111000000000111111' +
    226  '000000000011111000000000001111000000000000111000000000000011' +
    227  '000000000000001000000000000000233333333333333323333333333333' +
    228  '332333333333333333233333333333333323333333333333332333333333' +
    229  '333333233333333333333323333333333333332333333333333333233333' +
    230  '333333333323333333333333332333333333333333233333333333333323' +
    231  '333333333333332322222222222222232222222222222223222222222222' +
    232  '222322222222222222232222222222222223222222222222222322222222' +
    233  '222222232222222222222223222222222222222322222222222222232222' +
    234  '222222222223222222222222222322222222222222232222222222222223' +
    235  '322222233333333332222233333333333222233333333333322233333333' +
    236  '333332233333333333333233333333333333333333333222222232222222' +
    237  '222222233222222222222233322222222222233332222222222233333222' +
    238  '222222233333322222222233333332222222233333333322222222222222' +
    239  '332222222222222333222222222222333322222222222333332222222222' +
    240  '333333222222222333333322222222333333332222222333333333222222' +
    241  '333333333322222333333333332222333333333333222333333333333322' +
    242  '333333333333332333333333333333333333322222222233333322222222' +
    243  '223333322222222222333322222222222233322222222222223322222222' +
    244  '222222322222222333333333333333333333323333333333333322333333' +
    245  '333333322233333333333322223333333333322222333333333322222233' +
    246  '333333322222223333333333333333233333333333333223333333333333' +
    247  '222333333333333222233333333333222223333333333222222333333333' +
    248  '222222233333333222222223333333222222222333333222222222233333' +
    249  '222222222223333222222222222333222222222222233222222222222223' +
    250  '222222233333333322222233333333332222233333333333222233333333' +
    251  '333322233333333333332233333333333333233333333222222222222222' +
    252  '222222232222222222222233222222222222233322222222222233332222' +
    253  '222222233333222222222233333322222222233333332222222222222222' +
    254  '322222222222222332222222222222333222222222222333322222222222' +
    255  '333332222222222333333222222222333333322222222333333332222222' +
    256  '333333333222222333333333322222333333333332222333333333333222' +
    257  '333333333333322333333333333332233333322222222223333322222222' +
    258  '222333322222222222233322222222222223322222222222222322222222' +
    259  '222222222222222333333323333333333333322333333333333322233333' +
    260  '333333322223333333333322222333333333322222233333333322222223' +
    261  '333333322222222233333333333333223333333333333222333333333333' +
    262  '222233333333333222223333333333222222333333333222222233333333' +
    263  '222222223333333222222222333333222222222233333222222222223333' +
    264  '222222222222333222222222222233222222222222223222222222222222';
    265 
    266 assertEq(expectedSummaryString.length, 9000); // stay sane
    267 
    268 assertEq(actualSummaryString, expectedSummaryString);