Skip to content

Commit 7a2e2b5

Browse files
committed
perf memset
1 parent cf7c314 commit 7a2e2b5

File tree

1 file changed

+56
-5
lines changed

1 file changed

+56
-5
lines changed

protocol/uni_communication.c

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,62 @@ static void* _memcpy(void *dst, const void *src, unsigned int len) {
148148
return dst;
149149
}
150150

151-
static void _memset(void *s, unsigned char c, unsigned int n) {
152-
unsigned char *p = (unsigned char *)s;
153-
while (n--) {
154-
*p++ = c;
151+
#define OPSIZ sizeof(unsigned long int)
152+
static void* _memset(void *dstpp, int c, unsigned int len) {
153+
long int dstp = (long int) dstpp;
154+
if (len >= 8) {
155+
unsigned int xlen;
156+
unsigned long int cccc;
157+
158+
cccc = (unsigned char) c;
159+
cccc |= cccc << 8;
160+
cccc |= cccc << 16;
161+
if (OPSIZ > 4) {
162+
/* Do the shift in two steps to avoid warning if long has 32 bits. */
163+
cccc |= (cccc << 16) << 16;
164+
}
165+
166+
/* There are at least some bytes to set. No need to test for LEN == 0 in this alignment loop. */
167+
while (dstp % OPSIZ != 0) {
168+
((unsigned char *) dstp)[0] = c;
169+
dstp += 1;
170+
len -= 1;
171+
}
172+
173+
/* Write 8 `unsigned long int' per iteration until less than 8 `unsigned long int' remain. */
174+
xlen = len / (OPSIZ * 8);
175+
while (xlen > 0) {
176+
((unsigned long int *)dstp)[0] = cccc;
177+
((unsigned long int *)dstp)[1] = cccc;
178+
((unsigned long int *)dstp)[2] = cccc;
179+
((unsigned long int *)dstp)[3] = cccc;
180+
((unsigned long int *)dstp)[4] = cccc;
181+
((unsigned long int *)dstp)[5] = cccc;
182+
((unsigned long int *)dstp)[6] = cccc;
183+
((unsigned long int *)dstp)[7] = cccc;
184+
dstp += 8 * OPSIZ;
185+
xlen -= 1;
186+
}
187+
len %= OPSIZ * 8;
188+
189+
/* Write 1 `op_t' per iteration until less than OPSIZ bytes remain. */
190+
xlen = len / OPSIZ;
191+
while (xlen > 0) {
192+
((unsigned long int *) dstp)[0] = cccc;
193+
dstp += OPSIZ;
194+
xlen -= 1;
195+
}
196+
len %= OPSIZ;
155197
}
198+
199+
/* Write the last few bytes. */
200+
while (len > 0) {
201+
((unsigned char *) dstp)[0] = c;
202+
dstp += 1;
203+
len -= 1;
204+
}
205+
206+
return dstpp;
156207
}
157208

158209
void CommProtocolRegisterHooks(CommProtocolHooks *hooks) {
@@ -837,7 +888,7 @@ static void _set_started_seq() {
837888
char r1[1];
838889
char *r2 = g_hooks.malloc_fn(sizeof(char));
839890
if (r2) g_hooks.free_fn(r2);
840-
g_comm_protocol_business.sequence = (CommSequence)((unsigned int)r1 ^ (unsigned int)r2);
891+
g_comm_protocol_business.sequence = (CommSequence)((unsigned long int)r1 ^ (unsigned long int)r2);
841892
}
842893

843894
static void _protocol_business_init() {

0 commit comments

Comments
 (0)