source: trunk/libs/newlib/src/newlib/libc/string/memchr.c @ 444

Last change on this file since 444 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 3.1 KB
Line 
1/*
2FUNCTION
3        <<memchr>>---find character in memory
4
5INDEX
6        memchr
7
8SYNOPSIS
9        #include <string.h>
10        void *memchr(const void *<[src]>, int <[c]>, size_t <[length]>);
11
12DESCRIPTION
13        This function searches memory starting at <<*<[src]>>> for the
14        character <[c]>.  The search only ends with the first
15        occurrence of <[c]>, or after <[length]> characters; in
16        particular, <<NUL>> does not terminate the search.
17
18RETURNS
19        If the character <[c]> is found within <[length]> characters
20        of <<*<[src]>>>, a pointer to the character is returned. If
21        <[c]> is not found, then <<NULL>> is returned.
22
23PORTABILITY
24<<memchr>> is ANSI C.
25
26<<memchr>> requires no supporting OS subroutines.
27
28QUICKREF
29        memchr ansi pure
30*/
31
32#include <_ansi.h>
33#include <string.h>
34#include <limits.h>
35
36/* Nonzero if either X or Y is not aligned on a "long" boundary.  */
37#define UNALIGNED(X) ((long)X & (sizeof (long) - 1))
38
39/* How many bytes are loaded each iteration of the word copy loop.  */
40#define LBLOCKSIZE (sizeof (long))
41
42/* Threshhold for punting to the bytewise iterator.  */
43#define TOO_SMALL(LEN)  ((LEN) < LBLOCKSIZE)
44
45#if LONG_MAX == 2147483647L
46#define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
47#else
48#if LONG_MAX == 9223372036854775807L
49/* Nonzero if X (a long int) contains a NULL byte. */
50#define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
51#else
52#error long int is not a 32bit or 64bit type.
53#endif
54#endif
55
56#ifndef DETECTNULL
57#error long int is not a 32bit or 64bit byte
58#endif
59
60/* DETECTCHAR returns nonzero if (long)X contains the byte used
61   to fill (long)MASK. */
62#define DETECTCHAR(X,MASK) (DETECTNULL(X ^ MASK))
63
64void *
65memchr (const void *src_void,
66        int c,
67        size_t length)
68{
69  const unsigned char *src = (const unsigned char *) src_void;
70  unsigned char d = c;
71
72#if !defined(PREFER_SIZE_OVER_SPEED) && !defined(__OPTIMIZE_SIZE__)
73  unsigned long *asrc;
74  unsigned long  mask;
75  unsigned int i;
76
77  while (UNALIGNED (src))
78    {
79      if (!length--)
80        return NULL;
81      if (*src == d)
82        return (void *) src;
83      src++;
84    }
85
86  if (!TOO_SMALL (length))
87    {
88      /* If we get this far, we know that length is large and src is
89         word-aligned. */
90      /* The fast code reads the source one word at a time and only
91         performs the bytewise search on word-sized segments if they
92         contain the search character, which is detected by XORing
93         the word-sized segment with a word-sized block of the search
94         character and then detecting for the presence of NUL in the
95         result.  */
96      asrc = (unsigned long *) src;
97      mask = d << 8 | d;
98      mask = mask << 16 | mask;
99      for (i = 32; i < LBLOCKSIZE * 8; i <<= 1)
100        mask = (mask << i) | mask;
101
102      while (length >= LBLOCKSIZE)
103        {
104          if (DETECTCHAR (*asrc, mask))
105            break;
106          length -= LBLOCKSIZE;
107          asrc++;
108        }
109
110      /* If there are fewer than LBLOCKSIZE characters left,
111         then we resort to the bytewise loop.  */
112
113      src = (unsigned char *) asrc;
114    }
115
116#endif /* not PREFER_SIZE_OVER_SPEED */
117
118  while (length--)
119    {
120      if (*src == d)
121        return (void *) src;
122      src++;
123    }
124
125  return NULL;
126}
Note: See TracBrowser for help on using the repository browser.