xref: /haiku/src/system/libroot/posix/glibc/stdlib/random_r.c (revision 5af32e752606778be5dd7379f319fe43cb3f6b8c)
1*5af32e75SAxel Dörfler /*
2*5af32e75SAxel Dörfler    Copyright (C) 1995 Free Software Foundation
3*5af32e75SAxel Dörfler 
4*5af32e75SAxel Dörfler    The GNU C Library is free software; you can redistribute it and/or
5*5af32e75SAxel Dörfler    modify it under the terms of the GNU Lesser General Public
6*5af32e75SAxel Dörfler    License as published by the Free Software Foundation; either
7*5af32e75SAxel Dörfler    version 2.1 of the License, or (at your option) any later version.
8*5af32e75SAxel Dörfler 
9*5af32e75SAxel Dörfler    The GNU C Library is distributed in the hope that it will be useful,
10*5af32e75SAxel Dörfler    but WITHOUT ANY WARRANTY; without even the implied warranty of
11*5af32e75SAxel Dörfler    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12*5af32e75SAxel Dörfler    Lesser General Public License for more details.
13*5af32e75SAxel Dörfler 
14*5af32e75SAxel Dörfler    You should have received a copy of the GNU Lesser General Public
15*5af32e75SAxel Dörfler    License along with the GNU C Library; if not, write to the Free
16*5af32e75SAxel Dörfler    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17*5af32e75SAxel Dörfler    02111-1307 USA.  */
18*5af32e75SAxel Dörfler 
19*5af32e75SAxel Dörfler /*
20*5af32e75SAxel Dörfler    Copyright (C) 1983 Regents of the University of California.
21*5af32e75SAxel Dörfler    All rights reserved.
22*5af32e75SAxel Dörfler 
23*5af32e75SAxel Dörfler    Redistribution and use in source and binary forms, with or without
24*5af32e75SAxel Dörfler    modification, are permitted provided that the following conditions
25*5af32e75SAxel Dörfler    are met:
26*5af32e75SAxel Dörfler 
27*5af32e75SAxel Dörfler    1. Redistributions of source code must retain the above copyright
28*5af32e75SAxel Dörfler       notice, this list of conditions and the following disclaimer.
29*5af32e75SAxel Dörfler    2. Redistributions in binary form must reproduce the above copyright
30*5af32e75SAxel Dörfler       notice, this list of conditions and the following disclaimer in the
31*5af32e75SAxel Dörfler       documentation and/or other materials provided with the distribution.
32*5af32e75SAxel Dörfler    4. Neither the name of the University nor the names of its contributors
33*5af32e75SAxel Dörfler       may be used to endorse or promote products derived from this software
34*5af32e75SAxel Dörfler       without specific prior written permission.
35*5af32e75SAxel Dörfler 
36*5af32e75SAxel Dörfler    THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
37*5af32e75SAxel Dörfler    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38*5af32e75SAxel Dörfler    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39*5af32e75SAxel Dörfler    ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
40*5af32e75SAxel Dörfler    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41*5af32e75SAxel Dörfler    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
42*5af32e75SAxel Dörfler    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43*5af32e75SAxel Dörfler    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44*5af32e75SAxel Dörfler    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45*5af32e75SAxel Dörfler    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46*5af32e75SAxel Dörfler    SUCH DAMAGE.*/
47*5af32e75SAxel Dörfler 
48*5af32e75SAxel Dörfler /*
49*5af32e75SAxel Dörfler  * This is derived from the Berkeley source:
50*5af32e75SAxel Dörfler  *	@(#)random.c	5.5 (Berkeley) 7/6/88
51*5af32e75SAxel Dörfler  * It was reworked for the GNU C Library by Roland McGrath.
52*5af32e75SAxel Dörfler  * Rewritten to be reentrant by Ulrich Drepper, 1995
53*5af32e75SAxel Dörfler  */
54*5af32e75SAxel Dörfler 
55*5af32e75SAxel Dörfler #include <errno.h>
56*5af32e75SAxel Dörfler #include <limits.h>
57*5af32e75SAxel Dörfler #include <stddef.h>
58*5af32e75SAxel Dörfler #include <stdlib.h>
59*5af32e75SAxel Dörfler 
60*5af32e75SAxel Dörfler 
61*5af32e75SAxel Dörfler /* An improved random number generation package.  In addition to the standard
62*5af32e75SAxel Dörfler    rand()/srand() like interface, this package also has a special state info
63*5af32e75SAxel Dörfler    interface.  The initstate() routine is called with a seed, an array of
64*5af32e75SAxel Dörfler    bytes, and a count of how many bytes are being passed in; this array is
65*5af32e75SAxel Dörfler    then initialized to contain information for random number generation with
66*5af32e75SAxel Dörfler    that much state information.  Good sizes for the amount of state
67*5af32e75SAxel Dörfler    information are 32, 64, 128, and 256 bytes.  The state can be switched by
68*5af32e75SAxel Dörfler    calling the setstate() function with the same array as was initialized
69*5af32e75SAxel Dörfler    with initstate().  By default, the package runs with 128 bytes of state
70*5af32e75SAxel Dörfler    information and generates far better random numbers than a linear
71*5af32e75SAxel Dörfler    congruential generator.  If the amount of state information is less than
72*5af32e75SAxel Dörfler    32 bytes, a simple linear congruential R.N.G. is used.  Internally, the
73*5af32e75SAxel Dörfler    state information is treated as an array of longs; the zeroth element of
74*5af32e75SAxel Dörfler    the array is the type of R.N.G. being used (small integer); the remainder
75*5af32e75SAxel Dörfler    of the array is the state information for the R.N.G.  Thus, 32 bytes of
76*5af32e75SAxel Dörfler    state information will give 7 longs worth of state information, which will
77*5af32e75SAxel Dörfler    allow a degree seven polynomial.  (Note: The zeroth word of state
78*5af32e75SAxel Dörfler    information also has some other information stored in it; see setstate
79*5af32e75SAxel Dörfler    for details).  The random number generation technique is a linear feedback
80*5af32e75SAxel Dörfler    shift register approach, employing trinomials (since there are fewer terms
81*5af32e75SAxel Dörfler    to sum up that way).  In this approach, the least significant bit of all
82*5af32e75SAxel Dörfler    the numbers in the state table will act as a linear feedback shift register,
83*5af32e75SAxel Dörfler    and will have period 2^deg - 1 (where deg is the degree of the polynomial
84*5af32e75SAxel Dörfler    being used, assuming that the polynomial is irreducible and primitive).
85*5af32e75SAxel Dörfler    The higher order bits will have longer periods, since their values are
86*5af32e75SAxel Dörfler    also influenced by pseudo-random carries out of the lower bits.  The
87*5af32e75SAxel Dörfler    total period of the generator is approximately deg*(2**deg - 1); thus
88*5af32e75SAxel Dörfler    doubling the amount of state information has a vast influence on the
89*5af32e75SAxel Dörfler    period of the generator.  Note: The deg*(2**deg - 1) is an approximation
90*5af32e75SAxel Dörfler    only good for large deg, when the period of the shift register is the
91*5af32e75SAxel Dörfler    dominant factor.  With deg equal to seven, the period is actually much
92*5af32e75SAxel Dörfler    longer than the 7*(2**7 - 1) predicted by this formula.  */
93*5af32e75SAxel Dörfler 
94*5af32e75SAxel Dörfler 
95*5af32e75SAxel Dörfler 
96*5af32e75SAxel Dörfler /* For each of the currently supported random number generators, we have a
97*5af32e75SAxel Dörfler    break value on the amount of state information (you need at least this many
98*5af32e75SAxel Dörfler    bytes of state info to support this random number generator), a degree for
99*5af32e75SAxel Dörfler    the polynomial (actually a trinomial) that the R.N.G. is based on, and
100*5af32e75SAxel Dörfler    separation between the two lower order coefficients of the trinomial.  */
101*5af32e75SAxel Dörfler 
102*5af32e75SAxel Dörfler /* Linear congruential.  */
103*5af32e75SAxel Dörfler #define	TYPE_0		0
104*5af32e75SAxel Dörfler #define	BREAK_0		8
105*5af32e75SAxel Dörfler #define	DEG_0		0
106*5af32e75SAxel Dörfler #define	SEP_0		0
107*5af32e75SAxel Dörfler 
108*5af32e75SAxel Dörfler /* x**7 + x**3 + 1.  */
109*5af32e75SAxel Dörfler #define	TYPE_1		1
110*5af32e75SAxel Dörfler #define	BREAK_1		32
111*5af32e75SAxel Dörfler #define	DEG_1		7
112*5af32e75SAxel Dörfler #define	SEP_1		3
113*5af32e75SAxel Dörfler 
114*5af32e75SAxel Dörfler /* x**15 + x + 1.  */
115*5af32e75SAxel Dörfler #define	TYPE_2		2
116*5af32e75SAxel Dörfler #define	BREAK_2		64
117*5af32e75SAxel Dörfler #define	DEG_2		15
118*5af32e75SAxel Dörfler #define	SEP_2		1
119*5af32e75SAxel Dörfler 
120*5af32e75SAxel Dörfler /* x**31 + x**3 + 1.  */
121*5af32e75SAxel Dörfler #define	TYPE_3		3
122*5af32e75SAxel Dörfler #define	BREAK_3		128
123*5af32e75SAxel Dörfler #define	DEG_3		31
124*5af32e75SAxel Dörfler #define	SEP_3		3
125*5af32e75SAxel Dörfler 
126*5af32e75SAxel Dörfler /* x**63 + x + 1.  */
127*5af32e75SAxel Dörfler #define	TYPE_4		4
128*5af32e75SAxel Dörfler #define	BREAK_4		256
129*5af32e75SAxel Dörfler #define	DEG_4		63
130*5af32e75SAxel Dörfler #define	SEP_4		1
131*5af32e75SAxel Dörfler 
132*5af32e75SAxel Dörfler 
133*5af32e75SAxel Dörfler /* Array versions of the above information to make code run faster.
134*5af32e75SAxel Dörfler    Relies on fact that TYPE_i == i.  */
135*5af32e75SAxel Dörfler 
136*5af32e75SAxel Dörfler #define	MAX_TYPES	5	/* Max number of types above.  */
137*5af32e75SAxel Dörfler 
138*5af32e75SAxel Dörfler struct random_poly_info
139*5af32e75SAxel Dörfler {
140*5af32e75SAxel Dörfler   int seps[MAX_TYPES];
141*5af32e75SAxel Dörfler   int degrees[MAX_TYPES];
142*5af32e75SAxel Dörfler };
143*5af32e75SAxel Dörfler 
144*5af32e75SAxel Dörfler static const struct random_poly_info random_poly_info =
145*5af32e75SAxel Dörfler {
146*5af32e75SAxel Dörfler   { SEP_0, SEP_1, SEP_2, SEP_3, SEP_4 },
147*5af32e75SAxel Dörfler   { DEG_0, DEG_1, DEG_2, DEG_3, DEG_4 }
148*5af32e75SAxel Dörfler };
149*5af32e75SAxel Dörfler 
150*5af32e75SAxel Dörfler 
151*5af32e75SAxel Dörfler 
152*5af32e75SAxel Dörfler 
153*5af32e75SAxel Dörfler /* Initialize the random number generator based on the given seed.  If the
154*5af32e75SAxel Dörfler    type is the trivial no-state-information type, just remember the seed.
155*5af32e75SAxel Dörfler    Otherwise, initializes state[] based on the given "seed" via a linear
156*5af32e75SAxel Dörfler    congruential generator.  Then, the pointers are set to known locations
157*5af32e75SAxel Dörfler    that are exactly rand_sep places apart.  Lastly, it cycles the state
158*5af32e75SAxel Dörfler    information a given number of times to get rid of any initial dependencies
159*5af32e75SAxel Dörfler    introduced by the L.C.R.N.G.  Note that the initialization of randtbl[]
160*5af32e75SAxel Dörfler    for default usage relies on values produced by this routine.  */
161*5af32e75SAxel Dörfler int
__srandom_r(seed,buf)162*5af32e75SAxel Dörfler __srandom_r (seed, buf)
163*5af32e75SAxel Dörfler      unsigned int seed;
164*5af32e75SAxel Dörfler      struct random_data *buf;
165*5af32e75SAxel Dörfler {
166*5af32e75SAxel Dörfler   int type;
167*5af32e75SAxel Dörfler   int32_t *state;
168*5af32e75SAxel Dörfler   long int i;
169*5af32e75SAxel Dörfler   long int word;
170*5af32e75SAxel Dörfler   int32_t *dst;
171*5af32e75SAxel Dörfler   int kc;
172*5af32e75SAxel Dörfler 
173*5af32e75SAxel Dörfler   if (buf == NULL)
174*5af32e75SAxel Dörfler     goto fail;
175*5af32e75SAxel Dörfler   type = buf->rand_type;
176*5af32e75SAxel Dörfler   if ((unsigned int) type >= MAX_TYPES)
177*5af32e75SAxel Dörfler     goto fail;
178*5af32e75SAxel Dörfler 
179*5af32e75SAxel Dörfler   state = buf->state;
180*5af32e75SAxel Dörfler   /* We must make sure the seed is not 0.  Take arbitrarily 1 in this case.  */
181*5af32e75SAxel Dörfler   if (seed == 0)
182*5af32e75SAxel Dörfler     seed = 1;
183*5af32e75SAxel Dörfler   state[0] = seed;
184*5af32e75SAxel Dörfler   if (type == TYPE_0)
185*5af32e75SAxel Dörfler     goto done;
186*5af32e75SAxel Dörfler 
187*5af32e75SAxel Dörfler   dst = state;
188*5af32e75SAxel Dörfler   word = seed;
189*5af32e75SAxel Dörfler   kc = buf->rand_deg;
190*5af32e75SAxel Dörfler   for (i = 1; i < kc; ++i)
191*5af32e75SAxel Dörfler     {
192*5af32e75SAxel Dörfler       /* This does:
193*5af32e75SAxel Dörfler 	   state[i] = (16807 * state[i - 1]) % 2147483647;
194*5af32e75SAxel Dörfler 	 but avoids overflowing 31 bits.  */
195*5af32e75SAxel Dörfler       long int hi = word / 127773;
196*5af32e75SAxel Dörfler       long int lo = word % 127773;
197*5af32e75SAxel Dörfler       word = 16807 * lo - 2836 * hi;
198*5af32e75SAxel Dörfler       if (word < 0)
199*5af32e75SAxel Dörfler 	word += 2147483647;
200*5af32e75SAxel Dörfler       *++dst = word;
201*5af32e75SAxel Dörfler     }
202*5af32e75SAxel Dörfler 
203*5af32e75SAxel Dörfler   buf->fptr = &state[buf->rand_sep];
204*5af32e75SAxel Dörfler   buf->rptr = &state[0];
205*5af32e75SAxel Dörfler   kc *= 10;
206*5af32e75SAxel Dörfler   while (--kc >= 0)
207*5af32e75SAxel Dörfler     {
208*5af32e75SAxel Dörfler       int32_t discard;
209*5af32e75SAxel Dörfler       (void) __random_r (buf, &discard);
210*5af32e75SAxel Dörfler     }
211*5af32e75SAxel Dörfler 
212*5af32e75SAxel Dörfler  done:
213*5af32e75SAxel Dörfler   return 0;
214*5af32e75SAxel Dörfler 
215*5af32e75SAxel Dörfler  fail:
216*5af32e75SAxel Dörfler   return -1;
217*5af32e75SAxel Dörfler }
218*5af32e75SAxel Dörfler 
219*5af32e75SAxel Dörfler weak_alias (__srandom_r, srandom_r)
220*5af32e75SAxel Dörfler 
221*5af32e75SAxel Dörfler /* Initialize the state information in the given array of N bytes for
222*5af32e75SAxel Dörfler    future random number generation.  Based on the number of bytes we
223*5af32e75SAxel Dörfler    are given, and the break values for the different R.N.G.'s, we choose
224*5af32e75SAxel Dörfler    the best (largest) one we can and set things up for it.  srandom is
225*5af32e75SAxel Dörfler    then called to initialize the state information.  Note that on return
226*5af32e75SAxel Dörfler    from srandom, we set state[-1] to be the type multiplexed with the current
227*5af32e75SAxel Dörfler    value of the rear pointer; this is so successive calls to initstate won't
228*5af32e75SAxel Dörfler    lose this information and will be able to restart with setstate.
229*5af32e75SAxel Dörfler    Note: The first thing we do is save the current state, if any, just like
230*5af32e75SAxel Dörfler    setstate so that it doesn't matter when initstate is called.
231*5af32e75SAxel Dörfler    Returns a pointer to the old state.  */
232*5af32e75SAxel Dörfler int
233*5af32e75SAxel Dörfler __initstate_r (seed, arg_state, n, buf)
234*5af32e75SAxel Dörfler      unsigned int seed;
235*5af32e75SAxel Dörfler      char *arg_state;
236*5af32e75SAxel Dörfler      size_t n;
237*5af32e75SAxel Dörfler      struct random_data *buf;
238*5af32e75SAxel Dörfler {
239*5af32e75SAxel Dörfler   int type;
240*5af32e75SAxel Dörfler   int degree;
241*5af32e75SAxel Dörfler   int separation;
242*5af32e75SAxel Dörfler   int32_t *state;
243*5af32e75SAxel Dörfler 
244*5af32e75SAxel Dörfler   if (buf == NULL)
245*5af32e75SAxel Dörfler     goto fail;
246*5af32e75SAxel Dörfler 
247*5af32e75SAxel Dörfler   if (n >= BREAK_3)
248*5af32e75SAxel Dörfler     type = n < BREAK_4 ? TYPE_3 : TYPE_4;
249*5af32e75SAxel Dörfler   else if (n < BREAK_1)
250*5af32e75SAxel Dörfler     {
251*5af32e75SAxel Dörfler       if (n < BREAK_0)
252*5af32e75SAxel Dörfler 	{
253*5af32e75SAxel Dörfler 	  __set_errno (EINVAL);
254*5af32e75SAxel Dörfler 	  goto fail;
255*5af32e75SAxel Dörfler 	}
256*5af32e75SAxel Dörfler       type = TYPE_0;
257*5af32e75SAxel Dörfler     }
258*5af32e75SAxel Dörfler   else
259*5af32e75SAxel Dörfler     type = n < BREAK_2 ? TYPE_1 : TYPE_2;
260*5af32e75SAxel Dörfler 
261*5af32e75SAxel Dörfler   degree = random_poly_info.degrees[type];
262*5af32e75SAxel Dörfler   separation = random_poly_info.seps[type];
263*5af32e75SAxel Dörfler 
264*5af32e75SAxel Dörfler   buf->rand_type = type;
265*5af32e75SAxel Dörfler   buf->rand_sep = separation;
266*5af32e75SAxel Dörfler   buf->rand_deg = degree;
267*5af32e75SAxel Dörfler   state = &((int32_t *) arg_state)[1];	/* First location.  */
268*5af32e75SAxel Dörfler   /* Must set END_PTR before srandom.  */
269*5af32e75SAxel Dörfler   buf->end_ptr = &state[degree];
270*5af32e75SAxel Dörfler 
271*5af32e75SAxel Dörfler   buf->state = state;
272*5af32e75SAxel Dörfler 
273*5af32e75SAxel Dörfler   __srandom_r (seed, buf);
274*5af32e75SAxel Dörfler 
275*5af32e75SAxel Dörfler   state[-1] = TYPE_0;
276*5af32e75SAxel Dörfler   if (type != TYPE_0)
277*5af32e75SAxel Dörfler     state[-1] = (buf->rptr - state) * MAX_TYPES + type;
278*5af32e75SAxel Dörfler 
279*5af32e75SAxel Dörfler   return 0;
280*5af32e75SAxel Dörfler 
281*5af32e75SAxel Dörfler  fail:
282*5af32e75SAxel Dörfler   __set_errno (EINVAL);
283*5af32e75SAxel Dörfler   return -1;
284*5af32e75SAxel Dörfler }
285*5af32e75SAxel Dörfler 
286*5af32e75SAxel Dörfler weak_alias (__initstate_r, initstate_r)
287*5af32e75SAxel Dörfler 
288*5af32e75SAxel Dörfler /* Restore the state from the given state array.
289*5af32e75SAxel Dörfler    Note: It is important that we also remember the locations of the pointers
290*5af32e75SAxel Dörfler    in the current state information, and restore the locations of the pointers
291*5af32e75SAxel Dörfler    from the old state information.  This is done by multiplexing the pointer
292*5af32e75SAxel Dörfler    location into the zeroth word of the state information. Note that due
293*5af32e75SAxel Dörfler    to the order in which things are done, it is OK to call setstate with the
294*5af32e75SAxel Dörfler    same state as the current state
295*5af32e75SAxel Dörfler    Returns a pointer to the old state information.  */
296*5af32e75SAxel Dörfler int
297*5af32e75SAxel Dörfler __setstate_r (arg_state, buf)
298*5af32e75SAxel Dörfler      char *arg_state;
299*5af32e75SAxel Dörfler      struct random_data *buf;
300*5af32e75SAxel Dörfler {
301*5af32e75SAxel Dörfler   int32_t *new_state = 1 + (int32_t *) arg_state;
302*5af32e75SAxel Dörfler   int type;
303*5af32e75SAxel Dörfler   int old_type;
304*5af32e75SAxel Dörfler   int32_t *old_state;
305*5af32e75SAxel Dörfler   int degree;
306*5af32e75SAxel Dörfler   int separation;
307*5af32e75SAxel Dörfler 
308*5af32e75SAxel Dörfler   if (arg_state == NULL || buf == NULL)
309*5af32e75SAxel Dörfler     goto fail;
310*5af32e75SAxel Dörfler 
311*5af32e75SAxel Dörfler   old_type = buf->rand_type;
312*5af32e75SAxel Dörfler   old_state = buf->state;
313*5af32e75SAxel Dörfler   if (old_type == TYPE_0)
314*5af32e75SAxel Dörfler     old_state[-1] = TYPE_0;
315*5af32e75SAxel Dörfler   else
316*5af32e75SAxel Dörfler     old_state[-1] = (MAX_TYPES * (buf->rptr - old_state)) + old_type;
317*5af32e75SAxel Dörfler 
318*5af32e75SAxel Dörfler   type = new_state[-1] % MAX_TYPES;
319*5af32e75SAxel Dörfler   if (type < TYPE_0 || type > TYPE_4)
320*5af32e75SAxel Dörfler     goto fail;
321*5af32e75SAxel Dörfler 
322*5af32e75SAxel Dörfler   buf->rand_deg = degree = random_poly_info.degrees[type];
323*5af32e75SAxel Dörfler   buf->rand_sep = separation = random_poly_info.seps[type];
324*5af32e75SAxel Dörfler   buf->rand_type = type;
325*5af32e75SAxel Dörfler 
326*5af32e75SAxel Dörfler   if (type != TYPE_0)
327*5af32e75SAxel Dörfler     {
328*5af32e75SAxel Dörfler       int rear = new_state[-1] / MAX_TYPES;
329*5af32e75SAxel Dörfler       buf->rptr = &new_state[rear];
330*5af32e75SAxel Dörfler       buf->fptr = &new_state[(rear + separation) % degree];
331*5af32e75SAxel Dörfler     }
332*5af32e75SAxel Dörfler   buf->state = new_state;
333*5af32e75SAxel Dörfler   /* Set end_ptr too.  */
334*5af32e75SAxel Dörfler   buf->end_ptr = &new_state[degree];
335*5af32e75SAxel Dörfler 
336*5af32e75SAxel Dörfler   return 0;
337*5af32e75SAxel Dörfler 
338*5af32e75SAxel Dörfler  fail:
339*5af32e75SAxel Dörfler   __set_errno (EINVAL);
340*5af32e75SAxel Dörfler   return -1;
341*5af32e75SAxel Dörfler }
342*5af32e75SAxel Dörfler 
343*5af32e75SAxel Dörfler weak_alias (__setstate_r, setstate_r)
344*5af32e75SAxel Dörfler 
345*5af32e75SAxel Dörfler /* If we are using the trivial TYPE_0 R.N.G., just do the old linear
346*5af32e75SAxel Dörfler    congruential bit.  Otherwise, we do our fancy trinomial stuff, which is the
347*5af32e75SAxel Dörfler    same in all the other cases due to all the global variables that have been
348*5af32e75SAxel Dörfler    set up.  The basic operation is to add the number at the rear pointer into
349*5af32e75SAxel Dörfler    the one at the front pointer.  Then both pointers are advanced to the next
350*5af32e75SAxel Dörfler    location cyclically in the table.  The value returned is the sum generated,
351*5af32e75SAxel Dörfler    reduced to 31 bits by throwing away the "least random" low bit.
352*5af32e75SAxel Dörfler    Note: The code takes advantage of the fact that both the front and
353*5af32e75SAxel Dörfler    rear pointers can't wrap on the same call by not testing the rear
354*5af32e75SAxel Dörfler    pointer if the front one has wrapped.  Returns a 31-bit random number.  */
355*5af32e75SAxel Dörfler 
356*5af32e75SAxel Dörfler int
357*5af32e75SAxel Dörfler __random_r (buf, result)
358*5af32e75SAxel Dörfler      struct random_data *buf;
359*5af32e75SAxel Dörfler      int32_t *result;
360*5af32e75SAxel Dörfler {
361*5af32e75SAxel Dörfler   int32_t *state;
362*5af32e75SAxel Dörfler 
363*5af32e75SAxel Dörfler   if (buf == NULL || result == NULL)
364*5af32e75SAxel Dörfler     goto fail;
365*5af32e75SAxel Dörfler 
366*5af32e75SAxel Dörfler   state = buf->state;
367*5af32e75SAxel Dörfler 
368*5af32e75SAxel Dörfler   if (buf->rand_type == TYPE_0)
369*5af32e75SAxel Dörfler     {
370*5af32e75SAxel Dörfler       int32_t val = state[0];
371*5af32e75SAxel Dörfler       val = ((state[0] * 1103515245) + 12345) & 0x7fffffff;
372*5af32e75SAxel Dörfler       state[0] = val;
373*5af32e75SAxel Dörfler       *result = val;
374*5af32e75SAxel Dörfler     }
375*5af32e75SAxel Dörfler   else
376*5af32e75SAxel Dörfler     {
377*5af32e75SAxel Dörfler       int32_t *fptr = buf->fptr;
378*5af32e75SAxel Dörfler       int32_t *rptr = buf->rptr;
379*5af32e75SAxel Dörfler       int32_t *end_ptr = buf->end_ptr;
380*5af32e75SAxel Dörfler       int32_t val;
381*5af32e75SAxel Dörfler 
382*5af32e75SAxel Dörfler       val = *fptr += *rptr;
383*5af32e75SAxel Dörfler       /* Chucking least random bit.  */
384*5af32e75SAxel Dörfler       *result = (val >> 1) & 0x7fffffff;
385*5af32e75SAxel Dörfler       ++fptr;
386*5af32e75SAxel Dörfler       if (fptr >= end_ptr)
387*5af32e75SAxel Dörfler 	{
388*5af32e75SAxel Dörfler 	  fptr = state;
389*5af32e75SAxel Dörfler 	  ++rptr;
390*5af32e75SAxel Dörfler 	}
391*5af32e75SAxel Dörfler       else
392*5af32e75SAxel Dörfler 	{
393*5af32e75SAxel Dörfler 	  ++rptr;
394*5af32e75SAxel Dörfler 	  if (rptr >= end_ptr)
395*5af32e75SAxel Dörfler 	    rptr = state;
396*5af32e75SAxel Dörfler 	}
397*5af32e75SAxel Dörfler       buf->fptr = fptr;
398*5af32e75SAxel Dörfler       buf->rptr = rptr;
399*5af32e75SAxel Dörfler     }
400*5af32e75SAxel Dörfler   return 0;
401*5af32e75SAxel Dörfler 
402*5af32e75SAxel Dörfler  fail:
403*5af32e75SAxel Dörfler   __set_errno (EINVAL);
404*5af32e75SAxel Dörfler   return -1;
405*5af32e75SAxel Dörfler }
406*5af32e75SAxel Dörfler 
407*5af32e75SAxel Dörfler weak_alias (__random_r, random_r)
408