Debian Codegraph @

terps/glulxe/osdepend.c

raw symbols SCIP
1 /* osdepend.c: Glulxe platform-dependent code.
2 Designed by Andrew Plotkin <[email protected]>
3 http://eblong.com/zarf/glulx/index.html
4 */
5
6 #include "glk.h"
7 #include "glulxe.h"
8
9 /* This file contains definitions for platform-dependent code. Since
10 Glk takes care of I/O, this is a short list -- memory allocation
11 and random numbers.
12
13 The Makefile (or whatever) should define OS_UNIX, or some other
14 symbol. Code contributions welcome.
15 */
16
17
18 /* We have a slightly baroque random-number scheme. If the Glulx
19 @setrandom opcode is given seed 0, we use "true" randomness, from a
20 platform native RNG if possible. If @setrandom is given a nonzero
21 seed, we use a simple xoshiro128** RNG (provided below). The
22 use of a known algorithm aids cross-platform testing and debugging.
23 (Those being the cases where you'd set a nonzero seed.)
24
25 To define a native RNG, define the macros RAND_SET_SEED() (seed the
26 RNG with the clock or some other truly random source) and RAND_GET()
27 (grab a number). Note that RAND_SET_SEED() does not take an argument;
28 it is only called when seed=0. If RAND_GET() calls a non-seeded RNG
29 API (such as arc4random()), then RAND_SET_SEED() should be a no-op.
30
31 If RAND_SET_SEED/RAND_GET are not provided, we call back to the same
32 xoshiro128** RNG as before, but seeded from the system clock.
33 */
34
35 static glui32 xo_random(void);
36 static void xo_seed_random(glui32 seed);
37 static void xo_seed_random_4(glui32 seed0, glui32 seed1, glui32 seed2, glui32 seed3);
38
39 #ifdef OS_STDC
40
41 #include <time.h>
42 #include <stdlib.h>
43
44 /* Allocate a chunk of memory. */
45 void *glulx_malloc(glui32 len)
46 {
47 return malloc(len);
48 }
49
50 /* Resize a chunk of memory. This must follow ANSI rules: if the
51 size-change fails, this must return NULL, but the original chunk
52 must remain unchanged. */
53 void *glulx_realloc(void *ptr, glui32 len)
54 {
55 return realloc(ptr, len);
56 }
57
58 /* Deallocate a chunk of memory. */
59 void glulx_free(void *ptr)
60 {
61 free(ptr);
62 }
63
64 /* Use our xoshiro128** as the native RNG, seeded from the clock. */
65 #define RAND_SET_SEED() (xo_seed_random(time(NULL)))
66 #define RAND_GET() (xo_random())
67
68 #endif /* OS_STDC */
69
70 #ifdef OS_UNIX
71
72 #include <time.h>
73 #include <stdlib.h>
74
75 /* Allocate a chunk of memory. */
76 void *glulx_malloc(glui32 len)
77 {
78 return malloc(len);
79 }
80
81 /* Resize a chunk of memory. This must follow ANSI rules: if the
82 size-change fails, this must return NULL, but the original chunk
83 must remain unchanged. */
84 void *glulx_realloc(void *ptr, glui32 len)
85 {
86 return realloc(ptr, len);
87 }
88
89 /* Deallocate a chunk of memory. */
90 void glulx_free(void *ptr)
91 {
92 free(ptr);
93 }
94
95 #ifdef UNIX_RAND_ARC4
96
97 /* Use arc4random() as the native RNG. It doesn't need to be seeded. */
98 #define RAND_SET_SEED() (0)
99 #define RAND_GET() (arc4random())
100
101 #elif UNIX_RAND_GETRANDOM
102
103 /* Use xoshiro128** as the native RNG, seeded from getrandom(). */
104 #include <sys/random.h>
105
106 static void rand_set_seed(void)
107 {
108 glui32 seeds[4];
109 int res = getrandom(seeds, 4*sizeof(glui32), 0);
110 if (res < 0) {
111 /* Error; fall back to the clock. */
112 xo_seed_random(time(NULL));
113 }
114 else {
115 xo_seed_random_4(seeds[0], seeds[1], seeds[2], seeds[3]);
116 }
117 }
118
119 #define RAND_SET_SEED() (rand_set_seed())
120 #define RAND_GET() (xo_random())
121
122 #else /* UNIX_RAND_... */
123
124 /* Use our xoshiro128** as the native RNG, seeded from the clock. */
125 #define RAND_SET_SEED() (xo_seed_random(time(NULL)))
126 #define RAND_GET() (xo_random())
127
128 #endif /* UNIX_RAND_... */
129
130 #endif /* OS_UNIX */
131
132 #ifdef OS_MAC
133
134 #include <stdlib.h>
135
136 /* Allocate a chunk of memory. */
137 void *glulx_malloc(glui32 len)
138 {
139 return malloc(len);
140 }
141
142 /* Resize a chunk of memory. This must follow ANSI rules: if the
143 size-change fails, this must return NULL, but the original chunk
144 must remain unchanged. */
145 void *glulx_realloc(void *ptr, glui32 len)
146 {
147 return realloc(ptr, len);
148 }
149
150 /* Deallocate a chunk of memory. */
151 void glulx_free(void *ptr)
152 {
153 free(ptr);
154 }
155
156 /* Use arc4random() as the native RNG. It doesn't need to be seeded. */
157 #define RAND_SET_SEED() (0)
158 #define RAND_GET() (arc4random())
159
160 #endif /* OS_MAC */
161
162 #ifdef OS_WINDOWS
163
164 #ifdef _MSC_VER /* For Visual C++, get rand_s() */
165 #define _CRT_RAND_S
166 #endif
167
168 #include <time.h>
169 #include <stdlib.h>
170
171 /* Allocate a chunk of memory. */
172 void *glulx_malloc(glui32 len)
173 {
174 return malloc(len);
175 }
176
177 /* Resize a chunk of memory. This must follow ANSI rules: if the
178 size-change fails, this must return NULL, but the original chunk
179 must remain unchanged. */
180 void *glulx_realloc(void *ptr, glui32 len)
181 {
182 return realloc(ptr, len);
183 }
184
185 /* Deallocate a chunk of memory. */
186 void glulx_free(void *ptr)
187 {
188 free(ptr);
189 }
190
191 #ifdef _MSC_VER /* Visual C++ */
192
193 /* Do nothing, as rand_s() has no seed. */
194 static void msc_srandom()
195 {
196 }
197
198 /* Use the Visual C++ function rand_s() as the native RNG.
199 This calls the OS function RtlGetRandom(). */
200 static glui32 msc_random()
201 {
202 glui32 value;
203 rand_s(&value);
204 return value;
205 }
206
207 #define RAND_SET_SEED() (msc_srandom())
208 #define RAND_GET() (msc_random())
209
210 #else /* Other Windows compilers */
211
212 /* Use our xoshiro128** as the native RNG, seeded from the clock. */
213 #define RAND_SET_SEED() (xo_seed_random(time(NULL)))
214 #define RAND_GET() (xo_random())
215
216 #endif
217
218 #endif /* OS_WINDOWS */
219
220
221 /* If no native RNG is defined above, use the xoshiro128** implementation. */
222 #ifndef RAND_SET_SEED
223 #define RAND_SET_SEED() (xo_seed_random(time(NULL)))
224 #define RAND_GET() (xo_random())
225 #endif /* RAND_SET_SEED */
226
227 static int rand_use_native = TRUE;
228
229 /* Set the random-number seed, and also select which RNG to use.
230 */
231 void glulx_setrandom(glui32 seed)
232 {
233 if (seed == 0) {
234 rand_use_native = TRUE;
235 RAND_SET_SEED();
236 }
237 else {
238 rand_use_native = FALSE;
239 xo_seed_random(seed);
240 }
241 }
242
243 /* Return a random number in the range 0 to 2^32-1. */
244 glui32 glulx_random()
245 {
246 if (rand_use_native) {
247 return RAND_GET();
248 }
249 else {
250 return xo_random();
251 }
252 }
253
254
255 /* This is the "xoshiro128**" random-number generator and seed function.
256 Adapted from: https://prng.di.unimi.it/xoshiro128starstar.c
257 About this algorithm: https://prng.di.unimi.it/
258 */
259 static uint32_t xo_table[4];
260
261 static void xo_seed_random_4(glui32 seed0, glui32 seed1, glui32 seed2, glui32 seed3)
262 {
263 /* Set up the 128-bit state from four integers. Use this if you can get
264 four high-quality random values. */
265 xo_table[0] = seed0;
266 xo_table[1] = seed1;
267 xo_table[2] = seed2;
268 xo_table[3] = seed3;
269 }
270
271 static void xo_seed_random(glui32 seed)
272 {
273 int ix;
274 /* Set up the 128-bit state from a single 32-bit integer. We rely
275 on a different RNG, SplitMix32. This isn't high-quality, but we
276 just need to get a bunch of bits into xo_table. */
277 for (ix=0; ix<4; ix++) {
278 seed += 0x9E3779B9;
279 glui32 s = seed;
280 s ^= s >> 15;
281 s *= 0x85EBCA6B;
282 s ^= s >> 13;
283 s *= 0xC2B2AE35;
284 s ^= s >> 16;
285 xo_table[ix] = s;
286 }
287 }
288
289 static glui32 xo_random(void)
290 {
291 /* I've inlined the utility function:
292 rotl(x, k) => (x << k) | (x >> (32 - k))
293 */
294
295 const uint32_t t1x5 = xo_table[1] * 5;
296 const uint32_t result = ((t1x5 << 7) | (t1x5 >> (32-7))) * 9;
297
298 const uint32_t t1s9 = xo_table[1] << 9;
299
300 xo_table[2] ^= xo_table[0];
301 xo_table[3] ^= xo_table[1];
302 xo_table[1] ^= xo_table[2];
303 xo_table[0] ^= xo_table[3];
304
305 xo_table[2] ^= t1s9;
306
307 const uint32_t t3 = xo_table[3];
308 xo_table[3] = ((t3 << 11) | (t3 >> (32-11)));
309
310 return result;
311 }
312
313
314 #include <stdlib.h>
315
316 /* I'm putting a wrapper for qsort() here, in case I ever have to
317 worry about a platform without it. But I am not worrying at
318 present. */
319 void glulx_sort(void *addr, int count, int size,
320 int (*comparefunc)(void *p1, void *p2))
321 {
322 qsort(addr, count, size, (int (*)(const void *, const void *))comparefunc);
323 }
324
325 #ifdef FLOAT_SUPPORT
326 #include <math.h>
327
328 /* This wrapper handles all special cases, even if the underlying
329 powf() function doesn't. */
330 gfloat32 glulx_powf(gfloat32 val1, gfloat32 val2)
331 {
332 if (val1 == 1.0f)
333 return 1.0f;
334 else if ((val2 == 0.0f) || (val2 == -0.0f))
335 return 1.0f;
336 else if ((val1 == -1.0f) && isinf(val2))
337 return 1.0f;
338 return powf(val1, val2);
339 }
340
341 #endif /* FLOAT_SUPPORT */
342
343 #ifdef DOUBLE_SUPPORT
344
345 /* Same for pow(). */
346 extern gfloat64 glulx_pow(gfloat64 val1, gfloat64 val2)
347 {
348 if (val1 == 1.0)
349 return 1.0;
350 else if ((val2 == 0.0) || (val2 == -0.0))
351 return 1.0;
352 else if ((val1 == -1.0) && isinf(val2))
353 return 1.0;
354 return pow(val1, val2);
355 }
356
357 #endif /* DOUBLE_SUPPORT */