Skip to content

fp_utils

Floating-point bit manipulation utilities.

This module provides functions for converting floating-point values to and from sign/exponent/mantissa bit representations with configurable exponent and mantissa widths, following the IEEE 754 binary interchange conventions: exponent bias of 2^(nx-1) - 1, subnormals for zero exponents, and all-ones exponents reserved for inf/NaN.

Rounding on encode truncates towards zero (not IEEE round-to-nearest), so values requiring more than nm mantissa bits lose their trailing bits.

exp_bias(nx)

Compute the exponent bias for a given exponent width.

The bias is (2^(nx-1)) - 1, used to offset the exponent in floating-point representation.

Parameters:

Name Type Description Default
nx

Number of exponent bits.

required

Returns:

Type Description

The exponent bias integer.

Example
fp_utils.exp_bias(8)  # 127 (IEEE 754 single precision)
fp_utils.exp_bias(5)  # 15
Source code in py_misc_utils/fp_utils.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def exp_bias(nx):
  """Compute the exponent bias for a given exponent width.

  The bias is (2^(nx-1)) - 1, used to offset the exponent in floating-point
  representation.

  Args:
    nx: Number of exponent bits.

  Returns:
    The exponent bias integer.

  Example:
    fp_utils.exp_bias(8)  # 127 (IEEE 754 single precision)
    fp_utils.exp_bias(5)  # 15
  """
  return (1 << (nx - 1)) - 1

real_to_bits(v, nx, nm)

Convert a real value to sign, exponent, and mantissa bit components.

Parameters:

Name Type Description Default
v

The floating-point value to convert.

required
nx

Number of exponent bits.

required
nm

Number of mantissa bits.

required

Returns:

Type Description

A tuple (sign, exponent, mantissa) where sign is 0 or 1, exponent is an

integer, and mantissa is an integer. Negative zero yields sign 1.

Values below the subnormal range flush to zero. Non-finite inputs raise

ValueError.

Raises:

Type Description
ValueError

If v is not finite.

Example
fp_utils.real_to_bits(1.0, 8, 23)  # (0, 127, 4194304)
fp_utils.real_to_bits(-2.5, 8, 7)  # (1, 128, 80)
fp_utils.real_to_bits(1e-70, 8, 23)  # (0, 0, 0): flushed to zero.
Source code in py_misc_utils/fp_utils.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def real_to_bits(v, nx, nm):
  """Convert a real value to sign, exponent, and mantissa bit components.

  Args:
    v: The floating-point value to convert.
    nx: Number of exponent bits.
    nm: Number of mantissa bits.

  Returns:
    A tuple (sign, exponent, mantissa) where sign is 0 or 1, exponent is an
    integer, and mantissa is an integer. Negative zero yields sign 1.
    Values below the subnormal range flush to zero. Non-finite inputs raise
    ValueError.

  Raises:
    ValueError: If v is not finite.

  Example:
    fp_utils.real_to_bits(1.0, 8, 23)  # (0, 127, 4194304)
    fp_utils.real_to_bits(-2.5, 8, 7)  # (1, 128, 80)
    fp_utils.real_to_bits(1e-70, 8, 23)  # (0, 0, 0): flushed to zero.
  """
  if not math.isfinite(v):
    raise ValueError(f'Cannot encode non-finite value: {v}')

  # Preserving the sign of negative zero: v < 0 is False for -0.0.
  if v == 0.0:
    return 1 if math.copysign(1.0, v) < 0 else 0, 0, 0

  xm, xe = math.frexp(math.fabs(v))
  bias = exp_bias(nx)

  e = xe + bias - 1
  if e <= 0:
    # Subnormal: keep the leading bits of the mantissa with the fixed
    # minimum exponent, or flush to zero below the subnormal range.
    shift = nm + e
    if shift <= 0:
      return 1 if v < 0 else 0, 0, 0

    return 1 if v < 0 else 0, 0, int(xm * (1 << shift))

  m = int(xm * (1 << nm))
  if e >= (1 << nx):
    # Overflow: saturate to infinity with the sign of v.
    return 1 if v < 0 else 0, (1 << nx) - 1, 0

  return 1 if v < 0 else 0, e, m

pack_bits(s, e, m, nx, nm)

Pack sign, exponent, and mantissa into a single integer.

Parameters:

Name Type Description Default
s

Sign bit (0 or 1).

required
e

Exponent value.

required
m

Mantissa value.

required
nx

Number of exponent bits.

required
nm

Number of mantissa bits.

required

Returns:

Type Description

The packed integer representation.

Example
fp_utils.pack_bits(0, 127, 0, 8, 23)  # 2139095040 (float32 +inf bits)
fp_utils.pack_bits(0, 127, 4194304, 8, 23)  # 1069547520 (1.0 bits)
Source code in py_misc_utils/fp_utils.py
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def pack_bits(s, e, m, nx, nm):
  """Pack sign, exponent, and mantissa into a single integer.

  Args:
    s: Sign bit (0 or 1).
    e: Exponent value.
    m: Mantissa value.
    nx: Number of exponent bits.
    nm: Number of mantissa bits.

  Returns:
    The packed integer representation.

  Example:
    fp_utils.pack_bits(0, 127, 0, 8, 23)  # 2139095040 (float32 +inf bits)
    fp_utils.pack_bits(0, 127, 4194304, 8, 23)  # 1069547520 (1.0 bits)
  """
  return (s << (nx + nm)) | (e << nm) | m

real_to_packedbits(v, nx, nm)

Convert a real value to its packed bit representation.

Parameters:

Name Type Description Default
v

The floating-point value to convert.

required
nx

Number of exponent bits.

required
nm

Number of mantissa bits.

required

Returns:

Type Description

The packed integer bit representation.

Example
fp_utils.real_to_packedbits(1.0, 8, 23)  # 1069547520: the float32 bits of 1.0.
Source code in py_misc_utils/fp_utils.py
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
def real_to_packedbits(v, nx, nm):
  """Convert a real value to its packed bit representation.

  Args:
    v: The floating-point value to convert.
    nx: Number of exponent bits.
    nm: Number of mantissa bits.

  Returns:
    The packed integer bit representation.

  Example:
    fp_utils.real_to_packedbits(1.0, 8, 23)  # 1069547520: the float32 bits of 1.0.
  """
  s, e, m = real_to_bits(v, nx, nm)

  return pack_bits(s, e, m, nx, nm)

packedbits_to_real(v, nx, nm)

Convert a packed bit integer back to a real floating-point value.

Parameters:

Name Type Description Default
v

The packed integer bit representation.

required
nx

Number of exponent bits.

required
nm

Number of mantissa bits.

required

Returns:

Type Description

The decoded floating-point value, or math.inf/math.nan for special cases.

Example
fp_utils.packedbits_to_real(1069547520, 8, 23)  # 1.0
fp_utils.packedbits_to_real(fp_utils.real_to_packedbits(3.14, 8, 5), 8, 5)
# 3.125: values are truncated on encode, not rounded.
Source code in py_misc_utils/fp_utils.py
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
def packedbits_to_real(v, nx, nm):
  """Convert a packed bit integer back to a real floating-point value.

  Args:
    v: The packed integer bit representation.
    nx: Number of exponent bits.
    nm: Number of mantissa bits.

  Returns:
    The decoded floating-point value, or math.inf/math.nan for special cases.

  Example:
    fp_utils.packedbits_to_real(1069547520, 8, 23)  # 1.0
    fp_utils.packedbits_to_real(fp_utils.real_to_packedbits(3.14, 8, 5), 8, 5)
    # 3.125: values are truncated on encode, not rounded.
  """
  s, e, m = _bits(v, nx + nm, 1), _bits(v, nm, nx), _bits(v, 0, nm)

  if e == 0 and m == 0:
    return 0.0
  if e == ((1 << nx) - 1):
    if m == 0:
      return math.inf if s == 0 else -math.inf

    return math.nan

  # The mantissa keeps its leading bit in bit nm - 1, so the value is
  # m * 2^(e - bias - nm + 1); for a zero exponent this is the IEEE
  # subnormal formula m * 2^(1 - bias - nm).
  re = e - exp_bias(nx) - nm + 1
  if re >= 0:
    rm = m * (1 << re)
  else:
    rm = m / (1 << (-re))

  return rm if s == 0 else -rm