public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>,
	libc-alpha@sourceware.org
Subject: Re: [PATCH 6/7] stdlib: Optimization qsort{_r} swap implementation
Date: Mon, 22 Jan 2018 08:27:00 -0000	[thread overview]
Message-ID: <f1472674-e8f1-77d6-acd2-069f7fa89ce0@cs.ucla.edu> (raw)
In-Reply-To: <1516298002-4618-7-git-send-email-adhemerval.zanella@linaro.org>

Adhemerval Zanella wrote:
> +static inline bool
> +check_alignment (const void *base, size_t align)
> +{
> +  return _STRING_ARCH_unaligned || ((uintptr_t)base % (align - 1)) == 0;
> +}

Surely the '(align - 1)' was supposed to be 'align'. Has this been tested on an 
architecture that does not allow unaligned access?

> +static inline void
> +swap_generic (void *a, void *b, size_t size)

Why is this inline? It's used only as a function pointer, and the other 
functions so used are not declared inline.

> +static inline swap_t
> +select_swap_func (const void *base, size_t size)
> +{
> +  if (size == 4 && check_alignment (base, 4))
> +    return swap_u32;
> +  else if (size == 8 && check_alignment (base, 8))
> +    return swap_u64;
> +  return swap_generic;
> +}

The conditions aren't portable enough. Use something like this instead for 
swap_u32, and similarly for swap_u64.

   if (size == sizeof (uint32_t) && check_alignment (base, alignof (uint32_t)))
     return swap_u32;

> +static void
> +swap_u32 (void *a, void *b, size_t size)

The pointer arguments should be declared 'void *restrict'. This can help GCC 
generate better code. Similarly for the other swap functions.

> +  uint32_t tmp = *(uint32_t*) a;
> +  *(uint32_t*) a = *(uint32_t*) b;
> +  *(uint32_t*) b = tmp;

It's nicer to avoid casts when possible, as is the case here and elsewhere. This 
is because casts are too powerful in C. Something like this, say:

     uint32_t *ua = a, *ub = b, tmp = *ua;
     *ua = *ub, *ub = tmp;

> +  unsigned char tmp[128];

Why 128? A comment seems called for.

> +static inline void
> +swap_generic (void *a, void *b, size_t size)
> +{
> +  unsigned char tmp[128];
> +  do
> +    {
> +      size_t s = size > sizeof (tmp) ? sizeof (tmp) : size;
> +      memcpy (tmp, a, s);
> +      a = __mempcpy (a, b, s);
> +      b = __mempcpy (b, tmp, s);
> +      size -= s;
> +    }
> +  while (size > 0);
> +}

On my platform (GCC 7.2.1 20170915 (Red Hat 7.2.1-2) x86-64) this inlined the 
memcpy but not the mempcpy calls. How about something like this instead? It 
should let the compiler do a better job of block-move-style operations in the 
loop. If mempcpy is inlined for you, feel free to substitute it for two of the 
loop's calls to memcpy.

   static void
   swap_generic (void *restrict va, void *restrict vb, size_t size)
   {
     char *a = va, *b = vb;
     enum { n = 128 }; /* Why 128?  */
     unsigned char tmp[n];
     while (size >= n)
       {
	memcpy (tmp, a, n);
	memcpy (a, b, n);
	memcpy (b, tmp, n);
	a += n;
	b += n;
	size -= n;
       }
     memcpy (tmp, a, size);
     memcpy (a, b, size);
     memcpy (b, tmp, size);
   }

  reply	other threads:[~2018-01-22  8:27 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-18 17:53 [PATCH 0/7] Refactor qsort implementation Adhemerval Zanella
2018-01-18 17:53 ` [PATCH 5/7] stdlib: Remove use of mergesort on qsort Adhemerval Zanella
2018-01-18 17:53 ` [PATCH 2/7] support: Add Mersenne Twister pseudo-random number generator Adhemerval Zanella
2018-01-18 17:53 ` [PATCH 4/7] stdlib: Add more qsort{_r} coverage Adhemerval Zanella
2018-01-18 17:53 ` [PATCH 7/7] stdlib: Remove undefined behavior from qsort implementation Adhemerval Zanella
2018-01-18 17:53 ` [PATCH 3/7] benchtests: Add bench-qsort Adhemerval Zanella
2018-01-18 17:53 ` [PATCH 1/7] stdlib: Adjust tst-qsort{2} to libsupport Adhemerval Zanella
2018-01-18 17:53 ` [PATCH 6/7] stdlib: Optimization qsort{_r} swap implementation Adhemerval Zanella
2018-01-22  8:27   ` Paul Eggert [this message]
2018-01-22 10:55     ` Adhemerval Zanella
2018-01-22 13:46       ` Alexander Monakov
2018-01-22 15:23         ` Adhemerval Zanella
2018-01-22 17:15       ` Paul Eggert
2018-01-22 17:48         ` Adhemerval Zanella
2018-01-22 18:29           ` Paul Eggert
2018-01-22 19:33             ` Adhemerval Zanella
2018-01-23  6:04               ` Paul Eggert
2018-01-23 18:28                 ` Adhemerval Zanella
2018-01-23 23:37                   ` Paul Eggert
2018-01-24 10:47                     ` Adhemerval Zanella

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=f1472674-e8f1-77d6-acd2-069f7fa89ce0@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@sourceware.org \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).