Thread View: comp.arch
100 messages
100 total messages
Page 1 of 2
Started by Waldek Hebisch
Fri, 13 Nov 2026 14:30
Page 1 of 2 • 100 total messages
Large constants in RISC
Author: Waldek Hebisch
Date: Fri, 13 Nov 2026 14:30
Date: Fri, 13 Nov 2026 14:30
86 lines
5303 bytes
5303 bytes
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.
--
Waldek Hebisch
Re: Large constants in RISC
Author: Terje Mathisen
Date: Fri, 13 Nov 2026 16:12
Date: Fri, 13 Nov 2026 16:12
106 lines
6647 bytes
6647 bytes
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. Terje -- - <Terje.Mathisen at tmsw.no> "almost all programming can be viewed as an exercise in caching"
Re: Large constants in RISC
Author: Anton Ertl
Date: Fri, 13 Nov 2026 16:35
Date: Fri, 13 Nov 2026 16:35
34 lines
2830 bytes
2830 bytes
antispam@fricas.org (Waldek Hebisch) writes: >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. You only need one opcode here: When C is dead, set bits 37..12; when it is alive, set bits 63..38. Or alternatively, the instruction might shift the result of the first instruction left by 26 bits and insert its bits always at bits 37..12. Any other instruction clears C at the end (not before using it). One nasty aspect of this scheme is what you do when there is an interrupt or exception while C is alive. The easiest solution is probably to wait until C is dead before you serve the interrupt. But then C must not be alive at page boundaries, making code generation/linking harder. The other alternative is to save and restore C. But when you think about that, you see why Alpha and RV64G did not go for such a scheme, but for a scheme based on loading the constant from a constant table (which usually takes one or two instructions). - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Large constants in RISC
Author: Waldek Hebisch
Date: Fri, 13 Nov 2026 17:48
Date: Fri, 13 Nov 2026 17:48
127 lines
7532 bytes
7532 bytes
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.
--
Waldek Hebisch
Re: Large constants in RISC
Author: Robert Finch
Date: Fri, 13 Nov 2026 17:53
Date: Fri, 13 Nov 2026 17:53
94 lines
6176 bytes
6176 bytes
On 2026-11-13 9:30 a.m., 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. > Postfixes (suffixes?) for constants can be fused into the previous instruction. A wider instruction helps with 64-bit constants. No issue with interrupts, if landed on by an interrupt they can be treated as NOPs. Takes more bits than a variable length instruction.
Re: Large constants in RISC
Author: Waldek Hebisch
Date: Fri, 13 Nov 2026 17:57
Date: Fri, 13 Nov 2026 17:57
48 lines
3226 bytes
3226 bytes
Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
> antispam@fricas.org (Waldek Hebisch) writes:
>>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.
>
> You only need one opcode here: When C is dead, set bits 37..12; when
> it is alive, set bits 63..38. Or alternatively, the instruction might
> shift the result of the first instruction left by 26 bits and insert
> its bits always at bits 37..12. Any other instruction clears C at the
> end (not before using it).
Yes, that is one of possible variants.
> One nasty aspect of this scheme is what you do when there is an
> interrupt or exception while C is alive. The easiest solution is
> probably to wait until C is dead before you serve the interrupt. But
> then C must not be alive at page boundaries, making code
> generation/linking harder. The other alternative is to save and
> restore C.
Yes, interrupts need some solution. I was thinking rater on
restoring instruction pointer to the start of the sequence, at least
for page fauls. If hardware provides enough info complication
in page fault handler would be minimal, if any. Of course,
in such case page fault handler must make sure that both pages
are present, but there are many architectures that allow
instructions spanning page boundary and they handle it.
> But when you think about that, you see why Alpha and RV64G did not go
> for such a scheme,
RV64G allows 4 byte instruction with 2 byte alignment, so must handle
instructions spanning page boundary.
> but for a scheme based on loading the constant from
> a constant table (which usually takes one or two instructions).
--
Waldek Hebisch
Re: Large constants in RISC
Author: MitchAlsup
Date: Fri, 13 Nov 2026 18:36
Date: Fri, 13 Nov 2026 18:36
28 lines
1697 bytes
1697 bytes
antispam@fricas.org (Waldek Hebisch) posted: > 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. ------------------ +------+-----+-----+----+----+-----+ | OpC | Rd, | Rs1 | op | rt | Rs2 | +------+-----+-----+----+----+-----+ |.........lower 32-bits............| +----------------------------------+ |.........higher 32-bits...........| +----------------------------------+ OpC = 0b001 // 1 or 2-Operand OpC = 0b010 // 3-Operand Rt = 10xx // 32-bit constant Rt = 11xx // 64-bit constant xx = x0 // constant replaces Rs2 xx = x1 // constant replaces Rs1 No extra registers no extra instructions no need for fusing 64-bit VAS ready
Re: Large constants in RISC
Author: BGB
Date: Fri, 13 Nov 2026 19:12
Date: Fri, 13 Nov 2026 19:12
137 lines
7631 bytes
7631 bytes
On 11/13/2026 10:12 AM, Terje Mathisen 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?
>
I agree.
I went the prefix route, it works well in practice, and less bad than
most other options.
Even if yes, a prefix means the value is a bit chewed. But, this is
hardly a new thing in most RISC style designs.
> 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.
>
Yeah.
I guess I can note that I went one of two routes:
A Binary16 -> Binary64 constant load.
Works when constants are exactly represented.
Binary64 truncated to 32-bits for immediate values.
Actual constant was 33 bits,
but MSB was used to encode repeating-pattern cases.
0=Truncate
1=Repeating 8-bit pattern
In this case, did end up with in-pipeline converters for these cases.
Not free, but mostly acceptable, as it is "just moving bits around".
There is, however, a finite amount of space for immediate-related
pseudo-registers, and a budget cost for MUXing, though luckily these
sorts of cases would only apply to certain register ports.
> Terje
>
Re: Large constants in RISC
Author: BGB
Date: Fri, 13 Nov 2026 19:36
Date: Fri, 13 Nov 2026 19:36
138 lines
7998 bytes
7998 bytes
On 11/13/2026 11:53 AM, Robert Finch wrote:
> On 2026-11-13 9:30 a.m., 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.
>>
>
> Postfixes (suffixes?) for constants can be fused into the previous
> instruction.
> A wider instruction helps with 64-bit constants.
> No issue with interrupts, if landed on by an interrupt they can be
> treated as NOPs.
> Takes more bits than a variable length instruction.
>
But, requires looking at the following instruction words to decode the
instruction. So, creates a worse situation (than prefixes) in the case
of instructions crossing a cache-line boundary.
At least if one wants to allow for an implementation that figures out
instruction lengths and similar at cache-line fetch time.
Does create a pain point for superscalar, but at least the limitations
of "superscalar doesn't cross cache-line boundaries unless performed at
the time of instruction fetch" wont change visible results.
For a prefix, one can always know a suffix follows (if it appears at the
end of a cache line).
There was a considered option of:
Determine lengths and superscalar at the time of tag-checking and write
results back into I$;
Would avoid both the limitations of line-ingress and fetch-time handling;
But, is more complicated and would require additional pipeline stalls to
write this data back into the cache.
Though, this would be less of an issue with 32B cache lines than with
16B. Superscalar that only works within groups of 4 instructions being
kinda weak (can miss a lot of obvious cases of co-execution because they
happen to be at the wrong place in the cache line).
At present, my CPU design's I$ sort of uses a mixed strategy, doing part
of the work at ingress and part at the time of instruction fetch.
So, say:
Ingress Time: Determine lengths and fetch instruction control-bits;
Fetch Time: Check for register aliases and similar.
Combines with control bits to determine final fetch length.
...
>
Re: Large constants in RISC
Author: Robert Finch
Date: Fri, 13 Nov 2026 21:02
Date: Fri, 13 Nov 2026 21:02
160 lines
9321 bytes
9321 bytes
On 2026-11-13 2:36 p.m., BGB wrote: > On 11/13/2026 11:53 AM, Robert Finch wrote: >> On 2026-11-13 9:30 a.m., 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. >>> >> >> Postfixes (suffixes?) for constants can be fused into the previous >> instruction. >> A wider instruction helps with 64-bit constants. >> No issue with interrupts, if landed on by an interrupt they can be >> treated as NOPs. >> Takes more bits than a variable length instruction. >> > > But, requires looking at the following instruction words to decode the > instruction. So, creates a worse situation (than prefixes) in the case > of instructions crossing a cache-line boundary. Yes. The decoder looks at the following instruction space. When the instruction is extracted, the following instruction space is passed to the decoder. That makes it easy to work with. With a prefix one cannot always get the previous instruction. If the instruction is the first one fetched, where is the previous instruction? > > At least if one wants to allow for an implementation that figures out > instruction lengths and similar at cache-line fetch time. For fixed size instructions, there is no instruction length to figure out. They are always the same size. > > Does create a pain point for superscalar, but at least the limitations > of "superscalar doesn't cross cache-line boundaries unless performed at > the time of instruction fetch" wont change visible results. > The CPU is reading two cache lines in case an instruction spans a cache-line anyway. Suffixes can go on a ways into the upper cache line. > For a prefix, one can always know a suffix follows (if it appears at the > end of a cache line). > > > There was a considered option of: > Determine lengths and superscalar at the time of tag-checking and write > results back into I$; > Would avoid both the limitations of line-ingress and fetch-time handling; > But, is more complicated and would require additional pipeline stalls to > write this data back into the cache. > > > Though, this would be less of an issue with 32B cache lines than with > 16B. Superscalar that only works within groups of 4 instructions being > kinda weak (can miss a lot of obvious cases of co-execution because they > happen to be at the wrong place in the cache line). > > > At present, my CPU design's I$ sort of uses a mixed strategy, doing part > of the work at ingress and part at the time of instruction fetch. > > So, say: > Ingress Time: Determine lengths and fetch instruction control-bits; > Fetch Time: Check for register aliases and similar. > Combines with control bits to determine final fetch length. > > > ... Qupls uses fixed length instructions. Dealing with variable length instructions is real nasty at least for superscalar with several front-end pipeline stages. Variable length instructions need a good next-bundle predictor in the BTB possibly having the instruction points preset in the cache. What if the BTB predicts the next bundle of instructions wrong? One could have garbage instructions reaching later stages causing false prediction mismatches. Too complex for me. > > >> >
Re: Large constants in RISC
Author: BGB
Date: Fri, 13 Nov 2026 23:05
Date: Fri, 13 Nov 2026 23:05
276 lines
13771 bytes
13771 bytes
On 11/13/2026 3:02 PM, Robert Finch wrote:
> On 2026-11-13 2:36 p.m., BGB wrote:
>> On 11/13/2026 11:53 AM, Robert Finch wrote:
>>> On 2026-11-13 9:30 a.m., 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.
>>>>
>>>
>>> Postfixes (suffixes?) for constants can be fused into the previous
>>> instruction.
>>> A wider instruction helps with 64-bit constants.
>>> No issue with interrupts, if landed on by an interrupt they can be
>>> treated as NOPs.
>>> Takes more bits than a variable length instruction.
>>>
>>
>> But, requires looking at the following instruction words to decode the
>> instruction. So, creates a worse situation (than prefixes) in the case
>> of instructions crossing a cache-line boundary.
>
> Yes. The decoder looks at the following instruction space. When the
> instruction is extracted, the following instruction space is passed to
> the decoder. That makes it easy to work with. With a prefix one cannot
> always get the previous instruction. If the instruction is the first one
> fetched, where is the previous instruction?
Running backwards, the prefix case is just the same as the suffix case,
just running backwards:
If previous word is a prefix, we are not at the start of the instruction;
If previous word is not a prefix, we are at the start of an instruction.
In the original design of XG1, a similar property applied to 16/32:
Exxx/Fxxx: Start of a 32-bit instruction;
Else (usually): 16-bit op or part of a 32-bit instruction;
If prior word is Exxx/Fxxx and current word is not Exxx/Fxxx, we are
looking at the second word of a 32-bit instruction.
Granted, RISC-V does not have this property, and if you lose your place
in x86, you are SOL.
For XG3 and RV64+Jx, this property applies at the level of 32-bit
instruction words. Because, the only place a Jumbo Prefix is valid, is
when being used as a prefix.
Though, to be most useful, one needs a fair chunk of encoding space to
throw at these prefixes. This is a weak point of my RV+Jx scheme, as the
prefixes are small enough to barely allow Imm12->Imm33.
In XG3, for the Imm10->Imm33 case, there are actually a few bits left
over; but it allows Reg6->Imm33.
While it could theoretically be used to allow Imm10->Imm37, this gains
"basically nothing of value" over Imm33 so better to just leave these
bits reserved for future expansion.
As noted though: 27+27+10 => 64.
This is basically the same amount of encoding space that RISC-V spends
on its FMADD style instructions.
Mixing an XG3 jumbo prefix with a RV suffix could in theory allow for
Imm32 immediate-synthesis (from a Reg5), but I can note that my current
rules don't allow this (so, XG3 prefixes for XG3 instructions and RV-JX
prefixes for RV ops).
>>
>> At least if one wants to allow for an implementation that figures out
>> instruction lengths and similar at cache-line fetch time.
>
> For fixed size instructions, there is no instruction length to figure
> out. They are always the same size.
Possibly, as noted:
In XG3 everything is 32-bits, or larger, and unambiguous;
There was XG1, with 16-bit ops;
And, there is RV-C, with its set of 16-bit ops.
The RV-C ops are, horribly chewed.
While taken in isolation, each case is cheap:
XG1:
Exxx/Fxxx/7xxx/9xxx: 32-bit op, else 16-bit op;
FExx/FFxx: 64/96 bit
XG2:
FExx-xxxx/FFxx-xxxx: 64/96 bit, else 32-bit
XG3:
...xxxxx00: 32-bit (XG3 space)
...xxxxx01: 32-bit (XG3 space)
...xx11x10: 64/96 bit, else 32-bit (XG3 space)
Two prefixes in a row for 96-bit ops.
...0111111: 64/96 bit, else 32-bit (RV space)
RV64:
...xxxxx00: 16-bit
...xxxxx01: 16-bit
...xxxxx10: 16-bit
...xxxxx11: 32-bit space
...0011111: 48-bit space
...0111111: 64/96 bit, else 32-bit (RV space)
Current JX prefixes depend on Func3 for 64/96.
0xx: Generic 64-bit ops space
100: J21 prefix
101: -
110: J52 prefix (because 21+21+12 < 64)
111: -
This is enough to when all are taken together, it is enough to care.
There are some lookups that need to cover the instruction space for
superscalar.
With per-line tagging, does mean that I$ lines depend on the ISA mode;
currently this is treated as part of the cache-line hit/miss handling.
For RISc-V, there was a past rule (unused) for longer instruction encodings:
Func3 would specify the number of 16-bit units, allowing 80..192 bit ops.
Currently these are unused, and in my case there is no strong motive for
encodings past 96 bits. Had previously considered an "improved" scheme,
that would switch to coarser granularity and give more space to 96-bit
ops, but, not currently much strong reason to actually do so.
It was "less cramped" to merely define a J52 prefix as a 64-bit encoding.
Though, the J52 prefix would mean needing to look 2x 32-bit words in the
past to detect whether or not the current instruction is part of a
96-bit op in the RV case.
>>
>> Does create a pain point for superscalar, but at least the limitations
>> of "superscalar doesn't cross cache-line boundaries unless performed
>> at the time of instruction fetch" wont change visible results.
>>
> The CPU is reading two cache lines in case an instruction spans a cache-
> line anyway. Suffixes can go on a ways into the upper cache line.
>
Possibly.
But, in most cases this can be detected in advance with a prefix scheme,
but not with a postfix scheme.
>> For a prefix, one can always know a suffix follows (if it appears at
>> the end of a cache line).
>>
>>
>> There was a considered option of:
>> Determine lengths and superscalar at the time of tag-checking and
>> write results back into I$;
>> Would avoid both the limitations of line-ingress and fetch-time handling;
>> But, is more complicated and would require additional pipeline stalls
>> to write this data back into the cache.
>>
>>
>> Though, this would be less of an issue with 32B cache lines than with
>> 16B. Superscalar that only works within groups of 4 instructions being
>> kinda weak (can miss a lot of obvious cases of co-execution because
>> they happen to be at the wrong place in the cache line).
>>
>>
>> At present, my CPU design's I$ sort of uses a mixed strategy, doing
>> part of the work at ingress and part at the time of instruction fetch.
>>
>> So, say:
>> Ingress Time: Determine lengths and fetch instruction control-bits;
>> Fetch Time: Check for register aliases and similar.
>> Combines with control bits to determine final fetch length.
>>
>>
>> ...
> Qupls uses fixed length instructions. Dealing with variable length
> instructions is real nasty at least for superscalar with several front-
> end pipeline stages. Variable length instructions need a good next-
> bundle predictor in the BTB possibly having the instruction points
> preset in the cache. What if the BTB predicts the next bundle of
> instructions wrong? One could have garbage instructions reaching later
> stages causing false prediction mismatches. Too complex for me.
>
As noted, this is why both XG3 and my implementation of RV64 only work
with 32-bit aligned 32-bit instructions.
Dealing with superscalar + RV-C cases would be too much pain and cost.
So, as soon as RV-C appears, or the instructions are misaligned, the I$
gives up and falls back to "one instruction at a time".
Re: Large constants in RISC
Author: John Savard
Date: Sat, 14 Nov 2026 01:28
Date: Sat, 14 Nov 2026 01:28
13 lines
1684 bytes
1684 bytes
I am entirely puzzled by your post, although I was wrestling with this issue myself. If the problem is that, since every instruction is 32 bits long, immediate values in instructions can't be 32 or 64 bits long, how does adding a register solve the problem? However, if the bit that says that register is 'live' also changes the *instruction set*, so that now a large portion of the opcode space is devoted to 32 bit instructions that are mostly data with only a few bits for the opcode, which is what it looked like you're suggesting... then it *addresses* the issue, even if I wouldn't say that it solves the problem. John Savard
Re: Large constants in RISC
Author: 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"
Re: Large constants in RISC
Author: Waldek Hebisch
Date: Sat, 14 Nov 2026 15:02
Date: Sat, 14 Nov 2026 15:02
41 lines
3052 bytes
3052 bytes
John Savard <quadibloc@invalid.com> wrote:
> I am entirely puzzled by your post, although I was wrestling with this
> issue myself.
> If the problem is that, since every instruction is 32 bits long,
> immediate values in instructions can't be 32 or 64 bits long, how does
> adding a register solve the problem?
> However, if the bit that says that register is 'live' also changes the
> *instruction set*, so that now a large portion of the opcode space is
> devoted to 32 bit instructions that are mostly data with only a few
> bits for the opcode, which is what it looked like you're suggesting...
> then it *addresses* the issue, even if I wouldn't say that it solves
> the problem.
The point is that you get multinstruction sequences which behave
very much like single longer instruction containing a constant.
So you can write programs like for machine with variable length
instruction set. But each element of such sequence is a valid
instruction, so instruction decoders can work fully in parallel.
For best efficiency you want instruction fusing, but this is
optional performace enhancing feature. On one end you can
execute parts as separate instructions, paying extra latency
during execution. You can partially fuse (say fuse pairs),
limiting (or eliminating for moderately sized constants)
execution latency, but complicating decoders. Or you can
fully fuse.
Note that with fixed length 32 bit instructions, whatever you
do there will be 32 bit constants that do not fit into single
instruction. In practice such constants need to be handled
either by providing extra data for instruction, say by using
variable length instructions (so rejection fixed length
principle) or by some more funky schemes like out of band data
(which again means that instructions really are not fixed
lenght), or by using multi instruction sequences. My choice
is to use multi instruction sequences. Compared to other
such schemes it has advantage of convenience for programmers:
the sequence is equivalent to a longer instruction, so various
irregularities in instruction selection are gone.
--
Waldek Hebisch
Re: Large constants in RISC
Author: Waldek Hebisch
Date: Sat, 14 Nov 2026 15:13
Date: Sat, 14 Nov 2026 15:13
139 lines
8329 bytes
8329 bytes
Terje Mathisen <terje.mathisen@tmsw.no> wrote:
> 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.
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.
--
Waldek Hebisch
Re: Large constants in RISC
Author: Theo
Date: Sat, 14 Nov 2026 18:04
Date: Sat, 14 Nov 2026 18:04
30 lines
1728 bytes
1728 bytes
BGB <cr88192@gmail.com> wrote: > On 11/13/2026 10:12 AM, Terje Mathisen wrote: > > Waldek Hebisch wrote: > > > > Why not just have a prefix opcode that says that the next 32 or 64 bits > > is a constant? > > > > I agree. > > > I went the prefix route, it works well in practice, and less bad than > most other options. > > Even if yes, a prefix means the value is a bit chewed. But, this is > hardly a new thing in most RISC style designs. On aarch32 it could be something simple like: LDR rN, [pc,#4]! DCD 0xdeadbeef ; code continues here although on small designs that might create a pipeline bubble. It wouldn't with a branch predictor. That would be harder on ISAs where the pc is hidden away, but the prefix instruction could have a similar effect. Theo
Re: Large constants in RISC
Author: MitchAlsup
Date: Sat, 14 Nov 2026 18:53
Date: Sat, 14 Nov 2026 18:53
31 lines
2317 bytes
2317 bytes
Terje Mathisen <terje.mathisen@tmsw.no> posted: > Waldek Hebisch wrote: > > Terje Mathisen <terje.mathisen@tmsw.no> wrote: > >> Waldek Hebisch wrote: ---------------------------- > > 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
Re: Large constants in RISC
Author: BGB
Date: Sat, 14 Nov 2026 19:05
Date: Sat, 14 Nov 2026 19:05
365 lines
16102 bytes
16102 bytes
On 11/14/2026 9:13 AM, Waldek Hebisch wrote:
> Terje Mathisen <terje.mathisen@tmsw.no> wrote:
>> 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.
>
> 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.
Within the 33 bit spike, seemingly:
~ 67% fall in 0 .. +2G
~ 16% fall in +2G .. +4G
~ 16% fall in -1 .. -2G
~ 1% falls in -2G .. -4G
The 17-bit spike appears to follow a similar pattern.
For 2RI (LI or ADD):
~ 77% fit into 10 bits;
~ 79% fit into 12 bits.
~ 88% fit into 17 bits.
Of those that fall outside of the 17 bit range, it is roughly a 50%
split between the 32 and 64 bit spikes.
Apart from the values near the spikes, there tends to be very little
between ~ 34 and ~60 bits; so not likely bothering with any intermediate
sizes.
Like, if you have the option of a 48 or 56 bit constant, while one might
think they are "much of the way" to covering 64 bits, actually, no, they
are not. A 56-bit immediate field would hit barely anything more than a
33 bit field.
For normal Load/Store displacements (excluding global variables):
10-bit (unscaled): ~ 92%
12-bit (unscaled): ~ 98%
10-bit ( scaled): ~ 98%
12-bit ( scaled): ~ 99%
For global variables, it is generally better to have a 16-bit scaled
displacement that can (mostly) reach across a typical ".data" section.
It may also make sense for the compiler to sort global variables by
usage ranking, and to promote any commonly used variables from ".bss" to
".data".
That said, a 10 or 12 bit displacement still isn't enough to hit all of
the global variables in a typical program, so something larger, like 16
bits, can get a much better hit rate.
This is assuming an ABI which accesses global variables relative to a
global-pointer or similar.
If using PC-relative addressing, one will need somewhere in the area of
22 to 24 bits to cross .text + .data + .bss.
Can note that for arithmetic operations:
~ 60%: 3R (3 register)
~ 40%: 3RI (2 registers and immediate)
If splitting by operation in general (descending ranking):
ADD/SUB
Compare
Shift
OR
AND
MUL
XOR
If splitting by operation, likelihood of having an immediate:
Shift
Compare
ADD/SUB
AND
OR
MUL
XOR
DIV and MOD aren't usually checked, but are rarely used operators in
practice (when excluding cases where DIV and MOD aren't by a constant
power-of-two).
DIV and MOD by a constant power of two are a bit of an anomaly, but MOD
can be turned into an AND operator. DIV can't quite be turned into a
bare shift, mostly because DIV would round towards zero and Shift
towards -Inf. So, for correct semantics, turning the DIV case into a
shift requires conditionally adding a bias value if negative.
As for types:
Int32
Pointers
Int64 / UInt64
UInt32
... everything else ...
Had observed that XOR-with-Immediate and ADD-Uint32-with-Immediate are
nearly equal probability; but both small if compared with 32-bit ADD
(where, 'int' tends to be a very heavily used type).
Can note that Shift and Compare are special:
A strong majority of shift operations use a constant;
For compare, compare-with-immediate outranks 3-register forms;
For most other operators, the register forms tend to dominate.
Statistically speaking, floating-point operations and types are
in-general, rarely used if compared with integer operations.
Can noted that for floating-point constants, around 80% hit as Binary16.
Much of the rest have a repeating pattern in the fraction;
A small minority number represent irrational numbers or similar.
Most also fit the pattern of having between 1 and 4 digits past the
decimal point; but not a good way to leverage this in hardware (easier
to target the repeating fractions).
Mostly better to leave base-level floating-point ops as 3-register only,
and only bother with large-encodings for any immediate cases.
While a 5 or 6-bit FP immediate can work (E3.M2 or similar), it almost
isn't really worth the bother.
...
For addressing modes, had noted:
Static:
[Reg+Disp] : ~ 80%
[Reg+Index*Sc] : ~ 18% (Sc nearly always element size)
Anything else : ~ 2%
Dynamic:
[Reg+Disp] : ~ 50%
[Reg+Index*Sc] : ~ 44% (Sc nearly always element size)
Anything else : ~ 6%
So, register-indexed addressing has a smaller effect on static program
size, but a much bigger impact on actual performance.
Cases where [Reg+Index*Sc+Disp] or auto-increment would be applicable
are much rarer.
Like, even if it seems like C code has a lot of "*s++" and similar,
statistically speaking, not so much.
The exact skew between addressing modes does depend on how
prologs/epilogs are handled:
Individual loads/stores:
More [Reg+Disp] dominance (~ 95%);
Load/store pair: ~ 80% (as above)
(The LDP/STP end up as one of the more heavily used ops).
Load/Store Multiple: [Reg+Disp], ~ 68%
(But, this involves the "evil" of a state machine).
There is also a tuning factor based on how many GPRs one has (32-vs-64):
32: Slightly fewer in prologs/epilogs but more in spill/fill;
64: Longer prologs/epilogs but very little spill/fill.
On a 16 GPR machine, spill-and-fill would tend to dominate.
Contrast to a 64 GPR machine where for many functions it often becomes
more viable to statically pin each variables' live range to a register
(with no spill/fill at all).
Personally I have noted that spill/fill can't be mostly eliminated until
reaching 64 GPRs.
A big factor is the percentage of functions that fit within N local
variables:
12: ~ 52%
16: ~ 73%
24: ~ 84%
28: ~ 87%
Or, say, with RV (~ 28 GPRs, with 12 callee save), roughly half of
functions could avoid spill-and-fill (this would be, say, the normal
RISC-V case).
In my case, with XG3, it is up to 87%.
If you go much smaller than 12, it tanks, so unless one has at least 32
GPRs, most functions are going to need spill-and-fill.
There is a long tail though, would need ~ 256 callee save registers to
reach 100% (mostly to deal with the rare few functions that have ~ 200
local variables). However, this is clearly impractical.
But, one could get ~ 98% coverage with a machine with 128 GPRs, but this
is also going to require a 40 or 48 bit instruction word; or falling
back to 2R for most ops, which is arguably worse.
I went for 64 GPRs, but as noted:
Slightly smaller immediate values;
Slightly less opcode space;
...
But, I can use the prefix encodings to offload some common ops into
64-bit encodings, and so don't need to commit to strictly 32-bit
fixed-length instructions (instead, it is merely "most of the
instructions are 32-bit").
There are fudge-factors though, like cases where variables have
non-overlaping lifetimes, or cases where scratch registers can be used, ...
...
Re: Large constants in RISC
Author: MitchAlsup
Date: Sat, 14 Nov 2026 19:06
Date: Sat, 14 Nov 2026 19:06
54 lines
3007 bytes
3007 bytes
Terje Mathisen <terje.mathisen@tmsw.no> posted:
> Waldek Hebisch wrote:
> > Terje Mathisen <terje.mathisen@tmsw.no> wrote:
> >> Waldek Hebisch wrote:
----------------------------
> > 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.
28-bits (26-bits<<2) is fine only for statically linked modules.
> > 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.
If/when your polynomial code looks like::
LD Rt,[&constant1]
FMAC Rs,Rr,Rt,Rs
LD Rt,[&constant2]
FMAC Rs,Rr,Rt,Rs
you save 50% of the instructions as:
FMAC Rs,Rr,#constant1,Rs
FMAC Rs,Rr,#constant2,Rs
And you avoid DCache pollution.
> Ask Mitch!
>
> Seems he's getting very consistent 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.
If you count 70% of the instruction count as my gain of 30%, on average,
large constants are 10%-12% of that 30%.
If you look at the tabulations in H&P you can derive a similar number
1st edition would give 9%-ish for a 32-bit architecture {after you do
some massaging of the table data}}
>
> Terje
Re: Large constants in RISC
Author: Anton Ertl
Date: Sat, 14 Nov 2026 19:10
Date: Sat, 14 Nov 2026 19:10
18 lines
1910 bytes
1910 bytes
antispam@fricas.org (Waldek Hebisch) writes: >I read what he wrote and I do not doubt that code having constants >in instructions is faster. AMD64 has large constants (or at least better support for large constants than ARM A64), yet according to reviews ARM A64 implementations form Apple and IIRC also Qualcomm offer better performance than the best AMD64 implementations. >What is not clear how frequently >code with explicit constants appears in real life. That can be and has been measured, and IIRC results have been posted here. - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Large constants in RISC
Author: BGB
Date: Sat, 14 Nov 2026 19:26
Date: Sat, 14 Nov 2026 19:26
62 lines
3294 bytes
3294 bytes
On 11/14/2026 12:04 PM, Theo wrote: > BGB <cr88192@gmail.com> wrote: >> On 11/13/2026 10:12 AM, Terje Mathisen wrote: >>> Waldek Hebisch wrote: >>> >>> Why not just have a prefix opcode that says that the next 32 or 64 bits >>> is a constant? >>> >> >> I agree. >> >> >> I went the prefix route, it works well in practice, and less bad than >> most other options. >> >> Even if yes, a prefix means the value is a bit chewed. But, this is >> hardly a new thing in most RISC style designs. > > On aarch32 it could be something simple like: > > LDR rN, [pc,#4]! > DCD 0xdeadbeef > ; code continues here > > although on small designs that might create a pipeline bubble. It wouldn't > with a branch predictor. > > That would be harder on ISAs where the pc is hidden away, but the prefix > instruction could have a similar effect. > Yeah, basically. Can note that in my current ISA, there is neither a directly accessible PC nor an auto increment mode; so couldn't go that route. PC as a base register can be encoded indirectly though. However, without special magic in the decoder, such a memory load would have higher latency than using a prefix. And the "magic" needed to make prefixes fast would less than that needed to deal with special cases like "PC-auto-increment load". While the ancestor ISA had auto-increment, my own ISA efforts had dropped it mostly as it tended not to offer enough of a performance advantage to be worthwhile, and I was typically more focused on performance than on strictly optimizing code density (where auto-increment would still have some merit). Big issue is basically that auto-increment was basically almost never faster than a separate Load/Store + ADD sequence in practice. And, in certain cases (like PUSH/POP) actually slower than the "adjust stack once and use a sequence of Loads/Stores" route. So, a direct equivalent of PUSH/POP also got thrown out. ... > Theo
Re: Large constants in RISC
Author: BGB
Date: Sat, 14 Nov 2026 19:35
Date: Sat, 14 Nov 2026 19:35
41 lines
2744 bytes
2744 bytes
On 11/14/2026 1:10 PM, Anton Ertl wrote: > antispam@fricas.org (Waldek Hebisch) writes: >> I read what he wrote and I do not doubt that code having constants >> in instructions is faster. > > AMD64 has large constants (or at least better support for large > constants than ARM A64), yet according to reviews ARM A64 > implementations form Apple and IIRC also Qualcomm offer better > performance than the best AMD64 implementations. > WRT x86 (and x86-64 / AMD64): There is only so much you can polish a turd. Or, the big constants and relative niceties in some places, can't cover for the issue that in nearly every other regard it is kinda awful on this front. We are sort of reaching a point at this point, where jumping to a different architecture and then emulating x86 / x86-64 could become preferable to actually running the ISA natively. >> What is not clear how frequently >> code with explicit constants appears in real life. > > That can be and has been measured, and IIRC results have been posted here. > Yes. As I can note, cases where "prefix makes a 33 bit constant" are common enough to be worthwhile; even if still small in an absolute sense. Full 64-bit constants are uncommon enough to where one can debate them, but there are still cases where they can pay off. > - anton
Re: Large constants in RISC
Author: John Levine
Date: Sat, 14 Nov 2026 20:46
Date: Sat, 14 Nov 2026 20:46
34 lines
2520 bytes
2520 bytes
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. 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. -- Regards, John Levine, johnl@taugh.com, Primary Perpetrator of "The Internet for Dummies", Please consider the environment before reading this e-mail. https://jl.ly
Re: Large constants in RISC
Author: 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.
Re: Large constants in RISC
Author: Waldek Hebisch
Date: Sat, 14 Nov 2026 22:53
Date: Sat, 14 Nov 2026 22:53
29 lines
2268 bytes
2268 bytes
Anton Ertl <anton@mips.complang.tuwien.ac.at> wrote:
> antispam@fricas.org (Waldek Hebisch) writes:
>>I read what he wrote and I do not doubt that code having constants
>>in instructions is faster.
>
> AMD64 has large constants (or at least better support for large
> constants than ARM A64), yet according to reviews ARM A64
> implementations form Apple and IIRC also Qualcomm offer better
> performance than the best AMD64 implementations.
All I have seen are general claims. It is not clear to me
how various factors lead to observed results. It is clear
that advantage for x86_64 in handling constants is way too
small change result of comparison.
>>What is not clear how frequently
>>code with explicit constants appears in real life.
>
> That can be and has been measured, and IIRC results have been posted here.
I remember various statistics. What I wrote was specifically
in context of floating point constants. If published statistics
are relevant for them, then such constants are quite rare.
But there are reasons that could easily bias the current
results, so before more definitive information is available
I do not want to prematuraly declare this as known.
--
Waldek Hebisch
Re: Large constants in RISC
Author: Robert Finch
Date: Sun, 15 Nov 2026 00:38
Date: Sun, 15 Nov 2026 00:38
48 lines
3219 bytes
3219 bytes
On 2026-11-14 3:46 p.m., 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. One ends up with constants in the data cache. May be slower than having constants inline from the I$. One thought it to have instructions that can load constants using the I$. Qupls wastes about 3% of the opcode space for constant postfixes. The constant decoder is maybe a little disproportionate to its value. > > 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. > > PowerPC uses shifted immediate instructions. IIRC there were four of them. ARM has shifted constant instruction too. For Qupls it was an option at one point. Shifted immediate instructions are appealing as they can stick with the same instruction size.
Re: Large constants in RISC
Author: Brian G. Lucas
Date: Sun, 15 Nov 2026 00:57
Date: Sun, 15 Nov 2026 00:57
52 lines
3790 bytes
3790 bytes
On 11/14/26 6:38 PM, Robert Finch wrote: > On 2026-11-14 3:46 p.m., 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. > > One ends up with constants in the data cache. May be slower than having > constants inline from the I$. One thought it to have instructions that can > load constants using the I$. > No. No. No. In MCore the constants were loaded from an offset in the instruction stream. The constants were placed after an unconditional branch which terminated a basic block. Often the instructions were in i-cache that had already been loaded in order to execute the basic block. Not exactly free, but often so. All MCore instructions were 16-bits, and only 8-bit (or later 7-bit) immediates were encoded in the instruction. > Qupls wastes about 3% of the opcode space for constant postfixes. > The constant decoder is maybe a little disproportionate to its value. >> >> 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. >> >> > PowerPC uses shifted immediate instructions. IIRC there were four of them. ARM > has shifted constant instruction too. For Qupls it was an option at one point. > > Shifted immediate instructions are appealing as they can stick with the same > instruction size.
Re: Large constants in RISC
Author: John Savard
Date: Sun, 15 Nov 2026 01:51
Date: Sun, 15 Nov 2026 01:51
42 lines
3036 bytes
3036 bytes
On Sat, 14 Nov 2026 15:02:12 -0000 (UTC), antispam@fricas.org (Waldek Hebisch) wrote: >My choice >is to use multi instruction sequences. Compared to other >such schemes it has advantage of convenience for programmers: >the sequence is equivalent to a longer instruction, so various >irregularities in instruction selection are gone. Yes, that's true enough. While I had trouble understanding your initial post, when I thought about the basic idea, I saw that it was indeed reasonable. I suppose if I did something like it in Concertina, it would have looked like this: 111 followed by 29 bits would shift floating register 31 by 32 bits to the right, putting 000 followed by those 29 bits at the beginning. One such instruction would put most of a 32-bit constant in the first 32 bits of that register, two most of a 64-bit constant in the first 64 bits, and four most of a 128-bit constant in the whole register. Then an instruction with an "immediate" value would only need to use 3, 6, or 12 bits of the value within the 32-bit instruction itself, so it could fit in 32 bits. A fancy implementation could process the entire 64, 96, or 160-bit sequence of instructions as a single instruction with a normal immediate, just also reproducing the side effect of affecting floating register 31. Given that, I guess one could say it's a solution to the problem - it avoids the need to change instruction lengths, it works in a simple implementation that processes instructions strictly 32 bits at a time. And it's not as if I hadn't tried something similar recently, but for a different purpose. I took integer register 8, and filled it with bits, but their purpose was not to serve as immediate values, but just to lengthen instructions by a few bits - packaging the extra bits for several instructions neatly into a 32-bit instruction preceding them. John Savard
Re: Large constants in RISC
Author: John Savard
Date: Sun, 15 Nov 2026 01:57
Date: Sun, 15 Nov 2026 01:57
24 lines
2175 bytes
2175 bytes
On Sat, 14 Nov 2026 15:13:14 -0000 (UTC), antispam@fricas.org (Waldek Hebisch) wrote: >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. I guess that depends on how you define real life. It makes sense for a program to read in values, rather than depending on hardcoded values, if there's any chance at all they might change. Recompiling as a way to work on a new, but closely related, problem is wasteful. Even so, there were lots of constants in the FORTRAN programs I wrote as exercises when learning how to program. I still use a BASIC program to draw vector world maps in a number of projections, which I use to illustrate a section of my web site about cartography. The points ending the line segments of coastlines are in a file. The constants in the formulas describing the map projections are in the program itself. John Savard
Re: Large constants in RISC
Author: John Savard
Date: Sun, 15 Nov 2026 02:05
Date: Sun, 15 Nov 2026 02:05
35 lines
2728 bytes
2728 bytes
On Sat, 14 Nov 2026 19:10:17 GMT, anton@mips.complang.tuwien.ac.at (Anton Ertl) wrote: >antispam@fricas.org (Waldek Hebisch) writes: >>I read what he wrote and I do not doubt that code having constants >>in instructions is faster. > >AMD64 has large constants (or at least better support for large >constants than ARM A64), yet according to reviews ARM A64 >implementations form Apple and IIRC also Qualcomm offer better >performance than the best AMD64 implementations. Why do you say "yet", given that support for large constants is not something that mitigates against performance? Oh, I'm sorry: I misread what you wrote. I assumed ARM A64 had the better support for large constants, and didn't see the letters on the screen in front of me. However, support for large constants, while it contributes to better performance, is just one factor out of many. AMD64 has a baroque instruction set, so in an ARM A64 chip, a larger proportion of the transistors are doing useful work. When power and heat are the limiting factors, that should matter. But what I would say instead of what you said would be... "AMD64 has a huge market for powerful desktop computers and servers, while the only big market for ARM A64 is smartphones, and yet according to reviews..." ...because, at least up until very recently, Intel has had so much money to spend that it could afford to engineer its chips so cleverly as to make up for the CISC versus RISC disadvantage. John Savard
Re: Large constants in RISC
Author: Robert Finch
Date: Sun, 15 Nov 2026 02:06
Date: Sun, 15 Nov 2026 02:06
61 lines
4273 bytes
4273 bytes
On 2026-11-14 7:57 p.m., Brian G. Lucas wrote: > On 11/14/26 6:38 PM, Robert Finch wrote: >> On 2026-11-14 3:46 p.m., 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. >> >> One ends up with constants in the data cache. May be slower than >> having constants inline from the I$. One thought it to have >> instructions that can load constants using the I$. >> > No. No. No. In MCore the constants were loaded from an offset in the > instruction stream. The constants were placed after an unconditional > branch which terminated a basic block. Often the instructions were in i- > cache that had already been loaded in order to execute the basic block. > Not exactly free, but often so. All MCore instructions were 16-bits, > and only 8-bit (or later 7-bit) immediates were encoded in the instruction. > Tried something similar in another project and it worked but the code density was crpy. Constants were piled up at the end of a cache line then accessed with an offset stored in the instruction. The core tracked where the constants were and marked them as NOPs. A cache line always had to start with an instruction. >> Qupls wastes about 3% of the opcode space for constant postfixes. >> The constant decoder is maybe a little disproportionate to its value. >>> >>> 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. >>> >>> >> PowerPC uses shifted immediate instructions. IIRC there were four of >> them. ARM has shifted constant instruction too. For Qupls it was an >> option at one point. >> >> Shifted immediate instructions are appealing as they can stick with >> the same instruction size. >
Re: Large constants in RISC
Author: Anton Ertl
Date: Sun, 15 Nov 2026 07:34
Date: Sun, 15 Nov 2026 07:34
18 lines
2051 bytes
2051 bytes
antispam@fricas.org (Waldek Hebisch) writes: >What I wrote was specifically >in context of floating point constants. SSE2 (used by default on AMD64) accesses FP constants as normal memory operands. That includes FP masks, as used for FP negation and FP absolute. Back to constants in GPRs: On Alpha the constant table is also used for loading the addresses of functions in a different compilation unit, because bsr can only branch within IIRC 22 bits of relative address, and the register-indirect jsr is used for other calls. This kind of stuff has the potential to influence the statistics. - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Large constants in RISC
Author: Thomas Koenig
Date: Sun, 15 Nov 2026 10:06
Date: Sun, 15 Nov 2026 10:06
45 lines
2683 bytes
2683 bytes
Waldek Hebisch <antispam@fricas.org> schrieb: > 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. Some statistics, from counting assembly on my favorite applications. "labels mem" can be either 32-bit or 64-bit, depending on the memory model. The results depend on the current state of the compiler, of course. gnuplot: Instructions: 128531 16-bit-immediates mem: 11086 ( 8.63%) Labels mem: 25757 (20.04%) 5-bit immediates. 26520 (20.63%) 16-bit immediates: 7129 ( 5.55%) 32-bit-immediates: 627 ( 0.49%) 64-bit-immediates: 1191 ( 0.93%) Perl: Instructions: 552093 16-bit-immediates mem: 89642 (16.24%) Labels mem: 58505 (10.60%) 5-bit immediates. 114569 (20.75%) 16-bit immediates: 39361 ( 7.13%) 32-bit-immediates: 9075 ( 1.64%) 64-bit-immediates: 5427 ( 0.98%) GSL (GNU Scientific Library): Instructions: 279126 16-bit-immediates mem: 31401 (11.25%) Labels mem: 13266 ( 4.75%) 5-bit immediates. 57499 (20.60%) 16-bit immediates: 13957 ( 5.00%) 32-bit-immediates: 4350 ( 1.56%) 64-bit-immediates: 4418 ( 1.58%) -- This USENET posting was made without artificial intelligence, artificial impertinence, artificial arrogance, artificial stupidity, artificial flavorings or artificial colorants.
Re: Large constants in RISC
Author: Anton Ertl
Date: Sun, 15 Nov 2026 16:08
Date: Sun, 15 Nov 2026 16:08
43 lines
3012 bytes
3012 bytes
BGB <cr88192@gmail.com> writes: >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. The nice thing about the load is that its address is usually available right away. So the load operation is typically performed as soon as possible, and for a sequence load r1, d(rct) add r3=r1+r2 where rct is the register pointing to the constant table, the result may be ready in r1 before r2 is ready. The bottom line is that this load typically does not cost latency. Concerning cache misses: If the code is executed often, the constant will be in the (L1) D-Cache. If there is a cache miss, the code has not been executed recently. Bottom line: Cache misses are rare, because caches work. >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. How many globals does your program have. In order to be able to address the globals of a program with a single global pointer, the linker needs to do quite a bit of work. Maybe the RISC-V linker does. For the Alpha every compilation unit has its own constant and global table as far as I remember (and I think the same register is used for both on the Alpha). - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Large constants in RISC
Author: Waldek Hebisch
Date: Sun, 15 Nov 2026 16:23
Date: Sun, 15 Nov 2026 16:23
82 lines
5023 bytes
5023 bytes
John Levine <johnl@taugh.com> 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.
Other sources and my experience indicates higher percentage of
constants. I have a simple code generator for Risc-V. From
relevant sources it generates 917544 bytes of object code.
Assembly contains 13906 instances of 'la' and 8326 instances
of 'lui'. I am not sure how many istructions are there, but
reasonable estimante is about 250000. So probably about 8%
of instructions needs constants bigger than 12 bit. Changing
from literal pool to 'la' and 'lui' reduced total size by some
percents. Maybe not big thing, but it shows that constants
in instructions save total bits needed for program. Also,
constan pools decrease efficiency of caches. Namely, if
a line shares code and data, then it needs to be loaded to
both caches, possibly causing expensive changes of ownership
during accesses. If you put code and data in separtate
lines, then there is fragmentation.
ATM for constants bigger than what can by handled by 'la'
and 'lui' I use constant pools. Note that in my use case
I set up apropriate base register for other reasons, so
base register has no extra cost. Otherwise there would
be extra cost, that is I would have to use 'la' to load
address of the constant and then use it to load constant
itself. Of course if possible it is better to directly
load needed address using 'la' instead of having
extra indirection.
> 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.
360 is not a good example to follow. Initial software conventions
were quite inefficient. Later they adapted better conventions
and new instructions helped too. But IBM literature warned of
possible performance troubles due to literal pool (need to
keep literal pool at sufficient distance from code so that
it will use separate cache lines). Beside efficiency there
is also aspect of software complexity. Part of RISC philosophy
seem the be that compilers can cover up hardware limitations.
But AFAIK dealing with case when register allocator runs
out of registers trying to generate spill code was a reccuring
source of bugs in gcc. This is related because to spill one
needs to generate store with constant offset, and when this
constant can not be part of instruction one need a free
register to generate spill code (but you spill because you
are running out of free registers!). In case of 360 I
remember compiler bug discovered by a test program.
Relevant routine IIRC was slightly larger than 64 kB
(or maybe 32 kB) so gcc tried to split constant pool so
that it was in range of available displacements. But
initial split was not good and I got an ICE. This was
fixed, but shows that low frequency probelematic cases
may have negligible impact on average performance, but
offer plenty of space where bugs may hide and bit later.
--
Waldek Hebisch
Re: Large constants in RISC
Author: Anton Ertl
Date: Sun, 15 Nov 2026 16:28
Date: Sun, 15 Nov 2026 16:28
25 lines
2318 bytes
2318 bytes
Robert Finch <robfi680@gmail.com> writes: >One ends up with constants in the data cache. May be slower than having >constants inline from the I$. One thought it to have instructions that >can load constants using the I$. There is no data path from the I-cache to the load unit in existing CPUs. I also expect that if you add such a data path, it will be slower, because the I-cache is not close to the LSU, but close to the decoder and the branch predictor. So that idea is not going to fly. The better approach is to have the data in the instructions, and extract it during instruction decoding, like all the immediate operands in all instruction sets around do. Or to have the data in data memory and access it with a load that works with the D-cache, like Alpha and others do. >ARM has shifted constant instruction too. Not in ARM A64. But ARM A64 has an interesting encoding for certain bit patterns. - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Large constants in RISC
Author: Anton Ertl
Date: Sun, 15 Nov 2026 16:36
Date: Sun, 15 Nov 2026 16:36
63 lines
4252 bytes
4252 bytes
quadibloc@invalid.com (John Savard) writes: >On Sat, 14 Nov 2026 19:10:17 GMT, anton@mips.complang.tuwien.ac.at >(Anton Ertl) wrote: >>antispam@fricas.org (Waldek Hebisch) writes: >>>I read what he wrote and I do not doubt that code having constants >>>in instructions is faster. >> >>AMD64 has large constants (or at least better support for large >>constants than ARM A64), yet according to reviews ARM A64 >>implementations form Apple and IIRC also Qualcomm offer better >>performance than the best AMD64 implementations. ... >However, support for large constants, while it contributes to better >performance, is just one factor out of many. AMD64 has a baroque >instruction set, so in an ARM A64 chip, a larger proportion of the >transistors are doing useful work. And included in the "baroque" instruction set are instructions with 32-bit immediate operands, and a mov instruction with a 64-bit immediate operand. In any case, there is no empirical evidence "that having [large] constants in instructions is faster." And the weak evidence that we currently have, points in the other direction. So we have every reason to doubt that claim. >When power and heat are the >limiting factors, that should matter. Power and heat have been limiting factors since about 2005, if not earlier. In 2006, Apple switched from PowerPC (a RISC) to Intel's "baroque" IA-32 and later AMD64 offerings. >"AMD64 has a huge market for powerful desktop computers and servers, >while the only big market for ARM A64 is smartphones, and yet >according to reviews..." > >...because, at least up until very recently, Intel has had so much >money to spend that it could afford to engineer its chips so cleverly >as to make up for the CISC versus RISC disadvantage. In the late 1990s, AMD was a much smaller company than Intel, and also smaller than the larger members of the AIM (Apple, IBM, Motorola) consortium, and the "baroqueness" of IA-32 played a bigger role in the CPUs of the day, yet AMD managed to beat Intel in the GHz race and was very competetive in the following years, while the offerings of the AIM consortium were slow and only kept afloat by the faith in the church of Apple. And eventually Apple switched to Intel. It's not clear why the ARM A64 microarchtitects manage to outcompete AMD64 these days, while the AIM microarchitects could not do that in the early 2000s. Maybe there is something in the instruction sets that plays a role when you do 10-wide microarchitecture that was not an issue for 3-wide microarchitectures in the early 2000s. Maybe Intel's APX (32 GPRs, 3-address encoding) is a reaction to Apple M1 having better IPC, and maybe we will see better IPC on Intel machines with APX. I would not bet on it, though. - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Large constants in RISC
Author: Robert Finch
Date: Sun, 15 Nov 2026 17:48
Date: Sun, 15 Nov 2026 17:48
29 lines
2599 bytes
2599 bytes
On 2026-11-15 11:28 a.m., Anton Ertl wrote: > Robert Finch <robfi680@gmail.com> writes: >> One ends up with constants in the data cache. May be slower than having >> constants inline from the I$. One thought it to have instructions that >> can load constants using the I$. > > There is no data path from the I-cache to the load unit in existing > CPUs. I also expect that if you add such a data path, it will be > slower, because the I-cache is not close to the LSU, but close to the > decoder and the branch predictor. So that idea is not going to fly. > I was thinking of a dedicated port on the I$ not using the LS unit, like used for JTT instruction in My66000 IIRC. > The better approach is to have the data in the instructions, and > extract it during instruction decoding, like all the immediate > operands in all instruction sets around do. Or to have the data in > data memory and access it with a load that works with the D-cache, > like Alpha and others do. > I agree with that. >> ARM has shifted constant instruction too. > > Not in ARM A64. But ARM A64 has an interesting encoding for certain > bit patterns. > > - anton
Re: Large constants in RISC
Author: MitchAlsup
Date: Sun, 15 Nov 2026 18:50
Date: Sun, 15 Nov 2026 18:50
37 lines
2421 bytes
2421 bytes
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. > 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. > >
Re: Large constants in RISC
Author: MitchAlsup
Date: Sun, 15 Nov 2026 19:04
Date: Sun, 15 Nov 2026 19:04
46 lines
2855 bytes
2855 bytes
anton@mips.complang.tuwien.ac.at (Anton Ertl) posted: > BGB <cr88192@gmail.com> writes: > >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. > > The nice thing about the load is that its address is usually available > right away. So the load operation is typically performed as soon as > possible, and for a sequence > > load r1, d(rct) > add r3=r1+r2 > > where rct is the register pointing to the constant table, the result > may be ready in r1 before r2 is ready. > > The bottom line is that this load typically does not cost latency. > > Concerning cache misses: If the code is executed often, the constant > will be in the (L1) D-Cache. If there is a cache miss, the code has > not been executed recently. Bottom line: Cache misses are rare, > because caches work. > > >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. > > How many globals does your program have. > > In order to be able to address the globals of a program with a single > global pointer, Or efficient access to large displacements which frees up the global pointer, > the linker needs to do quite a bit of work. Maybe the > RISC-V linker does. For the Alpha every compilation unit has its own > constant and global table as far as I remember (and I think the same > register is used for both on the Alpha). > > - anton
Re: Large constants in RISC
Author: MitchAlsup
Date: Sun, 15 Nov 2026 19:08
Date: Sun, 15 Nov 2026 19:08
30 lines
2348 bytes
2348 bytes
anton@mips.complang.tuwien.ac.at (Anton Ertl) posted: > Robert Finch <robfi680@gmail.com> writes: > >One ends up with constants in the data cache. May be slower than having > >constants inline from the I$. One thought it to have instructions that > >can load constants using the I$. > > There is no data path from the I-cache to the load unit in existing > CPUs. I also expect that if you add such a data path, it will be > slower, because the I-cache is not close to the LSU, but close to the > decoder and the branch predictor. So that idea is not going to fly. You do NOT need said path, what you need is for the constant to show up from the instruction stream and be parse-able from that instruction stream easily. Given really small (1-wide) low ends, one might use a 4-word aligned fetch--higher end machines will need correspondingly wider Fetch and more prediction accuracy. > The better approach is to have the data in the instructions, and > extract it during instruction decoding, like all the immediate > operands in all instruction sets around do. Or to have the data in > data memory and access it with a load that works with the D-cache, > like Alpha and others do. > > >ARM has shifted constant instruction too. > > Not in ARM A64. But ARM A64 has an interesting encoding for certain > bit patterns. > > - anton
Re: Large constants in RISC
Author: EricP
Date: Sun, 15 Nov 2026 19:12
Date: Sun, 15 Nov 2026 19:12
76 lines
4107 bytes
4107 bytes
On 2026-Nov-15 11:08, Anton Ertl wrote: > BGB <cr88192@gmail.com> writes: >> 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. > > The nice thing about the load is that its address is usually available > right away. So the load operation is typically performed as soon as > possible, and for a sequence > > load r1, d(rct) > add r3=r1+r2 > > where rct is the register pointing to the constant table, the result > may be ready in r1 before r2 is ready. > > The bottom line is that this load typically does not cost latency. If I was using the constant table approach I would: - define for LD and ST instructions that if the base register was the zero register (R0 or R31) then it means use the 64-bit RIP. - add scaled index LD and ST instructions with the following format: opcode 6b Rs1_base 5b Rs2_index 5b Rd/Rs3 5b data_type 3b scale 3b offset 5b Data types are signed integer: B, H, W, L float: S, D This can access anywhere in 64b address space in 2 instructions: - use a small RIPrel offset to load the 64b RIPrel offset from the table = use the scaled index LD/ST with scale = 0, index = offset, and base RIP to address all of memory. For RIP-relative 64b addressing: LD.L r1 = [RIP + table_offset] LD.dt r2 = [RIP + r1<<0] For absolute 64b addressing, set the base register to the constant and the index register to the zero register (which equals 0 NOT the RIP): LD.L r1 = [RIP + table_offset] LD.dt r2 = [r1 + r0<<0] > Concerning cache misses: If the code is executed often, the constant > will be in the (L1) D-Cache. If there is a cache miss, the code has > not been executed recently. Bottom line: Cache misses are rare, > because caches work. > >> 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. > > How many globals does your program have. > > In order to be able to address the globals of a program with a single > global pointer, the linker needs to do quite a bit of work. Maybe the > RISC-V linker does. For the Alpha every compilation unit has its own > constant and global table as far as I remember (and I think the same > register is used for both on the Alpha). > > - anton Alpha put the constant table just above the entry to the routine running backwards and aligned the routine on a cache line. On entry it would do a BR rd,+0 to copy the PC into rd then they use that as a base to access the constant table. However neither Alpha or RV have Its 16b offset field should be enough
Re: Large constants in RISC
Author: EricP
Date: Sun, 15 Nov 2026 19:34
Date: Sun, 15 Nov 2026 19:34
99 lines
5055 bytes
5055 bytes
On 2026-Nov-15 14:12, EricP wrote: > On 2026-Nov-15 11:08, Anton Ertl wrote: >> BGB <cr88192@gmail.com> writes: >>> 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. >> >> The nice thing about the load is that its address is usually available >> right away. So the load operation is typically performed as soon as >> possible, and for a sequence >> >> load r1, d(rct) >> add r3=r1+r2 >> >> where rct is the register pointing to the constant table, the result >> may be ready in r1 before r2 is ready. >> >> The bottom line is that this load typically does not cost latency. > > If I was using the constant table approach I would: > > - define for LD and ST instructions that if the base register was > the zero register (R0 or R31) then it means use the 64-bit RIP. > > - add scaled index LD and ST instructions with the following format: > opcode 6b Rs1_base 5b Rs2_index 5b Rd/Rs3 5b data_type 3b scale 3b offset 5b > Data types are signed integer: B, H, W, L float: S, D > > This can access anywhere in 64b address space in 2 instructions: > - use a small RIPrel offset to load the 64b RIPrel offset from the table > = use the scaled index LD/ST with scale = 0, index = offset, and base RIP > to address all of memory. > > For RIP-relative 64b addressing: > > LD.L r1 = [RIP + table_offset] > LD.dt r2 = [RIP + r1<<0] > > For absolute 64b addressing, set the base register to the constant and > the index register to the zero register (which equals 0 NOT the RIP): > > LD.L r1 = [RIP + table_offset] > LD.dt r2 = [r1 + r0<<0] > >> Concerning cache misses: If the code is executed often, the constant >> will be in the (L1) D-Cache. If there is a cache miss, the code has >> not been executed recently. Bottom line: Cache misses are rare, >> because caches work. >> >>> 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. >> >> How many globals does your program have. >> >> In order to be able to address the globals of a program with a single >> global pointer, the linker needs to do quite a bit of work. Maybe the >> RISC-V linker does. For the Alpha every compilation unit has its own >> constant and global table as far as I remember (and I think the same >> register is used for both on the Alpha). >> >> - anton > > Alpha put the constant table just above the entry to the routine > running backwards and aligned the routine on a cache line. > On entry it would do a BR rd,+0 to copy the PC into rd > then they use that as a base to access the constant table. > > However neither Alpha or RV have Oops I hit send instead of save. However neither Alpha or RV have the scaled index address mode that saves having to manually ADD the offset to the RIP for each LD or ST. The method I suggest above LD.L r1 = [RIP + table_offset] LD.dt r2 = [RIP + r1<<0] has the drawback that the RIPrel offset to the same memory location will be different for each LD/ST instruction, requiring multiple constant loads. Instead one can use a Load Effective Address LEA instruction with a base of RIP to copy the RIP into a dest register, and have a single 64b offset for each variable relative to that fixed base. LEA r1 = [RIP] LD.L r2 = [r1 + table_offset] LD.dt r3 = [r1 + r2<<0] and reuse r1 base and r2 offset multiple times: ST.dt [r1 + r2<<0] = r3
Re: Large constants in RISC
Author: BGB
Date: Sun, 15 Nov 2026 20:47
Date: Sun, 15 Nov 2026 20:47
187 lines
8555 bytes
8555 bytes
On 11/15/2026 10:08 AM, Anton Ertl wrote:
> BGB <cr88192@gmail.com> writes:
>> 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.
>
> The nice thing about the load is that its address is usually available
> right away. So the load operation is typically performed as soon as
> possible, and for a sequence
>
> load r1, d(rct)
> add r3=r1+r2
>
> where rct is the register pointing to the constant table, the result
> may be ready in r1 before r2 is ready.
>
> The bottom line is that this load typically does not cost latency.
>
If loads have a 3 or 4 cycle latency and are placed directly before the
ALU op, on an in-order machine, one needs to pay for it...
Even on OoO machines (like x86), having a load directly followed by an
operation does often still have a visibly higher latency cost than if
they are spread out. However, spreading things out is harder on x86
because of the relative lack of registers.
The relative cost of loads does seem to be cheaper though, but depends
on the operation that follows.
Where:
Load -> ALU
Seemingly a lot cheaper than:
Load -> CMP -> Jcc
Generally better to have multiple branching paths pivoting off a single
load, than to have multiple loads driving each decision path.
This case does appear to be a place where I am seeing a difference
between my current PC's Xen+ CPU and a Core i7 though, as the "Load ->
CMP -> Jcc" path appears to be cheaper on the i7 vs Xen+; but with the
relative cost of the 'Jcc' in isolation being higher on the i7.
So, it is like the i7 has more expensive branching in-general, but
seemingly doesn't care as much where the input for the branch decision
came from.
Not fully investigated, this is just what I seem to be seeing from some
microbenchmarks.
This was observed mostly in some code that was tuned for my own ISA,
where my CPU design seems (in general) to have comparably more expensive
memory access, but comparably cheaper branching.
Where:
Basic memory load is 3 cycles, but also partly at the mercy of the
direct-mapped L1 D$;
Branches are typically 1-cycle hit / 10 cycle miss;
My branch-predictor design is reasonably effective, though I had
observed that the relative advantage of its core design would dilute
with larger context (*).
*: It uses the recent branch history and PC LOBs to index into a table
of 5/6-bit FSMs (6b in, 6b out, 5b of state) that can predict arbitrary
repeating patterns up to 4 bits.
Had noted that if the context was significantly larger, the relative
advantage versus saturating counters drops off, but in a small context,
the FSMs solidly beat out the saturating counters.
To admit, I am using FSM tables generated by genetic algorithms though,
as the GA generated tables tend to beat out my attempts at
hand-generated tables (but, can note also that fitting every possible 1
to 4-bit repeating pattern into a 5 bit state machine is a bit of a
mental puzzle).
And, in this case, the specialized application of random number
generators and selection heuristics seems to do a better job at it.
> Concerning cache misses: If the code is executed often, the constant
> will be in the (L1) D-Cache. If there is a cache miss, the code has
> not been executed recently. Bottom line: Cache misses are rare,
> because caches work.
>
However, the D$ puts it in conflict with whatever other data the program
is using, and D$ tends to be a much more volatile space than I$ IME.
So, if the constants are inline, they are almost always going to hit if
the loop is already warm or similar. For D$, one is more at the mercy of
whether some of the working data knocked it out of the cache.
Well, also this is partly why I am often going for 32K D$ + 16K I$:
The D$ tends to be a lot more chaotic and volatile, so needs to be bigger;
The I$ is a little less so, more just matters that the active loops fit
and (hopefully) there aren't too many address conflicts.
There may be differences here between a machine with direct-mapped vs
set-associative caches though.
But, in any case, generic memory loads for constants kinda sucks with a
direct-mapped L1 cache.
If a compiler could identify the programs' main hot path, could be
possible in theory for it to try to cluster the hot-path into a shared
block with hopefully non-overlapping cache ranges.
BGBCC isn't that clever though.
>> 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.
>
> How many globals does your program have.
>
Going and quickly adding a stat for this (for plain global variables):
TestKern kernel:
Globals: tot=351 (2145B) dat=351(2161B) bss=0(0B)
Doom:
Globals: tot=777 (4468B) dat=777(4524B) bss=0(0B)
Hexen:
Globals: tot=766 (4228B) dat=766(4260B) bss=0(0B)
ROTT:
Globals: tot=723 (3656B) dat=723(3736B) bss=0(0B)
SWQuake:
Globals: tot=791 (4936B) dat=792(5208B) bss=0(0B)
GLQuake:
Globals: tot=956 (6744B) dat=957(6920B) bss=0(0B)
Quake 3 Arena (main program):
Globals: tot=980 (7080B) dat=980(7088B) bss=0(0B)
...
This is excluding all arrays and similar, so only counting the basic
primitive-type globals.
But, as noted, basically all of these examples are larger than 2048 bytes.
Granted, a lot of my other programs would fall below the limit here, as
my own coding practices tend to rely more on structs than on global
variables.
They would be addressable with a 10-bit scaled displacement though.
In any case though, a Load/Store instruction with a hard-wired
global-pointer and 16-bit displacement can hit all of the global
variables though.
> In order to be able to address the globals of a program with a single
> global pointer, the linker needs to do quite a bit of work. Maybe the
> RISC-V linker does. For the Alpha every compilation unit has its own
> constant and global table as far as I remember (and I think the same
> register is used for both on the Alpha).
>
In BGBCC, it first sorts all the global variables into priority order
and then shuffles the order in an attempt to improve packing density
(say, if swapping the relative order of variables will reduce space
wasted for padding bytes).
Most basic globals tend to be a mix of 'int' and 'pointers'.
That said, can note that unlike GCC, BGBCC doesn't have a separate
compiler and linker (it instead handles separate compilation using a
stack-based IL, and then sorts everything out and compiles what things
are reachable).
> - anton
Re: Large constants in RISC
Author: 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.
>>
>>
Re: Large constants in RISC
Author: Thomas Koenig
Date: Sun, 15 Nov 2026 22:30
Date: Sun, 15 Nov 2026 22:30
20 lines
2114 bytes
2114 bytes
Anton Ertl <anton@mips.complang.tuwien.ac.at> schrieb: > And included in the "baroque" instruction set are instructions with > 32-bit immediate operands, and a mov instruction with a 64-bit > immediate operand. > > In any case, there is no empirical evidence "that having [large] > constants in instructions is faster." And the weak evidence that we > currently have, points in the other direction. So we have every > reason to doubt that claim. If you want data, you can look at a RISC architecture which has adopted 34-bit constants. They can save quite something on TOC for pc-relative code. -- This USENET posting was made without artificial intelligence, artificial impertinence, artificial arrogance, artificial stupidity, artificial flavorings or artificial colorants.
Re: Large constants in RISC
Author: Anton Ertl
Date: Mon, 16 Nov 2026 07:22
Date: Mon, 16 Nov 2026 07:22
22 lines
2166 bytes
2166 bytes
Thomas Koenig <tkoenig@netcologne.de> writes: >Anton Ertl <anton@mips.complang.tuwien.ac.at> schrieb: >> In any case, there is no empirical evidence "that having [large] >> constants in instructions is faster." And the weak evidence that we >> currently have, points in the other direction. So we have every >> reason to doubt that claim. > >If you want data, you can look at a RISC architecture which has >adopted 34-bit constants. Which one? And does it have faster implementations than ARM A64 which does not have such a feature last I looked. >They can save quite something on TOC >for pc-relative code. TOC? - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Large constants in RISC
Author: Anton Ertl
Date: Mon, 16 Nov 2026 07:24
Date: Mon, 16 Nov 2026 07:24
138 lines
7207 bytes
7207 bytes
BGB <cr88192@gmail.com> writes: >On 11/15/2026 10:08 AM, Anton Ertl wrote: >> The nice thing about the load is that its address is usually available >> right away. So the load operation is typically performed as soon as >> possible, and for a sequence >> >> load r1, d(rct) >> add r3=r1+r2 >> >> where rct is the register pointing to the constant table, the result >> may be ready in r1 before r2 is ready. >> >> The bottom line is that this load typically does not cost latency. >> > >If loads have a 3 or 4 cycle latency and are placed directly before the >ALU op, on an in-order machine, one needs to pay for it... Sure. That's why you use compiler instruction scheduling for in-order machines. The address is available from at least the start of the function, and the load is known not to produce an exception, so the load can be scheduled up to the start of the function if desired. This would cost a register until the last use of the constant, but would save loads if the constant is used several times. In any case, the compiler can schedule the constant load as is convenient, and the only cost is the opportunity cost of not being able to use the instruction slot and load unit for something else, and the cost of occupying the register from the load to the last use (another opportunity cost). >Even on OoO machines (like x86), having a load directly followed by an >operation does often still have a visibly higher latency cost than if >they are spread out. Often? How often? Evidence? In any case, for loading a constant, scheduling the load earlier can be done without problem, as discussed above. >Where: > Load -> ALU >Seemingly a lot cheaper than: > Load -> CMP -> Jcc If the branch is predicted correctly (the usual case), the cost of the latter is just that the instruction slots and functional units are occupied. And the difference from having the constant in the CMP instrction is just that the load unit is occupied, the other costs are the same. >This case does appear to be a place where I am seeing a difference >between my current PC's Xen+ CPU and a Core i7 though, as the "Load -> >CMP -> Jcc" path appears to be cheaper on the i7 vs Xen+; but with the >relative cost of the 'Jcc' in isolation being higher on the i7. Unless you have compared these sequences in a very large variety of settings and done a statistic over the results to come to this conclusion, it is likely that you are seeing some other effect, rather than anything specific to these instruction sequences. Given that you write "a Core i7", as if this marketing name had any technical meaning, I doubt that you have done such a well-founded empirical evaluation. The "Core i7" name has been given to about 11 different basic CPU designs, some of which contain cores with two different microarchitectures; and the same 11 CPU designs have also been given other names, such as "Core i5", "Core i3", "Xeon W", "Xeon E", "Pentium", or "Celeron"; not all names were used for all CPU designs, but in any case these marketing names don't tell us anything useful. By contrast, "Zen+" is a specific CPU design. >> Concerning cache misses: If the code is executed often, the constant >> will be in the (L1) D-Cache. If there is a cache miss, the code has >> not been executed recently. Bottom line: Cache misses are rare, >> because caches work. >> > >However, the D$ puts it in conflict with whatever other data the program >is using, and D$ tends to be a much more volatile space than I$ IME. > >So, if the constants are inline, they are almost always going to hit if >the loop is already warm or similar. For D$, one is more at the mercy of >whether some of the working data knocked it out of the cache. With a typical 8-way or 12-way set-associative D-cache with pseudo-LRU replacement, and the constant being accessed in a hot loop, it would need multiple accesses to the same set between the accesses to the constant in order to evict the constant. Either the constant is not used that often, or it will stay in the D-cache. Of course, there is still the cost that the other data does not have that cache line available for its purposes. Nothing is totally free. >But, in any case, generic memory loads for constants kinda sucks with a >direct-mapped L1 cache. Direct-mapped caches suck. No qualification needed. >>> 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. >> >> How many globals does your program have. >> > >Going and quickly adding a stat for this (for plain global variables): > TestKern kernel: > Globals: tot=351 (2145B) dat=351(2161B) bss=0(0B) > Doom: > Globals: tot=777 (4468B) dat=777(4524B) bss=0(0B) > Hexen: > Globals: tot=766 (4228B) dat=766(4260B) bss=0(0B) > ROTT: > Globals: tot=723 (3656B) dat=723(3736B) bss=0(0B) > SWQuake: > Globals: tot=791 (4936B) dat=792(5208B) bss=0(0B) > GLQuake: > Globals: tot=956 (6744B) dat=957(6920B) bss=0(0B) > Quake 3 Arena (main program): > Globals: tot=980 (7080B) dat=980(7088B) bss=0(0B) > ... > >This is excluding all arrays and similar, so only counting the basic >primitive-type globals. > > >But, as noted, basically all of these examples are larger than 2048 bytes. Two fit in 4096 bytes (accessible with a 12-bit offset), and the rest fits in 8192 bytes (accessible with a 13-bit offset). - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Large constants in RISC
Author: Anton Ertl
Date: Mon, 16 Nov 2026 08:04
Date: Mon, 16 Nov 2026 08:04
29 lines
2370 bytes
2370 bytes
BGB <cr88192@gmail.com> writes: >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. What makes you think that everybody expects accesses to global variables to be fast? Certainly my take has been for decades that anything that's not in registers is slow. And that global variables are not in registers, in general. - anton -- 'Anyone trying for "industrial quality" ISA should avoid undefined behavior.' Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Re: Large constants in RISC
Author: Thomas Koenig
Date: Mon, 16 Nov 2026 09:05
Date: Mon, 16 Nov 2026 09:05
32 lines
2608 bytes
2608 bytes
Anton Ertl <anton@mips.complang.tuwien.ac.at> schrieb: > Thomas Koenig <tkoenig@netcologne.de> writes: >>Anton Ertl <anton@mips.complang.tuwien.ac.at> schrieb: >>> In any case, there is no empirical evidence "that having [large] >>> constants in instructions is faster." And the weak evidence that we >>> currently have, points in the other direction. So we have every >>> reason to doubt that claim. >> >>If you want data, you can look at a RISC architecture which has >>adopted 34-bit constants. > > Which one? And does it have faster implementations than ARM A64 which > does not have such a feature last I looked. Power ISA v3.1 (aka Power 10). > >>They can save quite something on TOC >>for pc-relative code. > > TOC? Table of contents, for accessing global data. Their ABI has a special register for that, r2, which is no longer needed for pc-relative code (also introduced with v3.1). Plus, size restrictions were lifted. Interestingly enough, they (partially) followed Mitch on that. -- This USENET posting was made without artificial intelligence, artificial impertinence, artificial arrogance, artificial stupidity, artificial flavorings or artificial colorants.
Re: Large constants in RISC
Author: Anton Ertl
Date: Mon, 16 Nov 2026 09:41
Date: Mon, 16 Nov 2026 09:41
169 lines
7791 bytes
7791 bytes
Thomas Koenig <tkoenig@netcologne.de> writes:
>Anton Ertl <anton@mips.complang.tuwien.ac.at> schrieb:
>> Thomas Koenig <tkoenig@netcologne.de> writes:
>>>If you want data, you can look at a RISC architecture which has
>>>adopted 34-bit constants.
>>
>> Which one? And does it have faster implementations than ARM A64 which
>> does not have such a feature last I looked.
>
>Power ISA v3.1 (aka Power 10).
The data I have about that is that code on it runs slower than on the
fastest ARM A64 implementations, at least for single-threaded loads.
The per-core performance with all 8 threads exceeds that of AMD and
Intel CPUs, and probably also that of the fastest ARM A64
implementations, but I would need access to those ARM A64 CPUs to
confirm this.
>Table of contents, for accessing global data. Their ABI has
>a special register for that, r2, which is no longer needed
>for pc-relative code (also introduced with v3.1). Plus, size
>restrictions were lifted.
I looked at functions with references to global variables, large
constants, and separately linked functions in
<2025Jul1.180809@mips.complang.tuwien.ac.at>.
anton@cfarm121:~/bigconstants$ cat main.c
#include <math.h>
extern int a, b;
extern double x, y;
extern void foo(void);
int main()
{
foo();
x = lgamma(y);
a += signgam;
a += b;
return 0;
}
anton@cfarm121:~/bigconstants$ cat foo.c
int a, b;
double x, y;
void foo(void)
{
}
anton@cfarm121:~/bigconstants$ gcc -Wall -O -FPIC main.c foo.c -lm
anton@cfarm121:~/bigconstants$ objdump -d a.out
...
00000000000009f4 <.main>:
9f4: 7c 08 02 a6 mflr r0
9f8: f8 01 00 10 std r0,16(r1)
9fc: f8 21 ff 91 stdu r1,-112(r1)
a00: 48 00 00 79 bl a78 <.foo>
a04: 60 00 00 00 nop
a08: 60 00 00 00 nop
a0c: 39 22 81 90 addi r9,r2,-32368
a10: c8 29 00 00 lfd f1,0(r9)
a14: 4b ff fe 0d bl 820 <0000001a.plt_call.lgamma@@GLIBC_2.23>
a18: e8 41 00 28 ld r2,40(r1)
a1c: 60 00 00 00 nop
a20: 39 22 81 98 addi r9,r2,-32360
a24: d8 29 00 00 stfd f1,0(r9)
a28: 60 00 00 00 nop
a2c: 39 42 81 a4 addi r10,r2,-32348
a30: 81 2a 00 00 lwz r9,0(r10)
a34: 60 00 00 00 nop
a38: e9 02 80 28 ld r8,-32728(r2)
a3c: 81 08 00 00 lwz r8,0(r8)
a40: 7d 29 42 14 add r9,r9,r8
a44: 60 00 00 00 nop
a48: 39 02 81 a0 addi r8,r2,-32352
a4c: 81 08 00 00 lwz r8,0(r8)
a50: 7d 29 42 14 add r9,r9,r8
a54: 91 2a 00 00 stw r9,0(r10)
a58: 38 60 00 00 li r3,0
a5c: 38 21 00 70 addi r1,r1,112
a60: e8 01 00 10 ld r0,16(r1)
a64: 7c 08 03 a6 mtlr r0
a68: 4e 80 00 20 blr
a6c: 00 00 00 00 .long 0x0
a70: 00 00 00 01 .long 0x1
a74: 80 00 00 00 lwz r0,0(0)
0000000000000a78 <.foo>:
a78: 4e 80 00 20 blr
anton@cfarm121:~/bigconstants$ gcc -Wall -O -FPIC -mcpu=power10 main.c foo.c -lm
anton@cfarm121:~/bigconstants$ objdump -d a.out
0000000000000a40 <.main>:
a40: 7c 08 02 a6 mflr r0
a44: f8 01 00 10 std r0,16(r1)
a48: f8 21 ff 91 stdu r1,-112(r1)
a4c: 48 00 00 79 bl ac4 <.foo>
a50: 60 00 00 00 nop
a54: 06 10 00 01 pla r9,128572 # 20090 <y>
a58: 39 20 f6 3c
a5c: c8 29 00 00 lfd f1,0(r9)
a60: 4b ff fd e1 bl 840 <0000001a.plt_call.lgamma@@GLIBC_2.23>
a64: e8 41 00 28 ld r2,40(r1)
a68: 06 10 00 01 pla r9,128560 # 20098 <x>
a6c: 39 20 f6 30
a70: d8 29 00 00 stfd f1,0(r9)
a74: 06 10 00 01 pla r9,128560 # 200a4 <a>
a78: 39 20 f6 30
a7c: 80 c9 00 00 lwz r6,0(r9)
a80: 04 10 00 01 pld r10,128168 # 1ff28 [signgam@got]
a84: e5 40 f4 a8
a88: 80 ea 00 00 lwz r7,0(r10)
a8c: 06 10 00 01 pla r10,128532 # 200a0 <b>
a90: 39 40 f6 14
a94: 81 0a 00 00 lwz r8,0(r10)
a98: 7d 47 32 14 add r10,r7,r6
a9c: 7d 4a 42 14 add r10,r10,r8
aa0: 91 49 00 00 stw r10,0(r9)
aa4: 38 60 00 00 li r3,0
aa8: 38 21 00 70 addi r1,r1,112
aac: e8 01 00 10 ld r0,16(r1)
ab0: 7c 08 03 a6 mtlr r0
ab4: 4e 80 00 20 blr
ab8: 00 00 00 00 .long 0x0
abc: 00 00 00 01 .long 0x1
ac0: 80 00 00 00 lwz r0,0(0)
0000000000000ac4 <.foo>:
ac4: 4e 80 00 20 blr
In this case the difference does not impress me. But of course this
is not a case that would benefit from the availability of r2 for other
purposes.
For comparison, here's what I get on ARM A64:
0000000000000864 <main>:
864: a9bf7bfd stp x29, x30, [sp, #-16]!
868: 910003fd mov x29, sp
86c: 94000017 bl 8c8 <foo>
870: 90000080 adrp x0, 10000 <__FRAME_END__+0xf550>
874: f947e400 ldr x0, [x0, #4040]
878: fd400000 ldr d0, [x0]
87c: 97ffffad bl 730 <lgamma@plt>
880: 90000080 adrp x0, 10000 <__FRAME_END__+0xf550>
884: f947d400 ldr x0, [x0, #4008]
888: fd000000 str d0, [x0]
88c: 90000081 adrp x1, 10000 <__FRAME_END__+0xf550>
890: f947e021 ldr x1, [x1, #4032]
894: b9400020 ldr w0, [x1]
898: 90000082 adrp x2, 10000 <__FRAME_END__+0xf550>
89c: f947ec42 ldr x2, [x2, #4056]
8a0: b9400042 ldr w2, [x2]
8a4: 0b020000 add w0, w0, w2
8a8: 90000082 adrp x2, 10000 <__FRAME_END__+0xf550>
8ac: f947d042 ldr x2, [x2, #4000]
8b0: b9400042 ldr w2, [x2]
8b4: 0b020000 add w0, w0, w2
8b8: b9000020 str w0, [x1]
8bc: 52800000 mov w0, #0x0 // #0
8c0: a8c17bfd ldp x29, x30, [sp], #16
8c4: d65f03c0 ret
00000000000008c8 <foo>:
8c8: d65f03c0 ret
8cc: d503201f nop
- anton
--
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Page 1 of 2 • 100 total messages
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads