+/rtos

git clone https://git.julia.blue/+/rtos

Joseph RyanAdd variable mouse speedee76735

master
5.3 KiB264 linesraw
1#include "std.h"
2#include <stdbool.h>
3#include <stdint.h>
4
5uint32_t difference(uint32_t a, uint32_t b) {
6    return a < b ? b - a : a - b;
7}
8
9int32_t abs(int32_t n) {
10    return n < 0 ? -n : n;
11}
12
13float sin(float x) {
14    while (x > PI) { x -= 2 * PI; }
15    while (x < -PI) { x += 2 * PI; }
16    // Taylor series
17    float x3 = x * x * x;
18    float x5 = x3 * x * x;
19    float x7 = x3 * x * x;
20    float x9 = x3 * x * x;
21    return x - x3 / 6 + x5 / 120 - x7 / 5040 + x9 / 362880;
22}
23
24float cos(float x) {
25    while (x > PI) { x -= 2 * PI; }
26    while (x < -PI) { x += 2 * PI; }
27    // Taylor series
28    float x2 = x * x;
29    float x4 = x2 * x * x;
30    float x6 = x4 * x * x;
31    float x8 = x6 * x * x;
32    return 1 - x2 / 2 + x4 / 24 - x6 / 720 + x8 / 40320;
33}
34
35int32_t min(int32_t a, int32_t b) {
36    return a < b ? a : b;
37}
38
39int32_t max(int32_t a, int32_t b) {
40    return a > b ? a : b;
41}
42
43bool streq(const char* a, const char* b) {
44    while (*a == *b) {
45        if (!*a) {
46            break;
47        }
48        ++a, ++b;
49    }
50    return *a == *b;
51}
52
53int8_t strncmp(const char* str1, const char* str2, size_t num) {
54    for (size_t i = 0; i < num; i++) {
55        if (*str1 < *str2) {
56            return -1;
57        } else if (*str1 > *str2) {
58            return 1;
59        }
60        str1++;
61        str2++;
62    }
63    return 0;
64}
65
66char* strstr(char* str1, const char* str2) {
67    uint32_t len1 = strlen(str1);
68    uint32_t len2 = strlen(str2);
69
70    if (len1 < len2) {
71        return ((char*)0);
72    }
73
74    for (uint32_t i = 0; i < len1 - len2 + 1; i++) {
75        if (strncmp(str1, str2, len2) == 0) {
76            return str1;
77        }
78        str1++;
79    }
80    return ((char*)0);
81}
82
83void memset(void* dest, uint8_t value, uint32_t n) {
84    uint8_t* temp = (uint8_t*)dest;
85    while (n--) { *temp++ = value; }
86}
87
88int32_t memcmp(const void* s1, const void* s2, uint32_t n) {
89    uint8_t* a = (uint8_t*)s1;
90    uint8_t* b = (uint8_t*)s2;
91
92    for (uint32_t i = 0; i < n; i++) {
93        if (a[i] < b[i]) {
94            return -1;
95        } else if (a[i] > b[i]) {
96            return 1;
97        }
98    }
99    return 0;
100}
101
102uint16_t strlen(const char* s) {
103    uint16_t n = 0;
104    while (*s++) { n++; }
105    return n;
106}
107
108char* strcpy(char* dest, const char* src) {
109    while (*src) { *dest++ = *src++; }
110    *dest = '\0';
111    return dest;
112}
113
114char* strchr(const char* str, int32_t c) {
115    int16_t ret = find(str, c);
116    if (ret > -1) {
117        return (char*)(str + ret);
118    }
119    return 0;
120}
121
122size_t strspn(const char* str1, const char* str2) {
123    size_t n = 0;
124    while (*str1++) {
125        if (find(str2, *str1) == -1) {
126            return n;
127        }
128        n++;
129    }
130    return n;
131}
132
133size_t strcspn(const char* str1, const char* str2) {
134    size_t n = 0;
135    while (*str1++) {
136        if (find(str2, *str1) != -1) {
137            return n;
138        }
139        n++;
140    }
141    return n;
142}
143
144int16_t find(const char* s, char c) {
145    for (int i = 0; *s; ++i) {
146        if (c == *s++) {
147            return i;
148        }
149    }
150    return -1;
151}
152
153bool is_whitespace(char c) {
154    switch (c) {
155    case ' ':
156    case '\t':
157    case '\r':
158    case '\n': return true;
159    default: return false;
160    }
161}
162
163bool is_numeric_char(const char c) {
164    return c >= '0' && c <= '9';
165}
166
167bool is_numeric_str(const char* c) {
168    c += c[0] == '-' || c[0] == '+';
169    while (*c) {
170        if (!is_numeric_char(*c++)) {
171            return false;
172        }
173    }
174    return true;
175}
176
177int32_t atoi(char* s) {
178    bool neg = false;
179    uint8_t i = 0;
180    switch (s[i]) {
181    case '-': neg = true;
182    case '+': ++i; break;
183    }
184    int32_t result = 0;
185    for (; s[i] && i < 11; ++i) { // TODO: error on overflowing 32 bits
186        if (!is_numeric(s[i])) {
187            return 0;
188        }
189        result *= 10;
190        result += s[i] - '0';
191    }
192    return neg ? -result : result;
193}
194
195char* reverse(char* s) {
196    uint16_t len = strlen(s);
197    for (int i = 0; i < len / 2; ++i) {
198        char temp = s[i];
199        s[i] = s[len - 1 - i];
200        s[len - 1 - i] = temp;
201    }
202    return s;
203}
204
205char* itoa(int32_t value, char* s, uint8_t radix) {
206    uint8_t i = 0;
207    bool neg = false;
208    if (value < 0) {
209        neg = true;
210        value = -value;
211    }
212    do {
213        char current = value % radix;
214        if (current < 10) {
215            current += '0';
216        } else {
217            current += 'a' - 10; // TODO: case
218        }
219        s[i++] = current;
220        value /= radix;
221    } while (value);
222    if (neg) {
223        s[i++] = '-';
224    }
225    s[i] = '\0';
226    return reverse(s);
227}
228
229void memcpy(void* dest, const void* src, uint32_t n) {
230    uint32_t* wide_a = dest;
231    const uint32_t* wide_b = src;
232    if (!((uint32_t)src % 4 || (uint32_t)dest % 4)) {
233        while (n >= 4) {
234            *wide_a++ = *wide_b++;
235            n -= 4;
236        }
237    }
238    uint8_t* a = (uint8_t*)wide_a;
239    const uint8_t* b = (const uint8_t*)wide_b;
240    while (n--) { *a++ = *b++; }
241}
242
243// Compiler generated intrinsics
244
245void __aeabi_memclr4(void* mem, size_t bytes) {
246    uint32_t* buf = (uint32_t*)mem;
247    while (bytes > 0) {
248        *buf = 0;
249        ++buf;
250        bytes -= 4;
251    }
252}
253
254void __aeabi_memcpy4(void* dest, const void* src, uint32_t n) {
255    uint32_t* a = dest;
256    const uint32_t* b = src;
257    while (n > 0) {
258        *a++ = *b++;
259        n -= 4;
260    }
261}
262
263__attribute__((alias("memcpy"))) void
264__aeabi_memcpy(void* dest, const void* src, uint32_t n);