rt/vm/src/main/java/org/apidesign/vm4brwsr/NumberOperations.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 25 Jan 2016 08:14:42 +0100
changeset 1858 4dea14fafc31
child 1862 fa00fb053c72
permissions -rw-r--r--
Speeding up sieve by directly resolving all 32-bit number operations
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 final class NumberOperations {
    21     private static final int DIV32 = 1;
    22     private static final int MOD32 = 2;
    23     private static final int MUL32 = 4;
    24 
    25     private int used;
    26 
    27     public String mul32() {
    28         used |= MUL32;
    29         return "__mul32(@1,@2)";
    30     }
    31     public String div32() {
    32         used |= DIV32;
    33         return "__div32(@1,@2)";
    34     }
    35 
    36     public String mod32() {
    37         used |= MOD32;
    38         return "__mod32(@1,@2)";
    39     }
    40 
    41     public String generate() {
    42         if (used == 0) {
    43             return "";
    44         }
    45         StringBuilder sb = new StringBuilder();
    46         if ((used & MUL32) != 0) {
    47             sb.append(
    48                 "    __mul32 = function(x, y) {\n" +
    49                 "        return (((x * (y >> 16)) << 16) + x * (y & 0xFFFF)) | 0;\n" +
    50                 "    };\n" +
    51                 ""
    52             );
    53         }
    54         if ((used & (MOD32 | DIV32)) != 0) {
    55             sb.append(
    56                 "    function __handleDivByZero() {\n" +
    57                 "        var exception = new vm.java_lang_ArithmeticException;\n" +
    58                 "        vm.java_lang_ArithmeticException(false).constructor\n" +
    59                 "          .cons__VLjava_lang_String_2.call(exception, \"/ by zero\");\n" +
    60                 "\n" +
    61                 "        throw exception;\n" +
    62                 "    }\n" +
    63                 ""
    64             );
    65         }
    66         if ((used & MOD32) != 0) {
    67             sb.append(
    68                 "    function __mod32(x, y) {\n" +
    69                 "        if (y === 0) __handleDivByZero();\n" +
    70                 "        return (x % y) | 0;\n" +
    71                 "    }\n" +
    72                 ""
    73             );
    74         }
    75         if ((used & DIV32) != 0) {
    76             sb.append(
    77                 "    function __div32(x, y) {\n" +
    78                 "        if (y === 0) __handleDivByZero();\n" +
    79                 "        return (x / y) | 0;\n" +
    80                 "    }\n" +
    81                 ""
    82             );
    83         }
    84         return sb.toString();
    85     }
    86 }