| 1 |
/*========================== begin_copyright_notice ============================ |
| 2 |
|
| 3 |
Copyright (C) 2021 Intel Corporation |
| 4 |
|
| 5 |
SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
============================= end_copyright_notice ===========================*/ |
| 8 |
|
| 9 |
#ifndef CM_CL_DETAIL_VECTOR_IMPL_H |
| 10 |
#define CM_CL_DETAIL_VECTOR_IMPL_H |
| 11 |
|
| 12 |
#include <opencl_type_traits.h> |
| 13 |
|
| 14 |
namespace cm { |
| 15 |
namespace detail { |
| 16 |
|
| 17 |
template<typename T, int width> |
| 18 |
using vector_impl = T __attribute__((ext_vector_type(width))); |
| 19 |
|
| 20 |
// offset into vector |
| 21 |
using vector_offset_type = short; |
| 22 |
|
| 23 |
template <typename To, typename From, int width> |
| 24 |
constexpr int calc_bit_cast_width() { |
| 25 |
if constexpr (sizeof(To) <= sizeof(From)) |
| 26 |
return width * (sizeof(From) / sizeof(To)); |
| 27 |
else { |
| 28 |
constexpr auto ratio = sizeof(To) / sizeof(From); |
| 29 |
static_assert(width % ratio == 0, |
| 30 |
"cannot bitcast the provided vector to the requested type"); |
| 31 |
return width / ratio; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
template <typename To, typename From, int width> |
| 36 |
constexpr auto bit_cast(vector_impl<From, width> vec) { |
| 37 |
using ret_t = vector_impl<To, calc_bit_cast_width<To, From, width>()>; |
| 38 |
if constexpr (cl::is_same<To, From>::value) |
| 39 |
return vec; |
| 40 |
else |
| 41 |
return __builtin_bit_cast(ret_t, vec); |
| 42 |
} |
| 43 |
|
| 44 |
// clang-9 somehow doesn't consider this function constexpr. |
| 45 |
#if __clang_major__ > 9 |
| 46 |
template <typename T, int width> |
| 47 |
constexpr int get_width(vector_impl<T, width>) { |
| 48 |
return width; |
| 49 |
} |
| 50 |
#endif // __clang_major__ > 9 |
| 51 |
|
| 52 |
template <typename T> struct width_getter {}; |
| 53 |
|
| 54 |
template <typename T, int width> |
| 55 |
struct width_getter<__private vector_impl<T, width>> |
| 56 |
: public cl::integral_constant<int, width> {}; |
| 57 |
template <typename T, int width> |
| 58 |
struct width_getter<__global vector_impl<T, width>> |
| 59 |
: public cl::integral_constant<int, width> {}; |
| 60 |
template <typename T, int width> |
| 61 |
struct width_getter<__constant vector_impl<T, width>> |
| 62 |
: public cl::integral_constant<int, width> {}; |
| 63 |
template <typename T, int width> |
| 64 |
struct width_getter<__local vector_impl<T, width>> |
| 65 |
: public cl::integral_constant<int, width> {}; |
| 66 |
template <typename T, int width> |
| 67 |
struct width_getter<__generic vector_impl<T, width>> |
| 68 |
: public cl::integral_constant<int, width> {}; |
| 69 |
|
| 70 |
} // namespace detail |
| 71 |
} // namespace cm |
| 72 |
|
| 73 |
#endif // CM_CL_DETAIL_VECTOR_IMPL_H |