Extracting direct references to 64-bit operations
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Jan 2016 05:54:37 +0100
changeset 1862fa00fb053c72
parent 1861 433856f897dd
child 1863 14a122c18d73
Extracting direct references to 64-bit operations
benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/n64/Filter.java
benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/n64/Natural.java
benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/n64/Primes.java
benchmarks/sieve/src/test/java/org/apidesign/benchmark/sieve/n64/LongSieveTest.java
rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java
rt/vm/src/main/java/org/apidesign/vm4brwsr/NumberOperations.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/n64/Filter.java	Tue Jan 26 05:54:37 2016 +0100
     1.3 @@ -0,0 +1,41 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.benchmark.sieve.n64;
    1.22 +
    1.23 +final class Filter {
    1.24 +    private final long number;
    1.25 +    private final Filter next;
    1.26 +
    1.27 +    public Filter(long number, Filter next) {
    1.28 +        this.number = number;
    1.29 +        this.next = next;
    1.30 +    }
    1.31 +
    1.32 +    public boolean accept(long n) {
    1.33 +        Filter filter = this;
    1.34 +        for (;;) {
    1.35 +            if (n % filter.number == 0) {
    1.36 +                return false;
    1.37 +            }
    1.38 +            filter = filter.next;
    1.39 +            if (filter == null) {
    1.40 +                return true;
    1.41 +            }
    1.42 +        }
    1.43 +    }
    1.44 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/n64/Natural.java	Tue Jan 26 05:54:37 2016 +0100
     2.3 @@ -0,0 +1,26 @@
     2.4 +/**
     2.5 + * Back 2 Browser Bytecode Translator
     2.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     2.7 + *
     2.8 + * This program is free software: you can redistribute it and/or modify
     2.9 + * it under the terms of the GNU General Public License as published by
    2.10 + * the Free Software Foundation, version 2 of the License.
    2.11 + *
    2.12 + * This program is distributed in the hope that it will be useful,
    2.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.15 + * GNU General Public License for more details.
    2.16 + *
    2.17 + * You should have received a copy of the GNU General Public License
    2.18 + * along with this program. Look for COPYING file in the top folder.
    2.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    2.20 + */
    2.21 +package org.apidesign.benchmark.sieve.n64;
    2.22 +
    2.23 +final class Natural {
    2.24 +    private long cnt = 2;
    2.25 +
    2.26 +    long next() {
    2.27 +        return cnt++;
    2.28 +    }
    2.29 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/benchmarks/sieve/src/main/java/org/apidesign/benchmark/sieve/n64/Primes.java	Tue Jan 26 05:54:37 2016 +0100
     3.3 @@ -0,0 +1,55 @@
     3.4 +/**
     3.5 + * Back 2 Browser Bytecode Translator
     3.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     3.7 + *
     3.8 + * This program is free software: you can redistribute it and/or modify
     3.9 + * it under the terms of the GNU General Public License as published by
    3.10 + * the Free Software Foundation, version 2 of the License.
    3.11 + *
    3.12 + * This program is distributed in the hope that it will be useful,
    3.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    3.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    3.15 + * GNU General Public License for more details.
    3.16 + *
    3.17 + * You should have received a copy of the GNU General Public License
    3.18 + * along with this program. Look for COPYING file in the top folder.
    3.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    3.20 + */
    3.21 +package org.apidesign.benchmark.sieve.n64;
    3.22 +
    3.23 +abstract class Primes {
    3.24 +    private final Natural natural;
    3.25 +    private Filter filter;
    3.26 +
    3.27 +    protected Primes() {
    3.28 +        this.natural = new Natural();
    3.29 +    }
    3.30 +
    3.31 +    long next() {
    3.32 +        for (;;) {
    3.33 +            long n = natural.next();
    3.34 +            if (filter == null || filter.accept(n)) {
    3.35 +                filter = new Filter(n, filter);
    3.36 +                return n;
    3.37 +            }
    3.38 +        }
    3.39 +    }
    3.40 +
    3.41 +    protected abstract void log(String msg);
    3.42 +
    3.43 +    public final long compute(long count) {
    3.44 +        long cnt = 0;
    3.45 +        long res;
    3.46 +        for (;;) {
    3.47 +            res = next();
    3.48 +            cnt += 1;
    3.49 +            if (cnt % 1000 == 0) {
    3.50 +                log("Computed " + cnt + " primes. Last one is " + res);
    3.51 +            }
    3.52 +            if (cnt >= count) {
    3.53 +                break;
    3.54 +            }
    3.55 +        }
    3.56 +        return res;
    3.57 +    }
    3.58 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/benchmarks/sieve/src/test/java/org/apidesign/benchmark/sieve/n64/LongSieveTest.java	Tue Jan 26 05:54:37 2016 +0100
     4.3 @@ -0,0 +1,79 @@
     4.4 +/**
     4.5 + * Back 2 Browser Bytecode Translator
     4.6 + * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4.7 + *
     4.8 + * This program is free software: you can redistribute it and/or modify
     4.9 + * it under the terms of the GNU General Public License as published by
    4.10 + * the Free Software Foundation, version 2 of the License.
    4.11 + *
    4.12 + * This program is distributed in the hope that it will be useful,
    4.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    4.15 + * GNU General Public License for more details.
    4.16 + *
    4.17 + * You should have received a copy of the GNU General Public License
    4.18 + * along with this program. Look for COPYING file in the top folder.
    4.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    4.20 + */
    4.21 +package org.apidesign.benchmark.sieve.n64;
    4.22 +
    4.23 +import java.io.IOException;
    4.24 +import net.java.html.js.JavaScriptBody;
    4.25 +import org.apidesign.bck2brwsr.vmtest.Compare;
    4.26 +import org.apidesign.bck2brwsr.vmtest.VMTest;
    4.27 +import org.testng.annotations.Factory;
    4.28 +
    4.29 +/**
    4.30 + *
    4.31 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    4.32 + */
    4.33 +public class LongSieveTest extends Primes {
    4.34 +    public LongSieveTest() {
    4.35 +    }
    4.36 +
    4.37 +    @JavaScriptBody(args = {  }, body = "return new Date().getTime();")
    4.38 +    protected int time() {
    4.39 +        return (int) System.currentTimeMillis();
    4.40 +    }
    4.41 +
    4.42 +    @Compare
    4.43 +    public long oneThousand() throws IOException {
    4.44 +        LongSieveTest sieve = new LongSieveTest();
    4.45 +        int now = time();
    4.46 +        long res = sieve.compute(1000);
    4.47 +        int took = time() - now;
    4.48 +        log("oneThousand in " + took + " ms");
    4.49 +        return res;
    4.50 +    }
    4.51 +
    4.52 +/*
    4.53 +    @Compare(slowdown = 3.0)
    4.54 +    public long fiveThousand() throws IOException {
    4.55 +        LongSieveTest sieve = new LongSieveTest();
    4.56 +        int now = time();
    4.57 +        long res = sieve.compute(5000);
    4.58 +        int took = time() - now;
    4.59 +        log("oneThousand in " + took + " ms");
    4.60 +        return res;
    4.61 +    }
    4.62 +    @Compare(slowdown = 3.0)
    4.63 +    public long tenThousand() throws IOException {
    4.64 +        LongSieveTest sieve = new LongSieveTest();
    4.65 +        int now = time();
    4.66 +        long res = sieve.compute(10000);
    4.67 +        int took = time() - now;
    4.68 +        log("oneThousand in " + took + " ms");
    4.69 +        return res;
    4.70 +    }
    4.71 +*/
    4.72 +    @Factory
    4.73 +    public static Object[] create() {
    4.74 +        return VMTest.create(LongSieveTest.class);
    4.75 +    }
    4.76 +
    4.77 +    @JavaScriptBody(args = { "msg" }, body = "if (typeof console !== 'undefined') console.log(msg);")
    4.78 +    @Override
    4.79 +    protected void log(String msg) {
    4.80 +        System.err.println(msg);
    4.81 +    }
    4.82 +}
     5.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Tue Jan 26 04:42:06 2016 +0100
     5.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/ByteCodeToJavaScript.java	Tue Jan 26 05:54:37 2016 +0100
     5.3 @@ -735,7 +735,7 @@
     5.4                      smapper.replace(this, VarType.INTEGER, "(((@1) + (@2)) | 0)", smapper.getI(1), smapper.popI());
     5.5                      break;
     5.6                  case opc_ladd:
     5.7 -                    smapper.replace(this, VarType.LONG, "(@1).add64(@2)", smapper.getL(1), smapper.popL());
     5.8 +                    smapper.replace(this, VarType.LONG, numbers.add64(), smapper.getL(1), smapper.popL());
     5.9                      break;
    5.10                  case opc_fadd:
    5.11                      smapper.replace(this, VarType.FLOAT, "(@1 + @2)", smapper.getF(1), smapper.popF());
    5.12 @@ -747,7 +747,7 @@
    5.13                      smapper.replace(this, VarType.INTEGER, "(((@1) - (@2)) | 0)", smapper.getI(1), smapper.popI());
    5.14                      break;
    5.15                  case opc_lsub:
    5.16 -                    smapper.replace(this, VarType.LONG, "(@1).sub64(@2)", smapper.getL(1), smapper.popL());
    5.17 +                    smapper.replace(this, VarType.LONG, numbers.sub64(), smapper.getL(1), smapper.popL());
    5.18                      break;
    5.19                  case opc_fsub:
    5.20                      smapper.replace(this, VarType.FLOAT, "(@1 - @2)", smapper.getF(1), smapper.popF());
    5.21 @@ -759,7 +759,7 @@
    5.22                      smapper.replace(this, VarType.INTEGER, numbers.mul32(), smapper.getI(1), smapper.popI());
    5.23                      break;
    5.24                  case opc_lmul:
    5.25 -                    smapper.replace(this, VarType.LONG, "(@1).mul64(@2)", smapper.getL(1), smapper.popL());
    5.26 +                    smapper.replace(this, VarType.LONG, numbers.mul64(), smapper.getL(1), smapper.popL());
    5.27                      break;
    5.28                  case opc_fmul:
    5.29                      smapper.replace(this, VarType.FLOAT, "(@1 * @2)", smapper.getF(1), smapper.popF());
    5.30 @@ -772,7 +772,7 @@
    5.31                           smapper.getI(1), smapper.popI());
    5.32                      break;
    5.33                  case opc_ldiv:
    5.34 -                    smapper.replace(this, VarType.LONG, "(@1).div64(@2)",
    5.35 +                    smapper.replace(this, VarType.LONG, numbers.div64(),
    5.36                           smapper.getL(1), smapper.popL());
    5.37                      break;
    5.38                  case opc_fdiv:
    5.39 @@ -786,7 +786,7 @@
    5.40                           smapper.getI(1), smapper.popI());
    5.41                      break;
    5.42                  case opc_lrem:
    5.43 -                    smapper.replace(this, VarType.LONG, "(@1).mod64(@2)",
    5.44 +                    smapper.replace(this, VarType.LONG, numbers.mod64(),
    5.45                           smapper.getL(1), smapper.popL());
    5.46                      break;
    5.47                  case opc_frem:
    5.48 @@ -799,25 +799,25 @@
    5.49                      smapper.replace(this, VarType.INTEGER, "(@1 & @2)", smapper.getI(1), smapper.popI());
    5.50                      break;
    5.51                  case opc_land:
    5.52 -                    smapper.replace(this, VarType.LONG, "(@1).and64(@2)", smapper.getL(1), smapper.popL());
    5.53 +                    smapper.replace(this, VarType.LONG, numbers.and64(), smapper.getL(1), smapper.popL());
    5.54                      break;
    5.55                  case opc_ior:
    5.56                      smapper.replace(this, VarType.INTEGER, "(@1 | @2)", smapper.getI(1), smapper.popI());
    5.57                      break;
    5.58                  case opc_lor:
    5.59 -                    smapper.replace(this, VarType.LONG, "(@1).or64(@2)", smapper.getL(1), smapper.popL());
    5.60 +                    smapper.replace(this, VarType.LONG, numbers.or64(), smapper.getL(1), smapper.popL());
    5.61                      break;
    5.62                  case opc_ixor:
    5.63                      smapper.replace(this, VarType.INTEGER, "(@1 ^ @2)", smapper.getI(1), smapper.popI());
    5.64                      break;
    5.65                  case opc_lxor:
    5.66 -                    smapper.replace(this, VarType.LONG, "(@1).xor64(@2)", smapper.getL(1), smapper.popL());
    5.67 +                    smapper.replace(this, VarType.LONG, numbers.xor64(), smapper.getL(1), smapper.popL());
    5.68                      break;
    5.69                  case opc_ineg:
    5.70                      smapper.replace(this, VarType.INTEGER, "(-(@1))", smapper.getI(0));
    5.71                      break;
    5.72                  case opc_lneg:
    5.73 -                    smapper.replace(this, VarType.LONG, "(@1).neg64()", smapper.getL(0));
    5.74 +                    smapper.replace(this, VarType.LONG, numbers.neg64(), smapper.getL(0));
    5.75                      break;
    5.76                  case opc_fneg:
    5.77                      smapper.replace(this, VarType.FLOAT, "(-@1)", smapper.getF(0));
    5.78 @@ -829,19 +829,19 @@
    5.79                      smapper.replace(this, VarType.INTEGER, "(@1 << @2)", smapper.getI(1), smapper.popI());
    5.80                      break;
    5.81                  case opc_lshl:
    5.82 -                    smapper.replace(this, VarType.LONG, "(@1).shl64(@2)", smapper.getL(1), smapper.popI());
    5.83 +                    smapper.replace(this, VarType.LONG, numbers.shl64(), smapper.getL(1), smapper.popI());
    5.84                      break;
    5.85                  case opc_ishr:
    5.86                      smapper.replace(this, VarType.INTEGER, "(@1 >> @2)", smapper.getI(1), smapper.popI());
    5.87                      break;
    5.88                  case opc_lshr:
    5.89 -                    smapper.replace(this, VarType.LONG, "(@1).shr64(@2)", smapper.getL(1), smapper.popI());
    5.90 +                    smapper.replace(this, VarType.LONG, numbers.shr64(), smapper.getL(1), smapper.popI());
    5.91                      break;
    5.92                  case opc_iushr:
    5.93                      smapper.replace(this, VarType.INTEGER, "(@1 >>> @2)", smapper.getI(1), smapper.popI());
    5.94                      break;
    5.95                  case opc_lushr:
    5.96 -                    smapper.replace(this, VarType.LONG, "(@1).ushr64(@2)", smapper.getL(1), smapper.popI());
    5.97 +                    smapper.replace(this, VarType.LONG, numbers.ushr64(), smapper.getL(1), smapper.popI());
    5.98                      break;
    5.99                  case opc_iinc: {
   5.100                      ++i;
   5.101 @@ -1003,7 +1003,7 @@
   5.102                      break;
   5.103                  }
   5.104                  case opc_lcmp:
   5.105 -                    smapper.replace(this, VarType.INTEGER, "(@2).compare64(@1)", smapper.popL(), smapper.getL(0));
   5.106 +                    smapper.replace(this, VarType.INTEGER, numbers.compare64(), smapper.popL(), smapper.getL(0));
   5.107                      break;
   5.108                  case opc_fcmpl:
   5.109                  case opc_fcmpg:
     6.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/NumberOperations.java	Tue Jan 26 04:42:06 2016 +0100
     6.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/NumberOperations.java	Tue Jan 26 05:54:37 2016 +0100
     6.3 @@ -38,6 +38,58 @@
     6.4          return "__mod32(@1,@2)";
     6.5      }
     6.6  
     6.7 +    public String add64() {
     6.8 +        return "(@1).add64(@2)";
     6.9 +    }
    6.10 +
    6.11 +    public String sub64() {
    6.12 +        return "(@1).sub64(@2)";
    6.13 +    }
    6.14 +
    6.15 +    public String mul64() {
    6.16 +        return "(@1).mul64(@2)";
    6.17 +    }
    6.18 +
    6.19 +    public String div64() {
    6.20 +        return "(@1).div64(@2)";
    6.21 +    }
    6.22 +
    6.23 +    public String mod64() {
    6.24 +        return "(@1).mod64(@2)";
    6.25 +    }
    6.26 +
    6.27 +    public String and64() {
    6.28 +        return "(@1).and64(@2)";
    6.29 +    }
    6.30 +
    6.31 +    public String or64() {
    6.32 +        return "(@1).or64(@2)";
    6.33 +    }
    6.34 +
    6.35 +    public String xor64() {
    6.36 +        return "(@1).xor64(@2)";
    6.37 +    }
    6.38 +
    6.39 +    public String neg64() {
    6.40 +        return "(@1).neg64()";
    6.41 +    }
    6.42 +
    6.43 +    public String shl64() {
    6.44 +        return "(@1).shl64(@2)";
    6.45 +    }
    6.46 +
    6.47 +    public String shr64() {
    6.48 +        return "(@1).shr64(@2)";
    6.49 +    }
    6.50 +
    6.51 +    public String ushr64() {
    6.52 +        return "(@1).ushr64(@2)";
    6.53 +    }
    6.54 +
    6.55 +    public String compare64() {
    6.56 +        return "(@2).compare64(@1)";
    6.57 +    }
    6.58 +
    6.59      public String generate() {
    6.60          if (used == 0) {
    6.61              return "";