🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #767

Re: Large constants in RISC

#767
From: EricP
Date: Sun, 15 Nov 2026 19:34
99 lines
5055 bytes
On 2026-Nov-15 14:12, EricP wrote:
> On 2026-Nov-15 11:08, 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 I was using the constant table approach I would:
> 
> - define for LD and ST instructions that if the base register was
>    the zero register (R0 or R31) then it means use the 64-bit RIP.
> 
> - add scaled index LD and ST instructions with the following format:
>    opcode 6b Rs1_base 5b Rs2_index 5b Rd/Rs3 5b data_type 3b scale 3b offset 5b
>    Data types are signed integer: B, H, W, L  float: S, D
> 
> This can access anywhere in 64b address space in 2 instructions:
> - use a small RIPrel offset to load the 64b RIPrel offset from the table
> = use the scaled index LD/ST with scale = 0, index = offset, and base RIP
>    to address all of memory.
> 
> For RIP-relative 64b addressing:
> 
>    LD.L  r1 = [RIP + table_offset]
>    LD.dt r2 = [RIP + r1<<0]
> 
> For absolute 64b  addressing, set the base register to the constant and
> the index register to the zero register (which equals 0 NOT the RIP):
> 
>    LD.L  r1 = [RIP + table_offset]
>    LD.dt r2 = [r1 + r0<<0]
> 
>> 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.
>>
>>> 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.
>>
>> 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).
>>
>> - anton
> 
> Alpha put the constant table just above the entry to the routine
> running backwards and aligned the routine on a cache line.
> On entry it would do a BR  rd,+0 to copy the PC into rd
> then they use that as a base to access the constant table.
> 
> However neither Alpha or RV have
Oops I hit send instead of save.

However neither Alpha or RV have the scaled index address mode
that saves having to manually ADD the offset to the RIP for each LD or ST.

The method I suggest above

   LD.L  r1 = [RIP + table_offset]
   LD.dt r2 = [RIP + r1<<0]

has the drawback that the RIPrel offset to the same memory location will be
different for each LD/ST instruction, requiring multiple constant loads.
Instead one can use a Load Effective Address LEA instruction with a
base of RIP to copy the RIP into a dest register, and have a single 64b
offset for each variable relative to that fixed base.

   LEA   r1 = [RIP]
   LD.L  r2 = [r1 + table_offset]
   LD.dt r3 = [r1 + r2<<0]

and reuse r1 base and r2 offset multiple times:

   ST.dt [r1 + r2<<0] = r3

Technical Details
Message-ID: <lPnKS.129429$aI4.72240@fx15.iad>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx15.iad.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> <JunKS.106396$Cm3.49782@fx47.iad>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx15.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: Large constants in RISC
Content-Language: en-CA
Newsgroups: comp.arch
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>
 <JunKS.106396$Cm3.49782@fx47.iad>
From: EricP <ThatWouldBeTelling@thevillage.com>
In-Reply-To: <JunKS.106396$Cm3.49782@fx47.iad>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 99
Message-ID: <lPnKS.129429$aI4.72240@fx15.iad>
X-Complaints-To: abuse@UsenetServer.com
NNTP-Posting-Date: Sun, 15 Nov 2026 19:34:41 UTC
Date: Sun, 15 Nov 2026 14:34:39 -0500
X-Received-Bytes: 4818