🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #907

Re: Large constants in RISC

#907
From: MitchAlsup
Date: Mon, 23 Nov 2026 18:40
143 lines
7490 bytes
EricP <ThatWouldBeTelling@thevillage.com> posted:

> 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.

Errr...1 instruction:

       MEM     Rd,[Rbase,Rindex<<swcale,#Displacement64]

1 instruction 3-words
 
> Does it need a similar optimization for branch, call, jump, or
> case/switches to ranges larger than an ISA supports?

I found no reason to allow switches to use the whole 64-bit address
space. The rest, sure. I do support {10, 18, 34}-bit offset space for
a single switch.

But another optimization is a LD directly into IP with/without
saving return address in R0. I call these JMPX and CALX. If the
linker binds several modules into a final module and resolves
the addresses, I also have the JMPA and CALA versions which 
deliver the AGENed address into IP.

> How big are the native relative branch/call offsets?

(effective) 18-bits, 32-bits and 64-bits.

> 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?

If you have universal constants, that question does not need to be ask!
 
> 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.

Sounds painful. I can do that in one 3-word instruction.

> 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).

     CALX   [IP,,GOT[name]]

No trampoline needed.

> 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.

Not necessarily.
 
> 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: <1795459254-5857@newsgrouper.org>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!newsgrouper.org!.POSTED!not-for-mail
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> <JeHMS.106324$ATB4.99447@fx12.iad>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!newsgrouper.org!.POSTED!not-for-mail
From: MitchAlsup <user5857@newsgrouper.org.invalid>
Newsgroups: comp.arch
Subject: Re: Large constants in RISC
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> <JeHMS.106324$ATB4.99447@fx12.iad>
Date: Mon, 23 Nov 2026 18:40:54 GMT
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Message-ID: <1795459254-5857@newsgrouper.org>
Injection-Info: newsgrouper.org; mail-complaints-to="newsgrouper@yahoo.com"; posting-account=user5857
Injection-Date: Mon, 23 Nov 2026 18:40:54 GMT
User-Agent: Newsgrouper/0.9.0