Strictfp

Last updated

strictfp is an obsolete and redundant reserved word in the Java programming language. [1] [2] Previously, this keyword was used as a modifier that restricted floating-point calculations to IEEE 754 semantics to ensure portability. The strictfp keyword was introduced into Java with the Java virtual machine (JVM) version 1.2 and its functionality was removed in JVM version 17. [2] As of Java 17, IEEE 754 semantics is required, thus using this keyword has no effect.

Contents

Basis

The IEEE standard IEEE 754 specifies a standard method for both floating-point calculations and storage of floating-point values in various formats, including single (32-bit, used in Java's float) or double (64-bit, used in Java's double) precision.

Some hardware also provides extended precision formats that provide higher precision and/or a larger exponent range. On such architectures, it may be more efficient to compute intermediate results using such extended formats. This may avoid round-off errors, overflows and underflows that would otherwise occur, but can cause programs to produce different output on such architectures. It was particularly expensive to avoid the use of extended precision on x86 machines with the traditional x87 floating-point architecture. Although it was easy to control calculation precision, limiting the exponent range for intermediate results required additional costly instructions.

Before JVM 1.2, floating-point calculations were required to be strict; that is, all intermediate floating-point results were required to behave as if represented using IEEE single or double precisions. This made it expensive on common x87-based hardware to ensure that overflows would occur where required.

Starting with JVM 1.2, intermediate computations were, by default, allowed to exceed the standard exponent ranges associated with IEEE 32-bit and 64 bit formats. They were permitted to instead be represented as a member of the "extended-exponent" value set. On platforms like x87, overflows and underflows might not occur where expected, producing possibly more meaningful, but less repeatable, results instead.

Since x87 floating point is no longer necessary on x86 processors supporting SSE2, Java 17 again made all floating-point operations strict, effectively restoring the pre-1.2 semantics. [2]

How it works

In the absence of overflow or underflow, there is no difference in results with or without strictfp. If repeatability is essential, the strictfp modifier can be used to ensure that overflow and underflow occur in the same places on all platforms. Without the strictfp modifier, intermediate results may use a larger exponent range. [3]

The strictfp modifier accomplishes this by representing all intermediate values as IEEE single precision and double precision values, as occurred in earlier versions of the JVM. [4]

Usage

Programmers can use the modifier strictfp to ensure that calculations are performed as in the earlier versions; that is, only with IEEE single and double precision types used. Using strictfp guarantees that the results of floating-point calculations are identical on all platforms.

It can be used on classes, interfaces, and non-abstract methods. [5] When applied to a method, it causes all calculations inside the method to use strict floating-point math. When applied to a class, all calculations inside the class use strict floating-point math. Compile-time constant expressions must always use strict floating-point behavior. [6]

Examples

publicstrictfpclassMyFPclass{// ... contents of class here ...}

Related Research Articles

<span class="mw-page-title-main">Floating-point arithmetic</span> Computer approximation for real numbers

In computing, floating-point arithmetic (FP) is arithmetic that represents subsets of real numbers using an integer with a fixed precision, called the significand, scaled by an integer exponent of a fixed base. Numbers of this form are called floating-point numbers. For example, 12.345 is a floating-point number in base ten with five digits of precision:

IEEE 754-1985 is a historic industry standard for representing floating-point numbers in computers, officially adopted in 1985 and superseded in 2008 by IEEE 754-2008, and then again in 2019 by minor revision IEEE 754-2019. During its 23 years, it was the most widely used format for floating-point computation. It was implemented in software, in the form of floating-point libraries, and in hardware, in the instructions of many CPUs and FPUs. The first integrated circuit to implement the draft of what was to become IEEE 754-1985 was the Intel 8087.

In computing, NaN, standing for Not a Number, is a particular value of a numeric data type which is undefined as a number, such as the result of 0/0. Systematic use of NaNs was introduced by the IEEE 754 floating-point standard in 1985, along with the representation of other non-finite quantities such as infinities.

Double-precision floating-point format is a floating-point number format, usually occupying 64 bits in computer memory; it represents a wide range of numeric values by using a floating radix point.

In computer science, subnormal numbers are the subset of denormalized numbers that fill the underflow gap around zero in floating-point arithmetic. Any non-zero number with magnitude smaller than the smallest positive normal number is subnormal, while denormal can also refer to numbers outside that range.

SSE2 is one of the Intel SIMD processor supplementary instruction sets introduced by Intel with the initial version of the Pentium 4 in 2000. SSE2 instructions allow the use of XMM (SIMD) registers on x86 instruction set architecture processors. These registers can load up to 128 bits of data and perform instructions, such as vector addition and multiplication, simultaneously.

The IEEE Standard for Floating-Point Arithmetic is a technical standard for floating-point arithmetic originally established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard addressed many problems found in the diverse floating-point implementations that made them difficult to use reliably and portably. Many hardware floating-point units use the IEEE 754 standard.

Hexadecimal floating point is a format for encoding floating-point numbers first introduced on the IBM System/360 computers, and supported on subsequent machines based on that architecture, as well as machines which were intended to be application-compatible with System/360.

<span class="mw-page-title-main">C99</span> C programming language standard, 1999 revision

C99 is a past version of the C programming language open standard. It extends the previous version (C90) with new features for the language and the standard library, and helps implementations make better use of available computer hardware, such as IEEE 754-1985 floating-point arithmetic, and compiler technology. The C11 version of the C programming language standard, published in 2011, updates C99.

The term arithmetic underflow is a condition in a computer program where the result of a calculation is a number of more precise absolute value than the computer can actually represent in memory on its central processing unit (CPU).

x87 is a floating-point-related subset of the x86 architecture instruction set. It originated as an extension of the 8086 instruction set in the form of optional floating-point coprocessors that work in tandem with corresponding x86 CPUs. These microchips have names ending in "87". This is also known as the NPX. Like other extensions to the basic instruction set, x87 instructions are not strictly needed to construct working programs, but provide hardware and microcode implementations of common numerical tasks, allowing these tasks to be performed much faster than corresponding machine code routines can. The x87 instruction set includes instructions for basic floating-point operations such as addition, subtraction and comparison, but also for more complex numerical operations, such as the computation of the tangent function and its inverse, for example.

In C and related programming languages, long double refers to a floating-point data type that is often more precise than double precision though the language standard only requires it to be at least as precise as double. As with C's other floating-point types, it may not necessarily map to an IEEE format.

In computing, minifloats are floating-point values represented with very few bits. This reduced precision makes them ill-suited for general-purpose numerical calculations, but they are useful for special purposes such as:

Extended precision refers to floating-point number formats that provide greater precision than the basic floating-point formats. Extended precision formats support a basic format by minimizing roundoff and overflow errors in intermediate values of expressions on the base format. In contrast to extended precision, arbitrary-precision arithmetic refers to implementations of much larger numeric types using special software.

IEEE 754-2008 is a revision of the IEEE 754 standard for floating-point arithmetic. It was published in August 2008 and is a significant revision to, and replaces, the IEEE 754-1985 standard. The 2008 revision extended the previous standard where it was necessary, added decimal arithmetic and formats, tightened up certain areas of the original standard which were left undefined, and merged in IEEE 854 . In a few cases, where stricter definitions of binary floating-point arithmetic might be performance-incompatible with some existing implementation, they were made optional. In 2019, it was updated with a minor revision IEEE 754-2019.

In computing, half precision is a binary floating-point computer number format that occupies 16 bits in computer memory. It is intended for storage of floating-point values in applications where higher precision is not essential, in particular image processing and neural networks.

In computing, quadruple precision is a binary floating-point–based computer number format that occupies 16 bytes with precision at least twice the 53-bit double precision.

Single-precision floating-point format is a computer number format, usually occupying 32 bits in computer memory; it represents a wide dynamic range of numeric values by using a floating radix point.

In computing, Microsoft Binary Format (MBF) is a format for floating-point numbers which was used in Microsoft's BASIC languages, including MBASIC, GW-BASIC and QuickBASIC prior to version 4.00.

The bfloat16 floating-point format is a computer number format occupying 16 bits in computer memory; it represents a wide dynamic range of numeric values by using a floating radix point. This format is a shortened (16-bit) version of the 32-bit IEEE 754 single-precision floating-point format (binary32) with the intent of accelerating machine learning and near-sensor computing. It preserves the approximate dynamic range of 32-bit floating-point numbers by retaining 8 exponent bits, but supports only an 8-bit precision rather than the 24-bit significand of the binary32 format. More so than single-precision 32-bit floating-point numbers, bfloat16 numbers are unsuitable for integer calculations, but this is not their intended use. Bfloat16 is used to reduce the storage requirements and increase the calculation speed of machine learning algorithms.

References

  1. "Java Language Specification - Chapter 3. Lexical Structure".
  2. 1 2 3 "JEP 306: Restore Always-Strict Floating-Point Semantics".
  3. Gosling, James; Joy, Bill; Steele, Guy L. Jr.; Bracha, Gilad; Buckley, Alex; Smith, Daniel (2017). "4.2.3 Floating-Point Types, Formats, and Values". The Java Language Specification, Java SE 9 Edition. Addison-Wesley Professional. Retrieved 2017-10-06.
  4. Flanagan, David (March 2005). Java in a Nutshell (Fifth ed.). O'Reilly Media. ISBN   978-0-596-00773-7 . Retrieved 2010-03-03.
  5. Schildt, Herbert (2007). Java: A Beginner's Guide (4 ed.). McGraw-Hill Companies. ISBN   978-0-07-226384-8.
  6. Gosling, James; Joy, Bill; Steele, Guy L. Jr.; Bracha, Gilad (2005). "15.4 FP-strict Expressions". The Java Language Specification, Third Edition. Addison-Wesley Professional. p. 411. ISBN   0-321-24678-0 . Retrieved 2016-03-22.