Skip to content

Fix __memcpy_chunks skipping bytes in partial loads - #15

Open
codethinki wants to merge 1 commit into
GSI-HPC:mainfrom
codethinki:fix-memcpy-chunks
Open

codethinki wants to merge 1 commit into
GSI-HPC:mainfrom
codethinki:fix-memcpy-chunks

Conversation

@codethinki

Copy link
Copy Markdown

partial_load loses an element when the remaining bytes are at the top of a __memcpy_chunks branch: the second copy is only _Chunk bytes wide, so the bytes in between are never copied.

  • 1-byte elements, 4 remaining: bytes 0, 1 and 3 are copied
  • 2-byte elements, 4 remaining: bytes 0-3 and 6-7 are copied
std::array<signed char, 4> mem {1, 2, 3, 4};
std::size_t n = make_value_unknown(4uz);
partial_load<vec<signed char, 16>>(mem.begin(), n); // [1, 2, 0, 4, 0, ...]

With vec<char, 64> this hits every tail of 4, 20, 36 and 52 elements. The same code ships with libstdc++ in GCC 16.2.

Every branch now copies the same width from the front and the back, which covers (k, 2k] bytes for any chunk size.

tests/loads.cpp: partial_load_every_size loads every n from 0 to V::size with an unknown n (a known n takes the single memcpy path). It fails without the fix and passes with it for signed char, short, int, long long, float and double at widths 4, 8, 16, 20, 32, 36 and 64 on x86-64 and x86-64-v3 with GCC 16.2.1.

The second copy of a branch was only _Chunk bytes wide, so at the top of
a branch's range the bytes in between were never copied. 4 remaining
1-byte elements copied bytes 0, 1 and 3, 4 remaining 2-byte elements
copied bytes 0-3 and 6-7. Every branch now copies the same width from
the front and the back.

tests/loads.cpp: Add partial_load_every_size.
@codethinki

codethinki commented Oct 6, 2026 •

Copy link
Copy Markdown
Author

Did not write the fixes, claude opus 5.5 did. However I code reviewed and approve as far as I can understand it.

If you have any questions or comments I'll reply / fix it as soon as I can.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant