Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@
[submodule "deps/type_safe"]
path = deps/type_safe
url = https://github.com/foonathan/type_safe
[submodule "deps/int128"]
path = deps/int128
url = https://github.com/cppalliance/int128
9 changes: 9 additions & 0 deletions deps/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,14 @@ def build_type_safe(env):
env.exposed_includes += env.type_safe["INCPATH"]


def build_int128(env):
include_path = "int128/include"
env.int128 = {}
env.int128["INCPATH"] = [env.Dir(include_path)]
env.Append(CPPPATH=env.int128["INCPATH"])
env.exposed_includes += env.int128["INCPATH"]


def link_tbb(env):
import sys

Expand All @@ -271,4 +279,5 @@ build_memory(env)
build_spdlog(env)
build_xoshiro(env)
build_type_safe(env)
build_int128(env)
link_tbb(env)
1 change: 1 addition & 0 deletions deps/int128
Submodule int128 added at ea0f85
215 changes: 0 additions & 215 deletions tests/src/core/random/ExtendedMath.hpp

This file was deleted.

18 changes: 6 additions & 12 deletions tests/src/core/random/WeightedSampling.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#include "openvic-simulation/types/fixed_point/FixedPoint.hpp"
#include "openvic-simulation/core/random/WeightedSampling.hpp"

#include "core/random/ExtendedMath.hpp"

#include <cstdint>
#include <limits>
#include <boost/int128.hpp>

#include <snitch/snitch_macros_check.hpp>
#include <snitch/snitch_macros_test_case.hpp>

using namespace OpenVic;
using namespace OpenVic::testing;

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

Expand Down Expand Up @@ -46,16 +44,12 @@ TEST_CASE("WeightedSampling weights", "[WeightedSampling]") {
fixed_point_t cumulative_weight = 0;
for (size_t i = 0; i < weights.size(); ++i) {
cumulative_weight += weights[i];
const Int96DivisionResult random_value = portable_int96_div_int64(
portable_int64_mult_uint32_96bit(
cumulative_weight.get_raw_value(),
max_random_value
),
weights_sum.get_raw_value()
);
assert(!random_value.quotient_overflow);
const boost::int128::int128_t cumulative_weight_128 = cumulative_weight.get_raw_value();
const boost::int128::int128_t max_random_value_128 = max_random_value;
const boost::int128::int128_t weights_sum_128 = weights_sum.get_raw_value();
const boost::int128::int128_t random_value = cumulative_weight_128 * max_random_value_128 / weights_sum_128;
CHECK(sample_weighted_index(
static_cast<uint32_t>(random_value.quotient),
static_cast<uint32_t>(random_value),
weights,
weights_sum
) == i);
Expand Down