🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #467

Re: interrupt handling latency and weird register use (was Re: OT: Epic RISC-V rant)

#467
From: BGB
Date: Fri, 30 Oct 2026 19:01
231 lines
10371 bytes
On 10/29/2026 7:30 PM, MitchAlsup wrote:
> Josh Vanderhoof <x@y.z> posted:
> 
>> MitchAlsup <user5857@newsgrouper.org.invalid> writes:
>>
>>>>> Note to µArchitects, put the instruction in even if you are
>>>>> going to TRAP to SW emulation. Later on when DIV is
>>>>> implemented it just runs faster and you had to do nothing
>>>>> (but put it in).
>>>>   You need to make it fast enough so that compilers are not
>>>> tempted to call software emulation.  Also beware of interrput
>>>> latency,
>>>
>>> I am shooting at 10 cycles there and 10 cycles back.
>>
>> Would it be possible to have a hardware version of polyfill?  The
>> basic idea would be that instead of emulation via trapping, the
>> software calls a standardized divide routine.  On hardware with a
>> divide instruction it checks the routine and if it sees it's the
>> standard divide routine it just uses its divide instruction
>> instead.  If it doesn't have hardware division it just runs the
>> divide routine.
> 
> I do not see any justification of NOT having DIV as an instruction
> and in HW (by means of some king of sequencer). In the same way
> we can put 16 GBOoO cores on a single die with 256-bit wide SIMD,
> the amount of area saved not doing DIV is invisible when seen at
> the die-level.
> 
> In the smallest conceivable implementations, DIV is a multiplexer in
> the feedback-loop around the integer ADDer and a 5-state sequencer.
> 
> For me, I can't see building a core that does not have a 64×64
> multiplier, either.
>   


I did such a thing in an FPGA (for 64-bit MUL/DIV):
   Shift 1-bit left;
   Do an ADD/SUB;
   Maybe use carry status as a comparison;
   ~ 36 or 68 clock cycles for 32 or 64 bit DIV;
   ...
And it still managed to burn through around 1000 LUTs.

Granted, not particularly large if compared with some things (the L1 D$ 
is far larger, etc), but still not exactly negligible either.
   Well:
     L1 D$, I$, regfile/forwarding, and instruction decoder
   Being the "big eaters".
     Followed by, say:
       Main FPU
       SIMD Unit (Binary16/Binary32)
       Int64 MUL/DIV
       Integer ALUs (3 of them);
       ...
     Shift was more than plain ALU,
       but limited to the first 2 lanes.
       Third lane only does plain ALU ops.


There was an investigated option to allow speculatively running the 
32-bit DIV through the 32-bit multiplier by using a small lookup table 
for reciprocals, and using this result (skipping the main divider) if it 
hit (32-bit value being divided by a divisor in the range of 1 to 63 or 
similar).

This was actually effective, but the relative impact of making 
common-case divides faster was small enough to be negligible for overall 
performance; for a non-zero cost.


Still probably worth having a DIV instruction in any "application class" 
core though.
Where, if a core is intended for general purpose application software, 
probably should have a divide instruction. If intended for embedded 
systems or microcontrollers (where typically cost optimization is the 
dominant concern and code is typically compiled for a specific CPU 
model), maybe a good idea to leave it out.

I originally was more opposed to having instructions for things which 
couldn't be guaranteed to be affordably viable or cost effective in 
hardware; but have since softened more to the idea of using 
trap-and-emulate in these cases.

There is an option of trap and emulate, or hot-patch, but does require 
that the kernel has some space within JAL/BSR distance to patch into.


Can note that a lot of the mainline/dominant cores (in the ARM space) 
are in-order dual-issue.


This is arguably cheaper than the 3-wide approach I had used:
   Lane 1: ALU, Shift, BRA, CMP, MEM, FPU, ...
   Lane 2: ALU, Shift, CMP (3R), FPU-ALT, ...
   Lane 3: ALU

The main merit of 3 wide here is that:
   A 3-input instruction can co-execute with another instruction;
   Can dual issue 3-input ops;
   Can support 128-bit 3-input ops;
   ...

Whereas a strictly dual-lane core couldn't do any 128-bit 3-input ops, 
various edge-case instructions, ... And couldn't dual-issue instructions 
whenever doing something like an indexed store.

That said, 4R2W is still cheaper than 6R3W.
And, 6R2W dual-issue doesn't save enough to be worthwhile, vs having a 
3rd ALU; even if the 3rd lane ALU is relatively low traffic (but does 
occasionally pay off in dense integer-math heavy code).

In this case, the ALU does:
   ADD/SUB/AND/OR/XOR (in both 3R and 3RI variants).
   Also MOV(2R/2RI) / MV / LI, ...


But, despite the ARM cores being dual-issue, they tend to have NEON, 
which looks like a more expensive approach to SIMD than what I had been 
using (needs its own register file, etc).

Whereas my core had been using the GPR regfile for everything, and just 
pretending to have a GPR and FPR regfile for sake of the RISC-V mode.

Not seen a particularly strong case for having a split between GPRs and 
FPRs other than "FPU ops are higher latency than ALU ops, so need their 
own regfile and pipeline for 'reasons'...".


Though, in my case, that I went for having typically higher latency for 
ALU ops:
   The MV / LI cases are 1-cycle;
   Other ALU / Shift / etc: 2 cycle.
   Load: 3 or 4 cycles.
     3 cycles: Basic memory load;
     4 cycles: Load with a converter step
       Eg: Binary32->Binary64 or texture loads.

A lot of the other RV cores it seems had aimed for:
   1 cycle: All basic instructions;
   2 cycle: Memory Load;
   ...

Did experiment with some fast-path cases for ALU and Load, but had noted 
that while they helped the basic/pure RV64G base, they didn't help 
notably with other cases, and most of the difference for RV64G was tied 
fairly specifically to the need for multi-op sequences for index 
calculations. If you have LEA/SHnADD, and indexed-load, the relative 
performance gain of 1-cycle latency on Shift and ADD instructions and 
similar mostly goes away; except maybe when spinning in tight loops 
(which is seemingly a poor case on my CPU design).

Well, say "while(*ct++==*cs++);" is something preferably avoided.


Seemingly ran into a bit of an issue in this space:
Can't really move to higher clock speeds without sacrifices that hurt 
more than they gain;
Can't really do that much more per-cycle without needing to drop lower 
in terms of clock speed (which also hurts);
...

Like, it seems like I had found a local optimum in the current 50 MHz 
target range (at least for the current Artix-7 chips).


Like, can make a core that runs at 75 MHz on an FPGA, but the only thing 
it can really gain is "slightly faster at Doom, slower on everything else".

Or, maybe go for RV32IM at 100 MHz, which runs Doom and similar well, 
but is pretty limited for other tasks.


More recently, stuff has gotten a lot slower as this search space seems 
to be pretty much explored; and practical usefulness is more debatable 
(vs, say, the cheaper option of maybe using an ARM SBC maybe paired with 
a separate 8/16 microcontroller chip for any RT stuff).


And, all I really achieved with my own CPU design was seemingly just 
using it for running ports of games and similar, rather than for 
anything "actually useful".

Well, and even then, "Make framerates in Quake not suck" still being a 
difficult problem.


>> I think I'd be more likely to use new instructions if they worked
>> this way, providing the standard polyfill routine in the
>> documentation for the new instruction.  The way things work now I
>> only use them after they've been around for a decade.
> 
> I have no idea what you mean by polyfill.


My understanding of it is, say:
You call a function for something as the default path (possibly with an 
indirect call via a function pointer);
If there is a faster version, it patches over to that after detecting 
that it exists;
If an instruction exists, one could maybe go further and patch out the 
original function call itself.

Say:
   JAL X1, __divsi   //Default case
Then, say:
   __divsi:
     LD  X5, __divsi_fptr  //decomposes into AUIPC+LD or similar
     JALR X0, X5, 0

   .section .data
   __divsi_fptr .quad __divsi_initial

Where __divsi_initial checks if a faster divide exists and if so 
replaces itself with that, or replaces the call with a call to the 
generic version.

Say:
   __divsi_fast:
     DIVW X10, X10, X11   //if we have the DIVW instruction...
     RTS

   __divsi_slow:
     ... //ye olde shift-subtract loop


Or, basically the inverse of the "have instruction, but trap and 
emulate" case.

Technical Details
Message-ID: <11c2phu$gqfv$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <87qzk0nejz.fsf@nightsong.com> <116d89d$27baf$1@paganini.bofh.team> <87ecfcagiw.fsf_-_@debian> <2026Sep1.225146@mips.complang.tuwien.ac.at> <11bpfm8$190ap$1@dont-email.me> <2026Oct27.171414@mips.complang.tuwien.ac.at> <1793120789-5857@newsgrouper.org> <11bsq36$36q1t$3@paganini.bofh.team> <1793202439-5857@newsgrouper.org> <ygnwlr084yt.fsf@y.z> <1793320218-5857@newsgrouper.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: BGB <cr88192@gmail.com>
Newsgroups: comp.arch
Subject: Re: interrupt handling latency and weird register use (was Re: OT:
 Epic RISC-V rant)
Date: Fri, 30 Oct 2026 14:01:18 -0500
Organization: A noiseless patient Spider
Lines: 231
Message-ID: <11c2phu$gqfv$1@dont-email.me>
References: <87qzk0nejz.fsf@nightsong.com>
 <116d89d$27baf$1@paganini.bofh.team> <87ecfcagiw.fsf_-_@debian>
 <2026Sep1.225146@mips.complang.tuwien.ac.at> <11bpfm8$190ap$1@dont-email.me>
 <2026Oct27.171414@mips.complang.tuwien.ac.at>
 <1793120789-5857@newsgrouper.org> <11bsq36$36q1t$3@paganini.bofh.team>
 <1793202439-5857@newsgrouper.org> <ygnwlr084yt.fsf@y.z>
 <1793320218-5857@newsgrouper.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 30 Oct 2026 19:01:19 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="551423"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/+Uy9uOlkGoybzpOmKQGwxUjz0DeK43GQ="; posting-host="7b3cdb7fd5e5bc88c376673fa0e309f3"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:cObzU8r6v4eta1BD7tcX38UpZFE= sha256:5+9HyejB9ViMJ9L7VWy/Dj1GwBqVcpEiqWWmgrQSLO8=
	sha1:39F0CA/toCB7XjQPp/zxmwRNqE8= sha256:n0J3DPl5Bl5f21ueqaET8HIUeitMSoHNBZY/DvLqeqE=
Content-Language: en-US
In-Reply-To: <1793320218-5857@newsgrouper.org>