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