Skip to content

memcpy c implementation#11

Open
i3abghany wants to merge 3 commits into
mainfrom
memcpy_c_misaligned_access
Open

memcpy c implementation#11
i3abghany wants to merge 3 commits into
mainfrom
memcpy_c_misaligned_access

Conversation

@i3abghany

@i3abghany i3abghany commented Apr 14, 2025

Copy link
Copy Markdown
Collaborator

Add a code path for when source and dest are differently aligned.

If misaligned access is slow or prohibited, and the alignments of the source and destination are different, we align the destination to do word stores. This uses only one aligned store for every four (or eight for XLEN == 64) bytes of data.

This only affects the case where source and destination are differently aligned. Previously, this case was handled by copying one byte at a time. The new approach significantly reduces the number of executed (dynamic) instructions (30% weighted average for -mstrict-align, while not really affecting -mno-strict-align):

image
image

@i3abghany i3abghany requested a review from Copilot April 14, 2025 08:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

newlib/libc/machine/riscv/memcpy.c:26

  • Using 'char' for byte extraction can lead to sign extension issues on platforms where 'char' is signed. Consider using 'unsigned char' to ensure the byte values are correctly interpreted.
char b0 = *p++;

@i3abghany i3abghany changed the title RISC-V: memcpy() align dest when misaligned access is prohibited memcpy c implementation Apr 14, 2025
@i3abghany i3abghany force-pushed the memcpy_c_misaligned_access branch from 4ddb559 to 82defe7 Compare April 14, 2025 08:10
@i3abghany i3abghany requested a review from Copilot April 14, 2025 08:11

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (2)

newlib/libc/machine/riscv/memcpy.c:24

  • [nitpick] The parameter name 'p' in __load_word is generic; consider renaming it to 'src' for improved clarity.
__load_word (const unsigned char *p)

newlib/libc/machine/riscv/memcpy.c:87

  • [nitpick] The variable name 'la' is terse; consider using a more descriptive name (e.g., 'aligned_dest') to enhance readability.
long *la = (long *)a;

Comment thread newlib/libc/machine/riscv/memcpy.c Outdated
Comment thread newlib/libc/machine/riscv/memcpy.c Outdated
Comment thread newlib/libc/machine/riscv/memcpy.c Outdated
Comment thread newlib/libc/machine/riscv/memcpy.c Outdated

@christian-herber-nxp christian-herber-nxp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

small suggestion in comments.

@i3abghany i3abghany force-pushed the memcpy_c_misaligned_access branch from 82defe7 to 8332de7 Compare April 16, 2025 05:31
@i3abghany

Copy link
Copy Markdown
Collaborator Author

@christian-herber-nxp I think this looks good, so please let me know if you have other suggestions and if we can send a patch series soon.

@i3abghany i3abghany requested a review from Copilot April 16, 2025 05:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

Comment thread newlib/libc/machine/riscv/memcpy.c Outdated
@christian-herber-nxp christian-herber-nxp self-requested a review April 16, 2025 06:12

@christian-herber-nxp christian-herber-nxp left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

looks pretty good.
for any reviewers and future readers sake, could you add some more comments explaining why certain pieces of code are the way they are.
Also, results on LLVM would still be nice.

Add a code path for when source and dest are differently aligned.

If misaligned access is slow or prohibited, and the alignments of the
source and destination are different, we align the destination to do
XLEN stores. This uses only one aligned store for every four (or eight
for XLEN == 64) bytes of data.
@i3abghany i3abghany force-pushed the memcpy_c_misaligned_access branch from 8332de7 to 2323d35 Compare April 16, 2025 19:29
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.

3 participants