OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib/] [newlib/] [libc/] [string/] [memchr.c] - Blame information for rev 39

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.