Skip to content

Commit

Permalink
merge with head
Browse files Browse the repository at this point in the history
  • Loading branch information
klausspanderen committed Nov 19, 2023
2 parents b0be193 + 9ea7822 commit 53863ae
Show file tree
Hide file tree
Showing 223 changed files with 5,676 additions and 10,840 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ set(QL_INSTALL_CMAKEDIR "lib/cmake/${PACKAGE_NAME}" CACHE STRING
option(QL_BUILD_BENCHMARK "Build benchmark" ON)
option(QL_BUILD_EXAMPLES "Build examples" ON)
option(QL_BUILD_TEST_SUITE "Build test suite" ON)
option(QL_BUILD_FUZZ_TEST_SUITE "Build fuzz test suite" OFF)
option(QL_ENABLE_OPENMP "Detect and use OpenMP" OFF)
option(QL_ENABLE_PARALLEL_UNIT_TEST_RUNNER "Enable the parallel unit test runner" OFF)
option(QL_ENABLE_SESSIONS "Singletons return different instances for different sessions" OFF)
Expand Down Expand Up @@ -269,6 +270,10 @@ if (QL_BUILD_TEST_SUITE OR QL_BUILD_BENCHMARK)
add_subdirectory(test-suite)
endif()

if ('${CMAKE_CXX_COMPILER_ID}' MATCHES 'Clang' AND QL_BUILD_FUZZ_TEST_SUITE)
add_subdirectory(fuzz-test-suite)
endif()

# CPack support (make package, make package_source)
set(CPACK_PACKAGE_VERSION_MAJOR ${QUANTLIB_VERSION_MAJOR})
set(CPACK_PACKAGE_VERSION_MINOR ${QUANTLIB_VERSION_MINOR})
Expand Down
40 changes: 16 additions & 24 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,51 +174,49 @@
{
"name": "linux-clang-ninja-debug",
"inherits": [
"linux-clang-debug",
"ninja"
"ninja",
"linux-clang-debug"
]
},
{
"name": "linux-clang-ninja-release",
"inherits": [
"linux-clang-release",
"ninja"
"ninja",
"linux-clang-release"
]
},
{
"name": "linux-clang-ninja-relwithdebinfo",
"inherits": [
"linux-clang-relwithdebinfo",
"ninja"
"ninja",
"linux-clang-relwithdebinfo"
]
},
{
"name": "linux-gcc-ninja-debug",
"inherits": [
"linux-gcc-debug",
"ninja"
"ninja",
"linux-gcc-debug"
]
},
{
"name": "linux-gcc-ninja-release",
"inherits": [
"linux-gcc-release",
"ninja"
"ninja",
"linux-gcc-release"
]
},
{
"name": "linux-gcc-ninja-relwithdebinfo",
"inherits": [
"linux-gcc-relwithdebinfo",
"ninja"
"ninja",
"linux-gcc-relwithdebinfo"
]
},
{
"name": "linux-gcc-debug-with-clang-tidy",
"inherits": [
"linux-gcc-base",
"make",
"_debug"
"linux-gcc-debug"
],
"cacheVariables": {
"QL_CLANG_TIDY_OPTIONS": "-warnings-as-errors=*",
Expand Down Expand Up @@ -276,9 +274,7 @@
{
"name": "linux-ci-build-with-clang-tidy",
"inherits": [
"linux-gcc-base",
"make",
"_debug"
"linux-gcc-debug"
],
"cacheVariables": {
"BOOST_ROOT": "/usr",
Expand All @@ -292,9 +288,7 @@
{
"name": "linux-ci-build-with-nonstandard-options",
"inherits": [
"linux-gcc-base",
"ninja",
"_release"
"linux-gcc-ninja-release"
],
"cacheVariables": {
"BOOST_ROOT": "/usr",
Expand All @@ -319,9 +313,7 @@
{
"name": "windows-ci-build-with-nonstandard-options",
"inherits": [
"windows-msvc-base",
"ninja",
"_release"
"windows-msvc-release"
],
"cacheVariables": {
"CMAKE_CXX_STANDARD": "17",
Expand Down
2 changes: 2 additions & 0 deletions QuantLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,7 @@
<ClInclude Include="ql\math\quadratic.hpp" />
<ClInclude Include="ql\math\randomnumbers\all.hpp" />
<ClInclude Include="ql\math\randomnumbers\boxmullergaussianrng.hpp" />
<ClInclude Include="ql\math\randomnumbers\burley2020sobolrsg.hpp" />
<ClInclude Include="ql\math\randomnumbers\centrallimitgaussianrng.hpp" />
<ClInclude Include="ql\math\randomnumbers\faurersg.hpp" />
<ClInclude Include="ql\math\randomnumbers\haltonrsg.hpp" />
Expand Down Expand Up @@ -2311,6 +2312,7 @@
<ClCompile Include="ql\math\polynomialmathfunction.cpp" />
<ClCompile Include="ql\math\primenumbers.cpp" />
<ClCompile Include="ql\math\quadratic.cpp" />
<ClCompile Include="ql\math\randomnumbers\burley2020sobolrsg.cpp" />
<ClCompile Include="ql\math\randomnumbers\faurersg.cpp" />
<ClCompile Include="ql\math\randomnumbers\haltonrsg.cpp" />
<ClCompile Include="ql\math\randomnumbers\knuthuniformrng.cpp" />
Expand Down
6 changes: 6 additions & 0 deletions QuantLib.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -1242,6 +1242,9 @@
<ClInclude Include="ql\math\randomnumbers\boxmullergaussianrng.hpp">
<Filter>math\randomnumbers</Filter>
</ClInclude>
<ClInclude Include="ql\math\randomnumbers\burley2020sobolrsg.hpp">
<Filter>math\randomnumbers</Filter>
</ClInclude>
<ClInclude Include="ql\math\randomnumbers\centrallimitgaussianrng.hpp">
<Filter>math\randomnumbers</Filter>
</ClInclude>
Expand Down Expand Up @@ -4967,6 +4970,9 @@
<ClCompile Include="ql\math\matrixutilities\tqreigendecomposition.cpp">
<Filter>math\matrixutilities</Filter>
</ClCompile>
<ClCompile Include="ql\math\randomnumbers\burley2020sobolrsg.cpp">
<Filter>math\randomnumbers</Filter>
</ClCompile>
<ClCompile Include="ql\math\randomnumbers\faurersg.cpp">
<Filter>math\randomnumbers</Filter>
</ClCompile>
Expand Down
21 changes: 21 additions & 0 deletions fuzz-test-suite/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Determine the flags for fuzzing. Use OSS-Fuzz's configuration if available, otherwise fall back to defaults.
if(DEFINED ENV{LIB_FUZZING_ENGINE})
set(FUZZING_ENGINE $ENV{LIB_FUZZING_ENGINE})
set(FUZZING_COMPILE_FLAGS "")
set(FUZZING_LINK_FLAGS "${FUZZING_ENGINE}")
else()
set(FUZZING_COMPILE_FLAGS "-fsanitize=fuzzer")
set(FUZZING_LINK_FLAGS "-fsanitize=fuzzer")
endif()

# Define the fuzz target
add_executable(DateParserFuzzer dateparserfuzzer.cpp)

# Apply the determined flags
set_target_properties(DateParserFuzzer PROPERTIES
COMPILE_FLAGS "${FUZZING_COMPILE_FLAGS}"
LINK_FLAGS "${FUZZING_LINK_FLAGS}"
)

# Link QuantLib and any other necessary libraries
target_link_libraries(DateParserFuzzer ql_library ${QL_THREAD_LIBRARIES})
24 changes: 24 additions & 0 deletions fuzz-test-suite/dateparserfuzzer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <ql/time/asx.hpp>
#include <ql/time/date.hpp>
#include <ql/time/ecb.hpp>
#include <ql/time/imm.hpp>
#include <ql/time/timeunit.hpp>
#include <ql/utilities/dataparsers.hpp>
#include <string>

#ifndef __clang__
#pragma message("Fuzzer headers are available from clang, other compilers have not been tested.")
#endif
#include <fuzzer/FuzzedDataProvider.h>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
FuzzedDataProvider fdp(data, size);
constexpr int kMaxString = 100;
try {
(void)QuantLib::DateParser::parseFormatted(fdp.ConsumeRandomLengthString(kMaxString), "%Y-%m-%d");
(void)QuantLib::DateParser::parseISO(fdp.ConsumeRandomLengthString(kMaxString));
} catch (const std::exception& e) {
// Handle or ignore exceptions
}
return 0;
}
2 changes: 2 additions & 0 deletions ql/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ set(QL_SOURCES
math/polynomialmathfunction.cpp
math/primenumbers.cpp
math/quadratic.cpp
math/randomnumbers/burley2020sobolrsg.cpp
math/randomnumbers/faurersg.cpp
math/randomnumbers/haltonrsg.cpp
math/randomnumbers/knuthuniformrng.cpp
Expand Down Expand Up @@ -1518,6 +1519,7 @@ set(QL_HEADERS
math/primenumbers.hpp
math/quadratic.hpp
math/randomnumbers/boxmullergaussianrng.hpp
math/randomnumbers/burley2020sobolrsg.hpp
math/randomnumbers/centrallimitgaussianrng.hpp
math/randomnumbers/faurersg.hpp
math/randomnumbers/haltonrsg.hpp
Expand Down
6 changes: 4 additions & 2 deletions ql/math/randomnumbers/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ this_includedir=${includedir}/${subdir}
this_include_HEADERS = \
all.hpp \
boxmullergaussianrng.hpp \
burley2020sobolrsg.hpp \
centrallimitgaussianrng.hpp \
faurersg.hpp \
haltonrsg.hpp \
Expand All @@ -27,8 +28,9 @@ this_include_HEADERS = \
xoshiro256starstaruniformrng.hpp

cpp_files = \
faurersg.cpp \
haltonrsg.cpp \
faurersg.cpp \
haltonrsg.cpp \
burley2020sobolrsg.cpp \
knuthuniformrng.cpp \
latticersg.cpp \
latticerules.cpp \
Expand Down
1 change: 1 addition & 0 deletions ql/math/randomnumbers/all.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* Add the files to be included into Makefile.am instead. */

#include <ql/math/randomnumbers/boxmullergaussianrng.hpp>
#include <ql/math/randomnumbers/burley2020sobolrsg.hpp>
#include <ql/math/randomnumbers/centrallimitgaussianrng.hpp>
#include <ql/math/randomnumbers/faurersg.hpp>
#include <ql/math/randomnumbers/haltonrsg.hpp>
Expand Down
153 changes: 153 additions & 0 deletions ql/math/randomnumbers/burley2020sobolrsg.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */

/*
Copyright (C) 2023 Peter Caspers
This file is part of QuantLib, a free-software/open-source library
for financial quantitative analysts and developers - http://quantlib.org/
QuantLib is free software: you can redistribute it and/or modify it
under the terms of the QuantLib license. You should have received a
copy of the license along with this program; if not, please email
<[email protected]>. The license is also available online at
<http://quantlib.org/license.shtml>.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the license for more details.
*/

#include <ql/math/randomnumbers/burley2020sobolrsg.hpp>
#include <ql/math/randomnumbers/mt19937uniformrng.hpp>
#include <ql/errors.hpp>

namespace QuantLib {

Burley2020SobolRsg::Burley2020SobolRsg(Size dimensionality,
unsigned long seed,
SobolRsg::DirectionIntegers directionIntegers,
unsigned long scrambleSeed)
: dimensionality_(dimensionality), seed_(seed), directionIntegers_(directionIntegers),
integerSequence_(dimensionality), sequence_(std::vector<Real>(dimensionality), 1.0) {
reset();
group4Seeds_.resize((dimensionality_ - 1) / 4 + 1);
MersenneTwisterUniformRng mt(scrambleSeed);
for (auto& s : group4Seeds_) {
s = static_cast<std::uint32_t>(mt.nextInt32());
}
}

void Burley2020SobolRsg::reset() const {
sobolRsg_ = ext::make_shared<SobolRsg>(dimensionality_, seed_, directionIntegers_, false);
nextSequenceCounter_ = 0;
}

const std::vector<std::uint32_t>& Burley2020SobolRsg::skipTo(std::uint32_t n) const {
reset();
for (Size k = 0; k < n + 1; ++k) {
nextInt32Sequence();
}
return integerSequence_;
}

namespace {

// for reverseBits() see http://graphics.stanford.edu/~seander/bithacks.html#BitReverseTable

static const std::uint8_t bitReverseTable[] = {
0u, 128u, 64u, 192u, 32u, 160u, 96u, 224u, 16u, 144u, 80u, 208u, 48u, 176u,
112u, 240u, 8u, 136u, 72u, 200u, 40u, 168u, 104u, 232u, 24u, 152u, 88u, 216u,
56u, 184u, 120u, 248u, 4u, 132u, 68u, 196u, 36u, 164u, 100u, 228u, 20u, 148u,
84u, 212u, 52u, 180u, 116u, 244u, 12u, 140u, 76u, 204u, 44u, 172u, 108u, 236u,
28u, 156u, 92u, 220u, 60u, 188u, 124u, 252u, 2u, 130u, 66u, 194u, 34u, 162u,
98u, 226u, 18u, 146u, 82u, 210u, 50u, 178u, 114u, 242u, 10u, 138u, 74u, 202u,
42u, 170u, 106u, 234u, 26u, 154u, 90u, 218u, 58u, 186u, 122u, 250u, 6u, 134u,
70u, 198u, 38u, 166u, 102u, 230u, 22u, 150u, 86u, 214u, 54u, 182u, 118u, 246u,
14u, 142u, 78u, 206u, 46u, 174u, 110u, 238u, 30u, 158u, 94u, 222u, 62u, 190u,
126u, 254u, 1u, 129u, 65u, 193u, 33u, 161u, 97u, 225u, 17u, 145u, 81u, 209u,
49u, 177u, 113u, 241u, 9u, 137u, 73u, 201u, 41u, 169u, 105u, 233u, 25u, 153u,
89u, 217u, 57u, 185u, 121u, 249u, 5u, 133u, 69u, 197u, 37u, 165u, 101u, 229u,
21u, 149u, 85u, 213u, 53u, 181u, 117u, 245u, 13u, 141u, 77u, 205u, 45u, 173u,
109u, 237u, 29u, 157u, 93u, 221u, 61u, 189u, 125u, 253u, 3u, 131u, 67u, 195u,
35u, 163u, 99u, 227u, 19u, 147u, 83u, 211u, 51u, 179u, 115u, 243u, 11u, 139u,
75u, 203u, 43u, 171u, 107u, 235u, 27u, 155u, 91u, 219u, 59u, 187u, 123u, 251u,
7u, 135u, 71u, 199u, 39u, 167u, 103u, 231u, 23u, 151u, 87u, 215u, 55u, 183u,
119u, 247u, 15u, 143u, 79u, 207u, 47u, 175u, 111u, 239u, 31u, 159u, 95u, 223u,
63u, 191u, 127u, 255u};

inline std::uint32_t reverseBits(std::uint32_t x) {
return (bitReverseTable[x & 0xff] << 24) | (bitReverseTable[(x >> 8) & 0xff] << 16) |
(bitReverseTable[(x >> 16) & 0xff] << 8) | (bitReverseTable[(x >> 24) & 0xff]);
}

inline std::uint32_t laine_karras_permutation(std::uint32_t x, std::uint32_t seed) {
x += seed;
x ^= x * 0x6c50b47cu;
x ^= x * 0xb82f1e52u;
x ^= x * 0xc7afe638u;
x ^= x * 0x8d22f6e6u;
return x;
}

inline std::uint32_t nested_uniform_scramble(std::uint32_t x, std::uint32_t seed) {
x = reverseBits(x);
x = laine_karras_permutation(x, seed);
x = reverseBits(x);
return x;
}

// the results depend a lot on the details of the hash_combine() function that is used
// we use hash_combine() calling hash(), hash_mix() as implemented here:
// https://github.com/boostorg/container_hash/blob/boost-1.83.0/include/boost/container_hash/hash.hpp#L560
// https://github.com/boostorg/container_hash/blob/boost-1.83.0/include/boost/container_hash/hash.hpp#L115
// https://github.com/boostorg/container_hash/blob/boost-1.83.0/include/boost/container_hash/detail/hash_mix.hpp#L67

inline std::uint64_t local_hash_mix(std::uint64_t x) {
const std::uint64_t m = 0xe9846af9b1a615d;
x ^= x >> 32;
x *= m;
x ^= x >> 32;
x *= m;
x ^= x >> 28;
return x;
}

inline std::uint64_t local_hash(const std::uint64_t v) {
std::uint64_t seed = 0;
seed = (v >> 32) + local_hash_mix(seed);
seed = (v & 0xFFFFFFFF) + local_hash_mix(seed);
return seed;
}

inline std::uint64_t local_hash_combine(std::uint64_t x, const uint64_t v) {
return local_hash_mix(x + 0x9e3779b9 + local_hash(v));
}
}

const std::vector<std::uint32_t>& Burley2020SobolRsg::nextInt32Sequence() const {
auto n = nested_uniform_scramble(nextSequenceCounter_, group4Seeds_[0]);
const auto& seq = sobolRsg_->skipTo(n);
std::copy(seq.begin(), seq.end(), integerSequence_.begin());
Size i = 0, group = 0;
do {
std::uint64_t seed = group4Seeds_[group++];
for (Size g = 0; g < 4 && i < dimensionality_; ++g, ++i) {
seed = local_hash_combine(seed, g);
integerSequence_[i] =
nested_uniform_scramble(integerSequence_[i], static_cast<std::uint32_t>(seed));
}
} while (i < dimensionality_);
QL_REQUIRE(++nextSequenceCounter_ != 0,
"Burley2020SobolRsg::nextIn32Sequence(): period exceeded");
return integerSequence_;
}

const SobolRsg::sample_type& Burley2020SobolRsg::nextSequence() const {
const std::vector<std::uint32_t>& v = nextInt32Sequence();
// normalize to get a double in (0,1)
for (Size k = 0; k < dimensionality_; ++k) {
sequence_.value[k] = static_cast<double>(v[k]) / 4294967296.0;
}
return sequence_;
}
}
Loading

0 comments on commit 53863ae

Please sign in to comment.