first commit

This commit is contained in:
2025-07-03 23:58:20 +08:00
commit ce2b3cdfd4
444 changed files with 65256 additions and 0 deletions

View File

@ -0,0 +1,44 @@
#===============================================================================
# @brief cmake file
# Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2022-2023. All rights reserved.
#===============================================================================
set(COMPONENT_NAME "common_boot_libc")
set(SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/string/memset.c
${CMAKE_CURRENT_SOURCE_DIR}/src/string/memcpy.c
${CMAKE_CURRENT_SOURCE_DIR}/src/string/memcmp.c
${CMAKE_CURRENT_SOURCE_DIR}/src/string/strcmp.c
${CMAKE_CURRENT_SOURCE_DIR}/src/string/strlen.c
${CMAKE_CURRENT_SOURCE_DIR}/src/string/memmove.c
)
set(PUBLIC_HEADER
${CMAKE_CURRENT_SOURCE_DIR}/include
)
set(PRIVATE_HEADER
)
set(PRIVATE_DEFINES
)
set(PUBLIC_DEFINES
)
# use this when you want to add ccflags like -include xxx
set(COMPONENT_PUBLIC_CCFLAGS
)
set(COMPONENT_CCFLAGS
)
set(WHOLE_LINK
true
)
set(MAIN_COMPONENT
false
)
build_component()

View File

@ -0,0 +1,22 @@
/*
* Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2012-2019. All rights reserved.
* Description: Boot string function implementation.
*
* Create: 2012-12-22
*/
/**
* memcmp - Compare two areas of memory
* @cs: One area of memory
* @ct: Another area of memory
* @count: The size of the area.
*/
int memcmp(const void *cs, const void *ct, unsigned int count)
{
unsigned int cnt = count;
const unsigned char *l = cs;
const unsigned char *r = ct;
for (; (cnt != 0) && (*l == *r); cnt--, l++, r++) {
};
return (cnt != 0) ? *l - *r : 0;
}

View File

@ -0,0 +1,183 @@
#include <string.h>
#include <stdint.h>
#include <endian.h>
#ifdef __GNUC__
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define LS >>
#define RS <<
#else
#define LS <<
#define RS >>
#endif
#define MEMCPY_BYTE_BITS 8
#define MEMCPY_ALIGH_UNIT_BYTES_4 4
#define MEMCPY_FAST_COPY_UNIT_NUM_4 4
#define MEMCPY_FAST_COPY_UNIT_BYTES (MEMCPY_ALIGH_UNIT_BYTES_4 * MEMCPY_FAST_COPY_UNIT_NUM_4)
#define MEMCPY_ALIGH_UNIT_BITS (MEMCPY_BYTE_BITS * MEMCPY_ALIGH_UNIT_BYTES_4)
#define MEMCPY_FAST_COPY_OFFSET_UNIT_0 (MEMCPY_ALIGH_UNIT_BYTES_4 * 0)
#define MEMCPY_FAST_COPY_OFFSET_UNIT_1 (MEMCPY_ALIGH_UNIT_BYTES_4 * 1)
#define MEMCPY_FAST_COPY_OFFSET_UNIT_2 (MEMCPY_ALIGH_UNIT_BYTES_4 * 2)
#define MEMCPY_FAST_COPY_OFFSET_UNIT_3 (MEMCPY_ALIGH_UNIT_BYTES_4 * 3)
#define MEMCPY_NOT_ALIGN_FAST_COPY_THRESHOLD (MEMCPY_FAST_COPY_UNIT_BYTES * 2)
#define MEMCPY_OFFSET_BYTES_1 1
#define MEMCPY_ALIGH_OFFSET_BYTES_1 (MEMCPY_ALIGH_UNIT_BYTES_4 - MEMCPY_OFFSET_BYTES_1)
#define MEMCPY_OFFSET_BITS_1 (MEMCPY_BYTE_BITS * MEMCPY_OFFSET_BYTES_1)
#define MEMCPY_OFFSET_ALIGN_BITS_1 (MEMCPY_BYTE_BITS * MEMCPY_ALIGH_OFFSET_BYTES_1)
#define MEMCPY_OFFSET_BYTES_2 2
#define MEMCPY_ALIGH_OFFSET_BYTES_2 (MEMCPY_ALIGH_UNIT_BYTES_4 - MEMCPY_OFFSET_BYTES_2)
#define MEMCPY_OFFSET_BITS_2 (MEMCPY_BYTE_BITS * MEMCPY_OFFSET_BYTES_2)
#define MEMCPY_OFFSET_ALIGN_BITS_2 (MEMCPY_BYTE_BITS * MEMCPY_ALIGH_OFFSET_BYTES_2)
#define MEMCPY_OFFSET_BYTES_3 3
#define MEMCPY_ALIGH_OFFSET_BYTES_3 (MEMCPY_ALIGH_UNIT_BYTES_4 - MEMCPY_OFFSET_BYTES_3)
#define MEMCPY_OFFSET_BITS_3 (MEMCPY_BYTE_BITS * MEMCPY_OFFSET_BYTES_3)
#define MEMCPY_OFFSET_ALIGN_BITS_3 (MEMCPY_BYTE_BITS * MEMCPY_ALIGH_OFFSET_BYTES_3)
#define MEMCPY_BYTE_CHECK_NUM_1 0x01
#define MEMCPY_BYTE_CHECK_NUM_2 0x02
#define MEMCPY_BYTE_CHECK_NUM_4 0x04
#define MEMCPY_BYTE_CHECK_NUM_8 0x08
#define MEMCPY_BYTE_CHECK_NUM_16 0x10
#endif
void *memcpy(void *restrict dest, const void *restrict src, size_t num)
{
unsigned char *d = dest;
const unsigned char *s = src;
size_t n = num;
#ifdef __GNUC__
typedef uint32_t __attribute__((__may_alias__)) u32;
uint32_t w, x;
for (; (uintptr_t)s % MEMCPY_ALIGH_UNIT_BYTES_4 && n; n--) {
*d++ = *s++;
}
if ((uintptr_t)d % MEMCPY_ALIGH_UNIT_BYTES_4 == 0) {
for (; n >= MEMCPY_FAST_COPY_UNIT_BYTES; s += MEMCPY_FAST_COPY_UNIT_BYTES,
d += MEMCPY_FAST_COPY_UNIT_BYTES, n -= MEMCPY_FAST_COPY_UNIT_BYTES) {
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_0) = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_0);
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_1) = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_1);
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_2) = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_2);
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_3) = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_3);
}
if (n & MEMCPY_FAST_COPY_OFFSET_UNIT_2) {
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_0) = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_0);
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_1) = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_1);
d += MEMCPY_FAST_COPY_OFFSET_UNIT_2;
s += MEMCPY_FAST_COPY_OFFSET_UNIT_2;
}
if (n & MEMCPY_FAST_COPY_OFFSET_UNIT_1) {
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_0) = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_0);
d += MEMCPY_FAST_COPY_OFFSET_UNIT_1;
s += MEMCPY_FAST_COPY_OFFSET_UNIT_1;
}
if (n & MEMCPY_BYTE_CHECK_NUM_2) {
*d++ = *s++; *d++ = *s++;
}
if (n & MEMCPY_BYTE_CHECK_NUM_1) {
*d = *s;
}
return dest;
}
if (n >= MEMCPY_NOT_ALIGN_FAST_COPY_THRESHOLD) {
switch ((uintptr_t)d % MEMCPY_ALIGH_UNIT_BYTES_4) {
case MEMCPY_OFFSET_BYTES_1:
w = *(u32 *)s;
*d++ = *s++;
*d++ = *s++;
*d++ = *s++;
n -= MEMCPY_ALIGH_OFFSET_BYTES_1;
for (; n >= MEMCPY_FAST_COPY_UNIT_BYTES + MEMCPY_OFFSET_BYTES_1; s += MEMCPY_FAST_COPY_UNIT_BYTES,
d += MEMCPY_FAST_COPY_UNIT_BYTES, n -= MEMCPY_FAST_COPY_UNIT_BYTES) {
x = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_0 + MEMCPY_OFFSET_BYTES_1);
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_0) =
(w LS MEMCPY_OFFSET_ALIGN_BITS_1) | (x RS MEMCPY_OFFSET_BITS_1);
w = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_1 + MEMCPY_OFFSET_BYTES_1);
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_1) =
(x LS MEMCPY_OFFSET_ALIGN_BITS_1) | (w RS MEMCPY_OFFSET_BITS_1);
x = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_2 + MEMCPY_OFFSET_BYTES_1);
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_2) =
(w LS MEMCPY_OFFSET_ALIGN_BITS_1) | (x RS MEMCPY_OFFSET_BITS_1);
w = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_3 + MEMCPY_OFFSET_BYTES_1);
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_3) =
(x LS MEMCPY_OFFSET_ALIGN_BITS_1) | (w RS MEMCPY_OFFSET_BITS_1);
}
break;
case MEMCPY_OFFSET_BYTES_2:
w = *(u32 *)s;
*d++ = *s++;
*d++ = *s++;
n -= MEMCPY_ALIGH_OFFSET_BYTES_2;
for (; n >= MEMCPY_FAST_COPY_UNIT_BYTES + MEMCPY_OFFSET_BYTES_2; s += MEMCPY_FAST_COPY_UNIT_BYTES,
d += MEMCPY_FAST_COPY_UNIT_BYTES, n -= MEMCPY_FAST_COPY_UNIT_BYTES) {
x = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_0 + MEMCPY_OFFSET_BYTES_2);
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_0) =
(w LS MEMCPY_OFFSET_ALIGN_BITS_2) | (x RS MEMCPY_OFFSET_BITS_2);
w = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_1 + MEMCPY_OFFSET_BYTES_2);
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_1) =
(x LS MEMCPY_OFFSET_ALIGN_BITS_2) | (w RS MEMCPY_OFFSET_BITS_2);
x = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_2 + MEMCPY_OFFSET_BYTES_2);
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_2) =
(w LS MEMCPY_OFFSET_ALIGN_BITS_2) | (x RS MEMCPY_OFFSET_BITS_2);
w = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_3 + MEMCPY_OFFSET_BYTES_2);
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_3) =
(x LS MEMCPY_OFFSET_ALIGN_BITS_2) | (w RS MEMCPY_OFFSET_BITS_2);
}
break;
case MEMCPY_OFFSET_BYTES_3:
w = *(u32 *)s;
*d++ = *s++;
n -= MEMCPY_ALIGH_OFFSET_BYTES_3;
for (; n >= MEMCPY_FAST_COPY_UNIT_BYTES + MEMCPY_OFFSET_BYTES_3; s += MEMCPY_FAST_COPY_UNIT_BYTES,
d += MEMCPY_FAST_COPY_UNIT_BYTES, n -= MEMCPY_FAST_COPY_UNIT_BYTES) {
x = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_0 + MEMCPY_OFFSET_BYTES_3);
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_0) =
(w LS MEMCPY_OFFSET_ALIGN_BITS_3) | (x RS MEMCPY_OFFSET_BITS_3);
w = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_1 + MEMCPY_OFFSET_BYTES_3);
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_1) =
(x LS MEMCPY_OFFSET_ALIGN_BITS_3) | (w RS MEMCPY_OFFSET_BITS_3);
x = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_2 + MEMCPY_OFFSET_BYTES_3);
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_2) =
(w LS MEMCPY_OFFSET_ALIGN_BITS_3) | (x RS MEMCPY_OFFSET_BITS_3);
w = *(u32 *)(s + MEMCPY_FAST_COPY_OFFSET_UNIT_3 + MEMCPY_OFFSET_BYTES_3);
*(u32 *)(d + MEMCPY_FAST_COPY_OFFSET_UNIT_3) =
(x LS MEMCPY_OFFSET_ALIGN_BITS_3) | (w RS MEMCPY_OFFSET_BITS_3);
}
break;
}
}
if (n & MEMCPY_BYTE_CHECK_NUM_16) {
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
}
if (n & MEMCPY_BYTE_CHECK_NUM_8) {
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
}
if (n & MEMCPY_BYTE_CHECK_NUM_4) {
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
}
if (n & MEMCPY_BYTE_CHECK_NUM_2) {
*d++ = *s++; *d++ = *s++;
}
if (n & MEMCPY_BYTE_CHECK_NUM_1) {
*d = *s;
}
return dest;
#endif
for (; n; n--) *d++ = *s++;
return dest;
}

View File

@ -0,0 +1,60 @@
#include <string.h>
#include <stdint.h>
#ifdef __GNUC__
typedef __attribute__((__may_alias__)) size_t WT;
#define WS (sizeof(WT))
#endif
void *memmove(void *dest, const void *src, size_t size)
{
char *d = dest;
const char *s = src;
if (d == s) {
return d;
}
if ((uintptr_t)s-(uintptr_t)d-size <= -2*size) {
return memcpy(d, s, size);
}
if (d < s) {
#ifdef __GNUC__
if ((uintptr_t)s % WS == (uintptr_t)d % WS) {
while ((uintptr_t)d % WS) {
if (!size--) {
return dest;
}
*d++ = *s++;
}
for (; size>=WS; size-=WS, d+=WS, s+=WS) {
*(WT *)d = *(WT *)s;
}
}
#endif
for (; size; size--) {
*d++ = *s++;
}
} else {
#ifdef __GNUC__
if ((uintptr_t)s % WS == (uintptr_t)d % WS) {
while ((uintptr_t)(d+size) % WS) {
if (!size--) {
return dest;
}
d[size] = s[size];
}
while (size>=WS) {
size-=WS, *(WT *)(d+size) = *(WT *)(s+size);
}
}
#endif
while (size) {
size--, d[size] = s[size];
}
}
return dest;
}

View File

@ -0,0 +1,99 @@
#include <string.h>
#include <stdint.h>
#if defined(__LITEOS__) && defined(LOSCFG_BASE_MEM_NODE_SIZE_CHECK)
#include "los_memory.h"
static int __memset_check(void *dest, unsigned int nodeLength)
{
unsigned int ret;
unsigned int totalSize = 0;
unsigned int availSize = 0;
unsigned char *pool = m_aucSysMem1;
ret = LOS_MemNodeSizeCheck(pool, dest, &totalSize, &availSize);
if ((ret == 0) && (nodeLength > availSize)) {
return -1;
}
return 0;
}
#endif
void *memset(void *dest, int c, size_t n)
{
typedef uint32_t __attribute__((__may_alias__)) u32;
typedef uint64_t __attribute__((__may_alias__)) u64;
unsigned char *s = dest;
if (!n) {
return dest;
}
s[0] = c;
s[n-1] = c;
if (n <= 2) { // 2 bytes mem
return dest;
}
s[1] = c;
s[2] = c;
s[n-2] = c;
s[n-3] = c;
if (n <= 6) { // 6 bytes mem
return dest;
}
s[3] = c;
s[n-4] = c;
if (n <= 8) { // 8 bytes mem
return dest;
}
size_t k = -(uintptr_t)s & 3; // 3 : 4-byte boundary process
s += k;
n -= k;
n &= -4;
u32 c32 = ((u32)-1) / 255 * (unsigned char)c;
*(u32 *)(s + 0) = c32;
*(u32 *)(s + n - 4) = c32;
if (n <= 8) {
return dest;
}
*(u32 *)(s + 4) = c32;
*(u32 *)(s + 8) = c32;
*(u32 *)(s + n - 12) = c32;
*(u32 *)(s + n - 8) = c32;
if (n <= 24) { // 24 bytes mem
return dest;
}
*(u32 *)(s + 12) = c32;
*(u32 *)(s + 16) = c32;
*(u32 *)(s + 20) = c32;
*(u32 *)(s + 24) = c32;
*(u32 *)(s + n - 28) = c32;
*(u32 *)(s + n - 24) = c32;
*(u32 *)(s + n - 20) = c32;
*(u32 *)(s + n - 16) = c32;
k = 24 + ((uintptr_t)s & 4);
s += k;
n -= k;
u64 c64 = c32 | ((u64)c32 << 32);
for (; n >= 32; n -= 32, s += 32) {
*(u64 *)(s + 0) = c64;
*(u64 *)(s + 8) = c64;
*(u64 *)(s + 16) = c64;
*(u64 *)(s + 24) = c64;
}
return dest;
}

View File

@ -0,0 +1,7 @@
#include <string.h>
int strcmp(const char *l, const char *r)
{
for (; *l==*r && *l; l++, r++);
return *(unsigned char *)l - *(unsigned char *)r;
}

View File

@ -0,0 +1,25 @@
#include <string.h>
#include <stdint.h>
#include <limits.h>
#ifdef __LITEOS__
#undef ALIGN
#endif
#define ALIGN (sizeof(size_t))
#define ONES ((size_t)-1/UCHAR_MAX)
#define HIGHS (ONES * (UCHAR_MAX/2+1))
#define HASZERO(x) (((x)-ONES) & ~(x) & HIGHS)
size_t strlen(const char *s)
{
const char *a = s;
#ifdef __GNUC__
typedef size_t __attribute__((__may_alias__)) word;
const word *w;
for (; (uintptr_t)s % ALIGN; s++) if (!*s) return s-a;
for (w = (const void *)s; !HASZERO(*w); w++);
s = (const void *)w;
#endif
for (; *s; s++);
return s-a;
}