-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathcommon.c
66 lines (57 loc) · 1.36 KB
/
common.c
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
#include "common.h"
#if ACT_AS_ROMCODE
unsigned dl_crc16(const void *buf, unsigned len) {
uint8_t *s = (uint8_t*)buf; int i;
unsigned crc = 0;
while (len--) {
crc ^= *s++ << 8;
for (i = 0; i < 8; i++)
crc = crc << 1 ^ ((0 - (crc >> 15)) & 0x11021);
}
return crc;
}
#else
unsigned dl_fastchk16(const uint16_t *src, unsigned len) {
uint32_t sum = 0;
while (len > 3) {
sum += *src++;
sum += *src++;
len -= 4;
}
if (len & 2) sum += *src++;
if (len & 1) sum += *(const uint8_t*)src;
sum = (sum >> 16) + (sum & 0xffff);
sum += sum >> 16;
return ~sum & 0xffff;
}
#endif
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
uint16_t swap_be16(uint16_t v) {
return v >> 8 | v << 8;
}
uint32_t swap_be32(uint32_t v) {
uint32_t t = v >> 24 | v << 24, m = 0xff00;
return t | (v >> 8 & m) | (v & m) << 8;
}
#endif
void *memcpy(void *dst, const void *src, size_t len) {
uint8_t *d = (uint8_t*)dst;
const uint8_t *s = (const uint8_t*)src;
/* copy aligned data faster */
if (!(((uintptr_t)s | (uintptr_t)d) & 3)) {
unsigned len4 = len >> 2;
uint32_t *d4 = (uint32_t*)d;
const uint32_t *s4 = (const uint32_t*)s;
while (len4--) *d4++ = *s4++;
len &= 3;
d = (uint8_t*)d4;
s = (const uint8_t*)s4;
}
while (len--) *d++ = *s++;
return dst;
}
void *memset(void *dst, int c, size_t len) {
uint8_t *d = (uint8_t*)dst;
while (len--) *d++ = c;
return dst;
}