-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.c
80 lines (67 loc) · 1.58 KB
/
math.c
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdint.h>
#include "game.h"
int round(float f){
return f + (f >= 0 ? 0.5 : -0.5);
}
float min(float a, float b){
return a < b ? a : b;
}
float max(float a, float b){
return a > b ? a : b;
}
// Returns f if within min/max boundraries, else returns the broken boundrary.
float bound(float min, float f, float max){
return (f < min ? min : (f > max ? max : f));
}
float avg(float a, float b){
return (a+b)/2;
}
float abs(float f){
return f >= 0 ? f : -f;
}
// Returns 1/0/-1 based on sign of f.
int sign(float f){
return f > 0 ? 1 : (f < 0 ? -1 : 0);
}
// Like XOR, but for positive/negative integers instead of 1/0.
// Returns 1 or -1.
int ixor(float a, float b){
return ((sign(a)>=0 && sign(b)<0) || (sign(a)<0 && sign(b)>=0)) ? 1 : -1;
}
int floorMod(int d, int m){
int mod = d % m;
if(m >= 0) return mod >= 0 ? mod : mod+m;
else return mod < 0 ? mod : mod+m;
}
float pow(float a, int b){
if(b < 0) return 0;
if(b == 0) return 1;
if(b == 1) return a;
int i;
float c = a;
for(i = 1; i < b; i++)
c *= a;
return c;
}
// Pseudo-random generator
uint32_t seed = 0;
// Returns 32 pseudo-random bits.
uint32_t xorshifter(){
if(!seed) seed = tick_start;
int i;
for(i = 0; i < 64; i++){
seed ^= seed >> 16;
seed ^= seed << 25;
seed ^= seed >> 7;
}
return seed;
}
// Returns pseudo-random float value in range [0, 1).
float random(){
uint32_t r = xorshifter();
int i;
float f = 0;
for(i = 1; i < 16; i++)
f += ((r >> i) & 1) / pow(2, i);
return f;
}