Mercurial > ecos
annotate packages/language/c/libc/current/src/string/memchr.cxx @ 64:c38311975d4f ecos-sw-2000-01-28
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
| author | jlarmour |
|---|---|
| date | Fri, 28 Jan 2000 04:59:39 +0000 |
| parents | 443894e2e912 |
| children | bf00f99aec69 |
| rev | line source |
|---|---|
| 0 | 1 //=========================================================================== |
| 2 // | |
| 3 // memchr.cxx | |
| 4 // | |
| 5 // ANSI standard memchr() routine | |
| 6 // | |
| 7 //=========================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
9 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
10 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
11 // The contents of this file are subject to the Red Hat eCos Public License |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
13 // compliance with the License. You may obtain a copy of the License at |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
14 // http://sourceware.cygnus.com/ecos |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
15 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
16 // Software distributed under the License is distributed on an |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
18 // License for the specific language governing rights and limitations under |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
19 // the License. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
20 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
21 // The Original Code is eCos - Embedded Configurable Operating System, |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
22 // released September 30, 1998. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
23 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
24 // The Initial Developer of the Original Code is Red Hat. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
25 // Portions created by Red Hat are |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
27 // All Rights Reserved. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
28 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
29 // |
| 0 | 30 //####COPYRIGHTEND#### |
| 31 //=========================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 34 // Author(s): jlarmour | |
| 2 | 35 // Contributors: jlarmour |
| 0 | 36 // Date: 1998-02-13 |
| 37 // Purpose: | |
| 38 // Description: | |
| 39 // Usage: | |
| 40 // | |
| 41 //####DESCRIPTIONEND#### | |
| 42 // | |
| 43 //=========================================================================== | |
| 44 | |
| 45 // CONFIGURATION | |
| 46 | |
| 47 #include <pkgconf/libc.h> // Configuration header | |
| 48 | |
| 49 // Include the C library? | |
| 50 #ifdef CYGPKG_LIBC | |
| 51 | |
| 52 // INCLUDES | |
| 53 | |
| 54 #include <cyg/infra/cyg_type.h> // Common type definitions | |
| 55 #include <cyg/infra/cyg_trac.h> // Tracing support | |
| 56 #include <cyg/infra/cyg_ass.h> // Assertion support | |
| 57 #include <string.h> // Header for this file | |
| 58 #include <stddef.h> // Compiler definitions such as size_t, NULL etc. | |
| 59 #include "clibincl/stringsupp.hxx" // Useful string function support and | |
| 60 // prototypes | |
| 61 | |
| 62 // EXPORTED SYMBOLS | |
| 63 | |
| 64 externC void * | |
| 65 memchr( const void *s, int c, size_t n ) CYGPRI_LIBC_WEAK_ALIAS("_memchr"); | |
| 66 | |
| 67 // FUNCTIONS | |
| 68 | |
| 69 void * | |
| 70 _memchr( const void *s, int c, size_t n ) | |
| 71 { | |
| 72 CYG_REPORT_FUNCNAMETYPE( "_memchr", "returning addr %08x" ); | |
| 73 CYG_REPORT_FUNCARG3( "s=%08x, c=%d, n=%d", s, c, n ); | |
| 74 | |
| 75 CYG_CHECK_DATA_PTR( s, "s is not a valid pointer!" ); | |
| 76 | |
| 77 #if defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || defined(__OPTIMIZE_SIZE__) | |
| 78 const unsigned char *src = (const unsigned char *) s; | |
| 79 | |
| 2 | 80 c &= 0xff; |
| 81 | |
| 0 | 82 while (n--) |
| 83 { | |
| 84 if (*src == c) | |
| 85 { | |
| 86 CYG_REPORT_RETVAL( src ); | |
| 87 return (void *) src; | |
| 88 } // if | |
| 89 src++; | |
| 90 } | |
| 91 CYG_REPORT_RETVAL( NULL ); | |
| 92 return NULL; | |
| 93 #else | |
| 94 const unsigned char *src = (const unsigned char *) s; | |
| 95 CYG_WORD *aligned_src; | |
| 96 CYG_WORD buffer; | |
| 97 CYG_WORD mask; | |
| 98 cyg_ucount8 i; | |
| 2 | 99 |
| 100 c &= 0xff; | |
| 0 | 101 |
| 102 // If the size is small, or src is unaligned, then | |
| 103 // use the bytewise loop. We can hope this is rare. | |
| 104 if (CYG_LIBC_STR_OPT_TOO_SMALL (n) || CYG_LIBC_STR_UNALIGNED (src)) | |
| 105 { | |
| 106 while (n--) | |
| 107 { | |
| 108 if (*src == c) | |
| 109 { | |
| 110 CYG_REPORT_RETVAL( src ); | |
| 111 return (void *) src; | |
| 112 } // if | |
| 113 src++; | |
| 114 } | |
| 115 CYG_REPORT_RETVAL( NULL ); | |
| 116 return NULL; | |
| 117 } | |
| 118 | |
| 119 // The fast code reads the ASCII one word at a time and only | |
| 120 // performs the bytewise search on word-sized segments if they | |
| 121 // contain the search character, which is detected by XORing | |
| 122 // the word-sized segment with a word-sized block of the search | |
| 123 // character and then detecting for the presence of NULL in the | |
| 124 // result. | |
| 125 | |
| 126 aligned_src = (CYG_WORD *) src; | |
| 127 mask = 0; | |
| 128 for (i = 0; i < CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE; i++) | |
| 129 mask = (mask << 8) + c; | |
| 130 | |
| 131 while (n > CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE) | |
| 132 { | |
| 133 buffer = *aligned_src; | |
| 134 buffer ^= mask; | |
| 135 if (CYG_LIBC_STR_DETECTNULL (buffer)) | |
| 136 { | |
| 137 src = (unsigned char*) aligned_src; | |
| 138 for ( i = 0; i < CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE; i++ ) | |
| 139 { | |
| 140 if (*src == c) | |
| 141 { | |
| 142 CYG_REPORT_RETVAL( src ); | |
| 143 return (void *) src; | |
| 144 } // if | |
| 145 src++; | |
| 146 } | |
| 147 } | |
| 148 n -= CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE; | |
| 149 aligned_src++; | |
| 150 } | |
| 151 | |
| 152 // If there are fewer than CYG_LIBC_STR_OPT_LITTLEBLOCKSIZE characters | |
| 153 // left, then we resort to the bytewise loop. | |
| 154 | |
| 155 src = (const unsigned char *) aligned_src; | |
| 156 while (n--) | |
| 157 { | |
| 158 if (*src == c) | |
| 159 { | |
| 160 CYG_REPORT_RETVAL( src ); | |
| 161 return (void *) src; | |
| 162 } // if | |
| 163 src++; | |
| 164 } | |
| 165 | |
| 166 CYG_REPORT_RETVAL( NULL ); | |
| 167 return NULL; | |
| 168 #endif // not defined(CYGIMP_LIBC_STRING_PREFER_SMALL_TO_FAST) || | |
| 169 // defined(__OPTIMIZE_SIZE__) | |
| 170 } // _memchr() | |
| 171 | |
| 172 #endif // ifdef CYGPKG_LIBC | |
| 173 | |
| 174 // EOF memchr.cxx |
