/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/core/detail/swizzling.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/core/detail/swizzling.hpp Source File#

4 min read time

Applies to Linux

rocCV: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-roccv/checkouts/latest/include/core/detail/swizzling.hpp Source File
swizzling.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2025 Advanced Micro Devices, Inc. All rights reserved.
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21 
22 #pragma once
23 
24 #include <hip/hip_runtime.h>
25 
27 #include "core/image_format.hpp"
28 
29 namespace roccv::detail {
30 
31 template <eSwizzle Pattern, int N>
33 
34 // clang-format off
35 
36 // This section defines compile-time index mapping for all defined eSwizzle patterns using template specialization.
37 
38 // XYZW
39 template <int N>
41  using Seq = std::make_integer_sequence<size_t, N>;
42 };
43 
44 // ZYXW
45 template <> struct SwizzleIndexMap<eSwizzle::ZYXW, 1> { using Seq = std::integer_sequence<size_t, 0>; };
46 template <> struct SwizzleIndexMap<eSwizzle::ZYXW, 3> { using Seq = std::integer_sequence<size_t, 2, 1, 0>; };
47 template <> struct SwizzleIndexMap<eSwizzle::ZYXW, 4> { using Seq = std::integer_sequence<size_t, 2, 1, 0, 3>; };
48 // clang-format on
49 
50 template <typename T, size_t... Indices>
51 __host__ __device__ constexpr T SwizzleIndicesImpl(const T &v) {
52  return T{v[Indices]...};
53 }
54 
55 template <typename T, size_t... I>
56 __host__ __device__ constexpr T ExpandSwizzle(const T &v, std::integer_sequence<size_t, I...>) {
57  static_assert(((I < NumElements<T>) && ...), "Swizzle index out of range");
58  return SwizzleIndicesImpl<T, I...>(v);
59 }
60 
77 template <eSwizzle Pattern, typename T>
78 __host__ __device__ constexpr T Swizzle(const T &v) {
79  static_assert(HasTypeTraits<T> && IsCompound<T>, "Type must have type traits and be a compound type");
80 
81  constexpr int N = NumElements<T>;
82  static_assert(N >= 1 && N <= 4, "Unsupported element count");
83 
84  using Seq = typename SwizzleIndexMap<Pattern, N>::Seq;
85  return ExpandSwizzle<T>(v, Seq{});
86 }
87 
88 } // namespace roccv::detail
Definition: strided_data_wrap.hpp:33
__host__ constexpr __device__ T Swizzle(const T &v)
Rearranges the components of a vector according to a given swizzle pattern.
Definition: swizzling.hpp:78
__host__ constexpr __device__ T ExpandSwizzle(const T &v, std::integer_sequence< size_t, I... >)
Definition: swizzling.hpp:56
__host__ constexpr __device__ T SwizzleIndicesImpl(const T &v)
Definition: swizzling.hpp:51
eSwizzle
Defines the swizzling for channel ordering.
Definition: image_format.hpp:32
std::make_integer_sequence< size_t, N > Seq
Definition: swizzling.hpp:41
std::integer_sequence< size_t, 0 > Seq
Definition: swizzling.hpp:45
std::integer_sequence< size_t, 2, 1, 0 > Seq
Definition: swizzling.hpp:46
std::integer_sequence< size_t, 2, 1, 0, 3 > Seq
Definition: swizzling.hpp:47
Definition: swizzling.hpp:32