Skip to content

Commit

Permalink
[upstream_utils] Fix GCEM namespace usage and add hypot(x, y, z)
Browse files Browse the repository at this point in the history
  • Loading branch information
calcmogul committed Dec 3, 2023
1 parent a583ca0 commit 67e9cf8
Show file tree
Hide file tree
Showing 67 changed files with 1,696 additions and 229 deletions.

Large diffs are not rendered by default.

135 changes: 135 additions & 0 deletions upstream_utils/gcem_patches/0002-Add-hypot-x-y-z.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Tyler Veness <calcmogul@gmail.com>
Date: Sun, 3 Dec 2023 14:03:58 -0800
Subject: [PATCH 2/2] Add hypot(x, y, z)

---
include/gcem_incl/hypot.hpp | 85 +++++++++++++++++++++++++++++++++++--
1 file changed, 82 insertions(+), 3 deletions(-)

diff --git a/include/gcem_incl/hypot.hpp b/include/gcem_incl/hypot.hpp
index 00e10f899ace8f0da925fa9e46fa3f79f7e83aa0..13ea80c49d374c23434c1f9859bb6474184dc420 100644
--- a/include/gcem_incl/hypot.hpp
+++ b/include/gcem_incl/hypot.hpp
@@ -27,6 +27,7 @@
#ifndef _gcem_hypot_HPP
#define _gcem_hypot_HPP

+#include <algorithm>
#include <cmath>
#include <type_traits>

@@ -39,10 +40,29 @@ namespace internal
template<typename T>
constexpr
T
-hypot_compute(const T x, const T ydx)
+hypot_compute(const T x, const T y)
noexcept
{
- return abs(x) * sqrt( T(1) + (ydx * ydx) );
+ T a = std::max(abs(x), abs(y));
+ if (a) {
+ return a * sqrt((x / a) * (x / a) + (y / a) * (y / a));
+ } else {
+ return {};
+ }
+}
+
+template<typename T>
+constexpr
+T
+hypot_compute(const T x, const T y, const T z)
+noexcept
+{
+ T a = std::max({abs(x), abs(y), abs(z)});
+ if (a) {
+ return a * sqrt((x / a) * (x / a) + (y / a) * (y / a) + (z / a) * (z / a));
+ } else {
+ return {};
+ }
}

template<typename T>
@@ -62,7 +82,35 @@ noexcept
GCLIM<T>::min() > abs(y) ? \
abs(x) :
// else
- hypot_compute(x, y/x) );
+ hypot_compute(x, y) );
+}
+
+template<typename T>
+constexpr
+T
+hypot_vals_check(const T x, const T y, const T z)
+noexcept
+{
+ return( any_nan(x, y, z) ? \
+ GCLIM<T>::quiet_NaN() :
+ //
+ any_inf(x,y,z) ? \
+ GCLIM<T>::infinity() :
+ // indistinguishable from zero or one
+ GCLIM<T>::min() > abs(x) && GCLIM<T>::min() > abs(y) ? \
+ abs(z) :
+ GCLIM<T>::min() > abs(x) && GCLIM<T>::min() > abs(z) ? \
+ abs(y) :
+ GCLIM<T>::min() > abs(y) && GCLIM<T>::min() > abs(z) ? \
+ abs(x) :
+ GCLIM<T>::min() > abs(x) ? \
+ hypot_vals_check(y, z) :
+ GCLIM<T>::min() > abs(y) ? \
+ hypot_vals_check(x, z) :
+ GCLIM<T>::min() > abs(z) ? \
+ hypot_vals_check(x, y) :
+ // else
+ hypot_compute(x, y, z) );
}

template<typename T1, typename T2, typename TC = common_return_t<T1,T2>>
@@ -74,6 +122,15 @@ noexcept
return hypot_vals_check(static_cast<TC>(x),static_cast<TC>(y));
}

+template<typename T1, typename T2, typename T3, typename TC = common_return_t<T1,T2,T3>>
+constexpr
+TC
+hypot_type_check(const T1 x, const T2 y, const T3 z)
+noexcept
+{
+ return hypot_vals_check(static_cast<TC>(x),static_cast<TC>(y),static_cast<TC>(z));
+}
+
}

/**
@@ -97,6 +154,28 @@ noexcept
}
}

+/**
+ * Compile-time Pythagorean addition function
+ *
+ * @param x a real-valued input.
+ * @param y a real-valued input.
+ * @param z a real-valued input.
+ * @return Computes \f$ x \oplus y \oplus z = \sqrt{x^2 + y^2 + z^2} \f$.
+ */
+
+template<typename T1, typename T2, typename T3>
+constexpr
+common_return_t<T1,T2,T3>
+hypot(const T1 x, const T2 y, const T3 z)
+noexcept
+{
+ if (std::is_constant_evaluated()) {
+ return internal::hypot_type_check(x,y,z);
+ } else {
+ return std::hypot(x, y, z);
+ }
+}
+
}

#endif
5 changes: 4 additions & 1 deletion upstream_utils/update_gcem.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ def main():

# Apply patches to upstream Git repo
os.chdir(upstream_root)
for f in ["0001-Call-std-functions-if-not-constant-evaluated.patch"]:
for f in [
"0001-Call-std-functions-if-not-constant-evaluated.patch",
"0002-Add-hypot-x-y-z.patch",
]:
git_am(os.path.join(wpilib_root, "upstream_utils/gcem_patches", f))

# Delete old install
Expand Down
151 changes: 74 additions & 77 deletions wpimath/src/main/native/thirdparty/gcem/include/gcem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,82 +23,79 @@

#include "gcem_incl/gcem_options.hpp"

namespace gcem
{
#include "gcem_incl/quadrature/gauss_legendre_50.hpp"

#include "gcem_incl/is_inf.hpp"
#include "gcem_incl/is_nan.hpp"
#include "gcem_incl/is_finite.hpp"

#include "gcem_incl/signbit.hpp"
#include "gcem_incl/copysign.hpp"
#include "gcem_incl/neg_zero.hpp"
#include "gcem_incl/sgn.hpp"

#include "gcem_incl/abs.hpp"
#include "gcem_incl/ceil.hpp"
#include "gcem_incl/floor.hpp"
#include "gcem_incl/trunc.hpp"
#include "gcem_incl/is_odd.hpp"
#include "gcem_incl/is_even.hpp"
#include "gcem_incl/max.hpp"
#include "gcem_incl/min.hpp"
#include "gcem_incl/sqrt.hpp"
#include "gcem_incl/inv_sqrt.hpp"
#include "gcem_incl/hypot.hpp"

#include "gcem_incl/find_exponent.hpp"
#include "gcem_incl/find_fraction.hpp"
#include "gcem_incl/find_whole.hpp"
#include "gcem_incl/mantissa.hpp"
#include "gcem_incl/round.hpp"
#include "gcem_incl/fmod.hpp"

#include "gcem_incl/pow_integral.hpp"
#include "gcem_incl/exp.hpp"
#include "gcem_incl/expm1.hpp"
#include "gcem_incl/log.hpp"
#include "gcem_incl/log1p.hpp"
#include "gcem_incl/log2.hpp"
#include "gcem_incl/log10.hpp"
#include "gcem_incl/pow.hpp"

#include "gcem_incl/gcd.hpp"
#include "gcem_incl/lcm.hpp"

#include "gcem_incl/tan.hpp"
#include "gcem_incl/cos.hpp"
#include "gcem_incl/sin.hpp"

#include "gcem_incl/atan.hpp"
#include "gcem_incl/atan2.hpp"
#include "gcem_incl/acos.hpp"
#include "gcem_incl/asin.hpp"

#include "gcem_incl/tanh.hpp"
#include "gcem_incl/cosh.hpp"
#include "gcem_incl/sinh.hpp"

#include "gcem_incl/atanh.hpp"
#include "gcem_incl/acosh.hpp"
#include "gcem_incl/asinh.hpp"

#include "gcem_incl/binomial_coef.hpp"
#include "gcem_incl/lgamma.hpp"
#include "gcem_incl/tgamma.hpp"
#include "gcem_incl/factorial.hpp"
#include "gcem_incl/lbeta.hpp"
#include "gcem_incl/beta.hpp"
#include "gcem_incl/lmgamma.hpp"
#include "gcem_incl/log_binomial_coef.hpp"

#include "gcem_incl/erf.hpp"
#include "gcem_incl/erf_inv.hpp"
#include "gcem_incl/incomplete_beta.hpp"
#include "gcem_incl/incomplete_beta_inv.hpp"
#include "gcem_incl/incomplete_gamma.hpp"
#include "gcem_incl/incomplete_gamma_inv.hpp"
}
#include "gcem_incl/quadrature/gauss_legendre_50.hpp"

#include "gcem_incl/is_inf.hpp"
#include "gcem_incl/is_nan.hpp"
#include "gcem_incl/is_finite.hpp"

#include "gcem_incl/signbit.hpp"
#include "gcem_incl/copysign.hpp"
#include "gcem_incl/neg_zero.hpp"
#include "gcem_incl/sgn.hpp"

#include "gcem_incl/abs.hpp"
#include "gcem_incl/ceil.hpp"
#include "gcem_incl/floor.hpp"
#include "gcem_incl/trunc.hpp"
#include "gcem_incl/is_odd.hpp"
#include "gcem_incl/is_even.hpp"
#include "gcem_incl/max.hpp"
#include "gcem_incl/min.hpp"
#include "gcem_incl/sqrt.hpp"
#include "gcem_incl/inv_sqrt.hpp"
#include "gcem_incl/hypot.hpp"

#include "gcem_incl/find_exponent.hpp"
#include "gcem_incl/find_fraction.hpp"
#include "gcem_incl/find_whole.hpp"
#include "gcem_incl/mantissa.hpp"
#include "gcem_incl/round.hpp"
#include "gcem_incl/fmod.hpp"

#include "gcem_incl/pow_integral.hpp"
#include "gcem_incl/exp.hpp"
#include "gcem_incl/expm1.hpp"
#include "gcem_incl/log.hpp"
#include "gcem_incl/log1p.hpp"
#include "gcem_incl/log2.hpp"
#include "gcem_incl/log10.hpp"
#include "gcem_incl/pow.hpp"

#include "gcem_incl/gcd.hpp"
#include "gcem_incl/lcm.hpp"

#include "gcem_incl/tan.hpp"
#include "gcem_incl/cos.hpp"
#include "gcem_incl/sin.hpp"

#include "gcem_incl/atan.hpp"
#include "gcem_incl/atan2.hpp"
#include "gcem_incl/acos.hpp"
#include "gcem_incl/asin.hpp"

#include "gcem_incl/tanh.hpp"
#include "gcem_incl/cosh.hpp"
#include "gcem_incl/sinh.hpp"

#include "gcem_incl/atanh.hpp"
#include "gcem_incl/acosh.hpp"
#include "gcem_incl/asinh.hpp"

#include "gcem_incl/binomial_coef.hpp"
#include "gcem_incl/lgamma.hpp"
#include "gcem_incl/tgamma.hpp"
#include "gcem_incl/factorial.hpp"
#include "gcem_incl/lbeta.hpp"
#include "gcem_incl/beta.hpp"
#include "gcem_incl/lmgamma.hpp"
#include "gcem_incl/log_binomial_coef.hpp"

#include "gcem_incl/erf.hpp"
#include "gcem_incl/erf_inv.hpp"
#include "gcem_incl/incomplete_beta.hpp"
#include "gcem_incl/incomplete_beta_inv.hpp"
#include "gcem_incl/incomplete_gamma.hpp"
#include "gcem_incl/incomplete_gamma_inv.hpp"

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include <cmath>
#include <type_traits>

namespace gcem
{

/**
* Compile-time absolute value function
*
Expand All @@ -49,4 +52,6 @@ noexcept
}
}

}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include <cmath>
#include <type_traits>

namespace gcem
{

namespace internal
{

Expand Down Expand Up @@ -88,4 +91,6 @@ noexcept
}
}

}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include <cmath>
#include <type_traits>

namespace gcem
{

namespace internal
{

Expand Down Expand Up @@ -72,4 +75,6 @@ noexcept
}
}

}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include <cmath>
#include <type_traits>

namespace gcem
{

namespace internal
{

Expand Down Expand Up @@ -86,4 +89,6 @@ noexcept
}
}

}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include <cmath>
#include <type_traits>

namespace gcem
{

namespace internal
{

Expand Down Expand Up @@ -69,5 +72,6 @@ noexcept
}
}

}

#endif
Loading

0 comments on commit 67e9cf8

Please sign in to comment.