Can I thrust in GCC doing the right thing when casting to a wider type in C?

Aaron aaron_ng@inode.at
Sat Aug 3 07:31:00 GMT 2019


Can I thrust in GCC doing the right thing when casting to a wider type 
in C, now and in the future?

As far as I can tell this always worked provided that the pointers are 
aligned properly.
I am afraid LTO or other upcoming methods of code-improvementwill bring 
grief on us.
Why should someone want do write such code? Answer: speed

Unfortunately, it is not valid C due to the effective type rules. (6.5 
Expression 7).

Side-note 1: I am not aware of any negative impact on alias-analyses due 
to the casts in question.

Side-note 2: The wg14 does not seem to address this shortcoming for C20, 
ignoring quite a substantial amount of existing code.

Simplified example:
// I assume sizeof(any-int-type) is 2^n with n element N
double D_src[2], D_dest[2];
...
void *memmove_(void *dest,const void *src,size_t len){ // supposed to 
implement memmove()
     unsigned char *d;
     const unsigned char *s;
     s=(const unsigned char *)src;
     d=(unsigned char *)dest;
     if (d>s){
         s+=len;
         d+=len;
         while (len--)
             *--d=*--s;
     }else if (d<s){
         if (!((((uintptr_t)src)|((uintptr_t)dest)) & 
(sizeof(unsigned)-1u))){
             while (len>=sizeof(unsigned)){
                 // this is safe due to the check: if(... above
                 *(unsigned*)d=*(const unsigned*)s; // <===== line in 
question
                 s+=sizeof(unsigned);
                 d+=sizeof(unsigned);
                 len-=sizeof(unsigned);
             }
         }
         while (len--)
             *d++=*s++;
     }
     return dest;
}

memmove_(D_dest,D_src,(sizeof(D_dest));

Could you please answer this question for C++ as well?

If my question shows a fundamental misunderstanding, I will be glad if 
this is corrected as well.

Regards, Aaron Bachmann



More information about the Gcc-help mailing list