1- #ifndef SOURCEMETA_JSONBINPACK_RUNTIME_NUMERIC_H_
2- #define SOURCEMETA_JSONBINPACK_RUNTIME_NUMERIC_H_
1+ #ifndef SOURCEMETA_JSONBINPACK_NUMERIC_INTEGRAL_H_
2+ #define SOURCEMETA_JSONBINPACK_NUMERIC_INTEGRAL_H_
33
44#include < cassert> // assert
55#include < cmath> // std::abs
66#include < cstdint> // std::uint8_t, std::int64_t, std::uint64_t
77#include < limits> // std::numeric_limits
88
9- // TODO: Move to src/numeric
10-
119namespace sourcemeta ::jsonbinpack {
1210
13- template <unsigned int T> constexpr auto uint_max = (2 << (T - 1 )) - 1 ;
14-
11+ // / @ingroup numeric
1512template <typename T> constexpr auto is_byte (const T value) noexcept -> bool {
1613 return value <= std::numeric_limits<std::uint8_t >::max ();
1714}
1815
16+ // / @ingroup numeric
17+ constexpr auto count_multiples (const std::int64_t minimum,
18+ const std::int64_t maximum,
19+ const std::int64_t multiplier) -> std::uint64_t {
20+ assert (minimum <= maximum);
21+ assert (multiplier > 0 );
22+ return static_cast <std::uint64_t >((maximum / multiplier) -
23+ ((minimum - 1 ) / multiplier));
24+ }
25+
26+ // / @ingroup numeric
27+ template <unsigned int T> constexpr auto uint_max = (2 << (T - 1 )) - 1 ;
28+
29+ // / @ingroup numeric
1930template <typename T>
2031constexpr auto is_within (const T value, const std::int64_t lower,
2132 const std::int64_t higher) noexcept -> bool {
2233 return value >= lower && value <= higher;
2334}
2435
36+ // / @ingroup numeric
2537template <typename T>
2638constexpr auto is_within (const T value, const std::uint64_t lower,
2739 const std::uint64_t higher) noexcept -> bool {
@@ -33,6 +45,7 @@ constexpr auto is_within(const T value, const std::uint64_t lower,
3345 }
3446}
3547
48+ // / @ingroup numeric
3649constexpr auto abs (const std::int64_t value) noexcept -> std::uint64_t {
3750 if (value < 0 ) {
3851 return static_cast <std::uint64_t >(value * -1 );
@@ -41,6 +54,7 @@ constexpr auto abs(const std::int64_t value) noexcept -> std::uint64_t {
4154 }
4255}
4356
57+ // / @ingroup numeric
4458constexpr auto divide_ceil (const std::int64_t dividend,
4559 const std::uint64_t divisor) noexcept
4660 -> std::int64_t {
@@ -68,6 +82,7 @@ constexpr auto divide_ceil(const std::int64_t dividend,
6882 }
6983}
7084
85+ // / @ingroup numeric
7186constexpr auto divide_floor (const std::int64_t dividend,
7287 const std::uint64_t divisor) noexcept
7388 -> std::int64_t {
@@ -88,6 +103,7 @@ constexpr auto divide_floor(const std::int64_t dividend,
88103 }
89104}
90105
106+ // / @ingroup numeric
91107constexpr auto closest_smallest_exponent (const std::uint64_t value,
92108 const std::uint8_t base,
93109 const std::uint8_t exponent_start,
0 commit comments