Skip to content
This repository was archived by the owner on Jan 26, 2024. It is now read-only.

Commit 223d35d

Browse files
committed
Vector ops: support compilers without ext_vector_type
This allows compilation of code such as ```` int main() { float4 v1, v2, v3; v3 = v1 + v2; v3 = v1 - v2; v3 = v1 * v2; v3 = v1 / v2; int4 i1, i2, i3; i3 = -i1; i3 = ~i2; i3 = i1 % i2; i3 = i1 ^ i2; i3 = i1 >> i2; i3 = i1 << i2; } ```` with a host compiler that supports __has_attribute, but does not support the ext_vector_type attribute (e.g. g++).
1 parent cc81f2c commit 223d35d

File tree

1 file changed

+72
-12
lines changed

1 file changed

+72
-12
lines changed

include/hip/amd_detail/amd_hip_vector_types.h

Lines changed: 72 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,12 @@ template <typename __T> struct is_scalar : public integral_constant<bool, __is_s
811811
__HOST_DEVICE__
812812
HIP_vector_type& operator+=(const HIP_vector_type& x) noexcept
813813
{
814-
data += x.data;
814+
#if __has_attribute(ext_vector_type)
815+
data += x.data;
816+
#else
817+
for (auto i = 0u; i < rank; ++i)
818+
data[i] += x.data[i];
819+
#endif
815820
return *this;
816821
}
817822
template<
@@ -827,7 +832,12 @@ template <typename __T> struct is_scalar : public integral_constant<bool, __is_s
827832
__HOST_DEVICE__
828833
HIP_vector_type& operator-=(const HIP_vector_type& x) noexcept
829834
{
830-
data -= x.data;
835+
#if __has_attribute(ext_vector_type)
836+
data -= x.data;
837+
#else
838+
for (auto i = 0u; i < rank; ++i)
839+
data[i] -= x.data[i];
840+
#endif
831841
return *this;
832842
}
833843
template<
@@ -843,7 +853,12 @@ template <typename __T> struct is_scalar : public integral_constant<bool, __is_s
843853
__HOST_DEVICE__
844854
HIP_vector_type& operator*=(const HIP_vector_type& x) noexcept
845855
{
846-
data *= x.data;
856+
#if __has_attribute(ext_vector_type)
857+
data *= x.data;
858+
#else
859+
for (auto i = 0u; i < rank; ++i)
860+
data[i] *= x.data[i];
861+
#endif
847862
return *this;
848863
}
849864
template<
@@ -859,7 +874,12 @@ template <typename __T> struct is_scalar : public integral_constant<bool, __is_s
859874
__HOST_DEVICE__
860875
HIP_vector_type& operator/=(const HIP_vector_type& x) noexcept
861876
{
862-
data /= x.data;
877+
#if __has_attribute(ext_vector_type)
878+
data /= x.data;
879+
#else
880+
for (auto i = 0u; i < rank; ++i)
881+
data[i] /= x.data[i];
882+
#endif
863883
return *this;
864884
}
865885
template<
@@ -879,7 +899,12 @@ template <typename __T> struct is_scalar : public integral_constant<bool, __is_s
879899
HIP_vector_type operator-() const noexcept
880900
{
881901
auto tmp(*this);
882-
tmp.data = -tmp.data;
902+
#if __has_attribute(ext_vector_type)
903+
tmp.data = -tmp.data;
904+
#else
905+
for (auto i = 0u; i < rank; ++i)
906+
tmp.data[i] = -tmp.data[i];
907+
#endif
883908
return tmp;
884909
}
885910

@@ -890,7 +915,12 @@ template <typename __T> struct is_scalar : public integral_constant<bool, __is_s
890915
HIP_vector_type operator~() const noexcept
891916
{
892917
HIP_vector_type r{*this};
893-
r.data = ~r.data;
918+
#if __has_attribute(ext_vector_type)
919+
r.data = ~r.data;
920+
#else
921+
for (auto i = 0u; i < rank; ++i)
922+
r.data[i] = ~r.data[i];
923+
#endif
894924
return r;
895925
}
896926

@@ -900,7 +930,12 @@ template <typename __T> struct is_scalar : public integral_constant<bool, __is_s
900930
__HOST_DEVICE__
901931
HIP_vector_type& operator%=(const HIP_vector_type& x) noexcept
902932
{
903-
data %= x.data;
933+
#if __has_attribute(ext_vector_type)
934+
data %= x.data;
935+
#else
936+
for (auto i = 0u; i < rank; ++i)
937+
data[i] %= x.data[i];
938+
#endif
904939
return *this;
905940
}
906941

@@ -910,7 +945,12 @@ template <typename __T> struct is_scalar : public integral_constant<bool, __is_s
910945
__HOST_DEVICE__
911946
HIP_vector_type& operator^=(const HIP_vector_type& x) noexcept
912947
{
913-
data ^= x.data;
948+
#if __has_attribute(ext_vector_type)
949+
data ^= x.data;
950+
#else
951+
for (auto i = 0u; i < rank; ++i)
952+
data[i] ^= x.data[i];
953+
#endif
914954
return *this;
915955
}
916956

@@ -920,7 +960,12 @@ template <typename __T> struct is_scalar : public integral_constant<bool, __is_s
920960
__HOST_DEVICE__
921961
HIP_vector_type& operator|=(const HIP_vector_type& x) noexcept
922962
{
923-
data |= x.data;
963+
#if __has_attribute(ext_vector_type)
964+
data |= x.data;
965+
#else
966+
for (auto i = 0u; i < rank; ++i)
967+
data[i] |= x.data[i];
968+
#endif
924969
return *this;
925970
}
926971

@@ -930,7 +975,12 @@ template <typename __T> struct is_scalar : public integral_constant<bool, __is_s
930975
__HOST_DEVICE__
931976
HIP_vector_type& operator&=(const HIP_vector_type& x) noexcept
932977
{
933-
data &= x.data;
978+
#if __has_attribute(ext_vector_type)
979+
data &= x.data;
980+
#else
981+
for (auto i = 0u; i < rank; ++i)
982+
data[i] &= x.data[i];
983+
#endif
934984
return *this;
935985
}
936986

@@ -940,7 +990,12 @@ template <typename __T> struct is_scalar : public integral_constant<bool, __is_s
940990
__HOST_DEVICE__
941991
HIP_vector_type& operator>>=(const HIP_vector_type& x) noexcept
942992
{
943-
data >>= x.data;
993+
#if __has_attribute(ext_vector_type)
994+
data >>= x.data;
995+
#else
996+
for (auto i = 0u; i < rank; ++i)
997+
data[i] >>= x.data[i];
998+
#endif
944999
return *this;
9451000
}
9461001

@@ -950,7 +1005,12 @@ template <typename __T> struct is_scalar : public integral_constant<bool, __is_s
9501005
__HOST_DEVICE__
9511006
HIP_vector_type& operator<<=(const HIP_vector_type& x) noexcept
9521007
{
953-
data <<= x.data;
1008+
#if __has_attribute(ext_vector_type)
1009+
data <<= x.data;
1010+
#else
1011+
for (auto i = 0u; i < rank; ++i)
1012+
data[i] <<= x.data[i];
1013+
#endif
9541014
return *this;
9551015
}
9561016
};

0 commit comments

Comments
 (0)