Skip to content

Commit 53fb5e5

Browse files
committed
Improve strlen
Replace character-by-character iteration with 4-character blocks, reducing check/increment/jump operations. Prepared for future 32-bit word loading once unsigned type support is complete.
1 parent 96638ea commit 53fb5e5

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

lib/c.c

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

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

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

0 commit comments

Comments
 (0)