string:use builtin function to optimize libc function

This allows the compiler to automatically identify which string functions
can be compiled into libraries, and the compiler's internal implementation
is faster than libc functions.

Signed-off-by: anjiahao <anjiahao@xiaomi.com>
This commit is contained in:
anjiahao
2025-01-10 11:46:01 +08:00
committed by Xiang Xiao
parent 4028132910
commit 7c96537a58
23 changed files with 275 additions and 1 deletions
+22 -1
View File
@@ -103,7 +103,8 @@ extern void __asan_storeN(FAR void *addr, size_t size);
****************************************************************************/
#ifdef CONFIG_LIBC_ARCH_MEMCHR
#undef memchr
no_builtin("memchr")
FAR void *memchr(FAR const void *s, int c, size_t n)
{
# ifdef CONFIG_MM_KASAN_INSTRUMENT
@@ -117,6 +118,8 @@ FAR void *memchr(FAR const void *s, int c, size_t n)
#endif
#ifdef CONFIG_LIBC_ARCH_MEMCPY
#undef memcpy
no_builtin("memcpy")
FAR void *memcpy(FAR void *dest, FAR const void *src, FAR size_t n)
{
# ifdef CONFIG_MM_KASAN_INSTRUMENT
@@ -132,6 +135,8 @@ FAR void *memcpy(FAR void *dest, FAR const void *src, FAR size_t n)
#endif
#ifdef CONFIG_LIBC_ARCH_MEMCMP
#undef memcmp
no_builtin("memcmp")
int memcmp(FAR const void *s1, FAR const void *s2, size_t n)
{
# ifdef CONFIG_MM_KASAN_INSTRUMENT
@@ -160,6 +165,8 @@ FAR void *memmove(FAR void *dest, FAR const void *src, FAR size_t n)
#endif
#ifdef CONFIG_LIBC_ARCH_MEMSET
#undef memset
no_builtin("memset")
FAR void *memset(FAR void *s, int c, FAR size_t n)
{
# ifdef CONFIG_MM_KASAN_INSTRUMENT
@@ -172,6 +179,8 @@ FAR void *memset(FAR void *s, int c, FAR size_t n)
#endif
#ifdef CONFIG_LIBC_ARCH_STRCMP
#undef strcmp
no_builtin("strcmp")
int strcmp(FAR const char *s1, FAR const char *s2)
{
# ifdef CONFIG_MM_KASAN_INSTRUMENT
@@ -185,6 +194,8 @@ int strcmp(FAR const char *s1, FAR const char *s2)
#endif
#ifdef CONFIG_LIBC_ARCH_STRCPY
#undef strcpy
no_builtin("strcpy")
FAR char *strcpy(FAR char *dest, FAR const char *src)
{
# ifdef CONFIG_MM_KASAN_INSTRUMENT
@@ -200,6 +211,8 @@ FAR char *strcpy(FAR char *dest, FAR const char *src)
#endif
#ifdef CONFIG_LIBC_ARCH_STRLEN
#undef strlen
no_builtin("strlen")
size_t strlen(FAR const char *s)
{
size_t ret = ARCH_LIBCFUN(strlen)(s);
@@ -213,6 +226,8 @@ size_t strlen(FAR const char *s)
#endif
#ifdef CONFIG_LIBC_ARCH_STRNCPY
#undef strncpy
no_builtin("strncpy")
FAR char *strncpy(FAR char *dest, FAR const char *src, size_t n)
{
# ifdef CONFIG_MM_KASAN_INSTRUMENT
@@ -228,6 +243,8 @@ FAR char *strncpy(FAR char *dest, FAR const char *src, size_t n)
#endif
#ifdef CONFIG_LIBC_ARCH_STRCHR
#undef strchr
no_builtin("strchr")
FAR char *strchr(FAR const char *s, int c)
{
# ifdef CONFIG_MM_KASAN_INSTRUMENT
@@ -253,6 +270,8 @@ FAR char *strchrnul(FAR const char *s, int c)
#endif
#ifdef CONFIG_LIBC_ARCH_STRNCMP
#undef strncmp
no_builtin("strncmp")
int strncmp(FAR const char *s1, FAR const char *s2, size_t n)
{
# ifdef CONFIG_MM_KASAN_INSTRUMENT
@@ -285,6 +304,8 @@ size_t strnlen(FAR const char *s, size_t maxlen)
#endif
#ifdef CONFIG_LIBC_ARCH_STRRCHR
#undef strrchr
no_builtin("strrchr")
FAR char *strrchr(FAR const char *s, int c)
{
# ifdef CONFIG_MM_KASAN_INSTRUMENT
+1
View File
@@ -74,6 +74,7 @@
#if !defined(CONFIG_LIBC_ARCH_MEMCHR) && defined(LIBC_BUILD_MEMCHR)
#undef memchr
no_builtin("memchr")
FAR void *memchr(FAR const void *s, int c, size_t n)
{
FAR const unsigned char *p = (FAR const unsigned char *)s;
+1
View File
@@ -62,6 +62,7 @@
#ifndef CONFIG_LIBC_ARCH_STPCPY
#undef stpcpy
no_builtin("stpcpy")
nosanitize_address
FAR char *stpcpy(FAR char *dest, FAR const char *src)
{
+1
View File
@@ -78,6 +78,7 @@
#ifndef CONFIG_LIBC_ARCH_STPNCPY
#undef stpncpy
no_builtin("stpncpy")
FAR char *stpncpy(FAR char *dest, FAR const char *src, size_t n)
{
FAR char *ret = NULL;
+1
View File
@@ -49,6 +49,7 @@
#if !defined(CONFIG_LIBC_ARCH_STRCAT) && defined(LIBC_BUILD_STRCAT)
#undef strcat
no_builtin("strcat")
nosanitize_address
FAR char *strcat(FAR char *dest, FAR const char *src)
{
+1
View File
@@ -68,6 +68,7 @@
#if !defined(CONFIG_LIBC_ARCH_STRCHR) && defined(LIBC_BUILD_STRCHR)
#undef strchr
no_builtin("strchr")
nosanitize_address
FAR char *strchr(FAR const char *s, int c)
{
+1
View File
@@ -51,6 +51,7 @@
#if !defined(CONFIG_LIBC_ARCH_STRCMP) && defined(LIBC_BUILD_STRCMP)
#undef strcmp
no_builtin("strcmp")
nosanitize_address
int strcmp(FAR const char *cs, FAR const char *ct)
{
+1
View File
@@ -64,6 +64,7 @@
#if !defined(CONFIG_LIBC_ARCH_STRCPY) && defined(LIBC_BUILD_STRCPY)
#undef strcpy
nosanitize_address
no_builtin("strcpy")
FAR char *strcpy(FAR char *dest, FAR const char *src)
{
FAR char *dst0 = dest;
+1
View File
@@ -49,6 +49,7 @@
#if !defined(CONFIG_LIBC_ARCH_STRLEN) && defined(LIBC_BUILD_STRLEN)
#undef strlen
no_builtin("strlen")
nosanitize_address
size_t strlen(FAR const char *s)
{
+1
View File
@@ -53,6 +53,7 @@
#if !defined(CONFIG_LIBC_ARCH_STRNCMP) && defined(LIBC_BUILD_STRNCMP)
#undef strncmp
no_builtin("strncmp")
nosanitize_address
int strncmp(FAR const char *cs, FAR const char *ct, size_t nb)
{
+1
View File
@@ -77,6 +77,7 @@
#if !defined(CONFIG_LIBC_ARCH_STRNCPY) && defined(LIBC_BUILD_STRNCPY)
#undef strncpy
nosanitize_address
no_builtin("strncpy")
FAR char *strncpy(FAR char *dest, FAR const char *src, size_t n)
{
FAR char *dst0 = dest;
+1
View File
@@ -36,6 +36,7 @@
#if !defined(CONFIG_LIBC_ARCH_STRRCHR) && defined(LIBC_BUILD_STRRCHR)
#undef strrchr
no_builtin("strrchr")
FAR char *strrchr(FAR const char *s, int c)
{
FAR const char *last = NULL;
+1
View File
@@ -50,6 +50,7 @@
#if !defined(CONFIG_LIBC_ARCH_MEMCHR) && defined(LIBC_BUILD_MEMCHR)
#undef memchr
no_builtin("memchr")
FAR void *memchr(FAR const void *s, int c, size_t n)
{
FAR const unsigned char *p = (FAR const unsigned char *)s;
+1
View File
@@ -36,6 +36,7 @@
#if !defined(CONFIG_LIBC_ARCH_STRCAT) && defined(LIBC_BUILD_STRCAT)
#undef strcat
no_builtin("strcat")
FAR char *strcat(FAR char *dest, FAR const char *src)
{
FAR char *ret = dest;
+1
View File
@@ -50,6 +50,7 @@
#if !defined(CONFIG_LIBC_ARCH_STRCHR) && defined(LIBC_BUILD_STRCHR)
#undef strchr
no_builtin("strchr")
FAR char *strchr(FAR const char *s, int c)
{
for (; ; s++)
+1
View File
@@ -48,6 +48,7 @@
#if !defined(CONFIG_LIBC_ARCH_STRCPY) && defined(LIBC_BUILD_STRCPY)
#undef strcpy
no_builtin("strcpy")
FAR char *strcpy(FAR char *dest, FAR const char *src)
{
FAR char *tmp = dest;
+1
View File
@@ -42,6 +42,7 @@
****************************************************************************/
#undef strcspn
no_builtin("strcspn")
size_t strcspn(const char *s, const char *reject)
{
size_t i;
+1
View File
@@ -36,6 +36,7 @@
#if !defined(CONFIG_LIBC_ARCH_STRLEN) && defined(LIBC_BUILD_STRLEN)
#undef strlen
no_builtin("strlen")
size_t strlen(FAR const char *s)
{
FAR const char *sc;
+1
View File
@@ -36,6 +36,7 @@
#if !defined(CONFIG_LIBC_ARCH_STRNCMP) && defined(LIBC_BUILD_STRNCMP)
#undef strncmp
no_builtin("strncmp")
int strncmp(FAR const char *cs, FAR const char *ct, size_t nb)
{
register int result = 0;
+1
View File
@@ -40,6 +40,7 @@
#if !defined(CONFIG_LIBC_ARCH_STRRCHR) && defined(LIBC_BUILD_STRRCHR)
#undef strrchr
no_builtin("strrchr")
FAR char *strrchr(FAR const char *s, int c)
{
FAR const char *r = NULL;
+1
View File
@@ -53,6 +53,7 @@
*/
#undef strstr
no_builtin("strstr")
FAR char *strstr(FAR const char *haystack, FAR const char *needle)
{
#ifdef CONFIG_ALLOW_MIT_COMPONENTS