-
Notifications
You must be signed in to change notification settings - Fork 6
/
WMath.cpp
50 lines (40 loc) · 955 Bytes
/
WMath.cpp
1
2
3
4
5
6
7
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdint.h>
static uint32_t seed;
void randomSeed(uint32_t newseed)
{
if (newseed > 0) seed = newseed;
}
void srandom(uint32_t newseed)
{
seed = newseed;
}
uint32_t random(void)
{
int32_t hi, lo, x;
// the algorithm used in avr-libc 1.6.4
x = seed;
if (x == 0) x = 123459876;
hi = x / 127773;
lo = x % 127773;
x = 16807 * lo - 2836 * hi;
if (x < 0) x += 0x7FFFFFFF;
seed = x;
return x;
}
uint32_t random(uint32_t howbig)
{
if (howbig == 0) return 0;
return random() % howbig;
}
int32_t random(int32_t howsmall, int32_t howbig)
{
if (howsmall >= howbig) return howsmall;
int32_t diff = howbig - howsmall;
return random(diff) + howsmall;
}
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
unsigned int makeWord(unsigned int w) { return w; }
unsigned int makeWord(unsigned char h, unsigned char l) { return (h << 8) | l; }