numpy  2.0.0
src/npymath/halffloat.c File Reference
#include "numpy/halffloat.h"

Defines

#define NPY_NO_DEPRECATED_API   NPY_API_VERSION
#define NPY_HALF_ROUND_TIES_TO_EVEN   1
#define NPY_HALF_GENERATE_OVERFLOW   1
#define NPY_HALF_GENERATE_UNDERFLOW   1
#define NPY_HALF_GENERATE_INVALID   1

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_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_spacing (npy_half h)
npy_half npy_half_copysign (npy_half x, npy_half y)
npy_half npy_half_nextafter (npy_half x, npy_half y)
int npy_half_eq_nonan (npy_half h1, npy_half h2)
int npy_half_eq (npy_half h1, npy_half h2)
int npy_half_ne (npy_half h1, npy_half h2)
int npy_half_lt_nonan (npy_half h1, npy_half h2)
int npy_half_lt (npy_half h1, npy_half h2)
int npy_half_gt (npy_half h1, npy_half h2)
int npy_half_le_nonan (npy_half h1, npy_half h2)
int npy_half_le (npy_half h1, npy_half h2)
int npy_half_ge (npy_half h1, npy_half h2)
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_GENERATE_INVALID   1
If these are 1, the conversions try to trigger underflow, overflow, and invalid exceptions in the FP system when needed.
This chooses between 'ties to even' and 'ties away from zero'.
#define NPY_NO_DEPRECATED_API   NPY_API_VERSION

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 *

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().

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 *

References npy_halfbits_to_floatbits().

Referenced by _copysign(), _nextafter(), PyUFunc_e_e(), TIMEDELTA_setitem(), and UNICODE_compare().

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().