From: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
To: Alexander Monakov <amonakov@ispras.ru>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH v5 1/6] stdlib: Optimization qsort{_r} swap implementation
Date: Tue, 18 Jul 2023 11:01:31 -0300 [thread overview]
Message-ID: <85afee5d-69f7-f070-58a4-08251c07db96@linaro.org> (raw)
In-Reply-To: <49920fe1-b5a1-939c-9d34-bd553cd4352e@ispras.ru>
On 17/07/23 13:57, Alexander Monakov wrote:
>
> On Thu, 13 Jul 2023, Adhemerval Zanella via Libc-alpha wrote:
>
>> +/* Returns true if elements can be copied using word loads and stores.
>> + The SIZE and BASE must be a multiple of the ALIGN. */
>> +__attribute_const__ __always_inline static bool
>
> Kernel folks sure love their double underscores, but plain
> 'static inline bool' should work just as well here.
>
> I'd recommend to properly credit Linux lib/sort.c since obviously this
> commit originated as a direct copy of the code therein, not just being
> inspired by what's there.
In fact our msort_with_tmp uses the exact same idea, but it is somewhat
convoluted because it adds an specific optimization to 'unsigned long'
and for large values it uses a inplace mempcpy.
>
>> +is_aligned (const void *base, size_t size, unsigned char align)
>> +{
>> + return (((uintptr_t) base | size) & (align - 1)) == 0;
>> +}
>> +
>> +static void
>> +swap_words_64 (void * restrict a, void * restrict b, size_t n)
>> +{
>> + do
>> + {
>> + n -= 8;
>> + uint64_t t = *(uint64_t *)(a + n);
>> + *(uint64_t *)(a + n) = *(uint64_t *)(b + n);
>> + *(uint64_t *)(b + n) = t;
>> + } while (n);
>> +}
>
> The undefined behavior resulting from attempt to access via an 'uint64_t *'
> something of a different type is known to cause miscompilation in practice:
> https://www.spec.org/cpu2017/Docs/benchmarks/605.mcf_s.html#portability
>
> I see that the new string routines do it properly via
>
> typedef unsigned long int __attribute__ ((__may_alias__)) op_t;
>
> Would be nice to avoid new instances of such UB here.
Ack.
next prev parent reply other threads:[~2023-07-18 14:01 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-13 13:25 [PATCH v5 0/6] Use introsort for qsort Adhemerval Zanella
2023-07-13 13:25 ` [PATCH v5 1/6] stdlib: Optimization qsort{_r} swap implementation Adhemerval Zanella
2023-07-13 22:17 ` Paul Eggert
2023-07-14 1:01 ` Paul Eggert
2023-07-14 12:06 ` Adhemerval Zanella Netto
2023-07-17 16:57 ` Alexander Monakov
2023-07-17 17:29 ` Paul Eggert
2023-07-17 18:07 ` Adhemerval Zanella Netto
2023-07-17 18:58 ` Paul Eggert
2023-07-17 19:06 ` Adhemerval Zanella Netto
2023-07-18 14:01 ` Adhemerval Zanella Netto [this message]
2023-07-18 14:13 ` Adhemerval Zanella Netto
2023-07-13 13:25 ` [PATCH v5 2/6] stdlib: Move insertion sort out qsort Adhemerval Zanella
2023-07-13 13:25 ` [PATCH v5 3/6] stdlib: qsort: Move some macros to inline function Adhemerval Zanella
2023-07-13 13:25 ` [PATCH v5 4/6] stdlib: Implement introsort with qsort (BZ 19305) Adhemerval Zanella
2023-07-13 13:25 ` [PATCH v5 5/6] stdlib: Remove use of mergesort on qsort (BZ 21719) Adhemerval Zanella
2023-07-13 13:25 ` [PATCH v5 6/6] stdlib: Add more qsort{_r} coverage 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=85afee5d-69f7-f070-58a4-08251c07db96@linaro.org \
--to=adhemerval.zanella@linaro.org \
--cc=amonakov@ispras.ru \
--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).