Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
TThis is a patch demonstrating a proposal to add a new
alignment
tag which allows a safer and more user-friendly experience across platforms for alignment and facilitates generic code.Specifically, when specifying alignment, the current options are only
overaligned<N>
,vector_aligned
, orelement_aligned
.The difficulty comes from the fact that correctly writing this code is actually impossible in a cross-platform way. For example, suppose we take the following code:
This code only works on NEON and SSE, and would be incorrect on AVX, because AVX requires 32 and 64 byte alignment for native size vectors.
Fixing it is impossible without changing the code for the allocation,
But this code might not always be possible if the data buffer is allocated by a library.
Furthermore, if you are trying to use
simd::copy_to
, then this actually becomes impossible to write back to a variable defined on the stack in a cross platform way:Similarly, reading from a stack variable fails as well
So, writing correct code on all platforms actually becomes impossible and it is the programmers responsibility to know what alignemnt requirements are satisfied on all targets.
This might involve writing lots of ifdefs, or just dropping back to the slow case. Or maybe using template metaprogramming
Which is exactly the kind of code std::simd is supposed to prevent!
Writing generic code becomes even more difficult. Consider.
This proposed patch fixes that problem. By giving the programmer a way to specify the exact alignment that is used as a tag, generic code and cross platform code becomes possible again.
Internally, the new "stdx::aligned" tag automatically correctly selects an aligned vector load or an unaligned vector load at compile time, based on the platform architecture and data type of the vector the load is using and the byte alignment passed in by the user. It also throws a compile-time assertion if the given byte alignment isn't even element-aligned (which is common to misunderstand if using structure types). This allows platform agnostic and generic code to be written which doesn't throw alignment exceptions in any case. This takes the load of deciding what
works and what doesn't off of the client code's mind.
Example of reading from a pre-allocated buffer with a fixed alignment:
Example of reading from a reference
Example of writing back generic code to an output
This pull request is actually meant to really be something which is incorporated by @mattkretz into the standards proposal and into the libstdc++ implementation.