From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from zimbra.cs.ucla.edu (zimbra.cs.ucla.edu [131.179.128.68]) by sourceware.org (Postfix) with ESMTPS id D76683858427; Fri, 13 May 2022 04:56:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D76683858427 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=cs.ucla.edu Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=cs.ucla.edu Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id A142316007E; Thu, 12 May 2022 21:56:02 -0700 (PDT) Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id mnoMb01sQhsO; Thu, 12 May 2022 21:56:01 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id D9C54160091; Thu, 12 May 2022 21:56:01 -0700 (PDT) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id fQKmG2ZmYTlv; Thu, 12 May 2022 21:56:01 -0700 (PDT) Received: from [192.168.1.9] (cpe-172-91-119-151.socal.res.rr.com [172.91.119.151]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id A89BC16007E; Thu, 12 May 2022 21:56:01 -0700 (PDT) Message-ID: <4ffe566a-8002-b574-daee-d6927b8ceaef@cs.ucla.edu> Date: Thu, 12 May 2022 21:56:01 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.8.1 Content-Language: en-US To: Siddhesh Poyarekar Cc: adhemerval.zanella@linaro.org, fweimer@redhat.com, dickey@his.com, libc-alpha@sourceware.org References: <20220505184348.3357550-3-siddhesh@sourceware.org> <20220512131503.764504-1-siddhesh@sourceware.org> From: Paul Eggert Organization: UCLA Computer Science Department Subject: Re: [PATCH v3] wcrtomb: Make behavior POSIX compliant In-Reply-To: <20220512131503.764504-1-siddhesh@sourceware.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.8 required=5.0 tests=BAYES_00, KAM_DMARC_STATUS, NICE_REPLY_A, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 13 May 2022 04:56:07 -0000 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.)