From: Konstantin Ananyev <konstantin.ananyev@huawei.com>
To: "Morten Brørup" <mb@smartsharesystems.com>,
"dev@dpdk.org" <dev@dpdk.org>,
"Bruce Richardson" <bruce.richardson@intel.com>,
"Konstantin Ananyev" <konstantin.v.ananyev@yandex.ru>,
"Vipin Varghese" <vipin.varghese@amd.com>
Cc: Stephen Hemminger <stephen@networkplumber.org>
Subject: RE: [PATCH v3] eal/x86: optimize memcpy of small sizes
Date: Fri, 28 Nov 2025 18:10:32 +0000 [thread overview]
Message-ID: <11d412346d5548c5a5a3bf578b86f4ee@huawei.com> (raw)
In-Reply-To: <98CBD80474FA8B44BF855DF32C47DC35F6559E@smartserver.smartshare.dk>
> > From: Konstantin Ananyev [mailto:konstantin.ananyev@huawei.com]
> > Sent: Friday, 28 November 2025 15.03
> >
> > > +/**
> > > + * Copy bytes from one location to another,
> > > + * locations should not overlap.
> > > + * Use with n <= 16.
> > > + *
> > > + * Note: Copying uninitialized memory is perfectly acceptable.
> > > + * Using e.g. memcpy(dst, src, 8) instead of
> > > + * *(unaligned_uint64_t*) = *(const unaligned_uint64_t *)src
> > > + * avoids compiler warnings about source data may be uninitialized
> > > + * [-Wmaybe-uninitialized].
> > > + *
> > > + * Note: Using "n & X" generates 3-byte "test" instructions,
> > > + * instead of "n >= X", which would generate 4-byte "cmp"
> > instructions.
> > > + */
> > > +static __rte_always_inline void *
> > > +rte_mov16_or_less(void *dst, const void *src, size_t n)
> > > +{
> > > + /* Faster way when size is known at build time. */
> > > + if (__rte_constant(n)) {
> > > + if (n == 2)
> > > + return memcpy(dst, src, 2);
> > > + if (n == 4)
> > > + return memcpy(dst, src, 4);
> > > + if (n == 6) /* 4 + 2 */
> > > + return memcpy(dst, src, 6);
> > > + if (n == 8)
> > > + return memcpy(dst, src, 8);
> > > + if (n == 10) /* 8 + 2 */
> > > + return memcpy(dst, src, 10);
> > > + if (n == 12) /* 8 + 4 */
> > > + return memcpy(dst, src, 12);
> > > + if (n == 16) {
> > > + rte_mov16((uint8_t *)dst, (const uint8_t *)src);
> > > + return dst;
> > > + }
> > > + }
> > > +
> > > + if (n & 0x18) { /* n >= 8 */
> >
> > Probably 'n & 0x8'?
>
> It's intentional, to catch n == 0x10 too.
> It seems the associated comment should be more verbose. How about:
> if (n & 0x18) { /* n >= 8, including n == 0x10, hence n & 0x18 */
Ok, why just not simply : if (n >= 8) then?
> >
> > > + /* copy 8 ~ 16 bytes */
> > > + memcpy(dst, src, 8);
> > > + memcpy((uint8_t *)dst - 8 + n, (const uint8_t *)src - 8 +
> > n, 8);
> > > + } else if (n & 0x4) {
> > > + /* copy 4 ~ 7 bytes */
> > > + memcpy(dst, src, 4);
> > > + memcpy((uint8_t *)dst - 4 + n, (const uint8_t *)src - 4 +
> > n, 4);
> > > + } else if (n & 0x2) {
> > > + /* copy 2 ~ 3 bytes */
> > > + memcpy(dst, src, 2);
> > > + memcpy((uint8_t *)dst - 2 + n, (const uint8_t *)src - 2 +
> > n, 2);
> > > + } else if (n & 0x1) {
> > > + /* copy 1 byte */
> > > + memcpy(dst, src, 1);
> > > + }
> > > + return dst;
> > > +}
next prev parent reply other threads:[~2025-11-28 18:10 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-20 11:45 [PATCH] eal/x86: reduce memcpy code duplication Morten Brørup
2025-11-21 10:35 ` [PATCH v2] eal/x86: optimize memcpy of small sizes Morten Brørup
2025-11-21 16:57 ` Stephen Hemminger
2025-11-21 17:02 ` Bruce Richardson
2025-11-21 17:11 ` Stephen Hemminger
2025-11-21 21:36 ` Morten Brørup
2025-11-21 10:40 ` Morten Brørup
2025-11-21 10:40 ` [PATCH v3] " Morten Brørup
2025-11-24 13:36 ` Morten Brørup
2025-11-24 15:46 ` Patrick Robb
2025-11-28 14:02 ` Konstantin Ananyev
2025-11-28 15:55 ` Morten Brørup
2025-11-28 18:10 ` Konstantin Ananyev [this message]
2025-11-24 20:31 ` [PATCH v4] " Morten Brørup
2025-11-25 8:19 ` Morten Brørup
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=11d412346d5548c5a5a3bf578b86f4ee@huawei.com \
--to=konstantin.ananyev@huawei.com \
--cc=bruce.richardson@intel.com \
--cc=dev@dpdk.org \
--cc=konstantin.v.ananyev@yandex.ru \
--cc=mb@smartsharesystems.com \
--cc=stephen@networkplumber.org \
--cc=vipin.varghese@amd.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).