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

simpler version
master
leitner 17 years ago
parent 8583de1b55
commit 97ac873038

@ -3,12 +3,19 @@
/* byte_copy copies in[0] to out[0], in[1] to out[1], ... and in[len-1] /* byte_copy copies in[0] to out[0], in[1] to out[1], ... and in[len-1]
* to out[len-1]. */ * to out[len-1]. */
void byte_copy(void* out, size_t len, const void* in) { void byte_copy(void* out, size_t len, const void* in) {
register char* s=out; char* s=out;
register const char* t=in; const char* t=in;
register const char* u=t+len; #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) { if (len>127) {
while ((unsigned long)s&(sizeof(unsigned long)-1)) { 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 */ /* s (destination) is now unsigned long aligned */
#ifndef __i386__ #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;
if (t==u) break; *s=*t; ++s; ++t; if (t==u) break; *s=*t; ++s; ++t;
} }
#endif
} }

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

Loading…
Cancel
Save