remove union data

This commit is contained in:
BlackMATov
2020-11-23 06:39:06 +07:00
parent ad55131316
commit a14f719e21
11 changed files with 170 additions and 167 deletions

View File

@@ -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"

View File

@@ -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>((T(0) < x) - (x < T(0)));
}
using ::std::floor;

View File

@@ -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<T, Size>& l, const mat<T, Size>& 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<T, Size>& l, const mat<T, Size>& r) {
return !(l == r);
}
template < typename T, std::size_t Size >
constexpr bool operator<(const mat<T, Size>& l, const mat<T, Size>& 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;
}
}

View File

@@ -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<T, Size>& l, const mat<T, Size>& 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<T, Size>& l, const mat<T, Size>& r) {
return !(l == r);
}
template < typename T, std::size_t Size >
constexpr bool operator<(const mat<T, Size>& l, const mat<T, Size>& 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;
}
}

View File

@@ -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<T, 2> {
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<T, 3>& xy)
: x{xy.x}, y{xy.y} {}
: x{xy[0]}, y{xy[1]} {}
constexpr explicit vec_base(const vec_base<T, 4>& 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<T, 3> {
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<T, 2>& 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<T, 2>& yz)
: x{x}, y{yz.x}, z{yz.y} {}
: x{x}, y{yz[0]}, z{yz[1]} {}
constexpr explicit vec_base(const vec_base<T, 4>& 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<T, 4> {
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<T, 2>& 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<T, 2>& 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<T, 2>& 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<T, 2>& xy, const vec_base<T, 2>& 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<T, 3>& 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<T, 3>& 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<T, Size>& l, const vec<T, Size>& 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<T, Size>& l, const vec<T, Size>& r) {
return !(l == r);
}
template < typename T, std::size_t Size >
constexpr bool operator<(const vec<T, Size>& l, const vec<T, Size>& 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;
}
}

View File

@@ -145,6 +145,33 @@ namespace vmath_hpp
constexpr vec<T, Size> operator/(const vec<T, Size>& xs, const vec<T, Size>& ys) {
return zip(std::divides<>(), xs, ys);
}
// operator==
template < typename T, std::size_t Size >
constexpr bool operator==(const vec<T, Size>& xs, const vec<T, Size>& ys) {
return all(zip(std::equal_to<>(), xs, ys));
}
template < typename T, std::size_t Size >
constexpr bool operator!=(const vec<T, Size>& xs, const vec<T, Size>& ys) {
return any(zip(std::not_equal_to<>(), xs, ys));
}
// operator<
template < typename T, std::size_t Size >
constexpr bool operator<(const vec<T, Size>& l, const vec<T, Size>& 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<T, Size> frexp_impl(const vec<T, Size>& xs, vec<int, Size>* exps, std::index_sequence<Is...>) {
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<bool, Size> less(const vec<T, Size>& x, const vec<T, Size>& y) {
return detail::zip(std::less<>(), x, y);
return zip(std::less<>(), x, y);
}
template < typename T, std::size_t Size >
constexpr vec<bool, Size> less_equal(const vec<T, Size>& x, const vec<T, Size>& 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<bool, Size> greater(const vec<T, Size>& x, const vec<T, Size>& y) {
return detail::zip(std::greater<>(), x, y);
return zip(std::greater<>(), x, y);
}
template < typename T, std::size_t Size >
constexpr vec<bool, Size> greater_equal(const vec<T, Size>& x, const vec<T, Size>& 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<bool, Size> equal_to(const vec<T, Size>& x, const vec<T, Size>& 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<bool, Size> not_equal_to(const vec<T, Size>& x, const vec<T, Size>& 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<bool, Size>& 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<bool, Size>& x) {
return detail::fold(std::logical_and<>(), true, x);
return fold(std::logical_and<>(), true, x);
}
template < std::size_t Size >
constexpr vec<bool, Size> not_(const vec<bool, Size>& x) {
return detail::map(std::logical_not<>(), x);
return map(std::logical_not<>(), x);
}
}

View File

@@ -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") {

View File

@@ -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 <vmath.hpp/vmath_mat_fun.hpp>
#define CATCH_CONFIG_FAST_COMPILE
#include <catch2/catch.hpp>
namespace
{
}
TEST_CASE("vmath/mat_fun") {
using namespace vmath_hpp;
}

View File

@@ -5,6 +5,7 @@
******************************************************************************/
#include <vmath.hpp/vmath_mat.hpp>
#include <vmath.hpp/vmath_mat_fun.hpp>
#define CATCH_CONFIG_FAST_COMPILE
#include <catch2/catch.hpp>

View File

@@ -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));

View File

@@ -5,6 +5,7 @@
******************************************************************************/
#include <vmath.hpp/vmath_vec.hpp>
#include <vmath.hpp/vmath_vec_fun.hpp>
#define CATCH_CONFIG_FAST_COMPILE
#include <catch2/catch.hpp>