Mon Sep 18 01:58:40 1995 Roland McGrath <roland@churchy.gnu.ai.mit.edu>
[platform/upstream/glibc.git] / stdlib / drand48-iter.c
1 /* Copyright (C) 1995 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.
4
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.
9
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.
14
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
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.  */
19
20 #include <errno.h>
21 #include <stdlib.h>
22
23
24 /* Global state for non-reentrent functions.  */
25 struct drand48_data __libc_drand48_data;
26
27
28 int
29 __drand48_iterate (xsubi, buffer)
30      unsigned short int xsubi[3];
31      struct drand48_data *buffer;
32 {
33   /* Be generous for the arguments, detect some errors.  */
34   if (xsubi == NULL || buffer == NULL)
35     {
36       errno = EFAULT;
37       return -1;
38     }
39
40   /* Initialize buffer, if not yet done.  */
41   if (!buffer->init)
42     {
43       if (sizeof (unsigned short int) == 2)
44         {
45           buffer->a[2] = 0x5;
46           buffer->a[1] = 0xdeec;
47           buffer->a[0] = 0xe66d;
48         }
49       else
50         {
51           buffer->a[2] = 0x5deecUL;
52           buffer->a[1] = 0xe66d0000UL;
53           buffer->a[0] = 0;
54         }
55       buffer->c = 0xb;
56       buffer->init = 1;
57     }
58
59   /* Do the real work.  We choose a data type which contains at least
60      48 bits.  Because we compute the modulus it does not care how
61      many bits really are computed.  */
62
63   if (sizeof (long int) >= 6)
64     {
65       /* The `long' data type is sufficent.  */
66       unsigned long int X, a, result;
67
68 #define ONE_STEP                                                            \
69       if (sizeof (unsigned short int) == 2)                                 \
70         {                                                                   \
71           X = (xsubi[2] << 16 | xsubi[1]) << 16 | xsubi[0];                 \
72           a = (buffer->a[2] << 16 | buffer->a[1]) << 16 | buffer->a[0];     \
73                                                                             \
74           result = X * a + buffer->c;                                       \
75                                                                             \
76           xsubi[0] = result & 0xffff;                                       \
77           result >>= 16;                                                    \
78           xsubi[1] = result & 0xffff;                                       \
79           result >>= 16;                                                    \
80           xsubi[2] = result & 0xffff;                                       \
81         }                                                                   \
82       else                                                                  \
83         {                                                                   \
84           X = xsubi[2] << 16 | xsubi[1] >> 16;                              \
85           a = buffer->a[2] << 16 | buffer->a[1] >> 16;                      \
86                                                                             \
87           result = X * a + buffer->c;                                       \
88                                                                             \
89           xsubi[0] = result >> 16 & 0xffffffffl;                            \
90           xsubi[1] = result << 16 & 0xffff0000l;                            \
91         }
92       ONE_STEP;
93     }
94   else
95     {
96       /* We have to use the `long long' data type.  */
97       unsigned long long int X, a, result;
98       ONE_STEP;
99     }
100
101   return 0;
102 }