Skip to content

Commit

Permalink
Deprecate runtime indeterminates for now and add GCC force inlining
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyong committed Oct 28, 2019
1 parent 9fd7709 commit e3f49ae
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 271 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Geometric Algebra Library

[![Build Status](https://travis-ci.org/jeremyong/gal.svg?branch=master)](https://travis-ci.org/jeremyong/gal)
[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)

[**GAL**](https://www.jeremyong.com/gal/) (project page link) is a C++17 expression compiler and engine for computing with geometric algebra. It focuses primarily on speed and customizability with the ambition for being suitable for use in production environments where real-time speed is a factor.

Expand Down
55 changes: 6 additions & 49 deletions benchmark/ga-benchmark/SpecializedAlgorithmInverseKinematics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,16 @@ struct point_z
using algebra_t = cga_algebra;
using value_t = T;

T z;

constexpr point_z(T c) noexcept
: z{c}
{}

template <uint8_t... E>
constexpr point_z(entity<algebra_t, T, E...> in) noexcept
: z{in.template select<0b100>()}
{}

[[nodiscard]] constexpr static mv<algebra_t, 2, 3, 3> ie(uint32_t id) noexcept
[[nodiscard]] constexpr static mv<algebra_t, 0, 3, 3> ie(uint32_t id) noexcept
{
// A CGA point is represented as no + p + 1/2 p^2 ni
return {mv_size{2, 3, 3},
return {mv_size{0, 3, 3},
{
ind{id, rat{1}}, // ind2 = p_z
ind{id, rat{2}}, // ind5 = p_z^2
},
{
mon{one, one, 1, 0}, // p_z
mon{one, one, 0, 0}, // p_z
mon{one, zero, 0, 0}, // no
mon{one_half, rat{2}, 1, 1}, // 1/2 p_z^2
mon{one_half, rat{2}, 0, 0}, // 1/2 p_z^2
},
{
term{1, 0, 0b100}, // p_z
Expand All @@ -71,27 +58,7 @@ struct point_z

[[nodiscard]] constexpr static size_t size() noexcept
{
return 1;
}

[[nodiscard]] constexpr static uint32_t ind_count() noexcept
{
return 1;
}

[[nodiscard]] constexpr T const& operator[](size_t index) const noexcept
{
return z;
}

[[nodiscard]] constexpr T& operator[](size_t index) noexcept
{
return z;
}

[[nodiscard]] constexpr T get(size_t i) const noexcept
{
return NAN;
return 0;
}
};

Expand Down Expand Up @@ -157,11 +124,6 @@ union point_xz
return 2;
}

[[nodiscard]] constexpr static uint32_t ind_count() noexcept
{
return 2;
}

[[nodiscard]] constexpr T const& operator[](size_t index) const noexcept
{
return data[index];
Expand All @@ -171,11 +133,6 @@ union point_xz
{
return data[index];
}

[[nodiscard]] constexpr T get(size_t i) const noexcept
{
return NAN;
}
};

// Fourth order exp expansion
Expand Down Expand Up @@ -216,7 +173,7 @@ auto InverseKinematics(const Scalar& ang1, const Scalar& ang2, const Scalar& ang
point_xz<real_t> J2{J2_x, J2_z};
point_xz<real_t> J3{J3_x, J3_z};
point_xz<real_t> Jg{Jg_x, Jg_z};
point_z<real_t> Pz{1};
point_z<real_t> Pz;

auto Lz = compute(
[](auto Pz, auto ang1) { return frac<1, 2> * ang1 * ((n_o<real_t> ^ Pz ^ n_i<real_t>) >> ips<real_t>); },
Expand Down
10 changes: 0 additions & 10 deletions public/gal/cga.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,6 @@ namespace cga
return 3;
}

[[nodiscard]] constexpr static uint32_t ind_count() noexcept
{
return 3;
}

[[nodiscard]] constexpr T const& operator[](size_t index) const noexcept
{
return data[index];
Expand All @@ -179,11 +174,6 @@ namespace cga
{
return data[index];
}

[[nodiscard]] constexpr T get(size_t i) const noexcept
{
return NAN;
}
};
// TODO: provide representations for planes, spheres, flats, etc.
} // namespace cga
Expand Down
10 changes: 0 additions & 10 deletions public/gal/cga2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,6 @@ namespace cga2
return 2;
}

[[nodiscard]] constexpr static uint32_t ind_count() noexcept
{
return 2;
}

[[nodiscard]] constexpr T const& operator[](size_t index) const noexcept
{
return data[index];
Expand All @@ -173,11 +168,6 @@ namespace cga2
{
return data[index];
}

[[nodiscard]] constexpr T get(size_t i) const noexcept
{
return NAN;
}
};
// TODO: provide representations for planes, spheres, flats, etc.
} // namespace cga2
Expand Down
21 changes: 0 additions & 21 deletions public/gal/ega.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ namespace ega
return 3;
}

[[nodiscard]] constexpr static uint32_t ind_count() noexcept
{
return 3;
}

constexpr vector(T a, T b, T c) noexcept
: x{a}
, y{b}
Expand Down Expand Up @@ -99,12 +94,6 @@ namespace ega
{
return data[index];
}

[[nodiscard]] constexpr T get(size_t i) const noexcept
{
// Unused
return NAN;
}
};

template <typename T>
Expand All @@ -118,11 +107,6 @@ namespace ega
return 5;
}

[[nodiscard]] constexpr static uint32_t ind_count() noexcept
{
return 5;
}

std::array<T, 5> data;
struct
{
Expand Down Expand Up @@ -180,11 +164,6 @@ namespace ega
{
return data[index];
}

[[nodiscard]] constexpr T get(size_t i) const noexcept
{
return NAN;
}
};
} // namespace ega
} // namespace gal
69 changes: 29 additions & 40 deletions public/gal/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace detail
else
{
return ies<Ds...>(std::tuple_cat(out, std::make_tuple(expr<expr_op::identity, D, decltype(id)>{})),
std::integral_constant<uint, ID + D::ind_count()>{});
std::integral_constant<uint, ID + D::size()>{});
}
}

Expand Down Expand Up @@ -52,40 +52,29 @@ namespace detail
template <typename T>
struct ind_value
{
union
{
T const* pointer;
T value;
};

bool is_value;
T const* pointer;

[[nodiscard]] constexpr T operator*() const noexcept
{
return is_value ? value : *pointer;
return *pointer;
}
};

template <typename T, typename D, typename... Ds>
constexpr static void fill(T* out, D const& datum, Ds const&... data) noexcept
GAL_FORCE_INLINE constexpr static void fill(T* out, D const& datum, Ds const&... data) noexcept
{
for (size_t i = 0; i != D::size(); ++i)
{
auto& iv = *(out + i);
iv.pointer = &datum[i];
iv.is_value = false;
}

for (size_t i = D::size(); i != D::ind_count(); ++i)
if constexpr (D::size() > 0)
{
auto& iv = *(out + i);
iv.value = datum.get(i);
iv.is_value = true;
for (size_t i = 0; i != D::size(); ++i)
{
auto& iv = *(out + i);
iv.pointer = &datum[i];
}
}

if constexpr (sizeof...(Ds) > 0)
{
fill(out + D::ind_count(), data...);
fill(out + D::size(), data...);
}
}

Expand All @@ -97,7 +86,7 @@ namespace detail
struct cmon<F, ie, Index, std::index_sequence<I...>>
{
template <size_t N>
constexpr static F value(std::array<ind_value<F>, N> const& data) noexcept
GAL_FORCE_INLINE constexpr static F value(std::array<ind_value<F>, N> const& data) noexcept
{
constexpr auto m = ie.mons[Index];
if constexpr (m.q.is_zero())
Expand All @@ -112,8 +101,8 @@ namespace detail
{
return static_cast<F>(m.q)
* (::gal::pow(*data[ie.inds[m.ind_offset + I].id],
ie.inds[m.ind_offset + I].degree.num,
ie.inds[m.ind_offset + I].degree.den)
std::integral_constant<int, ie.inds[m.ind_offset + I].degree.num>{},
std::integral_constant<int, ie.inds[m.ind_offset + I].degree.den>{})
* ...);
}
}
Expand All @@ -127,30 +116,30 @@ namespace detail
struct cterm<F, ie, Offset, std::index_sequence<I...>>
{
template <size_t N>
constexpr static F value(std::array<ind_value<F>, N> const& data) noexcept
GAL_FORCE_INLINE constexpr static F value(std::array<ind_value<F>, N> const& data) noexcept
{
if constexpr (sizeof...(I) == 0)
{
return {0};
}
else
{
return (cmon<F, ie, Offset + I, std::make_index_sequence<ie.mons[Offset + I].count>>::value(data)
+ ...);
}
return (cmon<F, ie, Offset + I, std::make_index_sequence<ie.mons[Offset + I].count>>::value(data) + ...);
}
};

template <auto const& ie, typename F, typename A, size_t N, size_t... I>
[[nodiscard]] constexpr static auto
[[nodiscard]] GAL_FORCE_INLINE constexpr static inline auto
compute_entity(std::array<ind_value<F>, N> const& data, std::index_sequence<I...>) noexcept
{
using entity_t = entity<A, F, ie.terms[I].element...>;
return entity_t{cterm<F, ie, ie.terms[I].mon_offset, std::make_index_sequence<ie.terms[I].count>>::value(data)...};
if constexpr (sizeof...(I) == 0)
{
return entity_t{};
}
else
{
return entity_t{
cterm<F, ie, ie.terms[I].mon_offset, std::make_index_sequence<ie.terms[I].count>>::value(data)...};
}
}

template <typename A, typename V, typename T, typename D>
[[nodiscard]] static auto finalize_entity(D const& data)
[[nodiscard]] GAL_FORCE_INLINE static inline auto finalize_entity(D const& data)
{
constexpr static auto reified = reify<T>();
if constexpr (detail::uses_null_basis<A>)
Expand Down Expand Up @@ -210,7 +199,7 @@ template <typename L, typename... Data>
{
if constexpr (std::tuple_size_v<ie_result_t> != 0)
{
std::array<detail::ind_value<value_t>, (Data::ind_count() + ...)> data{};
std::array<detail::ind_value<value_t>, (Data::size() + ...)> data{};
detail::fill(data.data(), input...);

return std::apply(
Expand All @@ -223,7 +212,7 @@ template <typename L, typename... Data>
}
else
{
std::array<detail::ind_value<value_t>, (Data::ind_count() + ...)> data{};
std::array<detail::ind_value<value_t>, (Data::size() + ...)> data{};
detail::fill(data.data(), input...);
return detail::finalize_entity<algebra_t, value_t, ie_result_t>(data);
}
Expand Down
22 changes: 0 additions & 22 deletions public/gal/entity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ struct entity
return sizeof...(E);
}

[[nodiscard]] constexpr static uint32_t ind_count() noexcept
{
return sizeof...(E);
}

template <uint8_t... S>
[[nodiscard]] constexpr auto select() const noexcept
{
Expand Down Expand Up @@ -130,12 +125,6 @@ struct entity
{
return data_[index];
}

[[nodiscard]] constexpr T get(size_t) const noexcept
{
// Unreachable
return {};
}
};

template <typename A, typename T>
Expand All @@ -149,11 +138,6 @@ struct scalar
return 1;
}

[[nodiscard]] constexpr static uint32_t ind_count() noexcept
{
return 1;
}

// NOTE: in GAL code, `ie` refers always to "indeterminate expression"
[[nodiscard]] constexpr static mv<A, 1, 1, 1> ie(uint32_t id) noexcept
{
Expand All @@ -180,12 +164,6 @@ struct scalar
return value;
}

[[nodiscard]] constexpr T get(size_t) const noexcept
{
// Unreachable
return {};
}

T value;
};
} // namespace gal
Loading

0 comments on commit e3f49ae

Please sign in to comment.