mirror of
https://github.com/trapexit/mergerfs.git
synced 2026-10-19 08:15:50 +00:00
73 lines
1.5 KiB
C++
73 lines
1.5 KiB
C++
/*
|
|
ISC License
|
|
|
|
Copyright (c) 2026, Antonio SJ Musumeci <trapexit@spawn.link>
|
|
|
|
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 appear in all copies.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
|
|
#include "rnd.hpp"
|
|
|
|
#include "rapidhash/rapidhash.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
static u64 G_SEED;
|
|
|
|
__attribute__((constructor))
|
|
static
|
|
void
|
|
_constructor()
|
|
{
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv,NULL);
|
|
|
|
G_SEED = tv.tv_sec;
|
|
G_SEED <<= 32;
|
|
G_SEED |= tv.tv_usec;
|
|
}
|
|
|
|
// Lifted from wyhash.h's wyrand()
|
|
static
|
|
u64
|
|
_rapidhash_rand(u64 *seed_)
|
|
{
|
|
*seed_ += 0x2d358dccaa6c78a5ull;
|
|
return rapid_mix(*seed_,*seed_ ^ 0x8bb84b93962eacc9ull);
|
|
}
|
|
|
|
u64
|
|
RND::rand64(void)
|
|
{
|
|
return _rapidhash_rand(&G_SEED);
|
|
}
|
|
|
|
u64
|
|
RND::rand64(cu64 max_)
|
|
{
|
|
assert(max_ > 0);
|
|
return (RND::rand64() % max_);
|
|
}
|
|
|
|
u64
|
|
RND::rand64(cu64 min_,
|
|
cu64 max_)
|
|
{
|
|
assert(max_ > min_);
|
|
return (min_ + (RND::rand64() % (max_ - min_)));
|
|
}
|