1/* Copyright (C) 1991 Free Software Foundation, Inc.
2   Based on strlen implemention by Torbjorn Granlund (tege@sics.se),
3   with help from Dan Sahlin (dan@sics.se) and
4   commentary by Jim Blandy (jimb@ai.mit.edu);
5   adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
6   and implemented by Roland McGrath (roland@ai.mit.edu).
7
8The GNU C Library is free software; you can redistribute it and/or
9modify it under the terms of the GNU Library General Public License as
10published by the Free Software Foundation; either version 2 of the
11License, or (at your option) any later version.
12
13The GNU C Library is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16Library General Public License for more details.
17
18You should have received a copy of the GNU Library General Public
19License along with the GNU C Library; see the file COPYING.LIB.  If
20not, write to the Free Software Foundation, Inc., 59 Temple Place -
21Suite 330, Boston, MA 02111-1307, USA.
22
23This file was modified slightly by Ian Lance Taylor, May 1992, for
24Taylor UUCP.  It assumes 32 bit longs.  I'm willing to trust that any
25system which does not have 32 bit longs will have its own
26implementation of memchr.  */
27
28#include "uucp.h"
29
30/* Search no more than N bytes of S for C.  */
31
32pointer
33memchr (s, c, n)
34     constpointer s;
35     int c;
36     size_t n;
37{
38  const char *char_ptr;
39  const unsigned long int *longword_ptr;
40  unsigned long int longword, magic_bits, charmask;
41
42  c = BUCHAR (c);
43
44  /* Handle the first few characters by reading one character at a time.
45     Do this until CHAR_PTR is aligned on a 4-byte border.  */
46  for (char_ptr = s; n > 0 && ((unsigned long int) char_ptr & 3) != 0;
47       --n, ++char_ptr)
48    if (BUCHAR (*char_ptr) == c)
49      return (pointer) char_ptr;
50
51  longword_ptr = (unsigned long int *) char_ptr;
52
53  /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
54     the "holes."  Note that there is a hole just to the left of
55     each byte, with an extra at the end:
56
57     bits:  01111110 11111110 11111110 11111111
58     bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
59
60     The 1-bits make sure that carries propagate to the next 0-bit.
61     The 0-bits provide holes for carries to fall into.  */
62  magic_bits = 0x7efefeff;
63
64  /* Set up a longword, each of whose bytes is C.  */
65  charmask = c | (c << 8);
66  charmask |= charmask << 16;
67
68  /* Instead of the traditional loop which tests each character,
69     we will test a longword at a time.  The tricky part is testing
70     if *any of the four* bytes in the longword in question are zero.  */
71  while (n >= 4)
72    {
73      /* We tentatively exit the loop if adding MAGIC_BITS to
74	 LONGWORD fails to change any of the hole bits of LONGWORD.
75
76	 1) Is this safe?  Will it catch all the zero bytes?
77	 Suppose there is a byte with all zeros.  Any carry bits
78	 propagating from its left will fall into the hole at its
79	 least significant bit and stop.  Since there will be no
80	 carry from its most significant bit, the LSB of the
81	 byte to the left will be unchanged, and the zero will be
82	 detected.
83
84	 2) Is this worthwhile?  Will it ignore everything except
85	 zero bytes?  Suppose every byte of LONGWORD has a bit set
86	 somewhere.  There will be a carry into bit 8.  If bit 8
87	 is set, this will carry into bit 16.  If bit 8 is clear,
88	 one of bits 9-15 must be set, so there will be a carry
89	 into bit 16.  Similarly, there will be a carry into bit
90	 24.  If one of bits 24-30 is set, there will be a carry
91	 into bit 31, so all of the hole bits will be changed.
92
93	 The one misfire occurs when bits 24-30 are clear and bit
94	 31 is set; in this case, the hole at bit 31 is not
95	 changed.  If we had access to the processor carry flag,
96	 we could close this loophole by putting the fourth hole
97	 at bit 32!
98
99	 So it ignores everything except 128's, when they're aligned
100	 properly.
101
102	 3) But wait!  Aren't we looking for C, not zero?
103	 Good point.  So what we do is XOR LONGWORD with a longword,
104	 each of whose bytes is C.  This turns each byte that is C
105	 into a zero.  */
106
107      longword = *longword_ptr++ ^ charmask;
108
109      /* Add MAGIC_BITS to LONGWORD.  */
110      if ((((longword + magic_bits)
111
112	    /* Set those bits that were unchanged by the addition.  */
113	    ^ ~longword)
114
115	   /* Look at only the hole bits.  If any of the hole bits
116	      are unchanged, most likely one of the bytes was a
117	      zero.  */
118	   & ~magic_bits) != 0)
119	{
120	  /* Which of the bytes was C?  If none of them were, it was
121	     a misfire; continue the search.  */
122
123	  const char *cp = (const char *) (longword_ptr - 1);
124
125	  if (BUCHAR (cp[0]) == c)
126	    return (pointer) cp;
127	  if (BUCHAR (cp[1]) == c)
128	    return (pointer) &cp[1];
129	  if (BUCHAR (cp[2]) == c)
130	    return (pointer) &cp[2];
131	  if (BUCHAR (cp[3]) == c)
132	    return (pointer) &cp[3];
133	}
134
135      n -= 4;
136    }
137
138  char_ptr = (const char *) longword_ptr;
139
140  while (n-- > 0)
141    {
142      if (BUCHAR (*char_ptr) == c)
143	return (pointer) char_ptr;
144      else
145	++char_ptr;
146    }
147
148  return NULL;
149}
150