Commit 770c41fe authored by Philip Trettner's avatar Philip Trettner
Browse files

more math operators

parent 4b2912ef
......@@ -14,3 +14,5 @@ source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCES})
add_library(typed-math INTERFACE)
target_include_directories(typed-math INTERFACE "src")
add_custom_target(typed-math-src SOURCES ${SOURCES})
......@@ -4,10 +4,15 @@
namespace tg
{
// software emulation of 16bit math
struct half
{
// TODO
private:
uint16_t data;
};
// TODO
constexpr half operator-(half const& v) { return v; }
constexpr bool operator<(half const& a, float b) { return true; }
} // namespace tg
......@@ -4,10 +4,15 @@
namespace tg
{
// software emulation of a 8bit float, i.e. 0..1 normalized
struct quarter
{
// TODO
private:
std::uint8_t data;
};
// TODO
constexpr quarter operator-(quarter const& v) { return v; }
constexpr bool operator<(quarter const& a, float b) { return true; }
} // namespace tg
#pragma once
#include <cmath>
#include "types.hh"
// TODO:
// proper f8, f16
// constexpr sqrt, pow, ...
namespace tg
{
constexpr i8 abs(i8 v) { return v < 0 ? -v : v; }
constexpr i16 abs(i16 v) { return v < 0 ? -v : v; }
constexpr i32 abs(i32 v) { return v < 0 ? -v : v; }
constexpr i64 abs(i64 v) { return v < 0 ? -v : v; }
constexpr u8 abs(u8 v) { return v; }
constexpr u16 abs(u16 v) { return v; }
constexpr u32 abs(u32 v) { return v; }
constexpr u64 abs(u64 v) { return v; }
constexpr f8 abs(f8 v) { return v < 0 ? -v : v; }
constexpr f16 abs(f16 v) { return v < 0 ? -v : v; }
constexpr f32 abs(f32 v) { return v < 0 ? -v : v; }
constexpr f64 abs(f64 v) { return v < 0 ? -v : v; }
f32 sin(f32 v) { return std::sin(v); }
f64 sin(f64 v) { return std::sin(v); }
f32 cos(f32 v) { return std::cos(v); }
f64 cos(f64 v) { return std::cos(v); }
f32 sqrt(f32 v) { return std::sqrt(v); }
f64 sqrt(f64 v) { return std::sqrt(v); }
f32 pow(f32 b, f32 e) { return std::pow(b, e); }
f32 pow(f32 b, i32 e) { return std::pow(b, e); }
f64 pow(f64 b, f64 e) { return std::pow(b, e); }
f64 pow(f64 b, i32 e) { return std::pow(b, e); }
} // namespace tg
......@@ -8,7 +8,7 @@ namespace tg
template <int Order>
struct shape
{
static inline constexpr int order = Order;
static constexpr int order = Order;
std::array<int, order> dims;
......@@ -43,9 +43,9 @@ public:
}
};
inline constexpr shape<1> make_shape(int w) { return {{w}}; }
inline constexpr shape<2> make_shape(int w, int h) { return {{w, h}}; }
inline constexpr shape<3> make_shape(int w, int h, int d) { return {{w, h, d}}; }
constexpr shape<1> make_shape(int w) { return {{w}}; }
constexpr shape<2> make_shape(int w, int h) { return {{w, h}}; }
constexpr shape<3> make_shape(int w, int h, int d) { return {{w, h, d}}; }
template <int Order>
constexpr shape<Order> make_shape(std::array<int, Order> const& dims)
{
......
#pragma once
#include "types.hh"
namespace tg
{
#define TG_DEFINE_BOOL_TRAIT(trait, default_val) \
template <class T> \
struct trait##_t \
{ \
static constexpr bool value = default_val; \
}; \
template <class T> \
constexpr bool trait = trait##_t<T>::value
#define TG_ADD_BOOL_TRAIT(trait, type, val) \
template <> \
struct trait##_t<type> \
{ \
static constexpr bool value = val; \
}
// shape traits
TG_DEFINE_BOOL_TRAIT(is_scalar, false);
TG_DEFINE_BOOL_TRAIT(is_vector, false);
TG_DEFINE_BOOL_TRAIT(is_matrix, false);
TG_DEFINE_BOOL_TRAIT(is_quaternion, false);
// field traits
TG_DEFINE_BOOL_TRAIT(is_integer, false);
TG_DEFINE_BOOL_TRAIT(is_floating_point, false);
TG_DEFINE_BOOL_TRAIT(is_signed_integer, false);
TG_DEFINE_BOOL_TRAIT(is_unsigned_integer, false);
// scalars
TG_ADD_BOOL_TRAIT(is_scalar, i8, true);
TG_ADD_BOOL_TRAIT(is_scalar, i16, true);
TG_ADD_BOOL_TRAIT(is_scalar, i32, true);
TG_ADD_BOOL_TRAIT(is_scalar, i64, true);
TG_ADD_BOOL_TRAIT(is_scalar, u8, true);
TG_ADD_BOOL_TRAIT(is_scalar, u16, true);
TG_ADD_BOOL_TRAIT(is_scalar, u32, true);
TG_ADD_BOOL_TRAIT(is_scalar, u64, true);
TG_ADD_BOOL_TRAIT(is_scalar, f8, true);
TG_ADD_BOOL_TRAIT(is_scalar, f16, true);
TG_ADD_BOOL_TRAIT(is_scalar, f32, true);
TG_ADD_BOOL_TRAIT(is_scalar, f64, true);
TG_ADD_BOOL_TRAIT(is_integer, i8, true);
TG_ADD_BOOL_TRAIT(is_integer, i16, true);
TG_ADD_BOOL_TRAIT(is_integer, i32, true);
TG_ADD_BOOL_TRAIT(is_integer, i64, true);
TG_ADD_BOOL_TRAIT(is_signed_integer, i8, true);
TG_ADD_BOOL_TRAIT(is_signed_integer, i16, true);
TG_ADD_BOOL_TRAIT(is_signed_integer, i32, true);
TG_ADD_BOOL_TRAIT(is_signed_integer, i64, true);
TG_ADD_BOOL_TRAIT(is_integer, u8, true);
TG_ADD_BOOL_TRAIT(is_integer, u16, true);
TG_ADD_BOOL_TRAIT(is_integer, u32, true);
TG_ADD_BOOL_TRAIT(is_integer, u64, true);
TG_ADD_BOOL_TRAIT(is_unsigned_integer, u8, true);
TG_ADD_BOOL_TRAIT(is_unsigned_integer, u16, true);
TG_ADD_BOOL_TRAIT(is_unsigned_integer, u32, true);
TG_ADD_BOOL_TRAIT(is_unsigned_integer, u64, true);
TG_ADD_BOOL_TRAIT(is_floating_point, f8, true);
TG_ADD_BOOL_TRAIT(is_floating_point, f16, true);
TG_ADD_BOOL_TRAIT(is_floating_point, f32, true);
TG_ADD_BOOL_TRAIT(is_floating_point, f64, true);
// vectors
template <int D, class ScalarT>
struct is_vector_t<vec<D, ScalarT>>
{
static constexpr bool value = true;
};
template <int D, class ScalarT>
struct is_integer_t<vec<D, ScalarT>>
{
static constexpr bool value = is_integer<ScalarT>;
};
template <int D, class ScalarT>
struct is_signed_integer_t<vec<D, ScalarT>>
{
static constexpr bool value = is_signed_integer<ScalarT>;
};
template <int D, class ScalarT>
struct is_unsigned_integer_t<vec<D, ScalarT>>
{
static constexpr bool value = is_unsigned_integer<ScalarT>;
};
template <int D, class ScalarT>
struct is_floating_point_t<vec<D, ScalarT>>
{
static constexpr bool value = is_floating_point<ScalarT>;
};
#undef TG_DEFINE_BOOL_TRAIT
#undef TG_ADD_BOOL_TRAIT
} // namespace tg
......@@ -13,54 +13,66 @@ template <class ScalarT>
struct vec<1, ScalarT>
{
using scalar_t = ScalarT;
static inline constexpr shape shape = make_shape(1);
static constexpr shape<1> shape = make_shape(1);
scalar_t x = static_cast<scalar_t>(0);
vec() = default;
explicit vec(scalar_t v) : x(v) {}
constexpr scalar_t& operator[](int i) { return (&x)[i]; }
constexpr scalar_t const& operator[](int i) const { return (&x)[i]; }
constexpr vec() = default;
constexpr explicit vec(scalar_t v) : x(v) {}
};
template <class ScalarT>
struct vec<2, ScalarT>
{
using scalar_t = ScalarT;
static inline constexpr shape shape = make_shape(2);
static constexpr shape<1> shape = make_shape(2);
scalar_t x = static_cast<scalar_t>(0);
scalar_t y = static_cast<scalar_t>(0);
vec() = default;
explicit vec(scalar_t v) : x(v), y(v) {}
vec(scalar_t x, scalar_t y) : x(x), y(y) {}
constexpr scalar_t& operator[](int i) { return (&x)[i]; }
constexpr scalar_t const& operator[](int i) const { return (&x)[i]; }
constexpr vec() = default;
constexpr explicit vec(scalar_t v) : x(v), y(v) {}
constexpr vec(scalar_t x, scalar_t y) : x(x), y(y) {}
};
template <class ScalarT>
struct vec<3, ScalarT>
{
using scalar_t = ScalarT;
static inline constexpr shape shape = make_shape(3);
static constexpr shape<1> shape = make_shape(3);
scalar_t x = static_cast<scalar_t>(0);
scalar_t y = static_cast<scalar_t>(0);
scalar_t z = static_cast<scalar_t>(0);
vec() = default;
explicit vec(scalar_t v) : x(v), y(v), z(v) {}
vec(scalar_t x, scalar_t y, scalar_t z) : x(x), y(y), z(z) {}
constexpr scalar_t& operator[](int i) { return (&x)[i]; }
constexpr scalar_t const& operator[](int i) const { return (&x)[i]; }
constexpr vec() = default;
constexpr explicit vec(scalar_t v) : x(v), y(v), z(v) {}
constexpr vec(scalar_t x, scalar_t y, scalar_t z) : x(x), y(y), z(z) {}
};
template <class ScalarT>
struct vec<4, ScalarT>
{
using scalar_t = ScalarT;
static inline constexpr shape shape = make_shape(4);
static constexpr shape<1> shape = make_shape(4);
scalar_t x = static_cast<scalar_t>(0);
scalar_t y = static_cast<scalar_t>(0);
scalar_t z = static_cast<scalar_t>(0);
scalar_t w = static_cast<scalar_t>(0);
vec() = default;
explicit vec(scalar_t v) : x(v), y(v), z(v), w(v) {}
vec(scalar_t x, scalar_t y, scalar_t z, scalar_t w) : x(x), y(y), z(z), w(w) {}
constexpr scalar_t& operator[](int i) { return (&x)[i]; }
constexpr scalar_t const& operator[](int i) const { return (&x)[i]; }
constexpr vec() = default;
constexpr explicit vec(scalar_t v) : x(v), y(v), z(v), w(v) {}
constexpr vec(scalar_t x, scalar_t y, scalar_t z, scalar_t w) : x(x), y(y), z(z), w(w) {}
};
} // namespace tg
#pragma once
#include "vec.hh"
namespace tg
{
template <int D, class ScalarT>
constexpr ScalarT const* data_ptr(vec<D, ScalarT> const& v)
{
return &v.x;
}
template <int D, class ScalarT>
constexpr ScalarT* data_ptr(vec<D, ScalarT>& v)
{
return &v.x;
}
template <int D, class ScalarT>
constexpr ScalarT dot(vec<D, ScalarT> const& a, vec<D, ScalarT> const& b)
{
auto v = a.x + b.x;
for (auto i = 1; i < D; ++i)
v += a[i] + b[i];
return v;
}
} // namespace tg
\ No newline at end of file
......@@ -3,6 +3,9 @@
#include "detail/constants.hh"
#include "detail/mat.hh"
#include "detail/quat.hh"
#include "detail/scalar_math.hh"
#include "detail/shape.hh"
#include "detail/types.hh"
#include "detail/traits.hh"
#include "detail/vec.hh"
#include "detail/vec_ops.hh"
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment