| 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.
 | 
|---|
| 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 not,
 | 
|---|
| 17 |    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 | 
|---|
| 18 |    Boston, MA 02111-1307, USA.  */
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "rand48.h"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | /* Global state for non-reentrant functions.  */
 | 
|---|
| 23 | static struct drand48_data __libc_drand48_data;
 | 
|---|
| 24 | 
 | 
|---|
| 25 | 
 | 
|---|
| 26 | int
 | 
|---|
| 27 | __drand48_iterate (xsubi, buffer)
 | 
|---|
| 28 |      unsigned short int xsubi[3];
 | 
|---|
| 29 |      struct drand48_data *buffer;
 | 
|---|
| 30 | {
 | 
|---|
| 31 |   u_int64_t X, a, result;
 | 
|---|
| 32 | 
 | 
|---|
| 33 |   /* Initialize buffer, if not yet done.  */
 | 
|---|
| 34 |   if (!buffer->init)
 | 
|---|
| 35 |     {
 | 
|---|
| 36 | #if (USHRT_MAX == 0xffffU)
 | 
|---|
| 37 |       buffer->a[2] = 0x5;
 | 
|---|
| 38 |       buffer->a[1] = 0xdeec;
 | 
|---|
| 39 |       buffer->a[0] = 0xe66d;
 | 
|---|
| 40 | #else
 | 
|---|
| 41 |       buffer->a[2] = 0x5deecUL;
 | 
|---|
| 42 |       buffer->a[1] = 0xe66d0000UL;
 | 
|---|
| 43 |       buffer->a[0] = 0;
 | 
|---|
| 44 | #endif
 | 
|---|
| 45 |       buffer->c = 0xb;
 | 
|---|
| 46 |       buffer->init = 1;
 | 
|---|
| 47 |     }
 | 
|---|
| 48 | 
 | 
|---|
| 49 |   /* Do the real work.  We choose a data type which contains at least
 | 
|---|
| 50 |      48 bits.  Because we compute the modulus it does not care how
 | 
|---|
| 51 |      many bits really are computed.  */
 | 
|---|
| 52 | 
 | 
|---|
| 53 |   if (sizeof (unsigned short int) == 2)
 | 
|---|
| 54 |     {
 | 
|---|
| 55 |       X = (u_int64_t)xsubi[2] << 32 | (u_int64_t)xsubi[1] << 16 | xsubi[0];
 | 
|---|
| 56 |       a = ((u_int64_t)buffer->a[2] << 32 | (u_int64_t)buffer->a[1] << 16
 | 
|---|
| 57 |            | buffer->a[0]);
 | 
|---|
| 58 | 
 | 
|---|
| 59 |       result = X * a + buffer->c;
 | 
|---|
| 60 | 
 | 
|---|
| 61 |       xsubi[0] = result & 0xffff;
 | 
|---|
| 62 |       xsubi[1] = (result >> 16) & 0xffff;
 | 
|---|
| 63 |       xsubi[2] = (result >> 32) & 0xffff;
 | 
|---|
| 64 |     }
 | 
|---|
| 65 |   else
 | 
|---|
| 66 |     {
 | 
|---|
| 67 |       X = (u_int64_t)xsubi[2] << 16 | xsubi[1] >> 16;
 | 
|---|
| 68 |       a = (u_int64_t)buffer->a[2] << 16 | buffer->a[1] >> 16;
 | 
|---|
| 69 | 
 | 
|---|
| 70 |       result = X * a + buffer->c;
 | 
|---|
| 71 | 
 | 
|---|
| 72 |       xsubi[0] = result >> 16 & 0xffffffffl;
 | 
|---|
| 73 |       xsubi[1] = result << 16 & 0xffff0000l;
 | 
|---|
| 74 |     }
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   return 0;
 | 
|---|
| 77 | }
 | 
|---|
| 78 | /* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
 | 
|---|
| 79 |    This file is part of the GNU C Library.
 | 
|---|
| 80 |    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
 | 
|---|
| 81 | 
 | 
|---|
| 82 |    The GNU C Library is free software; you can redistribute it and/or
 | 
|---|
| 83 |    modify it under the terms of the GNU Library General Public License as
 | 
|---|
| 84 |    published by the Free Software Foundation; either version 2 of the
 | 
|---|
| 85 |    License, or (at your option) any later version.
 | 
|---|
| 86 | 
 | 
|---|
| 87 |    The GNU C Library is distributed in the hope that it will be useful,
 | 
|---|
| 88 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 89 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 | 
|---|
| 90 |    Library General Public License for more details.
 | 
|---|
| 91 | 
 | 
|---|
| 92 |    You should have received a copy of the GNU Library General Public
 | 
|---|
| 93 |    License along with the GNU C Library; see the file COPYING.LIB.  If not,
 | 
|---|
| 94 |    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 | 
|---|
| 95 |    Boston, MA 02111-1307, USA.  */
 | 
|---|
| 96 | 
 | 
|---|
| 97 | 
 | 
|---|
| 98 | unsigned short int *
 | 
|---|
| 99 | seed48 (seed16v)
 | 
|---|
| 100 |      unsigned short int seed16v[3];
 | 
|---|
| 101 | {
 | 
|---|
| 102 |   (void) seed48_r (seed16v, &__libc_drand48_data);
 | 
|---|
| 103 | 
 | 
|---|
| 104 |   return __libc_drand48_data.old_X;
 | 
|---|
| 105 | }
 | 
|---|
| 106 | /* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
 | 
|---|
| 107 |    This file is part of the GNU C Library.
 | 
|---|
| 108 |    Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, August 1995.
 | 
|---|
| 109 | 
 | 
|---|
| 110 |    The GNU C Library is free software; you can redistribute it and/or
 | 
|---|
| 111 |    modify it under the terms of the GNU Library General Public License as
 | 
|---|
| 112 |    published by the Free Software Foundation; either version 2 of the
 | 
|---|
| 113 |    License, or (at your option) any later version.
 | 
|---|
| 114 | 
 | 
|---|
| 115 |    The GNU C Library is distributed in the hope that it will be useful,
 | 
|---|
| 116 |    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 117 |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 | 
|---|
| 118 |    Library General Public License for more details.
 | 
|---|
| 119 | 
 | 
|---|
| 120 |    You should have received a copy of the GNU Library General Public
 | 
|---|
| 121 |    License along with the GNU C Library; see the file COPYING.LIB.  If not,
 | 
|---|
| 122 |    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 | 
|---|
| 123 |    Boston, MA 02111-1307, USA.  */
 | 
|---|
| 124 | 
 | 
|---|
| 125 | 
 | 
|---|
| 126 | void
 | 
|---|
| 127 | srand48 (seedval)
 | 
|---|
| 128 |      long seedval;
 | 
|---|
| 129 | {
 | 
|---|
| 130 |   (void) srand48_r (seedval, &__libc_drand48_data);
 | 
|---|
| 131 | }
 | 
|---|