public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Paul Cercueil <paul@crapouillou.net>
To: "Maciej W. Rozycki" <macro@orcam.me.uk>
Cc: "Jiri Slaby" <jslaby@suse.cz>,
	"David Laight" <David.Laight@ACULAB.COM>,
	"'Uwe Kleine-König'" <u.kleine-koenig@pengutronix.de>,
	gregkh@linuxfoundation.org,
	"Alexandre Belloni" <alexandre.belloni@bootlin.com>,
	"Mateusz Holenko" <mholenko@antmicro.com>,
	"Neil Armstrong" <narmstrong@baylibre.com>,
	"Benjamin Herrenschmidt" <benh@kernel.crashing.org>,
	"Liviu Dudau" <liviu.dudau@arm.com>,
	"Baruch Siach" <baruch@tkos.co.il>,
	linux-kernel@vger.kernel.org, "Paul Mackerras" <paulus@samba.org>,
	"Michael Ellerman" <mpe@ellerman.id.au>,
	"Michal Simek" <michal.simek@xilinx.com>,
	"Karol Gugala" <kgugala@antmicro.com>,
	"Jerome Brunet" <jbrunet@baylibre.com>,
	"Peter Korsgaard" <peter@korsgaard.com>,
	"Florian Fainelli" <f.fainelli@gmail.com>,
	"Alexander Shiyan" <shc_work@mail.ru>,
	"Krzysztof Kozlowski" <krzysztof.kozlowski@canonical.com>,
	"Alexandre Torgue" <alexandre.torgue@foss.st.com>,
	"Fabio Estevam" <festevam@gmail.com>,
	"Russell King" <linux@armlinux.org.uk>,
	"Ludovic Desroches" <ludovic.desroches@microchip.com>,
	"Andy Gross" <agross@kernel.org>,
	bcm-kernel-feedback-list@broadcom.com,
	"NXP Linux Team" <linux-imx@nxp.com>,
	linux-serial@vger.kernel.org, "Vineet Gupta" <vgupta@kernel.org>,
	"Orson Zhai" <orsonzhai@gmail.com>,
	"Tobias Klauser" <tklauser@distanz.ch>,
	"Patrice Chotard" <patrice.chotard@foss.st.com>,
	"Albert Ou" <aou@eecs.berkeley.edu>,
	"Maxime Coquelin" <mcoquelin.stm32@gmail.com>,
	"Manivannan Sadhasivam" <mani@kernel.org>,
	"Martin Blumenstingl" <martin.blumenstingl@googlemail.com>,
	"Sascha Hauer" <s.hauer@pengutronix.de>,
	"Takao Orito" <orito.takao@socionext.com>,
	"Vladimir Zapolskiy" <vz@mleia.com>,
	"Lorenzo Pieralisi" <lorenzo.pieralisi@arm.com>,
	"Paul Walmsley" <paul.walmsley@sifive.com>,
	"Bjorn Andersson" <bjorn.andersson@linaro.org>,
	"Sudeep Holla" <sudeep.holla@arm.com>,
	"Richard Genoud" <richard.genoud@gmail.com>,
	"Chunyan Zhang" <zhang.lyra@gmail.com>,
	"Nicolas Ferre" <nicolas.ferre@microchip.com>,
	"David S. Miller" <davem@davemloft.net>,
	"Taichi Sugaya" <sugaya.taichi@socionext.com>,
	"Palmer Dabbelt" <palmer@dabbelt.com>,
	"Pengutronix Kernel Team" <kernel@pengutronix.de>,
	"Kevin Hilman" <khilman@baylibre.com>,
	"Baolin Wang" <baolin.wang7@gmail.com>,
	"Shawn Guo" <shawnguo@kernel.org>,
	"Andreas Färber" <afaerber@suse.de>
Subject: Re: [PATCH v3] serial: make uart_console_write->putchar()'s character an unsigned char
Date: Thu, 03 Mar 2022 10:22:59 +0000	[thread overview]
Message-ID: <BIZ58R.9EXA9J3HVHS13@crapouillou.net> (raw)
In-Reply-To: <alpine.DEB.2.21.2203030738170.56670@angie.orcam.me.uk>

Hi Maciej,

Le jeu., mars 3 2022 at 09:55:17 +0000, Maciej W. Rozycki 
<macro@orcam.me.uk> 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



  reply	other threads:[~2022-03-03 10:23 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-02  7:27 [PATCH v3] serial: make uart_console_write->putchar()'s character an unsigned char Jiri Slaby
2022-03-02 17:52 ` Uwe Kleine-König
2022-03-02 23:57   ` David Laight
2022-03-03  7:06     ` Jiri Slaby
2022-03-03  9:55       ` Maciej W. Rozycki
2022-03-03 10:22         ` Paul Cercueil [this message]
2022-03-03 11:30           ` Maciej W. Rozycki
2022-03-03 11:35             ` Jiri Slaby
2022-03-03 11:44             ` David Laight
2022-03-03 12:27               ` Paul Cercueil
2022-03-03 10:23         ` David Laight
2022-03-31 22:59           ` Maciej W. Rozycki
2022-03-02 18:21 ` Vladimir Zapolskiy
2022-03-03  6:32 ` Jiri Slaby
2022-03-03  7:45   ` Greg KH
2022-03-03  7:49     ` Jiri Slaby
2022-03-03  8:07       ` Greg KH
2022-03-03  8:09         ` Jiri Slaby

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=BIZ58R.9EXA9J3HVHS13@crapouillou.net \
    --to=paul@crapouillou.net \
    --cc=David.Laight@ACULAB.COM \
    --cc=afaerber@suse.de \
    --cc=agross@kernel.org \
    --cc=alexandre.belloni@bootlin.com \
    --cc=alexandre.torgue@foss.st.com \
    --cc=aou@eecs.berkeley.edu \
    --cc=baolin.wang7@gmail.com \
    --cc=baruch@tkos.co.il \
    --cc=bcm-kernel-feedback-list@broadcom.com \
    --cc=benh@kernel.crashing.org \
    --cc=bjorn.andersson@linaro.org \
    --cc=davem@davemloft.net \
    --cc=f.fainelli@gmail.com \
    --cc=festevam@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jbrunet@baylibre.com \
    --cc=jslaby@suse.cz \
    --cc=kernel@pengutronix.de \
    --cc=kgugala@antmicro.com \
    --cc=khilman@baylibre.com \
    --cc=krzysztof.kozlowski@canonical.com \
    --cc=linux-imx@nxp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=liviu.dudau@arm.com \
    --cc=lorenzo.pieralisi@arm.com \
    --cc=ludovic.desroches@microchip.com \
    --cc=macro@orcam.me.uk \
    --cc=mani@kernel.org \
    --cc=martin.blumenstingl@googlemail.com \
    --cc=mcoquelin.stm32@gmail.com \
    --cc=mholenko@antmicro.com \
    --cc=michal.simek@xilinx.com \
    --cc=mpe@ellerman.id.au \
    --cc=narmstrong@baylibre.com \
    --cc=nicolas.ferre@microchip.com \
    --cc=orito.takao@socionext.com \
    --cc=orsonzhai@gmail.com \
    --cc=palmer@dabbelt.com \
    --cc=patrice.chotard@foss.st.com \
    --cc=paul.walmsley@sifive.com \
    --cc=paulus@samba.org \
    --cc=peter@korsgaard.com \
    --cc=richard.genoud@gmail.com \
    --cc=s.hauer@pengutronix.de \
    --cc=shawnguo@kernel.org \
    --cc=shc_work@mail.ru \
    --cc=sudeep.holla@arm.com \
    --cc=sugaya.taichi@socionext.com \
    --cc=tklauser@distanz.ch \
    --cc=u.kleine-koenig@pengutronix.de \
    --cc=vgupta@kernel.org \
    --cc=vz@mleia.com \
    --cc=zhang.lyra@gmail.com \
    /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