1 /* osdepend.c: Glulxe platform-dependent code.
2 Designed by Andrew Plotkin <erkyrath@eblong.com>
3 http://eblong.com/zarf/glulx/index.html
9 /* This file contains definitions for platform-dependent code. Since
10 Glk takes care of I/O, this is a short list -- memory allocation
13 The Makefile (or whatever) should define OS_UNIX, or some other
14 symbol. Code contributions welcome.
22 /* Allocate a chunk of memory. */
23 void *glulx_malloc(glui32 len)
28 /* Resize a chunk of memory. This must follow ANSI rules: if the
29 size-change fails, this must return NULL, but the original chunk
30 must remain unchanged. */
31 void *glulx_realloc(void *ptr, glui32 len)
33 return realloc(ptr, len);
36 /* Deallocate a chunk of memory. */
37 void glulx_free(void *ptr)
42 /* Set the random-number seed; zero means use as random a source as
44 void glulx_setrandom(glui32 seed)
51 /* Return a random number in the range 0 to 2^32-1. */
54 return (random() << 16) ^ random();
61 /* The Glk library uses malloc/free liberally, so we might as well also. */
64 /* Allocate a chunk of memory. */
65 void *glulx_malloc(glui32 len)
70 /* Resize a chunk of memory. This must follow ANSI rules: if the
71 size-change fails, this must return NULL, but the original chunk
72 must remain unchanged. */
73 void *glulx_realloc(void *ptr, glui32 len)
75 return realloc(ptr, len);
78 /* Deallocate a chunk of memory. */
79 void glulx_free(void *ptr)
84 #define COMPILE_RANDOM_CODE
85 static glui32 lo_random(void);
86 static void lo_seed_random(glui32 seed);
88 /* Return a random number in the range 0 to 2^32-1. */
91 return (lo_random() << 16) ^ lo_random();
94 /* Set the random-number seed; zero means use as random a source as
96 void glulx_setrandom(glui32 seed)
99 seed = TickCount() ^ Random();
100 lo_seed_random(seed);
110 /* Allocate a chunk of memory. */
111 void *glulx_malloc(glui32 len)
116 /* Resize a chunk of memory. This must follow ANSI rules: if the
117 size-change fails, this must return NULL, but the original chunk
118 must remain unchanged. */
119 void *glulx_realloc(void *ptr, glui32 len)
121 return realloc(ptr, len);
124 /* Deallocate a chunk of memory. */
125 void glulx_free(void *ptr)
130 /* Set the random-number seed; zero means use as random a source as
132 void glulx_setrandom(glui32 seed)
139 /* Return a random number in the range 0 to 2^32-1. */
140 glui32 glulx_random()
142 return (rand() << 24) ^ (rand() << 12) ^ rand();
147 #ifdef COMPILE_RANDOM_CODE
149 /* Here is a pretty standard random-number generator and seed function. */
150 static glui32 lo_random(void);
151 static void lo_seed_random(glui32 seed);
152 static glui32 rand_table[55]; /* State for the RNG. */
153 static int rand_index1, rand_index2;
155 static glui32 lo_random()
157 rand_index1 = (rand_index1 + 1) % 55;
158 rand_index2 = (rand_index2 + 1) % 55;
159 rand_table[rand_index1] = rand_table[rand_index1] - rand_table[rand_index2];
160 return rand_table[rand_index1];
163 static void lo_seed_random(glui32 seed)
168 rand_table[54] = seed;
172 for (i = 0; i < 55; i++) {
173 int ii = (21 * i) % 55;
176 seed = rand_table[ii];
178 for (loop = 0; loop < 4; loop++) {
179 for (i = 0; i < 55; i++)
180 rand_table[i] = rand_table[i] - rand_table[ (1 + i + 30) % 55];
184 #endif /* COMPILE_RANDOM_CODE */
188 /* I'm putting a wrapper for qsort() here, in case I ever have to
189 worry about a platform without it. But I am not worrying at
191 void glulx_sort(void *addr, int count, int size,
192 int (*comparefunc)(void *p1, void *p2))
194 qsort(addr, count, size, (int (*)(const void *, const void *))comparefunc);
200 #ifdef FLOAT_COMPILE_SAFER_POWF
202 /* This wrapper handles all special cases, even if the underlying
203 powf() function doesn't. */
204 gfloat32 glulx_powf(gfloat32 val1, gfloat32 val2)
208 else if ((val2 == 0.0f) || (val2 == -0.0f))
210 else if ((val1 == -1.0f) && isinf(val2))
212 return powf(val1, val2);
215 #else /* FLOAT_COMPILE_SAFER_POWF */
217 /* This is the standard powf() function, unaltered. */
218 gfloat32 glulx_powf(gfloat32 val1, gfloat32 val2)
220 return powf(val1, val2);
223 #endif /* FLOAT_COMPILE_SAFER_POWF */
225 #endif /* FLOAT_SUPPORT */