Skip to content

Commit b86a61f

Browse files
authored
Extract numeric helpers from src/runtime into src/numeric (#822)
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 1307ff7 commit b86a61f

23 files changed

+225
-221
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ if(JSONBINPACK_TESTS)
8383
find_package(GoogleTest REQUIRED)
8484
enable_testing()
8585

86+
if(JSONBINPACK_NUMERIC)
87+
add_subdirectory(test/numeric)
88+
endif()
89+
8690
if(JSONBINPACK_RUNTIME)
8791
add_subdirectory(test/runtime)
8892
endif()

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ configure: .always
1313
$(CMAKE) -S . -B ./build \
1414
-DCMAKE_BUILD_TYPE:STRING=$(PRESET) \
1515
-DCMAKE_COMPILE_WARNING_AS_ERROR:BOOL=ON \
16+
-DJSONBINPACK_NUMERIC:BOOL=ON \
1617
-DJSONBINPACK_RUNTIME:BOOL=ON \
1718
-DJSONBINPACK_COMPILER:BOOL=ON \
1819
-DJSONBINPACK_TESTS:BOOL=ON \

src/numeric/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
noa_library(NAMESPACE sourcemeta PROJECT jsonbinpack NAME numeric
2-
FOLDER "JSON BinPack/Numeric")
2+
FOLDER "JSON BinPack/Numeric"
3+
PRIVATE_HEADERS integral.h real.h zigzag.h)
34

45
if(JSONBINPACK_INSTALL)
56
noa_library_install(NAMESPACE sourcemeta PROJECT jsonbinpack NAME numeric)
Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
#ifndef SOURCEMETA_JSONBINPACK_NUMERIC_H_
22
#define SOURCEMETA_JSONBINPACK_NUMERIC_H_
33

4-
#include <cstdint> // std::uint8_t, std::int64_t
5-
#include <limits> // std::numeric_limits
6-
74
/// @defgroup numeric Numeric
85
/// @brief A comprehensive numeric library for JSON BinPack
96
///
@@ -13,23 +10,8 @@
1310
/// #include <sourcemeta/jsonbinpack/numeric.h>
1411
/// ```
1512

16-
namespace sourcemeta::jsonbinpack {
17-
18-
/// @ingroup numeric
19-
template <typename T> constexpr auto is_byte(const T value) noexcept -> bool {
20-
return value <= std::numeric_limits<std::uint8_t>::max();
21-
}
22-
23-
/// @ingroup numeric
24-
constexpr auto count_multiples(const std::int64_t minimum,
25-
const std::int64_t maximum,
26-
const std::int64_t multiplier) -> std::uint64_t {
27-
assert(minimum <= maximum);
28-
assert(multiplier > 0);
29-
return static_cast<std::uint64_t>((maximum / multiplier) -
30-
((minimum - 1) / multiplier));
31-
}
32-
33-
} // namespace sourcemeta::jsonbinpack
13+
#include <sourcemeta/jsonbinpack/numeric_integral.h>
14+
#include <sourcemeta/jsonbinpack/numeric_real.h>
15+
#include <sourcemeta/jsonbinpack/numeric_zigzag.h>
3416

3517
#endif

src/runtime/include/sourcemeta/jsonbinpack/runtime_numeric.h renamed to src/numeric/include/sourcemeta/jsonbinpack/numeric_integral.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,39 @@
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-
119
namespace sourcemeta::jsonbinpack {
1210

13-
template <unsigned int T> constexpr auto uint_max = (2 << (T - 1)) - 1;
14-
11+
/// @ingroup numeric
1512
template <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
1930
template <typename T>
2031
constexpr 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
2537
template <typename T>
2638
constexpr 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
3649
constexpr 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
4458
constexpr 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
7186
constexpr 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
91107
constexpr auto closest_smallest_exponent(const std::uint64_t value,
92108
const std::uint8_t base,
93109
const std::uint8_t exponent_start,

src/runtime/include/sourcemeta/jsonbinpack/runtime_encoder_real.h renamed to src/numeric/include/sourcemeta/jsonbinpack/numeric_real.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
#ifndef SOURCEMETA_JSONBINPACK_RUNTIME_ENCODER_REAL_H_
2-
#define SOURCEMETA_JSONBINPACK_RUNTIME_ENCODER_REAL_H_
1+
#ifndef SOURCEMETA_JSONBINPACK_NUMERIC_REAL_H_
2+
#define SOURCEMETA_JSONBINPACK_NUMERIC_REAL_H_
33

44
#include <cassert> // assert
55
#include <cmath> // std::modf, std::floor, std::isfinite
66
#include <concepts> // std::floating_point, std::integral
77

8-
// TODO: Move to src/numeric
9-
108
namespace sourcemeta::jsonbinpack {
119

1210
// IEEE764 floating-point encoding is not precise. Some real numbers
1311
// cannot be represented directly and thus approximations must be
1412
// used. Here, we abuse those imprecision characteristics for
1513
// space-efficiency by performing rounding on a very, very low
1614
// threshold.
15+
/// @ingroup numeric
1716
template <std::floating_point Real>
1817
constexpr auto correct_ieee764(const Real value) -> Real {
1918
assert(std::isfinite(value));
@@ -29,6 +28,7 @@ constexpr auto correct_ieee764(const Real value) -> Real {
2928
}
3029
}
3130

31+
/// @ingroup numeric
3232
template <std::integral Integer, std::floating_point Real>
3333
constexpr auto real_digits(Real value, std::uint64_t *point_position)
3434
-> Integer {

src/runtime/include/sourcemeta/jsonbinpack/runtime_zigzag.h renamed to src/numeric/include/sourcemeta/jsonbinpack/numeric_zigzag.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
#ifndef SOURCEMETA_JSONBINPACK_RUNTIME_ZIGZAG_H_
2-
#define SOURCEMETA_JSONBINPACK_RUNTIME_ZIGZAG_H_
1+
#ifndef SOURCEMETA_JSONBINPACK_NUMERIC_ZIGZAG_H_
2+
#define SOURCEMETA_JSONBINPACK_NUMERIC_ZIGZAG_H_
33

44
#include <cmath> // std::abs
55
#include <cstdint> // std::uint64_t, std::int64_t
66

7-
// TODO: Move to src/numeric
8-
97
namespace sourcemeta::jsonbinpack {
108

9+
/// @ingroup numeric
1110
constexpr auto zigzag_encode(const std::int64_t value) noexcept
1211
-> std::uint64_t {
1312
if (value >= 0) {
@@ -17,6 +16,7 @@ constexpr auto zigzag_encode(const std::int64_t value) noexcept
1716
return (static_cast<std::uint64_t>(std::abs(value)) * 2) - 1;
1817
}
1918

19+
/// @ingroup numeric
2020
constexpr auto zigzag_decode(const std::uint64_t value) noexcept
2121
-> std::int64_t {
2222
if (value % 2 == 0) {

src/runtime/CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ noa_library(NAMESPACE sourcemeta PROJECT jsonbinpack NAME runtime
22
FOLDER "JSON BinPack/Runtime"
33
PRIVATE_HEADERS
44
decoder.h decoder_basic.h
5-
encoder.h encoder_basic.h encoder_context.h encoder_real.h
5+
encoder.h encoder_basic.h encoder_context.h
66
plan.h plan_wrap.h parser.h
7-
zigzag.h varint.h numeric.h
7+
varint.h
88
SOURCES runtime_parser.cc runtime_parser_v1.h)
99

1010
if(JSONBINPACK_INSTALL)
@@ -13,3 +13,5 @@ endif()
1313

1414
target_link_libraries(sourcemeta_jsonbinpack_runtime PUBLIC
1515
sourcemeta::jsontoolkit::json)
16+
target_link_libraries(sourcemeta_jsonbinpack_runtime PUBLIC
17+
sourcemeta::jsonbinpack::numeric)

src/runtime/include/sourcemeta/jsonbinpack/runtime_decoder.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#ifndef SOURCEMETA_JSONBINPACK_RUNTIME_DECODER_H_
22
#define SOURCEMETA_JSONBINPACK_RUNTIME_DECODER_H_
33

4+
#include <sourcemeta/jsonbinpack/numeric.h>
5+
46
#include <sourcemeta/jsonbinpack/runtime_decoder_basic.h>
5-
#include <sourcemeta/jsonbinpack/runtime_numeric.h>
67
#include <sourcemeta/jsonbinpack/runtime_plan.h>
78
#include <sourcemeta/jsonbinpack/runtime_plan_wrap.h>
89

src/runtime/include/sourcemeta/jsonbinpack/runtime_decoder_basic.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
#define SOURCEMETA_JSONBINPACK_RUNTIME_DECODER_BASIC_H_
33
#ifndef DOXYGEN
44

5+
#include <sourcemeta/jsonbinpack/numeric.h>
6+
57
#include <sourcemeta/jsonbinpack/runtime_varint.h>
6-
#include <sourcemeta/jsonbinpack/runtime_zigzag.h>
78

89
#include <cassert> // assert
910
#include <cmath> // std::ceil

0 commit comments

Comments
 (0)