[2/6] newlib: memccpy: unify mask filling with other memory functions

Message ID 23531015089930bb0165eeb69a6b5e24d6c5a3f7.camel@espressif.com
State New
Headers
Series Refactor and optimize string/memory functions |

Commit Message

Alexey Lapshin Jan. 27, 2025, 10:45 a.m. UTC
  This change made just to have memccpy like others mem-functions
---
 newlib/libc/string/memccpy.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

-- 
2.43.0
  

Comments

Corinna Vinschen Jan. 28, 2025, 4:08 p.m. UTC | #1
On Jan 27 10:45, Alexey Lapshin wrote:
> This change made just to have memccpy like others mem-functions
> ---
>  newlib/libc/string/memccpy.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/newlib/libc/string/memccpy.c b/newlib/libc/string/memccpy.c
> index 332332489..802723a2e 100644
> --- a/newlib/libc/string/memccpy.c
> +++ b/newlib/libc/string/memccpy.c
> @@ -69,7 +69,7 @@ memccpy (void *__restrict dst0,
>    if (!TOO_SMALL_LITTLE_BLOCK(len0) && !UNALIGNED_X_Y(src, dst))
>      {
>        unsigned int i;
> -      unsigned long mask = 0;
> +      unsigned long mask;
>  
>        aligned_dst = (long*)dst;
>        aligned_src = (long*)src;
> @@ -80,9 +80,10 @@ memccpy (void *__restrict dst0,
>           the word-sized segment with a word-sized block of the search
>           character and then detecting for the presence of NULL in the
>           result.  */
> -      for (i = 0; i < sizeof(mask); i++)
> -        mask = (mask << 8) + endchar;
> +      mask = endchar << 8 | endchar;
> +      mask = mask << 16 | mask;
> +      for (i = 32; i < sizeof(mask) * 8; i <<= 1)
> +        mask = (mask << i) | endchar;
                                ^^^^^^^
                                Shouldn't that be mask?


Corinna
  

Patch

diff --git a/newlib/libc/string/memccpy.c b/newlib/libc/string/memccpy.c
index 332332489..802723a2e 100644
--- a/newlib/libc/string/memccpy.c
+++ b/newlib/libc/string/memccpy.c
@@ -69,7 +69,7 @@  memccpy (void *__restrict dst0,
   if (!TOO_SMALL_LITTLE_BLOCK(len0) && !UNALIGNED_X_Y(src, dst))
     {
       unsigned int i;
-      unsigned long mask = 0;
+      unsigned long mask;
 
       aligned_dst = (long*)dst;
       aligned_src = (long*)src;
@@ -80,9 +80,10 @@  memccpy (void *__restrict dst0,
          the word-sized segment with a word-sized block of the search
          character and then detecting for the presence of NULL in the
          result.  */
-      for (i = 0; i < sizeof(mask); i++)
-        mask = (mask << 8) + endchar;
-
+      mask = endchar << 8 | endchar;
+      mask = mask << 16 | mask;
+      for (i = 32; i < sizeof(mask) * 8; i <<= 1)
+        mask = (mask << i) | endchar;
 
       /* Copy one long word at a time if possible.  */
       while (!TOO_SMALL_LITTLE_BLOCK(len0))