Update.
[glibc.git] / stdlib / drand48-iter.c
blob707be8e5667d6f206f529a3f17b0eea9de0f82de
1 /* Copyright (C) 1995, 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <limits.h>
23 #include <sys/types.h>
25 /* Global state for non-reentrant functions. */
26 struct drand48_data __libc_drand48_data;
29 int
30 __drand48_iterate (xsubi, buffer)
31 unsigned short int xsubi[3];
32 struct drand48_data *buffer;
34 u_int64_t X, a, result;
36 /* Initialize buffer, if not yet done. */
37 if (!buffer->init)
39 #if (USHRT_MAX == 0xffffU)
40 buffer->a[2] = 0x5;
41 buffer->a[1] = 0xdeec;
42 buffer->a[0] = 0xe66d;
43 #else
44 buffer->a[2] = 0x5deecUL;
45 buffer->a[1] = 0xe66d0000UL;
46 buffer->a[0] = 0;
47 #endif
48 buffer->c = 0xb;
49 buffer->init = 1;
52 /* Do the real work. We choose a data type which contains at least
53 48 bits. Because we compute the modulus it does not care how
54 many bits really are computed. */
56 if (sizeof (unsigned short int) == 2)
58 X = (u_int64_t)xsubi[2] << 32 | (u_int64_t)xsubi[1] << 16 | xsubi[0];
59 a = ((u_int64_t)buffer->a[2] << 32 | (u_int64_t)buffer->a[1] << 16
60 | buffer->a[0]);
62 result = X * a + buffer->c;
64 xsubi[0] = result & 0xffff;
65 xsubi[1] = (result >> 16) & 0xffff;
66 xsubi[2] = (result >> 32) & 0xffff;
68 else
70 X = (u_int64_t)xsubi[2] << 16 | xsubi[1] >> 16;
71 a = (u_int64_t)buffer->a[2] << 16 | buffer->a[1] >> 16;
73 result = X * a + buffer->c;
75 xsubi[0] = result >> 16 & 0xffffffffl;
76 xsubi[1] = result << 16 & 0xffff0000l;
79 return 0;