public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Siddhesh Poyarekar <siddhesh@sourceware.org>
Cc: adhemerval.zanella@linaro.org, fweimer@redhat.com,
	dickey@his.com, libc-alpha@sourceware.org
Subject: Re: [PATCH v3] wcrtomb: Make behavior POSIX compliant
Date: Thu, 12 May 2022 21:56:01 -0700	[thread overview]
Message-ID: <4ffe566a-8002-b574-daee-d6927b8ceaef@cs.ucla.edu> (raw)
In-Reply-To: <20220512131503.764504-1-siddhesh@sourceware.org>

On 5/12/22 06:15, Siddhesh Poyarekar wrote:
> +	  switch (result)
> +	    {
> +	    case 4:
> +	      s[3] = buf[3];
> +	      /* Fall through.  */
> +	    case 3:
> +	      s[2] = buf[2];
> +	      /* Fall through.  */
> +	    case 2:
> +	      s[1] = buf[1];
> +	      /* Fall through.  */
> +	    case 1:
> +	      s[0] = buf[0];
> +	      break;
> +	    default:
> +	      memcpy (s, buf, result);
> +	    }

For me with GCC 12.1 -O2 on x86-64, the above code generates 2 
comparisons and 3 conditional branches in the common case where RESULT 
is 1. Plus, GCC generates a jmp from the end of case 3 to the start of 
case 2 (which precedes case 3 in the machine code), which is a bit odd.

How about something like the following instead? This generates machine 
code with only one conditional branch - the one that decides whether to 
call memcpy - and this branch is never taken with glibc-supplied 
charmaps. (I'm assuming RESULT is at least 1.)

   s[0] = buf[0];
   if (2 <= result && result <= 4)
     {
       s[1] = buf[1];
       memcpy (&s[result != 2], &buf[result != 2], 2);
       s[result - 1] = buf[result - 1];
     }
   else
     memcpy (s, buf, result);

Hope you don't mind my bikeshedding. (PATCH v3 looks correct as-is, for 
what it's worth.)

  reply	other threads:[~2022-05-13  4:56 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-07  6:26 [RFC] _FORTIFY_SOURCE strictness Siddhesh Poyarekar
2022-04-07 10:16 ` Andreas Schwab
2022-04-08  3:24   ` Siddhesh Poyarekar
2022-04-08  2:26 ` Paul Eggert
2022-04-08  3:32   ` Siddhesh Poyarekar
2022-04-08  5:37 ` Florian Weimer
2022-04-08  6:02   ` Siddhesh Poyarekar
2022-04-08 21:07     ` Paul Eggert
2022-04-11  8:02       ` Siddhesh Poyarekar
2022-05-05 18:43         ` [PATCH 0/2] More compliant wcrtomb Siddhesh Poyarekar
2022-05-05 18:43           ` [PATCH 1/2] benchtests: Add wcrtomb microbenchmark Siddhesh Poyarekar
2022-05-06  9:10             ` Florian Weimer
2022-05-06 12:49               ` [committed] " Siddhesh Poyarekar
2022-05-06 12:50             ` [PATCH 1/2] " Adhemerval Zanella
2022-05-06 12:59               ` Siddhesh Poyarekar
2022-05-06 13:20                 ` Adhemerval Zanella
2022-05-06 13:26                   ` Siddhesh Poyarekar
2022-05-06 13:36                     ` Siddhesh Poyarekar
2022-05-06 13:46                       ` Adhemerval Zanella
2022-05-05 18:43           ` [PATCH 2/2] wcrtomb: Make behavior POSIX compliant Siddhesh Poyarekar
2022-05-06  9:25             ` Paul Eggert
2022-05-06 13:40               ` Adhemerval Zanella
2022-05-06 13:46                 ` Siddhesh Poyarekar
2022-05-06 14:04             ` [PATCH v2] " Siddhesh Poyarekar
2022-05-09 13:22               ` Adhemerval Zanella
2022-05-09 13:35                 ` Siddhesh Poyarekar
2022-05-12 13:15             ` [PATCH v3] " Siddhesh Poyarekar
2022-05-13  4:56               ` Paul Eggert [this message]
2022-05-13  5:28                 ` Paul Eggert
2022-05-13 11:31                   ` Siddhesh Poyarekar
2022-05-13 11:38                     ` Florian Weimer
2022-05-13 11:51                       ` Siddhesh Poyarekar
2022-05-13 12:55                         ` Florian Weimer
2022-05-13 12:30                       ` Adhemerval Zanella
2022-05-13 13:42                         ` Siddhesh Poyarekar
2022-05-13 17:58                           ` Paul Eggert
2022-05-13 13:45                         ` [committed] " Siddhesh Poyarekar
2022-05-13  8:18                 ` [PATCH v3] " Siddhesh Poyarekar

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=4ffe566a-8002-b574-daee-d6927b8ceaef@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=adhemerval.zanella@linaro.org \
    --cc=dickey@his.com \
    --cc=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    --cc=siddhesh@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).