| 1 | /* rng/rng.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 <stdlib.h> |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
| 24 | #include "gsl_errno.h"cxx . . $ `<file>/external/gsl/gsl_errno.h`/ |
| 25 | #include "gsl_rng.h"cxx . . $ `<file>/external/gsl/gsl_rng.h`/ |
| 26 | |
| 27 | gsl_rngcxx . . $ gsl_rng# * |
| 28 | gsl_rng_alloccxx . . $ gsl_rng_alloc(1f772bfa0d4bcafa). (const gsl_rng_typecxx . . $ gsl_rng_type# * T) |
| 29 | { |
| 30 | |
| 31 | gsl_rngcxx . . $ gsl_rng# *r = (gsl_rngcxx . . $ gsl_rng# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (sizeof (gsl_rngcxx . . $ gsl_rng#)); |
| 32 | |
| 33 | if (r == 0) |
| 34 | { |
| 35 | GSL_ERROR_VALcxx . . $ `external/gsl/gsl_errno.h:112:9`! ("failed to allocate space for rng struct", |
| 36 | GSL_ENOMEM, 0); |
| 37 | }; |
| 38 | |
| 39 | r->statecxx . . $ $anonymous_type_ec10d0a94d738359_1#state. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (T->sizecxx . . $ $anonymous_type_ec10d0a94d738359_0#size.); |
| 40 | |
| 41 | if (r->statecxx . . $ $anonymous_type_ec10d0a94d738359_1#state. == 0) |
| 42 | { |
| 43 | freecxx . . $ free(5d22bdacc48458e8). (r); /* exception in constructor, avoid memory leak */ |
| 44 | |
| 45 | GSL_ERROR_VALcxx . . $ `external/gsl/gsl_errno.h:112:9`! ("failed to allocate space for rng state", |
| 46 | GSL_ENOMEM, 0); |
| 47 | }; |
| 48 | |
| 49 | r->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type. = T; |
| 50 | |
| 51 | gsl_rng_setcxx . . $ gsl_rng_set(eefb28dacfbccaab). (r, gsl_rng_default_seedcxx . . $ gsl_rng_default_seed.); /* seed the generator */ |
| 52 | |
| 53 | return r; |
| 54 | } |
| 55 | |
| 56 | int |
| 57 | gsl_rng_memcpycxx . . $ gsl_rng_memcpy(c115edc3cade2dda). (gsl_rngcxx . . $ gsl_rng# * dest, const gsl_rngcxx . . $ gsl_rng# * src) |
| 58 | { |
| 59 | if (dest->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type. != src->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type.) |
| 60 | { |
| 61 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("generators must be of the same type", GSL_EINVAL); |
| 62 | } |
| 63 | |
| 64 | memcpycxx . . $ memcpy(cc817cb175f282d). (dest->statecxx . . $ $anonymous_type_ec10d0a94d738359_1#state., src->statecxx . . $ $anonymous_type_ec10d0a94d738359_1#state., src->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type.->sizecxx . . $ $anonymous_type_ec10d0a94d738359_0#size.); |
| 65 | |
| 66 | return GSL_SUCCESScxx . . $ GSL_SUCCESS.; |
| 67 | } |
| 68 | |
| 69 | gsl_rngcxx . . $ gsl_rng# * |
| 70 | gsl_rng_clonecxx . . $ gsl_rng_clone(2d4fa4d79abeecf8). (const gsl_rngcxx . . $ gsl_rng# * q) |
| 71 | { |
| 72 | gsl_rngcxx . . $ gsl_rng# *r = (gsl_rngcxx . . $ gsl_rng# *) malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (sizeof (gsl_rngcxx . . $ gsl_rng#)); |
| 73 | |
| 74 | if (r == 0) |
| 75 | { |
| 76 | GSL_ERROR_VALcxx . . $ `external/gsl/gsl_errno.h:112:9`! ("failed to allocate space for rng struct", |
| 77 | GSL_ENOMEM, 0); |
| 78 | }; |
| 79 | |
| 80 | r->statecxx . . $ $anonymous_type_ec10d0a94d738359_1#state. = malloccxx . . $ malloc(bfcfa4d6b7f7ef64). (q->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type.->sizecxx . . $ $anonymous_type_ec10d0a94d738359_0#size.); |
| 81 | |
| 82 | if (r->statecxx . . $ $anonymous_type_ec10d0a94d738359_1#state. == 0) |
| 83 | { |
| 84 | freecxx . . $ free(5d22bdacc48458e8). (r); /* exception in constructor, avoid memory leak */ |
| 85 | |
| 86 | GSL_ERROR_VALcxx . . $ `external/gsl/gsl_errno.h:112:9`! ("failed to allocate space for rng state", |
| 87 | GSL_ENOMEM, 0); |
| 88 | }; |
| 89 | |
| 90 | r->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type. = q->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type.; |
| 91 | |
| 92 | memcpycxx . . $ memcpy(cc817cb175f282d). (r->statecxx . . $ $anonymous_type_ec10d0a94d738359_1#state., q->statecxx . . $ $anonymous_type_ec10d0a94d738359_1#state., q->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type.->sizecxx . . $ $anonymous_type_ec10d0a94d738359_0#size.); |
| 93 | |
| 94 | return r; |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | gsl_rng_setcxx . . $ gsl_rng_set(eefb28dacfbccaab). (const gsl_rngcxx . . $ gsl_rng# * r, unsigned long int seed) |
| 99 | { |
| 100 | (r->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type.->setcxx . . $ $anonymous_type_ec10d0a94d738359_0#set.) (r->statecxx . . $ $anonymous_type_ec10d0a94d738359_1#state., seed); |
| 101 | } |
| 102 | |
| 103 | #ifndef HIDE_INLINE_STATIC |
| 104 | unsigned long int |
| 105 | gsl_rng_getcxx . . $ gsl_rng_get(9d16a3db12989fd6). (const gsl_rngcxx . . $ gsl_rng# * r) |
| 106 | { |
| 107 | return (r->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type.->getcxx . . $ $anonymous_type_ec10d0a94d738359_0#get.) (r->statecxx . . $ $anonymous_type_ec10d0a94d738359_1#state.); |
| 108 | } |
| 109 | |
| 110 | double |
| 111 | gsl_rng_uniformcxx . . $ gsl_rng_uniform(ec5f992ac51a20eb). (const gsl_rngcxx . . $ gsl_rng# * r) |
| 112 | { |
| 113 | return (r->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type.->get_doublecxx . . $ $anonymous_type_ec10d0a94d738359_0#get_double.) (r->statecxx . . $ $anonymous_type_ec10d0a94d738359_1#state.); |
| 114 | } |
| 115 | |
| 116 | double |
| 117 | gsl_rng_uniform_poscxx . . $ gsl_rng_uniform_pos(ec5f992ac51a20eb). (const gsl_rngcxx . . $ gsl_rng# * r) |
| 118 | { |
| 119 | double x ; |
| 120 | do |
| 121 | { |
| 122 | x = (r->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type.->get_doublecxx . . $ $anonymous_type_ec10d0a94d738359_0#get_double.) (r->statecxx . . $ $anonymous_type_ec10d0a94d738359_1#state.) ; |
| 123 | } |
| 124 | while (x == 0) ; |
| 125 | |
| 126 | return x ; |
| 127 | } |
| 128 | |
| 129 | /* Note: to avoid integer overflow in (range+1) we work with scale = |
| 130 | range/n = (max-min)/n rather than scale=(max-min+1)/n, this reduces |
| 131 | efficiency slightly but avoids having to check for the out of range |
| 132 | value. Note that range is typically O(2^32) so the addition of 1 |
| 133 | is negligible in most usage. */ |
| 134 | |
| 135 | unsigned long int |
| 136 | gsl_rng_uniform_intcxx . . $ gsl_rng_uniform_int(b3407230488f1681). (const gsl_rngcxx . . $ gsl_rng# * r, unsigned long int n) |
| 137 | { |
| 138 | unsigned long int offset = r->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type.->mincxx . . $ $anonymous_type_ec10d0a94d738359_0#min.; |
| 139 | unsigned long int range = r->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type.->maxcxx . . $ $anonymous_type_ec10d0a94d738359_0#max. - offset; |
| 140 | unsigned long int scale; |
| 141 | unsigned long int k; |
| 142 | |
| 143 | if (n > range || n == 0) |
| 144 | { |
| 145 | GSL_ERROR_VALcxx . . $ `external/gsl/gsl_errno.h:112:9`! ("invalid n, either 0 or exceeds maximum value of generator", |
| 146 | GSL_EINVAL, 0) ; |
| 147 | } |
| 148 | |
| 149 | scale = range / n; |
| 150 | |
| 151 | do |
| 152 | { |
| 153 | k = (((r->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type.->getcxx . . $ $anonymous_type_ec10d0a94d738359_0#get.) (r->statecxx . . $ $anonymous_type_ec10d0a94d738359_1#state.)) - offset) / scale; |
| 154 | } |
| 155 | while (k >= n); |
| 156 | |
| 157 | return k; |
| 158 | } |
| 159 | #endif |
| 160 | |
| 161 | unsigned long int |
| 162 | gsl_rng_maxcxx . . $ gsl_rng_max(9d16a3db12989fd6). (const gsl_rngcxx . . $ gsl_rng# * r) |
| 163 | { |
| 164 | return r->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type.->maxcxx . . $ $anonymous_type_ec10d0a94d738359_0#max.; |
| 165 | } |
| 166 | |
| 167 | unsigned long int |
| 168 | gsl_rng_mincxx . . $ gsl_rng_min(9d16a3db12989fd6). (const gsl_rngcxx . . $ gsl_rng# * r) |
| 169 | { |
| 170 | return r->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type.->mincxx . . $ $anonymous_type_ec10d0a94d738359_0#min.; |
| 171 | } |
| 172 | |
| 173 | const char * |
| 174 | gsl_rng_namecxx . . $ gsl_rng_name(2babb1c2fb3cff8e). (const gsl_rngcxx . . $ gsl_rng# * r) |
| 175 | { |
| 176 | return r->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type.->namecxx . . $ $anonymous_type_ec10d0a94d738359_0#name.; |
| 177 | } |
| 178 | |
| 179 | size_t |
| 180 | gsl_rng_sizecxx . . $ gsl_rng_size(9d16a3db12989fd6). (const gsl_rngcxx . . $ gsl_rng# * r) |
| 181 | { |
| 182 | return r->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type.->sizecxx . . $ $anonymous_type_ec10d0a94d738359_0#size.; |
| 183 | } |
| 184 | |
| 185 | void * |
| 186 | gsl_rng_statecxx . . $ gsl_rng_state(d293253cf6410285). (const gsl_rngcxx . . $ gsl_rng# * r) |
| 187 | { |
| 188 | return r->statecxx . . $ $anonymous_type_ec10d0a94d738359_1#state.; |
| 189 | } |
| 190 | |
| 191 | void |
| 192 | gsl_rng_print_statecxx . . $ gsl_rng_print_state(ce242e4234f4aab4). (const gsl_rngcxx . . $ gsl_rng# * r) |
| 193 | { |
| 194 | size_t i; |
| 195 | unsigned char *p = (unsigned char *) (r->statecxx . . $ $anonymous_type_ec10d0a94d738359_1#state.); |
| 196 | const size_t n = r->typecxx . . $ $anonymous_type_ec10d0a94d738359_1#type.->sizecxx . . $ $anonymous_type_ec10d0a94d738359_0#size.; |
| 197 | |
| 198 | for (i = 0; i < n; i++) |
| 199 | { |
| 200 | /* FIXME: we're assuming that a char is 8 bits */ |
| 201 | printfcxx . . $ printf(581fddd72a8db8fc). ("%.2x", *(p + i)); |
| 202 | } |
| 203 | |
| 204 | } |
| 205 | |
| 206 | void |
| 207 | gsl_rng_freecxx . . $ gsl_rng_free(6074d0bd62578ee4). (gsl_rngcxx . . $ gsl_rng# * r) |
| 208 | { |
| 209 | freecxx . . $ free(5d22bdacc48458e8). (r->statecxx . . $ $anonymous_type_ec10d0a94d738359_1#state.); |
| 210 | freecxx . . $ free(5d22bdacc48458e8). (r); |
| 211 | } |