3 * $Id: fibrand.c,v 1.5 2004/04/08 01:36:15 mdw Exp $
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/bits.h>
44 /*----- Main code ---------------------------------------------------------*/
46 /* --- @fibrand_step@ --- *
48 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
50 * Returns: Next output from generator.
52 * Use: Steps the generator. Returns
53 * %$x_{i - 24} + x_{i - 55} \bmod 2^{32}$%.
56 uint32
fibrand_step(fibrand
*f
)
59 unsigned j
= i
+ (FIB_SZ
- FIB_TAP
);
63 x
= f
->x
[i
] = U32(f
->x
[i
] + f
->x
[j
]);
71 /* --- @fibrand_seed@ --- *
73 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
74 * @grand *r@ = random number generator to extract words from
78 * Use: Initializes a Fibonacci generator using word outputs from the
79 * given random number source @r@.
82 void fibrand_seed(fibrand
*f
, grand
*r
)
87 for (i
= 0; i
< FIB_SZ
; i
++)
88 p
|= f
->x
[i
] = r
->ops
->word(r
);
90 i
= r
->ops
->range(r
, FIB_SZ
);
96 /* --- @fibrand_lcseed@ --- *
98 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
99 * @uint32 seed@ = seed value
103 * Use: Initializes a Fibonacci generator using outputs from the
104 * @lcrand@ generator seeded from @seed@. This is faster than
105 * using a generic @lcrand@-based generator and @fibrand_rseed@
106 * because it uses raw outputs rather than uniformly distributed
110 void fibrand_lcseed(fibrand
*f
, uint32 seed
)
115 for (i
= 0; i
< FIB_SZ
; i
++)
116 p
|= f
->x
[i
] = seed
= lcrand(seed
);
118 i
= lcrand_range(&seed
, FIB_SZ
);
124 /* --- @fibrand_range@ --- *
126 * Arguments: @fibrand *f@ = pointer to Fibonacci generator context
129 * Returns: A uniformly distributed pseudorandom integer in the interval
133 uint32
fibrand_range(fibrand
*f
, uint32 m
)
135 uint32 r
= 0xffffffff - (0xffffffff % m
);
138 /* --- Now generate numbers until a good one comes along --- */
140 do x
= fibrand_step(f
); while (x
>= r
);
144 /*----- Generic interface -------------------------------------------------*/
146 typedef struct gctx
{
151 static void gdestroy(grand
*r
)
157 static int gmisc(grand
*r
, unsigned op
, ...)
166 switch (va_arg(ap
, unsigned)) {
169 case GRAND_SEEDUINT32
:
179 fibrand_lcseed(&g
->f
, va_arg(ap
, unsigned));
181 case GRAND_SEEDUINT32
:
182 fibrand_lcseed(&g
->f
, va_arg(ap
, uint32
));
185 fibrand_seed(&g
->f
, va_arg(ap
, grand
*));
196 static octet
gbyte(grand
*r
)
199 return (U8(fibrand_step(&g
->f
)));
202 static uint32
gword(grand
*r
)
205 return (fibrand_step(&g
->f
));
208 static uint32
grange(grand
*r
, uint32 l
)
211 return (fibrand_range(&g
->f
, l
));
214 static void gfill(grand
*r
, void *p
, size_t sz
)
219 *q
++ = U8(fibrand_step(&g
->f
));
224 static const grand_ops gops
= {
228 gword
, gbyte
, gword
, grange
, gfill
231 /* --- @fibrand_create@ --- *
233 * Arguments: @uint32 seed@ = initial seed
235 * Returns: Pointer to a generic generator.
237 * Use: Constructs a generic generator interface over a Fibonacci
238 * generator. The generator is seeded using @fibrand_lcseed@.
241 grand
*fibrand_create(uint32 seed
)
243 gctx
*g
= CREATE(gctx
);
245 fibrand_lcseed(&g
->f
, seed
);
249 /*----- That's all, folks -------------------------------------------------*/