2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
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.
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.
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.
18 package org.apidesign.vm4brwsr;
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;
27 public String mul32() {
29 return "__mul32(@1,@2)";
31 public String div32() {
33 return "__div32(@1,@2)";
36 public String mod32() {
38 return "__mod32(@1,@2)";
41 public String generate() {
45 StringBuilder sb = new StringBuilder();
46 if ((used & MUL32) != 0) {
48 " __mul32 = function(x, y) {\n" +
49 " return (((x * (y >> 16)) << 16) + x * (y & 0xFFFF)) | 0;\n" +
54 if ((used & (MOD32 | DIV32)) != 0) {
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" +
61 " throw exception;\n" +
66 if ((used & MOD32) != 0) {
68 " function __mod32(x, y) {\n" +
69 " if (y === 0) __handleDivByZero();\n" +
70 " return (x % y) | 0;\n" +
75 if ((used & DIV32) != 0) {
77 " function __div32(x, y) {\n" +
78 " if (y === 0) __handleDivByZero();\n" +
79 " return (x / y) | 0;\n" +