Revert "Reduce copies involved in Jet operations" This reverts commit c036c78196c7a9f36e48e6387691e8f4979aef5d. Change-Id: I736f2141d041e7a2c21491a159b48c5565df2bfa
diff --git a/include/ceres/jet.h b/include/ceres/jet.h index 99cff79..ac07b0a 100644 --- a/include/ceres/jet.h +++ b/include/ceres/jet.h
@@ -203,50 +203,43 @@ // Compound operators Jet<T, N>& operator+=(const Jet<T, N>& y) { - this->a += y.a; - this->v = this->v + y.v; // Better than += + *this = *this + y; return *this; } Jet<T, N>& operator-=(const Jet<T, N>& y) { - this->a -= y.a; - this->v = this->v - y.v; // Better than -= + *this = *this - y; return *this; } Jet<T, N>& operator*=(const Jet<T, N>& y) { - this->v = this->a * y.v + this->v * y.a; - this->a *= y.a; + *this = *this * y; return *this; } Jet<T, N>& operator/=(const Jet<T, N>& y) { - const T y_a_inverse = T(1.0) / y.a; - this->a *= y_a_inverse; - this->v = (this->v - this->a * y.v) * y_a_inverse; + *this = *this / y; return *this; } // Compound with scalar operators. Jet<T, N>& operator+=(const T& s) { - this->a += s; + *this = *this + s; return *this; } Jet<T, N>& operator-=(const T& s) { - this->a -= s; + *this = *this - s; return *this; } Jet<T, N>& operator*=(const T& s) { - this->a *= s; - this->v = this->v * s; // Better than *= + *this = *this * s; return *this; } Jet<T, N>& operator/=(const T& s) { - const T s_inverse = T(1.0) / s; - *this *= s_inverse; + *this = *this / s; return *this; } @@ -266,10 +259,6 @@ inline Jet<T, N> const& operator+(const Jet<T, N>& f) { return f; } -template <typename T, int N> -inline Jet<T, N>&& operator+(Jet<T, N>&& f) { - return std::move(f); -} // TODO(keir): Try adding __attribute__((always_inline)) to these functions to // see if it causes a performance increase. @@ -279,139 +268,60 @@ inline Jet<T, N> operator-(const Jet<T, N>& f) { return Jet<T, N>(-f.a, -f.v); } -template <typename T, int N> -inline Jet<T, N>&& operator-(Jet<T, N>&& f) { - f.a = -f.a; - f.v = -f.v; - return std::move(f); -} // Binary + template <typename T, int N> inline Jet<T, N> operator+(const Jet<T, N>& f, const Jet<T, N>& g) { return Jet<T, N>(f.a + g.a, f.v + g.v); } -template <typename T, int N> -inline Jet<T, N>&& operator+(Jet<T, N>&& f, const Jet<T, N>& g) { - f += g; - return std::move(f); -} -template <typename T, int N> -inline Jet<T, N>&& operator+(const Jet<T, N>& f, Jet<T, N>&& g) { - g += f; - return std::move(g); -} -template <typename T, int N> -inline Jet<T, N>&& operator+(Jet<T, N>&& f, Jet<T, N>&& g) { - return std::move(f) + g; -} // Binary + with a scalar: x + s template <typename T, int N> inline Jet<T, N> operator+(const Jet<T, N>& f, T s) { return Jet<T, N>(f.a + s, f.v); } -template <typename T, int N> -inline Jet<T, N>&& operator+(Jet<T, N>&& f, T s) { - f.a += s; - return std::move(f); -} // Binary + with a scalar: s + x template <typename T, int N> inline Jet<T, N> operator+(T s, const Jet<T, N>& f) { return Jet<T, N>(f.a + s, f.v); } -template <typename T, int N> -inline Jet<T, N>&& operator+(T s, Jet<T, N>&& f) { - f.a += s; - return std::move(f); -} // Binary - template <typename T, int N> inline Jet<T, N> operator-(const Jet<T, N>& f, const Jet<T, N>& g) { return Jet<T, N>(f.a - g.a, f.v - g.v); } -template <typename T, int N> -inline Jet<T, N>&& operator-(Jet<T, N>&& f, const Jet<T, N>& g) { - f -= g; - return std::move(f); -} -template <typename T, int N> -inline Jet<T, N>&& operator-(const Jet<T, N>& f, Jet<T, N>&& g) { - g -= f; - return -std::move(g); -} -template <typename T, int N> -inline Jet<T, N>&& operator-(Jet<T, N>&& f, Jet<T, N>&& g) { - return std::move(f) - g; -} // Binary - with a scalar: x - s template <typename T, int N> inline Jet<T, N> operator-(const Jet<T, N>& f, T s) { return Jet<T, N>(f.a - s, f.v); } -template <typename T, int N> -inline Jet<T, N>&& operator-(Jet<T, N>&& f, T s) { - f.a -= s; - return std::move(f); -} // Binary - with a scalar: s - x template <typename T, int N> inline Jet<T, N> operator-(T s, const Jet<T, N>& f) { return Jet<T, N>(s - f.a, -f.v); } -template <typename T, int N> -inline Jet<T, N>&& operator-(T s, Jet<T, N>&& f) { - f.a -= s; - return -std::move(f); -} // Binary * template <typename T, int N> inline Jet<T, N> operator*(const Jet<T, N>& f, const Jet<T, N>& g) { return Jet<T, N>(f.a * g.a, f.a * g.v + f.v * g.a); } -template <typename T, int N> -inline Jet<T, N>&& operator*(Jet<T, N>&& f, const Jet<T, N>& g) { - f *= g; - return std::move(f); -} -template <typename T, int N> -inline Jet<T, N>&& operator*(const Jet<T, N>& f, Jet<T, N>&& g) { - g *= f; - return std::move(g); -} -template <typename T, int N> -inline Jet<T, N>&& operator*(Jet<T, N>&& f, Jet<T, N>&& g) { - return std::move(f) * g; -} // Binary * with a scalar: x * s template <typename T, int N> inline Jet<T, N> operator*(const Jet<T, N>& f, T s) { return Jet<T, N>(f.a * s, f.v * s); } -// Binary * with a scalar: x * s -template <typename T, int N> -inline Jet<T, N>&& operator*(Jet<T, N>&& f, T s) { - f *= s; - return std::move(f); -} // Binary * with a scalar: s * x template <typename T, int N> inline Jet<T, N> operator*(T s, const Jet<T, N>& f) { return Jet<T, N>(f.a * s, f.v * s); } -template <typename T, int N> -inline Jet<T, N>&& operator*(T s, Jet<T, N>&& f) { - f *= s; - return std::move(f); -} // Binary / template <typename T, int N> @@ -427,22 +337,6 @@ const T f_a_by_g_a = f.a * g_a_inverse; return Jet<T, N>(f_a_by_g_a, (f.v - f_a_by_g_a * g.v) * g_a_inverse); } -template <typename T, int N> -inline Jet<T, N>&& operator/(Jet<T, N>&& f, const Jet<T, N>& g) { - f /= g; - return std::move(f); -} -template <typename T, int N> -inline Jet<T, N>&& operator/(const Jet<T, N>& f, Jet<T, N>&& g) { - const T g_a_inverse = T(1.0) / g.a; - g.a = f.a * g_a_inverse; - g.v = (f.v - g.a * g.v) * g_a_inverse; - return std::move(g); -} -template <typename T, int N> -inline Jet<T, N>&& operator/(Jet<T, N>&& f, Jet<T, N>&& g) { - return std::move(f) / g; -} // Binary / with a scalar: s / x template <typename T, int N> @@ -450,13 +344,6 @@ const T minus_s_g_a_inverse2 = -s / (g.a * g.a); return Jet<T, N>(s / g.a, g.v * minus_s_g_a_inverse2); } -template <typename T, int N> -inline Jet<T, N>&& operator/(T s, Jet<T, N>&& g) { - const T minus_s_g_a_inverse2 = -s / (g.a * g.a); - g.a = s / g.a; - g.v = g.v * minus_s_g_a_inverse2; // Better than *= - return std::move(g); -} // Binary / with a scalar: x / s template <typename T, int N> @@ -464,11 +351,6 @@ const T s_inverse = T(1.0) / s; return Jet<T, N>(f.a * s_inverse, f.v * s_inverse); } -template <typename T, int N> -inline Jet<T, N>&& operator/(Jet<T, N>&& f, T s) { - f /= s; - return std::move(f); -} // Binary comparison operators for both scalars and jets. #define CERES_DEFINE_JET_COMPARISON_OPERATOR(op) \
diff --git a/internal/ceres/jet_test.cc b/internal/ceres/jet_test.cc index 00a1227..6e98b86 100644 --- a/internal/ceres/jet_test.cc +++ b/internal/ceres/jet_test.cc
@@ -442,13 +442,9 @@ J b = 1.0 + x; J c = x; c += 1.0; - J d = J{x} + 1.0; - J e = 1.0 + J{x}; ExpectJetsClose(a, b); ExpectJetsClose(a, c); - ExpectJetsClose(a, d); - ExpectJetsClose(a, e); } { // Check that 1 - x == -(x - 1). @@ -456,13 +452,9 @@ J b = -(x - 1.0); J c = x; c -= 1.0; - J d = -(J{x} - 1.0); - J e = 1.0 - J{x}; ExpectJetsClose(a, b); ExpectJetsClose(a, -c); - ExpectJetsClose(a, d); - ExpectJetsClose(a, e); } { // Check that (x/s)*s == (x*s)/s. @@ -472,29 +464,19 @@ c /= 5.0; J d = x; d *= 5.0; - J e = J{x} / 5.0; - J f = J{x} * 5.0; - J g = 1.0 / (5.0 / J{x}); - J h = 5.0 * J{x}; ExpectJetsClose(5.0 * a, b / 5.0); ExpectJetsClose(a, c); ExpectJetsClose(b, d); - ExpectJetsClose(5.0 * e, f / 5.0); - ExpectJetsClose(5.0 * g, h / 5.0); } { // Check that x / y == 1 / (y / x). J a = x / y; J b = 1.0 / (y / x); - J c = J{x} / y; - J d = x / J{y}; VL << "a = " << a; VL << "b = " << b; ExpectJetsClose(a, b); - ExpectJetsClose(a, c); - ExpectJetsClose(a, d); } { // Check that abs(-x * x) == sqrt(x * x).