diff options
author | Thomas Martitz <kugel@rockbox.org> | 2010-05-06 21:04:40 +0000 |
---|---|---|
committer | Thomas Martitz <kugel@rockbox.org> | 2010-05-06 21:04:40 +0000 |
commit | 50a6ca39ad4ed01922aa4f755f0ca579788226cf (patch) | |
tree | c7881b015b220558167310345b162324c96be15a /firmware/libc/memchr.c | |
parent | adb506df14aded06ed6e9ebf8540e6fd383ffd6a (diff) | |
download | rockbox-50a6ca39ad4ed01922aa4f755f0ca579788226cf.tar.gz rockbox-50a6ca39ad4ed01922aa4f755f0ca579788226cf.zip |
Move c/h files implementing/defining standard library stuff into a new libc directory, also standard'ify some parts of the code base (almost entirely #include fixes).
This is to a) to cleanup firmware/common and firmware/include a bit, but also b) for Rockbox as an application which should use the host system's c library and headers, separating makes it easy to exclude our files from the build.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@25850 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/libc/memchr.c')
-rw-r--r-- | firmware/libc/memchr.c | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/firmware/libc/memchr.c b/firmware/libc/memchr.c new file mode 100644 index 0000000000..26bdb9eea3 --- /dev/null +++ b/firmware/libc/memchr.c | |||
@@ -0,0 +1,116 @@ | |||
1 | /* | ||
2 | FUNCTION | ||
3 | <<memchr>>---search for character in memory | ||
4 | |||
5 | INDEX | ||
6 | memchr | ||
7 | |||
8 | ANSI_SYNOPSIS | ||
9 | #include <string.h> | ||
10 | void * memchr(const void *<[s1]>, int <[c]>, size_t <[n]>); | ||
11 | |||
12 | TRAD_SYNOPSIS | ||
13 | #include <string.h> | ||
14 | void * memchr(<[s1]>, <[c]>, <[n]>); | ||
15 | void *<[string]>; | ||
16 | int *<[c]>; | ||
17 | size_t *<[n]>; | ||
18 | |||
19 | DESCRIPTION | ||
20 | This function scans the first <[n]> bytes of the memory pointed | ||
21 | to by <[s1]> for the character <[c]> (converted to a char). | ||
22 | |||
23 | RETURNS | ||
24 | Returns a pointer to the matching byte, or a null pointer if | ||
25 | <[c]> does not occur in <[s1]>. | ||
26 | |||
27 | PORTABILITY | ||
28 | <<memchr>> is ANSI C. | ||
29 | |||
30 | <<memchr>> requires no supporting OS subroutines. | ||
31 | |||
32 | QUICKREF | ||
33 | memchr ansi pure | ||
34 | */ | ||
35 | |||
36 | #include <string.h> | ||
37 | #include <limits.h> | ||
38 | |||
39 | /* Nonzero if X is not aligned on a "long" boundary. */ | ||
40 | #define UNALIGNED(X) ((long)X & (sizeof (long) - 1)) | ||
41 | |||
42 | /* How many bytes are loaded each iteration of the word copy loop. */ | ||
43 | #define LBLOCKSIZE (sizeof (long)) | ||
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 | /* DETECTCHAR returns nonzero if (long)X contains the byte used | ||
57 | to fill (long)MASK. */ | ||
58 | #define DETECTCHAR(X,MASK) (DETECTNULL(X ^ MASK)) | ||
59 | |||
60 | void * | ||
61 | _DEFUN (memchr, (s1, i, n), | ||
62 | _CONST void *s1 _AND | ||
63 | int i _AND size_t n) | ||
64 | { | ||
65 | _CONST unsigned char *s = (_CONST unsigned char *)s1; | ||
66 | #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__) | ||
67 | unsigned char c = (unsigned char)i; | ||
68 | |||
69 | while (n-- > 0) | ||
70 | { | ||
71 | if (*s == c) | ||
72 | { | ||
73 | return (void *)s; | ||
74 | } | ||
75 | s++; | ||
76 | } | ||
77 | |||
78 | return NULL; | ||
79 | #else | ||
80 | unsigned char c = (unsigned char)i; | ||
81 | unsigned long mask,j; | ||
82 | unsigned long *aligned_addr; | ||
83 | |||
84 | if (!UNALIGNED (s)) | ||
85 | { | ||
86 | mask = 0; | ||
87 | for (j = 0; j < LBLOCKSIZE; j++) | ||
88 | mask = (mask << 8) | c; | ||
89 | |||
90 | aligned_addr = (unsigned long*)s; | ||
91 | while ((!DETECTCHAR (*aligned_addr, mask)) && (n>LBLOCKSIZE)) | ||
92 | { | ||
93 | aligned_addr++; | ||
94 | n -= LBLOCKSIZE; | ||
95 | } | ||
96 | |||
97 | /* The block of bytes currently pointed to by aligned_addr | ||
98 | may contain the target character or there may be less than | ||
99 | LBLOCKSIZE bytes left to search. We check the last few | ||
100 | bytes using the bytewise search. */ | ||
101 | |||
102 | s = (unsigned char*)aligned_addr; | ||
103 | } | ||
104 | |||
105 | while (n-- > 0) | ||
106 | { | ||
107 | if (*s == c) | ||
108 | { | ||
109 | return (void *)s; | ||
110 | } | ||
111 | s++; | ||
112 | } | ||
113 | |||
114 | return NULL; | ||
115 | #endif /* not PREFER_SIZE_OVER_SPEED */ | ||
116 | } | ||