summaryrefslogtreecommitdiff
path: root/libc/string
diff options
context:
space:
mode:
Diffstat (limited to 'libc/string')
-rw-r--r--libc/string/arc/strcmp.S8
-rw-r--r--libc/string/explicit_bzero.c30
-rw-r--r--libc/string/generic/strchr.c23
-rw-r--r--libc/string/generic/strchrnul.c23
-rw-r--r--libc/string/generic/strnlen.c6
-rw-r--r--libc/string/kvx/memcpy.S4
-rw-r--r--libc/string/strcasestr.c2
-rw-r--r--libc/string/strstr.c2
-rw-r--r--libc/string/x86_64/strcat.S2
-rw-r--r--libc/string/x86_64/strcspn.S2
-rw-r--r--libc/string/x86_64/strlen.S2
-rw-r--r--libc/string/x86_64/strspn.S2
12 files changed, 65 insertions, 41 deletions
diff --git a/libc/string/arc/strcmp.S b/libc/string/arc/strcmp.S
index 3f64ac421..48d2d7ec1 100644
--- a/libc/string/arc/strcmp.S
+++ b/libc/string/arc/strcmp.S
@@ -103,23 +103,21 @@ ENTRY(strcmp)
brne r2, 0, @.Lcharloop
;;; s1 and s2 are word aligned
- ld.ab r2, [r0, 4]
mov_s r12, 0x01010101
ror r11, r12
.align 4
.LwordLoop:
+ ld.ab r2, [r0, 4]
+ sub r4, r2, r12
ld.ab r3, [r1, 4]
;; Detect NULL char in str1
- sub r4, r2, r12
- ld.ab r5, [r0, 4]
bic r4, r4, r2
and r4, r4, r11
brne.d.nt r4, 0, .LfoundNULL
;; Check if the read locations are the same
cmp r2, r3
- beq.d .LwordLoop
- mov.eq r2, r5
+ beq .LwordLoop
;; A match is found, spot it out
#ifdef __LITTLE_ENDIAN__
diff --git a/libc/string/explicit_bzero.c b/libc/string/explicit_bzero.c
new file mode 100644
index 000000000..b09e4c1f4
--- /dev/null
+++ b/libc/string/explicit_bzero.c
@@ -0,0 +1,30 @@
+/*
+Copyright © 2005-2020 Rich Felker, et al.
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+*/
+#define _BSD_SOURCE
+#include <string.h>
+
+void explicit_bzero(void *d, size_t n)
+{
+ d = memset(d, 0, n);
+ __asm__ __volatile__ ("" : : "r"(d) : "memory");
+}
diff --git a/libc/string/generic/strchr.c b/libc/string/generic/strchr.c
index 321d2b8c3..b34884d67 100644
--- a/libc/string/generic/strchr.c
+++ b/libc/string/generic/strchr.c
@@ -60,22 +60,19 @@ char *strchr (const char *s, int c_in)
The 1-bits make sure that carries propagate to the next 0-bit.
The 0-bits provide holes for carries to fall into. */
- switch (sizeof (longword))
- {
- case 4: magic_bits = 0x7efefeffL; break;
- case 8: magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL; break;
- default:
- abort ();
- }
-
/* Set up a longword, each of whose bytes is C. */
+#if __WORDSIZE == 32
+ magic_bits = 0x7efefeffL;
charmask = c | (c << 8);
charmask |= charmask << 16;
- if (sizeof (longword) > 4)
- /* Do the shift in two steps to avoid a warning if long has 32 bits. */
- charmask |= (charmask << 16) << 16;
- if (sizeof (longword) > 8)
- abort ();
+#elif __WORDSIZE == 64
+ magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL;
+ charmask = c | (c << 8);
+ charmask |= charmask << 16;
+ charmask |= (charmask << 16) << 16;
+#else
+ #error unexpected integer size strchr()
+#endif
/* Instead of the traditional loop which tests each character,
we will test a longword at a time. The tricky part is testing
diff --git a/libc/string/generic/strchrnul.c b/libc/string/generic/strchrnul.c
index d11d9e00d..d9fadc776 100644
--- a/libc/string/generic/strchrnul.c
+++ b/libc/string/generic/strchrnul.c
@@ -59,22 +59,19 @@ char *strchrnul (const char *s, int c_in)
The 1-bits make sure that carries propagate to the next 0-bit.
The 0-bits provide holes for carries to fall into. */
- switch (sizeof (longword))
- {
- case 4: magic_bits = 0x7efefeffL; break;
- case 8: magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL; break;
- default:
- abort ();
- }
- /* Set up a longword, each of whose bytes is C. */
+#if __WORDSIZE == 32
+ magic_bits = 0x7efefeffL;
charmask = c | (c << 8);
charmask |= charmask << 16;
- if (sizeof (longword) > 4)
- /* Do the shift in two steps to avoid a warning if long has 32 bits. */
- charmask |= (charmask << 16) << 16;
- if (sizeof (longword) > 8)
- abort ();
+#elif __WORDSIZE == 64
+ magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL;
+ charmask = c | (c << 8);
+ charmask |= charmask << 16;
+ charmask |= (charmask << 16) << 16;
+#else
+ #error unexpected integer size strchr()
+#endif
/* Instead of the traditional loop which tests each character,
we will test a longword at a time. The tricky part is testing
diff --git a/libc/string/generic/strnlen.c b/libc/string/generic/strnlen.c
index 4d4cde84f..82d4122ec 100644
--- a/libc/string/generic/strnlen.c
+++ b/libc/string/generic/strnlen.c
@@ -29,15 +29,17 @@
'\0' terminator is found in that many characters, return MAXLEN. */
size_t strnlen (const char *str, size_t maxlen)
{
- const char *char_ptr, *end_ptr = str + maxlen;
+ const char *char_ptr, *end_ptr;
const unsigned long int *longword_ptr;
unsigned long int longword, himagic, lomagic;
if (maxlen == 0)
return 0;
- if (__builtin_expect (end_ptr < str, 0))
+ if (__builtin_expect ((uintptr_t)str + maxlen < (uintptr_t)str, 0))
end_ptr = (const char *) ~0UL;
+ else
+ end_ptr = str + maxlen;
/* Handle the first few characters by reading one character at a time.
Do this until CHAR_PTR is aligned on a longword boundary. */
diff --git a/libc/string/kvx/memcpy.S b/libc/string/kvx/memcpy.S
index 290e705b4..70e8db910 100644
--- a/libc/string/kvx/memcpy.S
+++ b/libc/string/kvx/memcpy.S
@@ -53,7 +53,7 @@ ENTRY(memcpy)
;;
cb.deqz $r7? .Lstreaming_loop_end
;;
- loopdo $r7? .Lstreaming_loop_end
+ loopdo $r7, .Lstreaming_loop_end
;;
sq 0[$r0] = $r32r33
addd $r2 = $r2, -256
@@ -162,7 +162,7 @@ ENTRY(memcpy)
;;
cb.deqz $r7? .Lloop_32_end
;;
- loopdo $r7? .Lloop_32_end
+ loopdo $r7, .Lloop_32_end
;;
lo $r32r33r34r35 = 0[$r1]
addd $r1 = $r1, 32
diff --git a/libc/string/strcasestr.c b/libc/string/strcasestr.c
index 3334086bf..8f57cc0a3 100644
--- a/libc/string/strcasestr.c
+++ b/libc/string/strcasestr.c
@@ -16,7 +16,7 @@ char *strcasestr(const char *s1, const char *s2)
#if 1
do {
if (!*p) {
- return (char *) s1;;
+ return (char *) s1;
}
if ((*p == *s)
|| (tolower(*((unsigned char *)p)) == tolower(*((unsigned char *)s)))
diff --git a/libc/string/strstr.c b/libc/string/strstr.c
index 7e2a64e7d..bf56b9c12 100644
--- a/libc/string/strstr.c
+++ b/libc/string/strstr.c
@@ -22,7 +22,7 @@ Wchar *Wstrstr(const Wchar *s1, const Wchar *s2)
do {
if (!*p) {
- return (Wchar *) s1;;
+ return (Wchar *) s1;
}
if (*p == *s) {
++p;
diff --git a/libc/string/x86_64/strcat.S b/libc/string/x86_64/strcat.S
index 55e09e5f1..209e19062 100644
--- a/libc/string/x86_64/strcat.S
+++ b/libc/string/x86_64/strcat.S
@@ -106,7 +106,7 @@ ENTRY (BP_SYM (strcat))
/* Align, it is a jump target. */
/* Next 3 insns are 8 bytes total, make sure we decode them in one go */
- .p2align 3,,8
+ .p2align 3,,7
3:
subq $8,%rax /* correct pointer increment. */
diff --git a/libc/string/x86_64/strcspn.S b/libc/string/x86_64/strcspn.S
index 7a06c8867..5ef565db7 100644
--- a/libc/string/x86_64/strcspn.S
+++ b/libc/string/x86_64/strcspn.S
@@ -94,7 +94,7 @@ L(1): leaq -4(%rdx), %rax /* prepare loop */
/* but it will also align entire function to 16 bytes, */
/* potentially creating largish padding at link time. */
/* We are aligning to 8 bytes instead: */
- .p2align 3,,8
+ .p2align 3,,7
L(3): addq $4, %rax /* adjust pointer for full loop round */
diff --git a/libc/string/x86_64/strlen.S b/libc/string/x86_64/strlen.S
index 9e84326c2..2fe2f58b2 100644
--- a/libc/string/x86_64/strlen.S
+++ b/libc/string/x86_64/strlen.S
@@ -102,7 +102,7 @@ ENTRY (strlen)
/* Align, it is a jump target. */
/* Next 3 insns are 8 bytes total, make sure we decode them in one go */
- .p2align 3,,8
+ .p2align 3,,7
3:
subq $8,%rax /* correct pointer increment. */
diff --git a/libc/string/x86_64/strspn.S b/libc/string/x86_64/strspn.S
index 366377649..8dc42656b 100644
--- a/libc/string/x86_64/strspn.S
+++ b/libc/string/x86_64/strspn.S
@@ -89,7 +89,7 @@ L(1): leaq -4(%rdx), %rax /* prepare loop */
/* but it will also align entire function to 16 bytes, */
/* potentially creating largish padding at link time. */
/* We are aligning to 8 bytes instead: */
- .p2align 3,,8
+ .p2align 3,,7
L(3):
addq $4, %rax /* adjust pointer for full loop round */