blob: 4801154123bf4961a215b370d72190a30ab01cf2 (
plain)
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
|
#include <string.h>
#include <stdint.h>
void* memcpy(void* dst, const void* src, size_t count) {
// TODO: Can probably use better implementations based on hw instruction set.
uint32_t* dst32 = (uint32_t*)dst;
const uint32_t* src32 = (uint32_t*)src;
for(; count >= 4; count -= 4) {
*dst32++ = *src32++;
}
uint8_t* dst8 = (uint8_t*)dst32;
const uint8_t* src8 = (uint8_t*)src32;
for(; count != 0; count--) {
*dst8++ = *src8++;
}
return dst8;
}
static int count_digits_u(unsigned int x) {
int count = 0;
for(; x != 0; x /= 10, count++);
return count;
}
char* utoa(unsigned int x, char* buffer, size_t size) {
if (size > 0) {
const int num_digits = count_digits_u(x);
size_t i = 0;
while ((x != 0) && ((i+1) < size)) {
const unsigned int digit = x % 10;
x /= 10;
buffer[num_digits-1 - i++] = '0' + digit;
}
buffer[i] = 0;
}
return buffer;
}
char* ptoa(const void* ptr, char* buffer, size_t size) {
if (size > 2) {
size_t i = 0;
buffer[i++] = '0';
buffer[i++] = 'x';
uintptr_t x = (uintptr_t)ptr;
const int num_digits = count_digits_u(x);
while ((x != 0) && (i < size)) {
const unsigned int digit = x % 16;
x /= 16;
const char hex = (digit < 10) ? ('0' + digit) : ('a' + (digit-10));
buffer[num_digits-1+2 - i++] = hex;
}
buffer[i] = 0;
} else if (size > 0) {
buffer[0] = 0;
}
return buffer;
}
|