[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <fe6c2f646cf1468c89622cc1d848ae0c@AcuMS.aculab.com>
Date: Wed, 23 Jun 2021 09:05:48 +0000
From: David Laight <David.Laight@...LAB.COM>
To: 'Matteo Croce' <mcroce@...ux.microsoft.com>
CC: Nick Kossifidis <mick@....forth.gr>,
"linux-riscv@...ts.infradead.org" <linux-riscv@...ts.infradead.org>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
"linux-arch@...r.kernel.org" <linux-arch@...r.kernel.org>,
Paul Walmsley <paul.walmsley@...ive.com>,
Palmer Dabbelt <palmer@...belt.com>,
Albert Ou <aou@...s.berkeley.edu>,
Atish Patra <atish.patra@....com>,
Emil Renner Berthing <kernel@...il.dk>,
Akira Tsukamoto <akira.tsukamoto@...il.com>,
Drew Fustini <drew@...gleboard.org>,
Bin Meng <bmeng.cn@...il.com>, Guo Ren <guoren@...nel.org>
Subject: RE: [PATCH v3 3/3] riscv: optimized memset
From: Matteo Croce
> Sent: 23 June 2021 02:15
>
> On Tue, Jun 22, 2021 at 10:38 AM David Laight <David.Laight@...lab.com> wrote:
> >
> > From: Nick Kossifidis
...
> > You can just write:
> > cu = (unsigned long)c * 0x0101010101010101ull;
> > and let the compiler sort out the best way to generate the constant.
> >
>
> Interesting. I see that most compilers do an integer multiplication,
> is it faster than three shift and three or?
>
> clang on riscv generates even more instructions to create the immediate:
>
> unsigned long repeat_shift(int c)
> {
> unsigned long cu = (unsigned long)c;
> cu |= cu << 8;
> cu |= cu << 16;
> cu |= cu << 32;
>
> return cu;
> }
>
> unsigned long repeat_mul(int c)
> {
> return (unsigned long)c * 0x0101010101010101ull;
> }
>
> repeat_shift:
> slli a1, a0, 8
> or a0, a0, a1
> slli a1, a0, 16
> or a0, a0, a1
> slli a1, a0, 32
> or a0, a0, a1
> ret
>
> repeat_mul:
> lui a1, 4112
> addiw a1, a1, 257
> slli a1, a1, 16
> addi a1, a1, 257
> slli a1, a1, 16
> addi a1, a1, 257
> mul a0, a0, a1
> ret
Hmmm... I expected the compiler to convert it to the first form.
It is also pretty crap at generating that constant.
Stupid compilers.
In any case, for the usual case of 'c' being a constant zero
you really don't want the latency of those instructions at all.
It is almost worth just pushing that expansion into the caller.
eg by having:
#define memset(p, v, l) memset_w(p, (v) * 0x0101010101010101, l)
(or some other byte replicator).
Really annoyingly you want to write the code that generates
the 64bit constant, and then have the compiler optimise away
the part that generates the high 32 bits on 32 bits systems.
But one of the compilers is going to 'bleat' about truncating
a constant value.
Stupid compilers (again).
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Powered by blists - more mailing lists