Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
4232bcd
Avoid branching in casting implementations
zacharyvincze Jan 28, 2026
77cabc7
Add more tests for Saturate cast
zacharyvincze Feb 3, 2026
d887102
Fix issues with float -> integer saturate casts
zacharyvincze Feb 6, 2026
146a1f9
Undo changes to composite
zacharyvincze Feb 6, 2026
e9e9f0b
Review fixes
zacharyvincze Feb 6, 2026
13a78be
Add another test case for RangeCast
zacharyvincze Feb 6, 2026
f1b1571
Merge branch 'develop' into zv/optimization/optimize-casting-performance
zacharyvincze Feb 11, 2026
146c95f
Merge branch 'develop' into zv/optimization/optimize-casting-performance
zacharyvincze Feb 12, 2026
1cbedda
Merge branch 'develop' into zv/optimization/optimize-casting-performance
zacharyvincze Feb 20, 2026
f97231c
Merge branch 'develop' into zv/optimization/optimize-casting-performance
zacharyvincze Feb 23, 2026
e712805
Merge branch 'develop' into zv/optimization/optimize-casting-performance
zacharyvincze Feb 25, 2026
9ae56a5
Merge branch 'develop' into zv/optimization/optimize-casting-performance
zacharyvincze Mar 2, 2026
12d355b
Merge branch 'develop' into zv/optimization/optimize-casting-performance
zacharyvincze Mar 6, 2026
a883238
Merge branch 'develop' into zv/optimization/optimize-casting-performance
zacharyvincze Mar 6, 2026
2fcaf2f
Merge branch 'develop' into zv/optimization/optimize-casting-performance
zacharyvincze Mar 16, 2026
a88cf40
Merge branch 'develop' into zv/optimization/optimize-casting-performance
zacharyvincze Apr 7, 2026
6be0319
Merge branch 'develop' into zv/optimization/optimize-casting-performance
zacharyvincze Apr 7, 2026
70a9ef5
Merge branch 'develop' into zv/optimization/optimize-casting-performance
zacharyvincze Apr 8, 2026
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
103 changes: 70 additions & 33 deletions include/core/detail/casting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,63 @@ __device__ __host__ T ScalarSaturateCast(U v) {
constexpr bool bigToSmall = !smallToBig;

if constexpr (std::is_integral_v<T> && std::is_floating_point_v<U>) {
// Any float -> any integral
return static_cast<T>(std::clamp<U>(std::round(v), static_cast<U>(std::numeric_limits<T>::min()),
static_cast<U>(std::numeric_limits<T>::max())));
} else if constexpr (std::is_integral_v<T> && std::is_integral_v<U> && std::is_signed_v<U> &&
std::is_unsigned_v<T> && smallToBig) {
// Any integral signed -> Any integral unsigned, small -> big or equal
return v <= 0 ? 0 : static_cast<T>(v);
} else if constexpr (std::is_integral_v<U> && std::is_integral_v<T> &&
((std::is_signed_v<U> && std::is_signed_v<T>) ||
(std::is_unsigned_v<U> && std::is_unsigned_v<T>)) &&
bigToSmall) {
// Any integral signed -> Any integral signed, big -> small
// Any integral unsigned -> Any integral unsigned, big -> small
return v <= std::numeric_limits<T>::min()
? std::numeric_limits<T>::min()
: (v >= std::numeric_limits<T>::max() ? std::numeric_limits<T>::max() : static_cast<T>(v));
} else if constexpr (std::is_integral_v<U> && std::is_unsigned_v<U> && std::is_integral_v<T> &&
std::is_signed_v<T>) {
// Any integral unsigned -> Any integral signed, small -> big or equal
return v >= std::numeric_limits<T>::max() ? std::numeric_limits<T>::max() : static_cast<T>(v);
} else if constexpr (std::is_integral_v<U> && std::is_signed_v<U> && std::is_integral_v<T> &&
std::is_unsigned_v<T> && bigToSmall) {
// Any integral signed -> Any integral unsigned, big -> small
return v <= static_cast<U>(std::numeric_limits<T>::min())
? std::numeric_limits<T>::min()
: (v >= static_cast<U>(std::numeric_limits<T>::max()) ? std::numeric_limits<T>::max()
: static_cast<T>(v));
} else {
// All other cases fall into this
return v;
// Float -> integral: clamp to [min, max] then round.
constexpr U minVal = static_cast<U>(std::numeric_limits<T>::lowest());
constexpr U maxVal = static_cast<U>(std::numeric_limits<T>::max());

if constexpr (sizeof(T) <= 2) {
// 8/16 bit integer cases. These can be represented exactly in floating point.
#ifdef __HIP_DEVICE_COMPILE__
return static_cast<T>(rintf(fminf(fmaxf(v, minVal), maxVal)));
#else
return static_cast<T>(std::round(std::clamp(v, minVal, maxVal)));
#endif
} else {
// 32/64 bit integer cases.
#ifdef __HIP_DEVICE_COMPILE__
U rounded = rintf(v);
#else
U rounded = std::round(v);
#endif
Comment on lines +43 to +56
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The device-side float->integer paths use float-specific intrinsics (rintf, fminf/fmaxf, __saturatef, __float2int_rn) even though U is any floating-point type. If U is double, this will downcast to float and can change rounding/saturation behavior. Consider either constraining these branches to U == float (static_assert / if constexpr) or adding double-correct implementations (rint, fmin/fmax, __double2int_rn, etc.).

Copilot uses AI. Check for mistakes.

return rounded >= maxVal ? std::numeric_limits<T>::max()
: rounded <= minVal ? std::numeric_limits<T>::min()
: static_cast<T>(rounded);
}
}

else if constexpr (std::is_integral_v<T> && std::is_integral_v<U> && std::is_signed_v<U> && std::is_unsigned_v<T> &&
smallToBig) {
// Signed -> unsigned, small to big: clamp negative to 0
// Branchless: max(v, 0) handles negative values
return static_cast<T>(max(v, U{0}));
}

else if constexpr (std::is_integral_v<U> && std::is_integral_v<T> &&
((std::is_signed_v<U> && std::is_signed_v<T>) ||
(std::is_unsigned_v<U> && std::is_unsigned_v<T>)) &&
bigToSmall) {
// Same signedness, big -> small: clamp to [min, max]
constexpr U minVal = static_cast<U>(std::numeric_limits<T>::min());
constexpr U maxVal = static_cast<U>(std::numeric_limits<T>::max());
return static_cast<T>(min(max(v, minVal), maxVal));
}

else if constexpr (std::is_integral_v<U> && std::is_unsigned_v<U> && std::is_integral_v<T> && std::is_signed_v<T>) {
// Unsigned -> signed: clamp to max (can't exceed min since unsigned)
constexpr U maxVal = static_cast<U>(std::numeric_limits<T>::max());
return static_cast<T>(min(v, maxVal));
}

else if constexpr (std::is_integral_v<U> && std::is_signed_v<U> && std::is_integral_v<T> && std::is_unsigned_v<T> &&
bigToSmall) {
// Signed -> unsigned, big -> small: clamp to [0, max]
constexpr U maxVal = static_cast<U>(std::numeric_limits<T>::max());
return static_cast<T>(min(max(v, U{0}), maxVal));
}

else {
return static_cast<T>(v);
}
}

Expand Down Expand Up @@ -117,9 +144,19 @@ __device__ __host__ T ScalarRangeCast(U v) {

else if constexpr (std::is_integral_v<T> && std::is_floating_point_v<U> && std::is_unsigned_v<T>) {
// float to unsigned integers
return v >= T{1} ? std::numeric_limits<T>::max()
: v <= T{0} ? 0
: static_cast<T>(lrintf(static_cast<U>(std::numeric_limits<T>::max()) * v));
constexpr U scale = static_cast<U>(std::numeric_limits<T>::max());

if constexpr (sizeof(T) <= 2) {
// 8/16 bit integer cases. These can be represented exactly in floating point.
#ifdef __HIP_DEVICE_COMPILE__
return static_cast<T>(__float2int_rn(__saturatef(v) * scale));
#else
return static_cast<T>(lrintf(fminf(fmaxf(v, 0.0f), 1.0f) * scale));
#endif
} else {
// 32/64 bit integer cases.
return v >= U{1} ? std::numeric_limits<T>::max() : v <= U{0} ? 0 : static_cast<T>(std::round(v * scale));
}
}

else if constexpr (std::is_floating_point_v<T> && std::is_integral_v<U> && std::is_signed_v<U>) {
Expand Down
3 changes: 3 additions & 0 deletions include/core/detail/type_traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

#include <hip/hip_runtime.h>

#include <cassert>

#pragma once
Expand Down Expand Up @@ -83,6 +84,8 @@ DEFINE_TYPE_TRAITS_0_TO_4(int, signed int);
DEFINE_TYPE_TRAITS_0_TO_4(short, signed short);
DEFINE_TYPE_TRAITS_0_TO_4(ushort, unsigned short);
DEFINE_TYPE_TRAITS_0_TO_4(double, double);
DEFINE_TYPE_TRAITS_0_TO_4(long, signed long);
DEFINE_TYPE_TRAITS_0_TO_4(ulong, unsigned long);

/**
* @brief Returns the number of elements in a HIP vectorized type. For example: uchar3 will return 3, int2 will
Expand Down
2 changes: 2 additions & 0 deletions tests/roccv/cpp/src/tests/core/detail/test_range_cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ int main(int argc, char **argv) {
TEST_CASE(EXPECT_EQ(RangeCast<int>(-1.0f), std::numeric_limits<int>::min()));
TEST_CASE(EXPECT_EQ(RangeCast<uint>(1.0f), std::numeric_limits<uint>::max()));
TEST_CASE(EXPECT_EQ(RangeCast<uint>(-1.0f), 0));
TEST_CASE(EXPECT_EQ(RangeCast<uint>(0.0f), 0));


// Test unsigned/signed integer -> float casting
TEST_CASE(EXPECT_EQ(RangeCast<float>(std::numeric_limits<int>::max()), 1.0f));
Expand Down
54 changes: 54 additions & 0 deletions tests/roccv/cpp/src/tests/core/detail/test_saturate_cast.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2026 Advanced Micro Devices, Inc. All rights reserved.
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include <core/detail/casting.hpp>

#include "test_helpers.hpp"

using namespace roccv::detail;
using namespace roccv::tests;
using namespace roccv;

int main(int argc, char **argv) {
TEST_CASES_BEGIN();

TEST_CASE(EXPECT_EQ(SaturateCast<int>(1.0f), 1));
TEST_CASE(EXPECT_EQ(SaturateCast<int>(-1.0f), -1));
TEST_CASE(EXPECT_EQ(SaturateCast<uint>(1.0f), 1));
TEST_CASE(EXPECT_EQ(SaturateCast<uint>(-1.0f), 0));
TEST_CASE(EXPECT_EQ(SaturateCast<float>(1), 1.0f));
TEST_CASE(EXPECT_EQ(SaturateCast<float>(-1), -1.0f));
TEST_CASE(EXPECT_EQ(SaturateCast<double>(1), 1.0));
TEST_CASE(EXPECT_EQ(SaturateCast<double>(-1), -1.0));

// Test numeric limits
TEST_CASE(EXPECT_EQ(SaturateCast<int>(std::numeric_limits<float>::max()), std::numeric_limits<int>::max()));
TEST_CASE(EXPECT_EQ(SaturateCast<uint8_t>(std::numeric_limits<float>::max()), std::numeric_limits<uint8_t>::max()));
TEST_CASE(EXPECT_EQ(SaturateCast<long>(std::numeric_limits<float>::max()), std::numeric_limits<long>::max()));
TEST_CASE(EXPECT_EQ(SaturateCast<ulong>(std::numeric_limits<float>::lowest()), 0UL));

// Test vectorized types
TEST_CASE(EXPECT_TRUE((SaturateCast<float4>(uchar4{255, 128, 0, 255}) == float4{255.0f, 128.0f, 0.0f, 255.0f})));
TEST_CASE(EXPECT_TRUE(
(SaturateCast<float4>(char4{-128, -128, -128, -128}) == float4{-128.0f, -128.0f, -128.0f, -128.0f})));

TEST_CASES_END();
}