Blame string/strlen.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Written by Torbjorn Granlund (tege@sics.se),
Packit 6c4009
   with help from Dan Sahlin (dan@sics.se);
Packit 6c4009
   commentary by Jim Blandy (jimb@ai.mit.edu).
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
Packit 6c4009
#undef strlen
Packit 6c4009
Packit 6c4009
#ifndef STRLEN
Packit 6c4009
# define STRLEN strlen
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Return the length of the null-terminated string STR.  Scan for
Packit 6c4009
   the null terminator quickly by testing four bytes at a time.  */
Packit 6c4009
size_t
Packit 6c4009
STRLEN (const char *str)
Packit 6c4009
{
Packit 6c4009
  const char *char_ptr;
Packit 6c4009
  const unsigned long int *longword_ptr;
Packit 6c4009
  unsigned long int longword, himagic, lomagic;
Packit 6c4009
Packit 6c4009
  /* Handle the first few characters by reading one character at a time.
Packit 6c4009
     Do this until CHAR_PTR is aligned on a longword boundary.  */
Packit 6c4009
  for (char_ptr = str; ((unsigned long int) char_ptr
Packit 6c4009
			& (sizeof (longword) - 1)) != 0;
Packit 6c4009
       ++char_ptr)
Packit 6c4009
    if (*char_ptr == '\0')
Packit 6c4009
      return char_ptr - str;
Packit 6c4009
Packit 6c4009
  /* All these elucidatory comments refer to 4-byte longwords,
Packit 6c4009
     but the theory applies equally well to 8-byte longwords.  */
Packit 6c4009
Packit 6c4009
  longword_ptr = (unsigned long int *) char_ptr;
Packit 6c4009
Packit 6c4009
  /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
Packit 6c4009
     the "holes."  Note that there is a hole just to the left of
Packit 6c4009
     each byte, with an extra at the end:
Packit 6c4009
Packit 6c4009
     bits:  01111110 11111110 11111110 11111111
Packit 6c4009
     bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
Packit 6c4009
Packit 6c4009
     The 1-bits make sure that carries propagate to the next 0-bit.
Packit 6c4009
     The 0-bits provide holes for carries to fall into.  */
Packit 6c4009
  himagic = 0x80808080L;
Packit 6c4009
  lomagic = 0x01010101L;
Packit 6c4009
  if (sizeof (longword) > 4)
Packit 6c4009
    {
Packit 6c4009
      /* 64-bit version of the magic.  */
Packit 6c4009
      /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
Packit 6c4009
      himagic = ((himagic << 16) << 16) | himagic;
Packit 6c4009
      lomagic = ((lomagic << 16) << 16) | lomagic;
Packit 6c4009
    }
Packit 6c4009
  if (sizeof (longword) > 8)
Packit 6c4009
    abort ();
Packit 6c4009
Packit 6c4009
  /* Instead of the traditional loop which tests each character,
Packit 6c4009
     we will test a longword at a time.  The tricky part is testing
Packit 6c4009
     if *any of the four* bytes in the longword in question are zero.  */
Packit 6c4009
  for (;;)
Packit 6c4009
    {
Packit 6c4009
      longword = *longword_ptr++;
Packit 6c4009
Packit 6c4009
      if (((longword - lomagic) & ~longword & himagic) != 0)
Packit 6c4009
	{
Packit 6c4009
	  /* Which of the bytes was the zero?  If none of them were, it was
Packit 6c4009
	     a misfire; continue the search.  */
Packit 6c4009
Packit 6c4009
	  const char *cp = (const char *) (longword_ptr - 1);
Packit 6c4009
Packit 6c4009
	  if (cp[0] == 0)
Packit 6c4009
	    return cp - str;
Packit 6c4009
	  if (cp[1] == 0)
Packit 6c4009
	    return cp - str + 1;
Packit 6c4009
	  if (cp[2] == 0)
Packit 6c4009
	    return cp - str + 2;
Packit 6c4009
	  if (cp[3] == 0)
Packit 6c4009
	    return cp - str + 3;
Packit 6c4009
	  if (sizeof (longword) > 4)
Packit 6c4009
	    {
Packit 6c4009
	      if (cp[4] == 0)
Packit 6c4009
		return cp - str + 4;
Packit 6c4009
	      if (cp[5] == 0)
Packit 6c4009
		return cp - str + 5;
Packit 6c4009
	      if (cp[6] == 0)
Packit 6c4009
		return cp - str + 6;
Packit 6c4009
	      if (cp[7] == 0)
Packit 6c4009
		return cp - str + 7;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
libc_hidden_builtin_def (strlen)