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.
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.
Waldek Hebisch wrote:
Normal RISC architecture with 32 bit instructions has problemOuch!
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.
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.
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).
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.
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.
Waldek Hebisch wrote:
Normal RISC architecture with 32 bit instructions has problemOuch!
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.-a 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.-a For me traditional
way has two problems.-a One is excessive number of instructions
needed to build larger constants.-a 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.-a But
for 64 bit constants one possible seqence involeves 6
instructions.-a This trouble is easily fixable with more
(better) constant building instructions.-a The second
problem is need for free register in which constant is
build.-a 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.-a Consequently,
a constant needs write port to reqister file.-a 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.-a I also propose
special flag signaling that this extra register is live.-a It
is important that this flag gets cleared after the C register
is consumed (used).-a It also gets cleared if after setup of C
the next instruction does not use it.-a I propose to have 2 or
3 constant-building instructions.-a 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).-a 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.-a Use of constant-building
instructions also marks C as live.-a So in case of 2 constant-
building instructions the 2 instructions set high 52 bits
of C and mark it as live.-a When C is live it is combined
(OR-ed) with constant in the following constant-using
instruction.-a Assuming that normal instructions can provide
12 bits, this allows 3 instruction sequence equivelent to
a single instruction with 64 bit constant argument.-a One
can object that instruction providing 26 bits leaves only
6 bits for opcode and we need 2 such opcodes.-a This may be
deemed as using too much encoding space for benefits of
64-bit constants.-a That is why I propose the 3 instruction
variant.-a With 18 bits for constant there is 14 bits for
opcode.-a 3 such instructions consume rather small part of
instruction space.-a The cost is that now to get 64 bit
constant we need 4 instruction sequence.-a Depending on
frequency of 64 bit constants this variant may be more
satisfactory.
What we gain using such approach?-a 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.-a 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.-a 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.-a 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.
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
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.-a 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.-a For me traditional
way has two problems.-a One is excessive number of instructions
needed to build larger constants.-a 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.-a But
for 64 bit constants one possible seqence involeves 6
instructions.-a This trouble is easily fixable with more
(better) constant building instructions.-a The second
problem is need for free register in which constant is
build.-a 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.-a Consequently,
a constant needs write port to reqister file.-a 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.-a I also propose
special flag signaling that this extra register is live.-a It
is important that this flag gets cleared after the C register
is consumed (used).-a It also gets cleared if after setup of C
the next instruction does not use it.-a I propose to have 2 or
3 constant-building instructions.-a 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).-a 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.-a Use of constant-building
instructions also marks C as live.-a So in case of 2 constant-
building instructions the 2 instructions set high 52 bits
of C and mark it as live.-a When C is live it is combined
(OR-ed) with constant in the following constant-using
instruction.-a Assuming that normal instructions can provide
12 bits, this allows 3 instruction sequence equivelent to
a single instruction with 64 bit constant argument.-a One
can object that instruction providing 26 bits leaves only
6 bits for opcode and we need 2 such opcodes.-a This may be
deemed as using too much encoding space for benefits of
64-bit constants.-a That is why I propose the 3 instruction
variant.-a With 18 bits for constant there is 14 bits for
opcode.-a 3 such instructions consume rather small part of
instruction space.-a The cost is that now to get 64 bit
constant we need 4 instruction sequence.-a Depending on
frequency of 64 bit constants this variant may be more
satisfactory.
What we gain using such approach?-a 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.-a 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.-a 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.-a 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.
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.-a 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.-a For me traditional
way has two problems.-a One is excessive number of instructions
needed to build larger constants.-a 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.-a But
for 64 bit constants one possible seqence involeves 6
instructions.-a This trouble is easily fixable with more
(better) constant building instructions.-a The second
problem is need for free register in which constant is
build.-a 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.-a Consequently,
a constant needs write port to reqister file.-a 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.-a I also propose
special flag signaling that this extra register is live.-a It
is important that this flag gets cleared after the C register
is consumed (used).-a It also gets cleared if after setup of C
the next instruction does not use it.-a I propose to have 2 or
3 constant-building instructions.-a 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).-a 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.-a Use of constant-building
instructions also marks C as live.-a So in case of 2 constant-
building instructions the 2 instructions set high 52 bits
of C and mark it as live.-a When C is live it is combined
(OR-ed) with constant in the following constant-using
instruction.-a Assuming that normal instructions can provide
12 bits, this allows 3 instruction sequence equivelent to
a single instruction with 64 bit constant argument.-a One
can object that instruction providing 26 bits leaves only
6 bits for opcode and we need 2 such opcodes.-a This may be
deemed as using too much encoding space for benefits of
64-bit constants.-a That is why I propose the 3 instruction
variant.-a With 18 bits for constant there is 14 bits for
opcode.-a 3 such instructions consume rather small part of
instruction space.-a The cost is that now to get 64 bit
constant we need 4 instruction sequence.-a Depending on
frequency of 64 bit constants this variant may be more
satisfactory.
What we gain using such approach?-a 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.-a 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.-a 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.-a 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:
-a Ingress Time: Determine lengths and fetch instruction control-bits;
-a Fetch Time: Check for register aliases and similar.
-a-a-a 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
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.-a 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.-a For me traditional
way has two problems.-a One is excessive number of instructions
needed to build larger constants.-a 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.-a But
for 64 bit constants one possible seqence involeves 6
instructions.-a This trouble is easily fixable with more
(better) constant building instructions.-a The second
problem is need for free register in which constant is
build.-a 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.-a Consequently,
a constant needs write port to reqister file.-a 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.-a I also propose
special flag signaling that this extra register is live.-a It
is important that this flag gets cleared after the C register
is consumed (used).-a It also gets cleared if after setup of C
the next instruction does not use it.-a I propose to have 2 or
3 constant-building instructions.-a 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).-a 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.-a Use of constant-building
instructions also marks C as live.-a So in case of 2 constant-
building instructions the 2 instructions set high 52 bits
of C and mark it as live.-a When C is live it is combined
(OR-ed) with constant in the following constant-using
instruction.-a Assuming that normal instructions can provide
12 bits, this allows 3 instruction sequence equivelent to
a single instruction with 64 bit constant argument.-a One
can object that instruction providing 26 bits leaves only
6 bits for opcode and we need 2 such opcodes.-a This may be
deemed as using too much encoding space for benefits of
64-bit constants.-a That is why I propose the 3 instruction
variant.-a With 18 bits for constant there is 14 bits for
opcode.-a 3 such instructions consume rather small part of
instruction space.-a The cost is that now to get 64 bit
constant we need 4 instruction sequence.-a Depending on
frequency of 64 bit constants this variant may be more
satisfactory.
What we gain using such approach?-a 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.-a 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.-a 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.-a 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.
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.
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 atQupls uses fixed length instructions. Dealing with variable length instructions is real nasty at least for superscalar with several front-
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:
-a-a Ingress Time: Determine lengths and fetch instruction control-bits;
-a-a Fetch Time: Check for register aliases and similar.
-a-a-a-a Combines with control bits to determine final fetch length.
...
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.
Terje Mathisen <terje.mathisen@tmsw.no> wrote:
Waldek Hebisch wrote:
Normal RISC architecture with 32 bit instructions has problemOuch!
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.
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.
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.
Waldek Hebisch wrote:
Terje Mathisen <terje.mathisen@tmsw.no> wrote:
Waldek Hebisch wrote:
Normal RISC architecture with 32 bit instructions has problemOuch!
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.
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.
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.
Waldek Hebisch wrote:----------------------------
Terje Mathisen <terje.mathisen@tmsw.no> wrote:
Waldek Hebisch wrote:
--- Synchronet 3.22a-Linux NewsLink 1.2If 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 <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 problemOuch!
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.
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 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 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.
Terje--- Synchronet 3.22a-Linux NewsLink 1.2
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.
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
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
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. ...
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.
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.
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.
On 2026-11-14 3:46 p.m., John Levine wrote:
It appears that BGB-a <cr88192@gmail.com> said:
I read what he wrote and I do not doubt that code having constants
in instructions is faster.-a What is not clear how frequently
code with explicit constants appears in real life.
It depends, for 3RI forms IME:
-a-a ~ 93% .. 96% fit in 10 bits;
-a-a ~ 95% .. 98% fit into 12 bits.
Of those that miss, there is a clustering patters:
-a-a Big spike near 16 bits;
-a-a Another smaller spike near 32 bits;
-a-a 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.
PowerPC uses shifted immediate instructions. IIRC there were four of them. ARM
FYI, that's basically what S/360 did.-a The load address instruction
has a 12 bit immediate field-a you can use to load 12 bit constants,
other than that you're on your own.-a 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.
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.
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.
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.
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.
On 11/14/26 6:38 PM, Robert Finch wrote:
On 2026-11-14 3:46 p.m., John Levine wrote:No. No. No. In MCore the constants were loaded from an offset in the instruction stream.-a 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.
It appears that BGB-a <cr88192@gmail.com> said:
I read what he wrote and I do not doubt that code having constants
in instructions is faster.-a What is not clear how frequently
code with explicit constants appears in real life.
It depends, for 3RI forms IME:
-a-a ~ 93% .. 96% fit in 10 bits;
-a-a ~ 95% .. 98% fit into 12 bits.
Of those that miss, there is a clustering patters:
-a-a Big spike near 16 bits;
-a-a Another smaller spike near 32 bits;
-a-a 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$.
Not exactly free, but often so.-a 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.
PowerPC uses shifted immediate instructions. IIRC there were four of
FYI, that's basically what S/360 did.-a The load address instruction
has a 12 bit immediate field-a you can use to load 12 bit constants,
other than that you're on your own.-a 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.
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.
What I wrote was specifically
in context of floating point constants.
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 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.
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.
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.
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$.
ARM has shifted constant instruction too.
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.
When power and heat are the
limiting factors, that should matter.
"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.
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
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.
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--- Synchronet 3.22a-Linux NewsLink 1.2
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
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--- Synchronet 3.22a-Linux NewsLink 1.2
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
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
On 2026-Nov-15 11:08, Anton Ertl wrote:Oops I hit send instead of save.
BGB <cr88192@gmail.com> writes:
However, if counting clock-cycles, a prefix is a win:
-a-a Prefix + Op: Zero cycle overhead for the missed constant;
-a-a 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.-a 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
-a 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:
-a opcode 6b Rs1_base 5b Rs2_index 5b Rd/Rs3 5b data_type 3b scale 3b offset 5b
-a Data types are signed integer: B, H, W, L-a 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
-a to address all of memory.
For RIP-relative 64b addressing:
-a LD.L-a r1 = [RIP + table_offset]
-a LD.dt r2 = [RIP + r1<<0]
For absolute 64b-a addressing, set the base register to the constant and
the index register to the zero register (which equals 0 NOT the RIP):
-a LD.L-a r1 = [RIP + table_offset]
-a LD.dt r2 = [r1 + r0<<0]
Concerning cache misses: If the code is executed often, the constant
will be in the (L1) D-Cache.-a If there is a cache miss, the code has
not been executed recently.-a 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.-a Maybe the
RISC-V linker does.-a 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-a 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
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
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.
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.
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.
They can save quite something on TOC
for pc-relative code.
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...
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.
Where:
Load -> ALU
Seemingly a lot cheaper than:
Load -> CMP -> Jcc
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.
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.
But, in any case, generic memory loads for constants kinda sucks with a >direct-mapped L1 cache.
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.
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.
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 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).
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.
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
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
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 76 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 54:48:58 |
| Calls: | 1,265 |
| Calls today: | 1 |
| Files: | 1,362 |
| D/L today: |
3 files (2,684K bytes) |
| Messages: | 285,582 |