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 | |
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 | |
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 | |
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 | |
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 | |