This is a follow-up question to this question.
The following code is on Compiler Explorer.
#include <stddef.h>
#include <stdint.h>
union my_type
{
uint8_t m8[8];
uint16_t m16[4];
uint32_t m32[2];
uint64_t m64;
};
void my_copy(uint32_t volatile *restrict dst, uint32_t const *restrict src, size_t const count)
{
for (size_t i = 0; i < count / 8; ++i)
{
*dst++ = *src++;
*dst++ = *src++;
}
}
int main(int argc, char *argv[])
{
union my_type src[100];
union my_type dst[200];
my_copy(dst[42].m32, src[10].m32, sizeof(union my_type) * 60);
return 0;
}
While my_copy
looks contrived, the access pattern is forced by hardware (must have 2x 32-bit writes to consecutive aligned locations). The rest of the ugliness is due to the intersection of a couple of sections of code that were written by different developers a couple of years apart.
The question is, are the arguments passed to my_copy
an issue? If this was just to copy a single union my_type
, I believe that it would be ok. I don't know if that is valid to copy an array, though.