Skip to content

Commit

Permalink
Implemented sadd, ssub & reduce_add through the generic implementatio…
Browse files Browse the repository at this point in the history
…n for xsimd_sse2
  • Loading branch information
anutosh491 committed Oct 25, 2023
1 parent f27bbb8 commit 2898acf
Showing 1 changed file with 6 additions and 60 deletions.
66 changes: 6 additions & 60 deletions include/xsimd/arch/xsimd_sse2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1237,22 +1237,7 @@ namespace xsimd
batch<T, A> acc3 = min(acc2, step3);
return acc3.get(0);
}
// TODO: move this in xsimd_generic
namespace detail
{
template <class A, class T, class = typename std::enable_if<std::is_integral<T>::value, void>::type>
inline T hadd_default(batch<T, A> const& self, requires_arch<sse2>) noexcept
{
alignas(A::alignment()) T buffer[batch<T, A>::size];
self.store_aligned(buffer);
T res = 0;
for (T val : buffer)
{
res += val;
}
return res;
}
}

template <class A, class T, class = typename std::enable_if<std::is_integral<T>::value, void>::type>
inline T reduce_add(batch<T, A> const& self, requires_arch<sse2>) noexcept
{
Expand Down Expand Up @@ -1280,7 +1265,7 @@ namespace xsimd
}
else
{
return detail::hadd_default(self, A {});
return hadd(self, generic {});
}
}
template <class A>
Expand Down Expand Up @@ -1381,28 +1366,6 @@ namespace xsimd

// sadd

// TODO: move this in xsimd_generic
namespace detail
{
template <class A, class T, class = typename std::enable_if<std::is_integral<T>::value, void>::type>
inline batch<T, A> sadd_default(batch<T, A> const& self, batch<T, A> const& other, requires_arch<sse2>) noexcept
{
if (std::is_signed<T>::value)
{
auto mask = (other >> (8 * sizeof(T) - 1));
auto self_pos_branch = min(std::numeric_limits<T>::max() - other, self);
auto self_neg_branch = max(std::numeric_limits<T>::min() - other, self);
return other + select(batch_bool<T, A>(mask.data), self_neg_branch, self_pos_branch);
}
else
{
const auto diffmax = std::numeric_limits<T>::max() - self;
const auto mindiff = min(diffmax, other);
return self + mindiff;
}
}
}

template <class A, class T, class = typename std::enable_if<std::is_integral<T>::value, void>::type>
inline batch<T, A> sadd(batch<T, A> const& self, batch<T, A> const& other, requires_arch<sse2>) noexcept
{
Expand All @@ -1418,7 +1381,7 @@ namespace xsimd
}
else
{
return detail::sadd_default(self, other, A {});
return sadd(self, other, generic {});
}
}
else
Expand All @@ -1433,7 +1396,7 @@ namespace xsimd
}
else
{
return detail::sadd_default(self, other, A {});
return sadd(self, other, generic {});
}
}
}
Expand Down Expand Up @@ -1495,23 +1458,6 @@ namespace xsimd
}

// ssub
// TODO: move this in xsimd_generic
namespace detail
{
template <class A, class T, class = typename std::enable_if<std::is_integral<T>::value, void>::type>
inline batch<T, A> ssub_default(batch<T, A> const& self, batch<T, A> const& other, requires_arch<sse2>) noexcept
{
if (std::is_signed<T>::value)
{
return sadd(self, -other);
}
else
{
const auto diff = min(self, other);
return self - diff;
}
}
}

template <class A, class T, class = typename std::enable_if<std::is_integral<T>::value, void>::type>
inline batch<T, A> ssub(batch<T, A> const& self, batch<T, A> const& other, requires_arch<sse2>) noexcept
Expand All @@ -1528,7 +1474,7 @@ namespace xsimd
}
else
{
return detail::ssub_default(self, other, A {});
return ssub(self, other, generic {});
}
}
else
Expand All @@ -1543,7 +1489,7 @@ namespace xsimd
}
else
{
return detail::ssub_default(self, other, A {});
return ssub(self, other, generic {});
}
}
}
Expand Down

0 comments on commit 2898acf

Please sign in to comment.