Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The format is inspired by [Keep a Changelog](https://keepachangelog.com/en/1.1.0
- `utils.wrap` now wraps complex arrays component-wise instead of discarding the imaginary part with a `ComplexWarning` (issue `#108`).
- Fixed `utils.int_array` converting raw values in `[2**63, 2**64)` to `float` when mixed with smaller values (NumPy dtype inference), which lost precision in extended precision and unsigned 64-bit raw storage, saturation, and wrapping (issue `#115`).
- Fixed overflow/underflow status flags missing values just beyond limits for word sizes over 53 bits (bounds not representable in `float64`).
- Fixed `Fxp.__lshift__`/`__ilshift__` raising `ValueError` (ambiguous truth value) for array shift amounts: the result word size is now derived from the largest shift amount, so `a << shifts` shifts element-wise instead of crashing (issue `#120`).

### Tests
- Expanded issue `#73` regression coverage with explicit cross-platform paths in `tests/test_issues.py`: default behavior, emulated `n_word_max=32`, and emulated `n_word_max=64` boundary conditions.
Expand All @@ -55,6 +56,7 @@ The format is inspired by [Keep a Changelog](https://keepachangelog.com/en/1.1.0
- Added issue `#115` regression coverage for exact integer raw storage in mixed magnitude arrays (`int_array`, set_val, saturation, wrap).
- Added saturation guardrail test checking exact max/min raw values at word boundaries (including word sizes whose limits are not representable in `float64`), for upcoming saturation optimizations.
- Restricted default pytest collection to `tests/` (`testpaths`), so benchmarks only run explicitly.
- Added issue `#120` regression coverage for left shift with array shift amounts: element-wise values, signed arrays, result word sizing from the largest shift, the in-place form, and unchanged scalar behavior.
- Removed `tests/test_performace.py` (print-only timing checks run on every test suite), superseded by the benchmark suite.
- Added saturation regression tests for exact overflow flags at word boundaries, NaN/inf handling, and scalar, int, uint, raw and extended inputs.

Expand Down
5 changes: 4 additions & 1 deletion fxpmath/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1890,7 +1890,10 @@ def __lshift__(self, n):
Fxp
New fixed-point object containing the operation result."""
if self.config.shifting == 'expand':
n_word = max(self.n_word, int(np.max(np.ceil(np.log2(np.abs(self.val)+0.5)))) + self.signed + n)
# `n` may be an array of per-element shifts; `n_word` describes the
# whole object, so size the result for the largest shift amount.
n_max = int(np.max(n))
n_word = max(self.n_word, int(np.max(np.ceil(np.log2(np.abs(self.val)+0.5)))) + self.signed + n_max)
new_value = self.val << np.array(n, dtype=self.val.dtype)
elif self.config.shifting == 'keep':
n_word = self.n_word
Expand Down
26 changes: 26 additions & 0 deletions tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,3 +773,29 @@ def test_issue_115_v0_5_0():
# wrap path
w = Fxp(np.array([2**64 + 5, 1], dtype=object), signed=False, n_word=64, n_frac=0, raw=True, overflow='wrap')
assert w.val.tolist() == [5, 1]


def test_issue_120_v0_5_0():
"""Regression for issue #120: an array of shift amounts must shift element-wise instead of raising."""
a = Fxp(np.array([1, 2, 3]), signed=False, n_word=8, n_frac=0, overflow='wrap')
shifts = np.array([1, 2, 3])

b = a << shifts
assert np.all(b() == [2, 8, 24])
assert b.dtype == a.dtype

# in-place form uses the same path
c = a.deepcopy()
c <<= shifts
assert np.all(c() == [2, 8, 24])

# signed values are shifted element-wise too
s = Fxp(np.array([-1, 2, 3]), signed=True, n_word=8, n_frac=0)
assert np.all((s << shifts)() == [-2, 8, 24])

# the largest shift amount sizes the result word
w = Fxp(np.array([1]), signed=False, n_word=4, n_frac=0)
assert (w << np.array([9])).n_word == 10

# scalar shift amounts keep working as before
assert np.all((a << 2)() == [4, 8, 12])