Article View: comp.arch
Article #392Re: interrupt handling latency and weird register use (was Re: OT: Epic RISC-V rant)
From: BGB
Date: Tue, 27 Oct 2026 18:56
Date: Tue, 27 Oct 2026 18:56
151 lines
7167 bytes
7167 bytes
On 10/27/2026 11:14 AM, Anton Ertl wrote:
> makendo <makendo@makendo.invalid> writes:
>> The case where division is implemented but
>> multiplication is not does not have a name assigned -- it is pretty
>> well known that hardware designers, compiler writers and low-level
>> programmers hate division far more than multiplication, so I doubt
>> if divison-only hardware is even out in the wild for any ISA...
>
> I don't think that "hate" is the right concept. Division is costly:
> Costly to implement and it tends to run slowly (although ARM tries
> hard to disprove that part, even on low-end cores like the
> Cortex-A53). And it's not that frequent, which means that if you
> throw lots of hardware at making it fast, the payoff will be small.
> And because it is slow, various potential use cases avoid it and
> prefer to use shifts or multiplication by the inverse, making it even
> less frequent.
>
Yes.
General purpose division is infrequent enough that even slow hardware
support is debatable, vs implementing it in software as a runtime function.
And, since generally everyone knows division is often slow, there is a
tendency for programmers to avoid it whenever possible, which ironically
lessens the incentive for faster division in the general case.
Then, there are niche cases where one wants it to be fast.
But, then often in these cases a usable strategy is to use a lookup
table of reciprocals, which is often also a usable strategy to speed up
runtime-library division.
In the naive case, one can have a table of reciprocals and only divide
via the table when in range of the lookup table. However, this scales
poorly. With care, there are tricks to push it further with a smaller
lookup table, but more care is needed to avoid cases that would result
in inexact results (in cases where it is necessary to have an exact result).
One trick is, say:
You have a table of root reciprocals;
You have a table of derived reciprocals, holding an index to the root
reciprocal, and a right shift value (say, both packed into a 16-bit number);
This can be used as the main division table.
If outside of the naive range of the table:
Is the current divisor a power-of-2 multiple of an index in the table?
reuse that index, but add the scale to the final right-shift.
Maybe try some factorization tricks.
Else, fall back to a generic-but-slow strategy.
Such as a shift-and-subtract loop.
For something like a 3D renderer, typically exact results are not needed
so it is possible to behave as-if the power-of-2 case is always true.
Another option is doing a trick of trying the power-of-2 case, then
back-multiplying the result.
q_apx = div_apx(dividend, divisor);
dvi_apx = q_apx * divisor;
rem_apx = dividend - dvi_apx;
if(rem_apx < divisor)
return(q_apx); //exact
if(rem_apx < (divisor*2))
return(q_apx+1); //off by 1
//rare path, fall back to slow strategy.
Though, this approach mostly only makes sense if one assumes that the
multiplier is reasonably fast compared to the divider slow-path.
If using a shift-add multiplier, this approach does not make sense.
For 3D rendering, there is often a need for fast approximate
floating-point division, but then a reasonable strategy is:
Generate a reciprocal:
Do a magic integer subtract;
Patch it up with a few N-R stages.
Do division via multiplication by reciprocal.
Likewise for square-root, and reciprocal-square-root.
Though, another option is to do approximate floating-point reciprocal by
extracting the mantissa and then using an approximate integer reciprocal
of the HOBs as the starting point (with the shift-factors added to the
computed new exponent).
This is more complicated than the "subtract and N-R" trick, but can work
out being potentially faster if the FPU isn't particularly fast (or if
doing FPU emulation).
> Even relatively recent architectures like Alpha (1992) and IA-64
> (2001) do not include integer division instructions.
>
> So I found the inclusion of division in the RISC-V M extension
> surprising. They then separated them again by defining Zmmul.
>
If one assumes that multiplication is done with a Shift-and-Add unit,
then division comes cheaply as a very modest logic tweak.
Doesn't work if one uses a different strategy to implement multiply though.
In my case, I used shift-and-add for 64-bit multiply, and for 32 and 64
bit division.
64-bit multiply:
A little slower than the "decompose and use 32-bit multiply in software"
strategy, but less inconvenient.
32-bit divide:
A little faster than the software-side lookup-table trickery.
64-bit divide:
Notably faster than the shift-subtract loop.
However...
In most cases division is infrequent enough that it doesn't make a big
difference either way.
A cheaper strategy would be to only provide for 32-bit narrow and
widening multiply in a 64-bit machine.
Then argue that everything else be faked using 32-bit widening multiply.
But... annoyingly... RV64 doesn't provide a 32-bit widening multiply
instruction.
Though, did recently get around to adding a special case to BGBCC that
things like:
int x, y, z;
...
z=(((long long)x)*((long long)y))>>24;
Will now use a widening multiply (when available) rather than a full
64-bit multiply. Previously, was mostly often using an intrinsic for
this sort of thing, but this kinda sucked.
For RISC-V, can fake the full version of M with trap-and-emulate, but
this is the slowest option.
...
Technical Details
Message-ID:
<11bqs85$1pt4n$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-mailReferences:
<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>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: Tue, 27 Oct 2026 13:56:43 -0500 Organization: A noiseless patient Spider Lines: 151 Message-ID: <11bqs85$1pt4n$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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 27 Oct 2026 18:58:13 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1897623"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+9XG8MmideVmPh7JUF0AEjsdS7BYEk888="; posting-host="556ffa13be3e968dbe798e1c56f98249" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:ibgxgDchIaQmqQIsY6z7TWJ/ovc= sha256:m7E9dILRqKiaa731e9UgTjWMq40QSX/RAwssq8Z6dRk= sha1:rUB2hX/qg6RMsi2ZIFIyMrCuDqg= sha256:XNLbuMYrMmyxAj0BSAtvnCuCmpy1H46Eb8/kaZGooqA= In-Reply-To: <2026Oct27.171414@mips.complang.tuwien.ac.at> Content-Language: en-US