Skip to content

Simplify check condition for memcpy path in memmove #1418

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

Merged
merged 1 commit into from
Jul 30, 2020
Merged

Simplify check condition for memcpy path in memmove #1418

merged 1 commit into from
Jul 30, 2020

Conversation

MaxGraey
Copy link
Member

Ref: https://git.musl-libc.org/cgit/musl/tree/src/string/memmove.c

  • I've read the contributing guidelines

@dcodeIO
Copy link
Member

dcodeIO commented Jul 30, 2020

Relevant musl commit for background:

the comparison must take place in the address space model as an
integer type, since comparing pointers that are not pointing into the
same array is undefined.

the subsequent d<s comparison however is valid, because it's only
reached in the case where the source and dest overlap, in which case
they are necessarily pointing to parts of the same array.

to make the comparison, use an unsigned range check for dist(s,d)>=n,
algebraically !(-n<s-d<n). subtracting n yields !(-2n<s-d-n<0), which
mapped into unsigned modular arithmetic is !(-2
n<s-d-n) or rather
-2*n>=s-d-n.

@dcodeIO dcodeIO merged commit b622400 into AssemblyScript:master Jul 30, 2020
@MaxGraey MaxGraey deleted the optimize-mem-move-copy branch July 30, 2020 14:57
@github-actions
Copy link

🎉 This PR is included in version 0.14.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

2 participants