Skip to content

Commit fbd962f

Browse files
authored
Merge pull request #686 from OpenVicProject/import_int128
Add boost::int128
2 parents 90c095f + 0cd285a commit fbd962f

File tree

5 files changed

+19
-227
lines changed

5 files changed

+19
-227
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@
3939
[submodule "deps/type_safe"]
4040
path = deps/type_safe
4141
url = https://github.com/foonathan/type_safe
42+
[submodule "deps/int128"]
43+
path = deps/int128
44+
url = https://github.com/cppalliance/int128

deps/SCsub

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ def build_type_safe(env):
251251
env.exposed_includes += env.type_safe["INCPATH"]
252252

253253

254+
def build_int128(env):
255+
include_path = "int128/include"
256+
env.int128 = {}
257+
env.int128["INCPATH"] = [env.Dir(include_path)]
258+
env.Append(CPPPATH=env.int128["INCPATH"])
259+
env.exposed_includes += env.int128["INCPATH"]
260+
261+
254262
def link_tbb(env):
255263
import sys
256264

@@ -271,4 +279,5 @@ build_memory(env)
271279
build_spdlog(env)
272280
build_xoshiro(env)
273281
build_type_safe(env)
282+
build_int128(env)
274283
link_tbb(env)

deps/int128

Submodule int128 added at ea0f85e

tests/src/core/random/ExtendedMath.hpp

Lines changed: 0 additions & 215 deletions
This file was deleted.

tests/src/core/random/WeightedSampling.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
22
#include "openvic-simulation/core/random/WeightedSampling.hpp"
33

4-
#include "core/random/ExtendedMath.hpp"
5-
64
#include <cstdint>
75
#include <limits>
6+
#include <boost/int128.hpp>
87

98
#include <snitch/snitch_macros_check.hpp>
109
#include <snitch/snitch_macros_test_case.hpp>
1110

1211
using namespace OpenVic;
13-
using namespace OpenVic::testing;
1412

1513
constexpr uint32_t max_random_value = std::numeric_limits<uint32_t>().max();
1614

@@ -46,16 +44,12 @@ TEST_CASE("WeightedSampling weights", "[WeightedSampling]") {
4644
fixed_point_t cumulative_weight = 0;
4745
for (size_t i = 0; i < weights.size(); ++i) {
4846
cumulative_weight += weights[i];
49-
const Int96DivisionResult random_value = portable_int96_div_int64(
50-
portable_int64_mult_uint32_96bit(
51-
cumulative_weight.get_raw_value(),
52-
max_random_value
53-
),
54-
weights_sum.get_raw_value()
55-
);
56-
assert(!random_value.quotient_overflow);
47+
const boost::int128::int128_t cumulative_weight_128 = cumulative_weight.get_raw_value();
48+
const boost::int128::int128_t max_random_value_128 = max_random_value;
49+
const boost::int128::int128_t weights_sum_128 = weights_sum.get_raw_value();
50+
const boost::int128::int128_t random_value = cumulative_weight_128 * max_random_value_128 / weights_sum_128;
5751
CHECK(sample_weighted_index(
58-
static_cast<uint32_t>(random_value.quotient),
52+
static_cast<uint32_t>(random_value),
5953
weights,
6054
weights_sum
6155
) == i);

0 commit comments

Comments
 (0)