/src/quantlib/ql/math/randomnumbers/xoshiro256starstaruniformrng.cpp
Line | Count | Source |
1 | | /* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* |
4 | | Copyright (C) 2023 Ralf Konrad Eckel |
5 | | |
6 | | This file is part of QuantLib, a free-software/open-source library |
7 | | for financial quantitative analysts and developers - http://quantlib.org/ |
8 | | |
9 | | QuantLib is free software: you can redistribute it and/or modify it |
10 | | under the terms of the QuantLib license. You should have received a |
11 | | copy of the license along with this program; if not, please email |
12 | | <quantlib-dev@lists.sf.net>. The license is also available online at |
13 | | <https://www.quantlib.org/license.shtml>. |
14 | | |
15 | | This program is distributed in the hope that it will be useful, but WITHOUT |
16 | | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
17 | | FOR A PARTICULAR PURPOSE. See the license for more details. |
18 | | */ |
19 | | |
20 | | #include <ql/math/randomnumbers/seedgenerator.hpp> |
21 | | #include <ql/math/randomnumbers/xoshiro256starstaruniformrng.hpp> |
22 | | |
23 | | namespace QuantLib { |
24 | | |
25 | | namespace { |
26 | | |
27 | | // NOTE: The following copyright notice applies to the |
28 | | // original C implementation https://prng.di.unimi.it/splitmix64.c |
29 | | // that has been used for this class. |
30 | | |
31 | | /* Written in 2015 by Sebastiano Vigna (vigna@acm.org) |
32 | | |
33 | | To the extent possible under law, the author has dedicated all copyright |
34 | | and related and neighboring rights to this software to the public domain |
35 | | worldwide. This software is distributed without any warranty. |
36 | | |
37 | | See <http://creativecommons.org/publicdomain/zero/1.0/>. |
38 | | */ |
39 | | class SplitMix64 { |
40 | | public: |
41 | 0 | explicit SplitMix64(std::uint64_t x) : x_(x) {} |
42 | 0 | std::uint64_t next() const { |
43 | 0 | auto z = (x_ += 0x9e3779b97f4a7c15); |
44 | 0 | z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; |
45 | 0 | z = (z ^ (z >> 27)) * 0x94d049bb133111eb; |
46 | 0 | return z ^ (z >> 31); |
47 | 0 | }; |
48 | | |
49 | | private: |
50 | | mutable std::uint64_t x_; |
51 | | }; |
52 | | } |
53 | | |
54 | 0 | Xoshiro256StarStarUniformRng::Xoshiro256StarStarUniformRng(std::uint64_t seed) { |
55 | 0 | SplitMix64 splitMix64(seed != 0 ? seed : SeedGenerator::instance().get()); |
56 | 0 | s0_ = splitMix64.next(); |
57 | 0 | s1_ = splitMix64.next(); |
58 | 0 | s2_ = splitMix64.next(); |
59 | 0 | s3_ = splitMix64.next(); |
60 | 0 | } |
61 | | |
62 | | Xoshiro256StarStarUniformRng::Xoshiro256StarStarUniformRng(std::uint64_t s0, |
63 | | std::uint64_t s1, |
64 | | std::uint64_t s2, |
65 | | std::uint64_t s3) |
66 | 0 | : s0_(s0), s1_(s1), s2_(s2), s3_(s3) {} |
67 | | |
68 | | } |