numpy  2.0.0
include/numpy/halffloat.h File Reference
#include <Python.h>
#include <numpy/npy_math.h>

Go to the source code of this file.

Defines

#define NPY_HALF_ZERO   (0x0000u)
#define NPY_HALF_PZERO   (0x0000u)
#define NPY_HALF_NZERO   (0x8000u)
#define NPY_HALF_ONE   (0x3c00u)
#define NPY_HALF_NEGONE   (0xbc00u)
#define NPY_HALF_PINF   (0x7c00u)
#define NPY_HALF_NINF   (0xfc00u)
#define NPY_HALF_NAN   (0x7e00u)
#define NPY_MAX_HALF   (0x7bffu)

Functions

float npy_half_to_float (npy_half h)
double npy_half_to_double (npy_half h)
npy_half npy_float_to_half (float f)
npy_half npy_double_to_half (double d)
int npy_half_eq (npy_half h1, npy_half h2)
int npy_half_ne (npy_half h1, npy_half h2)
int npy_half_le (npy_half h1, npy_half h2)
int npy_half_lt (npy_half h1, npy_half h2)
int npy_half_ge (npy_half h1, npy_half h2)
int npy_half_gt (npy_half h1, npy_half h2)
int npy_half_eq_nonan (npy_half h1, npy_half h2)
int npy_half_lt_nonan (npy_half h1, npy_half h2)
int npy_half_le_nonan (npy_half h1, npy_half h2)
int npy_half_iszero (npy_half h)
int npy_half_isnan (npy_half h)
int npy_half_isinf (npy_half h)
int npy_half_isfinite (npy_half h)
int npy_half_signbit (npy_half h)
npy_half npy_half_copysign (npy_half x, npy_half y)
npy_half npy_half_spacing (npy_half h)
npy_half npy_half_nextafter (npy_half x, npy_half y)
npy_half npy_half_divmod (npy_half x, npy_half y, npy_half *modulus)
npy_uint16 npy_floatbits_to_halfbits (npy_uint32 f)
npy_uint16 npy_doublebits_to_halfbits (npy_uint64 d)
npy_uint32 npy_halfbits_to_floatbits (npy_uint16 h)
npy_uint64 npy_halfbits_to_doublebits (npy_uint16 h)

Define Documentation

#define NPY_HALF_NAN   (0x7e00u)

Referenced by npy_half_spacing().

#define NPY_HALF_NEGONE   (0xbc00u)
#define NPY_HALF_NINF   (0xfc00u)
#define NPY_HALF_NZERO   (0x8000u)
#define NPY_HALF_ONE   (0x3c00u)
#define NPY_HALF_PINF   (0x7c00u)

Referenced by npy_half_spacing().

#define NPY_HALF_PZERO   (0x0000u)
#define NPY_HALF_ZERO   (0x0000u)
Half-precision constants
#define NPY_MAX_HALF   (0x7bffu)

Function Documentation

npy_uint16 npy_doublebits_to_halfbits ( npy_uint64  d)
Exponent overflow/NaN converts to signed inf/NaN
Inf or NaN
NaN - propagate the flag in the significand...
...but make sure it stays a NaN
signed inf
overflow to signed inf
Exponent underflow converts to subnormal half or signed zero
Signed zeros, subnormal floats, and floats with small exponents all convert to signed zero halfs.
If d != 0, it underflowed to 0
Make the subnormal significand
If it's not exactly represented, it underflowed
Handle rounding by adding 1 to the bit beyond half precision
If the last bit in the half significand is 0 (already even), and the remaining bit pattern is 1000...0, then we do not add one to the bit after the half significand. In all other cases, we do.
If the rounding causes a bit to spill into h_exp, it will increment h_exp from zero to one and h_sig will be zero. This is the correct result.
Regular case with no overflow or underflow
Handle rounding by adding 1 to the bit beyond half precision
If the last bit in the half significand is 0 (already even), and the remaining bit pattern is 1000...0, then we do not add one to the bit after the half significand. In all other cases, we do.
If the rounding causes a bit to spill into h_exp, it will increment h_exp by one and h_sig will be zero. This is the correct result. h_exp may increment to 15, at greatest, in which case the result overflows to a signed inf.

Referenced by npy_double_to_half().

npy_uint16 npy_floatbits_to_halfbits ( npy_uint32  f)
Bit-level conversions

BIT-LEVEL CONVERSIONS *

Exponent overflow/NaN converts to signed inf/NaN
Inf or NaN
NaN - propagate the flag in the significand...
...but make sure it stays a NaN
signed inf
overflow to signed inf
Exponent underflow converts to a subnormal half or signed zero
Signed zeros, subnormal floats, and floats with small exponents all convert to signed zero halfs.
If f != 0, it underflowed to 0
Make the subnormal significand
If it's not exactly represented, it underflowed
Handle rounding by adding 1 to the bit beyond half precision
If the last bit in the half significand is 0 (already even), and the remaining bit pattern is 1000...0, then we do not add one to the bit after the half significand. In all other cases, we do.
If the rounding causes a bit to spill into h_exp, it will increment h_exp from zero to one and h_sig will be zero. This is the correct result.
Regular case with no overflow or underflow
Handle rounding by adding 1 to the bit beyond half precision
If the last bit in the half significand is 0 (already even), and the remaining bit pattern is 1000...0, then we do not add one to the bit after the half significand. In all other cases, we do.
If the rounding causes a bit to spill into h_exp, it will increment h_exp by one and h_sig will be zero. This is the correct result. h_exp may increment to 15, at greatest, in which case the result overflows to a signed inf.

References npy_set_floatstatus_underflow().

Referenced by npy_float_to_half().

npy_half npy_half_divmod ( npy_half  x,
npy_half  y,
npy_half modulus 
)
int npy_half_eq ( npy_half  h1,
npy_half  h2 
)
Comparisons
The equality cases are as follows:
  • If either value is NaN, never equal.
  • If the values are equal, equal.
  • If the values are both signed zeros, equal.
int npy_half_eq_nonan ( npy_half  h1,
npy_half  h2 
)
faster *_nonan variants for when you know h1 and h2 are not NaN

System Message: WARNING/2 (<string>, line 1); backlink Inline emphasis start-string without end-string.
int npy_half_ge ( npy_half  h1,
npy_half  h2 
)
int npy_half_gt ( npy_half  h1,
npy_half  h2 
)

Referenced by HALF_LT().

Miscellaneous functions
int npy_half_le ( npy_half  h1,
npy_half  h2 
)
int npy_half_le_nonan ( npy_half  h1,
npy_half  h2 
)
Signed zeros are equal, have to check for it
int npy_half_lt ( npy_half  h1,
npy_half  h2 
)
int npy_half_lt_nonan ( npy_half  h1,
npy_half  h2 
)
Signed zeros are equal, have to check for it
int npy_half_ne ( npy_half  h1,
npy_half  h2 
)

<

Smallest subnormal half

<

x > 0

<

x > y

<

x < y

<

Negative boundary case

<

If result is normalized

<

The result is a subnormal, but not the smallest

<

Smallest subnormal half

<

If result is still normalized

<

The result is a subnormal, but not the smallest

References NPY_HALF_NAN, NPY_HALF_PINF, npy_set_floatstatus_invalid(), and npy_set_floatstatus_overflow().

Half-precision routines
Conversions

HALF-PRECISION ROUTINES *

References npy_halfbits_to_floatbits().

Referenced by _floor_divide(), PyUFunc_f_f(), and TIMEDELTA_setitem().

npy_uint64 npy_halfbits_to_doublebits ( npy_uint16  h)

<

0 or subnormal
Signed zero
Subnormal

<

inf or NaN
All-ones exponent and a copy of the significand

<

normalized
Just need to adjust the exponent and shift

Referenced by npy_half_to_double().

npy_uint32 npy_halfbits_to_floatbits ( npy_uint16  h)

<

0 or subnormal
Signed zero
Subnormal

<

inf or NaN
All-ones exponent and a copy of the significand

<

normalized
Just need to adjust the exponent and shift

Referenced by npy_half_to_float().