2019-06-25 01:25:30 +08:00
|
|
|
// **** libc ****
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2020-06-03 05:21:41 +08:00
|
|
|
void delay(uint32_t a) {
|
|
|
|
volatile uint32_t i;
|
2019-06-25 01:25:30 +08:00
|
|
|
for (i = 0; i < a; i++);
|
2017-07-13 02:25:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *memset(void *str, int c, unsigned int n) {
|
2019-07-03 08:43:44 +08:00
|
|
|
uint8_t *s = str;
|
|
|
|
for (unsigned int i = 0; i < n; i++) {
|
|
|
|
*s = c;
|
|
|
|
s++;
|
2017-07-13 02:25:10 +08:00
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2021-11-05 07:06:51 +08:00
|
|
|
#define UNALIGNED(X, Y) \
|
|
|
|
(((uint32_t)(X) & (sizeof(uint32_t) - 1U)) | ((uint32_t)(Y) & (sizeof(uint32_t) - 1U)))
|
|
|
|
|
|
|
|
void *memcpy(void *dest, const void *src, unsigned int len) {
|
|
|
|
unsigned int n = len;
|
|
|
|
uint8_t *d8 = dest;
|
|
|
|
const uint8_t *s8 = src;
|
|
|
|
|
|
|
|
if ((n >= 4U) && !UNALIGNED(s8, d8)) {
|
|
|
|
uint32_t *d32 = (uint32_t *)d8; // cppcheck-suppress misra-c2012-11.3 ; already checked that it's properly aligned
|
|
|
|
const uint32_t *s32 = (const uint32_t *)s8; // cppcheck-suppress misra-c2012-11.3 ; already checked that it's properly aligned
|
|
|
|
|
|
|
|
while(n >= 16U) {
|
|
|
|
*d32 = *s32; d32++; s32++;
|
|
|
|
*d32 = *s32; d32++; s32++;
|
|
|
|
*d32 = *s32; d32++; s32++;
|
|
|
|
*d32 = *s32; d32++; s32++;
|
|
|
|
n -= 16U;
|
|
|
|
}
|
|
|
|
|
|
|
|
while(n >= 4U) {
|
|
|
|
*d32 = *s32; d32++; s32++;
|
|
|
|
n -= 4U;
|
|
|
|
}
|
|
|
|
|
|
|
|
d8 = (uint8_t *)d32;
|
|
|
|
s8 = (const uint8_t *)s32;
|
|
|
|
}
|
|
|
|
while (n-- > 0U) {
|
|
|
|
*d8 = *s8; d8++; s8++;
|
2017-07-13 02:25:10 +08:00
|
|
|
}
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
int memcmp(const void * ptr1, const void * ptr2, unsigned int num) {
|
2019-06-27 05:47:06 +08:00
|
|
|
int ret = 0;
|
2019-07-03 08:43:44 +08:00
|
|
|
const uint8_t *p1 = ptr1;
|
|
|
|
const uint8_t *p2 = ptr2;
|
|
|
|
for (unsigned int i = 0; i < num; i++) {
|
|
|
|
if (*p1 != *p2) {
|
2019-06-27 05:47:06 +08:00
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
2019-07-03 08:43:44 +08:00
|
|
|
p1++;
|
|
|
|
p2++;
|
2017-07-13 02:25:10 +08:00
|
|
|
}
|
2019-06-27 05:47:06 +08:00
|
|
|
return ret;
|
2017-07-13 02:25:10 +08:00
|
|
|
}
|