Skip to content

Commit 243108d

Browse files
committed
Implement a few more C functions for size
1 parent 996cc0d commit 243108d

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

src/cimpl.c

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// be in the critical path of any useful stuff.
2121

2222
#include <stddef.h>
23+
#include <stdint.h>
2324

2425
#include "compiler.h"
2526

@@ -88,4 +89,65 @@ size_t FNAME(strlen)(const char *s) {
8889
return ret;
8990
}
9091

92+
NOINLINE EXTERNAL
93+
void * FNAME(memmove)(void *dst, const void *src, size_t n) {
94+
unsigned char *d = dst;
95+
const unsigned char *s = src;
96+
97+
if (d < s)
98+
while (n--)
99+
*d++ = *s++;
100+
else {
101+
d += n;
102+
s += n;
103+
while (n--)
104+
*--d = *--s;
105+
}
106+
107+
return dst;
108+
}
109+
110+
NOINLINE EXTERNAL
111+
int FNAME(memcmp)(const void *a, const void *b, size_t n) {
112+
const unsigned char *p = a, *q = b;
113+
114+
while (n--) {
115+
if (*p != *q)
116+
return (int)*p - (int)*q;
117+
p++;
118+
q++;
119+
}
120+
121+
return 0;
122+
}
123+
124+
NOINLINE EXTERNAL
125+
void * FNAME(memset)(void *s, int c, size_t n) {
126+
unsigned char *p = s;
127+
unsigned char b = (unsigned char)c;
128+
uint32_t w = b * 0x01010101u;
129+
130+
while (n && ((uintptr_t)p & 3)) {
131+
*p++ = b;
132+
n--;
133+
}
134+
135+
while (n >= 4) {
136+
*(uint32_t *)p = w;
137+
p += 4;
138+
n -= 4;
139+
}
140+
141+
while (n--)
142+
*p++ = b;
143+
144+
return s;
145+
}
146+
147+
NOINLINE EXTERNAL
148+
char *FNAME(strcpy)(char *dst, const char *src) {
149+
char *d = dst;
150+
while ((*d++ = *src++));
151+
return dst;
152+
}
91153

src/compiler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@
2727
#endif
2828

2929
#define NOINLINE __attribute__((noinline))
30+
#define EXTERNAL __attribute__((used, externally_visible))

0 commit comments

Comments
 (0)