mirror of
https://github.com/apache/nuttx.git
synced 2026-08-18 20:18:17 +00:00
libc_data_t is 8 bytes wide, so a buffer which is 4-byte but not 8-byte aligned falls back to the byte at a time loop. Add a 32-bit middle path so such buffers still handle four bytes per iteration. * Add DETECTNULL32/DETECTCHAR32, UNALIGNED4/UNALIGNED4_X, LITTLEBLOCKSIZE4/BIGBLOCKSIZE4 and TOO_SMALL4 to libs/libc/libc.h. * Take the new path in memccpy, memcmp, memcpy, memset, stpcpy, stpncpy, strcmp, strcpy, strncmp and strncpy when both pointers are 4-byte aligned but the 8-byte path can't be used. Assisted-by: Claude:claude-opus-5 Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
130 lines
3.8 KiB
C
130 lines
3.8 KiB
C
/****************************************************************************
|
|
* libs/libc/string/lib_bsdmemchr.c
|
|
*
|
|
* SPDX-License-Identifier: BSD
|
|
* SPDX-FileCopyrightText: 1994-2009 Red Hat, Inc. All rights reserved
|
|
*
|
|
* Copyright (c) 1994-2009 Red Hat, Inc. All rights reserved.
|
|
*
|
|
* This copyrighted material is made available to anyone wishing to use,
|
|
* modify, copy, or redistribute it subject to the terms and conditions
|
|
* of the BSD License. This program is distributed in the hope that
|
|
* it will be useful, but WITHOUT ANY WARRANTY expressed or implied,
|
|
* including the implied warranties of MERCHANTABILITY or FITNESS FOR
|
|
* A PARTICULAR PURPOSE. A copy of this license is available at
|
|
* http://www.opensource.org/licenses. Any Red Hat trademarks that are
|
|
* incorporated in the source code or documentation are not subject to
|
|
* the BSD License and may only be used or replicated with the express
|
|
* permission of Red Hat, Inc.
|
|
*
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Included Files
|
|
****************************************************************************/
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "libc.h"
|
|
|
|
/****************************************************************************
|
|
* Pre-processor Definitions
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Public Functions
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
|
* Name: memchr
|
|
*
|
|
* Description:
|
|
* The memchr() function locates the first occurrence of 'c' (converted to
|
|
* an unsigned char) in the initial 'n' bytes (each interpreted as
|
|
* unsigned char) of the object pointed to by s.
|
|
*
|
|
* Returned Value:
|
|
* The memchr() function returns a pointer to the located byte, or a null
|
|
* pointer if the byte does not occur in the object.
|
|
*
|
|
****************************************************************************/
|
|
|
|
#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;
|
|
FAR libc_data_t *asrc;
|
|
unsigned char d = c;
|
|
libc_data_t mask;
|
|
unsigned int i;
|
|
|
|
while (UNALIGNED_X(p))
|
|
{
|
|
if (!n--)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
if (*p == d)
|
|
{
|
|
return (FAR void *)p;
|
|
}
|
|
|
|
p++;
|
|
}
|
|
|
|
if (!TOO_SMALL(n))
|
|
{
|
|
/* If we get this far, we know that n is large and p is
|
|
* word-aligned.
|
|
* The fast code reads the source one word at a time and only
|
|
* performs the bytewise search on word-sized segments if they
|
|
* contain the search character, which is detected by XORing
|
|
* the word-sized segment with a word-sized block of the search
|
|
* character and then detecting for the presence of NUL in the
|
|
* result.
|
|
*/
|
|
|
|
asrc = (FAR libc_data_t *)p;
|
|
mask = d << 8 | d;
|
|
mask = mask << 16 | mask;
|
|
for (i = 32; i < LITTLEBLOCKSIZE * 8; i <<= 1)
|
|
{
|
|
mask = (mask << i) | mask;
|
|
}
|
|
|
|
while (n >= LITTLEBLOCKSIZE)
|
|
{
|
|
if (DETECTCHAR(*asrc, mask))
|
|
{
|
|
break;
|
|
}
|
|
|
|
n -= LITTLEBLOCKSIZE;
|
|
asrc++;
|
|
}
|
|
|
|
/* If there are fewer than LITTLEBLOCKSIZE characters left,
|
|
* then we resort to the bytewise loop.
|
|
*/
|
|
|
|
p = (FAR unsigned char *)asrc;
|
|
}
|
|
|
|
while (n--)
|
|
{
|
|
if (*p == d)
|
|
{
|
|
return (FAR void *)p;
|
|
}
|
|
|
|
p++;
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
#endif
|