bc9afd8fedd40750d987cfa57e3c6009a0e42ff7
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
35 #include <mLib/bits.h>
42 /*----- Main code ---------------------------------------------------------*/
44 /* --- @fibrand_step@ --- *
46 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
48 * Returns: Next output from generator.
50 * Use: Steps the generator. Returns
51 * %$x_{i - 24} + x_{i - 55} \bmod 2^{32}$%.
54 uint32
fibrand_step(fibrand
*f
)
57 unsigned j
= i
+ (FIB_SZ
- FIB_TAP
);
61 x
= f
->x
[i
] = U32(f
->x
[i
] + f
->x
[j
]);
69 /* --- @fibrand_seed@ --- *
71 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
72 * @grand *r@ = random number generator to extract words from
76 * Use: Initializes a Fibonacci generator using word outputs from the
77 * given random number source @r@.
80 void fibrand_seed(fibrand
*f
, grand
*r
)
85 for (i
= 0; i
< FIB_SZ
; i
++)
86 p
|= f
->x
[i
] = r
->ops
->word(r
);
88 i
= r
->ops
->range(r
, FIB_SZ
);
94 /* --- @fibrand_lcseed@ --- *
96 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
97 * @uint32 seed@ = seed value
101 * Use: Initializes a Fibonacci generator using outputs from the
102 * @lcrand@ generator seeded from @seed@. This is faster than
103 * using a generic @lcrand@-based generator and @fibrand_rseed@
104 * because it uses raw outputs rather than uniformly distributed
108 void fibrand_lcseed(fibrand
*f
, uint32 seed
)
113 for (i
= 0; i
< FIB_SZ
; i
++)
114 p
|= f
->x
[i
] = seed
= lcrand(seed
);
116 i
= lcrand_range(&seed
, FIB_SZ
);
122 /* --- @fibrand_range@ --- *
124 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
127 * Returns: A uniformly distributed pseudorandom integer in the interval
131 uint32
fibrand_range(fibrand
*f
, uint32 m
)
133 uint32 r
= 0xffffffff - (0xffffffff % m
);
136 /* --- Now generate numbers until a good one comes along --- */
138 do x
= fibrand_step(f
); while (x
>= r
);
142 /*----- Generic interface -------------------------------------------------*/
144 typedef struct gctx
{
149 static void gdestroy(grand
*r
)
155 static int gmisc(grand
*r
, unsigned op
, ...)
164 switch (va_arg(ap
, unsigned)) {
167 case GRAND_SEEDUINT32
:
177 fibrand_lcseed(&g
->f
, va_arg(ap
, unsigned));
179 case GRAND_SEEDUINT32
:
180 fibrand_lcseed(&g
->f
, va_arg(ap
, uint32
));
183 fibrand_seed(&g
->f
, va_arg(ap
, grand
*));
194 static octet
gbyte(grand
*r
)
197 return (U8(fibrand_step(&g
->f
)));
200 static uint32
gword(grand
*r
)
203 return (fibrand_step(&g
->f
));
206 static uint32
grange(grand
*r
, uint32 l
)
209 return (fibrand_range(&g
->f
, l
));
212 static void gfill(grand
*r
, void *p
, size_t sz
)
217 *q
++ = U8(fibrand_step(&g
->f
));
222 static const grand_ops gops
= {
226 gword
, gbyte
, gword
, grange
, gfill
229 /* --- @fibrand_create@ --- *
231 * Arguments: @uint32 seed@ = initial seed
233 * Returns: Pointer to a generic generator.
235 * Use: Constructs a generic generator interface over a Fibonacci
236 * generator. The generator is seeded using @fibrand_lcseed@.
239 grand
*fibrand_create(uint32 seed
)
241 gctx
*g
= CREATE(gctx
);
243 fibrand_lcseed(&g
->f
, seed
);
247 /*----- That's all, folks -------------------------------------------------*/