jam 0.0.1
A compacting generational garbage collector for C++26
Loading...
Searching...
No Matches
units.ccm
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Edward Kmett <ekmett@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
3
6module;
7#include <chrono>
8#include <cmath>
9#include <compare>
10#include <concepts>
11#include <cstddef>
12#include <cstdint>
13#include <cstdio>
14#include <cstdlib>
15#include <limits>
16#include <ratio>
17#include <type_traits>
18
19export module jam.units;
20
21namespace jam::units::detail {
22[[noreturn]] inline void overflow() noexcept {
23 std::fputs("jam: space arithmetic overflow or invalid conversion\n", stderr);
24 std::abort();
25}
26
27template<class To, class From, class Scale = std::ratio<1>>
28constexpr To convert(From value) noexcept {
29 if constexpr (std::integral<To> && std::integral<From>) {
30 // A 64-bit count times an intmax_t ratio fits this signed intermediate.
31 auto const scaled = static_cast<__int128>(value) * Scale::num / Scale::den;
32 if (scaled < static_cast<__int128>(std::numeric_limits<To>::lowest()) ||
33 scaled > static_cast<__int128>(std::numeric_limits<To>::max())) overflow();
34 return static_cast<To>(scaled);
35 } else {
36 auto const scaled = static_cast<long double>(value) * Scale::num / Scale::den;
37 if constexpr (std::integral<To>) {
38 // Truncate before checking the destination, including -0.5 -> unsigned 0.
39 // This loose exact bound makes the intermediate float-to-integer cast safe.
40 constexpr auto bound = static_cast<long double>(__int128{1} << 65);
41 if (!std::isfinite(scaled) || scaled <= -bound || scaled >= bound) overflow();
42 auto const integral = static_cast<__int128>(scaled);
43 if (integral < static_cast<__int128>(std::numeric_limits<To>::lowest()) ||
44 integral > static_cast<__int128>(std::numeric_limits<To>::max())) overflow();
45 return static_cast<To>(integral);
46 }
47 return static_cast<To>(scaled);
48 }
49}
50
51template<char Op, class R>
52constexpr R arithmetic(R a, R b) noexcept {
53 if constexpr (std::integral<R>) {
54 R result;
55 if constexpr (Op == '+') { if (__builtin_add_overflow(a, b, &result)) overflow(); }
56 if constexpr (Op == '-') { if (__builtin_sub_overflow(a, b, &result)) overflow(); }
57 if constexpr (Op == '*') { if (__builtin_mul_overflow(a, b, &result)) overflow(); }
58 if constexpr (Op == '/' || Op == '%') {
59 if (!b) overflow();
60 if constexpr (std::is_signed_v<R>)
61 if (a == std::numeric_limits<R>::lowest() && b == -1) overflow();
62 if constexpr (Op == '/') result = a / b;
63 else result = a % b;
64 }
65 return result;
66 } else {
67 if constexpr (Op == '+') return a + b;
68 if constexpr (Op == '-') return a - b;
69 if constexpr (Op == '*') return a * b;
70 if constexpr (Op == '/') return a / b;
71 }
72}
73}
74
76export namespace jam::units {
82template<class Rep, class Ratio = std::ratio<1>>
83class space {
84 static_assert(std::is_arithmetic_v<Rep> && !std::same_as<Rep, bool> && sizeof(Rep) <= sizeof(long double));
85 static_assert(!std::integral<Rep> || sizeof(Rep) <= sizeof(std::uint64_t));
86 static_assert(Ratio::num > 0);
87 Rep value;
88public:
89 using rep = Rep;
90 using ratio = typename Ratio::type;
92 constexpr space() = default;
94 template<class R>
95 requires std::is_arithmetic_v<R> && (std::floating_point<Rep> || !std::floating_point<R>)
96 constexpr explicit space(R count) noexcept : value(detail::convert<Rep>(count)) {}
98 template<class R, class P>
99 requires (std::floating_point<Rep> ||
100 (!std::floating_point<R> && std::ratio_divide<P, ratio>::den == 1))
101 constexpr space(space<R, P> other) noexcept
102 : value(detail::convert<Rep, R, std::ratio_divide<P, ratio>>(other.count())) {}
103
104 [[nodiscard]] constexpr rep count() const noexcept { return value; }
106 [[nodiscard]] static constexpr space zero() noexcept { return space{rep{0}}; }
108 [[nodiscard]] static constexpr space min() noexcept { return space{std::numeric_limits<rep>::lowest()}; }
110 [[nodiscard]] static constexpr space max() noexcept { return space{std::numeric_limits<rep>::max()}; }
112 [[nodiscard]] constexpr space operator+() const noexcept { return *this; }
114 [[nodiscard]] constexpr space operator-() const noexcept { return space{detail::arithmetic<'-'>(rep{0}, value)}; }
116 constexpr space & operator++() noexcept { return *this += space{1}; }
118 constexpr space operator++(int) noexcept { auto old = *this; ++*this; return old; }
120 constexpr space & operator--() noexcept { return *this -= space{1}; }
122 constexpr space operator--(int) noexcept { auto old = *this; --*this; return old; }
124 constexpr space & operator+=(space other) noexcept { value = detail::arithmetic<'+'>(value, other.value); return *this; }
126 constexpr space & operator-=(space other) noexcept { value = detail::arithmetic<'-'>(value, other.value); return *this; }
128 constexpr space & operator*=(rep scalar) noexcept { value = detail::arithmetic<'*'>(value, scalar); return *this; }
130 constexpr space & operator/=(rep scalar) noexcept { value = detail::arithmetic<'/'>(value, scalar); return *this; }
132 constexpr space & operator%=(rep scalar) noexcept requires std::integral<rep> { value = detail::arithmetic<'%'>(value, scalar); return *this; }
134 constexpr space & operator%=(space other) noexcept requires std::integral<rep> { return *this %= other.value; }
135};
136}
137
138export namespace std {
140template<class R1, class P1, class R2, class P2>
141struct common_type<jam::units::space<R1, P1>, jam::units::space<R2, P2>> {
142 using duration = common_type_t<chrono::duration<R1, P1>, chrono::duration<R2, P2>>;
144};
145}
146
147export namespace jam::units {
149template<class To, class R, class P>
150[[nodiscard]] constexpr To space_cast(space<R, P> value) noexcept {
151 return To{detail::convert<typename To::rep, R, std::ratio_divide<P, typename To::ratio>>(value.count())};
152}
153
154template<class R1, class P1, class R2, class P2>
155[[nodiscard]] constexpr auto operator<=>(space<R1, P1> a, space<R2, P2> b) noexcept {
156 if constexpr (std::integral<R1> && std::integral<R2>) {
157 using Scale = std::ratio_divide<P1, P2>;
158 return (static_cast<__int128>(a.count()) * Scale::num)
159 <=> (static_cast<__int128>(b.count()) * Scale::den);
160 } else {
161 using C = std::common_type_t<decltype(a), decltype(b)>;
162 return C{a}.count() <=> C{b}.count();
163 }
164}
165
166template<class R1, class P1, class R2, class P2>
167[[nodiscard]] constexpr bool operator==(space<R1, P1> a, space<R2, P2> b) noexcept { return (a <=> b) == 0; }
169template<class R1, class P1, class R2, class P2>
170[[nodiscard]] constexpr auto operator+(space<R1, P1> a, space<R2, P2> b) noexcept {
171 using C = std::common_type_t<decltype(a), decltype(b)>;
172 return C{a} += C{b};
173}
174
175template<class R1, class P1, class R2, class P2>
176[[nodiscard]] constexpr auto operator-(space<R1, P1> a, space<R2, P2> b) noexcept {
177 using C = std::common_type_t<decltype(a), decltype(b)>;
178 return C{a} -= C{b};
179}
180
181template<class R, class P, class S> requires std::is_arithmetic_v<S>
182[[nodiscard]] constexpr auto operator*(space<R, P> a, S b) noexcept {
183 return space<std::common_type_t<R, S>, P>{a} *= b;
184}
185
186template<class R, class P, class S> requires std::is_arithmetic_v<S>
187[[nodiscard]] constexpr auto operator*(S a, space<R, P> b) noexcept { return b * a; }
189template<class R, class P, class S> requires std::is_arithmetic_v<S>
190[[nodiscard]] constexpr auto operator/(space<R, P> a, S b) noexcept {
191 return space<std::common_type_t<R, S>, P>{a} /= b;
192}
193
194template<class R1, class P1, class R2, class P2>
195[[nodiscard]] constexpr auto operator/(space<R1, P1> a, space<R2, P2> b) noexcept {
196 using C = std::common_type_t<decltype(a), decltype(b)>;
197 return detail::arithmetic<'/'>(C{a}.count(), C{b}.count());
198}
199
200template<std::integral R, class P, std::integral S>
201[[nodiscard]] constexpr auto operator%(space<R, P> a, S b) noexcept { return space<std::common_type_t<R, S>, P>{a} %= b; }
203template<std::integral R1, class P1, std::integral R2, class P2>
204[[nodiscard]] constexpr auto operator%(space<R1, P1> a, space<R2, P2> b) noexcept {
205 using C = std::common_type_t<decltype(a), decltype(b)>;
206 return C{a} %= C{b};
207}
208
209template<class To, class R, class P>
210[[nodiscard]] constexpr To floor(space<R, P> value) noexcept {
211 auto result = space_cast<To>(value);
212 if (result > value) --result;
213 return result;
214}
215
216template<class To, class R, class P>
217[[nodiscard]] constexpr To ceil(space<R, P> value) noexcept {
218 auto result = space_cast<To>(value);
219 if (result < value) ++result;
220 return result;
221}
222
223template<class To, class R, class P> requires std::integral<typename To::rep>
224[[nodiscard]] constexpr To round(space<R, P> value) noexcept {
225 auto const lo = floor<To>(value);
226 if (lo == value) return lo;
227 auto hi = lo; ++hi;
228 auto const down = value - lo, up = hi - value;
229 return down < up || (down == up && lo.count() % 2 == 0) ? lo : hi;
230}
231
232template<class R, class P> requires std::is_signed_v<R>
233[[nodiscard]] constexpr auto abs(space<R, P> value) noexcept { return value < value.zero() ? -value : value; }
234
235using kibi = std::ratio<1024>;
236using mebi = std::ratio<1048576>;
237using gibi = std::ratio<1073741824>;
246
248[[nodiscard]] consteval bytes operator""_B(unsigned long long n) noexcept { return bytes{n}; }
250[[nodiscard]] consteval space<long double, std::ratio<1>> operator""_B(long double n) noexcept { return space<long double, std::ratio<1>>{n}; }
252[[nodiscard]] consteval kilobytes operator""_kB(unsigned long long n) noexcept { return kilobytes{n}; }
254[[nodiscard]] consteval space<long double, std::kilo> operator""_kB(long double n) noexcept { return space<long double, std::kilo>{n}; }
256[[nodiscard]] consteval megabytes operator""_MB(unsigned long long n) noexcept { return megabytes{n}; }
258[[nodiscard]] consteval space<long double, std::mega> operator""_MB(long double n) noexcept { return space<long double, std::mega>{n}; }
260[[nodiscard]] consteval gigabytes operator""_GB(unsigned long long n) noexcept { return gigabytes{n}; }
262[[nodiscard]] consteval space<long double, std::giga> operator""_GB(long double n) noexcept { return space<long double, std::giga>{n}; }
264[[nodiscard]] consteval kibibytes operator""_KiB(unsigned long long n) noexcept { return kibibytes{n}; }
266[[nodiscard]] consteval space<long double, kibi> operator""_KiB(long double n) noexcept { return space<long double, kibi>{n}; }
268[[nodiscard]] consteval mebibytes operator""_MiB(unsigned long long n) noexcept { return mebibytes{n}; }
270[[nodiscard]] consteval space<long double, mebi> operator""_MiB(long double n) noexcept { return space<long double, mebi>{n}; }
272[[nodiscard]] consteval gibibytes operator""_GiB(unsigned long long n) noexcept { return gibibytes{n}; }
274[[nodiscard]] consteval space<long double, gibi> operator""_GiB(long double n) noexcept { return space<long double, gibi>{n}; }
275}
A count of storage units; Ratio measures one unit in bytes. Integer arithmetic and conversions termin...
Definition units.ccm:83
constexpr space()=default
Default-initialize the representation, as with chrono durations.
typename Ratio::type ratio
Definition units.ccm:90
constexpr space & operator++() noexcept
Add one unit.
Definition units.ccm:116
constexpr space operator-() const noexcept
Negate the count; unrepresentable negation terminates.
Definition units.ccm:114
constexpr space & operator*=(rep scalar) noexcept
Multiply by a scalar count.
Definition units.ccm:128
constexpr space & operator+=(space other) noexcept
Add an exactly convertible amount.
Definition units.ccm:124
static constexpr space zero() noexcept
Zero units.
Definition units.ccm:106
constexpr space(space< R, P > other) noexcept
Implicit conversion when the destination can express every source tick.
Definition units.ccm:101
constexpr space & operator/=(rep scalar) noexcept
Divide by a scalar count.
Definition units.ccm:130
constexpr space & operator-=(space other) noexcept
Subtract an exactly convertible amount.
Definition units.ccm:126
constexpr space operator--(int) noexcept
Subtract one unit, returning the previous count.
Definition units.ccm:122
static constexpr space min() noexcept
Least representable count.
Definition units.ccm:108
static constexpr space max() noexcept
Greatest representable count.
Definition units.ccm:110
constexpr space operator+() const noexcept
Preserve the count.
Definition units.ccm:112
constexpr space(R count) noexcept
Explicitly construct from a count, without implicit float-to-integer loss.
Definition units.ccm:96
constexpr space operator++(int) noexcept
Add one unit, returning the previous count.
Definition units.ccm:118
constexpr space & operator%=(space other) noexcept
Remainder after whole multiples of an amount.
Definition units.ccm:134
Rep rep
Count representation.
Definition units.ccm:89
constexpr space & operator%=(rep scalar) noexcept
Remainder of an integral count divided by a scalar.
Definition units.ccm:132
constexpr space & operator--() noexcept
Subtract one unit.
Definition units.ccm:120
Typed storage sizes, conversions and byte literals.
Definition units.ccm:21
space< std::size_t, std::mega > megabytes
Millions of bytes.
Definition units.ccm:240
std::ratio< 1024 > kibi
Binary kilo: 1024.
Definition units.ccm:235
constexpr auto operator*(space< R, P > a, S b) noexcept
Multiply an amount by a scalar, preserving its unit.
Definition units.ccm:182
constexpr bool operator==(space< R1, P1 > a, space< R2, P2 > b) noexcept
Equal amounts may have different units or representations.
Definition units.ccm:167
constexpr auto operator%(space< R, P > a, S b) noexcept
Integral remainder in the original unit.
Definition units.ccm:201
constexpr To round(space< R, P > value) noexcept
Nearest integral destination count; ties round to even.
Definition units.ccm:224
space< std::size_t, std::giga > gigabytes
Billions of bytes.
Definition units.ccm:241
space< std::size_t, mebi > mebibytes
Groups of 1048576 bytes.
Definition units.ccm:243
constexpr auto operator+(space< R1, P1 > a, space< R2, P2 > b) noexcept
Add amounts in their common unit.
Definition units.ccm:170
std::ratio< 1048576 > mebi
Binary mega: 1024 squared.
Definition units.ccm:236
constexpr auto operator/(space< R, P > a, S b) noexcept
Divide an amount by a scalar, preserving its unit.
Definition units.ccm:190
constexpr To ceil(space< R, P > value) noexcept
Least whole destination count not less than the source.
Definition units.ccm:217
constexpr To floor(space< R, P > value) noexcept
Greatest whole destination count not exceeding the source.
Definition units.ccm:210
constexpr auto operator<=>(space< R1, P1 > a, space< R2, P2 > b) noexcept
Compare amounts without overflowing an integral common-unit conversion.
Definition units.ccm:155
std::ratio< 1073741824 > gibi
Binary giga: 1024 cubed.
Definition units.ccm:237
space< std::size_t, std::ratio< JAM_PAGE_BYTES > > pages
Platform pages, fixed at build time.
Definition units.ccm:245
space< std::size_t, gibi > gibibytes
Groups of 1073741824 bytes.
Definition units.ccm:244
space< std::size_t, std::kilo > kilobytes
Thousands of bytes.
Definition units.ccm:239
space< std::size_t, kibi > kibibytes
Groups of 1024 bytes.
Definition units.ccm:242
constexpr auto operator-(space< R1, P1 > a, space< R2, P2 > b) noexcept
Subtract amounts in their common unit.
Definition units.ccm:176
constexpr To space_cast(space< R, P > value) noexcept
Explicit conversion; fractional integral ticks truncate toward zero.
Definition units.ccm:150
space< std::size_t > bytes
Whole bytes.
Definition units.ccm:238
constexpr auto abs(space< R, P > value) noexcept
Absolute value of a signed amount.
Definition units.ccm:233