# Half-precision floating-point format

Last updated

In computing, half precision (sometimes called FP16) is a binary floating-point computer number format that occupies 16 bits (two bytes in modern computers) in computer memory.

## Contents

They can express values in the range ±65,504, with the minimum value above 1 being 1 + 1/1024.

In the IEEE 754-2008 standard, the 16-bit base-2 format is referred to as binary16. It is intended for storage of floating-point values in applications where higher precision is not essential for performing arithmetic computations.

Although implementations of the IEEE half-precision floating point are relatively new, several earlier 16-bit floating point formats have existed including that of Hitachi's HD61810 DSP [1] of 1982, Scott's WIF [2] and the 3dfx Voodoo Graphics processor. [3]

Nvidia and Microsoft defined the half datatype in the Cg language, released in early 2002, and implemented it in silicon in the GeForce FX, released in late 2002. [4] ILM was searching for an image format that could handle a wide dynamic range, but without the hard drive and memory cost of floating-point representations that are commonly used for floating-point computation (single and double precision). [5] The hardware-accelerated programmable shading group led by John Airey at SGI (Silicon Graphics) invented the s10e5 data type in 1997 as part of the 'bali' design effort. This is described in a SIGGRAPH 2000 paper [6] (see section 4.3) and further documented in US patent 7518615. [7]

This format is used in several computer graphics environments including MATLAB, OpenEXR, JPEG XR, GIMP, OpenGL, Cg, Direct3D, and D3DX. The advantage over 8-bit or 16-bit binary integers is that the increased dynamic range allows for more detail to be preserved in highlights and shadows for images. The advantage over 32-bit single-precision binary formats is that it requires half the storage and bandwidth (at the expense of precision and range). [5]

The F16C extension allows x86 processors to convert half-precision floats to and from single-precision floats.

Depending on the computer, half-precision can be over an order of magnitude faster than double precision, e.g. 37 PFLOPS vs. for half 550 "AI-PFLOPS (Half Precision)". [8]

## IEEE 754 half-precision binary floating-point format: binary16

The IEEE 754 standard specifies a binary16 as having the following format:

The format is laid out as follows:

The format is assumed to have an implicit lead bit with value 1 unless the exponent field is stored with all zeros. Thus only 10 bits of the significand appear in the memory format but the total precision is 11 bits. In IEEE 754 parlance, there are 10 bits of significand, but there are 11 bits of significand precision (log10(211) ≈ 3.311 decimal digits, or 4 digits ± slightly less than 5 units in the last place).

### Exponent encoding

The half-precision binary floating-point exponent is encoded using an offset-binary representation, with the zero offset being 15; also known as exponent bias in the IEEE 754 standard.

• Emin = 000012 − 011112 = −14
• Emax = 111102 − 011112 = 15
• Exponent bias = 011112 = 15

Thus, as defined by the offset binary representation, in order to get the true exponent the offset of 15 has to be subtracted from the stored exponent.

The stored exponents 000002 and 111112 are interpreted specially.

ExponentSignificand = zeroSignificand ≠ zeroEquation
000002 zero, −0 subnormal numbers (−1)signbit × 2−14 × 0.significantbits2
000012, ..., 111102normalized value(−1)signbit × 2exponent−15 × 1.significantbits2
111112±infinity NaN (quiet, signalling)

The minimum strictly positive (subnormal) value is 2−24 ≈ 5.96 × 10−8. The minimum positive normal value is 2−14 ≈ 6.10 × 10−5. The maximum representable value is (2−2−10) × 215 = 65504.

### Half precision examples

These examples are given in bit representation of the floating-point value. This includes the sign bit, (biased) exponent, and significand.

0 00000 00000000012 = 000116 = ${\displaystyle 2^{-14}\times (0+{\frac {1}{1024}})}$ ≈ 0.000000059604645                               (smallest positive subnormal number)
0 00000 11111111112 = 03ff16 = ${\displaystyle 2^{-14}\times (0+{\frac {1023}{1024}})}$ ≈ 0.000060975552                               (largest subnormal number)
0 00001 00000000002 = 040016 = ${\displaystyle 2^{-14}\times (1+{\frac {0}{1024}})}$ ≈ 0.00006103515625                               (smallest positive normal number)
0 11110 11111111112 = 7bff16 = ${\displaystyle 2^{15}\times (1+{\frac {1023}{1024}})}$ = 65504                               (largest normal number)
0 01110 11111111112 = 3bff16 = ${\displaystyle 2^{-1}\times (1+{\frac {1023}{1024}})}$ ≈ 0.99951172                               (largest number less than one)
0 01111 00000000002 = 3c0016 = ${\displaystyle 2^{0}\times (1+{\frac {0}{1024}})}$ = 1                               (one)
0 01111 00000000012 = 3c0116 = ${\displaystyle 2^{0}\times (1+{\frac {1}{1024}})}$ ≈ 1.00097656                               (smallest number larger than one)
0 01101 01010101012 = 355516 = ${\displaystyle 2^{-2}\times (1+{\frac {341}{1024}})}$ = 0.33325195                               (the rounding of 1/3 to nearest)
1 10000 00000000002 = c00016 = −2
0 00000 00000000002 = 000016 = 0 1 00000 00000000002 = 800016 = −0
0 11111 00000000002 = 7c0016 = infinity 1 11111 00000000002 = fc0016 = −infinity

By default, 1/3 rounds down like for double precision, because of the odd number of bits in the significand. The bits beyond the rounding point are 0101... which is less than 1/2 of a unit in the last place.

### Precision limitations on decimal values in [0, 1]

• Decimals between 2−24 (minimum positive subnormal) and 2−14 (maximum subnormal): fixed interval 2−24
• Decimals between 2−14 (minimum positive normal) and 2−13: fixed interval 2−24
• Decimals between 2−13 and 2−12: fixed interval 2−23
• Decimals between 2−12 and 2−11: fixed interval 2−22
• Decimals between 2−11 and 2−10: fixed interval 2−21
• Decimals between 2−10 and 2−9: fixed interval 2−20
• Decimals between 2−9 and 2−8: fixed interval 2−19
• Decimals between 2−8 and 2−7: fixed interval 2−18
• Decimals between 2−7 and 2−6: fixed interval 2−17
• Decimals between 2−6 and 2−5: fixed interval 2−16
• Decimals between 2−5 and 2−4: fixed interval 2−15
• Decimals between 2−4 and 2−3: fixed interval 2−14
• Decimals between 2−3 and 2−2: fixed interval 2−13
• Decimals between 2−2 and 2−1: fixed interval 2−12
• Decimals between 2−1 and 2−0: fixed interval 2−11

### Precision limitations on decimal values in [1, 2048]

• Decimals between 1 and 2: fixed interval 2−10 (1+2−10 is the next largest float after 1)
• Decimals between 2 and 4: fixed interval 2−9
• Decimals between 4 and 8: fixed interval 2−8
• Decimals between 8 and 16: fixed interval 2−7
• Decimals between 16 and 32: fixed interval 2−6
• Decimals between 32 and 64: fixed interval 2−5
• Decimals between 64 and 128: fixed interval 2−4
• Decimals between 128 and 256: fixed interval 2−3
• Decimals between 256 and 512: fixed interval 2−2
• Decimals between 512 and 1024: fixed interval 2−1
• Decimals between 1024 and 2048: fixed interval 20

### Precision limitations on integer values

• Integers between 0 and 2048 can be exactly represented (and also between −2048 and 0)
• Integers between 2048 and 4096 round to a multiple of 2 (even number)
• Integers between 4096 and 8192 round to a multiple of 4
• Integers between 8192 and 16384 round to a multiple of 8
• Integers between 16384 and 32768 round to a multiple of 16
• Integers between 32768 and 65519 round to a multiple of 32
• Integers above 65519 are rounded to "infinity" if using round-to-even, or above 65535 if using round-to-zero, or above 65504 (the largest representable finite value) if using round-to-infinity.

## ARM alternative half-precision

ARM processors support (via a floating point control register bit) an "alternative half-precision" format, which does away with the special case for an exponent value of 31 (111112). [9] It is almost identical to the IEEE format, but there is no encoding for infinity or NaNs; instead, an exponent of 31 encodes normalized numbers in the range 65536 to 131008.

## Uses

Hardware and software for machine learning or neural networks tend to use half precision: such applications usually do a large amount of calculation, but don't require a high level of precision.

On older computers that access 8 or 16 bits at a time (most modern computers access 32 or 64 bits at a time), half precision arithmetic is faster than single precision, and substantially faster than double precision. On systems with instructions that can handle multiple floating point numbers with in one instruction, half-precision often offers a higher average throughput. [10]

## Related Research Articles

In computing, floating-point arithmetic (FP) is arithmetic using formulaic representation of real numbers as an approximation to support a trade-off between range and precision. For this reason, floating-point computation is often found in systems which include very small and very large real numbers, which require fast processing times. A number is, in general, represented approximately to a fixed number of significant digits and scaled using an exponent in some fixed base; the base for the scaling is normally two, ten, or sixteen. A number that can be represented exactly is of the following form:

IEEE 754-1985 was an 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.

A computer number format is the internal representation of numeric values in digital device hardware and software, such as in programmable computers and calculators. Numerical values are stored as groupings of bits, such as bytes and words. The encoding between numerical values and bit patterns is chosen for convenience of the operation of the computer; the encoding used by the computer's instruction set generally requires conversion for external use, such as for printing and display. Different types of processors may have different internal representations of numerical values and different conventions are used for integer and real numbers. Most calculations are carried out with number formats that fit into a processor register, but some software systems allow representation of arbitrarily large numbers using multiple words of memory.

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

The IEEE Standard for Floating-Point Arithmetic is a technical standard for floating-point arithmetic 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.

The significand is part of a number in scientific notation or a floating-point number, consisting of its significant digits. Depending on the interpretation of the exponent, the significand may represent an integer or a fraction.

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.

In computing, minifloats are floating-point values represented with very few bits. Predictably, they are not well suited for general-purpose numerical calculations. They are used for special purposes, most often in computer graphics, where iterations are small and precision has aesthetic effects. Machine learning also uses similar formats like bfloat16. Additionally, they are frequently encountered as a pedagogical tool in computer-science courses to demonstrate the properties and structures of floating-point arithmetic and IEEE 754 numbers.

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.

Decimal floating-point (DFP) arithmetic refers to both a representation and operations on decimal floating-point numbers. Working directly with decimal (base-10) fractions can avoid the rounding errors that otherwise typically occur when converting between decimal fractions and binary (base-2) fractions.

The IEEE 754-2008 standard includes decimal floating-point number formats in which the significand and the exponent can be encoded in two ways, referred to as binary encoding and decimal encoding.

IEEE 754-2008 was published in August 2008 and is a significant revision to, and replaces, the IEEE 754-1985 floating-point standard, while in 2019 it was updated with a minor revision IEEE 754-2019. 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.

Offset binary, also referred to as excess-K, excess-N, excess-e, excess code or biased representation, is a digital coding scheme where all-zero corresponds to the minimal negative value and all-one to the maximal positive value. There is no standard for offset binary, but most often the offset K for an n-bit binary word is K = 2n−1. This has the consequence that the "zero" value is represented by a 1 in the most significant bit and zero in all other bits, and in general the effect is conveniently the same as using two's complement except that the most significant bit is inverted. It also has the consequence that in a logical comparison operation, one gets the same result as with a true form numerical comparison operation, whereas, in two's complement notation a logical comparison will agree with true form numerical comparison operation if and only if the numbers being compared have the same sign. Otherwise the sense of the comparison will be inverted, with all negative values being taken as being larger than all positive values.

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, decimal32 is a decimal floating-point computer numbering format that occupies 4 bytes (32 bits) in computer memory. It is intended for applications where it is necessary to emulate decimal rounding exactly, such as financial and tax computations. Like the binary16 format, it is intended for memory saving storage.

In computing, decimal64 is a decimal floating-point computer numbering format that occupies 8 bytes in computer memory. It is intended for applications where it is necessary to emulate decimal rounding exactly, such as financial and tax computations.

In computing, decimal128 is a decimal floating-point computer numbering format that occupies 16 bytes (128 bits) in computer memory. It is intended for applications where it is necessary to emulate decimal rounding exactly, such as financial and tax computations.

In computing, octuple precision is a binary floating-point-based computer number format that occupies 32 bytes in computer memory. This 256-bit octuple precision is for applications requiring results in higher than quadruple precision. This format is rarely used and very few environments support it.

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 truncated (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. "hitachi :: dataBooks :: HD61810 Digital Signal Processor Users Manual". Archive.org. Retrieved 2017-07-14.
2. Scott, Thomas J. (March 1991). "Mathematics and Computer Science at Odds over Real Numbers". SIGCSE '91 Proceedings of the Twenty-second SIGCSE Technical Symposium on Computer Science Education. 23 (1): 130–139. doi:10.1145/107004.107029. ISBN   0897913779. S2CID   16648394.
3. "/home/usr/bk/glide/docs2.3.1/GLIDEPGM.DOC". Gamers.org. Retrieved 2017-07-14.
4. "vs_2_sw". Cg 3.1 Toolkit Documentation. Nvidia. Retrieved 17 August 2016.
5. "OpenEXR". OpenEXR. Retrieved 2017-07-14.
6. Mark S. Peercy; Marc Olano; John Airey; P. Jeffrey Ungar. "Interactive Multi-Pass Programmable Shading" (PDF). People.csail.mit.edu. Retrieved 2017-07-14.
7. "Patent US7518615 - Display system having floating point rasterization and floating point ... - Google Patents". Google.com. Retrieved 2017-07-14.