Skip to content

Commit 3c63abd

Browse files
committed
string: Cast memchr and memrchr val to 'unsigned long' while building mask
In the speed-optimized version of this code, the needle is replicated to fill an unsigned long. It does that by oring various shifts of the value. Of course, C performs the first of those by first casting to int, and on MSP430, the compiler ends up sign extending so values >= 0x80 end up with high bits set which wrecks the resulting mask value. Signed-off-by: Keith Packard <[email protected]>
1 parent 0fa07d9 commit 3c63abd

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

newlib/libc/string/memchr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ memchr (const void *src_void,
8383
character and then detecting for the presence of NUL in the
8484
result. */
8585
asrc = (unsigned long *) src;
86-
mask = d << 8 | d;
86+
mask = (unsigned long) d << 8 | (unsigned long) d;
8787
mask = mask << 16 | mask;
8888
for (i = 32; i < sizeof(mask) * 8; i <<= 1)
8989
mask = (mask << i) | mask;

newlib/libc/string/memrchr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ memrchr (const void *src_void,
6868
character and then detecting for the presence of NUL in the
6969
result. */
7070
asrc = (unsigned long *) (src - LITTLE_BLOCK_SIZE + 1);
71-
mask = d << 8 | d;
71+
mask = (unsigned long) d << 8 | (unsigned long) d;
7272
mask = mask << 16 | mask;
7373
for (i = 32; i < sizeof(mask) * 8; i <<= 1)
7474
mask = (mask << i) | mask;

0 commit comments

Comments
 (0)