Skip to content

Commit

Permalink
compare
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubpa committed May 27, 2020
1 parent a8f5490 commit 4ad0755
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(UGM VERSION 0.6.1)
project(UGM VERSION 0.6.2)
message(STATUS "[Project] ${PROJECT_NAME}")

include(FetchContent)
Expand Down
109 changes: 103 additions & 6 deletions include/UGM/Interfaces/IArray/IArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ namespace Ubpa {
template<typename... Us, std::enable_if_t<sizeof...(Us) == N>* = nullptr>
constexpr IArray_Impl(Us... vals) noexcept : std::array<T, N>{static_cast<T>(vals)...} {}

static Impl zero() noexcept {
return Impl{ T{static_cast<F>(0)} };
}

template<size_t i>
T get() const noexcept {
static_assert(i < N);
Expand Down Expand Up @@ -152,22 +156,111 @@ namespace Ubpa {
const float* data() const noexcept { return const_cast<IArray_Impl*>(this)->data(); }

friend bool operator==(const Impl& x, const Impl& y) noexcept {
return _mm_movemask_ps(_mm_cmpeq_ps(x, y)) == 0xf; // 1111
return _mm_movemask_ps(_mm_cmpeq_ps(x, y)) == 0xf;
}
friend bool operator!=(const Impl& x, const Impl& y) noexcept {
return _mm_movemask_ps(_mm_cmpneq_ps(x, y)) == 0xf; // 1111
return _mm_movemask_ps(_mm_cmpneq_ps(x, y)) == 0xf;
}

friend bool operator<(const Impl& x, const Impl& y) noexcept {
return _mm_movemask_ps(_mm_cmplt_ps(x, y)) == 0xf; // 1111
int mask_lt = _mm_movemask_ps(_mm_cmplt_ps(x, y));
int mask_eq = _mm_movemask_ps(_mm_cmpeq_ps(x, y));
for (size_t i = 0; i < 4; i++) {
int cur_bit = (1 << i);
if (mask_eq & cur_bit)
continue;
if (mask_lt & cur_bit)
return true;
else
return false;
}
return false;
}
friend bool operator<=(const Impl& x, const Impl& y) noexcept {
return _mm_movemask_ps(_mm_cmple_ps(x, y)) == 0xf; // 1111
int mask_lt = _mm_movemask_ps(_mm_cmplt_ps(x, y));
int mask_eq = _mm_movemask_ps(_mm_cmpeq_ps(x, y));
for (size_t i = 0; i < 4; i++) {
int cur_bit = (1 << i);
if (mask_eq & cur_bit)
continue;
if (mask_lt & cur_bit)
return true;
else
return false;
}
return true;
}
friend bool operator>(const Impl& x, const Impl& y) noexcept {
return _mm_movemask_ps(_mm_cmpgt_ps(x, y)) == 0xf; // 1111
int mask_lt = _mm_movemask_ps(_mm_cmplt_ps(x, y));
int mask_eq = _mm_movemask_ps(_mm_cmpeq_ps(x, y));
for (size_t i = 0; i < 4; i++) {
int cur_bit = (1 << i);
if (mask_eq & cur_bit)
continue;
if (mask_lt & cur_bit)
return false;
else
return true;
}
return false;
}
friend bool operator>=(const Impl& x, const Impl& y) noexcept {
return _mm_movemask_ps(_mm_cmpge_ps(x, y)) == 0xf; // 1111
int mask_lt = _mm_movemask_ps(_mm_cmplt_ps(x, y));
int mask_eq = _mm_movemask_ps(_mm_cmpeq_ps(x, y));
for (size_t i = 0; i < 4; i++) {
int cur_bit = (1 << i);
if (mask_eq & cur_bit)
continue;
if (mask_lt & cur_bit)
return false;
else
return true;
}
return true;
}
bool lex_lt(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return lex_lt(x, y);
}
bool lex_le(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return lex_le(x, y);
}
bool lex_gt(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return lex_gt(x, y);
}
bool lex_ge(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return lex_ge(x, y);
}
static bool all_lt(const Impl& x, const Impl& y) noexcept {
return _mm_movemask_ps(_mm_cmplt_ps(x, y)) == 0xf;
}
static bool all_le(const Impl& x, const Impl& y) noexcept {
return _mm_movemask_ps(_mm_cmple_ps(x, y)) == 0xf;
}
static bool all_gt(const Impl& x, const Impl& y) noexcept {
return _mm_movemask_ps(_mm_cmpgt_ps(x, y)) == 0xf;
}
static bool all_ge(const Impl& x, const Impl& y) noexcept {
return _mm_movemask_ps(_mm_cmpge_ps(x, y)) == 0xf;
}
bool all_lt(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return all_lt(x, y);
}
bool all_le(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return all_le(x, y);
}
bool all_gt(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return all_gt(x, y);
}
bool all_ge(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return all_ge(x, y);
}

// ==================
Expand All @@ -185,6 +278,10 @@ namespace Ubpa {
IArray_Impl(Ux x, Uy y, Uz z, Uw w) noexcept
: IArray_Impl{ static_cast<float>(x),static_cast<float>(y),static_cast<float>(z),static_cast<float>(w) } {}

static Impl zero() noexcept {
return _mm_setzero_ps();
}

template<size_t i>
float get() const noexcept {
static_assert(i < 4);
Expand Down
4 changes: 3 additions & 1 deletion include/UGM/Interfaces/IArray/IArrayCast.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ namespace Ubpa {

template<typename To>
To& as() & noexcept {
static_assert(sizeof(To) == sizeof(Impl) && std::is_same_v<typename To::T, ImplTraits_T<Impl>>);
static_assert(sizeof(To) == sizeof(Impl)
&& std::is_same_v<ImplTraits_T<To>, ImplTraits_T<Impl>>
&& alignof(Impl) == alignof(To));
return reinterpret_cast<To&>(*this);
}

Expand Down
116 changes: 116 additions & 0 deletions include/UGM/Interfaces/IArray/IEuclideanV.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,122 @@ namespace Ubpa {
const auto& x = static_cast<const Impl&>(*this);
return x - x.v3_project(n);
}

static bool v3_eq(const Impl& x, const Impl& y) noexcept {
return (_mm_movemask_ps(_mm_cmpeq_ps(x, y)) & 0x7) == 0x7; // 0111
}
static bool v3_neq(const Impl& x, const Impl& y) noexcept {
return (_mm_movemask_ps(_mm_cmpneq_ps(x, y)) & 0x7) == 0x7; // 0111
}
bool v3_eq(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return v3_eq(x, y);
}
bool v3_neq(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return v3_neq(x, y);
}

static bool v3_lex_lt(const Impl& x, const Impl& y) noexcept {
int mask_lt = _mm_movemask_ps(_mm_cmplt_ps(x, y));
int mask_eq = _mm_movemask_ps(_mm_cmpeq_ps(x, y));
for (size_t i = 0; i < 3; i++) {
int cur_bit = (1 << i);
if (mask_eq & cur_bit)
continue;
if (mask_lt & cur_bit)
return true;
else
return false;
}
return false;
}
static bool v3_lex_le(const Impl& x, const Impl& y) noexcept {
int mask_lt = _mm_movemask_ps(_mm_cmplt_ps(x, y));
int mask_eq = _mm_movemask_ps(_mm_cmpeq_ps(x, y));
for (size_t i = 0; i < 3; i++) {
int cur_bit = (1 << i);
if (mask_eq & cur_bit)
continue;
if (mask_lt & cur_bit)
return true;
else
return false;
}
return true;
}
static bool v3_lex_gt(const Impl& x, const Impl& y) noexcept {
int mask_lt = _mm_movemask_ps(_mm_cmplt_ps(x, y));
int mask_eq = _mm_movemask_ps(_mm_cmpeq_ps(x, y));
for (size_t i = 0; i < 3; i++) {
int cur_bit = (1 << i);
if (mask_eq & cur_bit)
continue;
if (mask_lt & cur_bit)
return false;
else
return true;
}
return false;
}
static bool v3_lex_ge(const Impl& x, const Impl& y) noexcept {
int mask_lt = _mm_movemask_ps(_mm_cmplt_ps(x, y));
int mask_eq = _mm_movemask_ps(_mm_cmpeq_ps(x, y));
for (size_t i = 0; i < 3; i++) {
int cur_bit = (1 << i);
if (mask_eq & cur_bit)
continue;
if (mask_lt & cur_bit)
return false;
else
return true;
}
return true;
}
bool v3_lex_lt(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return v3_lex_lt(x, y);
}
bool v3_lex_le(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return v3_lex_le(x, y);
}
bool v3_lex_gt(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return v3_lex_gt(x, y);
}
bool v3_lex_ge(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return v3_lex_ge(x, y);
}
static bool v3_all_lt(const Impl& x, const Impl& y) noexcept {
return (_mm_movemask_ps(_mm_cmplt_ps(x, y)) & 0x7) == 0x7; // 0111
}
static bool v3_all_le(const Impl& x, const Impl& y) noexcept {
return (_mm_movemask_ps(_mm_cmple_ps(x, y)) & 0x7) == 0x7; // 0111
}
static bool v3_all_gt(const Impl& x, const Impl& y) noexcept {
return (_mm_movemask_ps(_mm_cmpgt_ps(x, y)) & 0x7) == 0x7; // 0111
}
static bool v3_all_ge(const Impl& x, const Impl& y) noexcept {
return (_mm_movemask_ps(_mm_cmpge_ps(x, y)) & 0x7) == 0x7; // 0111
}
bool v3_all_lt(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return v3_all_lt(x, y);
}
bool v3_all_le(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return v3_all_le(x, y);
}
bool v3_all_gt(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return v3_all_gt(x, y);
}
bool v3_all_ge(const Impl& y) const noexcept {
const auto& x = static_cast<const Impl&>(*this);
return v3_all_ge(x, y);
}
#endif

private:
Expand Down
21 changes: 20 additions & 1 deletion src/test/12_v3/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,39 @@ using namespace std;
int main(int argc, char* argv[])
{
vecf4 f0{ 1,2,3,4 };
vecf4 f1{ 4,3,2,1 };
vecf4 f1{ 5,4,3,2 };

cout << f0.is_scalar() << endl;
cout << vecf4{ 1,1,1,1 }.is_scalar() << endl;

cout << f0.v3_cos_theta(f1) << endl;

cout << f0.v3_cross(f1) << endl;

cout << f0.v3_distance(f1) << endl;
cout << f0.v3_distance2(f1) << endl;

cout << f0.v3_dot(f1) << endl;

cout << f0.v3_is_normalized() << endl;
cout << f0.v3_norm() << endl;
cout << f0.v3_norm2() << endl;
cout << f0.v3_normalize() << endl;
cout << f0.v3_normalize_self() << endl;

cout << f0.v3_perpendicular({ 0,1,0,0 }) << endl;
cout << f0.v3_project({ 0,1,0,0 }) << endl;

cout << f0.v3_eq(f1) << endl;
cout << f0.v3_neq(f1) << endl;
cout << f0.v3_all_lt(f1) << endl;
cout << f0.v3_all_le(f1) << endl;
cout << f0.v3_all_gt(f1) << endl;
cout << f0.v3_all_ge(f1) << endl;
cout << f0.v3_lex_lt(f1) << endl;
cout << f0.v3_lex_le(f1) << endl;
cout << f0.v3_lex_gt(f1) << endl;
cout << f0.v3_lex_ge(f1) << endl;

return 0;
}

0 comments on commit 4ad0755

Please sign in to comment.