From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 81EEBC433F5 for ; Thu, 3 Mar 2022 10:23:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232289AbiCCKYJ convert rfc822-to-8bit (ORCPT ); Thu, 3 Mar 2022 05:24:09 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232241AbiCCKYF (ORCPT ); Thu, 3 Mar 2022 05:24:05 -0500 Received: from aposti.net (aposti.net [89.234.176.197]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E58A75EBDD for ; Thu, 3 Mar 2022 02:23:18 -0800 (PST) Date: Thu, 03 Mar 2022 10:22:59 +0000 From: Paul Cercueil Subject: Re: [PATCH v3] serial: make uart_console_write->putchar()'s character an unsigned char To: "Maciej W. Rozycki" Cc: Jiri Slaby , David Laight , 'Uwe =?iso-8859-1?q?Kleine-K=F6nig=27?= , gregkh@linuxfoundation.org, Alexandre Belloni , Mateusz Holenko , Neil Armstrong , Benjamin Herrenschmidt , Liviu Dudau , Baruch Siach , linux-kernel@vger.kernel.org, Paul Mackerras , Michael Ellerman , Michal Simek , Karol Gugala , Jerome Brunet , Peter Korsgaard , Florian Fainelli , Alexander Shiyan , Krzysztof Kozlowski , Alexandre Torgue , Fabio Estevam , Russell King , Ludovic Desroches , Andy Gross , bcm-kernel-feedback-list@broadcom.com, NXP Linux Team , linux-serial@vger.kernel.org, Vineet Gupta , Orson Zhai , Tobias Klauser , Patrice Chotard , Albert Ou , Maxime Coquelin , Manivannan Sadhasivam , Martin Blumenstingl , Sascha Hauer , Takao Orito , Vladimir Zapolskiy , Lorenzo Pieralisi , Paul Walmsley , Bjorn Andersson , Sudeep Holla , Richard Genoud , Chunyan Zhang , Nicolas Ferre , "David S. Miller" , Taichi Sugaya , Palmer Dabbelt , Pengutronix Kernel Team , Kevin Hilman , Baolin Wang , Shawn Guo , Andreas =?iso-8859-1?q?F=E4rber?= Message-Id: In-Reply-To: References: <20220302072732.1916-1-jslaby@suse.cz> <20220302175242.ejiaf36vszr4xvou@pengutronix.de> <5c7045c1910143e08ced432d938b5825@AcuMS.aculab.com> <84ad3854-28b9-e450-f0a2-f1448f32f137@suse.cz> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: 8BIT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Maciej, Le jeu., mars 3 2022 at 09:55:17 +0000, Maciej W. Rozycki a écrit : > On Thu, 3 Mar 2022, Jiri Slaby wrote: > >> > The real problem is that using char (or short) for a function >> parameter >> > or result is very likely to require the compile add code to mask >> > the value to 8 (or 16) bits. >> > >> > Remember that almost every time you do anything with a signed or >> unsigned >> > char/short variable the compiler has to use the integer promotion >> rules >> > to convert the value to int. >> > >> > You'll almost certainly get better code if the value is left in an >> > int (or unsigned int) variable until the low 8 bits get written to >> > a buffer (or hardware register). >> >> So should we use int/uint instead of more appropriate shorter types >> everywhere >> now? The answer is: definitely not. The assembly on x86 looks good >> (it uses >> movz, no ands), RISC architectures have to do what they chose to. > > We do have an issue, because we still have this: > > void uart_console_write(struct uart_port *port, const char *s, > unsigned int count, > void (*putchar)(struct uart_port *, int)) > > and then: > > putchar(port, *s); > > there. Consequently on targets where plain `char' type is signed the > value retrieved from `*s' has to be truncated in the call to > `putchar'. > And indeed it happens with the MIPS target: > > 803ae47c: 82050000 lb a1,0(s0) > 803ae480: 26100001 addiu s0,s0,1 > 803ae484: 02402025 move a0,s2 > 803ae488: 0220f809 jalr s1 > 803ae48c: 30a500ff andi a1,a1,0xff > > vs current code: > > 803ae47c: 82050000 lb a1,0(s0) > 803ae480: 26100001 addiu s0,s0,1 > 803ae484: 0220f809 jalr s1 > 803ae488: 02402025 move a0,s2 And how is that at all a problem? > (NB the last instruction shown after the call instruction, JALR, is > in the > delay slot that is executed before the PC gets updated). Now > arguably the > compiler might notice that and use an unsigned LBU load instruction > rather > than the signed LB load instruction, which would make the ANDI > instruction > redundant, but still I think we ought to avoid gratuitous type > signedness > changes. > > So I'd recommend changing `s' here to `const unsigned char *' or, as > I > previously suggested, maybe to `const u8 *' even. Just cast the string to "const u8 *" within the function, while keeping a "const char *s" argument. The compiler will then most likely generate LBUs. Cheers, -Paul