28 #ifndef HIP_INCLUDE_HIP_HCC_DETAIL_HIP_VECTOR_TYPES_H 29 #define HIP_INCLUDE_HIP_HCC_DETAIL_HIP_VECTOR_TYPES_H 31 #if defined(__HCC__) && (__hcc_workweek__ < 16032) 32 #error("This version of HIP requires a newer version of HCC."); 37 #if defined(__clang__) 38 #define __NATIVE_VECTOR__(n, ...) __attribute__((ext_vector_type(n))) 39 #elif defined(__GNUC__) // N.B.: GCC does not support .xyzw syntax. 40 #define __ROUND_UP_TO_NEXT_POT__(x) \ 41 (1 << (31 - __builtin_clz(x) + (x > (1 << (31 - __builtin_clz(x)))))) 42 #define __NATIVE_VECTOR__(n, T) \ 43 __attribute__((vector_size(__ROUND_UP_TO_NEXT_POT__(n) * sizeof(T)))) 46 #if defined(__cplusplus) 47 #include <type_traits> 49 template<
typename T,
unsigned int n>
struct HIP_vector_base;
52 struct HIP_vector_base<T, 1> {
53 typedef T Native_vec_ __NATIVE_VECTOR__(1, T);
64 struct HIP_vector_base<T, 2> {
65 typedef T Native_vec_ __NATIVE_VECTOR__(2, T);
77 struct HIP_vector_base<T, 3> {
78 typedef T Native_vec_ __NATIVE_VECTOR__(3, T);
91 struct HIP_vector_base<T, 4> {
92 typedef T Native_vec_ __NATIVE_VECTOR__(4, T);
105 template<
typename T,
unsigned int rank>
106 struct HIP_vector_type :
public HIP_vector_base<T, rank> {
107 using HIP_vector_base<T, rank>::data;
108 using typename HIP_vector_base<T, rank>::Native_vec_;
111 HIP_vector_type() =
default;
114 typename std::enable_if<
115 std::is_convertible<U, T>{}>::type* =
nullptr>
117 HIP_vector_type(U x) noexcept
119 for (
auto i = 0u; i != rank; ++i) data[i] = x;
123 typename std::enable_if<
124 (rank > 1) &&
sizeof...(Us) == rank>::type* =
nullptr>
126 HIP_vector_type(Us... xs) noexcept { data = Native_vec_{
static_cast<T
>(xs)...}; }
128 HIP_vector_type(
const HIP_vector_type&) =
default;
130 HIP_vector_type(HIP_vector_type&&) =
default;
132 ~HIP_vector_type() =
default;
135 HIP_vector_type& operator=(
const HIP_vector_type&) =
default;
137 HIP_vector_type& operator=(HIP_vector_type&&) =
default;
141 HIP_vector_type& operator++() noexcept
143 return *
this += HIP_vector_type{1};
146 HIP_vector_type operator++(
int) noexcept
153 HIP_vector_type& operator--() noexcept
155 return *
this -= HIP_vector_type{1};
158 HIP_vector_type operator--(
int) noexcept
165 HIP_vector_type& operator+=(
const HIP_vector_type& x) noexcept
171 HIP_vector_type& operator-=(
const HIP_vector_type& x) noexcept
178 typename std::enable_if<
179 std::is_convertible<U, T>{}>::type* =
nullptr>
181 HIP_vector_type& operator-=(U x) noexcept
183 return *
this -= HIP_vector_type{x};
186 HIP_vector_type& operator*=(
const HIP_vector_type& x) noexcept
192 HIP_vector_type& operator/=(
const HIP_vector_type& x) noexcept
200 typename std::enable_if<std::is_signed<U>{}>::type* =
nullptr>
202 HIP_vector_type operator-() noexcept
205 tmp.data = -tmp.data;
211 typename std::enable_if<std::is_integral<U>{}>::type* =
nullptr>
213 HIP_vector_type operator~() noexcept
215 HIP_vector_type r{*
this};
221 typename std::enable_if<std::is_integral<U>{}>::type* =
nullptr>
223 HIP_vector_type& operator%=(
const HIP_vector_type& x) noexcept
230 typename std::enable_if<std::is_integral<U>{}>::type* =
nullptr>
232 HIP_vector_type& operator^=(
const HIP_vector_type& x) noexcept
239 typename std::enable_if<std::is_integral<U>{}>::type* =
nullptr>
241 HIP_vector_type& operator|=(
const HIP_vector_type& x) noexcept
248 typename std::enable_if<std::is_integral<U>{}>::type* =
nullptr>
250 HIP_vector_type& operator&=(
const HIP_vector_type& x) noexcept
257 typename std::enable_if<std::is_integral<U>{}>::type* =
nullptr>
259 HIP_vector_type& operator>>=(
const HIP_vector_type& x) noexcept
266 typename std::enable_if<std::is_integral<U>{}>::type* =
nullptr>
268 HIP_vector_type& operator<<=(
const HIP_vector_type& x) noexcept
276 template<
typename T,
unsigned int n>
279 HIP_vector_type<T, n> operator+(
280 const HIP_vector_type<T, n>& x,
const HIP_vector_type<T, n>& y) noexcept
282 return HIP_vector_type<T, n>{x} += y;
284 template<
typename T,
unsigned int n,
typename U>
287 HIP_vector_type<T, n> operator+(
288 const HIP_vector_type<T, n>& x, U y) noexcept
290 return HIP_vector_type<T, n>{x} += y;
292 template<
typename T,
unsigned int n,
typename U>
295 HIP_vector_type<T, n> operator+(
296 U x,
const HIP_vector_type<T, n>& y) noexcept
301 template<
typename T,
unsigned int n>
304 HIP_vector_type<T, n> operator-(
305 const HIP_vector_type<T, n>& x,
const HIP_vector_type<T, n>& y) noexcept
307 return HIP_vector_type<T, n>{x} -= y;
309 template<
typename T,
unsigned int n,
typename U>
312 HIP_vector_type<T, n> operator-(
313 const HIP_vector_type<T, n>& x, U y) noexcept
315 return HIP_vector_type<T, n>{x} -= y;
317 template<
typename T,
unsigned int n,
typename U>
320 HIP_vector_type<T, n> operator-(
321 U x,
const HIP_vector_type<T, n>& y) noexcept
323 return HIP_vector_type<T, n>{x} -= y;
326 template<
typename T,
unsigned int n>
329 HIP_vector_type<T, n> operator*(
330 const HIP_vector_type<T, n>& x,
const HIP_vector_type<T, n>& y) noexcept
332 return HIP_vector_type<T, n>{x} *= y;
334 template<
typename T,
unsigned int n,
typename U>
337 HIP_vector_type<T, n> operator*(
338 const HIP_vector_type<T, n>& x, U y) noexcept
340 return HIP_vector_type<T, n>{x} *= y;
342 template<
typename T,
unsigned int n,
typename U>
345 HIP_vector_type<T, n> operator*(
346 U x,
const HIP_vector_type<T, n>& y) noexcept
351 template<
typename T,
unsigned int n>
354 HIP_vector_type<T, n> operator/(
355 const HIP_vector_type<T, n>& x,
const HIP_vector_type<T, n>& y) noexcept
357 return HIP_vector_type<T, n>{x} /= y;
359 template<
typename T,
unsigned int n,
typename U>
362 HIP_vector_type<T, n> operator/(
363 const HIP_vector_type<T, n>& x, U y) noexcept
365 return HIP_vector_type<T, n>{x} /= y;
367 template<
typename T,
unsigned int n,
typename U>
370 HIP_vector_type<T, n> operator/(
371 U x,
const HIP_vector_type<T, n>& y) noexcept
373 return HIP_vector_type<T, n>{x} /= y;
376 template<
typename T,
unsigned int n>
380 const HIP_vector_type<T, n>& x,
const HIP_vector_type<T, n>& y) noexcept
382 auto tmp = x.data == y.data;
383 for (
auto i = 0u; i != n; ++i)
if (tmp[i] == 0)
return false;
386 template<
typename T,
unsigned int n,
typename U>
389 bool operator==(
const HIP_vector_type<T, n>& x, U y) noexcept
391 return x == HIP_vector_type<T, n>{y};
393 template<
typename T,
unsigned int n,
typename U>
396 bool operator==(U x,
const HIP_vector_type<T, n>& y) noexcept
398 return HIP_vector_type<T, n>{x} == y;
401 template<
typename T,
unsigned int n>
405 const HIP_vector_type<T, n>& x,
const HIP_vector_type<T, n>& y) noexcept
409 template<
typename T,
unsigned int n,
typename U>
412 bool operator!=(
const HIP_vector_type<T, n>& x, U y) noexcept
416 template<
typename T,
unsigned int n,
typename U>
419 bool operator!=(U x,
const HIP_vector_type<T, n>& y) noexcept
427 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
429 HIP_vector_type<T, n> operator%(
430 const HIP_vector_type<T, n>& x,
const HIP_vector_type<T, n>& y) noexcept
432 return HIP_vector_type<T, n>{x} %= y;
438 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
440 HIP_vector_type<T, n> operator%(
441 const HIP_vector_type<T, n>& x, U y) noexcept
443 return HIP_vector_type<T, n>{x} %= y;
449 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
451 HIP_vector_type<T, n> operator%(
452 U x,
const HIP_vector_type<T, n>& y) noexcept
454 return HIP_vector_type<T, n>{x} %= y;
460 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
462 HIP_vector_type<T, n> operator^(
463 const HIP_vector_type<T, n>& x,
const HIP_vector_type<T, n>& y) noexcept
465 return HIP_vector_type<T, n>{x} ^= y;
471 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
473 HIP_vector_type<T, n> operator^(
474 const HIP_vector_type<T, n>& x, U y) noexcept
476 return HIP_vector_type<T, n>{x} ^= y;
482 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
484 HIP_vector_type<T, n> operator^(
485 U x,
const HIP_vector_type<T, n>& y) noexcept
487 return HIP_vector_type<T, n>{x} ^= y;
493 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
495 HIP_vector_type<T, n> operator|(
496 const HIP_vector_type<T, n>& x,
const HIP_vector_type<T, n>& y) noexcept
498 return HIP_vector_type<T, n>{x} |= y;
504 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
506 HIP_vector_type<T, n> operator|(
507 const HIP_vector_type<T, n>& x, U y) noexcept
509 return HIP_vector_type<T, n>{x} |= y;
515 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
517 HIP_vector_type<T, n> operator|(
518 U x,
const HIP_vector_type<T, n>& y) noexcept
520 return HIP_vector_type<T, n>{x} |= y;
526 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
528 HIP_vector_type<T, n> operator&(
529 const HIP_vector_type<T, n>& x,
const HIP_vector_type<T, n>& y) noexcept
531 return HIP_vector_type<T, n>{x} &= y;
537 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
539 HIP_vector_type<T, n> operator&(
540 const HIP_vector_type<T, n>& x, U y) noexcept
542 return HIP_vector_type<T, n>{x} &= y;
548 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
550 HIP_vector_type<T, n> operator&(
551 U x,
const HIP_vector_type<T, n>& y) noexcept
553 return HIP_vector_type<T, n>{x} &= y;
559 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
561 HIP_vector_type<T, n> operator>>(
562 const HIP_vector_type<T, n>& x,
const HIP_vector_type<T, n>& y) noexcept
564 return HIP_vector_type<T, n>{x} >>= y;
570 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
572 HIP_vector_type<T, n> operator>>(
573 const HIP_vector_type<T, n>& x, U y) noexcept
575 return HIP_vector_type<T, n>{x} >>= y;
581 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
583 HIP_vector_type<T, n> operator>>(
584 U x,
const HIP_vector_type<T, n>& y) noexcept
586 return HIP_vector_type<T, n>{x} >>= y;
592 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
594 HIP_vector_type<T, n> operator<<(
595 const HIP_vector_type<T, n>& x,
const HIP_vector_type<T, n>& y) noexcept
597 return HIP_vector_type<T, n>{x} <<= y;
603 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
605 HIP_vector_type<T, n> operator<<(
606 const HIP_vector_type<T, n>& x, U y) noexcept
608 return HIP_vector_type<T, n>{x} <<= y;
614 typename std::enable_if<std::is_integral<T>{}>* =
nullptr>
616 HIP_vector_type<T, n> operator<<(
617 U x,
const HIP_vector_type<T, n>& y) noexcept
619 return HIP_vector_type<T, n>{x} <<= y;
622 #define __MAKE_VECTOR_TYPE__(CUDA_name, T) \ 623 using CUDA_name##1 = HIP_vector_type<T, 1>;\ 624 using CUDA_name##2 = HIP_vector_type<T, 2>;\ 625 using CUDA_name##3 = HIP_vector_type<T, 3>;\ 626 using CUDA_name##4 = HIP_vector_type<T, 4>; 628 #define __MAKE_VECTOR_TYPE__(CUDA_name, T) \ 629 typedef T CUDA_name##_impl1 __NATIVE_VECTOR__(1, T);\ 630 typedef T CUDA_name##_impl2 __NATIVE_VECTOR__(2, T);\ 631 typedef T CUDA_name##_impl3 __NATIVE_VECTOR__(3, T);\ 632 typedef T CUDA_name##_impl4 __NATIVE_VECTOR__(4, T);\ 635 CUDA_name##_impl1 data;\ 643 CUDA_name##_impl2 data;\ 652 CUDA_name##_impl3 data;\ 662 CUDA_name##_impl4 data;\ 673 __MAKE_VECTOR_TYPE__(uchar,
unsigned char);
674 __MAKE_VECTOR_TYPE__(
char,
char);
675 __MAKE_VECTOR_TYPE__(ushort,
unsigned short);
676 __MAKE_VECTOR_TYPE__(
short,
short);
677 __MAKE_VECTOR_TYPE__(uint,
unsigned int);
678 __MAKE_VECTOR_TYPE__(
int,
int);
679 __MAKE_VECTOR_TYPE__(ulong,
unsigned long);
680 __MAKE_VECTOR_TYPE__(
long,
long);
681 __MAKE_VECTOR_TYPE__(ulonglong,
unsigned long long);
682 __MAKE_VECTOR_TYPE__(longlong,
long long);
683 __MAKE_VECTOR_TYPE__(
float,
float);
684 __MAKE_VECTOR_TYPE__(
double,
double);
686 #define DECLOP_MAKE_ONE_COMPONENT(comp, type) \ 687 __device__ __host__ \ 690 type make_##type(comp x) { type r = {x}; return r; } 692 #define DECLOP_MAKE_TWO_COMPONENT(comp, type) \ 693 __device__ __host__ \ 696 type make_##type(comp x, comp y) { type r = {x, y}; return r; } 698 #define DECLOP_MAKE_THREE_COMPONENT(comp, type) \ 699 __device__ __host__ \ 702 type make_##type(comp x, comp y, comp z) { type r = {x, y, z}; return r; } 704 #define DECLOP_MAKE_FOUR_COMPONENT(comp, type) \ 705 __device__ __host__ \ 708 type make_##type(comp x, comp y, comp z, comp w) { \ 709 type r = {x, y, z, w}; \ 713 DECLOP_MAKE_ONE_COMPONENT(
unsigned char, uchar1);
714 DECLOP_MAKE_TWO_COMPONENT(
unsigned char, uchar2);
715 DECLOP_MAKE_THREE_COMPONENT(
unsigned char, uchar3);
716 DECLOP_MAKE_FOUR_COMPONENT(
unsigned char, uchar4);
718 DECLOP_MAKE_ONE_COMPONENT(
signed char, char1);
719 DECLOP_MAKE_TWO_COMPONENT(
signed char, char2);
720 DECLOP_MAKE_THREE_COMPONENT(
signed char, char3);
721 DECLOP_MAKE_FOUR_COMPONENT(
signed char, char4);
723 DECLOP_MAKE_ONE_COMPONENT(
unsigned short, ushort1);
724 DECLOP_MAKE_TWO_COMPONENT(
unsigned short, ushort2);
725 DECLOP_MAKE_THREE_COMPONENT(
unsigned short, ushort3);
726 DECLOP_MAKE_FOUR_COMPONENT(
unsigned short, ushort4);
728 DECLOP_MAKE_ONE_COMPONENT(
signed short, short1);
729 DECLOP_MAKE_TWO_COMPONENT(
signed short, short2);
730 DECLOP_MAKE_THREE_COMPONENT(
signed short, short3);
731 DECLOP_MAKE_FOUR_COMPONENT(
signed short, short4);
733 DECLOP_MAKE_ONE_COMPONENT(
unsigned int, uint1);
734 DECLOP_MAKE_TWO_COMPONENT(
unsigned int, uint2);
735 DECLOP_MAKE_THREE_COMPONENT(
unsigned int, uint3);
736 DECLOP_MAKE_FOUR_COMPONENT(
unsigned int, uint4);
738 DECLOP_MAKE_ONE_COMPONENT(
signed int, int1);
739 DECLOP_MAKE_TWO_COMPONENT(
signed int, int2);
740 DECLOP_MAKE_THREE_COMPONENT(
signed int, int3);
741 DECLOP_MAKE_FOUR_COMPONENT(
signed int, int4);
743 DECLOP_MAKE_ONE_COMPONENT(
float, float1);
744 DECLOP_MAKE_TWO_COMPONENT(
float, float2);
745 DECLOP_MAKE_THREE_COMPONENT(
float, float3);
746 DECLOP_MAKE_FOUR_COMPONENT(
float, float4);
748 DECLOP_MAKE_ONE_COMPONENT(
double, double1);
749 DECLOP_MAKE_TWO_COMPONENT(
double, double2);
750 DECLOP_MAKE_THREE_COMPONENT(
double, double3);
751 DECLOP_MAKE_FOUR_COMPONENT(
double, double4);
753 DECLOP_MAKE_ONE_COMPONENT(
unsigned long, ulong1);
754 DECLOP_MAKE_TWO_COMPONENT(
unsigned long, ulong2);
755 DECLOP_MAKE_THREE_COMPONENT(
unsigned long, ulong3);
756 DECLOP_MAKE_FOUR_COMPONENT(
unsigned long, ulong4);
758 DECLOP_MAKE_ONE_COMPONENT(
signed long, long1);
759 DECLOP_MAKE_TWO_COMPONENT(
signed long, long2);
760 DECLOP_MAKE_THREE_COMPONENT(
signed long, long3);
761 DECLOP_MAKE_FOUR_COMPONENT(
signed long, long4);
763 DECLOP_MAKE_ONE_COMPONENT(
unsigned long long, ulonglong1);
764 DECLOP_MAKE_TWO_COMPONENT(
unsigned long long, ulonglong2);
765 DECLOP_MAKE_THREE_COMPONENT(
unsigned long long, ulonglong3);
766 DECLOP_MAKE_FOUR_COMPONENT(
unsigned long long, ulonglong4);
768 DECLOP_MAKE_ONE_COMPONENT(
signed long long, longlong1);
769 DECLOP_MAKE_TWO_COMPONENT(
signed long long, longlong2);
770 DECLOP_MAKE_THREE_COMPONENT(
signed long long, longlong3);
771 DECLOP_MAKE_FOUR_COMPONENT(
signed long long, longlong4);
#define __host__
Definition: host_defines.h:41