🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #752

Re: Large constants in RISC

#752
From: Waldek Hebisch
Date: Sun, 15 Nov 2026 16:23
82 lines
5023 bytes
John Levine <johnl@taugh.com> wrote:
> It appears that BGB  <cr88192@gmail.com> said:
>>> I read what he wrote and I do not doubt that code having constants
>>> in instructions is faster.  What is not clear how frequently
>>> code with explicit constants appears in real life.
>>> 
>>
>>It depends, for 3RI forms IME:
>>   ~ 93% .. 96% fit in 10 bits;
>>   ~ 95% .. 98% fit into 12 bits.
>>
>>Of those that miss, there is a clustering patters:
>>   Big spike near 16 bits;
>>   Another smaller spike near 32 bits;
>>   Very little else up until around 64 bits.
>>
>>The vast majority fall along a "near zero" curve, with a fairly steep 
>>falloff. ...
> 
> That tells me that it's not a good use of instruction bits to try to
> encode large constants. For the 3% of constants that can't be encoded
> in 12 bits, use a constant pool.

Other sources and my experience indicates higher percentage of
constants.  I have a simple code generator for Risc-V.  From
relevant sources it generates 917544 bytes of object code.
Assembly contains 13906 instances of 'la' and 8326 instances
of 'lui'.  I am not sure how many istructions are there, but
reasonable estimante is about 250000.  So probably about 8%
of instructions needs constants bigger than 12 bit.  Changing
from literal pool to 'la' and 'lui' reduced total size by some
percents.  Maybe not big thing, but it shows that constants
in instructions save total bits needed for program.  Also,
constan pools decrease efficiency of caches.  Namely, if
a line shares code and data, then it needs to be loaded to
both caches, possibly causing expensive changes of ownership
during accesses.  If you put code and data in separtate
lines, then there is fragmentation.

ATM for constants bigger than what can by handled by 'la'
and 'lui' I use constant pools.  Note that in my use case
I set up apropriate base register for other reasons, so
base register has no extra cost.  Otherwise there would
be extra cost, that is I would have to use 'la' to load
address of the constant and then use it to load constant
itself.  Of course if possible it is better to directly
load needed address using 'la' instead of having
extra indirection.

> FYI, that's basically what S/360 did.  The load address instruction
> has a 12 bit immediate field  you can use to load 12 bit constants,
> other than that you're on your own.  S/390 added a load immediate with
> a 16 bit signed value, z added load immediates with a 32 bit operand,
> and versions that load the high or low halves of a regster, so with
> two 48-bit instructions you can load any 64 bit value.

360 is not a good example to follow.  Initial software conventions
were quite inefficient.  Later they adapted better conventions
and new instructions helped too.  But IBM literature warned of
possible performance troubles due to literal pool (need to
keep literal pool at sufficient distance from code so that
it will use separate cache lines).  Beside efficiency there
is also aspect of software complexity.  Part of RISC philosophy
seem the be that compilers can cover up hardware limitations.
But AFAIK dealing with case when register allocator runs
out of registers trying to generate spill code was a reccuring
source of bugs in gcc.  This is related because to spill one
needs to generate store with constant offset, and when this
constant can not be part of instruction one need a free
register to generate spill code (but you spill because you
are running out of free registers!).  In case of 360 I
remember compiler bug discovered by a test program.
Relevant routine IIRC was slightly larger than 64 kB
(or maybe 32 kB) so gcc tried to split constant pool so
that it was in range of available displacements.  But
initial split was not good and I got an ICE.  This was
fixed, but shows that low frequency probelematic cases
may have negligible impact on average performance, but
offer plenty of space where bugs may hide and bit later.

-- 
                              Waldek Hebisch
Technical Details
Message-ID: <11dcmae$1lbn2$1@paganini.bofh.team>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
References: <11d76uq$11cdc$1@paganini.bofh.team> <11d9d0d$1rsce$1@dont-email.me> <11d9tq8$1b9c1$2@paganini.bofh.team> <11dabct$26fdl$1@dont-email.me> <11dahac$2t74$1@gal.iecc.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
From: antispam@fricas.org (Waldek Hebisch)
Newsgroups: comp.arch
Subject: Re: Large constants in RISC
Date: Sun, 15 Nov 2026 16:23:44 -0000 (UTC)
Organization: bofh.team Usenet service
Message-ID: <11dcmae$1lbn2$1@paganini.bofh.team>
References: <11d76uq$11cdc$1@paganini.bofh.team> <11d9d0d$1rsce$1@dont-email.me> <11d9tq8$1b9c1$2@paganini.bofh.team> <11dabct$26fdl$1@dont-email.me> <11dahac$2t74$1@gal.iecc.com>
Injection-Date: Sun, 15 Nov 2026 16:23:44 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="1748706"; mail-complaints-to="usenet@bofh.team"; posting-host="d028f9ad2fa2c51f22dbfed0d81d7daf"; posting-account="69e66f1681224ab92235b464ce7a90b6"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
X-Postfilter: Postfilter-NG 2026.10.2