🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #769

Re: Large constants in RISC

#769
From: BGB
Date: Sun, 15 Nov 2026 20:47
187 lines
8555 bytes
On 11/15/2026 10:08 AM, Anton Ertl wrote:
> BGB <cr88192@gmail.com> writes:
>> However, if counting clock-cycles, a prefix is a win:
>>    Prefix + Op: Zero cycle overhead for the missed constant;
>>    Load + Op: May add 3 or 4 clock cycles.
>>
>> So, would need to speculatively load the immediate values several
>> instructions in advance, and hope that there are no extra cache misses.
> 
> 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...

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. However, spreading things out is harder on x86 
because of the relative lack of registers.


The relative cost of loads does seem to be cheaper though, but depends 
on the operation that follows.

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

Generally better to have multiple branching paths pivoting off a single 
load, than to have multiple loads driving each decision path.

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.

So, it is like the i7 has more expensive branching in-general, but 
seemingly doesn't care as much where the input for the branch decision 
came from.


Not fully investigated, this is just what I seem to be seeing from some 
microbenchmarks.


This was observed mostly in some code that was tuned for my own ISA, 
where my CPU design seems (in general) to have comparably more expensive 
memory access, but comparably cheaper branching.

Where:
Basic memory load is 3 cycles, but also partly at the mercy of the 
direct-mapped L1 D$;
Branches are typically 1-cycle hit / 10 cycle miss;
My branch-predictor design is reasonably effective, though I had 
observed that the relative advantage of its core design would dilute 
with larger context (*).


*: It uses the recent branch history and PC LOBs to index into a table 
of 5/6-bit FSMs (6b in, 6b out, 5b of state) that can predict arbitrary 
repeating patterns up to 4 bits.

Had noted that if the context was significantly larger, the relative 
advantage versus saturating counters drops off, but in a small context, 
the FSMs solidly beat out the saturating counters.

To admit, I am using FSM tables generated by genetic algorithms though, 
as the GA generated tables tend to beat out my attempts at 
hand-generated tables (but, can note also that fitting every possible 1 
to 4-bit repeating pattern into a 5 bit state machine is a bit of a 
mental puzzle).

And, in this case, the specialized application of random number 
generators and selection heuristics seems to do a better job at it.


> 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.

Well, also this is partly why I am often going for 32K D$ + 16K I$:
The D$ tends to be a lot more chaotic and volatile, so needs to be bigger;
The I$ is a little less so, more just matters that the active loops fit 
and (hopefully) there aren't too many address conflicts.


There may be differences here between a machine with direct-mapped vs 
set-associative caches though.

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


If a compiler could identify the programs' main hot path, could be 
possible in theory for it to try to cluster the hot-path into a shared 
block with hopefully non-overlapping cache ranges.

BGBCC isn't that clever though.


>> 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.

Granted, a lot of my other programs would fall below the limit here, as 
my own coding practices tend to rely more on structs than on global 
variables.


They would be addressable with a 10-bit scaled displacement though.

In any case though, a Load/Store instruction with a hard-wired 
global-pointer and 16-bit displacement can hit all of the global 
variables though.


> In order to be able to address the globals of a program with a single
> global pointer, the linker needs to do quite a bit of work.  Maybe the
> RISC-V linker does.  For the Alpha every compilation unit has its own
> constant and global table as far as I remember (and I think the same
> register is used for both on the Alpha).
> 

In BGBCC, it first sorts all the global variables into priority order 
and then shuffles the order in an attempt to improve packing density 
(say, if swapping the relative order of variables will reduce space 
wasted for padding bytes).

Most basic globals tend to be a mix of 'int' and 'pointers'.


That said, can note that unlike GCC, BGBCC doesn't have a separate 
compiler and linker (it instead handles separate compilation using a 
stack-based IL, and then sorts everything out and compiles what things 
are reachable).


> - anton
Technical Details
Message-ID: <11dd5pn$32jg3$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.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>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: BGB <cr88192@gmail.com>
Newsgroups: comp.arch
Subject: Re: Large constants in RISC
Date: Sun, 15 Nov 2026 14:47:50 -0600
Organization: A noiseless patient Spider
Lines: 187
Message-ID: <11dd5pn$32jg3$1@dont-email.me>
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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 15 Nov 2026 20:47:52 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3231235"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/7rAs4wZ0xuhFK/LrnA227AL9OtTFO4ig="; posting-host="06c6f696e7de5a2f8191c3b89611499c"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:plkvlO8IJUzHlEgEr9P+C3/nWqI= sha256:sCptfySgldJXDF9UcPwcX3a0WK9htc1EJU01VdYnoaw=
	sha1:kvyXFlVKQpL0iho5ZdZDmrZCH8s= sha256:3del9lmGT3ftHorc3UB/IV2xUK3pCOHdfa+65zdE5kA=
In-Reply-To: <2026Nov15.170804@mips.complang.tuwien.ac.at>
Content-Language: en-US