Skip to content

Commit 3f954b8

Browse files
committed
update PR following changes on master
1 parent b777ad6 commit 3f954b8

File tree

5 files changed

+49
-24
lines changed

5 files changed

+49
-24
lines changed

example/measurement.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ class measurement {
140140

141141
template<typename T>
142142
struct mp_units::scaling_traits<measurement<T>, mp_units::unspecified_rep> {
143-
template<mp_units::Magnitude auto M>
143+
template<mp_units::UnitMagnitude auto M>
144144
[[nodiscard]] static constexpr auto scale(const measurement<T>& value)
145145
{
146146
return measurement{
@@ -152,7 +152,7 @@ struct mp_units::scaling_traits<measurement<T>, mp_units::unspecified_rep> {
152152

153153
template<typename From, typename To>
154154
struct mp_units::scaling_traits<measurement<From>, measurement<To>> {
155-
template<mp_units::Magnitude auto M>
155+
template<mp_units::UnitMagnitude auto M>
156156
[[nodiscard]] static constexpr measurement<To> scale(const measurement<From>& value)
157157
{
158158
constexpr std::type_identity<To> to_type;

src/core/include/mp-units/bits/fixed_point.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,23 @@
2222

2323
#pragma once
2424

25-
// IWYU pragma: private, include <mp-units/framework.h>
26-
#include <mp-units/framework/magnitude.h>
25+
#include <mp-units/bits/hacks.h> // IWYU pragma: keep
2726

2827
#ifndef MP_UNITS_IN_MODULE_INTERFACE
2928
#ifdef MP_UNITS_IMPORT_STD
3029
import std;
3130
#else
31+
#include <algorithm>
3232
#include <bit>
33+
#include <cmath>
3334
#include <concepts>
3435
#include <cstdint>
3536
#include <cstdlib>
3637
#include <limits>
3738
#include <numbers>
39+
#include <tuple>
40+
#include <type_traits>
41+
#include <version>
3842
#endif
3943
#endif
4044

@@ -43,6 +47,26 @@ namespace mp_units::detail {
4347
template<typename T>
4448
constexpr std::size_t integer_rep_width_v = std::numeric_limits<std::make_unsigned_t<T>>::digits;
4549

50+
template<std::floating_point T>
51+
[[nodiscard]] consteval T int_power(T base, int exponent)
52+
{
53+
#if defined(__cpp_lib_constexpr_cmath) && __cpp_lib_constexpr_cmath >= 202202L
54+
return std::ldexp(base, exponent);
55+
#else
56+
if (exponent < 0) {
57+
base = T{1} / base;
58+
exponent = -exponent;
59+
}
60+
T ret = 1;
61+
while (exponent) {
62+
if (exponent & 1) ret *= base;
63+
exponent >>= 1;
64+
base *= base;
65+
}
66+
return ret;
67+
68+
#endif
69+
}
4670

4771
// this class synthesizes a double-width integer from two base-width integers.
4872
template<std::integral T>

src/core/include/mp-units/bits/sudo_cast.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ template<Quantity To, typename FwdFrom, Quantity From = std::remove_cvref_t<FwdF
7878
To::reference}; // this is the only (and recommended) way to do a truncating conversion on a number, so we
7979
// are using static_cast to suppress all the compiler warnings on conversions
8080
} else {
81-
constexpr Magnitude auto c_mag = get_canonical_unit(From::unit).mag / get_canonical_unit(To::unit).mag;
81+
constexpr UnitMagnitude auto c_mag = get_canonical_unit(From::unit).mag / get_canonical_unit(To::unit).mag;
8282

8383
typename To::rep res =
8484
scale(std::type_identity<typename To::rep>{}, c_mag, q.numerical_value_is_an_implementation_detail_);
@@ -122,7 +122,7 @@ template<QuantityPoint ToQP, typename FwdFromQP, QuantityPoint FromQP = std::rem
122122
constexpr UnitMagnitude auto c_mag = get_canonical_unit(FromQP::unit).mag / get_canonical_unit(ToQP::unit).mag;
123123
using type_traits = conversion_type_traits<c_mag, typename FromQP::rep, typename ToQP::rep>;
124124
using c_type = type_traits::c_type;
125-
if constexpr (_get_value<long double>(c_mag) > 1.) {
125+
if constexpr (get_value<long double>(c_mag) > 1.) {
126126
// original unit had a larger unit magnitude; if we first convert to the common representation but retain the
127127
// unit, we obtain the largest possible range while not causing truncation of fractional values. This is optimal
128128
// for the offset computation.

src/core/include/mp-units/framework/representation_concepts.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
// IWYU pragma: private, include <mp-units/framework.h>
2626
#include <mp-units/bits/module_macros.h>
2727
#include <mp-units/framework/customization_points.h>
28-
#include <mp-units/framework/magnitude.h>
2928
#include <mp-units/framework/quantity_spec_concepts.h>
3029
#include <mp-units/framework/scaling.h>
30+
#include <mp-units/framework/unit_magnitude.h>
3131

3232
#ifndef MP_UNITS_IN_MODULE_INTERFACE
3333
#ifdef MP_UNITS_IMPORT_STD

src/core/include/mp-units/framework/scaling.h

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
// IWYU pragma: private, include <mp-units/framework.h>
2626
#include <mp-units/bits/fixed_point.h>
2727
#include <mp-units/framework/customization_points.h>
28+
#include <mp-units/framework/unit_magnitude_concepts.h>
2829

2930
#ifndef MP_UNITS_IN_MODULE_INTERFACE
3031
#ifdef MP_UNITS_IMPORT_STD
@@ -79,8 +80,8 @@ template<typename T>
7980
concept UsesFloatingPointScaling =
8081
treat_as_floating_point<T> && requires(T value, floating_point_scaling_factor_type<value_type_t<T>>::type f) {
8182
// the result representation does not necessarily have to be the same.
82-
{ value* f } -> std::equality_comparable;
83-
{ value* f } -> std::copyable;
83+
{ value * f } -> std::equality_comparable;
84+
{ value * f } -> std::copyable;
8485
};
8586

8687
template<typename T>
@@ -122,21 +123,21 @@ struct scaling_traits<From, To> {
122123
using _scaling_factor_type = std::common_type_t<value_type_t<From>, value_type_t<To>>;
123124
static_assert(std::is_floating_point_v<_scaling_factor_type>);
124125

125-
template<Magnitude auto M>
126+
template<UnitMagnitude auto M>
126127
static constexpr bool implicitly_scalable =
127128
std::is_convertible_v<decltype(detail::cast_if_integral<_scaling_factor_type>(std::declval<From>()) *
128129
std::declval<_scaling_factor_type>()),
129130
To>;
130131

131-
template<Magnitude auto M>
132+
template<UnitMagnitude auto M>
132133
static constexpr To scale(const From& value)
133134
{
134135
using U = _scaling_factor_type;
135-
if constexpr (_is_integral(_pow<-1>(M)) && !_is_integral(M)) {
136-
constexpr U div = static_cast<U>(_get_value<long double>(_pow<-1>(M)));
136+
if constexpr (is_integral(pow<-1>(M)) && !is_integral(M)) {
137+
constexpr U div = static_cast<U>(get_value<long double>(pow<-1>(M)));
137138
return static_cast<To>(detail::cast_if_integral<U>(value) / div);
138139
} else {
139-
constexpr U ratio = static_cast<U>(_get_value<long double>(M));
140+
constexpr U ratio = static_cast<U>(get_value<long double>(M));
140141
return static_cast<To>(detail::cast_if_integral<U>(value) * ratio);
141142
}
142143
}
@@ -154,20 +155,20 @@ struct scaling_traits<From, To> {
154155

155156
// TODO: should we take possible overflow into account here? This would lead to this almost always resulting
156157
// in explicit conversions, except for small integral factors combined with a widening conversion.
157-
template<Magnitude auto M>
158-
static constexpr bool implicitly_scalable = std::is_convertible_v<From, To> && _is_integral(M);
158+
template<UnitMagnitude auto M>
159+
static constexpr bool implicitly_scalable = std::is_convertible_v<From, To> && is_integral(M);
159160

160-
template<Magnitude auto M>
161+
template<UnitMagnitude auto M>
161162
static constexpr To scale(const From& value)
162163
{
163-
if constexpr (_is_integral(M)) {
164-
constexpr auto mul = _get_value<_common_type>(M);
164+
if constexpr (is_integral(M)) {
165+
constexpr auto mul = get_value<_common_type>(M);
165166
return static_cast<To>(static_cast<value_type_t<From>>(value) * mul);
166-
} else if constexpr (_is_integral(_pow<-1>(M))) {
167-
constexpr auto div = _get_value<_common_type>(_pow<-1>(M));
167+
} else if constexpr (is_integral(pow<-1>(M))) {
168+
constexpr auto div = get_value<_common_type>(pow<-1>(M));
168169
return static_cast<To>(static_cast<value_type_t<From>>(value) / div);
169170
} else {
170-
constexpr auto ratio = detail::fixed_point<_common_type>(_get_value<long double>(M));
171+
constexpr auto ratio = detail::fixed_point<_common_type>(get_value<long double>(M));
171172
return static_cast<To>(ratio.scale(static_cast<value_type_t<From>>(value)));
172173
}
173174
}
@@ -181,7 +182,7 @@ MP_UNITS_EXPORT_BEGIN
181182

182183
// @brief approximate the result of the symbolic multiplication of @c from by @c scaling_factor, and represent it as an
183184
// instance of @to
184-
template<typename To, Magnitude M, typename From>
185+
template<typename To, UnitMagnitude M, typename From>
185186
requires detail::HasScalingTraits<From, To>
186187
constexpr To scale(std::type_identity<To>, M scaling_factor [[maybe_unused]], const From& value)
187188
{
@@ -191,7 +192,7 @@ constexpr To scale(std::type_identity<To>, M scaling_factor [[maybe_unused]], co
191192
}
192193

193194
// @brief approximate the result of the symbolic multiplication of @c from by @c scaling_factor
194-
template<Magnitude M, typename From>
195+
template<UnitMagnitude M, typename From>
195196
requires detail::HasScalingTraits<From, unspecified_rep>
196197
constexpr auto scale(M scaling_factor [[maybe_unused]], const From& value)
197198
{

0 commit comments

Comments
 (0)