-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
206 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
#pragma once | ||
|
||
#include <xsimd/xsimd.hpp> | ||
|
||
|
||
namespace dspbb { | ||
|
||
|
||
template <class T> | ||
struct is_vectorized { | ||
static constexpr bool value = xsimd::simd_traits<T>::size > 1; | ||
}; | ||
|
||
|
||
//------------------------------------------------------------------------------ | ||
// Binary operations. | ||
//------------------------------------------------------------------------------ | ||
|
||
template <class R, class T, class U, class Op> | ||
void BinaryOperation(R* out, const T* a, const U* b, size_t length, Op op) { | ||
const R* last = out + length; | ||
for (; out != last; ++out, ++a, ++b) { | ||
*out = R(op(*a, *b)); | ||
} | ||
} | ||
|
||
template <class R, class T, class U, class Op, std::enable_if_t<!std::is_pointer<T>::value, int> = 0> | ||
void BinaryOperation(R* out, const T& a, const U* b, size_t length, Op op) { | ||
const R* last = out + length; | ||
for (; out != last; ++out, ++b) { | ||
*out = R(op(a, *b)); | ||
} | ||
} | ||
|
||
template <class R, class T, class U, class Op, std::enable_if_t<!std::is_pointer<T>::value, int> = 0> | ||
void BinaryOperation(R* out, const T* a, const U& b, size_t length, Op op) { | ||
const R* last = out + length; | ||
for (; out != last; ++out, ++a) { | ||
*out = R(op(*a, b)); | ||
} | ||
} | ||
|
||
template <class T, class Op, std::enable_if_t<is_vectorized<T>::value, int> = 0> | ||
void BinaryOperationVectorized(T* out, const T* a, const T* b, size_t length, Op op) { | ||
using V = xsimd::simd_type<T>; | ||
constexpr size_t vsize = xsimd::simd_traits<T>::size; | ||
|
||
const size_t vlength = (length / vsize) * vsize; | ||
|
||
const T* vlast = out + vlength; | ||
for (; out < vlast; out += vsize, a += vsize, b += vsize) { | ||
V va, vb; | ||
va.load_unaligned(a); | ||
vb.load_unaligned(b); | ||
auto vr = op(va, vb); | ||
vr.store_unaligned(out); | ||
} | ||
|
||
BinaryOperation(out, a, b, length - vlength, op); | ||
} | ||
|
||
template <class T, class Op, std::enable_if_t<!std::is_pointer<T>::value && is_vectorized<T>::value, int> = 0> | ||
void BinaryOperationVectorized(T* out, const T& a, const T* b, size_t length, Op op) { | ||
using V = xsimd::simd_type<T>; | ||
constexpr size_t vsize = xsimd::simd_traits<T>::size; | ||
|
||
const size_t vlength = (length / vsize) * vsize; | ||
|
||
const T* vlast = out + vlength; | ||
V va{ a }; | ||
for (; out < vlast; out += vsize, b += vsize) { | ||
V vb; | ||
vb.load_unaligned(b); | ||
auto vr = op(va, vb); | ||
vr.store_unaligned(out); | ||
} | ||
|
||
BinaryOperation(out, a, b, length - vlength, op); | ||
} | ||
|
||
template <class T, class Op, std::enable_if_t<!std::is_pointer<T>::value && is_vectorized<T>::value, int> = 0> | ||
void BinaryOperationVectorized(T* out, const T* a, const T& b, size_t length, Op op) { | ||
using V = xsimd::simd_type<T>; | ||
constexpr size_t vsize = xsimd::simd_traits<T>::size; | ||
|
||
const size_t vlength = (length / vsize) * vsize; | ||
|
||
const T* vlast = out + vlength; | ||
V vb{ b }; | ||
for (; out < vlast; out += vsize, a += vsize) { | ||
V va; | ||
va.load_unaligned(a); | ||
auto vr = op(va, vb); | ||
vr.store_unaligned(out); | ||
} | ||
|
||
BinaryOperation(out, a, b, length - vlength, op); | ||
} | ||
|
||
|
||
template <class R, class T, class U, class Op> | ||
void BinaryOperationVectorized(R* out, const T* a, const U* b, size_t length, Op op) { | ||
BinaryOperation(out, a, b, length, op); | ||
} | ||
|
||
template <class R, class T, class U, class Op, std::enable_if_t<!std::is_pointer<T>::value, int> = 0> | ||
void BinaryOperationVectorized(R* out, const T& a, const U* b, size_t length, Op op) { | ||
BinaryOperation(out, a, b, length, op); | ||
} | ||
|
||
template <class R, class T, class U, class Op, std::enable_if_t<!std::is_pointer<T>::value, int> = 0> | ||
void BinaryOperationVectorized(R* out, const T* a, const U& b, size_t length, Op op) { | ||
BinaryOperation(out, a, b, length, op); | ||
} | ||
|
||
//------------------------------------------------------------------------------ | ||
// Unary operations. | ||
//------------------------------------------------------------------------------ | ||
|
||
template <class R, class T, class Op> | ||
void UnaryOperation(R* out, const T* in, size_t length, Op op) { | ||
const R* last = out + length; | ||
for (; out != last; ++out, ++in) { | ||
*out = R(op(*in)); | ||
} | ||
} | ||
|
||
template <class R, class T, class Op> | ||
void UnaryOperationVectorized(R* out, const T* in, size_t length, Op op) { | ||
return UnaryOperation(out, in, length, op); | ||
} | ||
|
||
template <class T, class Op, std::enable_if_t<!is_vectorized<T>::value, int> = 0> | ||
void UnaryOperationVectorized(T* out, T* in, size_t length, Op op) { | ||
using V = xsimd::simd_type<T>; | ||
constexpr size_t vsize = xsimd::simd_traits<T>::size; | ||
|
||
const size_t vlength = (length / vsize) * vsize; | ||
|
||
const T* vlast = out + vlength; | ||
for (; out < vlast; out += vsize, in += vsize) { | ||
V vin; | ||
vin.load_unaligned(in); | ||
auto vr = op(vin); | ||
vr.store_unaligned(out); | ||
} | ||
|
||
UnaryOperation(out, in, length - vlength, op); | ||
} | ||
|
||
|
||
} // namespace dspbb |
Oops, something went wrong.