🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #773

Re: Large constants in RISC

#773
From: Anton Ertl
Date: Mon, 16 Nov 2026 07:24
138 lines
7207 bytes
BGB <cr88192@gmail.com> writes:
>On 11/15/2026 10:08 AM, Anton Ertl wrote:
>> The nice thing about the load is that its address is usually available
>> right away.  So the load operation is typically performed as soon as
>> possible, and for a sequence
>> 
>> load r1, d(rct)
>> add r3=r1+r2
>> 
>> where rct is the register pointing to the constant table, the result
>> may be ready in r1 before r2 is ready.
>> 
>> The bottom line is that this load typically does not cost latency.
>> 
>
>If loads have a 3 or 4 cycle latency and are placed directly before the 
>ALU op, on an in-order machine, one needs to pay for it...

Sure.  That's why you use compiler instruction scheduling for in-order
machines.  The address is available from at least the start of the
function, and the load is known not to produce an exception, so the
load can be scheduled up to the start of the function if desired.
This would cost a register until the last use of the constant, but
would save loads if the constant is used several times.  In any case,
the compiler can schedule the constant load as is convenient, and the
only cost is the opportunity cost of not being able to use the
instruction slot and load unit for something else, and the cost of
occupying the register from the load to the last use (another
opportunity cost).

>Even on OoO machines (like x86), having a load directly followed by an 
>operation does often still have a visibly higher latency cost than if 
>they are spread out.

Often?  How often?  Evidence?

In any case, for loading a constant, scheduling the load earlier can
be done without problem, as discussed above.

>Where:
>   Load -> ALU
>Seemingly a lot cheaper than:
>   Load -> CMP -> Jcc

If the branch is predicted correctly (the usual case), the cost of the
latter is just that the instruction slots and functional units are
occupied.  And the difference from having the constant in the CMP
instrction is just that the load unit is occupied, the other costs are
the same.

>This case does appear to be a place where I am seeing a difference 
>between my current PC's Xen+ CPU and a Core i7 though, as the "Load -> 
>CMP -> Jcc" path appears to be cheaper on the i7 vs Xen+; but with the 
>relative cost of the 'Jcc' in isolation being higher on the i7.

Unless you have compared these sequences in a very large variety of
settings and done a statistic over the results to come to this
conclusion, it is likely that you are seeing some other effect, rather
than anything specific to these instruction sequences.

Given that you write "a Core i7", as if this marketing name had any
technical meaning, I doubt that you have done such a well-founded
empirical evaluation.  The "Core i7" name has been given to about 11
different basic CPU designs, some of which contain cores with two
different microarchitectures; and the same 11 CPU designs have also
been given other names, such as "Core i5", "Core i3", "Xeon W", "Xeon
E", "Pentium", or "Celeron"; not all names were used for all CPU
designs, but in any case these marketing names don't tell us anything
useful.

By contrast, "Zen+" is a specific CPU design.

>> Concerning cache misses: If the code is executed often, the constant
>> will be in the (L1) D-Cache.  If there is a cache miss, the code has
>> not been executed recently.  Bottom line: Cache misses are rare,
>> because caches work.
>> 
>
>However, the D$ puts it in conflict with whatever other data the program 
>is using, and D$ tends to be a much more volatile space than I$ IME.
>
>So, if the constants are inline, they are almost always going to hit if 
>the loop is already warm or similar. For D$, one is more at the mercy of 
>whether some of the working data knocked it out of the cache.

With a typical 8-way or 12-way set-associative D-cache with pseudo-LRU
replacement, and the constant being accessed in a hot loop, it would
need multiple accesses to the same set between the accesses to the
constant in order to evict the constant.  Either the constant is not
used that often, or it will stay in the D-cache.

Of course, there is still the cost that the other data does not have
that cache line available for its purposes.  Nothing is totally free.

>But, in any case, generic memory loads for constants kinda sucks with a 
>direct-mapped L1 cache.

Direct-mapped caches suck.  No qualification needed.

>>> This sort of thing can be a bigger saving though for things like
>>> accessing global variables (where the existing options "generally
>>> suck"). Typically, an unscaled Disp12 falls short of being able to
>>> directly address all of the commonly-used global variables in a typical
>>> program.
>> 
>> How many globals does your program have.
>> 
>
>Going and quickly adding a stat for this (for plain global variables):
>   TestKern kernel:
>     Globals: tot=351 (2145B) dat=351(2161B) bss=0(0B)
>   Doom:
>     Globals: tot=777 (4468B) dat=777(4524B) bss=0(0B)
>   Hexen:
>     Globals: tot=766 (4228B) dat=766(4260B) bss=0(0B)
>   ROTT:
>     Globals: tot=723 (3656B) dat=723(3736B) bss=0(0B)
>   SWQuake:
>     Globals: tot=791 (4936B) dat=792(5208B) bss=0(0B)
>   GLQuake:
>     Globals: tot=956 (6744B) dat=957(6920B) bss=0(0B)
>   Quake 3 Arena (main program):
>     Globals: tot=980 (7080B) dat=980(7088B) bss=0(0B)
>   ...
>
>This is excluding all arrays and similar, so only counting the basic 
>primitive-type globals.
>
>
>But, as noted, basically all of these examples are larger than 2048 bytes.

Two fit in 4096 bytes (accessible with a 12-bit offset), and the rest
fits in 8192 bytes (accessible with a 13-bit offset).

- anton
-- 
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
  Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Technical Details
Message-ID: <2026Nov16.082458@mips.complang.tuwien.ac.at>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!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> <11dakku$29j9n$1@dont-email.me> <2026Nov15.170804@mips.complang.tuwien.ac.at> <11dd5pn$32jg3$1@dont-email.me>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.arch
Subject: Re: Large constants in RISC
Date: Mon, 16 Nov 2026 07:24:58 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 138
Message-ID: <2026Nov16.082458@mips.complang.tuwien.ac.at>
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> <11dakku$29j9n$1@dont-email.me> <2026Nov15.170804@mips.complang.tuwien.ac.at> <11dd5pn$32jg3$1@dont-email.me>
Injection-Date: Mon, 16 Nov 2026 08:04:43 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3570534"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18i/h3SZdzeliFDatnbBt1L"; posting-host="162378f659f64fe0e343a03d3521a08a"
Cancel-Lock: sha1:E9zw62fZjvVOtrn3srwEdHQTBd8= sha256:DmNsupXBdJb4TR88t6KkWxOmBDsKBimo1kvFL2/NFCE=
	sha1:7buGFrOxFQd0H4T0bDbZQ0qLrng= sha256:6d/A8su/OchxqZow8dVM3ELNjNtLgpHU5XIYnajLcVs=
X-newsreader: xrn 10.11