2018-02-04 03:22:01 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
2017-08-19 22:27:51 +02:00
|
|
|
|
|
|
|
#pragma once
|
2018-01-12 12:21:58 +01:00
|
|
|
#define HAVE_IRCD_RAND_H
|
2017-08-19 22:27:51 +02:00
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// Some character set dictionaries
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::rand::dict
|
2017-08-19 22:27:51 +02:00
|
|
|
{
|
|
|
|
extern const std::string alnum;
|
|
|
|
extern const std::string alpha;
|
|
|
|
extern const std::string upper;
|
|
|
|
extern const std::string lower;
|
|
|
|
extern const std::string numeric;
|
|
|
|
}
|
|
|
|
|
2017-09-12 18:37:44 +02:00
|
|
|
/// Tools for randomization
|
2017-08-28 23:51:22 +02:00
|
|
|
namespace ircd::rand
|
|
|
|
{
|
2020-12-05 16:07:21 +01:00
|
|
|
struct xoshiro256p;
|
|
|
|
|
2020-10-07 00:46:36 +02:00
|
|
|
// Interface state.
|
2017-09-12 18:37:44 +02:00
|
|
|
extern std::random_device device;
|
|
|
|
extern std::mt19937_64 mt;
|
|
|
|
|
2020-10-07 00:46:36 +02:00
|
|
|
// Random integer
|
2020-12-11 21:20:29 +01:00
|
|
|
uint64_t integer() noexcept;
|
|
|
|
uint64_t integer(const uint64_t &min, const uint64_t &max) noexcept; // inclusive
|
|
|
|
uint64_t integer(xoshiro256p &) noexcept;
|
2017-08-19 22:27:51 +02:00
|
|
|
|
2020-10-07 11:59:30 +02:00
|
|
|
// Random vector
|
2020-12-11 21:20:29 +01:00
|
|
|
template<class T> T vector() noexcept = delete;
|
|
|
|
template<> u128x1 vector() noexcept;
|
|
|
|
template<> u256x1 vector() noexcept;
|
|
|
|
template<> u512x1 vector() noexcept;
|
|
|
|
|
|
|
|
// Random fill of buffer
|
|
|
|
const_buffer fill(const mutable_buffer &out) noexcept;
|
|
|
|
|
|
|
|
// Random fill of array
|
|
|
|
template<class T,
|
|
|
|
size_t S>
|
|
|
|
decltype(auto) fill(T (&buf)[S]) noexcept;
|
2020-10-07 11:59:30 +02:00
|
|
|
|
2017-08-28 23:51:22 +02:00
|
|
|
// Random character from dictionary
|
2020-12-11 21:20:29 +01:00
|
|
|
char character(const std::string &dict = dict::alnum) noexcept;
|
2017-08-19 22:27:51 +02:00
|
|
|
|
2020-10-07 00:46:36 +02:00
|
|
|
// Random string from dictionary, fills buffer
|
2020-12-11 21:20:29 +01:00
|
|
|
string_view string(const mutable_buffer &out, const std::string &dict) noexcept;
|
2017-08-28 23:51:22 +02:00
|
|
|
}
|
2017-08-19 22:27:51 +02:00
|
|
|
|
2020-12-05 16:07:21 +01:00
|
|
|
struct ircd::rand::xoshiro256p
|
|
|
|
{
|
2020-12-11 21:20:29 +01:00
|
|
|
uint64_t s[4];
|
2020-12-05 16:07:21 +01:00
|
|
|
|
2020-12-11 21:20:29 +01:00
|
|
|
xoshiro256p() noexcept;
|
2020-12-05 16:07:21 +01:00
|
|
|
};
|
|
|
|
|
2020-12-11 21:20:29 +01:00
|
|
|
template<class T,
|
|
|
|
size_t S>
|
|
|
|
inline decltype(auto)
|
|
|
|
ircd::rand::fill(T (&buf)[S])
|
|
|
|
noexcept
|
2020-12-05 16:07:21 +01:00
|
|
|
{
|
2020-12-11 21:20:29 +01:00
|
|
|
static_assert
|
|
|
|
(
|
|
|
|
sizeof(buf) == sizeof(T) * S
|
|
|
|
);
|
2020-12-05 16:07:21 +01:00
|
|
|
|
2020-12-11 21:20:29 +01:00
|
|
|
const mutable_buffer mb
|
|
|
|
{
|
|
|
|
reinterpret_cast<char *>(buf), sizeof(buf)
|
|
|
|
};
|
2020-12-05 16:07:21 +01:00
|
|
|
|
2020-12-11 21:20:29 +01:00
|
|
|
fill(mb);
|
|
|
|
return buf;
|
|
|
|
}
|
2020-12-05 16:07:21 +01:00
|
|
|
|
2020-10-07 00:46:36 +02:00
|
|
|
/// Random character from dictionary
|
2017-08-19 22:27:51 +02:00
|
|
|
inline char
|
|
|
|
ircd::rand::character(const std::string &dict)
|
2020-12-11 21:20:29 +01:00
|
|
|
noexcept
|
2017-08-19 22:27:51 +02:00
|
|
|
{
|
|
|
|
assert(!dict.empty());
|
2020-10-07 00:46:36 +02:00
|
|
|
const auto pos
|
|
|
|
{
|
|
|
|
integer(0, dict.size() - 1)
|
|
|
|
};
|
|
|
|
|
2020-12-11 21:20:29 +01:00
|
|
|
assert(pos < dict.size());
|
|
|
|
return dict[pos];
|
2017-08-19 22:27:51 +02:00
|
|
|
}
|
|
|
|
|
2020-12-11 21:20:29 +01:00
|
|
|
inline
|
|
|
|
ircd::rand::xoshiro256p::xoshiro256p()
|
|
|
|
noexcept
|
2017-08-19 22:27:51 +02:00
|
|
|
{
|
2020-12-11 21:20:29 +01:00
|
|
|
fill(s);
|
2017-08-19 22:27:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline uint64_t
|
2020-12-11 21:20:29 +01:00
|
|
|
ircd::rand::integer(xoshiro256p &state)
|
|
|
|
noexcept
|
2017-08-19 22:27:51 +02:00
|
|
|
{
|
2020-12-11 21:20:29 +01:00
|
|
|
auto &s(state.s);
|
|
|
|
|
|
|
|
const u64
|
|
|
|
ret(s[0] + s[3]),
|
|
|
|
t(s[1] << 17);
|
|
|
|
|
|
|
|
s[2] ^= s[0];
|
|
|
|
s[3] ^= s[1];
|
|
|
|
s[1] ^= s[2];
|
|
|
|
s[0] ^= s[3];
|
|
|
|
s[2] ^= t;
|
|
|
|
|
|
|
|
#if __has_builtin(__builtin_rotateleft64)
|
|
|
|
s[3] = __builtin_rotateleft64(s[3], 45);
|
|
|
|
#else
|
|
|
|
s[3] = (s[3] << 45) | (s[3] >> (64 - 45));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return ret;
|
2017-08-19 22:27:51 +02:00
|
|
|
}
|