1 | /* |
2 | * This file is part of the MicroPython project, http://micropython.org/ |
3 | * |
4 | * The MIT License (MIT) |
5 | * |
6 | * Copyright (c) 2016 Paul Sokolovsky |
7 | * |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
9 | * of this software and associated documentation files (the "Software"), to deal |
10 | * in the Software without restriction, including without limitation the rights |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12 | * copies of the Software, and to permit persons to whom the Software is |
13 | * furnished to do so, subject to the following conditions: |
14 | * |
15 | * The above copyright notice and this permission notice shall be included in |
16 | * all copies or substantial portions of the Software. |
17 | * |
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24 | * THE SOFTWARE. |
25 | */ |
26 | |
27 | #include <assert.h> |
28 | #include <string.h> |
29 | |
30 | #include "py/runtime.h" |
31 | |
32 | #if MICROPY_PY_URANDOM |
33 | |
34 | // Work out if the seed will be set on import or not. |
35 | #if MICROPY_MODULE_BUILTIN_INIT && defined(MICROPY_PY_URANDOM_SEED_INIT_FUNC) |
36 | #define SEED_ON_IMPORT (1) |
37 | #else |
38 | #define SEED_ON_IMPORT (0) |
39 | #endif |
40 | |
41 | // Yasmarang random number generator |
42 | // by Ilya Levin |
43 | // http://www.literatecode.com/yasmarang |
44 | // Public Domain |
45 | |
46 | #if !MICROPY_ENABLE_DYNRUNTIME |
47 | #if SEED_ON_IMPORT |
48 | // If the state is seeded on import then keep these variables in the BSS. |
49 | STATIC uint32_t yasmarang_pad, yasmarang_n, yasmarang_d; |
50 | STATIC uint8_t yasmarang_dat; |
51 | #else |
52 | // Without seed-on-import these variables must be initialised via the data section. |
53 | STATIC uint32_t yasmarang_pad = 0xeda4baba, yasmarang_n = 69, yasmarang_d = 233; |
54 | STATIC uint8_t yasmarang_dat = 0; |
55 | #endif |
56 | #endif |
57 | |
58 | STATIC uint32_t yasmarang(void) { |
59 | yasmarang_pad += yasmarang_dat + yasmarang_d * yasmarang_n; |
60 | yasmarang_pad = (yasmarang_pad << 3) + (yasmarang_pad >> 29); |
61 | yasmarang_n = yasmarang_pad | 2; |
62 | yasmarang_d ^= (yasmarang_pad << 31) + (yasmarang_pad >> 1); |
63 | yasmarang_dat ^= (char)yasmarang_pad ^ (yasmarang_d >> 8) ^ 1; |
64 | |
65 | return yasmarang_pad ^ (yasmarang_d << 5) ^ (yasmarang_pad >> 18) ^ (yasmarang_dat << 1); |
66 | } /* yasmarang */ |
67 | |
68 | // End of Yasmarang |
69 | |
70 | #if MICROPY_PY_URANDOM_EXTRA_FUNCS |
71 | |
72 | // returns an unsigned integer below the given argument |
73 | // n must not be zero |
74 | STATIC uint32_t yasmarang_randbelow(uint32_t n) { |
75 | uint32_t mask = 1; |
76 | while ((n & mask) < n) { |
77 | mask = (mask << 1) | 1; |
78 | } |
79 | uint32_t r; |
80 | do { |
81 | r = yasmarang() & mask; |
82 | } while (r >= n); |
83 | return r; |
84 | } |
85 | |
86 | #endif |
87 | |
88 | STATIC mp_obj_t mod_urandom_getrandbits(mp_obj_t num_in) { |
89 | int n = mp_obj_get_int(num_in); |
90 | if (n > 32 || n == 0) { |
91 | mp_raise_ValueError(NULL); |
92 | } |
93 | uint32_t mask = ~0; |
94 | // Beware of C undefined behavior when shifting by >= than bit size |
95 | mask >>= (32 - n); |
96 | return mp_obj_new_int_from_uint(yasmarang() & mask); |
97 | } |
98 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_urandom_getrandbits_obj, mod_urandom_getrandbits); |
99 | |
100 | STATIC mp_obj_t mod_urandom_seed(size_t n_args, const mp_obj_t *args) { |
101 | mp_uint_t seed; |
102 | if (n_args == 0 || args[0] == mp_const_none) { |
103 | #ifdef MICROPY_PY_URANDOM_SEED_INIT_FUNC |
104 | seed = MICROPY_PY_URANDOM_SEED_INIT_FUNC; |
105 | #else |
106 | mp_raise_ValueError(MP_ERROR_TEXT("no default seed" )); |
107 | #endif |
108 | } else { |
109 | seed = mp_obj_get_int_truncated(args[0]); |
110 | } |
111 | yasmarang_pad = seed; |
112 | yasmarang_n = 69; |
113 | yasmarang_d = 233; |
114 | yasmarang_dat = 0; |
115 | return mp_const_none; |
116 | } |
117 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_urandom_seed_obj, 0, 1, mod_urandom_seed); |
118 | |
119 | #if MICROPY_PY_URANDOM_EXTRA_FUNCS |
120 | |
121 | STATIC mp_obj_t mod_urandom_randrange(size_t n_args, const mp_obj_t *args) { |
122 | mp_int_t start = mp_obj_get_int(args[0]); |
123 | if (n_args == 1) { |
124 | // range(stop) |
125 | if (start > 0) { |
126 | return mp_obj_new_int(yasmarang_randbelow(start)); |
127 | } else { |
128 | goto error; |
129 | } |
130 | } else { |
131 | mp_int_t stop = mp_obj_get_int(args[1]); |
132 | if (n_args == 2) { |
133 | // range(start, stop) |
134 | if (start < stop) { |
135 | return mp_obj_new_int(start + yasmarang_randbelow(stop - start)); |
136 | } else { |
137 | goto error; |
138 | } |
139 | } else { |
140 | // range(start, stop, step) |
141 | mp_int_t step = mp_obj_get_int(args[2]); |
142 | mp_int_t n; |
143 | if (step > 0) { |
144 | n = (stop - start + step - 1) / step; |
145 | } else if (step < 0) { |
146 | n = (stop - start + step + 1) / step; |
147 | } else { |
148 | goto error; |
149 | } |
150 | if (n > 0) { |
151 | return mp_obj_new_int(start + step * yasmarang_randbelow(n)); |
152 | } else { |
153 | goto error; |
154 | } |
155 | } |
156 | } |
157 | |
158 | error: |
159 | mp_raise_ValueError(NULL); |
160 | } |
161 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_urandom_randrange_obj, 1, 3, mod_urandom_randrange); |
162 | |
163 | STATIC mp_obj_t mod_urandom_randint(mp_obj_t a_in, mp_obj_t b_in) { |
164 | mp_int_t a = mp_obj_get_int(a_in); |
165 | mp_int_t b = mp_obj_get_int(b_in); |
166 | if (a <= b) { |
167 | return mp_obj_new_int(a + yasmarang_randbelow(b - a + 1)); |
168 | } else { |
169 | mp_raise_ValueError(NULL); |
170 | } |
171 | } |
172 | STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_urandom_randint_obj, mod_urandom_randint); |
173 | |
174 | STATIC mp_obj_t mod_urandom_choice(mp_obj_t seq) { |
175 | mp_int_t len = mp_obj_get_int(mp_obj_len(seq)); |
176 | if (len > 0) { |
177 | return mp_obj_subscr(seq, mp_obj_new_int(yasmarang_randbelow(len)), MP_OBJ_SENTINEL); |
178 | } else { |
179 | mp_raise_type(&mp_type_IndexError); |
180 | } |
181 | } |
182 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_urandom_choice_obj, mod_urandom_choice); |
183 | |
184 | #if MICROPY_PY_BUILTINS_FLOAT |
185 | |
186 | // returns a number in the range [0..1) using Yasmarang to fill in the fraction bits |
187 | STATIC mp_float_t yasmarang_float(void) { |
188 | mp_float_union_t u; |
189 | u.p.sgn = 0; |
190 | u.p.exp = (1 << (MP_FLOAT_EXP_BITS - 1)) - 1; |
191 | if (MP_FLOAT_FRAC_BITS <= 32) { |
192 | u.p.frc = yasmarang(); |
193 | } else { |
194 | u.p.frc = ((uint64_t)yasmarang() << 32) | (uint64_t)yasmarang(); |
195 | } |
196 | return u.f - 1; |
197 | } |
198 | |
199 | STATIC mp_obj_t mod_urandom_random(void) { |
200 | return mp_obj_new_float(yasmarang_float()); |
201 | } |
202 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_urandom_random_obj, mod_urandom_random); |
203 | |
204 | STATIC mp_obj_t mod_urandom_uniform(mp_obj_t a_in, mp_obj_t b_in) { |
205 | mp_float_t a = mp_obj_get_float(a_in); |
206 | mp_float_t b = mp_obj_get_float(b_in); |
207 | return mp_obj_new_float(a + (b - a) * yasmarang_float()); |
208 | } |
209 | STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_urandom_uniform_obj, mod_urandom_uniform); |
210 | |
211 | #endif |
212 | |
213 | #endif // MICROPY_PY_URANDOM_EXTRA_FUNCS |
214 | |
215 | #if SEED_ON_IMPORT |
216 | STATIC mp_obj_t mod_urandom___init__() { |
217 | // This module may be imported by more than one name so need to ensure |
218 | // that it's only ever seeded once. |
219 | static bool seeded = false; |
220 | if (!seeded) { |
221 | seeded = true; |
222 | mod_urandom_seed(0, NULL); |
223 | } |
224 | return mp_const_none; |
225 | } |
226 | STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_urandom___init___obj, mod_urandom___init__); |
227 | #endif |
228 | |
229 | #if !MICROPY_ENABLE_DYNRUNTIME |
230 | STATIC const mp_rom_map_elem_t mp_module_urandom_globals_table[] = { |
231 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_urandom) }, |
232 | #if SEED_ON_IMPORT |
233 | { MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&mod_urandom___init___obj) }, |
234 | #endif |
235 | { MP_ROM_QSTR(MP_QSTR_getrandbits), MP_ROM_PTR(&mod_urandom_getrandbits_obj) }, |
236 | { MP_ROM_QSTR(MP_QSTR_seed), MP_ROM_PTR(&mod_urandom_seed_obj) }, |
237 | #if MICROPY_PY_URANDOM_EXTRA_FUNCS |
238 | { MP_ROM_QSTR(MP_QSTR_randrange), MP_ROM_PTR(&mod_urandom_randrange_obj) }, |
239 | { MP_ROM_QSTR(MP_QSTR_randint), MP_ROM_PTR(&mod_urandom_randint_obj) }, |
240 | { MP_ROM_QSTR(MP_QSTR_choice), MP_ROM_PTR(&mod_urandom_choice_obj) }, |
241 | #if MICROPY_PY_BUILTINS_FLOAT |
242 | { MP_ROM_QSTR(MP_QSTR_random), MP_ROM_PTR(&mod_urandom_random_obj) }, |
243 | { MP_ROM_QSTR(MP_QSTR_uniform), MP_ROM_PTR(&mod_urandom_uniform_obj) }, |
244 | #endif |
245 | #endif |
246 | }; |
247 | |
248 | STATIC MP_DEFINE_CONST_DICT(mp_module_urandom_globals, mp_module_urandom_globals_table); |
249 | |
250 | const mp_obj_module_t mp_module_urandom = { |
251 | .base = { &mp_type_module }, |
252 | .globals = (mp_obj_dict_t *)&mp_module_urandom_globals, |
253 | }; |
254 | #endif |
255 | |
256 | #endif // MICROPY_PY_URANDOM |
257 | |