mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-31 13:19:05 +00:00
LibC: Add drand48
, srand48
These functions are required for the `perl5` port's random function to operate correctly. As a bonus, this allows us to remove a nasty patch that replaces `drand48` with `random`.
This commit is contained in:
parent
12e534c8c6
commit
8bc1f1b63e
Notes:
sideshowbarker
2024-07-16 23:57:20 +09:00
Author: https://github.com/tarob0ba
Commit: 8bc1f1b63e
Pull-request: https://github.com/SerenityOS/serenity/pull/20771
Reviewed-by: https://github.com/LucasChollet
Reviewed-by: https://github.com/timschumi ✅
2 changed files with 20 additions and 0 deletions
|
@ -657,6 +657,7 @@ int ptsname_r(int fd, char* buffer, size_t size)
|
|||
}
|
||||
|
||||
static unsigned long s_next_rand = 1;
|
||||
static long s_next_rand48 = 0;
|
||||
|
||||
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/rand.html
|
||||
int rand()
|
||||
|
@ -671,6 +672,23 @@ void srand(unsigned seed)
|
|||
s_next_rand = seed;
|
||||
}
|
||||
|
||||
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/drand48.html
|
||||
double drand48()
|
||||
{
|
||||
constexpr u64 a = 0x5DEECE66DULL;
|
||||
constexpr u64 c = 0xBULL;
|
||||
constexpr u64 m = 1ULL << 48;
|
||||
|
||||
s_next_rand48 = (a * s_next_rand48 + c) & (m - 1);
|
||||
return static_cast<double>(s_next_rand48) / m;
|
||||
}
|
||||
|
||||
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/srand48.html
|
||||
void srand48(long seed)
|
||||
{
|
||||
s_next_rand48 = (seed & 0xFFFFFFFF) << 16 | 0x330E;
|
||||
}
|
||||
|
||||
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/abs.html
|
||||
int abs(int i)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue