Skip to content

Commit 831afce

Browse files
authored
Merge pull request #1865 from AntelopeIO/size_literals
UDLs for byte units (KiB, MiB, etc) and power-of-two
2 parents 11c2914 + ff80cca commit 831afce

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

libraries/libfc/include/fc/utility.hpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <type_traits>
88
#include <tuple>
99
#include <memory>
10+
#include <numeric>
1011

1112
#ifdef _MSC_VER
1213
#pragma warning(disable: 4482) // nonstandard extension used enum Name::Val, standard in C++11
@@ -204,6 +205,44 @@ namespace fc {
204205

205206
using yield_function_t = optional_delegate<void()>;
206207

208+
//TODO: these should really be consteval, but they're getting pulled in by a few of our c++17 compiled files
209+
namespace size_literals {
210+
namespace detail {
211+
constexpr unsigned long long multiply_pow2(const unsigned long long val, const unsigned long long shift) {
212+
if(shift >= std::numeric_limits<unsigned long long>::digits)
213+
throw std::invalid_argument("exponent for pow2 too large");
214+
215+
constexpr unsigned long long max_ull = std::numeric_limits<unsigned long long>::max();
216+
217+
if(val > (max_ull >> shift))
218+
throw std::invalid_argument("literal too large");
219+
220+
return val << shift;
221+
}
222+
}
223+
224+
constexpr unsigned long long operator""_KiB(const unsigned long long val) {
225+
return detail::multiply_pow2(val, 10);
226+
}
227+
constexpr unsigned long long operator""_MiB(const unsigned long long val) {
228+
return detail::multiply_pow2(val, 20);
229+
}
230+
constexpr unsigned long long operator""_GiB(const unsigned long long val) {
231+
return detail::multiply_pow2(val, 30);
232+
}
233+
constexpr unsigned long long operator""_TiB(const unsigned long long val) {
234+
return detail::multiply_pow2(val, 40);
235+
}
236+
237+
constexpr unsigned long long operator""_pow2(const unsigned long long n) {
238+
return detail::multiply_pow2(1, n);
239+
}
240+
241+
static_assert(4_MiB == 4*1024*1024);
242+
static_assert(16_pow2 == 64*1024);
243+
static_assert(63_pow2 == 9223372036854775808ull);
244+
}
245+
207246
}
208247

209248
// outside of namespace fc becuase of VC++ conflict with std::swap

0 commit comments

Comments
 (0)