From a11f5a92b6b5f914b35192abb233ee7edd256843 Mon Sep 17 00:00:00 2001 From: Jan Sende <44576417+jansende@users.noreply.github.com> Date: Mon, 30 Sep 2019 23:38:33 +0200 Subject: [PATCH] Optimized floating point unit conversions - Divisions are now made at compile time. - Only runtime multiplications are left. --- src/include/units/quantity.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/include/units/quantity.h b/src/include/units/quantity.h index 3c5a803a..beb137cb 100644 --- a/src/include/units/quantity.h +++ b/src/include/units/quantity.h @@ -89,6 +89,14 @@ namespace units { template struct quantity_cast_impl { template + requires treat_as_floating_point + static constexpr To cast(const Q& q) + { + return To(static_cast(static_cast(q.count()) * (static_cast(CR::num) / + static_cast(CR::den)))); + } + template + requires !treat_as_floating_point static constexpr To cast(const Q& q) { return To(static_cast(static_cast(q.count()) * static_cast(CR::num) / @@ -108,6 +116,13 @@ namespace units { template struct quantity_cast_impl { template + requires treat_as_floating_point + static constexpr To cast(const Q& q) + { + return To(static_cast(static_cast(q.count()) * (CRep{1} / static_cast(CR::den)))); + } + template + requires !treat_as_floating_point static constexpr To cast(const Q& q) { return To(static_cast(static_cast(q.count()) / static_cast(CR::den)));