root/lib/memrchr.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. __memrchr

     1 /* memrchr -- find the last occurrence of a byte in a memory block
     2 
     3    Copyright (C) 1991, 1993, 1996-1997, 1999-2000, 2003-2023 Free
     4    Software Foundation, Inc.
     5 
     6    Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
     7    with help from Dan Sahlin (dan@sics.se) and
     8    commentary by Jim Blandy (jimb@ai.mit.edu);
     9    adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
    10    and implemented by Roland McGrath (roland@ai.mit.edu).
    11 
    12    This file is free software: you can redistribute it and/or modify
    13    it under the terms of the GNU Lesser General Public License as
    14    published by the Free Software Foundation, either version 3 of the
    15    License, or (at your option) any later version.
    16 
    17    This file is distributed in the hope that it will be useful,
    18    but WITHOUT ANY WARRANTY; without even the implied warranty of
    19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    20    GNU Lesser General Public License for more details.
    21 
    22    You should have received a copy of the GNU Lesser General Public License
    23    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
    24 
    25 #if defined _LIBC
    26 # include <memcopy.h>
    27 #else
    28 # include <config.h>
    29 # define reg_char char
    30 #endif
    31 
    32 #include <string.h>
    33 #include <limits.h>
    34 
    35 #undef __memrchr
    36 #ifdef _LIBC
    37 # undef memrchr
    38 #endif
    39 
    40 #ifndef weak_alias
    41 # define __memrchr memrchr
    42 #endif
    43 
    44 /* Search no more than N bytes of S for C.  */
    45 void *
    46 __memrchr (void const *s, int c_in, size_t n)
    47 {
    48   /* On 32-bit hardware, choosing longword to be a 32-bit unsigned
    49      long instead of a 64-bit uintmax_t tends to give better
    50      performance.  On 64-bit hardware, unsigned long is generally 64
    51      bits already.  Change this typedef to experiment with
    52      performance.  */
    53   typedef unsigned long int longword;
    54 
    55   const unsigned char *char_ptr;
    56   const longword *longword_ptr;
    57   longword repeated_one;
    58   longword repeated_c;
    59   unsigned reg_char c;
    60 
    61   c = (unsigned char) c_in;
    62 
    63   /* Handle the last few bytes by reading one byte at a time.
    64      Do this until CHAR_PTR is aligned on a longword boundary.  */
    65   for (char_ptr = (const unsigned char *) s + n;
    66        n > 0 && (size_t) char_ptr % sizeof (longword) != 0;
    67        --n)
    68     if (*--char_ptr == c)
    69       return (void *) char_ptr;
    70 
    71   longword_ptr = (const void *) char_ptr;
    72 
    73   /* All these elucidatory comments refer to 4-byte longwords,
    74      but the theory applies equally well to any size longwords.  */
    75 
    76   /* Compute auxiliary longword values:
    77      repeated_one is a value which has a 1 in every byte.
    78      repeated_c has c in every byte.  */
    79   repeated_one = 0x01010101;
    80   repeated_c = c | (c << 8);
    81   repeated_c |= repeated_c << 16;
    82   if (0xffffffffU < (longword) -1)
    83     {
    84       repeated_one |= repeated_one << 31 << 1;
    85       repeated_c |= repeated_c << 31 << 1;
    86       if (8 < sizeof (longword))
    87         {
    88           size_t i;
    89 
    90           for (i = 64; i < sizeof (longword) * 8; i *= 2)
    91             {
    92               repeated_one |= repeated_one << i;
    93               repeated_c |= repeated_c << i;
    94             }
    95         }
    96     }
    97 
    98   /* Instead of the traditional loop which tests each byte, we will test a
    99      longword at a time.  The tricky part is testing if *any of the four*
   100      bytes in the longword in question are equal to c.  We first use an xor
   101      with repeated_c.  This reduces the task to testing whether *any of the
   102      four* bytes in longword1 is zero.
   103 
   104      We compute tmp =
   105        ((longword1 - repeated_one) & ~longword1) & (repeated_one << 7).
   106      That is, we perform the following operations:
   107        1. Subtract repeated_one.
   108        2. & ~longword1.
   109        3. & a mask consisting of 0x80 in every byte.
   110      Consider what happens in each byte:
   111        - If a byte of longword1 is zero, step 1 and 2 transform it into 0xff,
   112          and step 3 transforms it into 0x80.  A carry can also be propagated
   113          to more significant bytes.
   114        - If a byte of longword1 is nonzero, let its lowest 1 bit be at
   115          position k (0 <= k <= 7); so the lowest k bits are 0.  After step 1,
   116          the byte ends in a single bit of value 0 and k bits of value 1.
   117          After step 2, the result is just k bits of value 1: 2^k - 1.  After
   118          step 3, the result is 0.  And no carry is produced.
   119      So, if longword1 has only non-zero bytes, tmp is zero.
   120      Whereas if longword1 has a zero byte, call j the position of the least
   121      significant zero byte.  Then the result has a zero at positions 0, ...,
   122      j-1 and a 0x80 at position j.  We cannot predict the result at the more
   123      significant bytes (positions j+1..3), but it does not matter since we
   124      already have a non-zero bit at position 8*j+7.
   125 
   126      So, the test whether any byte in longword1 is zero is equivalent to
   127      testing whether tmp is nonzero.  */
   128 
   129   while (n >= sizeof (longword))
   130     {
   131       longword longword1 = *--longword_ptr ^ repeated_c;
   132 
   133       if ((((longword1 - repeated_one) & ~longword1)
   134            & (repeated_one << 7)) != 0)
   135         {
   136           longword_ptr++;
   137           break;
   138         }
   139       n -= sizeof (longword);
   140     }
   141 
   142   char_ptr = (const unsigned char *) longword_ptr;
   143 
   144   /* At this point, we know that either n < sizeof (longword), or one of the
   145      sizeof (longword) bytes starting at char_ptr is == c.  On little-endian
   146      machines, we could determine the first such byte without any further
   147      memory accesses, just by looking at the tmp result from the last loop
   148      iteration.  But this does not work on big-endian machines.  Choose code
   149      that works in both cases.  */
   150 
   151   while (n-- > 0)
   152     {
   153       if (*--char_ptr == c)
   154         return (void *) char_ptr;
   155     }
   156 
   157   return NULL;
   158 }
   159 #ifdef weak_alias
   160 weak_alias (__memrchr, memrchr)
   161 #endif

/* [<][>][^][v][top][bottom][index][help] */