rt/emul/compacttest/src/test/java/org/apidesign/bck2brwsr/tck/LongArithmeticTest.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 07 Jan 2015 14:17:22 +0100
changeset 1761 d2a5a7a0e167
parent 1723 3a1f262311cf
child 1779 9d757281c666
permissions -rw-r--r--
Using >>> in shr64
Martin@582
     1
/**
Martin@582
     2
 * Back 2 Browser Bytecode Translator
Martin@582
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Martin@582
     4
 *
Martin@582
     5
 * This program is free software: you can redistribute it and/or modify
Martin@582
     6
 * it under the terms of the GNU General Public License as published by
Martin@582
     7
 * the Free Software Foundation, version 2 of the License.
Martin@582
     8
 *
Martin@582
     9
 * This program is distributed in the hope that it will be useful,
Martin@582
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Martin@582
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Martin@582
    12
 * GNU General Public License for more details.
Martin@582
    13
 *
Martin@582
    14
 * You should have received a copy of the GNU General Public License
Martin@582
    15
 * along with this program. Look for COPYING file in the top folder.
Martin@582
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
Martin@582
    17
 */
Martin@582
    18
package org.apidesign.bck2brwsr.tck;
Martin@582
    19
Martin@582
    20
import org.apidesign.bck2brwsr.vmtest.Compare;
Martin@582
    21
import org.apidesign.bck2brwsr.vmtest.VMTest;
Martin@582
    22
import org.testng.annotations.Factory;
Martin@582
    23
Martin@582
    24
/**
Martin@582
    25
 *
Martin@582
    26
 * @author Jaroslav Tulach <jtulach@netbeans.org>
Martin@582
    27
 */
Martin@582
    28
public class LongArithmeticTest {
lubomir@778
    29
Martin@582
    30
    private static long add(long x, long y) {
Martin@582
    31
        return (x + y);
Martin@582
    32
    }
lubomir@778
    33
Martin@582
    34
    private static long sub(long x, long y) {
Martin@582
    35
        return (x - y);
Martin@582
    36
    }
lubomir@778
    37
Martin@582
    38
    private static long mul(long x, long y) {
Martin@582
    39
        return (x * y);
Martin@582
    40
    }
jaroslav@1761
    41
    
Martin@582
    42
    private static long div(long x, long y) {
Martin@582
    43
        return (x / y);
Martin@582
    44
    }
lubomir@778
    45
Martin@582
    46
    private static long mod(long x, long y) {
Martin@582
    47
        return (x % y);
Martin@582
    48
    }
lubomir@778
    49
Martin@698
    50
    private static long neg(long x) {
Martin@699
    51
        return (-x);
Martin@699
    52
    }
lubomir@778
    53
Martin@699
    54
    private static long shl(long x, int b) {
Martin@699
    55
        return (x << b);
Martin@699
    56
    }
lubomir@778
    57
Martin@699
    58
    private static long shr(long x, int b) {
Martin@699
    59
        return (x >> b);
Martin@699
    60
    }
lubomir@778
    61
Martin@699
    62
    private static long ushr(long x, int b) {
Martin@699
    63
        return (x >>> b);
Martin@699
    64
    }
lubomir@778
    65
Martin@699
    66
    private static long and(long x, long y) {
Martin@699
    67
        return (x & y);
Martin@699
    68
    }
lubomir@778
    69
Martin@699
    70
    private static long or(long x, long y) {
Martin@699
    71
        return (x | y);
Martin@699
    72
    }
lubomir@778
    73
Martin@699
    74
    private static long xor(long x, long y) {
Martin@699
    75
        return (x ^ y);
Martin@699
    76
    }
lubomir@778
    77
lubomir@778
    78
    private static float fadd(float x, float y) {
lubomir@778
    79
        return x + y;
lubomir@778
    80
    }
lubomir@778
    81
lubomir@778
    82
    private static double dadd(double x, double y) {
lubomir@778
    83
        return x + y;
lubomir@778
    84
    }
jaroslav@1761
    85
    
jaroslav@1761
    86
    private static long mul64shr64(long base, int mul, int shr) {
jaroslav@1761
    87
        long tmp = base * mul;
jaroslav@1761
    88
        if (shr == -1) {
jaroslav@1761
    89
            return tmp;
jaroslav@1761
    90
        }
jaroslav@1761
    91
        return tmp >> shr;
jaroslav@1761
    92
    }
lubomir@778
    93
Martin@699
    94
    public static int compare(long x, long y, int zero) {
Martin@699
    95
        final int xyResult = compareL(x, y, zero);
Martin@699
    96
        final int yxResult = compareL(y, x, zero);
Martin@699
    97
Martin@699
    98
        return ((xyResult + yxResult) == 0) ? xyResult : -2;
Martin@699
    99
    }
Martin@699
   100
Martin@699
   101
    private static int compareL(long x, long y, int zero) {
Martin@699
   102
        int result = -2;
Martin@699
   103
        int trueCount = 0;
Martin@699
   104
Martin@699
   105
        x += zero;
Martin@699
   106
        if (x == y) {
Martin@699
   107
            result = 0;
Martin@699
   108
            ++trueCount;
Martin@699
   109
        }
Martin@699
   110
Martin@699
   111
        x += zero;
Martin@699
   112
        if (x < y) {
Martin@699
   113
            result = -1;
Martin@699
   114
            ++trueCount;
Martin@699
   115
        }
Martin@699
   116
Martin@699
   117
        x += zero;
Martin@699
   118
        if (x > y) {
Martin@699
   119
            result = 1;
Martin@699
   120
            ++trueCount;
Martin@699
   121
        }
Martin@699
   122
Martin@699
   123
        return (trueCount == 1) ? result : -2;
Martin@698
   124
    }
jaroslav@1663
   125
    
jaroslav@1663
   126
    @Compare public int parameterSlotCount() {
jaroslav@1663
   127
        long argCounts = 281479271874563L;
jaroslav@1663
   128
        int x = unpack(argCounts, 2);
jaroslav@1663
   129
        return x;
jaroslav@1663
   130
    }
jaroslav@1663
   131
    private static char unpack(long packed, int word) { // word==0 => return a, ==3 => return d
jaroslav@1663
   132
        assert(word <= 3);
jaroslav@1663
   133
        final long val = packed >> ((3-word) * 16);
jaroslav@1663
   134
        return (char)val;
jaroslav@1663
   135
    }
Martin@582
   136
    @Compare public long conversion() {
Martin@582
   137
        return Long.MAX_VALUE;
Martin@582
   138
    }
lubomir@778
   139
Martin@698
   140
    @Compare public long negate1() {
Martin@698
   141
        return neg(0x00fa37d7763e0ca1l);
Martin@698
   142
    }
lubomir@778
   143
Martin@698
   144
    @Compare public long negate2() {
Martin@698
   145
        return neg(0x80fa37d7763e0ca1l);
Martin@698
   146
    }
Martin@698
   147
Martin@698
   148
    @Compare public long negate3() {
Martin@698
   149
        return neg(0xfffffffffffffeddl);
Martin@698
   150
    }
Martin@698
   151
Martin@582
   152
    @Compare public long addOverflow() {
Martin@582
   153
        return add(Long.MAX_VALUE, 1l);
Martin@582
   154
    }
Martin@698
   155
Martin@582
   156
    @Compare public long subUnderflow() {
Martin@582
   157
        return sub(Long.MIN_VALUE, 1l);
Martin@582
   158
    }
Martin@698
   159
Martin@582
   160
    @Compare public long addMaxLongAndMaxLong() {
Martin@582
   161
        return add(Long.MAX_VALUE, Long.MAX_VALUE);
Martin@582
   162
    }
lubomir@778
   163
Martin@582
   164
    @Compare public long subMinLongAndMinLong() {
Martin@582
   165
        return sub(Long.MIN_VALUE, Long.MIN_VALUE);
Martin@582
   166
    }
lubomir@778
   167
Martin@698
   168
    @Compare public long subMinLongAndMaxLong() {
Martin@698
   169
        return sub(Long.MIN_VALUE, Long.MAX_VALUE);
Martin@698
   170
    }
Martin@698
   171
Martin@582
   172
    @Compare public long multiplyMaxLong() {
Martin@582
   173
        return mul(Long.MAX_VALUE, 2l);
Martin@582
   174
    }
lubomir@778
   175
Martin@582
   176
    @Compare public long multiplyMaxLongAndMaxLong() {
Martin@582
   177
        return mul(Long.MAX_VALUE, Long.MAX_VALUE);
Martin@582
   178
    }
lubomir@778
   179
Martin@582
   180
    @Compare public long multiplyMinLong() {
Martin@582
   181
        return mul(Long.MIN_VALUE, 2l);
Martin@582
   182
    }
lubomir@778
   183
Martin@582
   184
    @Compare public long multiplyMinLongAndMinLong() {
Martin@582
   185
        return mul(Long.MIN_VALUE, Long.MIN_VALUE);
Martin@582
   186
    }
lubomir@778
   187
Martin@582
   188
    @Compare public long multiplyPrecision() {
Martin@698
   189
        return mul(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
Martin@582
   190
    }
lubomir@778
   191
Martin@698
   192
    @Compare public long divideSmallPositiveNumbers() {
Martin@698
   193
        return div(0xabcdef, 0x123);
Martin@582
   194
    }
Martin@698
   195
Martin@698
   196
    @Compare public long divideSmallNegativeNumbers() {
Martin@698
   197
        return div(-0xabcdef, -0x123);
Martin@582
   198
    }
Martin@698
   199
Martin@698
   200
    @Compare public long divideSmallMixedNumbers() {
Martin@698
   201
        return div(0xabcdef, -0x123);
Martin@698
   202
    }
Martin@698
   203
Martin@698
   204
    @Compare public long dividePositiveNumbersOneDigitDenom() {
Martin@698
   205
        return div(0xabcdef0102ffffl, 0x654);
Martin@698
   206
    }
Martin@698
   207
Martin@698
   208
    @Compare public long divideNegativeNumbersOneDigitDenom() {
Martin@698
   209
        return div(-0xabcdef0102ffffl, -0x654);
Martin@698
   210
    }
Martin@698
   211
Martin@698
   212
    @Compare public long divideMixedNumbersOneDigitDenom() {
Martin@698
   213
        return div(-0xabcdef0102ffffl, 0x654);
Martin@698
   214
    }
Martin@698
   215
Martin@698
   216
    @Compare public long dividePositiveNumbersMultiDigitDenom() {
Martin@698
   217
        return div(0x7ffefc003322aabbl, 0x89ab1000l);
Martin@698
   218
    }
Martin@698
   219
Martin@698
   220
    @Compare public long divideNegativeNumbersMultiDigitDenom() {
Martin@698
   221
        return div(-0x7ffefc003322aabbl, -0x123489ab1001l);
Martin@698
   222
    }
Martin@698
   223
Martin@698
   224
    @Compare public long divideMixedNumbersMultiDigitDenom() {
Martin@698
   225
        return div(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
Martin@698
   226
    }
Martin@698
   227
Martin@698
   228
    @Compare public long divideWithOverflow() {
Martin@698
   229
        return div(0x8000fffe0000l, 0x8000ffffl);
Martin@698
   230
    }
Martin@698
   231
Martin@698
   232
    @Compare public long divideWithCorrection() {
Martin@698
   233
        return div(0x7fff800000000000l, 0x800000000001l);
Martin@698
   234
    }
Martin@698
   235
Martin@698
   236
    @Compare public long moduloSmallPositiveNumbers() {
Martin@698
   237
        return mod(0xabcdef, 0x123);
Martin@698
   238
    }
Martin@698
   239
Martin@698
   240
    @Compare public long moduloSmallNegativeNumbers() {
Martin@698
   241
        return mod(-0xabcdef, -0x123);
Martin@698
   242
    }
Martin@698
   243
Martin@698
   244
    @Compare public long moduloSmallMixedNumbers() {
Martin@698
   245
        return mod(0xabcdef, -0x123);
Martin@698
   246
    }
Martin@698
   247
Martin@698
   248
    @Compare public long moduloPositiveNumbersOneDigitDenom() {
Martin@698
   249
        return mod(0xabcdef0102ffffl, 0x654);
Martin@698
   250
    }
Martin@698
   251
Martin@698
   252
    @Compare public long moduloNegativeNumbersOneDigitDenom() {
Martin@698
   253
        return mod(-0xabcdef0102ffffl, -0x654);
Martin@698
   254
    }
Martin@698
   255
Martin@698
   256
    @Compare public long moduloMixedNumbersOneDigitDenom() {
Martin@698
   257
        return mod(-0xabcdef0102ffffl, 0x654);
Martin@698
   258
    }
Martin@698
   259
Martin@698
   260
    @Compare public long moduloPositiveNumbersMultiDigitDenom() {
Martin@698
   261
        return mod(0x7ffefc003322aabbl, 0x89ab1000l);
Martin@698
   262
    }
Martin@698
   263
Martin@698
   264
    @Compare public long moduloNegativeNumbersMultiDigitDenom() {
Martin@698
   265
        return mod(-0x7ffefc003322aabbl, -0x123489ab1001l);
Martin@698
   266
    }
Martin@698
   267
Martin@698
   268
    @Compare public long moduloMixedNumbersMultiDigitDenom() {
Martin@698
   269
        return mod(0x7ffefc003322aabbl, -0x38f49b0b7574e36l);
Martin@698
   270
    }
Martin@698
   271
Martin@698
   272
    @Compare public long moduloWithOverflow() {
Martin@698
   273
        return mod(0x8000fffe0000l, 0x8000ffffl);
Martin@698
   274
    }
Martin@698
   275
Martin@698
   276
    @Compare public long moduloWithCorrection() {
Martin@698
   277
        return mod(0x7fff800000000000l, 0x800000000001l);
Martin@698
   278
    }
lubomir@737
   279
lubomir@778
   280
    @Compare public long conversionFromFloatPositive() {
lubomir@778
   281
        return (long) fadd(2, 0.6f);
lubomir@778
   282
    }
lubomir@778
   283
lubomir@778
   284
    @Compare public long conversionFromFloatNegative() {
lubomir@778
   285
        return (long) fadd(-2, -0.6f);
lubomir@778
   286
    }
lubomir@778
   287
lubomir@778
   288
    @Compare public long conversionFromDoublePositive() {
lubomir@778
   289
        return (long) dadd(0x20ffff0000L, 0.6);
lubomir@778
   290
    }
lubomir@778
   291
lubomir@778
   292
    @Compare public long conversionFromDoubleNegative() {
lubomir@778
   293
        return (long) dadd(-0x20ffff0000L, -0.6);
lubomir@778
   294
    }
lubomir@778
   295
lubomir@737
   296
    @Compare public boolean divByZeroThrowsArithmeticException() {
lubomir@737
   297
        try {
lubomir@737
   298
            div(1, 0);
lubomir@737
   299
            return false;
lubomir@737
   300
        } catch (final ArithmeticException e) {
lubomir@737
   301
            return true;
lubomir@737
   302
        }
lubomir@737
   303
    }
lubomir@737
   304
lubomir@737
   305
    @Compare public boolean modByZeroThrowsArithmeticException() {
lubomir@737
   306
        try {
lubomir@737
   307
            mod(1, 0);
lubomir@737
   308
            return false;
lubomir@737
   309
        } catch (final ArithmeticException e) {
lubomir@737
   310
            return true;
lubomir@737
   311
        }
lubomir@737
   312
    }
lubomir@737
   313
Martin@699
   314
    @Compare public long shiftL1() {
Martin@699
   315
        return shl(0x00fa37d7763e0ca1l, 5);
Martin@699
   316
    }
lubomir@778
   317
Martin@699
   318
    @Compare public long shiftL2() {
Martin@699
   319
        return shl(0x00fa37d7763e0ca1l, 32);
Martin@699
   320
    }
lubomir@778
   321
Martin@699
   322
    @Compare public long shiftL3() {
Martin@699
   323
        return shl(0x00fa37d7763e0ca1l, 45);
Martin@699
   324
    }
Martin@1352
   325
    
Martin@1352
   326
    @Compare public long shiftL4() {
Martin@1352
   327
        return shl(0x00fa37d7763e0ca1l, 0);
Martin@1352
   328
    }
Martin@1352
   329
    
Martin@1352
   330
    @Compare public long shiftL5() {
Martin@1352
   331
        return shl(0x00fa37d7763e0ca1l, 70);
Martin@1352
   332
    }
lubomir@778
   333
Martin@699
   334
    @Compare public long shiftR1() {
Martin@699
   335
        return shr(0x00fa37d7763e0ca1l, 5);
Martin@699
   336
    }
lubomir@778
   337
Martin@699
   338
    @Compare public long shiftR2() {
Martin@699
   339
        return shr(0x00fa37d7763e0ca1l, 32);
Martin@699
   340
    }
lubomir@778
   341
Martin@699
   342
    @Compare public long shiftR3() {
Martin@699
   343
        return shr(0x00fa37d7763e0ca1l, 45);
Martin@699
   344
    }
Martin@1352
   345
    
Martin@1352
   346
    @Compare public long shiftR4() {
Martin@1352
   347
        return shr(0x00fa37d7763e0ca1l, 0);
Martin@1352
   348
    }
Martin@1352
   349
    
Martin@1352
   350
    @Compare public long shiftR5() {
Martin@1352
   351
        return shr(0x00fa37d7763e0ca1l, 70);
Martin@1352
   352
    }
lubomir@778
   353
Martin@699
   354
    @Compare public long uShiftR1() {
Martin@699
   355
        return ushr(0x00fa37d7763e0ca1l, 5);
Martin@699
   356
    }
lubomir@778
   357
Martin@699
   358
    @Compare public long uShiftR2() {
Martin@699
   359
        return ushr(0x00fa37d7763e0ca1l, 45);
Martin@699
   360
    }
Martin@1352
   361
    
Martin@1352
   362
    @Compare public long uShiftR3() {
Martin@1352
   363
        return ushr(0x00fa37d7763e0ca1l, 0);
Martin@1352
   364
    }
Martin@1352
   365
    
Martin@1352
   366
    @Compare public long uShiftR4() {
Martin@1352
   367
        return ushr(0x00fa37d7763e0ca1l, 70);
Martin@1352
   368
    }
lubomir@778
   369
Martin@1352
   370
    @Compare public long uShiftR5() {
Martin@699
   371
        return ushr(0xf0fa37d7763e0ca1l, 5);
Martin@699
   372
    }
lubomir@778
   373
Martin@1352
   374
    @Compare public long uShiftR6() {
Martin@699
   375
        return ushr(0xf0fa37d7763e0ca1l, 45);
Martin@699
   376
    }
Martin@1352
   377
    
Martin@1352
   378
    @Compare public long uShiftR7() {
Martin@1352
   379
        return ushr(0xf0fa37d7763e0ca1l, 0);
Martin@1352
   380
    }
Martin@1352
   381
    
Martin@1352
   382
    @Compare public long uShiftR8() {
Martin@1352
   383
        return ushr(0xf0fa37d7763e0ca1l, 70);
Martin@1352
   384
    }
lubomir@778
   385
Martin@699
   386
    @Compare public long and1() {
Martin@699
   387
        return and(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
Martin@699
   388
    }
lubomir@778
   389
Martin@699
   390
    @Compare public long or1() {
Martin@699
   391
        return or(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
Martin@699
   392
    }
lubomir@778
   393
Martin@699
   394
    @Compare public long xor1() {
Martin@699
   395
        return xor(0x00fa37d7763e0ca1l, 0xa7b3432fff00123el);
Martin@699
   396
    }
lubomir@778
   397
Martin@699
   398
    @Compare public long xor2() {
Martin@699
   399
        return xor(0x00fa37d7763e0ca1l, 0x00000000ff00123el);
Martin@699
   400
    }
lubomir@778
   401
Martin@699
   402
    @Compare public long xor3() {
Martin@699
   403
        return xor(0x00000000763e0ca1l, 0x00000000ff00123el);
Martin@699
   404
    }
lubomir@778
   405
Martin@699
   406
    @Compare public int compareSameNumbers() {
Martin@699
   407
        return compare(0x0000000000000000l, 0x0000000000000000l, 0);
Martin@699
   408
    }
Martin@699
   409
Martin@699
   410
    @Compare public int comparePositiveNumbers() {
Martin@699
   411
        return compare(0x0000000000200000l, 0x0000000010000000l, 0);
Martin@699
   412
    }
Martin@699
   413
Martin@699
   414
    @Compare public int compareNegativeNumbers() {
Martin@699
   415
        return compare(0xffffffffffffffffl, 0xffffffff00000000l, 0);
Martin@699
   416
    }
Martin@699
   417
Martin@699
   418
    @Compare public int compareMixedNumbers() {
Martin@699
   419
        return compare(0x8000000000000000l, 0x7fffffffffffffffl, 0);
Martin@699
   420
    }
Martin@699
   421
    
jaroslav@1761
   422
    @Compare public int intConversionProblemInRandom() {
jaroslav@1761
   423
        long res = mul64shr64(16, 1561751147, 31);
jaroslav@1761
   424
        return (int) res;
jaroslav@1761
   425
    }
jaroslav@1761
   426
jaroslav@1761
   427
    @Compare public long shiftProblemInRandom() {
jaroslav@1761
   428
        long res = mul64shr64(16, 1561751147, 31);
jaroslav@1761
   429
        return res;
jaroslav@1761
   430
    }
jaroslav@1761
   431
jaroslav@1761
   432
    @Compare public long multiplyProblemInRandom() {
jaroslav@1761
   433
        long res = mul64shr64(16, 1561751147, -1);
jaroslav@1761
   434
        return res;
jaroslav@1761
   435
    }
jaroslav@1761
   436
    
Martin@582
   437
    @Factory
Martin@582
   438
    public static Object[] create() {
Martin@582
   439
        return VMTest.create(LongArithmeticTest.class);
Martin@582
   440
    }
Martin@582
   441
}