This is an automated email from the ASF dual-hosted git repository.

jerpelea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit bd6b2265466a1b7fdce6728930e70b1d8b8e2399
Author: anjiahao <[email protected]>
AuthorDate: Wed Jun 4 19:41:20 2025 +0800

    libs/libc/string: Use long long to speed up the BSD string functions.
    
    Most hardware accesses the memory through a 64-bit bus, so handle the
    data in 64-bit chunks instead of "long" chunks which are only 32-bit
    wide on the 32-bit platforms.
    
    * Add the libc_data_t type (unsigned long long) and move the shared
      UNALIGNED/UNALIGNED_X/ALIGNED, LITTLEBLOCKSIZE, TOO_SMALL and
      DETECTNULL helpers from the individual C files to libs/libc/libc.h.
    * Convert all lib_bsd*.c implementations to the new type and macros,
      which also drops the duplicated LONG_MAX conditionals.
    
    Assisted-by: Claude:claude-opus-5
    Signed-off-by: anjiahao <[email protected]>
---
 libs/libc/libc.h                  | 28 +++++++++++++++++++++++--
 libs/libc/string/lib_bsdmemccpy.c | 39 ++++++++---------------------------
 libs/libc/string/lib_bsdmemchr.c  | 38 ++++++++--------------------------
 libs/libc/string/lib_bsdmemcmp.c  | 25 ++++++-----------------
 libs/libc/string/lib_bsdmemcpy.c  | 29 +++++++-------------------
 libs/libc/string/lib_bsdmemrchr.c | 43 +++++++++++----------------------------
 libs/libc/string/lib_bsdmemset.c  | 22 ++++++++------------
 libs/libc/string/lib_bsdstpcpy.c  | 27 +++++++-----------------
 libs/libc/string/lib_bsdstpncpy.c | 37 ++++++++-------------------------
 libs/libc/string/lib_bsdstrcat.c  | 15 +-------------
 libs/libc/string/lib_bsdstrchr.c  | 36 ++++++++++----------------------
 libs/libc/string/lib_bsdstrcmp.c  | 23 ++++-----------------
 libs/libc/string/lib_bsdstrcpy.c  | 25 +++++------------------
 libs/libc/string/lib_bsdstrlen.c  | 19 +++--------------
 libs/libc/string/lib_bsdstrncmp.c | 29 ++++++--------------------
 libs/libc/string/lib_bsdstrncpy.c | 33 +++++++-----------------------
 16 files changed, 130 insertions(+), 338 deletions(-)

diff --git a/libs/libc/libc.h b/libs/libc/libc.h
index a8197a424ce..3e010bdb9e6 100644
--- a/libs/libc/libc.h
+++ b/libs/libc/libc.h
@@ -171,16 +171,40 @@
 #  define ARCH_LIBCFUN(x)  x
 #endif
 
+/* Nonzero if either x or y is not aligned on a "libc_data_t" boundary. */
+
+#define UNALIGNED_X(x) \
+  (((libc_data_t)(uintptr_t)(x) & (sizeof(libc_data_t) - 1)) != 0)
+
+#define UNALIGNED(x, y) ((UNALIGNED_X(x)) | (UNALIGNED_X(y)))
+
+#define ALIGNED(x) \
+  (((libc_data_t)(uintptr_t)(x) & (sizeof(libc_data_t) - 1)) == 0)
+
+/* How many bytes are copied each iteration of the word copy loop. */
+
+#define LITTLEBLOCKSIZE (sizeof(libc_data_t))
+
+/* Threshold for punting to the byte copier. */
+
+#define TOO_SMALL(len) ((len) < LITTLEBLOCKSIZE)
+
+/* Macros for detecting endchar */
+
+#define DETECTNULL(x) (((x) - 0x0101010101010101LL) & ~(x) & 
0x8080808080808080LL)
+
+#ifndef __ASSEMBLY__
+
 /****************************************************************************
  * Public Types
  ****************************************************************************/
 
+typedef unsigned long long libc_data_t;
+
 /****************************************************************************
  * Public Data
  ****************************************************************************/
 
-#ifndef __ASSEMBLY__
-
 #undef EXTERN
 #if defined(__cplusplus)
 #define EXTERN extern "C"
diff --git a/libs/libc/string/lib_bsdmemccpy.c 
b/libs/libc/string/lib_bsdmemccpy.c
index c2efbf24d56..406f1ddc408 100644
--- a/libs/libc/string/lib_bsdmemccpy.c
+++ b/libs/libc/string/lib_bsdmemccpy.c
@@ -27,33 +27,12 @@
 #include <sys/types.h>
 #include <string.h>
 
+#include "libc.h"
+
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* Nonzero if either x or y is not aligned on a "long" boundary. */
-
-#define UNALIGNED(x, y) \
-  (((long)(uintptr_t)(x) & (sizeof(long) - 1)) | ((long)(uintptr_t)(y) & 
(sizeof(long) - 1)))
-
-/* How many bytes are copied each iteration of the word copy loop. */
-
-#define LITTLEBLOCKSIZE (sizeof(long))
-
-/* Threshold for punting to the byte copier. */
-
-#define TOO_SMALL(len) ((len) < LITTLEBLOCKSIZE)
-
-/* Macros for detecting endchar */
-
-#if LONG_MAX == 2147483647
-#  define DETECTNULL(x) (((x) - 0x01010101) & ~(x) & 0x80808080)
-#elif LONG_MAX == 9223372036854775807
-/* Nonzero if x (a long int) contains a NULL byte. */
-
-#  define DETECTNULL(x) (((x) - 0x0101010101010101) & ~(x) & 
0x8080808080808080)
-#endif
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -80,8 +59,8 @@ FAR void *memccpy(FAR void *s1, FAR const void *s2, int c, 
size_t n)
   FAR void *ptr = NULL;
   FAR unsigned char *pout = (FAR unsigned char *)s1;
   FAR const unsigned char *pin = (FAR const unsigned char *)s2;
-  FAR long *paligned_out;
-  FAR const long *paligned_in;
+  FAR libc_data_t *paligned_out;
+  FAR const libc_data_t *paligned_in;
   unsigned char endchar = c & 0xff;
 
   /* If the size is small, or either pin or pout is unaligned,
@@ -91,10 +70,10 @@ FAR void *memccpy(FAR void *s1, FAR const void *s2, int c, 
size_t n)
   if (!TOO_SMALL(n) && !UNALIGNED(pin, pout))
     {
       unsigned int i;
-      unsigned long mask = 0;
+      libc_data_t mask = 0;
 
-      paligned_out = (FAR long *)pout;
-      paligned_in = (FAR long *)pin;
+      paligned_out = (FAR libc_data_t *)pout;
+      paligned_in = (FAR libc_data_t *)pin;
 
       /* The fast code reads the ASCII one word at a time and only
        * performs the bytewise search on word-sized segments if they
@@ -109,11 +88,11 @@ FAR void *memccpy(FAR void *s1, FAR const void *s2, int c, 
size_t n)
           mask = (mask << 8) + endchar;
         }
 
-      /* Copy one long word at a time if possible.  */
+      /* Copy one libc_data_t word at a time if possible.  */
 
       while (n >= LITTLEBLOCKSIZE)
         {
-          unsigned long buffer = (unsigned long)(*paligned_in);
+          libc_data_t buffer = (libc_data_t)(*paligned_in);
           buffer ^= mask;
           if (DETECTNULL(buffer))
             {
diff --git a/libs/libc/string/lib_bsdmemchr.c b/libs/libc/string/lib_bsdmemchr.c
index 1a4887e0cdb..88ed624fc7b 100644
--- a/libs/libc/string/lib_bsdmemchr.c
+++ b/libs/libc/string/lib_bsdmemchr.c
@@ -33,28 +33,6 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-#define UNALIGNED(x) ((long)(uintptr_t)(x) & (sizeof(long) - 1))
-
-/* How many bytes are loaded each iteration of the word copy loop. */
-
-#define LBLOCKSIZE (sizeof(long))
-
-/* Threshold for punting to the bytewise iterator. */
-
-#define TOO_SMALL(len) ((len) < LBLOCKSIZE)
-
-#if LONG_MAX == 2147483647
-#  define DETECTNULL(x) (((x) - 0x01010101) & ~(x) & 0x80808080)
-#elif LONG_MAX == 9223372036854775807
-/* Nonzero if x (a long int) contains a NULL byte. */
-
-#  define DETECTNULL(x) (((x) - 0x0101010101010101) & ~(x) & 
0x8080808080808080)
-#endif
-
-/* DETECTCHAR returns nonzero if (long)x contains the byte used
- * to fill (long)mask.
- */
-
 #define DETECTCHAR(x, mask) (DETECTNULL((x) ^ (mask)))
 
 /****************************************************************************
@@ -81,12 +59,12 @@ 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 unsigned long *asrc;
+  FAR libc_data_t *asrc;
   unsigned char d = c;
-  unsigned long mask;
+  libc_data_t mask;
   unsigned int i;
 
-  while (UNALIGNED(p))
+  while (UNALIGNED_X(p))
     {
       if (!n--)
         {
@@ -113,26 +91,26 @@ FAR void *memchr(FAR const void *s, int c, size_t n)
        * result.
        */
 
-      asrc = (FAR unsigned long *)p;
+      asrc = (FAR libc_data_t *)p;
       mask = d << 8 | d;
       mask = mask << 16 | mask;
-      for (i = 32; i < LBLOCKSIZE * 8; i <<= 1)
+      for (i = 32; i < LITTLEBLOCKSIZE * 8; i <<= 1)
         {
           mask = (mask << i) | mask;
         }
 
-      while (n >= LBLOCKSIZE)
+      while (n >= LITTLEBLOCKSIZE)
         {
           if (DETECTCHAR(*asrc, mask))
             {
               break;
             }
 
-          n -= LBLOCKSIZE;
+          n -= LITTLEBLOCKSIZE;
           asrc++;
         }
 
-      /* If there are fewer than LBLOCKSIZE characters left,
+      /* If there are fewer than LITTLEBLOCKSIZE characters left,
        * then we resort to the bytewise loop.
        */
 
diff --git a/libs/libc/string/lib_bsdmemcmp.c b/libs/libc/string/lib_bsdmemcmp.c
index 85b1428cdf3..5a7a532c936 100644
--- a/libs/libc/string/lib_bsdmemcmp.c
+++ b/libs/libc/string/lib_bsdmemcmp.c
@@ -33,19 +33,6 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* Nonzero if either x or y is not aligned on a "long" boundary. */
-
-#define UNALIGNED(x, y) \
-  (((long)(uintptr_t)(x) & (sizeof(long) - 1)) | ((long)(uintptr_t)(y) & 
(sizeof(long) - 1)))
-
-/* How many bytes are copied each iteration of the word copy loop. */
-
-#define LBLOCKSIZE (sizeof(long))
-
-/* Threshold for punting to the byte copier. */
-
-#define TOO_SMALL(len) ((len) < LBLOCKSIZE)
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -57,8 +44,8 @@ int memcmp(FAR const void *s1, FAR const void *s2, size_t n)
 {
   FAR unsigned char *p1 = (FAR unsigned char *)s1;
   FAR unsigned char *p2 = (FAR unsigned char *)s2;
-  FAR unsigned long *a1;
-  FAR unsigned long *a2;
+  FAR libc_data_t *a1;
+  FAR libc_data_t *a2;
 
   /* If the size is too small, or either pointer is unaligned,
    * then we punt to the byte compare loop.  Hopefully this will
@@ -71,9 +58,9 @@ int memcmp(FAR const void *s1, FAR const void *s2, size_t n)
        * word at a time.
        */
 
-      a1 = (FAR unsigned long *)p1;
-      a2 = (FAR unsigned long *)p2;
-      while (n >= LBLOCKSIZE)
+      a1 = (FAR libc_data_t *)p1;
+      a2 = (FAR libc_data_t *)p2;
+      while (n >= LITTLEBLOCKSIZE)
         {
           if (*a1 != *a2)
             {
@@ -82,7 +69,7 @@ int memcmp(FAR const void *s1, FAR const void *s2, size_t n)
 
           a1++;
           a2++;
-          n -= LBLOCKSIZE;
+          n -= LITTLEBLOCKSIZE;
         }
 
       /* check s mod LBLOCKSIZE remaining characters */
diff --git a/libs/libc/string/lib_bsdmemcpy.c b/libs/libc/string/lib_bsdmemcpy.c
index 78f77933fe7..6686088f6eb 100644
--- a/libs/libc/string/lib_bsdmemcpy.c
+++ b/libs/libc/string/lib_bsdmemcpy.c
@@ -33,22 +33,7 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* Nonzero if either x or y is not aligned on a "long" boundary. */
-
-#define UNALIGNED(x, y) \
-  (((long)(uintptr_t)(x) & (sizeof(long) - 1)) | ((long)(uintptr_t)(y) & 
(sizeof(long) - 1)))
-
-/* How many bytes are copied each iteration of the 4X unrolled loop. */
-
-#define BIGBLOCKSIZE (sizeof(long) << 2)
-
-/* How many bytes are copied each iteration of the word copy loop. */
-
-#define LITTLEBLOCKSIZE (sizeof(long))
-
-/* Threshold for punting to the byte copier. */
-
-#define TOO_SMALL(len) ((len) < BIGBLOCKSIZE)
+#define BIGBLOCKSIZE (sizeof(libc_data_t) << 2)
 
 /****************************************************************************
  * Public Functions
@@ -65,8 +50,8 @@ FAR void *memcpy(FAR void *dest, FAR const void *src, size_t 
n)
 {
   FAR char *pout = dest;
   FAR const char *pin = src;
-  FAR long *paligned_out;
-  FAR const long *paligned_in;
+  FAR libc_data_t *paligned_out;
+  FAR const libc_data_t *paligned_in;
 
   /* If the size is small, or either pin or pout is unaligned,
    * then punt into the byte copy loop.  This should be rare.
@@ -74,10 +59,10 @@ FAR void *memcpy(FAR void *dest, FAR const void *src, 
size_t n)
 
   if (!TOO_SMALL(n) && !UNALIGNED(pin, pout))
     {
-      paligned_out = (FAR long *)pout;
-      paligned_in = (FAR long *)pin;
+      paligned_out = (FAR libc_data_t *)pout;
+      paligned_in = (FAR libc_data_t *)pin;
 
-      /* Copy 4X long words at a time if possible. */
+      /* Copy 4X libc_data_t words at a time if possible. */
 
       while (n >= BIGBLOCKSIZE)
         {
@@ -88,7 +73,7 @@ FAR void *memcpy(FAR void *dest, FAR const void *src, size_t 
n)
           n -= BIGBLOCKSIZE;
         }
 
-      /* Copy one long word at a time if possible. */
+      /* Copy one libc_data_t word at a time if possible. */
 
       while (n >= LITTLEBLOCKSIZE)
         {
diff --git a/libs/libc/string/lib_bsdmemrchr.c 
b/libs/libc/string/lib_bsdmemrchr.c
index 36691103576..d11a288ae82 100644
--- a/libs/libc/string/lib_bsdmemrchr.c
+++ b/libs/libc/string/lib_bsdmemrchr.c
@@ -27,32 +27,12 @@
 
 #include <string.h>
 
+#include "libc.h"
+
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* Nonzero if x is not aligned on a "long" boundary. */
-
-#define UNALIGNED(x) ((long)(uintptr_t)((x) + 1) & (sizeof(long) - 1))
-
-/* How many bytes are loaded each iteration of the word copy loop. */
-
-#define LBLOCKSIZE (sizeof(long))
-
-/* Threshold for punting to the bytewise iterator. */
-
-#define TOO_SMALL(len) ((len) < LBLOCKSIZE)
-
-/* Macros for detecting endchar */
-
-#if LONG_MAX == 2147483647
-#  define DETECTNULL(x) (((x) - 0x01010101) & ~(x) & 0x80808080)
-#elif LONG_MAX == 9223372036854775807
-/* Nonzero if x (a long int) contains a NULL byte. */
-
-#  define DETECTNULL(x) (((x) - 0x0101010101010101) & ~(x) & 
0x8080808080808080)
-#endif
-
 #define DETECTCHAR(x, mask) (DETECTNULL((x) ^ (mask)))
 
 /****************************************************************************
@@ -78,12 +58,12 @@ FAR void *memrchr(FAR const void *s, int c, size_t n)
 {
   FAR const unsigned char *src0 =
             (FAR const unsigned char *)s + n - 1;
-  FAR unsigned long *asrc;
+  FAR libc_data_t *asrc;
   unsigned char d = c;
-  unsigned long mask;
+  libc_data_t mask;
   unsigned int i;
 
-  while (UNALIGNED(src0))
+  while (UNALIGNED_X(src0))
     {
       if (!n--)
         {
@@ -110,30 +90,31 @@ FAR void *memrchr(FAR const void *s, int c, size_t n)
        * result.
        */
 
-      asrc = (FAR unsigned long *)(src0 - LBLOCKSIZE + 1);
+      asrc = (FAR libc_data_t *)((uintptr_t)(src0 - LITTLEBLOCKSIZE + 1) &
+                                 ~(sizeof(libc_data_t) - 1));
       mask = d << 8 | d;
       mask = mask << 16 | mask;
-      for (i = 32; i < LBLOCKSIZE * 8; i <<= 1)
+      for (i = 32; i < LITTLEBLOCKSIZE * 8; i <<= 1)
         {
           mask = (mask << i) | mask;
         }
 
-      while (n >= LBLOCKSIZE)
+      while (n >= LITTLEBLOCKSIZE)
         {
           if (DETECTCHAR(*asrc, mask))
             {
               break;
             }
 
-          n -= LBLOCKSIZE;
+          n -= LITTLEBLOCKSIZE;
           asrc--;
         }
 
-      /* If there are fewer than LBLOCKSIZE characters left,
+      /* If there are fewer than LITTLEBLOCKSIZE characters left,
        * then we resort to the bytewise loop.
        */
 
-      src0 = (FAR unsigned char *)asrc + LBLOCKSIZE - 1;
+      src0 = (FAR unsigned char *)asrc + LITTLEBLOCKSIZE - 1;
     }
 
   while (n--)
diff --git a/libs/libc/string/lib_bsdmemset.c b/libs/libc/string/lib_bsdmemset.c
index 21ca7d6b058..399774e1ced 100644
--- a/libs/libc/string/lib_bsdmemset.c
+++ b/libs/libc/string/lib_bsdmemset.c
@@ -33,10 +33,6 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-#define LBLOCKSIZE     (sizeof(long))
-#define UNALIGNED(x)   ((long)(x) & (LBLOCKSIZE - 1))
-#define TOO_SMALL(len) ((len) < LBLOCKSIZE)
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -50,14 +46,14 @@
 no_builtin("memset")
 FAR void *memset(FAR void *m, int c, size_t n)
 {
-  FAR unsigned long *aligned_addr;
+  FAR libc_data_t *aligned_addr;
   FAR char *s = (FAR char *)m;
-  unsigned long buffer;
+  libc_data_t buffer;
   int i;
 
   /* To avoid sign extension, copy C to an unsigned variable.  */
 
-  while (UNALIGNED(s))
+  while (UNALIGNED(s, 0))
     {
       if (n--)
         {
@@ -73,29 +69,29 @@ FAR void *memset(FAR void *m, int c, size_t n)
     {
       /* If we get this far, we know that n is large and s is word-aligned. */
 
-      aligned_addr = (FAR unsigned long *)s;
+      aligned_addr = (FAR libc_data_t *)s;
       buffer = ((unsigned int)c << 8) | c;
       buffer |= (buffer << 16);
-      for (i = 32; i < LBLOCKSIZE * 8; i <<= 1)
+      for (i = 32; i < LITTLEBLOCKSIZE * 8; i <<= 1)
         {
           buffer = (buffer << i) | buffer;
         }
 
       /* Unroll the loop.  */
 
-      while (n >= LBLOCKSIZE * 4)
+      while (n >= LITTLEBLOCKSIZE * 4)
         {
           *aligned_addr++ = buffer;
           *aligned_addr++ = buffer;
           *aligned_addr++ = buffer;
           *aligned_addr++ = buffer;
-          n -= 4 * LBLOCKSIZE;
+          n -= 4 * LITTLEBLOCKSIZE;
         }
 
-      while (n >= LBLOCKSIZE)
+      while (n >= LITTLEBLOCKSIZE)
         {
           *aligned_addr++ = buffer;
-          n -= LBLOCKSIZE;
+          n -= LITTLEBLOCKSIZE;
         }
 
       /* Pick up the remainder with a bytewise loop.  */
diff --git a/libs/libc/string/lib_bsdstpcpy.c b/libs/libc/string/lib_bsdstpcpy.c
index 786d7f251b3..db6a5090841 100644
--- a/libs/libc/string/lib_bsdstpcpy.c
+++ b/libs/libc/string/lib_bsdstpcpy.c
@@ -27,25 +27,12 @@
 
 #include <string.h>
 
+#include "libc.h"
+
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* Nonzero if either x or y is not aligned on a "long" boundary. */
-
-#define UNALIGNED(x, y) \
-  (((long)(uintptr_t)(x) & (sizeof(long) - 1)) | ((long)(uintptr_t)(y) & 
(sizeof(long) - 1)))
-
-/* Macros for detecting endchar */
-
-#if LONG_MAX == 2147483647
-#  define DETECTNULL(x) (((x) - 0x01010101) & ~(x) & 0x80808080)
-#elif LONG_MAX == 9223372036854775807
-/* Nonzero if x (a long int) contains a NULL byte. */
-
-#  define DETECTNULL(x) (((x) - 0x0101010101010101) & ~(x) & 
0x8080808080808080)
-#endif
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -69,17 +56,17 @@ no_builtin("stpcpy")
 nosanitize_address
 FAR char *stpcpy(FAR char *dest, FAR const char *src)
 {
-  FAR long *aligned_dst;
-  FAR const long *aligned_src;
+  FAR libc_data_t *aligned_dst;
+  FAR const libc_data_t *aligned_src;
 
   /* If src or dest is unaligned, then copy bytes. */
 
   if (!UNALIGNED(src, dest))
     {
-      aligned_dst = (FAR long *)dest;
-      aligned_src = (FAR long *)src;
+      aligned_dst = (FAR libc_data_t *)dest;
+      aligned_src = (FAR libc_data_t *)src;
 
-      /* src and dest are both "long int" aligned, try to do "long int"
+      /* src and dest are both "libc_data_t" aligned, try to do "libc_data_t"
        * sized copies.
        */
 
diff --git a/libs/libc/string/lib_bsdstpncpy.c 
b/libs/libc/string/lib_bsdstpncpy.c
index 38e4c846954..c06533a8835 100644
--- a/libs/libc/string/lib_bsdstpncpy.c
+++ b/libs/libc/string/lib_bsdstpncpy.c
@@ -27,31 +27,12 @@
 #include <sys/types.h>
 #include <string.h>
 
+#include "libc.h"
+
 /****************************************************************************
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* Nonzero if either x or y is not aligned on a "long" boundary. */
-
-#define UNALIGNED(x, y) \
-  (((long)(uintptr_t)(x) & (sizeof(long) - 1)) | ((long)(uintptr_t)(y) & 
(sizeof(long) - 1)))
-
-/* How many bytes are loaded each iteration of the word copy loop. */
-
-#define LBLOCKSIZE (sizeof(long))
-
-/* Macros for detecting endchar */
-
-#if LONG_MAX == 2147483647
-#define DETECTNULL(x) (((x) - 0x01010101) & ~(x) & 0x80808080)
-#elif LONG_MAX == 9223372036854775807
-/* Nonzero if x (a long int) contains a NULL byte. */
-
-#define DETECTNULL(x) (((x) - 0x0101010101010101) & ~(x) & 0x8080808080808080)
-#endif
-
-#define TOO_SMALL(len) ((len) < sizeof(long))
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -85,23 +66,23 @@ no_builtin("stpncpy")
 FAR char *stpncpy(FAR char *dest, FAR const char *src, size_t n)
 {
   FAR char *ret = NULL;
-  FAR long *aligned_dst;
-  FAR const long *aligned_src;
+  FAR libc_data_t *aligned_dst;
+  FAR const libc_data_t *aligned_src;
 
   /* If src and dest is aligned and n large enough, then copy words. */
 
   if (!UNALIGNED(src, dest) && !TOO_SMALL(n))
     {
-      aligned_dst = (FAR long *)dest;
-      aligned_src = (FAR long *)src;
+      aligned_dst = (FAR libc_data_t *)dest;
+      aligned_src = (FAR libc_data_t *)src;
 
-      /* src and dest are both "long int" aligned, try to do "long int"
+      /* src and dest are both "libc_data_t" aligned, try to do "libc_data_t"
        * sized copies.
        */
 
-      while (n >= LBLOCKSIZE && !DETECTNULL(*aligned_src))
+      while (n >= LITTLEBLOCKSIZE && !DETECTNULL(*aligned_src))
         {
-          n -= LBLOCKSIZE;
+          n -= LITTLEBLOCKSIZE;
           *aligned_dst++ = *aligned_src++;
         }
 
diff --git a/libs/libc/string/lib_bsdstrcat.c b/libs/libc/string/lib_bsdstrcat.c
index 1263cf53259..898ef95876e 100644
--- a/libs/libc/string/lib_bsdstrcat.c
+++ b/libs/libc/string/lib_bsdstrcat.c
@@ -33,19 +33,6 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-#define ALIGNED(x) \
-  (((long)(uintptr_t)(x) & (sizeof(long) - 1)) == 0)
-
-/* Macros for detecting endchar */
-
-#if LONG_MAX == 2147483647
-#  define DETECTNULL(x) (((x) - 0x01010101) & ~(x) & 0x80808080)
-#elif LONG_MAX == 9223372036854775807
-/* Nonzero if x (a long int) contains a NULL byte. */
-
-#  define DETECTNULL(x) (((x) - 0x0101010101010101) & ~(x) & 
0x8080808080808080)
-#endif
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -62,7 +49,7 @@ FAR char *strcat(FAR char *dest, FAR const char *src)
 
   if (ALIGNED(dest))
     {
-      FAR unsigned long *aligned_s1 = (FAR unsigned long *)dest;
+      FAR libc_data_t *aligned_s1 = (FAR libc_data_t *)dest;
       while (!DETECTNULL(*aligned_s1))
         {
           aligned_s1++;
diff --git a/libs/libc/string/lib_bsdstrchr.c b/libs/libc/string/lib_bsdstrchr.c
index 05f661f7a8c..e49b0dd725e 100644
--- a/libs/libc/string/lib_bsdstrchr.c
+++ b/libs/libc/string/lib_bsdstrchr.c
@@ -33,22 +33,6 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-#define UNALIGNED(x) ((long)(uintptr_t)(x) & (sizeof(long) - 1))
-
-/* How many bytes are loaded each iteration of the word copy loop. */
-
-#define LBLOCKSIZE (sizeof(long))
-
-/* Macros for detecting endchar */
-
-#if LONG_MAX == 2147483647
-#  define DETECTNULL(x) (((x) - 0x01010101) & ~(x) & 0x80808080)
-#elif LONG_MAX == 9223372036854775807
-/* Nonzero if x (a long int) contains a NULL byte. */
-
-#  define DETECTNULL(x) (((x) - 0x0101010101010101) & ~(x) & 
0x8080808080808080)
-#endif
-
 #define DETECTCHAR(x, mask) (DETECTNULL((x) ^ (mask)))
 
 /****************************************************************************
@@ -76,16 +60,16 @@ nosanitize_address
 FAR char *strchr(FAR const char *s, int c)
 {
   FAR const unsigned char *s1 = (FAR const unsigned char *)s;
-  FAR unsigned long *aligned_addr;
+  FAR libc_data_t *aligned_addr;
   unsigned char i = c;
-  unsigned long mask;
-  unsigned long j;
+  libc_data_t mask;
+  libc_data_t j;
 
   /* Special case for finding 0. */
 
   if (!i)
     {
-      while (UNALIGNED(s1))
+      while (UNALIGNED_X(s1))
         {
           if (!*s1)
             {
@@ -97,7 +81,7 @@ FAR char *strchr(FAR const char *s, int c)
 
       /* Operate a word at a time. */
 
-      aligned_addr = (FAR unsigned long *)s1;
+      aligned_addr = (FAR libc_data_t *)s1;
       while (!DETECTNULL(*aligned_addr))
         {
           aligned_addr++;
@@ -114,9 +98,11 @@ FAR char *strchr(FAR const char *s, int c)
       return (FAR char *)s1;
     }
 
-  /* All other bytes.  Align the pointer, then search a long at a time. */
+  /* All other bytes.  Align the pointer,
+   * then search a libc_data_t at a time.
+   */
 
-  while (UNALIGNED(s1))
+  while (UNALIGNED_X(s1))
     {
       if (!*s1)
         {
@@ -132,12 +118,12 @@ FAR char *strchr(FAR const char *s, int c)
     }
 
   mask = i;
-  for (j = 8; j < LBLOCKSIZE * 8; j <<= 1)
+  for (j = 8; j < LITTLEBLOCKSIZE * 8; j <<= 1)
     {
       mask = (mask << j) | mask;
     }
 
-  aligned_addr = (FAR unsigned long *)s1;
+  aligned_addr = (FAR libc_data_t *)s1;
   while (!DETECTNULL(*aligned_addr) && !DETECTCHAR(*aligned_addr, mask))
     {
       aligned_addr++;
diff --git a/libs/libc/string/lib_bsdstrcmp.c b/libs/libc/string/lib_bsdstrcmp.c
index d22258055eb..fb2224e4f41 100644
--- a/libs/libc/string/lib_bsdstrcmp.c
+++ b/libs/libc/string/lib_bsdstrcmp.c
@@ -33,21 +33,6 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* Nonzero if either x or y is not aligned on a "long" boundary. */
-
-#define UNALIGNED(x, y) \
-  (((long)(uintptr_t)(x) & (sizeof(long) - 1)) | ((long)(uintptr_t)(y) & 
(sizeof(long) - 1)))
-
-/* Macros for detecting endchar */
-
-#if LONG_MAX == 2147483647
-#  define DETECTNULL(x) (((x) - 0x01010101) & ~(x) & 0x80808080)
-#elif LONG_MAX == 9223372036854775807
-/* Nonzero if x (a long int) contains a NULL byte. */
-
-#  define DETECTNULL(x) (((x) - 0x0101010101010101) & ~(x) & 
0x8080808080808080)
-#endif
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -58,8 +43,8 @@ no_builtin("strcmp")
 nosanitize_address
 int strcmp(FAR const char *cs, FAR const char *ct)
 {
-  FAR unsigned long *a1;
-  FAR unsigned long *a2;
+  FAR libc_data_t *a1;
+  FAR libc_data_t *a2;
 
   /* If cs or ct are unaligned, then compare bytes. */
 
@@ -67,8 +52,8 @@ int strcmp(FAR const char *cs, FAR const char *ct)
     {
       /* If cs and ct are word-aligned, compare them a word at a time. */
 
-      a1 = (FAR unsigned long *)cs;
-      a2 = (FAR unsigned long *)ct;
+      a1 = (FAR libc_data_t *)cs;
+      a2 = (FAR libc_data_t *)ct;
       while (*a1 == *a2)
         {
           /* To get here, *a1 == *a2, thus if we find a null in *a1,
diff --git a/libs/libc/string/lib_bsdstrcpy.c b/libs/libc/string/lib_bsdstrcpy.c
index 371e67f819b..439489c1a64 100644
--- a/libs/libc/string/lib_bsdstrcpy.c
+++ b/libs/libc/string/lib_bsdstrcpy.c
@@ -33,21 +33,6 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-/* Nonzero if either x or y is not aligned on a "long" boundary. */
-
-#define UNALIGNED(x, y) \
-  (((long)(uintptr_t)(x) & (sizeof(long) - 1)) | ((long)(uintptr_t)(y) & 
(sizeof(long) - 1)))
-
-/* Macros for detecting endchar */
-
-#if LONG_MAX == 2147483647
-#  define DETECTNULL(x) (((x) - 0x01010101) & ~(x) & 0x80808080)
-#elif LONG_MAX == 9223372036854775807
-/* Nonzero if x (a long int) contains a NULL byte. */
-
-#  define DETECTNULL(x) (((x) - 0x0101010101010101) & ~(x) & 
0x8080808080808080)
-#endif
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -72,17 +57,17 @@ FAR char *strcpy(FAR char *dest, FAR const char *src)
 {
   FAR char *dst0 = dest;
   FAR const char *src0 = src;
-  FAR unsigned long *aligned_dst;
-  FAR const unsigned long *aligned_src;
+  FAR libc_data_t *aligned_dst;
+  FAR const libc_data_t *aligned_src;
 
   /* If SRC or DEST is unaligned, then copy bytes. */
 
   if (!UNALIGNED(src0, dst0))
     {
-      aligned_dst = (FAR unsigned long *)dst0;
-      aligned_src = (FAR unsigned long *)src0;
+      aligned_dst = (FAR libc_data_t *)dst0;
+      aligned_src = (FAR libc_data_t *)src0;
 
-      /* SRC and DEST are both "long int" aligned, try to do "long int"
+      /* SRC and DEST are both "libc_data_t" aligned, try to do "libc_data_t"
        * sized copies.
        */
 
diff --git a/libs/libc/string/lib_bsdstrlen.c b/libs/libc/string/lib_bsdstrlen.c
index 6f78837f388..fd108a1ef1f 100644
--- a/libs/libc/string/lib_bsdstrlen.c
+++ b/libs/libc/string/lib_bsdstrlen.c
@@ -33,19 +33,6 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-#define LBLOCKSIZE (sizeof(long))
-#define UNALIGNED(x) ((long)(uintptr_t)(x) & (LBLOCKSIZE - 1))
-
-/* Macros for detecting endchar */
-
-#if LONG_MAX == 2147483647
-#  define DETECTNULL(x) (((x) - 0x01010101) & ~(x) & 0x80808080)
-#elif LONG_MAX == 9223372036854775807
-/* Nonzero if x (a long int) contains a NULL byte. */
-
-#  define DETECTNULL(x) (((x) - 0x0101010101010101) & ~(x) & 
0x8080808080808080)
-#endif
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -57,11 +44,11 @@ nosanitize_address
 size_t strlen(FAR const char *s)
 {
   FAR const char *start = s;
-  FAR unsigned long *aligned_addr;
+  FAR libc_data_t *aligned_addr;
 
   /* Align the pointer, so we can search a word at a time. */
 
-  while (UNALIGNED(s))
+  while (UNALIGNED_X(s))
     {
       if (!*s)
         {
@@ -75,7 +62,7 @@ size_t strlen(FAR const char *s)
    * a null in each word-sized block.
    */
 
-  aligned_addr = (FAR unsigned long *)s;
+  aligned_addr = (FAR libc_data_t *)s;
   while (!DETECTNULL(*aligned_addr))
     {
       aligned_addr++;
diff --git a/libs/libc/string/lib_bsdstrncmp.c 
b/libs/libc/string/lib_bsdstrncmp.c
index b3724ecd734..747b3b55a22 100644
--- a/libs/libc/string/lib_bsdstrncmp.c
+++ b/libs/libc/string/lib_bsdstrncmp.c
@@ -33,23 +33,6 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-#define LBLOCKSIZE (sizeof(long))
-
-/* Nonzero if either x or y is not aligned on a "long" boundary. */
-
-#define UNALIGNED(x, y) \
-  (((long)(uintptr_t)(x) & (sizeof(long) - 1)) | ((long)(uintptr_t)(y) & 
(sizeof(long) - 1)))
-
-/* Macros for detecting endchar */
-
-#if LONG_MAX == 2147483647
-#  define DETECTNULL(x) (((x) - 0x01010101) & ~(x) & 0x80808080)
-#elif LONG_MAX == 9223372036854775807
-/* Nonzero if x (a long int) contains a NULL byte. */
-
-#  define DETECTNULL(x) (((x) - 0x0101010101010101) & ~(x) & 
0x8080808080808080)
-#endif
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -60,8 +43,8 @@ no_builtin("strncmp")
 nosanitize_address
 int strncmp(FAR const char *cs, FAR const char *ct, size_t nb)
 {
-  FAR unsigned long *a1;
-  FAR unsigned long *a2;
+  FAR libc_data_t *a1;
+  FAR libc_data_t *a2;
 
   if (nb == 0)
     {
@@ -74,11 +57,11 @@ int strncmp(FAR const char *cs, FAR const char *ct, size_t 
nb)
     {
       /* If cs and ct are word-aligned, compare them a word at a time. */
 
-      a1 = (FAR unsigned long *)cs;
-      a2 = (FAR unsigned long *)ct;
-      while (nb >= LBLOCKSIZE && *a1 == *a2)
+      a1 = (FAR libc_data_t *)cs;
+      a2 = (FAR libc_data_t *)ct;
+      while (nb >= LITTLEBLOCKSIZE && *a1 == *a2)
         {
-          nb -= LBLOCKSIZE;
+          nb -= LITTLEBLOCKSIZE;
 
           /* If we've run out of bytes or hit a null, return zero
            * since we already know *a1 == *a2.
diff --git a/libs/libc/string/lib_bsdstrncpy.c 
b/libs/libc/string/lib_bsdstrncpy.c
index cd7abf688db..adeed181234 100644
--- a/libs/libc/string/lib_bsdstrncpy.c
+++ b/libs/libc/string/lib_bsdstrncpy.c
@@ -33,25 +33,6 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-#define LBLOCKSIZE (sizeof(long))
-
-/* Nonzero if either x or y is not aligned on a "long" boundary. */
-
-#define UNALIGNED(x, y) \
-  (((long)(uintptr_t)(x) & (sizeof(long) - 1)) | ((long)(uintptr_t)(y) & 
(sizeof(long) - 1)))
-
-/* Macros for detecting endchar */
-
-#if LONG_MAX == 2147483647
-#  define DETECTNULL(x) (((x) - 0x01010101) & ~(x) & 0x80808080)
-#elif LONG_MAX == 9223372036854775807
-/* Nonzero if x (a long int) contains a NULL byte. */
-
-#  define DETECTNULL(x) (((x) - 0x0101010101010101) & ~(x) & 
0x8080808080808080)
-#endif
-
-#define TOO_SMALL(len) ((len) < sizeof(long))
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -85,23 +66,23 @@ FAR char *strncpy(FAR char *dest, FAR const char *src, 
size_t n)
 {
   FAR char *dst0 = dest;
   FAR const char *src0 = src;
-  FAR long *aligned_dst;
-  FAR const long *aligned_src;
+  FAR libc_data_t *aligned_dst;
+  FAR const libc_data_t *aligned_src;
 
   /* If src and dest is aligned and n large enough, then copy words. */
 
   if (!UNALIGNED(src0, dst0) && !TOO_SMALL(n))
     {
-      aligned_dst = (FAR long *)dst0;
-      aligned_src = (FAR long *)src0;
+      aligned_dst = (FAR libc_data_t *)dst0;
+      aligned_src = (FAR libc_data_t *)src0;
 
-      /* src and dest are both "long int" aligned, try to do "long int"
+      /* src and dest are both "libc_data_t" aligned, try to do "libc_data_t"
        * sized copies.
        */
 
-      while (n >= LBLOCKSIZE && !DETECTNULL(*aligned_src))
+      while (n >= LITTLEBLOCKSIZE && !DETECTNULL(*aligned_src))
         {
-          n -= LBLOCKSIZE;
+          n -= LITTLEBLOCKSIZE;
           *aligned_dst++ = *aligned_src++;
         }
 

Reply via email to