🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #710

Re: Large constants in RISC

#710
From: BGB
Date: Fri, 13 Nov 2026 23:05
276 lines
13771 bytes
On 11/13/2026 3:02 PM, Robert Finch wrote:
> On 2026-11-13 2:36 p.m., BGB wrote:
>> On 11/13/2026 11:53 AM, Robert Finch wrote:
>>> On 2026-11-13 9:30 a.m., Waldek Hebisch wrote:
>>>> Normal RISC architecture with 32 bit instructions has problem
>>>> with constants: 64 or 32 bit constants are to big to put into
>>>> instructions and even smaller constants demand uncomfortable
>>>> tradoff: either consume a lot of encoding space on larger
>>>> constants or live with restricted range of constants.
>>>>
>>>> I have now what to me looks like pretty satisfactory solution
>>>> or rather family of solutions.  It has some similarites to
>>>> things proposed here earlier, but some elements may be new.
>>>>
>>>> Tranditional RISC solution was to have special instructions
>>>> for building constants in registers.  For me traditional
>>>> way has two problems.  One is excessive number of instructions
>>>> needed to build larger constants.  For example, Risc-V can
>>>> build almost all 32 bit constants in 2 instructions and in
>>>> many cases two instruction sequence have similar effect
>>>> to single instruction with 32 bit constant argument.  But
>>>> for 64 bit constants one possible seqence involeves 6
>>>> instructions.  This trouble is easily fixable with more
>>>> (better) constant building instructions.  The second
>>>> problem is need for free register in which constant is
>>>> build.  Such register frequently is used only once, but
>>>> from point of view of hardware is potentially live as long
>>>> as it is not overwritten by new value.  Consequently,
>>>> a constant needs write port to reqister file.  AFAICS this
>>>> may limit possibilities for instruction fusing.
>>>>
>>>> To solve problem of needing extra register I propose to have
>>>> extra register (call it C) dedicated to building constants,
>>>> different from normal architectural registers.  I also propose
>>>> special flag signaling that this extra register is live.  It
>>>> is important that this flag gets cleared after the C register
>>>> is consumed (used).  It also gets cleared if after setup of C
>>>> the next instruction does not use it.  I propose to have 2 or
>>>> 3 constant-building instructions.  One sets high k bits of C,
>>>> where k may be 26 (for case of 2 constant-building instructions)
>>>> or 18 (for case of 3 constant-building instructions).  The
>>>> second constant-building instruction sets next k bits of C.
>>>> In case of 3 constant-building instructions the third one
>>>> sets the following k bits of C.  Use of constant-building
>>>> instructions also marks C as live.  So in case of 2 constant-
>>>> building instructions the 2 instructions set high 52 bits
>>>> of C and mark it as live.  When C is live it is combined
>>>> (OR-ed) with constant in the following constant-using
>>>> instruction.  Assuming that normal instructions can provide
>>>> 12 bits, this allows 3 instruction sequence equivelent to
>>>> a single instruction with 64 bit constant argument.  One
>>>> can object that instruction providing 26 bits leaves only
>>>> 6 bits for opcode and we need 2 such opcodes.  This may be
>>>> deemed as using too much encoding space for benefits of
>>>> 64-bit constants.  That is why I propose the 3 instruction
>>>> variant.  With 18 bits for constant there is 14 bits for
>>>> opcode.  3 such instructions consume rather small part of
>>>> instruction space.  The cost is that now to get 64 bit
>>>> constant we need 4 instruction sequence.  Depending on
>>>> frequency of 64 bit constants this variant may be more
>>>> satisfactory.
>>>>
>>>> What we gain using such approach?  Low end implementation
>>>> may have real C register and literaly implement description
>>>> above, dully executing needed sequence as separate instructions.
>>>> High end implementation may recognize the whole seqence and
>>>> fuse it into a single instruction.  AFAICS fusing 3 instructions
>>>> does not look harder than what popular variable length
>>>> istruction decoders are doing, fusing 4 instruction may
>>>> look extreme but probably is possible too.  Even if longest
>>>> seqence is not fused fusing 2 instruction seqences looks
>>>> easy and in second variant would give effect of instructions
>>>> having 28 bit constants.  Let me reiterate, that unlike
>>>> currently popular architectures and schemes that I saw
>>>> previously in scheme above fused instruction is fully
>>>> equivalent to instruction containing bigger constant, that
>>>> is there is no extra change to machine state (except for
>>>> instruction pointer which changes by instruction length).
>>>>
>>>> Compared to variable length encoding scheme above probably
>>>> uses more instruction space, but gives more freedom to
>>>> implementation, with fusing decoder implementation will
>>>> behave as if instruction set was variable length, but
>>>> it is possible also to use simple fixed length decoding.
>>>>
>>>> For me the above essentially solves problem of constants in
>>>> RISC.
>>>>
>>>
>>> Postfixes (suffixes?) for constants can be fused into the previous 
>>> instruction.
>>> A wider instruction helps with 64-bit constants.
>>> No issue with interrupts, if landed on by an interrupt they can be 
>>> treated as NOPs.
>>> Takes more bits than a variable length instruction.
>>>
>>
>> But, requires looking at the following instruction words to decode the 
>> instruction. So, creates a worse situation (than prefixes) in the case 
>> of instructions crossing a cache-line boundary.
> 
> Yes. The decoder looks at the following instruction space. When the 
> instruction is extracted, the following instruction space is passed to 
> the decoder. That makes it easy to work with. With a prefix one cannot 
> always get the previous instruction. If the instruction is the first one 
> fetched, where is the previous instruction?

Running backwards, the prefix case is just the same as the suffix case, 
just running backwards:
If previous word is a prefix, we are not at the start of the instruction;
If previous word is not a prefix, we are at the start of an instruction.


In the original design of XG1, a similar property applied to 16/32:
   Exxx/Fxxx: Start of a 32-bit instruction;
   Else (usually): 16-bit op or part of a 32-bit instruction;
If prior word is Exxx/Fxxx and current word is not Exxx/Fxxx, we are 
looking at the second word of a 32-bit instruction.

Granted, RISC-V does not have this property, and if you lose your place 
in x86, you are SOL.


For XG3 and RV64+Jx, this property applies at the level of 32-bit 
instruction words. Because, the only place a Jumbo Prefix is valid, is 
when being used as a prefix.

Though, to be most useful, one needs a fair chunk of encoding space to 
throw at these prefixes. This is a weak point of my RV+Jx scheme, as the 
prefixes are small enough to barely allow Imm12->Imm33.


In XG3, for the Imm10->Imm33 case, there are actually a few bits left 
over; but it allows Reg6->Imm33.

While it could theoretically be used to allow Imm10->Imm37, this gains 
"basically nothing of value" over Imm33 so better to just leave these 
bits reserved for future expansion.

As noted though: 27+27+10 => 64.

This is basically the same amount of encoding space that RISC-V spends 
on its FMADD style instructions.


Mixing an XG3 jumbo prefix with a RV suffix could in theory allow for 
Imm32 immediate-synthesis (from a Reg5), but I can note that my current 
rules don't allow this (so, XG3 prefixes for XG3 instructions and RV-JX 
prefixes for RV ops).


>>
>> At least if one wants to allow for an implementation that figures out 
>> instruction lengths and similar at cache-line fetch time.
> 
> For fixed size instructions, there is no instruction length to figure 
> out. They are always the same size.

Possibly, as noted:
   In XG3 everything is 32-bits, or larger, and unambiguous;
   There was XG1, with 16-bit ops;
   And, there is RV-C, with its set of 16-bit ops.

The RV-C ops are, horribly chewed.


While taken in isolation, each case is cheap:
   XG1:
     Exxx/Fxxx/7xxx/9xxx: 32-bit op, else 16-bit op;
     FExx/FFxx: 64/96 bit
   XG2:
     FExx-xxxx/FFxx-xxxx: 64/96 bit, else 32-bit
   XG3:
     ...xxxxx00: 32-bit (XG3 space)
     ...xxxxx01: 32-bit (XG3 space)
     ...xx11x10: 64/96 bit, else 32-bit (XG3 space)
       Two prefixes in a row for 96-bit ops.
     ...0111111: 64/96 bit, else 32-bit (RV space)
   RV64:
     ...xxxxx00: 16-bit
     ...xxxxx01: 16-bit
     ...xxxxx10: 16-bit
     ...xxxxx11: 32-bit space
     ...0011111: 48-bit space
     ...0111111: 64/96 bit, else 32-bit (RV space)
       Current JX prefixes depend on Func3 for 64/96.
         0xx: Generic 64-bit ops space
         100: J21 prefix
         101: -
         110: J52 prefix (because 21+21+12 < 64)
         111: -

This is enough to when all are taken together, it is enough to care.

There are some lookups that need to cover the instruction space for 
superscalar.

With per-line tagging, does mean that I$ lines depend on the ISA mode; 
currently this is treated as part of the cache-line hit/miss handling.

For RISc-V, there was a past rule (unused) for longer instruction encodings:
Func3 would specify the number of 16-bit units, allowing 80..192 bit ops.


Currently these are unused, and in my case there is no strong motive for 
encodings past 96 bits. Had previously considered an "improved" scheme, 
that would switch to coarser granularity and give more space to 96-bit 
ops, but, not currently much strong reason to actually do so.

It was "less cramped" to merely define a J52 prefix as a 64-bit encoding.

Though, the J52 prefix would mean needing to look 2x 32-bit words in the 
past to detect whether or not the current instruction is part of a 
96-bit op in the RV case.


>>
>> Does create a pain point for superscalar, but at least the limitations 
>> of "superscalar doesn't cross cache-line boundaries unless performed 
>> at the time of instruction fetch" wont change visible results.
>>
> The CPU is reading two cache lines in case an instruction spans a cache- 
> line anyway. Suffixes can go on a ways into the upper cache line.
> 

Possibly.

But, in most cases this can be detected in advance with a prefix scheme, 
but not with a postfix scheme.


>> For a prefix, one can always know a suffix follows (if it appears at 
>> the end of a cache line).
>>
>>
>> There was a considered option of:
>> Determine lengths and superscalar at the time of tag-checking and 
>> write results back into I$;
>> Would avoid both the limitations of line-ingress and fetch-time handling;
>> But, is more complicated and would require additional pipeline stalls 
>> to write this data back into the cache.
>>
>>
>> Though, this would be less of an issue with 32B cache lines than with 
>> 16B. Superscalar that only works within groups of 4 instructions being 
>> kinda weak (can miss a lot of obvious cases of co-execution because 
>> they happen to be at the wrong place in the cache line).
>>
>>
>> At present, my CPU design's I$ sort of uses a mixed strategy, doing 
>> part of the work at ingress and part at the time of instruction fetch.
>>
>> So, say:
>>    Ingress Time: Determine lengths and fetch instruction control-bits;
>>    Fetch Time: Check for register aliases and similar.
>>      Combines with control bits to determine final fetch length.
>>
>>
>> ...
> Qupls uses fixed length instructions. Dealing with variable length 
> instructions is real nasty at least for superscalar with several front- 
> end pipeline stages. Variable length instructions need a good next- 
> bundle predictor in the BTB possibly having the instruction points 
> preset in the cache. What if the BTB predicts the next bundle of 
> instructions wrong? One could have garbage instructions reaching later 
> stages causing false prediction mismatches. Too complex for me.
> 

As noted, this is why both XG3 and my implementation of RV64 only work 
with 32-bit aligned 32-bit instructions.

Dealing with superscalar + RV-C cases would be too much pain and cost. 
So, as soon as RV-C appears, or the instructions are misaligned, the I$ 
gives up and falls back to "one instruction at a time".

Technical Details
Message-ID: <11d854b$1g085$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> <11d7iqd$19e6h$1@dont-email.me> <11d7or3$1b064$2@dont-email.me> <11d7tsq$1dn4p$1@dont-email.me>

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: Fri, 13 Nov 2026 17:05:45 -0600
Organization: A noiseless patient Spider
Lines: 276
Message-ID: <11d854b$1g085$1@dont-email.me>
References: <11d76uq$11cdc$1@paganini.bofh.team>
 <11d7iqd$19e6h$1@dont-email.me> <11d7or3$1b064$2@dont-email.me>
 <11d7tsq$1dn4p$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 13 Nov 2026 23:05:48 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1573125"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/wzRBvieIsxh3+gr+71/oeusVps7NUZHw="; posting-host="31e799b15a89ad0e63da3475fa0f1057"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:jbYpYW69FxUQcm97rnSBzq4TjFY= sha256:UXqkgsUqX3cdBC1AP8A+SsxCaEsPuUq66UhIQH4uOIU=
	sha1:Kl2qA6m5AHeYp6rnDK7+RcIifGE= sha256:DMzX+nHmA4i5SWlJzwcuR27KGbWcO/IGKSs08+EshTM=
Content-Language: en-US
In-Reply-To: <11d7tsq$1dn4p$1@dont-email.me>