| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include "glk.h" |
| 7 |
#include "glulxe.h" |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 45 |
void *glulx_malloc(glui32 len) |
| 46 |
{ |
| 47 |
return malloc(len); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
void *glulx_realloc(void *ptr, glui32 len) |
| 54 |
{ |
| 55 |
return realloc(ptr, len); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
void glulx_free(void *ptr) |
| 60 |
{ |
| 61 |
free(ptr); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
#define RAND_SET_SEED() (xo_seed_random(time(NULL))) |
| 66 |
#define RAND_GET() (xo_random()) |
| 67 |
|
| 68 |
#endif |
| 69 |
|
| 70 |
#ifdef OS_UNIX |
| 71 |
|
| 72 |
#include <time.h> |
| 73 |
#include <stdlib.h> |
| 74 |
|
| 75 |
|
| 76 |
void *glulx_malloc(glui32 len) |
| 77 |
{ |
| 78 |
return malloc(len); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
void *glulx_realloc(void *ptr, glui32 len) |
| 85 |
{ |
| 86 |
return realloc(ptr, len); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
void glulx_free(void *ptr) |
| 91 |
{ |
| 92 |
free(ptr); |
| 93 |
} |
| 94 |
|
| 95 |
#ifdef UNIX_RAND_ARC4 |
| 96 |
|
| 97 |
|
| 98 |
#define RAND_SET_SEED() (0) |
| 99 |
#define RAND_GET() (arc4random()) |
| 100 |
|
| 101 |
#elif UNIX_RAND_GETRANDOM |
| 102 |
|
| 103 |
|
| 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 |
|
| 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 |
| 123 |
|
| 124 |
|
| 125 |
#define RAND_SET_SEED() (xo_seed_random(time(NULL))) |
| 126 |
#define RAND_GET() (xo_random()) |
| 127 |
|
| 128 |
#endif |
| 129 |
|
| 130 |
#endif |
| 131 |
|
| 132 |
#ifdef OS_MAC |
| 133 |
|
| 134 |
#include <stdlib.h> |
| 135 |
|
| 136 |
|
| 137 |
void *glulx_malloc(glui32 len) |
| 138 |
{ |
| 139 |
return malloc(len); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
void *glulx_realloc(void *ptr, glui32 len) |
| 146 |
{ |
| 147 |
return realloc(ptr, len); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
void glulx_free(void *ptr) |
| 152 |
{ |
| 153 |
free(ptr); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
#define RAND_SET_SEED() (0) |
| 158 |
#define RAND_GET() (arc4random()) |
| 159 |
|
| 160 |
#endif |
| 161 |
|
| 162 |
#ifdef OS_WINDOWS |
| 163 |
|
| 164 |
#ifdef _MSC_VER |
| 165 |
#define _CRT_RAND_S |
| 166 |
#endif |
| 167 |
|
| 168 |
#include <time.h> |
| 169 |
#include <stdlib.h> |
| 170 |
|
| 171 |
|
| 172 |
void *glulx_malloc(glui32 len) |
| 173 |
{ |
| 174 |
return malloc(len); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
void *glulx_realloc(void *ptr, glui32 len) |
| 181 |
{ |
| 182 |
return realloc(ptr, len); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
void glulx_free(void *ptr) |
| 187 |
{ |
| 188 |
free(ptr); |
| 189 |
} |
| 190 |
|
| 191 |
#ifdef _MSC_VER |
| 192 |
|
| 193 |
|
| 194 |
static void msc_srandom() |
| 195 |
{ |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 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 |
| 211 |
|
| 212 |
|
| 213 |
#define RAND_SET_SEED() (xo_seed_random(time(NULL))) |
| 214 |
#define RAND_GET() (xo_random()) |
| 215 |
|
| 216 |
#endif |
| 217 |
|
| 218 |
#endif |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
#ifndef RAND_SET_SEED |
| 223 |
#define RAND_SET_SEED() (xo_seed_random(time(NULL))) |
| 224 |
#define RAND_GET() (xo_random()) |
| 225 |
#endif |
| 226 |
|
| 227 |
static int rand_use_native = TRUE; |
| 228 |
|
| 229 |
|
| 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 |
|
| 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 |
|
| 256 |
|
| 257 |
|
| 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 |
|
| 264 |
|
| 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 |
|
| 275 |
|
| 276 |
|
| 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 |
|
| 292 |
|
| 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 |
|
| 317 |
|
| 318 |
|
| 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 |
|
| 329 |
|
| 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 |
| 342 |
|
| 343 |
#ifdef DOUBLE_SUPPORT |
| 344 |
|
| 345 |
|
| 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 |