Article View: comp.arch
Article #735Re: Large constants in RISC
From: BGB
Date: Sat, 14 Nov 2026 21:42
Date: Sat, 14 Nov 2026 21:42
118 lines
5619 bytes
5619 bytes
On 11/14/2026 2:46 PM, John Levine wrote: > 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. > That is apparently what the RISC-V people thought as well... From code-density POV, in a strict sense, prefixes don't necessarily win much over, say, PC-relative memory loads (which RISC-V also lack), or constant-pool loads; Or register-indexed addressing (to make the case of a failing displacement less bad). 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. However, ironically, without the big chunk of encoding space spent on the prefixes, 64 GPRs (like in XG3) is a harder sell; because then one has no good escape-hatch for running out of opcode space, whereas with the prefixes they can also have a case where they escape to more opcode space (in my case, these cases usually instead merely giving a 17-bit immediate). But, can note though, that while RISC-V had a larger opcode space initially, they burned through it somewhat inefficiently. IMO, however, the larger immediate and displacement values via prefixes are still worthwhile even if only around 3% or so of immediate values miss. Well, more so vs something like RISC-V where failing to hit on an immediate or displacement typically requires around 3x as many instructions to compensate. Say: ADDI: Hits 97% of the time; LUI + ADDI + ADD: Needed, 3% of the time. ... J21I + ADDI: Only 2/3 the size. Even if, yes, this sort of thing would only save ~ 1% off the size of a binary. A load/store displacement failing often needs: LUI + ADD + LW/SW: Also weak. If each op takes 2 or 3 cycles, this also hurts a lot worse: Basic load: 3 cycles (maybe less, if result not used immediately); Fallback case: 7 cycles (with 5 unavoidable if the ops are end-to-end). 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. > 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. > > OK. Initially (before adding jumbo prefixes), I effectively had a LDSH / SHORI instruction. In RV style notation, it would sort of like: SHORI Rd, Imm16 // Rd = (Rd << 16) | Imm16u Along with, say: LI Rd, Imm17s // Rd = Imm17s This would allow loading an arbitrary 64-bit constant in 4x 32-bit instructions; and 33-bit in 2 instructions. RISC-V has LUI + ADDI, but can note that LUI needs both more encoding space while also being less versatile than LI-Imm17 + SHORI or similar. The Imm17s here also addresses a fair chunk of cases that would miss with Imm12 but without needing to go all the way to a 2-op sequence. This still works pretty well for code-density, just weaker for performance.
Technical Details
Message-ID:
<11dakku$29j9n$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>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 15:42:53 -0600 Organization: A noiseless patient Spider Lines: 118 Message-ID: <11dakku$29j9n$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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 14 Nov 2026 21:42:55 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2411831"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19WWSnCVLHPNfDDEuyxgffoszV8J1PHQ9M="; posting-host="7ae1d57c66f0e466bf10769bbb72476f" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:XN2ewBPJ+dqWlOl+tFLBGY8paTw= sha256:W14r0NFCRpF9IHomIXzwsfL8rY9slargZg2g2A0LQ0k= sha1:uuz6D95eNQlE3kHfuU352QkIQ+w= sha256:DZHYcrCspQ+upF0xuPDthNcGN4NvlTIMUtjY1vmVdlg= Content-Language: en-US In-Reply-To: <11dahac$2t74$1@gal.iecc.com>