diff --git a/headers/vmath.hpp/vmath.hpp b/headers/vmath.hpp/vmath.hpp index 25faaf7..770ce34 100644 --- a/headers/vmath.hpp/vmath.hpp +++ b/headers/vmath.hpp/vmath.hpp @@ -8,5 +8,6 @@ #include "vmath_fun.hpp" #include "vmath_mat.hpp" +#include "vmath_mat_fun.hpp" #include "vmath_vec.hpp" #include "vmath_vec_fun.hpp" diff --git a/headers/vmath.hpp/vmath_fun.hpp b/headers/vmath.hpp/vmath_fun.hpp index de6146e..7992d8b 100644 --- a/headers/vmath.hpp/vmath_fun.hpp +++ b/headers/vmath.hpp/vmath_fun.hpp @@ -71,7 +71,7 @@ namespace vmath_hpp template < typename T > T sign(T x) noexcept { - return (T(0) < x) - (x < T(0)); + return static_cast((T(0) < x) - (x < T(0))); } using ::std::floor; diff --git a/headers/vmath.hpp/vmath_mat.hpp b/headers/vmath.hpp/vmath_mat.hpp index 67bf6b6..d9f3972 100644 --- a/headers/vmath.hpp/vmath_mat.hpp +++ b/headers/vmath.hpp/vmath_mat.hpp @@ -7,7 +7,9 @@ #pragma once #include "vmath_fwd.hpp" + #include "vmath_vec.hpp" +#include "vmath_vec_fun.hpp" namespace vmath_hpp::detail { @@ -220,34 +222,3 @@ namespace vmath_hpp l.swap(r); } } - -namespace vmath_hpp -{ - template < typename T, std::size_t Size > - constexpr bool operator==(const mat& l, const mat& r) { - for ( std::size_t i = 0; i < Size; ++i ) { - if ( !(l[i] == r[i]) ) { - return false; - } - } - return true; - } - - template < typename T, std::size_t Size > - constexpr bool operator!=(const mat& l, const mat& r) { - return !(l == r); - } - - template < typename T, std::size_t Size > - constexpr bool operator<(const mat& l, const mat& r) { - for ( std::size_t i = 0; i < Size; ++i ) { - if ( l[i] < r[i] ) { - return true; - } - if ( r[i] < l[i] ) { - return false; - } - } - return false; - } -} diff --git a/headers/vmath.hpp/vmath_mat_fun.hpp b/headers/vmath.hpp/vmath_mat_fun.hpp new file mode 100644 index 0000000..c194f7a --- /dev/null +++ b/headers/vmath.hpp/vmath_mat_fun.hpp @@ -0,0 +1,47 @@ +/******************************************************************************* + * This file is part of the "https://github.com/blackmatov/vmath.hpp" + * For conditions of distribution and use, see copyright notice in LICENSE.md + * Copyright (C) 2020, by Matvey Cherevko (blackmatov@gmail.com) + ******************************************************************************/ + +#pragma once + +#include "vmath_fwd.hpp" + +#include "vmath_fun.hpp" +#include "vmath_mat.hpp" + +// +// Operators +// + +namespace vmath_hpp +{ + template < typename T, std::size_t Size > + constexpr bool operator==(const mat& l, const mat& r) { + for ( std::size_t i = 0; i < Size; ++i ) { + if ( !(l[i] == r[i]) ) { + return false; + } + } + return true; + } + + template < typename T, std::size_t Size > + constexpr bool operator!=(const mat& l, const mat& r) { + return !(l == r); + } + + template < typename T, std::size_t Size > + constexpr bool operator<(const mat& l, const mat& r) { + for ( std::size_t i = 0; i < Size; ++i ) { + if ( l[i] < r[i] ) { + return true; + } + if ( r[i] < l[i] ) { + return false; + } + } + return false; + } +} diff --git a/headers/vmath.hpp/vmath_vec.hpp b/headers/vmath.hpp/vmath_vec.hpp index 2f2aafd..5f14307 100644 --- a/headers/vmath.hpp/vmath_vec.hpp +++ b/headers/vmath.hpp/vmath_vec.hpp @@ -8,18 +8,6 @@ #include "vmath_fwd.hpp" -#if defined(__GNUC__) -# pragma GCC diagnostic push -# pragma GCC diagnostic ignored "-Wpedantic" -#elif defined(__clang__) -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wgnu-anonymous-struct" -# pragma clang diagnostic ignored "-Wnested-anon-types" -#elif defined(_MSC_VER) -# pragma warning(push) -# pragma warning(disable: 4201) -#endif - namespace vmath_hpp::detail { template < typename T, std::size_t Size > @@ -28,13 +16,9 @@ namespace vmath_hpp::detail template < typename T > class vec_base { public: - union { - struct { T x, y; }; - T data[2]; - }; + T x{}, y{}; public: - constexpr vec_base() - : x{0}, y{0} {} + vec_base() = default; constexpr explicit vec_base(T v) : x{v}, y{v} {} @@ -43,10 +27,10 @@ namespace vmath_hpp::detail : x{x}, y{y} {} constexpr explicit vec_base(const vec_base& xy) - : x{xy.x}, y{xy.y} {} + : x{xy[0]}, y{xy[1]} {} constexpr explicit vec_base(const vec_base& xy) - : x{xy.x}, y{xy.y} {} + : x{xy[0]}, y{xy[1]} {} constexpr T& operator[](std::size_t index) noexcept { switch ( index ) { @@ -68,13 +52,9 @@ namespace vmath_hpp::detail template < typename T > class vec_base { public: - union { - struct { T x, y, z; }; - T data[3]; - }; + T x{}, y{}, z{}; public: - constexpr vec_base() - : x{0}, y{0}, z{0} {} + vec_base() = default; constexpr explicit vec_base(T v) : x{v}, y{v}, z{v} {} @@ -83,13 +63,13 @@ namespace vmath_hpp::detail : x{x}, y{y}, z{z} {} constexpr vec_base(const vec_base& xy, T z) - : x{xy.x}, y{xy.y}, z{z} {} + : x{xy[0]}, y{xy[1]}, z{z} {} constexpr vec_base(T x, const vec_base& yz) - : x{x}, y{yz.x}, z{yz.y} {} + : x{x}, y{yz[0]}, z{yz[1]} {} constexpr explicit vec_base(const vec_base& xyz) - : x{xyz.x}, y{xyz.y}, z{xyz.z} {} + : x{xyz[0]}, y{xyz[1]}, z{xyz[2]} {} constexpr T& operator[](std::size_t index) noexcept { switch ( index ) { @@ -113,13 +93,9 @@ namespace vmath_hpp::detail template < typename T > class vec_base { public: - union { - struct { T x, y, z, w; }; - T data[4]; - }; + T x{}, y{}, z{}, w{}; public: - constexpr vec_base() - : x{0}, y{0}, z{0}, w{0} {} + vec_base() = default; constexpr explicit vec_base(T v) : x{v}, y{v}, z{v}, w{v} {} @@ -128,22 +104,22 @@ namespace vmath_hpp::detail : x{x}, y{y}, z{z}, w{w} {} constexpr vec_base(const vec_base& xy, T z, T w) - : x{xy.x}, y{xy.y}, z{z}, w{w} {} + : x{xy[0]}, y{xy[1]}, z{z}, w{w} {} constexpr vec_base(T x, const vec_base& yz, T w) - : x{x}, y{yz.x}, z{yz.y}, w{w} {} + : x{x}, y{yz[0]}, z{yz[1]}, w{w} {} constexpr vec_base(T x, T y, const vec_base& zw) - : x{x}, y{y}, z{zw.x}, w{zw.y} {} + : x{x}, y{y}, z{zw[0]}, w{zw[1]} {} constexpr vec_base(const vec_base& xy, const vec_base& zw) - : x{xy.x}, y{xy.y}, z{zw.x}, w{zw.y} {} + : x{xy[0]}, y{xy[1]}, z{zw[0]}, w{zw[1]} {} constexpr vec_base(const vec_base& xyz, T w) - : x{xyz.x}, y{xyz.y}, z{xyz.z}, w{w} {} + : x{xyz[0]}, y{xyz[1]}, z{xyz[2]}, w{w} {} constexpr vec_base(T x, const vec_base& yzw) - : x{x}, y{yzw.x}, z{yzw.y}, w{yzw.z} {} + : x{x}, y{yzw[0]}, z{yzw[1]}, w{yzw[2]} {} constexpr T& operator[](std::size_t index) noexcept { switch ( index ) { @@ -167,14 +143,6 @@ namespace vmath_hpp::detail }; } -#if defined(__GNUC__) -# pragma GCC diagnostic pop -#elif defined(__clang__) -# pragma clang diagnostic pop -#elif defined(_MSC_VER) -# pragma warning(pop) -#endif - namespace vmath_hpp { template < typename T, std::size_t Size > @@ -231,34 +199,3 @@ namespace vmath_hpp l.swap(r); } } - -namespace vmath_hpp -{ - template < typename T, std::size_t Size > - constexpr bool operator==(const vec& l, const vec& r) { - for ( std::size_t i = 0; i < Size; ++i ) { - if ( !(l[i] == r[i]) ) { - return false; - } - } - return true; - } - - template < typename T, std::size_t Size > - constexpr bool operator!=(const vec& l, const vec& r) { - return !(l == r); - } - - template < typename T, std::size_t Size > - constexpr bool operator<(const vec& l, const vec& r) { - for ( std::size_t i = 0; i < Size; ++i ) { - if ( l[i] < r[i] ) { - return true; - } - if ( r[i] < l[i] ) { - return false; - } - } - return false; - } -} diff --git a/headers/vmath.hpp/vmath_vec_fun.hpp b/headers/vmath.hpp/vmath_vec_fun.hpp index c605411..9cad825 100644 --- a/headers/vmath.hpp/vmath_vec_fun.hpp +++ b/headers/vmath.hpp/vmath_vec_fun.hpp @@ -145,6 +145,33 @@ namespace vmath_hpp constexpr vec operator/(const vec& xs, const vec& ys) { return zip(std::divides<>(), xs, ys); } + + // operator== + + template < typename T, std::size_t Size > + constexpr bool operator==(const vec& xs, const vec& ys) { + return all(zip(std::equal_to<>(), xs, ys)); + } + + template < typename T, std::size_t Size > + constexpr bool operator!=(const vec& xs, const vec& ys) { + return any(zip(std::not_equal_to<>(), xs, ys)); + } + + // operator< + + template < typename T, std::size_t Size > + constexpr bool operator<(const vec& l, const vec& r) { + for ( std::size_t i = 0; i < Size; ++i ) { + if ( l[i] < r[i] ) { + return true; + } + if ( r[i] < l[i] ) { + return false; + } + } + return false; + } } // @@ -424,7 +451,7 @@ namespace vmath_hpp { template < typename T, std::size_t Size, std::size_t... Is > vec frexp_impl(const vec& xs, vec* exps, std::index_sequence) { - return { frexp(xs.data[Is], &exps->data[Is])... }; + return { frexp(xs[Is], &(*exps)[Is])... }; } } @@ -499,46 +526,46 @@ namespace vmath_hpp { template < typename T, std::size_t Size > constexpr vec less(const vec& x, const vec& y) { - return detail::zip(std::less<>(), x, y); + return zip(std::less<>(), x, y); } template < typename T, std::size_t Size > constexpr vec less_equal(const vec& x, const vec& y) { - return detail::zip(std::less_equal<>(), x, y); + return zip(std::less_equal<>(), x, y); } template < typename T, std::size_t Size > constexpr vec greater(const vec& x, const vec& y) { - return detail::zip(std::greater<>(), x, y); + return zip(std::greater<>(), x, y); } template < typename T, std::size_t Size > constexpr vec greater_equal(const vec& x, const vec& y) { - return detail::zip(std::greater_equal<>(), x, y); + return zip(std::greater_equal<>(), x, y); } template < typename T, std::size_t Size > constexpr vec equal_to(const vec& x, const vec& y) { - return detail::zip(std::equal_to<>(), x, y); + return zip(std::equal_to<>(), x, y); } template < typename T, std::size_t Size > constexpr vec not_equal_to(const vec& x, const vec& y) { - return detail::zip(std::not_equal_to<>(), x, y); + return zip(std::not_equal_to<>(), x, y); } template < std::size_t Size > constexpr bool any(const vec& x) { - return detail::fold(std::logical_or<>(), false, x); + return fold(std::logical_or<>(), false, x); } template < std::size_t Size > constexpr bool all(const vec& x) { - return detail::fold(std::logical_and<>(), true, x); + return fold(std::logical_and<>(), true, x); } template < std::size_t Size > constexpr vec not_(const vec& x) { - return detail::map(std::logical_not<>(), x); + return map(std::logical_not<>(), x); } } diff --git a/untests/vmath_fun_tests.cpp b/untests/vmath_fun_tests.cpp index 715bc58..ca23017 100644 --- a/untests/vmath_fun_tests.cpp +++ b/untests/vmath_fun_tests.cpp @@ -33,32 +33,32 @@ TEST_CASE("vmath/fun") { STATIC_REQUIRE(radians(degrees(12.13f)) == approx(12.13f)); STATIC_REQUIRE(degrees(radians(12.13f)) == approx(12.13f)); - sin(0.f); - cos(0.f); - tan(0.f); + (void)sin(0.f); + (void)cos(0.f); + (void)tan(0.f); - asin(0.f); - acos(0.f); - atan(0.f); - atan2(0.f, 0.f); + (void)asin(0.f); + (void)acos(0.f); + (void)atan(0.f); + (void)atan2(0.f, 0.f); - sinh(0.f); - cosh(0.f); - tanh(0.f); + (void)sinh(0.f); + (void)cosh(0.f); + (void)tanh(0.f); - asinh(0.f); - acosh(0.f); - atanh(0.f); + (void)asinh(0.f); + (void)acosh(0.f); + (void)atanh(0.f); } SECTION("Exponential Functions") { - pow(2.f, 3.f); - exp(2.f); - log(2.f); - exp2(2.f); - log2(2.f); - sqrt(2.f); - invsqrt(2.f); + (void)pow(2.f, 3.f); + (void)exp(2.f); + (void)log(2.f); + (void)exp2(2.f); + (void)log2(2.f); + (void)sqrt(2.f); + (void)invsqrt(2.f); } SECTION("Common Functions") { diff --git a/untests/vmath_mat_fun_tests.cpp b/untests/vmath_mat_fun_tests.cpp new file mode 100644 index 0000000..603955d --- /dev/null +++ b/untests/vmath_mat_fun_tests.cpp @@ -0,0 +1,18 @@ +/******************************************************************************* + * This file is part of the "https://github.com/blackmatov/vmath.hpp" + * For conditions of distribution and use, see copyright notice in LICENSE.md + * Copyright (C) 2020, by Matvey Cherevko (blackmatov@gmail.com) + ******************************************************************************/ + +#include + +#define CATCH_CONFIG_FAST_COMPILE +#include + +namespace +{ +} + +TEST_CASE("vmath/mat_fun") { + using namespace vmath_hpp; +} diff --git a/untests/vmath_mat_tests.cpp b/untests/vmath_mat_tests.cpp index 00ce90f..06860b9 100644 --- a/untests/vmath_mat_tests.cpp +++ b/untests/vmath_mat_tests.cpp @@ -5,6 +5,7 @@ ******************************************************************************/ #include +#include #define CATCH_CONFIG_FAST_COMPILE #include diff --git a/untests/vmath_vec_fun_tests.cpp b/untests/vmath_vec_fun_tests.cpp index b1b5730..c90660d 100644 --- a/untests/vmath_vec_fun_tests.cpp +++ b/untests/vmath_vec_fun_tests.cpp @@ -74,43 +74,43 @@ TEST_CASE("vmath/vec_fun") { STATIC_REQUIRE(radians(degrees(vec2f(12.13f))) == approx2(12.13f)); STATIC_REQUIRE(degrees(radians(vec2f(12.13f))) == approx2(12.13f)); - sin(vec2f(1.f)); - cos(vec2f(1.f)); - tan(vec2f(1.f)); + (void)sin(vec2f(1.f)); + (void)cos(vec2f(1.f)); + (void)tan(vec2f(1.f)); - asin(vec2f(1.f)); - acos(vec2f(1.f)); - atan(vec2f(1.f)); - atan2(vec2f(1.f), vec2f(1.f)); + (void)asin(vec2f(1.f)); + (void)acos(vec2f(1.f)); + (void)atan(vec2f(1.f)); + (void)atan2(vec2f(1.f), vec2f(1.f)); - sinh(vec2f(1.f)); - cosh(vec2f(1.f)); - tanh(vec2f(1.f)); + (void)sinh(vec2f(1.f)); + (void)cosh(vec2f(1.f)); + (void)tanh(vec2f(1.f)); - asinh(vec2f(1.f)); - acosh(vec2f(1.f)); - atanh(vec2f(1.f)); + (void)asinh(vec2f(1.f)); + (void)acosh(vec2f(1.f)); + (void)atanh(vec2f(1.f)); } SECTION("Exponential Functions") { - pow(vec2f(1.f), vec2f(2.f)); - exp(vec2f(1.f)); - log(vec2f(1.f)); - exp2(vec2f(1.f)); - log2(vec2f(1.f)); - sqrt(vec2f(1.f)); - invsqrt(vec2f(1.f)); + (void)pow(vec2f(1.f), vec2f(2.f)); + (void)exp(vec2f(1.f)); + (void)log(vec2f(1.f)); + (void)exp2(vec2f(1.f)); + (void)log2(vec2f(1.f)); + (void)sqrt(vec2f(1.f)); + (void)invsqrt(vec2f(1.f)); } SECTION("Common Functions") { REQUIRE(abs(vec2f(1.f, -1.f)) == approx2(1.f,1.f)); REQUIRE(sign(vec3f(1.f, -1.f, 0.f)) == approx3(1.f,-1.f,0.f)); - floor(vec2f(1.f, -1.f)); - trunc(vec2f(1.f, -1.f)); - round(vec2f(1.f, -1.f)); - ceil(vec2f(1.f, -1.f)); - fract(vec2f(1.f, -1.f)); + (void)floor(vec2f(1.f, -1.f)); + (void)trunc(vec2f(1.f, -1.f)); + (void)round(vec2f(1.f, -1.f)); + (void)ceil(vec2f(1.f, -1.f)); + (void)fract(vec2f(1.f, -1.f)); REQUIRE(fmod(vec2f(1.7f), 1.2f) == approx2(0.5f)); REQUIRE(fmod(vec2f(1.7f), vec2f(1.2f)) == approx2(0.5f)); diff --git a/untests/vmath_vec_tests.cpp b/untests/vmath_vec_tests.cpp index 8b0a985..5129d58 100644 --- a/untests/vmath_vec_tests.cpp +++ b/untests/vmath_vec_tests.cpp @@ -5,6 +5,7 @@ ******************************************************************************/ #include +#include #define CATCH_CONFIG_FAST_COMPILE #include