From 48f4f9bf9cbffa750fca0a749abf793ecc13fb6c Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Fri, 27 May 2022 12:46:22 -0600 Subject: [PATCH] temporal: remove some arithmetic operators and rewrite others to use muldiv() --- libs/temporal/temporal/timeline.h | 8 +--- libs/temporal/timeline.cc | 76 ++----------------------------- 2 files changed, 6 insertions(+), 78 deletions(-) diff --git a/libs/temporal/temporal/timeline.h b/libs/temporal/temporal/timeline.h index a2edeaae2b..64d39f3bee 100644 --- a/libs/temporal/temporal/timeline.h +++ b/libs/temporal/temporal/timeline.h @@ -193,10 +193,7 @@ class LIBTEMPORAL_API timepos_t : public int62_t { * along the x (time) axis. */ - timepos_t operator/ (ratio_t const & n) const; - timepos_t operator* (ratio_t const & n) const; - timepos_t & operator/= (ratio_t const & n); - timepos_t & operator*= (ratio_t const & n); + timepos_t scale (ratio_t const & n) const; bool operator< (samplepos_t s) { return samples() < s; } bool operator< (Temporal::Beats const & b) { return beats() < b; } @@ -357,8 +354,7 @@ class LIBTEMPORAL_API timecnt_t { timecnt_t operator++ () { _distance += 1; return *this; } timecnt_t operator-- () { _distance -= 1; return *this; } - timecnt_t operator*(ratio_t const &) const; - timecnt_t operator/(ratio_t const &) const; + timecnt_t scale (ratio_t const &) const; ratio_t operator/ (timecnt_t const &) const; diff --git a/libs/temporal/timeline.cc b/libs/temporal/timeline.cc index 15dcdecc1d..3c55936f4b 100644 --- a/libs/temporal/timeline.cc +++ b/libs/temporal/timeline.cc @@ -21,8 +21,6 @@ #include #include -#include - #include "pbd/enumwriter.h" #include "pbd/error.h" #include "pbd/compose.h" @@ -200,25 +198,9 @@ timecnt_t::compute_beats() const } timecnt_t -timecnt_t::operator*(ratio_t const & r) const +timecnt_t::scale (ratio_t const & r) const { - boost::multiprecision::int512_t bignum = _distance.val(); - - bignum *= r.numerator (); - bignum /= r.denominator (); - - try { - - int64_t midnum = bignum.convert_to (); - assert (midnum < int62_t::max); - const int62_t v (_distance.flagged(), midnum); - return timecnt_t (v, _position); - - } catch (...) { - fatal << X_("arithmetic overflow in timeline math\n") << endmsg; - /* NOTREACHED */ - return timecnt_t (); - } + return timecnt_t (PBD::muldiv (_distance.val(), r.numerator(), r.denominator()), _position); } ratio_t @@ -235,15 +217,6 @@ timecnt_t::operator/ (timecnt_t const & other) const return ratio_t (beats().to_ticks(), other.beats().to_ticks()); } -timecnt_t -timecnt_t::operator/(ratio_t const & r) const -{ - /* note: x / (N/D) => x * (D/N) => (x * D) / N */ - - const int62_t v (_distance.flagged(), int_div_round (_distance.val() * r.denominator(), r.numerator())); - return timecnt_t (v, _position); -} - timecnt_t timecnt_t::operator% (timecnt_t const & d) const { @@ -594,50 +567,9 @@ timepos_t::_ticks () const } timepos_t -timepos_t::operator/(ratio_t const & n) const +timepos_t::scale (ratio_t const & n) const { - /* this cannot make the value negative, since ratio_t is always positive */ - /* note: v / (N/D) = (v * D) / N */ - - return timepos_t (is_beats(), int_div_round (val() * n.denominator(), n.numerator())); -} - -timepos_t -timepos_t::operator*(ratio_t const & n) const -{ - /* this cannot make the value negative, since ratio_t is always positive */ - boost::multiprecision::int512_t bignum = val(); - - bignum *= n.numerator (); - bignum /= n.denominator (); - - try { - - int64_t midnum = bignum.convert_to (); - assert (midnum < int62_t::max); - return timepos_t (is_beats(), midnum); - - } catch (...) { - fatal << X_("arithmetic overflow in timepos_t::operator* (ratio_t)\n") << endmsg; - /* NOTREACHED */ - return timepos_t(); - } -} - -timepos_t & -timepos_t::operator/=(ratio_t const & n) -{ - /* this cannot make the value negative, since ratio_t is always positive */ - v = build (flagged(), int_div_round (val() * n.numerator(), n.denominator())); - return *this; -} - -timepos_t & -timepos_t::operator*=(ratio_t const & n) -{ - /* this cannot make the value negative, since ratio_t is always positive */ - v = build (flagged(), int_div_round (val() * n.denominator(), n.numerator())); - return *this; + return timepos_t (PBD::muldiv (val(), n.numerator(), n.denominator())); } timepos_t