Skip to content

Commit

Permalink
refactor 0
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed Nov 7, 2020
1 parent 382f462 commit 50f56b6
Show file tree
Hide file tree
Showing 21 changed files with 185 additions and 115 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(UGM VERSION 0.6.12)
project(UGM VERSION 0.6.13)
message(STATUS "[Project] ${PROJECT_NAME}")

include(cmake/InitUCMake.cmake)
Ubpa_InitUCMake()

Ubpa_InitProject()

set(Ubpa_USE_SIMD TRUE CACHE BOOL "use simd")
set(Ubpa_USE_SSE_4_1 TRUE CACHE BOOL "use sse 4.1")
option(Ubpa_UGM_UseSIMD "use SIMD" TRUE)
option(Ubpa_UGM_UseSSE_4_1 "use SSE 4.1" TRUE)

Ubpa_AddDep(UTemplate 0.4.8)

Expand Down
2 changes: 1 addition & 1 deletion include/UGM/Interfaces/IArray/IArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ namespace Ubpa {
}
};

#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
template<typename IArray_Base, typename Impl>
struct alignas(16) IArray_Impl<true, IArray_Base, Impl> : IArray_Base {
public:
Expand Down
20 changes: 10 additions & 10 deletions include/UGM/Interfaces/IArray/IArray1D_Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ namespace Ubpa {

inline const Impl abs() const noexcept {
const auto& x = static_cast<const Impl&>(*this);
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>)
return _mm_abs_ps(x);
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
{
Impl rst;
for (size_t i = 0; i < N; i++)
Expand All @@ -33,7 +33,7 @@ namespace Ubpa {
}

inline T min_component() const noexcept {
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>) {
// 5 instructions
const auto& s0 = *this;
Expand All @@ -46,7 +46,7 @@ namespace Ubpa {
//return std::min(std::min((*this)[0], (*this)[1]), std::min((*this)[2], (*this)[3]));
}
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
return (*this)[min_dim()];
}

Expand All @@ -55,7 +55,7 @@ namespace Ubpa {
}

inline T max_component() const noexcept {
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>) {
// 5 instructions
const auto& s0 = *this;
Expand All @@ -68,7 +68,7 @@ namespace Ubpa {
//return std::max(std::max((*this)[0], (*this)[1]), std::max((*this)[2], (*this)[3]));
}
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
return (*this)[max_dim()];
}

Expand Down Expand Up @@ -97,11 +97,11 @@ namespace Ubpa {
}

static const Impl min(const Impl& x, const Impl& y) noexcept {
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>)
return _mm_min_ps(x, y);
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
{
Impl rst;
for (size_t i = 0; i < N; i++)
Expand All @@ -112,11 +112,11 @@ namespace Ubpa {
}

static const Impl max(const Impl& x, const Impl& y) noexcept {
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>)
return _mm_max_ps(x, y);
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
{
Impl rst;
for (size_t i = 0; i < N; i++)
Expand Down
28 changes: 14 additions & 14 deletions include/UGM/Interfaces/IArray/IArrayAdd.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ namespace Ubpa {

inline const Impl impl_add(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>)
return _mm_add_ps(x, y);
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
{
Impl rst;
for (size_t i = 0; i < N; i++)
Expand All @@ -33,11 +33,11 @@ namespace Ubpa {

inline Impl& impl_add_to_self(const Impl& y) noexcept {
auto& x = static_cast<Impl&>(*this);
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>)
return x = x + y;
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
{
for (size_t i = 0; i < N; i++)
x[i] += y[i];
Expand All @@ -47,12 +47,12 @@ namespace Ubpa {

inline const Impl impl_add_inverse() const noexcept {
const auto& x = static_cast<const Impl&>(*this);
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>)
// ref: https://stackoverflow.com/questions/20083997/how-to-negate-change-sign-of-the-floating-point-elements-in-a-m128-type-vari
return _mm_sub_ps(Impl{ 0.f }, x);
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
{
Impl rst;
for (size_t i = 0; i < N; i++) {
Expand All @@ -67,12 +67,12 @@ namespace Ubpa {

inline const Impl impl_minus(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>) {
return _mm_sub_ps(x, y);
}
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
{
Impl rst;
for (size_t i = 0; i < N; i++)
Expand All @@ -83,12 +83,12 @@ namespace Ubpa {

inline Impl& impl_minus_to_self(const Impl& y) noexcept {
auto& x = static_cast<Impl&>(*this);
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>) {
return x = x - y;
}
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
{
for (size_t i = 0; i < N; i++)
x[i] -= y[i];
Expand All @@ -99,11 +99,11 @@ namespace Ubpa {
template<typename U, std::enable_if_t<std::is_integral_v<U>>* = nullptr>
inline Impl impl_add_mul(U v) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>)
return _mm_mul_ps(x, Impl{ v });
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
{
Impl rst;
for (size_t i = 0; i < N; i++)
Expand All @@ -115,11 +115,11 @@ namespace Ubpa {
template<typename U, std::enable_if_t<std::is_integral_v<U>>* = nullptr>
inline Impl& impl_add_mul_to_self(U v) noexcept {
auto& x = static_cast<Impl&>(*this);
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>)
return x = x * v;
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
{
for (size_t i = 0; i < N; i++)
x[i] *= v;
Expand Down
20 changes: 10 additions & 10 deletions include/UGM/Interfaces/IArray/IArrayHadamardProduct.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ namespace Ubpa {

Impl& operator*=(const Impl& y) noexcept {
auto& x = static_cast<Impl&>(*this);
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>)
x = x * y;
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
for (size_t i = 0; i < N; i++)
x[i] *= y[i];
return x;
}

const Impl operator/(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>)
return _mm_div_ps(x, y);
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
{
Impl rst;
for (size_t i = 0; i < N; i++)
Expand All @@ -45,11 +45,11 @@ namespace Ubpa {

Impl& operator/=(const Impl& y) noexcept {
auto& x = static_cast<Impl&>(*this);
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>)
return x = x / y;
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
for (size_t i = 0; i < N; i++)
x[i] /= y[i];
return x;
Expand All @@ -58,11 +58,11 @@ namespace Ubpa {
inline const Impl inverse() const noexcept {
const auto& x = static_cast<const Impl&>(*this);

#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>)
return _mm_div_ps(Impl{ 1.f }, x);
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
{
Impl rst;
for (size_t i = 0; i < N; i++)
Expand All @@ -78,11 +78,11 @@ namespace Ubpa {
inline const Impl impl_mul(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);

#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>)
return _mm_mul_ps(x, y);
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
{
Impl rst;
for (size_t i = 0; i < N; i++)
Expand Down
12 changes: 6 additions & 6 deletions include/UGM/Interfaces/IArray/IArrayScalarMul.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Ubpa {
static constexpr size_t N = ImplTraits_N<Impl>;
using F = ImplTraits_F<Impl>;

#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
using Base::operator*;
using Base::operator*=;
using Base::operator/;
Expand Down Expand Up @@ -47,19 +47,19 @@ namespace Ubpa {
const auto& x = static_cast<const Impl&>(*this);
return x = x / k;
}
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD

private:
template<typename Base, typename Impl>
friend struct IScalarMul;

inline Impl impl_scalar_mul(F k) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>)
return _mm_mul_ps(x, Impl{ k });
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
{
Impl rst;
for (size_t i = 0; i < N; i++)
Expand All @@ -70,11 +70,11 @@ namespace Ubpa {

inline Impl& impl_scalar_mul_to_self(F k) noexcept {
auto& x = static_cast<Impl&>(*this);
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>)
return x = x * k;
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
{
for (size_t i = 0; i < N; i++)
x[i] *= k;
Expand Down
8 changes: 4 additions & 4 deletions include/UGM/Interfaces/IArray/IArrayUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ namespace Ubpa {

static const Impl lerp(const Impl& x, const Impl& y, F t) noexcept {
F one_minus_t = static_cast<F>(1) - t;
#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>)
return _mm_add_ps(_mm_mul_ps(x, _mm_set1_ps(one_minus_t)), _mm_mul_ps(y, _mm_set1_ps(t)));
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
{
Impl rst;
for (size_t i = 0; i < N; i++)
Expand All @@ -65,7 +65,7 @@ namespace Ubpa {
auto val_iter = vals.begin();
auto weight_iter = weights.begin();

#ifdef UBPA_USE_SIMD
#ifdef UBPA_UGM_USE_SIMD
if constexpr (ImplTraits_SupportSIMD<Impl>) {
__m128 rst = _mm_mul_ps(*val_iter, *weight_iter);
++val_iter;
Expand All @@ -78,7 +78,7 @@ namespace Ubpa {
return rst;
}
else
#endif // UBPA_USE_SIMD
#endif // UBPA_UGM_USE_SIMD
{
Impl rst;
for (size_t j = 0; j < N; j++)
Expand Down
Loading

0 comments on commit 50f56b6

Please sign in to comment.