Skip to content

Commit 4f35440

Browse files
authored
Merge pull request #213 from sysprog21/improve-strlen
Improve strlen
2 parents 2000ea7 + d510ca4 commit 4f35440

File tree

6 files changed

+17
-9
lines changed

6 files changed

+17
-9
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
CC ?= gcc
21
CFLAGS := -O -g \
32
-std=c99 -pedantic
43

@@ -14,7 +13,8 @@ CFLAGS_TO_CHECK := \
1413
-Wno-strict-prototypes \
1514
-Wno-declaration-after-statement \
1615
-Wno-format \
17-
-Wno-format-pedantic
16+
-Wno-format-pedantic \
17+
-Wno-overflow
1818

1919
SUPPORTED_CFLAGS :=
2020
# Check if a specific compiler flag is supported, attempting a dummy compilation

lib/c.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,18 @@ void abort(void);
4949

5050
int strlen(char *str)
5151
{
52+
/* process the string by checking 4 characters (a 32-bit word) at a time */
5253
int i = 0;
53-
while (str[i])
54-
i++;
55-
return i;
54+
for (;; i += 4) {
55+
if (!str[i])
56+
return i;
57+
if (!str[i + 1])
58+
return i + 1;
59+
if (!str[i + 2])
60+
return i + 2;
61+
if (!str[i + 3])
62+
return i + 3;
63+
}
5664
}
5765

5866
int strcmp(char *s1, char *s2)

tests/snapshots/fib-arm.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/snapshots/fib-riscv.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/snapshots/hello-arm.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/snapshots/hello-riscv.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)