Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp1427151pxb; Fri, 1 Apr 2022 13:11:15 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwq67NaD/gATS6onqUwStW8Nu1j0ha6OoDC2TTsQVdzGG3MP/EFDI3uQJQ3H69JZPUdZsRd X-Received: by 2002:a17:902:b782:b0:153:e0dc:755e with SMTP id e2-20020a170902b78200b00153e0dc755emr12174123pls.1.1648843875611; Fri, 01 Apr 2022 13:11:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1648843875; cv=none; d=google.com; s=arc-20160816; b=RgRY+kNvCbvGIzMGw6OpBu0uHODEDbbW3pn3giER96C6Dx0tzLs2Pr7CUHe1m6P+6b 5hBiJ84ZI8i5OrLA5RlO1gQM/SQ6nqqI121hx8WvrQxvjhjd6fQulZuvWARR7hmYuWOE rqBQpauIApUFw9qTnUXFLcTLE2xzaCbM/YeqfUSRwjtUnfksZ2ycJQ3gjzUv7s8MviAR XWoMTCODJsIaqchVEnebOrExen/7hlnZfcmLUfKCDC2yJ7IRqSS/8Mt70yfcbGRnXpGQ mrdo2EBtAAgNyVxW0FeuHsPY36N/fSI9fCE471t9aW0LWEm35xb3ukf++DCxdoDsghrh kAAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:user-agent:references:message-id :in-reply-to:subject:cc:to:from:date; bh=bJg1hMyerwBhcxOjLxCALN/n2KMB+DIM0WYoSnCtqso=; b=XctQE0BGbCBT33t90/YPIgfXjLJuXQMTCeIqBGnr+dKzHRxb3/78l1oEKbZNCjBOS5 tAEFddp1xRQ0pVMtRcGDqOFSkczUJDoCLz1iYj3Ld01usQnbHoWm/4S2E6HCErHLQO2D AGYxcMJqiTEvCLh7YC/W+73qLaY94VJr440TEb2WajIklKqh9cqICsOi/Q1G96SF/ptd hhA36BpcAv07hu59IiXql2y5w0T/mSwUvlK+J2HZtHrBRJQK8KAfM4JGyG1Aq8D4wi82 kc4wIlZwVB5aqiqQNmPqjxXZ41eFXfli37RQMufc2zM5fabRpZ5rW76e05e8iuA7vXPi 4yyw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e18-20020a656792000000b003986bd2b597si3400091pgr.38.2022.04.01.13.11.00; Fri, 01 Apr 2022 13:11:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240089AbiCaXBl (ORCPT + 99 others); Thu, 31 Mar 2022 19:01:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229506AbiCaXBk (ORCPT ); Thu, 31 Mar 2022 19:01:40 -0400 Received: from angie.orcam.me.uk (angie.orcam.me.uk [78.133.224.34]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B9DAB25E2; Thu, 31 Mar 2022 15:59:51 -0700 (PDT) Received: by angie.orcam.me.uk (Postfix, from userid 500) id B6FED92009C; Fri, 1 Apr 2022 00:59:49 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by angie.orcam.me.uk (Postfix) with ESMTP id A807E92009B; Thu, 31 Mar 2022 23:59:49 +0100 (BST) Date: Thu, 31 Mar 2022 23:59:49 +0100 (BST) From: "Maciej W. Rozycki" To: David Laight cc: Jiri Slaby , =?UTF-8?Q?'Uwe_Kleine-K=C3=B6nig'?= , "gregkh@linuxfoundation.org" , Alexandre Belloni , Mateusz Holenko , Neil Armstrong , Benjamin Herrenschmidt , Liviu Dudau , Baruch Siach , "linux-kernel@vger.kernel.org" , Paul Cercueil , 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 , =?UTF-8?Q?Andreas_F=C3=A4rber?= Subject: RE: [PATCH v3] serial: make uart_console_write->putchar()'s character an unsigned char In-Reply-To: <20f24492d4944de4986245c59e5be71f@AcuMS.aculab.com> Message-ID: References: <20220302072732.1916-1-jslaby@suse.cz> <20220302175242.ejiaf36vszr4xvou@pengutronix.de> <5c7045c1910143e08ced432d938b5825@AcuMS.aculab.com> <84ad3854-28b9-e450-f0a2-f1448f32f137@suse.cz> <20f24492d4944de4986245c59e5be71f@AcuMS.aculab.com> User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_NONE,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 lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 3 Mar 2022, David Laight wrote: > > 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 > > > > (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. > > Or just not worry that the 'char' value (either [128..127] or [0..255]) > is held in a 'signed int' variable. > That basically happens every time it is loaded into a register anyway. That might be true with a hypothetical 8-bit ABI on top of a higher-width machine architecture. It does happen with the 32-bit MIPS ABI (o32) and a 64-bit architecture, which is why LW (load word signed) is a universal 32-bit and 64-bit instruction while the LWU (load word unsigned) operation is restricted to 64-bit code. In this case however a signed `char' value ([-128..127]) is sign-extended while an unsigned `char' value ([0..255]) is zero-extended, even though both are carried in a 'signed int' variable from the architecture's point of view. Anyway I have looked into it some more and the immediate cause for LBU not to be used here is the: if (*s == '\n') putchar(port, '\r'); conditional. If this part is removed, then LBU does get used for the: putchar(port, *s); part and no ANDI is produced. The reason for that is that the compiler decides to reuse the load used to evaluate (*s == '\n') (which is done using the plain `char' data type) for the following `putchar(port, *s)' call if the expression used as the condition turns out to be false and therefore the value of `*s' has to be subsequently zero-extended: b4: 00e08825 move s1,a3 b8: 2413000a li s3,10 bc: 82050000 lb a1,0(s0) c0: 00000000 nop c4: 14b30005 bne a1,s3,dc c8: 00000000 nop cc: 2405000d li a1,13 d0: 0220f809 jalr s1 d4: 02402025 move a0,s2 d8: 82050000 lb a1,0(s0) dc: 26100001 addiu s0,s0,1 e0: 02402025 move a0,s2 e4: 0220f809 jalr s1 e8: 30a500ff andi a1,a1,0xff (the load at bc is reused for the `putchar' call at e4 unless it's `\n', or otherwise the character is reloaded at d8). By using a temporary `unsigned char' variable and massaging the source code suitably GCC can be persuaded to use LBU instead, but the obfuscation of the source code and the resulting machine code produced seem not worth the effort IMO, so let's keep it simple. JFTR, Maciej