-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.h
More file actions
147 lines (133 loc) · 3.17 KB
/
math.h
File metadata and controls
147 lines (133 loc) · 3.17 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// math.h clone - SimpleC
#ifndef SIMPLEC_MATH_H
#define SIMPLEC_MATH_H
static inline int pow(int base, int exp) {
int result = 1;
while (exp--) result *= base;
return result;
}
static inline int abs(int x) { return x < 0 ? -x : x; }
static inline int max(int a, int b) { return a > b ? a : b; }
static inline int min(int a, int b) { return a < b ? a : b; }
static inline int clamp(int x, int lo, int hi) {
return max(lo, min(x, hi));
}
static inline float sqrt(float x) {
float result;
#if defined(__i386__)
// Use x87 FPU instructions for 32-bit
asm volatile (
"flds %[input]\n"
"fsqrt\n"
"fstps %[output]\n"
: [output] "=m" (result)
: [input] "m" (x)
);
#elif defined(__x86_64__)
// Use SSE2 instructions for 64-bit
asm volatile (
"sqrtss %[input], %[output]\n"
: [output] "=x" (result)
: [input] "x" (x)
);
#else
#error "Unsupported architecture for sqrt."
#endif
return result;
}
static inline double floor(double x) {
double result;
#if defined(__x86_64__)
asm volatile (
"roundsd $1, %[input], %[output]\n" // round down
: [output] "=x"(result)
: [input] "x"(x)
);
#elif defined(__i386__)
asm volatile (
"fldl %[input]\n"
"frndint\n"
"fstpl %[output]\n"
: [output] "=m"(result)
: [input] "m"(x)
);
#else
#error "Unsupported architecture for floor."
#endif
return result;
}
static inline double ceil(double x) {
double result;
#if defined(__x86_64__)
asm volatile (
"roundsd $2, %[input], %[output]\n" // round up
: [output] "=x"(result)
: [input] "x"(x)
);
#elif defined(__i386__)
asm volatile (
"fldl %[input]\n"
"fchs\n"
"frndint\n"
"fchs\n"
"fstpl %[output]\n"
: [output] "=m"(result)
: [input] "m"(x)
);
#else
#error "Unsupported architecture for ceil."
#endif
return result;
}
static inline double round(double x) {
double result;
#if defined(__x86_64__)
asm volatile (
"roundsd $0, %[input], %[output]\n" // round to nearest
: [output] "=x"(result)
: [input] "x"(x)
);
#elif defined(__i386__)
asm volatile (
"fldl %[input]\n"
"fldl %[half]\n"
"faddp %%st(1), %%st(0)\n"
"frndint\n"
"fstpl %[output]\n"
: [output] "=m"(result)
: [input] "m"(x), [half] "m"(0.5)
);
#else
#error "Unsupported architecture for round."
#endif
return result;
}
static inline double sin(double x) {
double x2 = x * x;
double term = x;
double result = term;
term *= -x2 / (2 * 3);
result += term;
term *= -x2 / (4 * 5);
result += term;
term *= -x2 / (6 * 7);
result += term;
term *= -x2 / (8 * 9);
result += term;
return result;
}
static inline double cos(double x) {
double x2 = x * x;
double term = 1.0;
double result = term;
term *= -x2 / (1 * 2);
result += term;
term *= -x2 / (3 * 4);
result += term;
term *= -x2 / (5 * 6);
result += term;
term *= -x2 / (7 * 8);
result += term;
return result;
}
#endif // SIMPLEC_MATH_H