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