# HG changeset patch # User Jaroslav Tulach # Date 1362826730 -3600 # Node ID fb751bcc23fd3a6173cbaa6c82d3472bacae774a # Parent 0585605d4913c3ac99f03bc0e5d354014f3b0016 doubleToLongBits and floatToIntBits implemented via typed arrays. Requires real browser (Rhino does not support typed arrays) and as such creating new module to hold the tests and execute them in 'brwsr' mode. diff -r 0585605d4913 -r fb751bcc23fd pom.xml --- a/pom.xml Sat Mar 09 11:57:04 2013 +0100 +++ b/pom.xml Sat Mar 09 11:58:50 2013 +0100 @@ -110,6 +110,15 @@ true + + org.apache.maven.plugins + maven-compiler-plugin + 2.3.2 + + 1.7 + 1.7 + + diff -r 0585605d4913 -r fb751bcc23fd rt/emul/brwsrtest/pom.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/emul/brwsrtest/pom.xml Sat Mar 09 11:58:50 2013 +0100 @@ -0,0 +1,47 @@ + + + 4.0.0 + + org.apidesign.bck2brwsr + emul.pom + 0.5-SNAPSHOT + + org.apidesign.bck2brwsr + brwsrtest + 0.5-SNAPSHOT + brwsrtest + http://maven.apache.org + + UTF-8 + + + + + org.apache.maven.plugins + maven-deploy-plugin + 2.7 + + true + + + + org.apache.maven.plugins + maven-surefire-plugin + + + brwsr + + + + + + + + ${project.groupId} + vmtest + ${project.version} + test + + + diff -r 0585605d4913 -r fb751bcc23fd rt/emul/brwsrtest/src/test/java/org/apidesign/bck2brwsr/brwsrtest/DoubleBitsTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/rt/emul/brwsrtest/src/test/java/org/apidesign/bck2brwsr/brwsrtest/DoubleBitsTest.java Sat Mar 09 11:58:50 2013 +0100 @@ -0,0 +1,42 @@ +/** + * Back 2 Browser Bytecode Translator + * Copyright (C) 2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.brwsrtest; + +import org.apidesign.bck2brwsr.vmtest.Compare; +import org.apidesign.bck2brwsr.vmtest.VMTest; +import org.testng.annotations.Factory; + +/** + * + * @author Jaroslav Tulach + */ +public class DoubleBitsTest { + + @Compare public String doubleToBits() { + long val = Double.doubleToLongBits(333.456); + return Long.toString(val); + } + + @Compare public int floatToBits() { + return Float.floatToIntBits(333.456f); + } + + @Factory public static Object[] create() { + return VMTest.create(DoubleBitsTest.class); + } +} diff -r 0585605d4913 -r fb751bcc23fd rt/emul/mini/src/main/java/java/lang/Double.java --- a/rt/emul/mini/src/main/java/java/lang/Double.java Sat Mar 09 11:57:04 2013 +0100 +++ b/rt/emul/mini/src/main/java/java/lang/Double.java Sat Mar 09 11:58:50 2013 +0100 @@ -809,15 +809,17 @@ * @return the bits that represent the floating-point number. */ public static long doubleToLongBits(double value) { - throw new UnsupportedOperationException(); -// long result = doubleToRawLongBits(value); -// // Check for NaN based on values of bit fields, maximum -// // exponent and nonzero significand. -// if ( ((result & DoubleConsts.EXP_BIT_MASK) == -// DoubleConsts.EXP_BIT_MASK) && -// (result & DoubleConsts.SIGNIF_BIT_MASK) != 0L) -// result = 0x7ff8000000000000L; -// return result; + final long EXP_BIT_MASK = 9218868437227405312L; + final long SIGNIF_BIT_MASK = 4503599627370495L; + + long result = doubleToRawLongBits(value); + // Check for NaN based on values of bit fields, maximum + // exponent and nonzero significand. + if ( ((result & EXP_BIT_MASK) == + EXP_BIT_MASK) && + (result & SIGNIF_BIT_MASK) != 0L) + result = 0x7ff8000000000000L; + return result; } /** @@ -856,7 +858,21 @@ * @return the bits that represent the floating-point number. * @since 1.3 */ - public static native long doubleToRawLongBits(double value); + public static long doubleToRawLongBits(double value) { + int[] arr = { 0, 0 }; + doubleToRawLongBits(value, arr); + long l = arr[1]; + return (l << 32) | arr[0]; + } + + @JavaScriptBody(args = { "value", "arr" }, body = "" + + "var a = new ArrayBuffer(8);" + + "new Float64Array(a)[0] = value;" + + "var out = new Int32Array(a);" + + "arr[0] = out[0];" + + "arr[1] = out[1];" + ) + private static native void doubleToRawLongBits(double value, int[] arr); /** * Returns the {@code double} value corresponding to a given diff -r 0585605d4913 -r fb751bcc23fd rt/emul/mini/src/main/java/java/lang/Float.java --- a/rt/emul/mini/src/main/java/java/lang/Float.java Sat Mar 09 11:57:04 2013 +0100 +++ b/rt/emul/mini/src/main/java/java/lang/Float.java Sat Mar 09 11:58:50 2013 +0100 @@ -710,15 +710,17 @@ * @return the bits that represent the floating-point number. */ public static int floatToIntBits(float value) { - throw new UnsupportedOperationException(); -// int result = floatToRawIntBits(value); -// // Check for NaN based on values of bit fields, maximum -// // exponent and nonzero significand. -// if ( ((result & FloatConsts.EXP_BIT_MASK) == -// FloatConsts.EXP_BIT_MASK) && -// (result & FloatConsts.SIGNIF_BIT_MASK) != 0) -// result = 0x7fc00000; -// return result; + final int EXP_BIT_MASK = 2139095040; + final int SIGNIF_BIT_MASK = 8388607; + + int result = floatToRawIntBits(value); + // Check for NaN based on values of bit fields, maximum + // exponent and nonzero significand. + if ( ((result & EXP_BIT_MASK) == + EXP_BIT_MASK) && + (result & SIGNIF_BIT_MASK) != 0) + result = 0x7fc00000; + return result; } /** @@ -756,6 +758,11 @@ * @return the bits that represent the floating-point number. * @since 1.3 */ + @JavaScriptBody(args = { "value" }, body = "" + + "var a = new ArrayBuffer(4);" + + "new Float32Array(a)[0] = value;" + + "return new Int32Array(a)[0];" + ) public static native int floatToRawIntBits(float value); /** diff -r 0585605d4913 -r fb751bcc23fd rt/emul/pom.xml --- a/rt/emul/pom.xml Sat Mar 09 11:57:04 2013 +0100 +++ b/rt/emul/pom.xml Sat Mar 09 11:58:50 2013 +0100 @@ -14,5 +14,6 @@ mini compact + brwsrtest