forked from openjdk/jdk
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
8295159: DSO created with -ffast-math breaks Java floating-point arit…
…hmetic Reviewed-by: ihse, dholmes, stuefe
- Loading branch information
Andrew Haley
committed
Nov 4, 2023
1 parent
c099cf5
commit df599db
Showing
10 changed files
with
277 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
test/hotspot/jtreg/compiler/floatingpoint/TestSubnormalDouble.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) 2023, Red Hat, Inc. 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 8295159 | ||
* @summary DSO created with -ffast-math breaks Java floating-point arithmetic | ||
* @run main/othervm/native compiler.floatingpoint.TestSubnormalDouble | ||
*/ | ||
|
||
package compiler.floatingpoint; | ||
|
||
import static java.lang.System.loadLibrary; | ||
|
||
public class TestSubnormalDouble { | ||
static volatile double lastDouble; | ||
|
||
private static void testDoubles() { | ||
lastDouble = 0x1.0p-1074; | ||
for (double x = lastDouble * 2; x <= 0x1.0p1022; x *= 2) { | ||
if (x != x || x <= lastDouble) { | ||
throw new RuntimeException("TEST FAILED: " + x); | ||
} | ||
lastDouble = x; | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
testDoubles(); | ||
System.out.println("Loading libfast-math.so"); | ||
loadLibrary("fast-math"); | ||
testDoubles(); | ||
System.out.println("Test passed."); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
test/hotspot/jtreg/compiler/floatingpoint/TestSubnormalFloat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Copyright (c) 2023, Red Hat, Inc. 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 8295159 | ||
* @summary DSO created with -ffast-math breaks Java floating-point arithmetic | ||
* @run main/othervm/native compiler.floatingpoint.TestSubnormalFloat | ||
*/ | ||
|
||
package compiler.floatingpoint; | ||
|
||
import static java.lang.System.loadLibrary; | ||
|
||
public class TestSubnormalFloat { | ||
static volatile float lastFloat; | ||
|
||
private static void testFloats() { | ||
lastFloat = 0x1.0p-149f; | ||
for (float x = lastFloat * 2; x <= 0x1.0p127f; x *= 2) { | ||
if (x != x || x <= lastFloat) { | ||
throw new RuntimeException("TEST FAILED: " + x); | ||
} | ||
lastFloat = x; | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
testFloats(); | ||
System.out.println("Loading libfast-math.so"); | ||
loadLibrary("fast-math"); | ||
testFloats(); | ||
System.out.println("Test passed."); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright (c) 2023, Red Hat, Inc. 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. | ||
*/ | ||
|
||
#include "jni.h" | ||
|
||
// See GCC bug 55522: | ||
// | ||
// "When used at link-time, [ GCC with -ffast-math ] may include | ||
// libraries or startup files that change the default FPU control word | ||
// or other similar optimizations." | ||
// | ||
// This breaks Java's floating point arithmetic. | ||
|
||
#if defined(__GNUC__) | ||
|
||
// On systems on which GCC bug 55522 has been fixed, this constructor | ||
// serves to reproduce that bug for the purposes of testing HotSpot. | ||
static void __attribute__((constructor)) set_flush_to_zero(void) { | ||
|
||
#if defined(__x86_64__) | ||
|
||
#define MXCSR_DAZ (1 << 6) /* Enable denormals are zero mode */ | ||
#define MXCSR_FTZ (1 << 15) /* Enable flush to zero mode */ | ||
unsigned int mxcsr = __builtin_ia32_stmxcsr (); | ||
mxcsr |= MXCSR_DAZ | MXCSR_FTZ; | ||
__builtin_ia32_ldmxcsr (mxcsr); | ||
|
||
#elif defined(__aarch64__) | ||
|
||
#define _FPU_FPCR_FZ (unsigned long)0x1000000 | ||
#define _FPU_SETCW(fpcr) \ | ||
__asm__ __volatile__ ("msr fpcr, %0" : : "r" (fpcr)); | ||
|
||
/* Flush to zero, round to nearest, IEEE exceptions disabled. */ | ||
_FPU_SETCW (_FPU_FPCR_FZ); | ||
|
||
#endif // CPU arch | ||
|
||
} | ||
#endif // defined(__GNUC__) |