Article View: comp.arch
Article #728Re: Large constants in RISC
From: BGB
Date: Sat, 14 Nov 2026 19:05
Date: Sat, 14 Nov 2026 19:05
365 lines
16102 bytes
16102 bytes
On 11/14/2026 9:13 AM, Waldek Hebisch wrote:
> Terje Mathisen <terje.mathisen@tmsw.no> wrote:
>> Waldek Hebisch wrote:
>>> Terje Mathisen <terje.mathisen@tmsw.no> wrote:
>>>> 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.
>>>>>
>>>> Ouch!
>>>>
>>>> That's not even lipstick on a pig, it is just ugly.
>>>>
>>>> Even if a fancy version creates a macro-op on the fly, it would still
>>>> need all the pathways to align the multiple chunks into the correct bit
>>>> lanes of the 64-bit target.
>>>>
>>>> Why not just have a prefix opcode that says that the next 32 or 64 bits
>>>> is a constant?
>>>>
>>>> Yes, you could conceivably attempt to execute such a constant if the
>>>> instruction pointer got messed up (malware?), but to me that seems far
>>>> less onerous than the need for 4-instruction fusing for pretty much all
>>>> negative floating point constants.
>>>
>>> Note that prefix + 64 bit constant + instruction gives 4 units
>>> 32 bits each. In my scheme if you expend enough instruction space
>>> you can do this in 3 units. The second scheme is needs 4 units
>>> which is more or less equivalent.
>>>
>>> If you assume that your constants are 64 bit or very short, then
>>> prefix looks better. However, pretty frequent case are not so
>>> small constants that fits into 28 bits. Basically, calls need
>>> addresses and they go semi-random in given library. 28 bit is
>>> enough for most libraries (and can be effectively increased by
>>> 2 bits if one assumes 4 byte alignment for the target), while
>>> 12 or even 16 bit offsets are too short.
>>>
>>> So this covers constants of various length, with smaller cost
>>> for smaller constants. I was mainly looking at integer case.
>>> For FPU use some constants may be represented in single precision
>>> and converted to doubles, but nontrivial fraction of double
>>> constants will need full 64 bits. OTOH it is not clear to
>>> me how much one gains thanks to 64 bit floating point constants.
>>> IME precomputed constants are frequently kept in tables, it
>>> is not clear to me if there could be significant gain from
>>> putting them into instructions.
>>
>> Ask Mitch!
>>
>> Seems he's getting very consisten gains from having 32 and 64-bit
>> immdiates as part of the instruction stream, one of the major ones being
>> no cache misses when loading said constant.
>
> 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.
Within the 33 bit spike, seemingly:
~ 67% fall in 0 .. +2G
~ 16% fall in +2G .. +4G
~ 16% fall in -1 .. -2G
~ 1% falls in -2G .. -4G
The 17-bit spike appears to follow a similar pattern.
For 2RI (LI or ADD):
~ 77% fit into 10 bits;
~ 79% fit into 12 bits.
~ 88% fit into 17 bits.
Of those that fall outside of the 17 bit range, it is roughly a 50%
split between the 32 and 64 bit spikes.
Apart from the values near the spikes, there tends to be very little
between ~ 34 and ~60 bits; so not likely bothering with any intermediate
sizes.
Like, if you have the option of a 48 or 56 bit constant, while one might
think they are "much of the way" to covering 64 bits, actually, no, they
are not. A 56-bit immediate field would hit barely anything more than a
33 bit field.
For normal Load/Store displacements (excluding global variables):
10-bit (unscaled): ~ 92%
12-bit (unscaled): ~ 98%
10-bit ( scaled): ~ 98%
12-bit ( scaled): ~ 99%
For global variables, it is generally better to have a 16-bit scaled
displacement that can (mostly) reach across a typical ".data" section.
It may also make sense for the compiler to sort global variables by
usage ranking, and to promote any commonly used variables from ".bss" to
".data".
That said, a 10 or 12 bit displacement still isn't enough to hit all of
the global variables in a typical program, so something larger, like 16
bits, can get a much better hit rate.
This is assuming an ABI which accesses global variables relative to a
global-pointer or similar.
If using PC-relative addressing, one will need somewhere in the area of
22 to 24 bits to cross .text + .data + .bss.
Can note that for arithmetic operations:
~ 60%: 3R (3 register)
~ 40%: 3RI (2 registers and immediate)
If splitting by operation in general (descending ranking):
ADD/SUB
Compare
Shift
OR
AND
MUL
XOR
If splitting by operation, likelihood of having an immediate:
Shift
Compare
ADD/SUB
AND
OR
MUL
XOR
DIV and MOD aren't usually checked, but are rarely used operators in
practice (when excluding cases where DIV and MOD aren't by a constant
power-of-two).
DIV and MOD by a constant power of two are a bit of an anomaly, but MOD
can be turned into an AND operator. DIV can't quite be turned into a
bare shift, mostly because DIV would round towards zero and Shift
towards -Inf. So, for correct semantics, turning the DIV case into a
shift requires conditionally adding a bias value if negative.
As for types:
Int32
Pointers
Int64 / UInt64
UInt32
... everything else ...
Had observed that XOR-with-Immediate and ADD-Uint32-with-Immediate are
nearly equal probability; but both small if compared with 32-bit ADD
(where, 'int' tends to be a very heavily used type).
Can note that Shift and Compare are special:
A strong majority of shift operations use a constant;
For compare, compare-with-immediate outranks 3-register forms;
For most other operators, the register forms tend to dominate.
Statistically speaking, floating-point operations and types are
in-general, rarely used if compared with integer operations.
Can noted that for floating-point constants, around 80% hit as Binary16.
Much of the rest have a repeating pattern in the fraction;
A small minority number represent irrational numbers or similar.
Most also fit the pattern of having between 1 and 4 digits past the
decimal point; but not a good way to leverage this in hardware (easier
to target the repeating fractions).
Mostly better to leave base-level floating-point ops as 3-register only,
and only bother with large-encodings for any immediate cases.
While a 5 or 6-bit FP immediate can work (E3.M2 or similar), it almost
isn't really worth the bother.
...
For addressing modes, had noted:
Static:
[Reg+Disp] : ~ 80%
[Reg+Index*Sc] : ~ 18% (Sc nearly always element size)
Anything else : ~ 2%
Dynamic:
[Reg+Disp] : ~ 50%
[Reg+Index*Sc] : ~ 44% (Sc nearly always element size)
Anything else : ~ 6%
So, register-indexed addressing has a smaller effect on static program
size, but a much bigger impact on actual performance.
Cases where [Reg+Index*Sc+Disp] or auto-increment would be applicable
are much rarer.
Like, even if it seems like C code has a lot of "*s++" and similar,
statistically speaking, not so much.
The exact skew between addressing modes does depend on how
prologs/epilogs are handled:
Individual loads/stores:
More [Reg+Disp] dominance (~ 95%);
Load/store pair: ~ 80% (as above)
(The LDP/STP end up as one of the more heavily used ops).
Load/Store Multiple: [Reg+Disp], ~ 68%
(But, this involves the "evil" of a state machine).
There is also a tuning factor based on how many GPRs one has (32-vs-64):
32: Slightly fewer in prologs/epilogs but more in spill/fill;
64: Longer prologs/epilogs but very little spill/fill.
On a 16 GPR machine, spill-and-fill would tend to dominate.
Contrast to a 64 GPR machine where for many functions it often becomes
more viable to statically pin each variables' live range to a register
(with no spill/fill at all).
Personally I have noted that spill/fill can't be mostly eliminated until
reaching 64 GPRs.
A big factor is the percentage of functions that fit within N local
variables:
12: ~ 52%
16: ~ 73%
24: ~ 84%
28: ~ 87%
Or, say, with RV (~ 28 GPRs, with 12 callee save), roughly half of
functions could avoid spill-and-fill (this would be, say, the normal
RISC-V case).
In my case, with XG3, it is up to 87%.
If you go much smaller than 12, it tanks, so unless one has at least 32
GPRs, most functions are going to need spill-and-fill.
There is a long tail though, would need ~ 256 callee save registers to
reach 100% (mostly to deal with the rare few functions that have ~ 200
local variables). However, this is clearly impractical.
But, one could get ~ 98% coverage with a machine with 128 GPRs, but this
is also going to require a 40 or 48 bit instruction word; or falling
back to 2R for most ops, which is arguably worse.
I went for 64 GPRs, but as noted:
Slightly smaller immediate values;
Slightly less opcode space;
...
But, I can use the prefix encodings to offload some common ops into
64-bit encodings, and so don't need to commit to strictly 32-bit
fixed-length instructions (instead, it is merely "most of the
instructions are 32-bit").
There are fudge-factors though, like cases where variables have
non-overlaping lifetimes, or cases where scratch registers can be used, ...
...
Technical Details
Message-ID:
<11dabct$26fdl$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-mailReferences:
<11d76uq$11cdc$1@paganini.bofh.team> <11d7ct8$17akn$1@dont-email.me> <11d7ih2$11orr$1@paganini.bofh.team> <11d9d0d$1rsce$1@dont-email.me> <11d9tq8$1b9c1$2@paganini.bofh.team>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: Sat, 14 Nov 2026 13:05:00 -0600 Organization: A noiseless patient Spider Lines: 365 Message-ID: <11dabct$26fdl$1@dont-email.me> References: <11d76uq$11cdc$1@paganini.bofh.team> <11d7ct8$17akn$1@dont-email.me> <11d7ih2$11orr$1@paganini.bofh.team> <11d9d0d$1rsce$1@dont-email.me> <11d9tq8$1b9c1$2@paganini.bofh.team> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 14 Nov 2026 19:05:02 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2309557"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ZflKFCD3lo1KuLMxWGA4JDb4oSYoivCo="; posting-host="7ae1d57c66f0e466bf10769bbb72476f" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:5KXBdHV3UqtvMBMTyOoMhiXzW6o= sha256:2E9Z2k550qxiaScwPmWdmpjCZrKDaAI1bnWdYxbLh5I= sha1:gnaU0GVaZn4TBTYH4OsrGRxZYoc= sha256:7R/QVdWMWDs4pTUtKRszs0HlLwznal0pKPWowPiep/4= In-Reply-To: <11d9tq8$1b9c1$2@paganini.bofh.team> Content-Language: en-US