Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

include: util: Add mem_xor functions #67159

Merged
merged 1 commit into from
Jan 5, 2024

Conversation

Thalley
Copy link
Collaborator

@Thalley Thalley commented Jan 3, 2024

Add functions to do XOR on arrays of memory, with one that takes arbitrary sizes and one for 32 bits and 128 bits as those are common sizes for this functionality.

Add functions to do XOR on arrays of memory, with one that
takes arbitrary sizes and one for 32 bits and 128 bits as
those are common sizes for this functionality.

Signed-off-by: Emil Gydesen <[email protected]>
*/
static inline void mem_xor_32(uint8_t dst[4], const uint8_t src1[4], const uint8_t src2[4])
{
mem_xor_n(dst, src1, src2, 4U);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One possible optimization here would be to cast to uint32_t and then xor directly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a bad suggestion. Since the code was copied from implementations from @cvinayak I'd like his input as to why it was not done like that in the first place.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to support CPUs needing 4-byte aligned memory read/write operations (ARM cortex-m0 variants etc...).

Fun fact, was my very first contribution to Zephyr Project! de999a8

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably solvable by using UNALIGNED_PUT/UNALIGNED_GET macros.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry look at your comment on UNALIGNED_PUT/UNALIGNED_GET now. The implementation in our toolchain header has workarounds for compiler version, and my feeling goes towards sticking to simple old while loop for now.

*/
static inline void mem_xor_128(uint8_t dst[16], const uint8_t src1[16], const uint8_t src2[16])
{
mem_xor_n(dst, src1, src2, 16);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here have a small loop and call the 32 variant instead.

*/
static inline void mem_xor_32(uint8_t dst[4], const uint8_t src1[4], const uint8_t src2[4])
{
mem_xor_n(dst, src1, src2, 4U);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to support CPUs needing 4-byte aligned memory read/write operations (ARM cortex-m0 variants etc...).

Fun fact, was my very first contribution to Zephyr Project! de999a8

@carlescufi carlescufi merged commit 87c8b89 into zephyrproject-rtos:main Jan 5, 2024
34 checks passed
@Thalley Thalley deleted the xor_n branch January 5, 2024 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

6 participants