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.
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 09 Mar 2013 11:58:50 +0100
changeset 826fb751bcc23fd
parent 825 0585605d4913
child 827 309878691222
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.
pom.xml
rt/emul/brwsrtest/pom.xml
rt/emul/brwsrtest/src/test/java/org/apidesign/bck2brwsr/brwsrtest/DoubleBitsTest.java
rt/emul/mini/src/main/java/java/lang/Double.java
rt/emul/mini/src/main/java/java/lang/Float.java
rt/emul/pom.xml
     1.1 --- a/pom.xml	Sat Mar 09 11:57:04 2013 +0100
     1.2 +++ b/pom.xml	Sat Mar 09 11:58:50 2013 +0100
     1.3 @@ -110,6 +110,15 @@
     1.4                      <skip>true</skip>
     1.5                  </configuration>
     1.6                </plugin>
     1.7 +            <plugin>
     1.8 +                <groupId>org.apache.maven.plugins</groupId>
     1.9 +                <artifactId>maven-compiler-plugin</artifactId>
    1.10 +                <version>2.3.2</version>
    1.11 +                <configuration>
    1.12 +                    <source>1.7</source>
    1.13 +                    <target>1.7</target>
    1.14 +                </configuration>
    1.15 +            </plugin>
    1.16            </plugins>
    1.17        </pluginManagement>
    1.18    </build>
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/rt/emul/brwsrtest/pom.xml	Sat Mar 09 11:58:50 2013 +0100
     2.3 @@ -0,0 +1,47 @@
     2.4 +<?xml version="1.0"?>
     2.5 +<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
     2.6 +    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     2.7 +  <modelVersion>4.0.0</modelVersion>
     2.8 +  <parent>
     2.9 +    <groupId>org.apidesign.bck2brwsr</groupId>
    2.10 +    <artifactId>emul.pom</artifactId>
    2.11 +    <version>0.5-SNAPSHOT</version>
    2.12 +  </parent>
    2.13 +  <groupId>org.apidesign.bck2brwsr</groupId>
    2.14 +  <artifactId>brwsrtest</artifactId>
    2.15 +  <version>0.5-SNAPSHOT</version>
    2.16 +  <name>brwsrtest</name>
    2.17 +  <url>http://maven.apache.org</url>
    2.18 +  <properties>
    2.19 +    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    2.20 +  </properties>
    2.21 +  <build>
    2.22 +      <plugins>
    2.23 +          <plugin>
    2.24 +           <groupId>org.apache.maven.plugins</groupId>
    2.25 +           <artifactId>maven-deploy-plugin</artifactId>
    2.26 +           <version>2.7</version>
    2.27 +           <configuration>
    2.28 +             <skip>true</skip>
    2.29 +           </configuration>
    2.30 +         </plugin>      
    2.31 +          <plugin>
    2.32 +              <groupId>org.apache.maven.plugins</groupId>
    2.33 +              <artifactId>maven-surefire-plugin</artifactId>
    2.34 +              <configuration>
    2.35 +                  <systemProperties>
    2.36 +                      <vmtest.js>brwsr</vmtest.js>
    2.37 +                  </systemProperties>
    2.38 +              </configuration>
    2.39 +          </plugin>
    2.40 +      </plugins>
    2.41 +  </build>
    2.42 +  <dependencies>
    2.43 +    <dependency>
    2.44 +      <groupId>${project.groupId}</groupId>
    2.45 +      <artifactId>vmtest</artifactId>
    2.46 +      <version>${project.version}</version>
    2.47 +      <scope>test</scope>
    2.48 +    </dependency>
    2.49 +  </dependencies>
    2.50 +</project>
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/rt/emul/brwsrtest/src/test/java/org/apidesign/bck2brwsr/brwsrtest/DoubleBitsTest.java	Sat Mar 09 11:58:50 2013 +0100
     3.3 @@ -0,0 +1,42 @@
     3.4 +/**
     3.5 + * Back 2 Browser Bytecode Translator
     3.6 + * Copyright (C) 2012 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.bck2brwsr.brwsrtest;
    3.22 +
    3.23 +import org.apidesign.bck2brwsr.vmtest.Compare;
    3.24 +import org.apidesign.bck2brwsr.vmtest.VMTest;
    3.25 +import org.testng.annotations.Factory;
    3.26 +
    3.27 +/**
    3.28 + *
    3.29 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    3.30 + */
    3.31 +public class DoubleBitsTest {
    3.32 +
    3.33 +    @Compare public String doubleToBits() {
    3.34 +        long val = Double.doubleToLongBits(333.456);
    3.35 +        return Long.toString(val);
    3.36 +    }
    3.37 +
    3.38 +    @Compare public int floatToBits() {
    3.39 +        return Float.floatToIntBits(333.456f);
    3.40 +    }
    3.41 +    
    3.42 +    @Factory public static Object[] create() {
    3.43 +        return VMTest.create(DoubleBitsTest.class);
    3.44 +    }
    3.45 +}
     4.1 --- a/rt/emul/mini/src/main/java/java/lang/Double.java	Sat Mar 09 11:57:04 2013 +0100
     4.2 +++ b/rt/emul/mini/src/main/java/java/lang/Double.java	Sat Mar 09 11:58:50 2013 +0100
     4.3 @@ -809,15 +809,17 @@
     4.4       * @return the bits that represent the floating-point number.
     4.5       */
     4.6      public static long doubleToLongBits(double value) {
     4.7 -        throw new UnsupportedOperationException();
     4.8 -//        long result = doubleToRawLongBits(value);
     4.9 -//        // Check for NaN based on values of bit fields, maximum
    4.10 -//        // exponent and nonzero significand.
    4.11 -//        if ( ((result & DoubleConsts.EXP_BIT_MASK) ==
    4.12 -//              DoubleConsts.EXP_BIT_MASK) &&
    4.13 -//             (result & DoubleConsts.SIGNIF_BIT_MASK) != 0L)
    4.14 -//            result = 0x7ff8000000000000L;
    4.15 -//        return result;
    4.16 +        final long EXP_BIT_MASK = 9218868437227405312L;
    4.17 +        final long SIGNIF_BIT_MASK = 4503599627370495L;
    4.18 +        
    4.19 +        long result = doubleToRawLongBits(value);
    4.20 +        // Check for NaN based on values of bit fields, maximum
    4.21 +        // exponent and nonzero significand.
    4.22 +        if ( ((result & EXP_BIT_MASK) ==
    4.23 +              EXP_BIT_MASK) &&
    4.24 +             (result & SIGNIF_BIT_MASK) != 0L)
    4.25 +            result = 0x7ff8000000000000L;
    4.26 +        return result;
    4.27      }
    4.28  
    4.29      /**
    4.30 @@ -856,7 +858,21 @@
    4.31       * @return the bits that represent the floating-point number.
    4.32       * @since 1.3
    4.33       */
    4.34 -    public static native long doubleToRawLongBits(double value);
    4.35 +    public static long doubleToRawLongBits(double value) {
    4.36 +        int[] arr = { 0, 0 };
    4.37 +        doubleToRawLongBits(value, arr);
    4.38 +        long l = arr[1];
    4.39 +        return (l << 32) | arr[0];
    4.40 +    }
    4.41 +    
    4.42 +    @JavaScriptBody(args = { "value", "arr" }, body = ""
    4.43 +        + "var a = new ArrayBuffer(8);"
    4.44 +        + "new Float64Array(a)[0] = value;"
    4.45 +        + "var out = new Int32Array(a);"
    4.46 +        + "arr[0] = out[0];"
    4.47 +        + "arr[1] = out[1];"
    4.48 +    )
    4.49 +    private static native void doubleToRawLongBits(double value, int[] arr);
    4.50  
    4.51      /**
    4.52       * Returns the {@code double} value corresponding to a given
     5.1 --- a/rt/emul/mini/src/main/java/java/lang/Float.java	Sat Mar 09 11:57:04 2013 +0100
     5.2 +++ b/rt/emul/mini/src/main/java/java/lang/Float.java	Sat Mar 09 11:58:50 2013 +0100
     5.3 @@ -710,15 +710,17 @@
     5.4       * @return the bits that represent the floating-point number.
     5.5       */
     5.6      public static int floatToIntBits(float value) {
     5.7 -        throw new UnsupportedOperationException();
     5.8 -//        int result = floatToRawIntBits(value);
     5.9 -//        // Check for NaN based on values of bit fields, maximum
    5.10 -//        // exponent and nonzero significand.
    5.11 -//        if ( ((result & FloatConsts.EXP_BIT_MASK) ==
    5.12 -//              FloatConsts.EXP_BIT_MASK) &&
    5.13 -//             (result & FloatConsts.SIGNIF_BIT_MASK) != 0)
    5.14 -//            result = 0x7fc00000;
    5.15 -//        return result;
    5.16 +        final int EXP_BIT_MASK = 2139095040;
    5.17 +        final int SIGNIF_BIT_MASK = 8388607;
    5.18 +        
    5.19 +        int result = floatToRawIntBits(value);
    5.20 +        // Check for NaN based on values of bit fields, maximum
    5.21 +        // exponent and nonzero significand.
    5.22 +        if ( ((result & EXP_BIT_MASK) ==
    5.23 +              EXP_BIT_MASK) &&
    5.24 +             (result & SIGNIF_BIT_MASK) != 0)
    5.25 +            result = 0x7fc00000;
    5.26 +        return result;
    5.27      }
    5.28  
    5.29      /**
    5.30 @@ -756,6 +758,11 @@
    5.31       * @return the bits that represent the floating-point number.
    5.32       * @since 1.3
    5.33       */
    5.34 +    @JavaScriptBody(args = { "value" }, body = ""
    5.35 +        + "var a = new ArrayBuffer(4);"
    5.36 +        + "new Float32Array(a)[0] = value;"
    5.37 +        + "return new Int32Array(a)[0];"
    5.38 +    )
    5.39      public static native int floatToRawIntBits(float value);
    5.40  
    5.41      /**
     6.1 --- a/rt/emul/pom.xml	Sat Mar 09 11:57:04 2013 +0100
     6.2 +++ b/rt/emul/pom.xml	Sat Mar 09 11:58:50 2013 +0100
     6.3 @@ -14,5 +14,6 @@
     6.4    <modules>
     6.5      <module>mini</module>
     6.6      <module>compact</module>
     6.7 +    <module>brwsrtest</module>
     6.8    </modules>
     6.9  </project>