FreeCalypso > hg > ffs-editor
comparison src/libsys/rand.c @ 0:92470e5d0b9e
src: partial import from FC Selenite
| author | Mychaela Falconia <falcon@freecalypso.org> |
|---|---|
| date | Fri, 15 May 2020 01:28:16 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:92470e5d0b9e |
|---|---|
| 1 /* | |
| 2 * This version of rand() has been lifted from Ancient UNIX, and modified | |
| 3 * to match the version used in TI's TCS211 GSM firmware, as revealed by | |
| 4 * disassembly of rand.obj in the rts16le_flash.lib binary library used | |
| 5 * by that semi-src package. TI's version (most likely from their compiler | |
| 6 * tools group, rather than the GSM fw group, but who knows) uses the | |
| 7 * same trivial implementation of rand() as the original Ancient UNIX libc, | |
| 8 * but with one change: TI's return value is right-shifted by 16 bits | |
| 9 * compared to what the Ancient UNIX rand() would have returned. | |
| 10 * The caller thus gets back only 15 pseudo-random bits rather than 31, | |
| 11 * but then the lower bits of the original rand() return value are | |
| 12 * known to be pretty bad. | |
| 13 * | |
| 14 * rand() is used by some FFS code in reclaim.c. If we don't provide our | |
| 15 * own version of rand() and let the linker pull the version from newlib, | |
| 16 * the link fails because the latter uses malloc. This ancient implementation | |
| 17 * of rand() is quite poor, but my plan is to look into possibly adopting | |
| 18 * some better PRNG after we get the basic TI GSM firmware reintegrated. | |
| 19 */ | |
| 20 | |
| 21 static long randx = 1; | |
| 22 | |
| 23 srand(x) | |
| 24 unsigned x; | |
| 25 { | |
| 26 randx = x; | |
| 27 } | |
| 28 | |
| 29 rand() | |
| 30 { | |
| 31 return ((randx = randx * 1103515245 + 12345) & 0x7fffffff) >> 16; | |
| 32 } |
