Article View: comp.arch
Article #716Re: Large constants in RISC
From: Terje Mathisen
Date: Sat, 14 Nov 2026 10:26
Date: Sat, 14 Nov 2026 10:26
136 lines
8437 bytes
8437 bytes
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. Terje -- - <Terje.Mathisen at tmsw.no> "almost all programming can be viewed as an exercise in caching"
Technical Details
Message-ID:
<11d9d0d$1rsce$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>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: Terje Mathisen <terje.mathisen@tmsw.no> Newsgroups: comp.arch Subject: Re: Large constants in RISC Date: Sat, 14 Nov 2026 11:26:20 +0100 Organization: A noiseless patient Spider Lines: 136 Message-ID: <11d9d0d$1rsce$1@dont-email.me> References: <11d76uq$11cdc$1@paganini.bofh.team> <11d7ct8$17akn$1@dont-email.me> <11d7ih2$11orr$1@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 10:26:22 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1962382"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18trwAEUn7zGj4Goovek/hqlRUULFu1875hHzcMT3DGEw=="; posting-host="f51cd0a907f21eef4e95863b39621f2f" User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0 SeaMonkey/2.53.24 Cancel-Lock: sha1:Utge+sGofwCgLzHh1mfZtzkIN5Y= sha256:wYvGWva3QhkR26vgjhyq5Fe7Njn4+OPJMcDgVG0LJrY= sha1:FMNJQyPNqBTV1xvUXjs0aiid8kU= sha256:XkwR5NFw15QgeK1pSOxaMdVM9TclBxsYlgaS/qK/YqE= In-Reply-To: <11d7ih2$11orr$1@paganini.bofh.team>