DPDK patches and discussions
 help / color / mirror / Atom feed
From: Zhihong Wang <zhihong.wang@intel.com>
To: rkerur@gmail.com
Cc: dev@dpdk.org
Subject: Re: [dpdk-dev] [dpdk-dev, v3] Implement memcmp using Intel SIMD instrinsics.
Date: Wed, 27 Jan 2016 22:08:26 -0500	[thread overview]
Message-ID: <1453950506-28067-1-git-send-email-zhihong.wang@intel.com> (raw)
In-Reply-To: <1431979303-1346-2-git-send-email-rkerur@gmail.com>

> diff --git a/lib/librte_eal/common/include/arch/x86/rte_memcmp.h b/lib
> /librte_eal/common/include/arch/x86/rte_memcmp.h

[...]

> +#ifdef __cplusplus
> +extern "C" {
> +#endif
> +
> +/**
> + * Compare bytes between two locations. The locations must not overlap.
> + *

Parameter names should be kept consistent as they are in function body.

> + * @param src_1
> + *   Pointer to the first source of the data.
> + * @param src_2
> + *   Pointer to the second source of the data.
> + * @param n
> + *   Number of bytes to compare.
> + * @return
> + *   zero if src_1 equal src_2
> + *   -ve if src_1 less than src_2
> + *   +ve if src_1 greater than src_2
> + */
> +static inline int
> +rte_memcmp(const void *src_1, const void *src,
> +		size_t n) __attribute__((always_inline));
> +
> +/**
> + * Find the first different bit for comparison.
> + */
> +static inline int
> +rte_cmpffd (uint32_t x, uint32_t y)
> +{
> +	int i;
> +	int pos = x ^ y;
> +	for (i = 0; i < 32; i++)
> +		if (pos & (1<<i))

Coding style check :-)
BTW, does the bsf instruction provide this check?

> +			return i;
> +	return -1;
> +}
> +

[...]

> +/**
> + * Compare 48 bytes between two locations.
> + * Locations should not overlap.
> + */
> +static inline int
> +rte_cmp48(const void *src_1, const void *src_2)

Guess this is not used.

[...]

> +/**
> + * Compare 256 bytes between two locations.
> + * Locations should not overlap.
> + */
> +static inline int
> +rte_cmp256(const void *src_1, const void *src_2)
> +{
> +	int ret;
> +
> +	ret = rte_cmp64((const uint8_t *)src_1 + 0 * 64,
> +			(const uint8_t *)src_2 + 0 * 64);

Why not just use rte_cmp128?


[...]

> +static inline int
> +rte_memcmp(const void *_src_1, const void *_src_2, size_t n)
> +{
> +	const uint8_t *src_1 = (const uint8_t *)_src_1;
> +	const uint8_t *src_2 = (const uint8_t *)_src_2;
> +	int ret = 0;
> +
> +	if (n < 16)
> +		return rte_memcmp_regular(src_1, src_2, n);
> +
> +	if (n <= 32) {
> +		ret = rte_cmp16(src_1, src_2);
> +		if (unlikely(ret != 0))
> +			return ret;
> +
> +		return rte_cmp16(src_1 - 16 + n, src_2 - 16 + n);
> +	}
> +

Too many conditions here may harm the overall performance.
It's a trade-off thing, all about balancing the overhead.
Just make sure this is tuned based on actual test numbers.


> +	if (n <= 48) {
> +		ret = rte_cmp32(src_1, src_2);
> +		if (unlikely(ret != 0))
> +			return ret;
> +
> +		return rte_cmp16(src_1 - 16 + n, src_2 - 16 + n);
> +	}
> +
> +	if (n <= 64) {
> +		ret = rte_cmp32(src_1, src_2);
> +		if (unlikely(ret != 0))
> +			return ret;
> +
> +		ret = rte_cmp16(src_1 + 32, src_2 + 32);
> +
> +		if (unlikely(ret != 0))
> +			return ret;
> +
> +		return rte_cmp16(src_1 - 16 + n, src_2 - 16 + n);
> +	}
> +
> +	if (n <= 96) {
> +		ret = rte_cmp64(src_1, src_2);
> +		if (unlikely(ret != 0))
> +			return ret;
> +
> +		ret = rte_cmp16(src_1 + 64, src_2 + 64);
> +		if (unlikely(ret != 0))
> +			return ret;
> +
> +		return rte_cmp16(src_1 - 16 + n, src_2 - 16 + n);
> +	}
> +
> +	if (n <= 128) {
> +		ret = rte_cmp64(src_1, src_2);
> +		if (unlikely(ret != 0))
> +			return ret;
> +
> +		ret = rte_cmp32(src_1 + 64, src_2 + 64);
> +		if (unlikely(ret != 0))
> +			return ret;
> +
> +		ret = rte_cmp16(src_1 + 96, src_2 + 96);
> +		if (unlikely(ret != 0))
> +			return ret;
> +
> +		return rte_cmp16(src_1 - 16 + n, src_2 - 16 + n);
> +	}

[...]

> +/**
> + * Compare 48 bytes between two locations.
> + * Locations should not overlap.
> + */
> +static inline int
> +rte_cmp48(const void *src_1, const void *src_2)

Not used.

> +{
> +	int ret;
> +
> +	ret = rte_cmp16((const uint8_t *)src_1 + 0 * 16,
> +			(const uint8_t *)src_2 + 0 * 16);
> +
> +	if (unlikely(ret != 0))
> +		return ret;
> +
> +	ret = rte_cmp16((const uint8_t *)src_1 + 1 * 16,
> +			(const uint8_t *)src_2 + 1 * 16);
> +
> +	if (unlikely(ret != 0))
> +		return ret;
> +
> +	return rte_cmp16((const uint8_t *)src_1 + 2 * 16,
> +			(const uint8_t *)src_2 + 2 * 16);
> +}
> +
> +/**
> + * Compare 64 bytes between two locations.
> + * Locations should not overlap.
> + */
> +static inline int
> +rte_cmp64(const void *src_1, const void *src_2)
> +{
> +	int ret;
> +
> +	ret = rte_cmp16((const uint8_t *)src_1 + 0 * 16,
> +			(const uint8_t *)src_2 + 0 * 16);

Why not rte_cmp32? And use rte_cmp64 for rte_cmp128, and so on.
That should make the code looks clearer.


It'd be great if you could format this patch into a patch set with several
little ones. :-)
Also, the kernel checkpatch is very helpful.
Good coding style and patch organization make it easy for in-depth reviews.

  parent reply	other threads:[~2016-01-28 10:11 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-18 20:01 [dpdk-dev] [PATCH v3] Implement memcmp using SIMD intrinsics Ravi Kerur
2015-05-18 20:01 ` [dpdk-dev] [PATCH v3] Implement memcmp using Intel SIMD instrinsics Ravi Kerur
2015-10-14  0:32   ` Stephen Hemminger
2016-01-28  3:08   ` Zhihong Wang [this message]
2016-02-19 17:50     ` [dpdk-dev] [dpdk-dev, " Ravi Kerur
2016-02-23 12:22       ` Wang, Zhihong
2016-02-24  4:00         ` Ravi Kerur
2015-06-12  8:30 ` [dpdk-dev] [PATCH v3] Implement memcmp using SIMD intrinsics Ondřej Bílka
2015-06-12  9:03   ` Bruce Richardson
2015-06-15 20:47   ` Ravi Kerur

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=1453950506-28067-1-git-send-email-zhihong.wang@intel.com \
    --to=zhihong.wang@intel.com \
    --cc=dev@dpdk.org \
    --cc=rkerur@gmail.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).