You can tell the compiler to align your buffer like so:
Other then that, there really isn't anything special involved, a normal memcpy will work just fine:
which compiles to something like the following:
Unions work as well as long as you're writing just C, but is at least formally undefined for C++ ...
EDIT: Fixed the assembly to actual be thumb code; also the assembly is straight off of godbolt so it may look a bit different from the real thing
Code:
uint8_t __attribute__((aligned(4))) screen_buf[320][240*2];Code:
void fill_buffer(uint32_t start_pos) { for (uint32_t i = start_pos; i < 120; ++i) { memcpy(&screen_buf[100][i*4], &i, 4); }}Code:
fill_buffer: ldr r3, .L8.L6: cmp r0, #119 bls .L7 bx lr.L7: lsls r2, r0, #2 str r0, [r3, r2] adds r0, r0, #1 b .L6EDIT: Fixed the assembly to actual be thumb code; also the assembly is straight off of godbolt so it may look a bit different from the real thing
Statistics: Posted by Tharre — Thu Jun 05, 2025 10:13 pm