Blame string/memcmp.c

Packit 6c4009
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Torbjorn Granlund (tege@sics.se).
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
#ifdef HAVE_CONFIG_H
Packit 6c4009
# include "config.h"
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#if defined HAVE_STRING_H || defined _LIBC
Packit 6c4009
# include <string.h>
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#undef memcmp
Packit 6c4009
Packit 6c4009
#ifndef MEMCMP
Packit 6c4009
# define MEMCMP memcmp
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
#ifdef _LIBC
Packit 6c4009
Packit 6c4009
# include <memcopy.h>
Packit 6c4009
# include <endian.h>
Packit 6c4009
Packit 6c4009
# if __BYTE_ORDER == __BIG_ENDIAN
Packit 6c4009
#  define WORDS_BIGENDIAN
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
#else	/* Not in the GNU C library.  */
Packit 6c4009
Packit 6c4009
# include <sys/types.h>
Packit 6c4009
Packit 6c4009
/* Type to use for aligned memory operations.
Packit 6c4009
   This should normally be the biggest type supported by a single load
Packit 6c4009
   and store.  Must be an unsigned type.  */
Packit 6c4009
# define op_t	unsigned long int
Packit 6c4009
# define OPSIZ	(sizeof(op_t))
Packit 6c4009
Packit 6c4009
/* Threshold value for when to enter the unrolled loops.  */
Packit 6c4009
# define OP_T_THRES	16
Packit 6c4009
Packit 6c4009
/* Type to use for unaligned operations.  */
Packit 6c4009
typedef unsigned char byte;
Packit 6c4009
Packit 6c4009
# ifndef WORDS_BIGENDIAN
Packit 6c4009
#  define MERGE(w0, sh_1, w1, sh_2) (((w0) >> (sh_1)) | ((w1) << (sh_2)))
Packit 6c4009
# else
Packit 6c4009
#  define MERGE(w0, sh_1, w1, sh_2) (((w0) << (sh_1)) | ((w1) >> (sh_2)))
Packit 6c4009
# endif
Packit 6c4009
Packit 6c4009
#endif	/* In the GNU C library.  */
Packit 6c4009
Packit 6c4009
#ifdef WORDS_BIGENDIAN
Packit 6c4009
# define CMP_LT_OR_GT(a, b) ((a) > (b) ? 1 : -1)
Packit 6c4009
#else
Packit 6c4009
# define CMP_LT_OR_GT(a, b) memcmp_bytes ((a), (b))
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* BE VERY CAREFUL IF YOU CHANGE THIS CODE!  */
Packit 6c4009
Packit 6c4009
/* The strategy of this memcmp is:
Packit 6c4009
Packit 6c4009
   1. Compare bytes until one of the block pointers is aligned.
Packit 6c4009
Packit 6c4009
   2. Compare using memcmp_common_alignment or
Packit 6c4009
      memcmp_not_common_alignment, regarding the alignment of the other
Packit 6c4009
      block after the initial byte operations.  The maximum number of
Packit 6c4009
      full words (of type op_t) are compared in this way.
Packit 6c4009
Packit 6c4009
   3. Compare the few remaining bytes.  */
Packit 6c4009
Packit 6c4009
#ifndef WORDS_BIGENDIAN
Packit 6c4009
/* memcmp_bytes -- Compare A and B bytewise in the byte order of the machine.
Packit 6c4009
   A and B are known to be different.
Packit 6c4009
   This is needed only on little-endian machines.  */
Packit 6c4009
Packit 6c4009
static int memcmp_bytes (op_t, op_t) __THROW;
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
memcmp_bytes (op_t a, op_t b)
Packit 6c4009
{
Packit 6c4009
  long int srcp1 = (long int) &a;
Packit 6c4009
  long int srcp2 = (long int) &b;
Packit 6c4009
  op_t a0, b0;
Packit 6c4009
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      a0 = ((byte *) srcp1)[0];
Packit 6c4009
      b0 = ((byte *) srcp2)[0];
Packit 6c4009
      srcp1 += 1;
Packit 6c4009
      srcp2 += 1;
Packit 6c4009
    }
Packit 6c4009
  while (a0 == b0);
Packit 6c4009
  return a0 - b0;
Packit 6c4009
}
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
static int memcmp_common_alignment (long, long, size_t) __THROW;
Packit 6c4009
Packit 6c4009
/* memcmp_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN `op_t'
Packit 6c4009
   objects (not LEN bytes!).  Both SRCP1 and SRCP2 should be aligned for
Packit 6c4009
   memory operations on `op_t's.  */
Packit 6c4009
static int
Packit 6c4009
memcmp_common_alignment (long int srcp1, long int srcp2, size_t len)
Packit 6c4009
{
Packit 6c4009
  op_t a0, a1;
Packit 6c4009
  op_t b0, b1;
Packit 6c4009
Packit 6c4009
  switch (len % 4)
Packit 6c4009
    {
Packit 6c4009
    default: /* Avoid warning about uninitialized local variables.  */
Packit 6c4009
    case 2:
Packit 6c4009
      a0 = ((op_t *) srcp1)[0];
Packit 6c4009
      b0 = ((op_t *) srcp2)[0];
Packit 6c4009
      srcp1 -= 2 * OPSIZ;
Packit 6c4009
      srcp2 -= 2 * OPSIZ;
Packit 6c4009
      len += 2;
Packit 6c4009
      goto do1;
Packit 6c4009
    case 3:
Packit 6c4009
      a1 = ((op_t *) srcp1)[0];
Packit 6c4009
      b1 = ((op_t *) srcp2)[0];
Packit 6c4009
      srcp1 -= OPSIZ;
Packit 6c4009
      srcp2 -= OPSIZ;
Packit 6c4009
      len += 1;
Packit 6c4009
      goto do2;
Packit 6c4009
    case 0:
Packit 6c4009
      if (OP_T_THRES <= 3 * OPSIZ && len == 0)
Packit 6c4009
	return 0;
Packit 6c4009
      a0 = ((op_t *) srcp1)[0];
Packit 6c4009
      b0 = ((op_t *) srcp2)[0];
Packit 6c4009
      goto do3;
Packit 6c4009
    case 1:
Packit 6c4009
      a1 = ((op_t *) srcp1)[0];
Packit 6c4009
      b1 = ((op_t *) srcp2)[0];
Packit 6c4009
      srcp1 += OPSIZ;
Packit 6c4009
      srcp2 += OPSIZ;
Packit 6c4009
      len -= 1;
Packit 6c4009
      if (OP_T_THRES <= 3 * OPSIZ && len == 0)
Packit 6c4009
	goto do0;
Packit 6c4009
      /* Fall through.  */
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      a0 = ((op_t *) srcp1)[0];
Packit 6c4009
      b0 = ((op_t *) srcp2)[0];
Packit 6c4009
      if (a1 != b1)
Packit 6c4009
	return CMP_LT_OR_GT (a1, b1);
Packit 6c4009
Packit 6c4009
    do3:
Packit 6c4009
      a1 = ((op_t *) srcp1)[1];
Packit 6c4009
      b1 = ((op_t *) srcp2)[1];
Packit 6c4009
      if (a0 != b0)
Packit 6c4009
	return CMP_LT_OR_GT (a0, b0);
Packit 6c4009
Packit 6c4009
    do2:
Packit 6c4009
      a0 = ((op_t *) srcp1)[2];
Packit 6c4009
      b0 = ((op_t *) srcp2)[2];
Packit 6c4009
      if (a1 != b1)
Packit 6c4009
	return CMP_LT_OR_GT (a1, b1);
Packit 6c4009
Packit 6c4009
    do1:
Packit 6c4009
      a1 = ((op_t *) srcp1)[3];
Packit 6c4009
      b1 = ((op_t *) srcp2)[3];
Packit 6c4009
      if (a0 != b0)
Packit 6c4009
	return CMP_LT_OR_GT (a0, b0);
Packit 6c4009
Packit 6c4009
      srcp1 += 4 * OPSIZ;
Packit 6c4009
      srcp2 += 4 * OPSIZ;
Packit 6c4009
      len -= 4;
Packit 6c4009
    }
Packit 6c4009
  while (len != 0);
Packit 6c4009
Packit 6c4009
  /* This is the right position for do0.  Please don't move
Packit 6c4009
     it into the loop.  */
Packit 6c4009
 do0:
Packit 6c4009
  if (a1 != b1)
Packit 6c4009
    return CMP_LT_OR_GT (a1, b1);
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int memcmp_not_common_alignment (long, long, size_t) __THROW;
Packit 6c4009
Packit 6c4009
/* memcmp_not_common_alignment -- Compare blocks at SRCP1 and SRCP2 with LEN
Packit 6c4009
   `op_t' objects (not LEN bytes!).  SRCP2 should be aligned for memory
Packit 6c4009
   operations on `op_t', but SRCP1 *should be unaligned*.  */
Packit 6c4009
static int
Packit 6c4009
memcmp_not_common_alignment (long int srcp1, long int srcp2, size_t len)
Packit 6c4009
{
Packit 6c4009
  op_t a0, a1, a2, a3;
Packit 6c4009
  op_t b0, b1, b2, b3;
Packit 6c4009
  op_t x;
Packit 6c4009
  int shl, shr;
Packit 6c4009
Packit 6c4009
  /* Calculate how to shift a word read at the memory operation
Packit 6c4009
     aligned srcp1 to make it aligned for comparison.  */
Packit 6c4009
Packit 6c4009
  shl = 8 * (srcp1 % OPSIZ);
Packit 6c4009
  shr = 8 * OPSIZ - shl;
Packit 6c4009
Packit 6c4009
  /* Make SRCP1 aligned by rounding it down to the beginning of the `op_t'
Packit 6c4009
     it points in the middle of.  */
Packit 6c4009
  srcp1 &= -OPSIZ;
Packit 6c4009
Packit 6c4009
  switch (len % 4)
Packit 6c4009
    {
Packit 6c4009
    default: /* Avoid warning about uninitialized local variables.  */
Packit 6c4009
    case 2:
Packit 6c4009
      a1 = ((op_t *) srcp1)[0];
Packit 6c4009
      a2 = ((op_t *) srcp1)[1];
Packit 6c4009
      b2 = ((op_t *) srcp2)[0];
Packit 6c4009
      srcp1 -= 1 * OPSIZ;
Packit 6c4009
      srcp2 -= 2 * OPSIZ;
Packit 6c4009
      len += 2;
Packit 6c4009
      goto do1;
Packit 6c4009
    case 3:
Packit 6c4009
      a0 = ((op_t *) srcp1)[0];
Packit 6c4009
      a1 = ((op_t *) srcp1)[1];
Packit 6c4009
      b1 = ((op_t *) srcp2)[0];
Packit 6c4009
      srcp2 -= 1 * OPSIZ;
Packit 6c4009
      len += 1;
Packit 6c4009
      goto do2;
Packit 6c4009
    case 0:
Packit 6c4009
      if (OP_T_THRES <= 3 * OPSIZ && len == 0)
Packit 6c4009
	return 0;
Packit 6c4009
      a3 = ((op_t *) srcp1)[0];
Packit 6c4009
      a0 = ((op_t *) srcp1)[1];
Packit 6c4009
      b0 = ((op_t *) srcp2)[0];
Packit 6c4009
      srcp1 += 1 * OPSIZ;
Packit 6c4009
      goto do3;
Packit 6c4009
    case 1:
Packit 6c4009
      a2 = ((op_t *) srcp1)[0];
Packit 6c4009
      a3 = ((op_t *) srcp1)[1];
Packit 6c4009
      b3 = ((op_t *) srcp2)[0];
Packit 6c4009
      srcp1 += 2 * OPSIZ;
Packit 6c4009
      srcp2 += 1 * OPSIZ;
Packit 6c4009
      len -= 1;
Packit 6c4009
      if (OP_T_THRES <= 3 * OPSIZ && len == 0)
Packit 6c4009
	goto do0;
Packit 6c4009
      /* Fall through.  */
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      a0 = ((op_t *) srcp1)[0];
Packit 6c4009
      b0 = ((op_t *) srcp2)[0];
Packit 6c4009
      x = MERGE(a2, shl, a3, shr);
Packit 6c4009
      if (x != b3)
Packit 6c4009
	return CMP_LT_OR_GT (x, b3);
Packit 6c4009
Packit 6c4009
    do3:
Packit 6c4009
      a1 = ((op_t *) srcp1)[1];
Packit 6c4009
      b1 = ((op_t *) srcp2)[1];
Packit 6c4009
      x = MERGE(a3, shl, a0, shr);
Packit 6c4009
      if (x != b0)
Packit 6c4009
	return CMP_LT_OR_GT (x, b0);
Packit 6c4009
Packit 6c4009
    do2:
Packit 6c4009
      a2 = ((op_t *) srcp1)[2];
Packit 6c4009
      b2 = ((op_t *) srcp2)[2];
Packit 6c4009
      x = MERGE(a0, shl, a1, shr);
Packit 6c4009
      if (x != b1)
Packit 6c4009
	return CMP_LT_OR_GT (x, b1);
Packit 6c4009
Packit 6c4009
    do1:
Packit 6c4009
      a3 = ((op_t *) srcp1)[3];
Packit 6c4009
      b3 = ((op_t *) srcp2)[3];
Packit 6c4009
      x = MERGE(a1, shl, a2, shr);
Packit 6c4009
      if (x != b2)
Packit 6c4009
	return CMP_LT_OR_GT (x, b2);
Packit 6c4009
Packit 6c4009
      srcp1 += 4 * OPSIZ;
Packit 6c4009
      srcp2 += 4 * OPSIZ;
Packit 6c4009
      len -= 4;
Packit 6c4009
    }
Packit 6c4009
  while (len != 0);
Packit 6c4009
Packit 6c4009
  /* This is the right position for do0.  Please don't move
Packit 6c4009
     it into the loop.  */
Packit 6c4009
 do0:
Packit 6c4009
  x = MERGE(a2, shl, a3, shr);
Packit 6c4009
  if (x != b3)
Packit 6c4009
    return CMP_LT_OR_GT (x, b3);
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
MEMCMP (const void *s1, const void *s2, size_t len)
Packit 6c4009
{
Packit 6c4009
  op_t a0;
Packit 6c4009
  op_t b0;
Packit 6c4009
  long int srcp1 = (long int) s1;
Packit 6c4009
  long int srcp2 = (long int) s2;
Packit 6c4009
  op_t res;
Packit 6c4009
Packit 6c4009
  if (len >= OP_T_THRES)
Packit 6c4009
    {
Packit 6c4009
      /* There are at least some bytes to compare.  No need to test
Packit 6c4009
	 for LEN == 0 in this alignment loop.  */
Packit 6c4009
      while (srcp2 % OPSIZ != 0)
Packit 6c4009
	{
Packit 6c4009
	  a0 = ((byte *) srcp1)[0];
Packit 6c4009
	  b0 = ((byte *) srcp2)[0];
Packit 6c4009
	  srcp1 += 1;
Packit 6c4009
	  srcp2 += 1;
Packit 6c4009
	  res = a0 - b0;
Packit 6c4009
	  if (res != 0)
Packit 6c4009
	    return res;
Packit 6c4009
	  len -= 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* SRCP2 is now aligned for memory operations on `op_t'.
Packit 6c4009
	 SRCP1 alignment determines if we can do a simple,
Packit 6c4009
	 aligned compare or need to shuffle bits.  */
Packit 6c4009
Packit 6c4009
      if (srcp1 % OPSIZ == 0)
Packit 6c4009
	res = memcmp_common_alignment (srcp1, srcp2, len / OPSIZ);
Packit 6c4009
      else
Packit 6c4009
	res = memcmp_not_common_alignment (srcp1, srcp2, len / OPSIZ);
Packit 6c4009
      if (res != 0)
Packit 6c4009
	return res;
Packit 6c4009
Packit 6c4009
      /* Number of bytes remaining in the interval [0..OPSIZ-1].  */
Packit 6c4009
      srcp1 += len & -OPSIZ;
Packit 6c4009
      srcp2 += len & -OPSIZ;
Packit 6c4009
      len %= OPSIZ;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* There are just a few bytes to compare.  Use byte memory operations.  */
Packit 6c4009
  while (len != 0)
Packit 6c4009
    {
Packit 6c4009
      a0 = ((byte *) srcp1)[0];
Packit 6c4009
      b0 = ((byte *) srcp2)[0];
Packit 6c4009
      srcp1 += 1;
Packit 6c4009
      srcp2 += 1;
Packit 6c4009
      res = a0 - b0;
Packit 6c4009
      if (res != 0)
Packit 6c4009
	return res;
Packit 6c4009
      len -= 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
libc_hidden_builtin_def(memcmp)
Packit 6c4009
#ifdef weak_alias
Packit 6c4009
# undef bcmp
Packit 6c4009
weak_alias (memcmp, bcmp)
Packit 6c4009
#endif