| 1 | /* rng/ranf.c |
| 2 | * |
| 3 | * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include "gsl__config.h"cxx . . $ `<file>/external/gsl/gsl__config.h`/ |
| 21 | #include <math.h> |
| 22 | #include <stdlib.h> |
| 23 | #include "gsl_rng.h"cxx . . $ `<file>/external/gsl/gsl_rng.h`/ |
| 24 | |
| 25 | /* This is the CRAY RANF generator. The generator returns the |
| 26 | upper 32 bits from each term of the sequence, |
| 27 | |
| 28 | x_{n+1} = (a x_n) mod m |
| 29 | |
| 30 | using 48-bit unsigned arithmetic, with a = 0x2875A2E7B175 and m = |
| 31 | 2^48. The seed specifies the lower 32 bits of the initial value, |
| 32 | x_1, with the lowest bit set (to prevent the seed taking an even |
| 33 | value), and the upper 16 bits set to 0. |
| 34 | |
| 35 | There is a subtlety in the implementation of the seed. The initial |
| 36 | state is put one step back by multiplying by the modular inverse of |
| 37 | a mod m. This is done for compatibility with the original CRAY |
| 38 | implementation. |
| 39 | |
| 40 | Note, you can only seed the generator with integers up to 2^32, |
| 41 | while the CRAY uses wide integers which can cover all 2^48 states |
| 42 | of the generator. |
| 43 | |
| 44 | The theoretical value of x_{10001} is 141091827447341. |
| 45 | |
| 46 | The period of this generator is 2^{46}. */ |
| 47 | |
| 48 | static inline void ranf_advancecxx . . $ ranf_advance(5d22bdacc48458e8). (void *vstate); |
| 49 | static unsigned long int ranf_getcxx . . $ ranf_get(751e5d26f8574c32). (void *vstate); |
| 50 | static double ranf_get_doublecxx . . $ ranf_get_double(a405b17f82753acc). (void *vstate); |
| 51 | static void ranf_setcxx . . $ ranf_set(5500c4293b1bbef0). (void *state, unsigned long int s); |
| 52 | |
| 53 | static const unsigned short int a0cxx . . $ a0. = 0xB175 ; |
| 54 | static const unsigned short int a1cxx . . $ a1. = 0xA2E7 ; |
| 55 | static const unsigned short int a2cxx . . $ a2. = 0x2875 ; |
| 56 | |
| 57 | typedef structcxx . . $ $anonymous_type_57c2e9a725bf24f3_0# |
| 58 | { |
| 59 | unsigned short int x0cxx . . $ $anonymous_type_57c2e9a725bf24f3_0#x0., x1cxx . . $ $anonymous_type_57c2e9a725bf24f3_0#x1., x2cxx . . $ $anonymous_type_57c2e9a725bf24f3_0#x2.; |
| 60 | } |
| 61 | ranf_state_tcxx . . $ ranf_state_t#; |
| 62 | |
| 63 | static inline void |
| 64 | ranf_advancecxx . . $ ranf_advance(5d22bdacc48458e8). (void *vstate) |
| 65 | { |
| 66 | ranf_state_tcxx . . $ ranf_state_t# *state = (ranf_state_tcxx . . $ ranf_state_t# *) vstate; |
| 67 | |
| 68 | const unsigned long int x0 = (unsigned long int) state->x0cxx . . $ $anonymous_type_57c2e9a725bf24f3_0#x0. ; |
| 69 | const unsigned long int x1 = (unsigned long int) state->x1cxx . . $ $anonymous_type_57c2e9a725bf24f3_0#x1. ; |
| 70 | const unsigned long int x2 = (unsigned long int) state->x2cxx . . $ $anonymous_type_57c2e9a725bf24f3_0#x2. ; |
| 71 | |
| 72 | unsigned long int r ; |
| 73 | |
| 74 | r = a0cxx . . $ a0. * x0 ; |
| 75 | state->x0cxx . . $ $anonymous_type_57c2e9a725bf24f3_0#x0. = (r & 0xFFFF) ; |
| 76 | |
| 77 | r >>= 16 ; |
| 78 | r += a0cxx . . $ a0. * x1 + a1cxx . . $ a1. * x0 ; |
| 79 | state->x1cxx . . $ $anonymous_type_57c2e9a725bf24f3_0#x1. = (r & 0xFFFF) ; |
| 80 | |
| 81 | r >>= 16 ; |
| 82 | r += a0cxx . . $ a0. * x2 + a1cxx . . $ a1. * x1 + a2cxx . . $ a2. * x0 ; |
| 83 | state->x2cxx . . $ $anonymous_type_57c2e9a725bf24f3_0#x2. = (r & 0xFFFF) ; |
| 84 | } |
| 85 | |
| 86 | static unsigned long int |
| 87 | ranf_getcxx . . $ ranf_get(751e5d26f8574c32). (void *vstate) |
| 88 | { |
| 89 | unsigned long int x1, x2; |
| 90 | |
| 91 | ranf_state_tcxx . . $ ranf_state_t# *state = (ranf_state_tcxx . . $ ranf_state_t# *) vstate; |
| 92 | ranf_advancecxx . . $ ranf_advance(5d22bdacc48458e8). (state) ; |
| 93 | |
| 94 | x1 = (unsigned long int) state->x1cxx . . $ $anonymous_type_57c2e9a725bf24f3_0#x1.; |
| 95 | x2 = (unsigned long int) state->x2cxx . . $ $anonymous_type_57c2e9a725bf24f3_0#x2.; |
| 96 | |
| 97 | return (x2 << 16) + x1; |
| 98 | } |
| 99 | |
| 100 | static double |
| 101 | ranf_get_doublecxx . . $ ranf_get_double(a405b17f82753acc). (void * vstate) |
| 102 | { |
| 103 | ranf_state_tcxx . . $ ranf_state_t# *state = (ranf_state_tcxx . . $ ranf_state_t# *) vstate; |
| 104 | |
| 105 | ranf_advancecxx . . $ ranf_advance(5d22bdacc48458e8). (state) ; |
| 106 | |
| 107 | return (ldexp((double) state->x2cxx . . $ $anonymous_type_57c2e9a725bf24f3_0#x2., -16) |
| 108 | + ldexp((double) state->x1cxx . . $ $anonymous_type_57c2e9a725bf24f3_0#x1., -32) |
| 109 | + ldexp((double) state->x0cxx . . $ $anonymous_type_57c2e9a725bf24f3_0#x0., -48)) ; |
| 110 | } |
| 111 | |
| 112 | static void |
| 113 | ranf_setcxx . . $ ranf_set(5500c4293b1bbef0). (void *vstate, unsigned long int s) |
| 114 | { |
| 115 | ranf_state_tcxx . . $ ranf_state_t# *state = (ranf_state_tcxx . . $ ranf_state_t# *) vstate; |
| 116 | |
| 117 | unsigned short int x0, x1, x2 ; |
| 118 | unsigned long int r ; |
| 119 | |
| 120 | unsigned long int b0 = 0xD6DD ; |
| 121 | unsigned long int b1 = 0xB894 ; |
| 122 | unsigned long int b2 = 0x5CEE ; |
| 123 | |
| 124 | if (s == 0) /* default seed */ |
| 125 | { |
| 126 | x0 = 0x9CD1 ; |
| 127 | x1 = 0x53FC ; |
| 128 | x2 = 0x9482 ; |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | x0 = (s | 1) & 0xFFFF ; |
| 133 | x1 = s >> 16 & 0xFFFF ; |
| 134 | x2 = 0 ; |
| 135 | } |
| 136 | |
| 137 | r = b0 * x0 ; |
| 138 | state->x0cxx . . $ $anonymous_type_57c2e9a725bf24f3_0#x0. = (r & 0xFFFF) ; |
| 139 | |
| 140 | r >>= 16 ; |
| 141 | r += b0 * x1 + b1 * x0 ; |
| 142 | state->x1cxx . . $ $anonymous_type_57c2e9a725bf24f3_0#x1. = (r & 0xFFFF) ; |
| 143 | |
| 144 | r >>= 16 ; |
| 145 | r += b0 * x2 + b1 * x1 + b2 * x0 ; |
| 146 | state->x2cxx . . $ $anonymous_type_57c2e9a725bf24f3_0#x2. = (r & 0xFFFF) ; |
| 147 | |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | static const gsl_rng_typecxx . . $ gsl_rng_type# ranf_typecxx . . $ ranf_type. = |
| 152 | {"ranf", /* name */ |
| 153 | 0xffffffffUL, /* RAND_MAX */ |
| 154 | 0, /* RAND_MIN */ |
| 155 | sizeof (ranf_state_tcxx . . $ ranf_state_t#), |
| 156 | &ranf_setcxx . . $ ranf_set(5500c4293b1bbef0)., |
| 157 | &ranf_getcxx . . $ ranf_get(751e5d26f8574c32)., |
| 158 | &ranf_get_doublecxx . . $ ranf_get_double(a405b17f82753acc). |
| 159 | }; |
| 160 | |
| 161 | const gsl_rng_typecxx . . $ gsl_rng_type# *gsl_rng_ranfcxx . . $ gsl_rng_ranf. = &ranf_typecxx . . $ ranf_type.; |