refactor: compiple-time performance optimizations for dimensions

This commit is contained in:
Mateusz Pusz
2024-06-12 14:18:29 +02:00
parent 6aa848a456
commit 18b16c51a0

View File

@ -159,15 +159,15 @@ struct is_dimension_one<struct dimension_one> : std::true_type {};
MP_UNITS_EXPORT template<Dimension Lhs, Dimension Rhs> MP_UNITS_EXPORT template<Dimension Lhs, Dimension Rhs>
[[nodiscard]] consteval Dimension auto operator*(Lhs, Rhs) [[nodiscard]] consteval Dimension auto operator*(Lhs, Rhs)
{ {
return detail::expr_multiply<derived_dimension, struct dimension_one, detail::type_list_of_base_dimension_less>( return decltype(detail::expr_multiply<derived_dimension, struct dimension_one,
Lhs{}, Rhs{}); detail::type_list_of_base_dimension_less>(Lhs{}, Rhs{})){};
} }
MP_UNITS_EXPORT template<Dimension Lhs, Dimension Rhs> MP_UNITS_EXPORT template<Dimension Lhs, Dimension Rhs>
[[nodiscard]] consteval Dimension auto operator/(Lhs, Rhs) [[nodiscard]] consteval Dimension auto operator/(Lhs, Rhs)
{ {
return detail::expr_divide<derived_dimension, struct dimension_one, detail::type_list_of_base_dimension_less>(Lhs{}, return decltype(detail::expr_divide<derived_dimension, struct dimension_one,
Rhs{}); detail::type_list_of_base_dimension_less>(Lhs{}, Rhs{})){};
} }
namespace detail { namespace detail {
@ -191,7 +191,7 @@ template<Dimension Lhs, Dimension Rhs>
return is_same_v<Lhs, Rhs> || detail::derived_from_the_same_base_dimension(lhs, rhs); return is_same_v<Lhs, Rhs> || detail::derived_from_the_same_base_dimension(lhs, rhs);
} }
[[nodiscard]] consteval Dimension auto inverse(Dimension auto d) { return dimension_one / d; } [[nodiscard]] consteval Dimension auto inverse(Dimension auto d) { return decltype(dimension_one / d){}; }
/** /**
* @brief Computes the value of a dimension raised to the `Num/Den` power * @brief Computes the value of a dimension raised to the `Num/Den` power
@ -212,8 +212,8 @@ template<std::intmax_t Num, std::intmax_t Den = 1, Dimension D>
else else
return derived_dimension<power<D, Num, Den>>{}; return derived_dimension<power<D, Num, Den>>{};
} else } else
return detail::expr_pow<Num, Den, derived_dimension, struct dimension_one, return decltype(detail::expr_pow<Num, Den, derived_dimension, struct dimension_one,
detail::type_list_of_base_dimension_less>(d); detail::type_list_of_base_dimension_less>(d)){};
} }
/** /**
@ -223,7 +223,7 @@ template<std::intmax_t Num, std::intmax_t Den = 1, Dimension D>
* *
* @return Dimension The result of computation * @return Dimension The result of computation
*/ */
[[nodiscard]] consteval Dimension auto sqrt(Dimension auto d) { return pow<1, 2>(d); } [[nodiscard]] consteval Dimension auto sqrt(Dimension auto d) { return decltype(pow<1, 2>(d)){}; }
/** /**
* @brief Computes the cubic root of a dimension * @brief Computes the cubic root of a dimension
@ -232,7 +232,7 @@ template<std::intmax_t Num, std::intmax_t Den = 1, Dimension D>
* *
* @return Dimension The result of computation * @return Dimension The result of computation
*/ */
[[nodiscard]] consteval Dimension auto cbrt(Dimension auto d) { return pow<1, 3>(d); } [[nodiscard]] consteval Dimension auto cbrt(Dimension auto d) { return decltype(pow<1, 3>(d)){}; }
struct dimension_symbol_formatting { struct dimension_symbol_formatting {