diff --git a/headers/vmath.hpp/vmath_mat_fun.hpp b/headers/vmath.hpp/vmath_mat_fun.hpp index d9013ff..8a1cff7 100644 --- a/headers/vmath.hpp/vmath_mat_fun.hpp +++ b/headers/vmath.hpp/vmath_mat_fun.hpp @@ -199,6 +199,13 @@ namespace vmath_hpp return map_join([](const vec& x){ return -x; }, xs); } + // ~operator + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator~(const mat& xs) { + return map_join([](const vec& x){ return ~x; }, xs); + } + // !operator template < typename T, std::size_t Size > @@ -227,12 +234,12 @@ namespace vmath_hpp template < typename T, std::size_t Size > constexpr mat& operator+=(mat& xs, T y) { - return (xs = xs + y); + return (xs = (xs + y)); } template < typename T, std::size_t Size > constexpr mat& operator+=(mat& xs, const mat& ys) { - return (xs = xs + ys); + return (xs = (xs + ys)); } // operator- @@ -256,12 +263,12 @@ namespace vmath_hpp template < typename T, std::size_t Size > constexpr mat& operator-=(mat& xs, T y) { - return (xs = xs - y); + return (xs = (xs - y)); } template < typename T, std::size_t Size > constexpr mat& operator-=(mat& xs, const mat& ys) { - return (xs = xs - ys); + return (xs = (xs - ys)); } // operator* @@ -294,17 +301,17 @@ namespace vmath_hpp template < typename T, std::size_t Size > constexpr mat& operator*=(mat& xs, T y) { - return (xs = xs * y); + return (xs = (xs * y)); } template < typename T, std::size_t Size > constexpr vec& operator*=(vec& xs, const mat& ys) { - return (xs = xs * ys); + return (xs = (xs * ys)); } template < typename T, std::size_t Size > constexpr mat& operator*=(mat& xs, const mat& ys) { - return (xs = xs * ys); + return (xs = (xs * ys)); } // operator/ @@ -328,12 +335,133 @@ namespace vmath_hpp template < typename T, std::size_t Size > constexpr mat& operator/=(mat& xs, T y) { - return (xs = xs / y); + return (xs = (xs / y)); } template < typename T, std::size_t Size > constexpr mat& operator/=(mat& xs, const mat& ys) { - return (xs = xs / ys); + return (xs = (xs / ys)); + } + + // operator& + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator&(const mat& xs, T y) { + return map_join([y](const vec& x){ return x & y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator&(T x, const mat& ys) { + return map_join([x](const vec& y){ return x & y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator&(const mat& xs, const mat& ys) { + return map_join([](const vec& x, const vec& y){ return x & y; }, xs, ys); + } + + // operator&= + + template < typename T, std::size_t Size > + constexpr mat& operator&=(mat& xs, T y) { + return (xs = (xs & y)); + } + + template < typename T, std::size_t Size > + constexpr mat& operator&=(mat& xs, const mat& ys) { + return (xs = (xs & ys)); + } + + // operator| + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator|(const mat& xs, T y) { + return map_join([y](const vec& x){ return x | y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator|(T x, const mat& ys) { + return map_join([x](const vec& y){ return x | y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator|(const mat& xs, const mat& ys) { + return map_join([](const vec& x, const vec& y){ return x | y; }, xs, ys); + } + + // operator|= + + template < typename T, std::size_t Size > + constexpr mat& operator|=(mat& xs, T y) { + return (xs = (xs | y)); + } + + template < typename T, std::size_t Size > + constexpr mat& operator|=(mat& xs, const mat& ys) { + return (xs = (xs | ys)); + } + + // operator^ + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator^(const mat& xs, T y) { + return map_join([y](const vec& x){ return x ^ y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator^(T x, const mat& ys) { + return map_join([x](const vec& y){ return x ^ y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator^(const mat& xs, const mat& ys) { + return map_join([](const vec& x, const vec& y){ return x ^ y; }, xs, ys); + } + + // operator^= + + template < typename T, std::size_t Size > + constexpr mat& operator^=(mat& xs, T y) { + return (xs = (xs ^ y)); + } + + template < typename T, std::size_t Size > + constexpr mat& operator^=(mat& xs, const mat& ys) { + return (xs = (xs ^ ys)); + } + + // operator&& + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator&&(const mat& xs, T y) { + return map_join([y](const vec& x){ return x && y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator&&(T x, const mat& ys) { + return map_join([x](const vec& y){ return x && y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator&&(const mat& xs, const mat& ys) { + return map_join([](const vec& x, const vec& y){ return x && y; }, xs, ys); + } + + // operator|| + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator||(const mat& xs, T y) { + return map_join([y](const vec& x){ return x || y; }, xs); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator||(T x, const mat& ys) { + return map_join([x](const vec& y){ return x || y; }, ys); + } + + template < typename T, std::size_t Size > + [[nodiscard]] constexpr mat operator||(const mat& xs, const mat& ys) { + return map_join([](const vec& x, const vec& y){ return x || y; }, xs, ys); } // operator== diff --git a/untests/vmath_mat_fun_tests.cpp b/untests/vmath_mat_fun_tests.cpp index 6ceeac9..db4e2c3 100644 --- a/untests/vmath_mat_fun_tests.cpp +++ b/untests/vmath_mat_fun_tests.cpp @@ -59,17 +59,28 @@ TEST_CASE("vmath/mat_fun") { SUBCASE("operators") { STATIC_REQUIRE(-int2x2(1,2,3,4) == int2x2(-1,-2,-3,-4)); + STATIC_REQUIRE(~uint2x2(0xF0F0F0F0,0x0F0F0F0F,0xF0F0F0F0,0x0F0F0F0F) == uint2x2(0x0F0F0F0F,0xF0F0F0F0,0x0F0F0F0F,0xF0F0F0F0)); STATIC_REQUIRE(!int2x2(-1,0,1,2) == bool2x2(false,true,false,false)); STATIC_REQUIRE(int2x2(1,2,3,4) + 2 == int2x2(3,4,5,6)); STATIC_REQUIRE(int2x2(1,2,3,4) - 2 == int2x2(-1,0,1,2)); STATIC_REQUIRE(int2x2(1,2,3,4) * 2 == int2x2(2,4,6,8)); STATIC_REQUIRE(int2x2(1,2,3,4) / 2 == int2x2(0,1,1,2)); + STATIC_REQUIRE((int2x2(11,12,11,12) & 6) == int2x2(2,4,2,4)); + STATIC_REQUIRE((int2x2(11,12,11,12) | 6) == int2x2(15,14,15,14)); + STATIC_REQUIRE((int2x2(11,12,11,12) ^ 6) == int2x2(13,10,13,10)); + STATIC_REQUIRE((int2x2(1,0,1,0) && 1) == bool2x2(1,0,1,0)); + STATIC_REQUIRE((int2x2(1,0,1,0) || 1) == bool2x2(1,1,1,1)); STATIC_REQUIRE(4 + int2x2(1,2,3,4) == int2x2(5,6,7,8)); STATIC_REQUIRE(4 - int2x2(1,2,3,4) == int2x2(3,2,1,0)); STATIC_REQUIRE(4 * int2x2(1,2,3,4) == int2x2(4,8,12,16)); STATIC_REQUIRE(4 / int2x2(1,2,3,4) == int2x2(4,2,1,1)); + STATIC_REQUIRE((6 &int2x2(11,12,11,12)) == int2x2(2,4,2,4)); + STATIC_REQUIRE((6 |int2x2(11,12,11,12)) == int2x2(15,14,15,14)); + STATIC_REQUIRE((6 ^ int2x2(11,12,11,12)) == int2x2(13,10,13,10)); + STATIC_REQUIRE((1 && int2x2(1,0,1,0)) == bool2x2(1,0,1,0)); + STATIC_REQUIRE((1 || int2x2(1,0,1,0)) == bool2x2(1,1,1,1)); STATIC_REQUIRE(int2x2(1,2,3,4) + int2x2(5,6,7,8) == int2x2(6,8,10,12)); STATIC_REQUIRE(int2x2(1,2,3,4) - int2x2(5,6,7,8) == int2x2(-4,-4,-4,-4)); @@ -82,6 +93,12 @@ TEST_CASE("vmath/mat_fun") { STATIC_REQUIRE(int3(1,2,3) * int3x3() == int3(1,2,3)); STATIC_REQUIRE(int4(1,2,3,4) * int4x4() == int4(1,2,3,4)); + STATIC_REQUIRE((int2x2(6,7,6,7) & int2x2(11,12,11,12)) == int2x2(2,4,2,4)); + STATIC_REQUIRE((int2x2(6,7,6,7) | int2x2(11,12,11,12)) == int2x2(15,15,15,15)); + STATIC_REQUIRE((int2x2(6,7,6,7) ^ int2x2(11,12,11,12)) == int2x2(13,11,13,11)); + STATIC_REQUIRE((int2x2(0,1,0,1) && int2x2(1,0,1,0)) == bool2x2(0,0,0,0)); + STATIC_REQUIRE((int2x2(0,1,0,1) || int2x2(1,0,1,0)) == bool2x2(1,1,1,1)); + { int2x2 v{1,2,3,4}; REQUIRE(&v == &(v += 3)); @@ -129,6 +146,30 @@ TEST_CASE("vmath/mat_fun") { REQUIRE(&v == &(v *= int3x3(scale(int3{2,3,4})))); REQUIRE(v == int3x3(scale(int3{2,6,12}))); } + { + int2x2 v1{11,12,11,12}; + REQUIRE(&v1 == &(v1 &= 6)); + REQUIRE(v1 == int2x2(2,4,2,4)); + int2x2 v2{6,7,6,7}; + REQUIRE(&v2 == &(v2 &= int2x2(11,12,11,12))); + REQUIRE(v2 == int2x2(2,4,2,4)); + } + { + int2x2 v1{11,12,11,12}; + REQUIRE(&v1 == &(v1 |= 6)); + REQUIRE(v1 == int2x2(15,14,15,14)); + int2x2 v2{6,7,6,7}; + REQUIRE(&v2 == &(v2 |= int2x2(11,12,11,12))); + REQUIRE(v2 == int2x2(15,15,15,15)); + } + { + int2x2 v1{11,12,11,12}; + REQUIRE(&v1 == &(v1 ^= 6)); + REQUIRE(v1 == int2x2(13,10,13,10)); + int2x2 v2{6,7,6,7}; + REQUIRE(&v2 == &(v2 ^= int2x2(11,12,11,12))); + REQUIRE(v2 == int2x2(13,11,13,11)); + } } SUBCASE("transpose") {