8046060: Different results of floating point multiplication for lambda code block
authorrfield
Fri, 20 Jun 2014 10:56:47 -0700
changeset 25278 8a326edaf411
parent 25277 993f29159350
child 25279 a665d5cbb8c3
8046060: Different results of floating point multiplication for lambda code block Summary: propogate strictfp into lambda body Reviewed-by: vromero, jlahoda
langtools/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java
langtools/test/tools/javac/lambda/LambdaTestStrictFP.java
langtools/test/tools/javac/lambda/LambdaTestStrictFPFlag.java
langtools/test/tools/javac/lambda/LambdaTestStrictFPMethod.java
--- a/langtools/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Fri Jun 20 11:42:16 2014 -0600
+++ b/langtools/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java	Fri Jun 20 10:56:47 2014 -0700
@@ -1992,7 +1992,11 @@
                 // If instance access isn't needed, make it static.
                 // Interface instance methods must be default methods.
                 // Lambda methods are private synthetic.
+                // Inherit ACC_STRICT from the enclosing method, or, for clinit,
+                // from the class.
                 translatedSym.flags_field = SYNTHETIC | LAMBDA_METHOD |
+                        owner.flags_field & STRICTFP |
+                        owner.owner.flags_field & STRICTFP |
                         PRIVATE |
                         (thisReferenced? (inInterface? DEFAULT : 0) : STATIC);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/LambdaTestStrictFP.java	Fri Jun 20 10:56:47 2014 -0700
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code 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
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8046060
+ * @summary Different results of floating point multiplication for lambda code block
+ */
+
+strictfp
+public class LambdaTestStrictFP {
+
+    static double fld =  eval(() -> {
+            double x = Double.longBitsToDouble(0x1e7ee00000000000L);
+            double y = Double.longBitsToDouble(0x2180101010101010L);
+
+            return x * y;
+        });
+
+    public static void main(String args[]) {
+        double result = eval(() -> {
+            double x = Double.longBitsToDouble(0x1e7ee00000000000L);
+            double y = Double.longBitsToDouble(0x2180101010101010L);
+
+            return x * y;
+        });
+        {
+            double x = Double.longBitsToDouble(0x1e7ee00000000000L);
+            double y = Double.longBitsToDouble(0x2180101010101010L);
+
+            double z = x * y;
+            check(z, result, "method");
+            check(z, fld, "field");
+        }
+    }
+
+    private static void check(double expected, double got, String where) {
+        if (got != expected) {
+            throw new AssertionError(where + ": Non-strictfp " + got + " != " + expected);
+        }
+    }
+
+    private static double eval(Face arg) {
+        return arg.m();
+    }
+
+    private interface Face {
+        double m();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/LambdaTestStrictFPFlag.java	Fri Jun 20 10:56:47 2014 -0700
@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code 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
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8046060
+ * @summary Different results of floating point multiplication for lambda code block
+ */
+
+import java.io.*;
+import java.net.URL;
+import com.sun.tools.classfile.*;
+import static com.sun.tools.classfile.AccessFlags.ACC_STRICT;
+
+public class LambdaTestStrictFPFlag {
+    public static void main(String[] args) throws Exception {
+        new LambdaTestStrictFPFlag().run();
+    }
+
+    void run() throws Exception {
+        ClassFile cf = getClassFile("LambdaTestStrictFPFlag$Test.class");
+        ConstantPool cp = cf.constant_pool;
+        boolean found = false;
+        for (Method meth: cf.methods) {
+            if (meth.getName(cp).startsWith("lambda$")) {
+                if ((meth.access_flags.flags & ACC_STRICT) == 0) {
+                    throw new Exception("strict flag missing from lambda");
+                }
+                found = true;
+            }
+        }
+        if (!found) {
+            throw new Exception("did not find lambda method");
+        }
+    }
+
+    ClassFile getClassFile(String name) throws IOException, ConstantPoolException {
+        URL url = getClass().getResource(name);
+        InputStream in = url.openStream();
+        try {
+            return ClassFile.read(in);
+        } finally {
+            in.close();
+        }
+    }
+
+    class Test {
+        strictfp void test() {
+            Face itf = () -> { };
+        }
+    }
+
+    interface Face {
+        void m();
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/lambda/LambdaTestStrictFPMethod.java	Fri Jun 20 10:56:47 2014 -0700
@@ -0,0 +1,65 @@
+/*
+ * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code 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
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8046060
+ * @summary Different results of floating point multiplication for lambda code block
+ */
+
+public class LambdaTestStrictFPMethod {
+
+    public static void main(String args[]) {
+        new LambdaTestStrictFPMethod().test();
+    }
+
+    strictfp void test() {
+        double result = eval(() -> {
+            double x = Double.longBitsToDouble(0x1e7ee00000000000L);
+            double y = Double.longBitsToDouble(0x2180101010101010L);
+
+            return x * y;
+        });
+        {
+            double x = Double.longBitsToDouble(0x1e7ee00000000000L);
+            double y = Double.longBitsToDouble(0x2180101010101010L);
+
+            double z = x * y;
+            check(z, result, "method");
+        }
+    }
+
+    strictfp void check(double expected, double got, String where) {
+        if (got != expected) {
+            throw new AssertionError(where + ": Non-strictfp " + got + " != " + expected);
+        }
+    }
+
+    static double eval(Face arg) {
+        return arg.m();
+    }
+
+    interface Face {
+        double m();
+    }
+}