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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
/* util.h & time.h */
#ifndef UTIL_H_
#define UTIL_H_
/* these things that are highly portable among programs and thus written in a modular style */
#include <math.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdint.h>
#define always_inline static inline __attribute__((always_inline))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define CLAMP(a,b,c) (a)<(b)?(b):(a)>(c)?(c):(a)
always_inline void Root(char * filename) {
char path[PATH_MAX], * terminator;
if (!realpath(filename, path)) { return; }
if ((terminator = strrchr(path, '/'))) {
*terminator = '\0';
if(chdir(path)) { abort(); }
}
}
always_inline float Angle(float a_x, float a_y, float b_x, float b_y) {
return atan2f(b_y - a_y, b_x - a_x);
}
always_inline float Distance(float a_x, float a_y, float b_x, float b_y) {
return sqrtf(powf(b_x - a_x, 2) + powf(b_y - a_y, 2));
}
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef float f32;
typedef double f64;
#endif
/* time.h */
#ifndef TIME_H_
#define TIME_H_
#include <time.h>
#define TIMESPEC_TO_F64(ts) ((double)(ts).tv_sec + ((double)(ts).tv_nsec / TIMESPEC_HZ))
enum { TIMESPEC_HZ = 1000000000 };
typedef struct timespec timespec_t;
static const timespec_t one_second = {1, 0}, zero_seconds = {0, 0};
always_inline timespec_t timespec_add(timespec_t a, timespec_t b) {
a.tv_sec += b.tv_sec;
a.tv_nsec += b.tv_nsec;
if (a.tv_nsec >= 1000000000) {
a.tv_sec++;
a.tv_nsec -= 1000000000;
}
return a;
}
always_inline timespec_t timespec_sub(timespec_t a, timespec_t b) {
a.tv_sec -= b.tv_sec;
a.tv_nsec -= b.tv_nsec;
if (a.tv_nsec < 0) {
a.tv_sec--;
a.tv_nsec += 1000000000;
}
return a;
}
always_inline int timespec_cmp(timespec_t a, timespec_t b) {
return a.tv_sec > b.tv_sec ?
(1) :
a.tv_sec < b.tv_sec ?
(-1) :
(
a.tv_nsec > b.tv_nsec ?
(1) :
a.tv_nsec < b.tv_nsec ?
(-1) : 0
);
}
always_inline timespec_t timespec_max(timespec_t a, timespec_t b) {
return a.tv_sec > b.tv_sec ?
a :
a.tv_sec < b.tv_sec ?
b :
(
a.tv_nsec > b.tv_nsec ?
a : b
);
}
always_inline timespec_t timespec_min(timespec_t a, timespec_t b) {
return a.tv_sec < b.tv_sec ?
a :
a.tv_sec > b.tv_sec ?
b :
(
a.tv_nsec < b.tv_nsec ?
a : b
);
}
#endif /* TIME_H_ */
|