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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This release is compatible with NumPy 2.4.5.
* Fixed incorrect `dpnp.tensor.acosh` result for `complex(±0, NaN)` special case to match the Python Array API specification [#2914](https://github.com/IntelPython/dpnp/pull/2914)
* Fixed fork PR documentation workflow failures by implementing conditional publishing strategy: upstream PRs publish to GitHub Pages with comment, fork PRs upload artifacts [#2910](https://github.com/IntelPython/dpnp/pull/2910)
* Fixed missing `libtensor` headers in the installed `dpnp` package [#2915](https://github.com/IntelPython/dpnp/pull/2915)
* Fixed a bug in `astype` where casting floating point types to unsigned integral types could cause an intermediate signed integral type to overflow, leading to incorrect results [#2930](https://github.com/IntelPython/dpnp/pull/2930)

### Security

Expand Down
11 changes: 8 additions & 3 deletions dpnp/tensor/libtensor/include/utils/type_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,14 @@ dstTy convert_impl(const srcTy &v)
else if constexpr (!std::is_integral_v<srcTy> &&
!std::is_same_v<dstTy, bool> &&
std::is_integral_v<dstTy> && std::is_unsigned_v<dstTy>) {
// first cast to signed variant, the cast to unsigned one
using signedT = typename std::make_signed_t<dstTy>;
return static_cast<dstTy>(convert_impl<signedT, srcTy>(v));
// for negative values, cast through signed integer to get two's
// complement wrapping
using intermediateT =
std::conditional_t<sizeof(dstTy) < sizeof(std::int32_t),
std::int32_t, std::int64_t>;
return (v < srcTy{0})
? static_cast<dstTy>(static_cast<intermediateT>(v))
: static_cast<dstTy>(v);
}
else {
return static_cast<dstTy>(v);
Expand Down
9 changes: 9 additions & 0 deletions dpnp/tests/tensor/test_usm_ndarray_ctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,15 @@ def test_astype_gh_2121():
assert dpt.all(res == expected)


def test_astype_gh_2882():
get_queue_or_skip()

x = dpt.asarray([160.0, 120.0], dtype="f4")
r = dpt.astype(x, dpt.uint8)
expected = dpt.asarray([160, 120], dtype="u1")
assert dpt.all(r == expected)


def test_copy():
try:
X = dpt.usm_ndarray((5, 5), "i4")[2:4, 1:4]
Expand Down
Loading