fixes for host gcc 4.6.1
[zpugcc/jano.git] / toolchain / gcc / newlib / libc / string / memccpy.c
blobf677ac02ec1eed4685ec68edb2aee21397647ebc
1 /*
2 FUNCTION
3 <<memccpy>>---copy memory regions with end-token check
5 ANSI_SYNOPSIS
6 #include <string.h>
7 void* memccpy(void *<[out]>, const void *<[in]>,
8 int <[endchar]>, size_t <[n]>);
10 TRAD_SYNOPSIS
11 void *memccpy(<[out]>, <[in]>, <[endchar]>, <[n]>
12 void *<[out]>;
13 void *<[in]>;
14 int <[endchar]>;
15 size_t <[n]>;
17 DESCRIPTION
18 This function copies up to <[n]> bytes from the memory region
19 pointed to by <[in]> to the memory region pointed to by
20 <[out]>. If a byte matching the <[endchar]> is encountered,
21 the byte is copied and copying stops.
23 If the regions overlap, the behavior is undefined.
25 RETURNS
26 <<memccpy>> returns a pointer to the first byte following the
27 <[endchar]> in the <[out]> region. If no byte matching
28 <[endchar]> was copied, then <<NULL>> is returned.
30 PORTABILITY
31 <<memccpy>> is a GNU extension.
33 <<memccpy>> requires no supporting OS subroutines.
37 #include <_ansi.h>
38 #include <stddef.h>
39 #include <string.h>
40 #include <limits.h>
42 /* Nonzero if either X or Y is not aligned on a "long" boundary. */
43 #define UNALIGNED(X, Y) \
44 (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
46 /* How many bytes are copied each iteration of the word copy loop. */
47 #define LITTLEBLOCKSIZE (sizeof (long))
49 /* Threshhold for punting to the byte copier. */
50 #define TOO_SMALL(LEN) ((LEN) < LITTLEBLOCKSIZE)
52 /* Macros for detecting endchar */
53 #if LONG_MAX == 2147483647L
54 #define DETECTNULL(X) (((X) - 0x01010101) & ~(X) & 0x80808080)
55 #else
56 #if LONG_MAX == 9223372036854775807L
57 /* Nonzero if X (a long int) contains a NULL byte. */
58 #define DETECTNULL(X) (((X) - 0x0101010101010101) & ~(X) & 0x8080808080808080)
59 #else
60 #error long int is not a 32bit or 64bit type.
61 #endif
62 #endif
65 _PTR
66 _DEFUN (memccpy, (dst0, src0, endchar, len0),
67 _PTR dst0 _AND
68 _CONST _PTR src0 _AND
69 int endchar0 _AND
70 size_t len0)
73 #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
74 _PTR ptr = NULL;
75 char *dst = (char *) dst0;
76 char *src = (char *) src0;
77 char endchar = endchar0 & 0xff;
79 while (len0--)
81 if ((*dst++ = *src++) == endchar)
83 ptr = dst;
84 break;
88 return ptr;
89 #else
90 _PTR ptr = NULL;
91 char *dst = dst0;
92 _CONST char *src = src0;
93 long *aligned_dst;
94 _CONST long *aligned_src;
95 int len = len0;
96 char endchar = endchar0 & 0xff;
98 /* If the size is small, or either SRC or DST is unaligned,
99 then punt into the byte copy loop. This should be rare. */
100 if (!TOO_SMALL(len) && !UNALIGNED (src, dst))
102 int i;
103 unsigned long mask = 0;
105 aligned_dst = (long*)dst;
106 aligned_src = (long*)src;
108 /* The fast code reads the ASCII one word at a time and only
109 performs the bytewise search on word-sized segments if they
110 contain the search character, which is detected by XORing
111 the word-sized segment with a word-sized block of the search
112 character and then detecting for the presence of NULL in the
113 result. */
114 for (i = 0; i < LITTLEBLOCKSIZE; i++)
115 mask = (mask << 8) + endchar;
118 /* Copy one long word at a time if possible. */
119 while (len >= LITTLEBLOCKSIZE)
121 unsigned long buffer = (unsigned long)(*aligned_src);
122 buffer ^= mask;
123 if (DETECTNULL (buffer))
124 break; /* endchar is found, go byte by byte from here */
125 *aligned_dst++ = *aligned_src++;
126 len -= LITTLEBLOCKSIZE;
129 /* Pick up any residual with a byte copier. */
130 dst = (char*)aligned_dst;
131 src = (char*)aligned_src;
134 while (len--)
136 if ((*dst++ = *src++) == endchar)
138 ptr = dst;
139 break;
143 return ptr;
144 #endif /* not PREFER_SIZE_OVER_SPEED */