Article View: comp.arch
Article #893Re: Large constants in RISC
From: EricP
Date: Sun, 22 Nov 2026 19:19
Date: Sun, 22 Nov 2026 19:19
107 lines
6513 bytes
6513 bytes
On 2026-Nov-22 12:23, EricP wrote: > On 2026-Nov-20 22:23, Stefan Monnier wrote: >> On 2026-Nov-19 08:59, Stefan Monnier wrote: >>> aph@littlepinkcloud.invalid [2026-11-19 09:42:51] wrote: >>>> In a way, it does. Most large constants I see are addresses. >>>> AArch64 (almost) always has a 48-bit user space, so except for >>>> floating-point doubles and the very occasional large integer, constant >>>> loads look like >>>> mov x0, 42857; movk x0, 0x60b8, lsl 16; movk x0, 0xaaaa, lsl 32 >>> Interesting. I would have expected that most AArch64 code is position >>> independent (e.g. for address space randomization purposes), so I wonder >>> in which circumstances we end up with such instruction sequences to >>> build hardcoded addresses. >> >> EricP [2026-11-20 15:47:57] wrote: >>> An application or OS can require that certain data structures >>> are mapped at predefined virtual addresses. >> >> I find it hard to believe that these thingies would account for a large >> portion of large constants. > > In many ISAs code can be optimized to smaller a smaller memory model > than the full 64b address space because most applications and libraries > reside in the first 2GB and access small objects. > > As both absolute and PC-rel methods are possible on all cpu's, > the comparison is a matter of how efficient each is in an ISA, > in a compiled memory model. Compiled memory model matters > because if one knows that only the lower and upper 2GB will > be accessed then compiler can use sign extended 32b values > as both relative offsets or absolute addresses. > > I'm interested is what happens when one tries to use an ISA in its > full 64b address space as that is where many inefficiencies show up. > Eg. RV requires 2 instructions to calculate a constant in a 32b memory > model but 5 instructions in the full 64b memory model. > Eg. RV's LD instruction contains a 12b immediate offset but if the > offset is larger you must load the immediate, 2 or 5 instructions, > add it to a base, then LD using it, 7 instructions. > > One needs to consider scenarios for: > - each ISA, x64, A64, RV, > - various memory models: > - tiny memory model smaller than 32b, > - small memory model using 32b subset inside a 64b address space, > - full 64b address space, > - absolute and PC-rel addressing, > - various address sequences - first access vs subsequent access. > - various usage scenarios - unshared vs shared exe or dll > (and this brings different OS behaviour into consideration). > The above examination shows where the weaknesses are in each ISA. This is towards considering what modifications would improve each in different scenarios. Eg. as I noted earlier, defining rBase == R0 => RIP and adding LD & ST instructions with scaled-index address mode then it gives access to the full 64b address space, absolute or RIP-rel addressing, in two instructions. Does it need a similar optimization for branch, call, jump, or case/switches to ranges larger than an ISA supports? How big are the native relative branch/call offsets? If it is just a 12 or 13 bit offset, what is the sequence for a larger range? Does that need to be optimized too? I use the term "branch" and its variations like "branch and link" or "branch subroutine" to mean the destination is relative to the branching instruction, and "jump" and its variations to mean the destination is independent of the jumping instruction. In my terminology RV's Jump And Link JAL instruction would be BAL because it is relative, whereas its Jump and Link Register would remain JALR as it calculates its destination independent of the jumping instruction. RVs conditional branches remain Bcc as they are relative to the branching instruction. RV has a two instruction sequence to unconditional branch relative to any address within 32b, +-2GB, AUIPC and JALR plus a temp register. To branch relative to any address in 64b space requires AUIPC, a LD to load a 64b constant offset from the constant table, ADD to the PC base to get an absolute address, and JALR. If the destination is relocatable, as it would be with a destination in a shared library, then the 64b offset constant would need a relocation table entry so it is patched by the loader (aka an imported DLL library routine trampoline). However that relocation patch could make this code unsharable (depending on how the OS handles shared code and data in EXE and DLL's) so a longer code sequence might be necessary to extract a dynamic address from the GOT table, which is itself located at an absolute address. The above becomes simpler if a new JALR instruction incorporates the same rBase == R0 => RIP definition plus an offset register, which may be R0 the zero register. A general 64 branch relative is LD.L r1 = [RIP + table_offset] // load 64b offset JALR r0, [RIP + r1] // add offset to rip and jump and a general absolute jump is LD.L r1 = [RIP + table_offset] // load absolute address JALR r0, [r1 + r0] // jump there
Technical Details
Message-ID:
<JeHMS.106324$ATB4.99447@fx12.iad>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!feeder.usenetexpress.com!tr1.iad1.usenetexpress.com!news-out.netnews.com!netnews.com!s1-4.netnews.com!peer05.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx12.iad.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> <2026Nov14.201017@mips.complang.tuwien.ac.at> <6af911ea.18530156@news.eternal-september.org> <SYScnWsR3rgGWWP3nZ2dnZfqn_WdnZ2d@supernews.com> <jwv1p8hhqmy.fsf-monnier+comp.arch@gnu.org> <4m2MS.132607$Lt64.106487@fx41.iad> <jwvo6bidgit.fsf-monnier+comp.arch@gnu.org> <5yFMS.36883$gOr4.31829@fx08.iad>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!feeder.usenetexpress.com!tr1.iad1.usenetexpress.com!news-out.netnews.com!netnews.com!s1-4.netnews.com!peer05.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx12.iad.POSTED!not-for-mail MIME-Version: 1.0 User-Agent: Mozilla Thunderbird From: EricP <ThatWouldBeTelling@thevillage.com> Subject: Re: Large constants in RISC Newsgroups: comp.arch 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> <2026Nov14.201017@mips.complang.tuwien.ac.at> <6af911ea.18530156@news.eternal-september.org> <SYScnWsR3rgGWWP3nZ2dnZfqn_WdnZ2d@supernews.com> <jwv1p8hhqmy.fsf-monnier+comp.arch@gnu.org> <4m2MS.132607$Lt64.106487@fx41.iad> <jwvo6bidgit.fsf-monnier+comp.arch@gnu.org> <5yFMS.36883$gOr4.31829@fx08.iad> Content-Language: en-CA In-Reply-To: <5yFMS.36883$gOr4.31829@fx08.iad> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Lines: 107 Message-ID: <JeHMS.106324$ATB4.99447@fx12.iad> X-Complaints-To: abuse@UsenetServer.com NNTP-Posting-Date: Sun, 22 Nov 2026 19:19:05 UTC Date: Sun, 22 Nov 2026 14:19:01 -0500 X-Received-Bytes: 6217