Blender V4.3
RandGen.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2012-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10#include "RandGen.h"
11
12#include "BLI_sys_types.h"
13
14namespace Freestyle {
15
16//
17// Macro definitions
18//
20
21#define N 16
22#define MASK (uint(1 << (N - 1)) + (1 << (N - 1)) - 1)
23#define X0 0x330E
24#define X1 0xABCD
25#define X2 0x1234
26#define A0 0xE66D
27#define A1 0xDEEC
28#define A2 0x5
29#define C 0xB
30#if 0 // XXX Unused
31# define HI_BIT (1L << (2 * N - 1))
32#endif
33
34#define LOW(x) (uint(x) & MASK)
35#define HIGH(x) LOW((x) >> N)
36
37#define MUL(x, y, z) \
38 { \
39 long l = long(x) * long(y); \
40 (z)[0] = LOW(l); \
41 (z)[1] = HIGH(l); \
42 } \
43 ((void)0)
44
45#define CARRY(x, y) (ulong(long(x) + long(y)) > MASK)
46#define ADDEQU(x, y, z) (z = CARRY(x, (y)), x = LOW(x + (y)))
47#define SET3(x, x0, x1, x2) ((x)[0] = (x0), (x)[1] = (x1), (x)[2] = (x2))
48#if 0 // XXX, unused
49# define SETLOW(x, y, n) SET3(x, LOW((y)[n]), LOW((y)[(n) + 1]), LOW((y)[(n) + 2]))
50#endif
51#define SEED(x0, x1, x2) (SET3(x, x0, x1, x2), SET3(a, A0, A1, A2), c = C)
52
53#if 0 // XXX, unused
54# define REST(v) \
55 for (i = 0; i < 3; i++) { \
56 xsubi[i] = x[i]; \
57 x[i] = temp[i]; \
58 } \
59 return (v); \
60 (void)0
61
62# define NEST(TYPE, f, F) \
63 TYPE f(ushort *xsubi) \
64 { \
65 int i; \
66 TYPE v; \
67 uint temp[3]; \
68 for (i = 0; i < 3; i++) { \
69 temp[i] = x[i]; \
70 x[i] = LOW(xsubi[i]); \
71 } \
72 v = F(); \
73 REST(v); \
74 }
75#endif
76
77static uint x[3] = {
78 X0,
79 X1,
80 X2,
81};
82static uint a[3] = {
83 A0,
84 A1,
85 A2,
86};
87static uint c = C;
88
89//
90// Methods implementation
91//
93
95{
96 static real two16m = 1.0 / (1L << N);
97 next();
98 return (two16m * (two16m * (two16m * x[0] + x[1]) + x[2]));
99}
100
101void RandGen::srand48(long seedval)
102{
103 SEED(X0, LOW(seedval), HIGH(seedval));
104}
105
106void RandGen::next()
107{
108 uint p[2], q[2], r[2], carry0, carry1;
109
110 MUL(a[0], x[0], p);
111 ADDEQU(p[0], c, carry0);
112 ADDEQU(p[1], carry0, carry1);
113 MUL(a[0], x[1], q);
114 ADDEQU(p[1], q[0], carry0);
115 MUL(a[1], x[0], r);
116 x[2] = LOW(carry0 + carry1 + CARRY(p[1], r[0]) + q[1] + r[1] + a[0] * x[2] + a[1] * x[1] +
117 a[2] * x[0]);
118 x[1] = LOW(p[1] + r[0]);
119 x[0] = LOW(p[0]);
120}
121
122} /* namespace Freestyle */
unsigned int uint
#define A0
Definition RandGen.cpp:26
#define X1
Definition RandGen.cpp:24
#define A2
Definition RandGen.cpp:28
#define LOW(x)
Definition RandGen.cpp:34
#define X2
Definition RandGen.cpp:25
#define X0
Definition RandGen.cpp:23
#define ADDEQU(x, y, z)
Definition RandGen.cpp:46
#define MUL(x, y, z)
Definition RandGen.cpp:37
#define HIGH(x)
Definition RandGen.cpp:35
#define C
Definition RandGen.cpp:29
#define A1
Definition RandGen.cpp:27
#define CARRY(x, y)
Definition RandGen.cpp:45
#define SEED(x0, x1, x2)
Definition RandGen.cpp:51
Pseudo-random number generator.
static void srand48(long seedval)
Definition RandGen.cpp:101
static real drand48()
Definition RandGen.cpp:94
#define N
inherits from class Rep
Definition AppCanvas.cpp:20
static uint c
Definition RandGen.cpp:87
double real
Definition Precision.h:14