Skip to content

Commit c0b73cb

Browse files
committed
Add memcmp implementation
This introduces a simple byte-by-byte memory comparison function, ensuring correctness and reliability in self-hosting environments.
1 parent 6da68db commit c0b73cb

File tree

5 files changed

+17
-4
lines changed

5 files changed

+17
-4
lines changed

lib/c.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@ char *memcpy(char *dest, char *src, int count)
143143
return dest;
144144
}
145145

146+
int memcmp(void *s1, void *s2, int n)
147+
{
148+
char *p1 = (char *) s1, *p2 = (char *) s2;
149+
150+
for (int i = 0; i < n; i++) {
151+
if (p1[i] < p2[i])
152+
return -1;
153+
if (p1[i] > p2[i])
154+
return 1;
155+
}
156+
return 0;
157+
}
158+
146159
void *memset(void *s, int c, int n)
147160
{
148161
int i = 0;

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)