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

Commit e5d1e47

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 20cc63e commit e5d1e47

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
@@ -795,7 +795,12 @@ typedef basic_ostream<char> ostream;
795795
__HOST_DEVICE__
796796
HIP_vector_type& operator+=(const HIP_vector_type& x) noexcept
797797
{
798-
data += x.data;
798+
#if __has_attribute(ext_vector_type)
799+
data += x.data;
800+
#else
801+
for (auto i = 0u; i < rank; ++i)
802+
data[i] += x.data[i];
803+
#endif
799804
return *this;
800805
}
801806
template<
@@ -811,7 +816,12 @@ typedef basic_ostream<char> ostream;
811816
__HOST_DEVICE__
812817
HIP_vector_type& operator-=(const HIP_vector_type& x) noexcept
813818
{
814-
data -= x.data;
819+
#if __has_attribute(ext_vector_type)
820+
data -= x.data;
821+
#else
822+
for (auto i = 0u; i < rank; ++i)
823+
data[i] -= x.data[i];
824+
#endif
815825
return *this;
816826
}
817827
template<
@@ -827,7 +837,12 @@ typedef basic_ostream<char> ostream;
827837
__HOST_DEVICE__
828838
HIP_vector_type& operator*=(const HIP_vector_type& x) noexcept
829839
{
830-
data *= x.data;
840+
#if __has_attribute(ext_vector_type)
841+
data *= x.data;
842+
#else
843+
for (auto i = 0u; i < rank; ++i)
844+
data[i] *= x.data[i];
845+
#endif
831846
return *this;
832847
}
833848
template<
@@ -843,7 +858,12 @@ typedef basic_ostream<char> ostream;
843858
__HOST_DEVICE__
844859
HIP_vector_type& operator/=(const HIP_vector_type& x) noexcept
845860
{
846-
data /= x.data;
861+
#if __has_attribute(ext_vector_type)
862+
data /= x.data;
863+
#else
864+
for (auto i = 0u; i < rank; ++i)
865+
data[i] /= x.data[i];
866+
#endif
847867
return *this;
848868
}
849869
template<
@@ -863,7 +883,12 @@ typedef basic_ostream<char> ostream;
863883
HIP_vector_type operator-() const noexcept
864884
{
865885
auto tmp(*this);
866-
tmp.data = -tmp.data;
886+
#if __has_attribute(ext_vector_type)
887+
tmp.data = -tmp.data;
888+
#else
889+
for (auto i = 0u; i < rank; ++i)
890+
tmp.data[i] = -tmp.data[i];
891+
#endif
867892
return tmp;
868893
}
869894

@@ -874,7 +899,12 @@ typedef basic_ostream<char> ostream;
874899
HIP_vector_type operator~() const noexcept
875900
{
876901
HIP_vector_type r{*this};
877-
r.data = ~r.data;
902+
#if __has_attribute(ext_vector_type)
903+
r.data = ~r.data;
904+
#else
905+
for (auto i = 0u; i < rank; ++i)
906+
r.data[i] = ~r.data[i];
907+
#endif
878908
return r;
879909
}
880910

@@ -884,7 +914,12 @@ typedef basic_ostream<char> ostream;
884914
__HOST_DEVICE__
885915
HIP_vector_type& operator%=(const HIP_vector_type& x) noexcept
886916
{
887-
data %= x.data;
917+
#if __has_attribute(ext_vector_type)
918+
data %= x.data;
919+
#else
920+
for (auto i = 0u; i < rank; ++i)
921+
data[i] %= x.data[i];
922+
#endif
888923
return *this;
889924
}
890925

@@ -894,7 +929,12 @@ typedef basic_ostream<char> ostream;
894929
__HOST_DEVICE__
895930
HIP_vector_type& operator^=(const HIP_vector_type& x) noexcept
896931
{
897-
data ^= x.data;
932+
#if __has_attribute(ext_vector_type)
933+
data ^= x.data;
934+
#else
935+
for (auto i = 0u; i < rank; ++i)
936+
data[i] ^= x.data[i];
937+
#endif
898938
return *this;
899939
}
900940

@@ -904,7 +944,12 @@ typedef basic_ostream<char> ostream;
904944
__HOST_DEVICE__
905945
HIP_vector_type& operator|=(const HIP_vector_type& x) noexcept
906946
{
907-
data |= x.data;
947+
#if __has_attribute(ext_vector_type)
948+
data |= x.data;
949+
#else
950+
for (auto i = 0u; i < rank; ++i)
951+
data[i] |= x.data[i];
952+
#endif
908953
return *this;
909954
}
910955

@@ -914,7 +959,12 @@ typedef basic_ostream<char> ostream;
914959
__HOST_DEVICE__
915960
HIP_vector_type& operator&=(const HIP_vector_type& x) noexcept
916961
{
917-
data &= x.data;
962+
#if __has_attribute(ext_vector_type)
963+
data &= x.data;
964+
#else
965+
for (auto i = 0u; i < rank; ++i)
966+
data[i] &= x.data[i];
967+
#endif
918968
return *this;
919969
}
920970

@@ -924,7 +974,12 @@ typedef basic_ostream<char> ostream;
924974
__HOST_DEVICE__
925975
HIP_vector_type& operator>>=(const HIP_vector_type& x) noexcept
926976
{
927-
data >>= x.data;
977+
#if __has_attribute(ext_vector_type)
978+
data >>= x.data;
979+
#else
980+
for (auto i = 0u; i < rank; ++i)
981+
data[i] >>= x.data[i];
982+
#endif
928983
return *this;
929984
}
930985

@@ -934,7 +989,12 @@ typedef basic_ostream<char> ostream;
934989
__HOST_DEVICE__
935990
HIP_vector_type& operator<<=(const HIP_vector_type& x) noexcept
936991
{
937-
data <<= x.data;
992+
#if __has_attribute(ext_vector_type)
993+
data <<= x.data;
994+
#else
995+
for (auto i = 0u; i < rank; ++i)
996+
data[i] <<= x.data[i];
997+
#endif
938998
return *this;
939999
}
9401000
};

0 commit comments

Comments
 (0)