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

Subversion Repositories or1k

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

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

Line No. Rev Author Line
1 1010 ivang
/*
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
#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
75
  _CONST unsigned char *src = (_CONST unsigned char *) src_void;
76
 
77
  c &= 0xff;
78
 
79
  while (length--)
80
    {
81
      if (*src == c)
82
        return (char *) src;
83
      src++;
84
    }
85
  return NULL;
86
#else
87
  _CONST unsigned char *src = (_CONST unsigned char *) src_void;
88
  unsigned long *asrc;
89
  unsigned long  buffer;
90
  unsigned long  mask;
91
  int i, j;
92
 
93
  c &= 0xff;
94
 
95
  /* If the size is small, or src is unaligned, then
96
     use the bytewise loop.  We can hope this is rare.  */
97
  if (!TOO_SMALL (length) && !UNALIGNED (src))
98
    {
99
      /* The fast code reads the ASCII one word at a time and only
100
         performs the bytewise search on word-sized segments if they
101
         contain the search character, which is detected by XORing
102
         the word-sized segment with a word-sized block of the search
103
         character and then detecting for the presence of NULL in the
104
         result.  */
105
      asrc = (unsigned long*) src;
106
      mask = 0;
107
      for (i = 0; i < LBLOCKSIZE; i++)
108
        mask = (mask << 8) + c;
109
 
110
      while (length >= LBLOCKSIZE)
111
        {
112
          buffer = *asrc;
113
          buffer ^=  mask;
114
          if (DETECTNULL (buffer))
115
            {
116
              src = (unsigned char*) asrc;
117
              for ( j = 0; j < LBLOCKSIZE; j++ )
118
                {
119
                  if (*src == c)
120
                    return (char*) src;
121
                  src++;
122
                }
123
            }
124
          length -= LBLOCKSIZE;
125
          asrc++;
126
        }
127
 
128
      /* If there are fewer than LBLOCKSIZE characters left,
129
         then we resort to the bytewise loop.  */
130
 
131
      src = (unsigned char*) asrc;
132
    }
133
 
134
  while (length--)
135
    {
136
      if (*src == c)
137
        return (char*) src;
138
      src++;
139
    }
140
 
141
  return NULL;
142
#endif /* not PREFER_SIZE_OVER_SPEED */
143
}

powered by: WebSVN 2.1.0

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