Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SYCL] Split sycl::clamp implementation in two #12637

Merged
merged 1 commit into from
Feb 7, 2024
Merged
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
25 changes: 4 additions & 21 deletions sycl/include/sycl/detail/builtins/common_functions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -72,32 +72,15 @@ min(T x, detail::get_elem_type_t<T> y) {
detail::simplify_if_swizzle_t<T>{y});
}

#undef BUILTIN_COMMON

#ifdef __SYCL_DEVICE_ONLY__
DEVICE_IMPL_TEMPLATE(THREE_ARGS, clamp, builtin_enable_generic_t,
[](auto... xs) {
using ElemTy = detail::get_elem_type_t<T0>;
if constexpr (std::is_integral_v<ElemTy>) {
if constexpr (std::is_signed_v<ElemTy>) {
return __spirv_ocl_s_clamp(xs...);
} else {
return __spirv_ocl_u_clamp(xs...);
}
} else {
return __spirv_ocl_fclamp(xs...);
}
})
#else
HOST_IMPL_TEMPLATE(THREE_ARGS, clamp, builtin_enable_generic_t, common,
default_ret_type)
#endif
BUILTIN_COMMON(THREE_ARGS, clamp, __spirv_ocl_fclamp)
template <typename T>
detail::builtin_enable_generic_non_scalar_t<T>
detail::builtin_enable_common_non_scalar_t<T>
clamp(T x, detail::get_elem_type_t<T> y, detail::get_elem_type_t<T> z) {
return clamp(detail::simplify_if_swizzle_t<T>{x},
detail::simplify_if_swizzle_t<T>{y},
detail::simplify_if_swizzle_t<T>{z});
}

#undef BUILTIN_COMMON
} // namespace _V1
} // namespace sycl
9 changes: 9 additions & 0 deletions sycl/include/sycl/detail/builtins/integer_functions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ min(T x, detail::get_elem_type_t<T> y) {
detail::simplify_if_swizzle_t<T>{y});
}

BUILTIN_GENINT_SU(THREE_ARGS, clamp)
template <typename T>
detail::builtin_enable_integer_non_scalar_t<T>
clamp(T x, detail::get_elem_type_t<T> y, detail::get_elem_type_t<T> z) {
return clamp(detail::simplify_if_swizzle_t<T>{x},
detail::simplify_if_swizzle_t<T>{y},
detail::simplify_if_swizzle_t<T>{z});
}

BUILTIN_GENINT(ONE_ARG, clz)
BUILTIN_GENINT(ONE_ARG, ctz)
BUILTIN_GENINT(ONE_ARG, popcount)
Expand Down
12 changes: 2 additions & 10 deletions sycl/source/builtins/common_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,8 @@ BUILTIN_COMMON(TWO_ARGS, max,
BUILTIN_COMMON(TWO_ARGS, min,
[](auto x, auto y) -> decltype(x) { return (y < x ? y : x); })

// clamp is implemented for INTEGER_TYPES as well, so expand/inline
// BUILTIN_COMMON manually.
HOST_IMPL(clamp, [](auto x, auto y, auto z) -> decltype(x) {
using ElemTy = detail::get_elem_type_t<decltype(x)>;
if constexpr (std::is_integral_v<ElemTy>) {
return std::min(std::max(x, y), z);
} else {
return std::fmin(std::fmax(x, y), z);
}
BUILTIN_COMMON(THREE_ARGS, clamp, [](auto x, auto y, auto z) -> decltype(x) {
return std::fmin(std::fmax(x, y), z);
})
EXPORT_SCALAR_AND_VEC_1_16(THREE_ARGS, clamp, INTEGER_TYPES, FP_TYPES)
} // namespace _V1
} // namespace sycl
4 changes: 4 additions & 0 deletions sycl/source/builtins/integer_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ BUILTIN_GENINT_SU(TWO_ARGS, max,
BUILTIN_GENINT_SU(TWO_ARGS, min,
[](auto x, auto y) -> decltype(x) { return y < x ? y : x; })

BUILTIN_GENINT_SU(THREE_ARGS, clamp, [](auto x, auto y, auto z) -> decltype(x) {
return std::min(std::max(x, y), z);
})

template <typename T> static inline constexpr T __clz_impl(T x, T m, T n = 0) {
return (x & m) ? n : __clz_impl(x, T(m >> 1), ++n);
}
Expand Down
Loading