Skip to content

Commit b0d92ff

Browse files
[SYCL] Split sycl::clamp implementation in two (#12637)
SYCL2020, revision 8 defines two separate clamp functions - once in integer functions and once in common functions. Follow the same in the implementation so that clamp's handling is uniform with other common/integer functions.
1 parent c320f3b commit b0d92ff

File tree

4 files changed

+19
-31
lines changed

4 files changed

+19
-31
lines changed

sycl/include/sycl/detail/builtins/common_functions.inc

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,32 +72,15 @@ min(T x, detail::get_elem_type_t<T> y) {
7272
detail::simplify_if_swizzle_t<T>{y});
7373
}
7474

75-
#undef BUILTIN_COMMON
76-
77-
#ifdef __SYCL_DEVICE_ONLY__
78-
DEVICE_IMPL_TEMPLATE(THREE_ARGS, clamp, builtin_enable_generic_t,
79-
[](auto... xs) {
80-
using ElemTy = detail::get_elem_type_t<T0>;
81-
if constexpr (std::is_integral_v<ElemTy>) {
82-
if constexpr (std::is_signed_v<ElemTy>) {
83-
return __spirv_ocl_s_clamp(xs...);
84-
} else {
85-
return __spirv_ocl_u_clamp(xs...);
86-
}
87-
} else {
88-
return __spirv_ocl_fclamp(xs...);
89-
}
90-
})
91-
#else
92-
HOST_IMPL_TEMPLATE(THREE_ARGS, clamp, builtin_enable_generic_t, common,
93-
default_ret_type)
94-
#endif
75+
BUILTIN_COMMON(THREE_ARGS, clamp, __spirv_ocl_fclamp)
9576
template <typename T>
96-
detail::builtin_enable_generic_non_scalar_t<T>
77+
detail::builtin_enable_common_non_scalar_t<T>
9778
clamp(T x, detail::get_elem_type_t<T> y, detail::get_elem_type_t<T> z) {
9879
return clamp(detail::simplify_if_swizzle_t<T>{x},
9980
detail::simplify_if_swizzle_t<T>{y},
10081
detail::simplify_if_swizzle_t<T>{z});
10182
}
83+
84+
#undef BUILTIN_COMMON
10285
} // namespace _V1
10386
} // namespace sycl

sycl/include/sycl/detail/builtins/integer_functions.inc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,15 @@ min(T x, detail::get_elem_type_t<T> y) {
122122
detail::simplify_if_swizzle_t<T>{y});
123123
}
124124

125+
BUILTIN_GENINT_SU(THREE_ARGS, clamp)
126+
template <typename T>
127+
detail::builtin_enable_integer_non_scalar_t<T>
128+
clamp(T x, detail::get_elem_type_t<T> y, detail::get_elem_type_t<T> z) {
129+
return clamp(detail::simplify_if_swizzle_t<T>{x},
130+
detail::simplify_if_swizzle_t<T>{y},
131+
detail::simplify_if_swizzle_t<T>{z});
132+
}
133+
125134
BUILTIN_GENINT(ONE_ARG, clz)
126135
BUILTIN_GENINT(ONE_ARG, ctz)
127136
BUILTIN_GENINT(ONE_ARG, popcount)

sycl/source/builtins/common_functions.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,8 @@ BUILTIN_COMMON(TWO_ARGS, max,
6363
BUILTIN_COMMON(TWO_ARGS, min,
6464
[](auto x, auto y) -> decltype(x) { return (y < x ? y : x); })
6565

66-
// clamp is implemented for INTEGER_TYPES as well, so expand/inline
67-
// BUILTIN_COMMON manually.
68-
HOST_IMPL(clamp, [](auto x, auto y, auto z) -> decltype(x) {
69-
using ElemTy = detail::get_elem_type_t<decltype(x)>;
70-
if constexpr (std::is_integral_v<ElemTy>) {
71-
return std::min(std::max(x, y), z);
72-
} else {
73-
return std::fmin(std::fmax(x, y), z);
74-
}
66+
BUILTIN_COMMON(THREE_ARGS, clamp, [](auto x, auto y, auto z) -> decltype(x) {
67+
return std::fmin(std::fmax(x, y), z);
7568
})
76-
EXPORT_SCALAR_AND_VEC_1_16(THREE_ARGS, clamp, INTEGER_TYPES, FP_TYPES)
7769
} // namespace _V1
7870
} // namespace sycl

sycl/source/builtins/integer_functions.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ BUILTIN_GENINT_SU(TWO_ARGS, max,
214214
BUILTIN_GENINT_SU(TWO_ARGS, min,
215215
[](auto x, auto y) -> decltype(x) { return y < x ? y : x; })
216216

217+
BUILTIN_GENINT_SU(THREE_ARGS, clamp, [](auto x, auto y, auto z) -> decltype(x) {
218+
return std::min(std::max(x, y), z);
219+
})
220+
217221
template <typename T> static inline constexpr T __clz_impl(T x, T m, T n = 0) {
218222
return (x & m) ? n : __clz_impl(x, T(m >> 1), ++n);
219223
}

0 commit comments

Comments
 (0)