javafloating-pointstrictfp

enforce strictfp behavior in a Java program run


Is there a way to tell the Java VM to enforce the strictfp behavior across all float point operations regardless of whether the code used the strictfp modifier or not?

I have looked for -X option that would do the trick in Oracle's SE Jdk (I restricted to only use 1.8) but there is such an option.

Is there perhaps a VM for MacOS that in compliant with 1.8 and that only implements strictfp behavior regardless of hardware present?


Solution

  • I researched this a long time ago for a different Q&A. What I found was:

    1. If you could change to using the Oracle JRockit JVM, it has a JVM option to enable strictfp semantics globally: -XstrictFP

    2. According to the 2nd reference, there is a -XX:-UseStrictFP option on some Hotspot JVMs. However:

      • It has the reverse effect to what you want; i.e. it disables all strictfp semantics.
      • It is only available in a "debug version of the VM".
      • It will have been removed in Java 17 ... along with all other support for strictfp.

      I was able to confirm the first two points by looking at the OpenJDK Java 8 source code1.

    In summary, there is probably nothing that will help you in a regular Hotspot JVM.

    References:


    1 - While looking, I noticed that the JIT compiler for intel platforms does appear to pay some attention to strictfp. So @rzwitserloot's comment about strictfp being irrelevant on modern hardware might not be correct. However, I don't have the knowledge or patience to research this properly.