Browse Source

gcc 4.3.1 generates bad code for byte_copy, so I'm switching to a

simpler version
master
Felix von Leitner 13 years ago
parent
commit
e4010479e5
  1. 16
      byte/byte_copy.c
  2. 39
      test/byte_copy.c

16
byte/byte_copy.c

@ -3,12 +3,19 @@
/* byte_copy copies in[0] to out[0], in[1] to out[1], ... and in[len-1]
* to out[len-1]. */
void byte_copy(void* out, size_t len, const void* in) {
register char* s=out;
register const char* t=in;
register const char* u=t+len;
char* s=out;
const char* t=in;
#if 1
/* gcc 4.3.1 generates wrong code for this, so I'm switching to
* simpler code */
size_t i;
for (i=0; i<len; ++i)
s[i]=t[i];
#else
const char* u=t+len;
if (len>127) {
while ((unsigned long)s&(sizeof(unsigned long)-1)) {
if (t==u) break; *s=*t; ++s; ++t;
*s=*t; ++s; ++t;
}
/* s (destination) is now unsigned long aligned */
#ifndef __i386__
@ -25,4 +32,5 @@ void byte_copy(void* out, size_t len, const void* in) {
if (t==u) break; *s=*t; ++s; ++t;
if (t==u) break; *s=*t; ++s; ++t;
}
#endif
}

39
test/byte_copy.c

@ -1,15 +1,36 @@
#include "byte.h"
#include "errmsg.h"
#include <string.h>
char buf[4096];
char text[128];
int main() {
char buf[4096];
carp("both aligned");
byte_copy(buf,16,"this is a test!\n");
carp("destination aligned, source unaligned");
byte_copy(buf,1000,buf+1);
carp("destination unaligned, source aligned");
byte_copy(buf+1,1000,buf);
carp("both unaligned");
byte_copy(buf+1,1000,buf+3);
memset(buf,0,sizeof(buf));
strcpy(text,"this is a test!\n");
// carp("both aligned");
byte_copy(buf,16,text);
if (memcmp(buf,"this is a test!\n\0",18))
die(1,"fail 1");
memset(buf,0,sizeof(buf));
// carp("destination aligned, source unaligned");
byte_copy(buf,15,text+1);
if (memcmp(buf,"his is a test!\n\0\0",18))
die(1,"fail 2");
memset(buf,0,sizeof(buf));
// carp("destination unaligned, source aligned");
byte_copy(buf+1,15,text);
if (memcmp(buf,"\0this is a test!\0\0",18))
die(1,"fail 3");
memset(buf,0,sizeof(buf));
// carp("both unaligned");
byte_copy(buf+1,10,text+3);
if (memcmp(buf,"\0s is a tes\0\0",14))
die(1,"fail 4");
return 0;
}

Loading…
Cancel
Save