Article View: comp.arch
Article #770Re: Large constants in RISC
From: BGB
Date: Sun, 15 Nov 2026 21:43
Date: Sun, 15 Nov 2026 21:43
128 lines
6582 bytes
6582 bytes
On 11/15/2026 12:50 PM, MitchAlsup wrote:
> John Levine <johnl@taugh.com> posted:
>
>> It appears that BGB <cr88192@gmail.com> said:
>>>> 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. ...
>>
>> That tells me that it's not a good use of instruction bits to try to
>> encode large constants. For the 3% of constants that can't be encoded
>> in 12 bits, use a constant pool.
>
> In a constant pool, one still suffers the latency of the LD and the
> sacrifice of a register (hurts 16-register ISA more than 32-register
> ISA). Whereas, with constants in the instruction stream, you suffer
> neither.
>
Luckily, register pressure isn't too big of an issue here on a 64 GPR
ISA, but the memory load can still hurt.
Particularly on an in-order machine with direct-mapped caches.
And, as noted:
Prefix is still generally more compact than a 3-instruction fallback
scenario (typical case on RISC-V or similar).
As for how much it matters for only the last few percent of immediate
values and displacements, is one of those areas where people can fight
over it.
Or, if one happens to use something like a rarely-used global variable
(in the static-count sense) in the middle of a loop in the middle of the
hot path (which is where this sort of thing is seemingly most prone to
show up sometimes).
So, sometimes it is better to be defensive and mostly address the last
few percent as a precautionary effort against slow-cases randomly
showing up in the hot path.
Well, at least excluding programmer-level "everyone already knows this
is slow" things, like integer divide and modulo.
Pretty much everyone seems to expect things like integer constants and
global variables to be nearly free though.
Nevermind if things like ELF PIE kinda suck really hard for code that
depends heavily on using global variables in the hot path though (well,
or the GCC + ELF-PIE combo where using a member in a passed context
struct is typically cheaper than using a global variable).
And one is better advised for these targets to cache anything from
global variables into local variables preferably outside of loop bodies,
similar to how it is better to try to do struct-member and array loads
in advance with compilers like MSVC (contrast to GCC which is seemingly
clever enough to not really care when/where the code does a "foo->x" or
similar).
Though, granted, often there are other reasons to minimize putting any
volatile state in global variables independent of the relative
performance tradeoffs here (but, for non-volatile state or constant
data, global variables are often the most natural place to put it).
Well, and other GCC style cleverness, like one can write something like:
u32 getu32(byte *ptr)
{ return(ptr[0]|(ptr[1]<<8)|(ptr[2]<<16)|(ptr[3]<<24)); }
And (with optimizations enabled) the compiler may magically turn calls
to this function into a bare memory load.
Well, and on the other side of things, recently writing some code and
lazily doing:
memcpy(ct, cs, 4);
And observing that seemingly MSVC seemingly still does not reliably
optimize away the memcpy call. There is still some sort of magic
dice-roll factor here (like "memcpy(&z, &x, 4);" gets optimized away,
but then randomly if fails when just passing bare pointers to the memcpy
"for whatever reason", meaning the code still needs to have alternate
patterns to force a load). But, then finds, at least on the GCC fallback
case one can use a load-shift sequence, which is maybe a little more
portable than a "memcpy()" when one knows the intended endianess of the
source/destination (but, sometimes it gets annoying to need a bunch of
repetitive boilerplate for this sort of stuff for any code intended to
"not be slow").
Nevermind if I lack an explanation for *why* this case would fail to be
optimized, in some semi-random way, where most times it works but then
just fails randomly for some unexplained reason. It seems like almost
the most obvious possible situation to optimize for, but alas...
Well, and which compiler to use is also not simply a factor of whichever
is "better".
But, yeah...
>> FYI, that's basically what S/360 did. The load address instruction
>> has a 12 bit immediate field you can use to load 12 bit constants,
>> other than that you're on your own. S/390 added a load immediate with
>> a 16 bit signed value, z added load immediates with a 32 bit operand,
>> and versions that load the high or low halves of a regster, so with
>> two 48-bit instructions you can load any 64 bit value.
>>
>>
Technical Details
Message-ID:
<11dd91o$33nqv$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> <11d9d0d$1rsce$1@dont-email.me> <11d9tq8$1b9c1$2@paganini.bofh.team> <11dabct$26fdl$1@dont-email.me> <11dahac$2t74$1@gal.iecc.com> <1794768602-5857@newsgrouper.org>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 15:43:17 -0600 Organization: A noiseless patient Spider Lines: 128 Message-ID: <11dd91o$33nqv$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> <1794768602-5857@newsgrouper.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 15 Nov 2026 21:43:21 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3268447"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+K18ClDn+mLlbsyJ3QhUuheMlKP8T11Io="; posting-host="06c6f696e7de5a2f8191c3b89611499c" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:Liw3n1ayPBez8+zZOcHb6xA1x98= sha256:kKGLGmFsrg/JSJvMR1w4q1BHqsTPF0TF2PINm9rEzYc= sha1:znVxCv4+TJilPF2M6A8A2t+o3IU= sha256:u8tvySTLwPUnFmyH6PeCrDBocM1gKIj7NI/KrLr830o= In-Reply-To: <1794768602-5857@newsgrouper.org> Content-Language: en-US