1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
#include "mupdf/fitz.h"
/* The pseudo-random number generator in this file is based on the MIT licensed
* implementation in musl libc. */
#include <string.h>
/* The seed is initialized in context.c as follows:
* static uint16_t __seed48[7] = { 0, 0, 0, 0xe66d, 0xdeec, 0x5, 0xb };
*/
static uint64_t fz_rand48_step(uint16_t *xi, uint16_t *lc)
{
uint64_t a, x;
x = xi[0] | (xi[1]+0U)<<16 | (xi[2]+0ULL)<<32;
a = lc[0] | (lc[1]+0U)<<16 | (lc[2]+0ULL)<<32;
x = a*x + lc[3];
xi[0] = x;
xi[1] = x>>16;
xi[2] = x>>32;
return x & 0xffffffffffffull;
}
double fz_erand48(fz_context *ctx, uint16_t s[3])
{
union {
uint64_t u;
double f;
} x = { 0x3ff0000000000000ULL | fz_rand48_step(s, ctx->seed48+3)<<4 };
return x.f - 1.0;
}
double fz_drand48(fz_context *ctx)
{
return fz_erand48(ctx, ctx->seed48);
}
int32_t fz_nrand48(fz_context *ctx, uint16_t s[3])
{
return fz_rand48_step(s, ctx->seed48+3) >> 17;
}
int32_t fz_lrand48(fz_context *ctx)
{
return fz_nrand48(ctx, ctx->seed48);
}
int32_t fz_jrand48(fz_context *ctx, uint16_t s[3])
{
return (int32_t)(fz_rand48_step(s, ctx->seed48+3) >> 16);
}
int32_t fz_mrand48(fz_context *ctx)
{
return fz_jrand48(ctx, ctx->seed48);
}
void fz_lcong48(fz_context *ctx, uint16_t p[7])
{
memcpy(ctx->seed48, p, sizeof ctx->seed48);
}
uint16_t *fz_seed48(fz_context *ctx, uint16_t *s)
{
static uint16_t p[3];
memcpy(p, ctx->seed48, sizeof p);
memcpy(ctx->seed48, s, sizeof p);
return p;
}
void fz_srand48(fz_context *ctx, int32_t seed)
{
fz_seed48(ctx, (uint16_t [3]){ 0x330e, seed, seed>>16 });
}
void fz_memrnd(fz_context *ctx, unsigned char *data, int len)
{
while (len-- > 0)
*data++ = (unsigned char)fz_lrand48(ctx);
}
|