# HG changeset patch # User rfield # Date 1403554472 25200 # Node ID eb284abd64fe95033b4c0e4b08fabd24aee7f979 # Parent 37c7dbe8efee974be8beade740de7f737cacdb15 8046060: Different results of floating point multiplication for lambda code block Summary: propogate strictfp into lambda body Reviewed-by: vromero, jlahoda diff -r 37c7dbe8efee -r eb284abd64fe src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java --- a/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java Fri Jun 20 10:56:31 2014 -0600 +++ b/src/share/classes/com/sun/tools/javac/comp/LambdaToMethod.java Mon Jun 23 13:14:32 2014 -0700 @@ -1994,7 +1994,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); diff -r 37c7dbe8efee -r eb284abd64fe test/tools/javac/lambda/LambdaTestStrictFP.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/lambda/LambdaTestStrictFP.java Mon Jun 23 13:14:32 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(); + } +} diff -r 37c7dbe8efee -r eb284abd64fe test/tools/javac/lambda/LambdaTestStrictFPFlag.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/lambda/LambdaTestStrictFPFlag.java Mon Jun 23 13:14:32 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(); + } +} diff -r 37c7dbe8efee -r eb284abd64fe test/tools/javac/lambda/LambdaTestStrictFPMethod.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/tools/javac/lambda/LambdaTestStrictFPMethod.java Mon Jun 23 13:14:32 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(); + } +}