🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.arch
Article #553

Re: two's complement and Flags (Re: arithmetic and endian history)

#553
From: Anton Ertl
Date: Fri, 06 Nov 2026 15:08
102 lines
5109 bytes
Terje Mathisen <terje.mathisen@tmsw.no> writes:
[add with carry-in and carry-out]
>On Alpha you needed to play tricks with predicated instructions, afair 
>you had two forms of ADD, one that just added two registers and one that 
>did the same plus an extra increment which represented an incoming carry.

No, Alpha is just like MIPS and RISC-V.  For RISC-V:

add  tmp1, carry, summand1
sltu carry1, tmp1, summand1
add  sum, tmp1, summand2
sltu carry2, sum, tmp1
add  carry, carry2, carry1

Alpha has no predicated instructions apart from a conditional move and
no addition with an extra increment.

What you describe reminds me of IA-64; it certeinly selects between
a+b and a+b+1 with predication, but I don't remember how it gets the
carry-out into a flag.

>Even on x86/x64 you get into similar situations when you need to add 
>multiple parts together, it is easy to get stuck with long manual carry 
>chains.

Not sure what you mean here.  If you do it the conventional way, you
certainly have a carry chain of n-1 carries if you add two n*64-bit
numbers; so you have n dependent adds involved.

If you want more parallelism, Michael S has presented a technique here
that allows breaking such a chain into pieces, but it increases the
number of instructions to execute, so you probably want to have a
chain of a few such dependencies before you break one.

Alexander Yee has implemented Kogge-Stone addition in AVX-512, but
found that the variant that uses mask operations it is slower on
Skylake X than the conventional way.  On AMD processors with AVX-512,
it looks better.  He also has a longer sequence that avoids the mask
instructions and is faster on Skylake X.

If you want to have more addition chains at the same time, as in
m1+m2+m3, or m1*m2 (which needs two addition chains in a conventional
implementation), AMD64 gives you two carry chains with ADX (which,
interestingly, is not included in x64-64-v4, even though ADX is
included in all processors that have all of the features of
x64-64-v4).  Once you need three carry chains, you are out of luck
with AMD64.

>Possibly the best primitive is MAA (Mul Add Add) representing
>
>  (sumlo, sumhi) = muladdadd(f1, f2, a1, a2);
>
>since this can never overflow the two-element result?

It is useful for m1*m2, but not for addition.  With one carry flag
(e.g., ARM A64), a primitive

(lo, hi) = muladd(f1,f2,a1)

or alternatively

(li, hi) = muladd_carry(f1,f2,a1)

would already be helpful and would be a little closer to the usual
instruction formats.

A disadvantage of implementing all these primitives for multiplication
as instruction is that in any usual implementation of such
instructions it adds the latency of the multiplication on the whole
chain, whereas you can avoid that if the addition is separate.

BTW, the Alpha/RISC-V approach is not as bad for implementing
muladdadd as it is for add with carry-in and carry-out; e.g., if you
want to minimize the latency from a1 to hi (because hi will become the
a1 in the next limb):

mulhu hi, f1, f2
mul   lo, f1, f2
add   lo, lo, a2
sltu  carry, lo, a2
add   hi, hi, carry
add   lo, lo, a1
sltu  carry, lo, a1
add   hi, hi, carry

That's three cycles of latency.  Of course, AMD with ADX is better:

# a1co = a1-C-O
# f1 is in rdx; AT&T operand order
mulx f2, lo, hico
adcx a2, hico
adox a1, hico
# hi = hico+C+O

The addition of C and O happens in the next limb.  So here we have a
latency of one cycle from one limb to the next (if C and O are
independent, as in Intel P-cores).

- anton
-- 
'Anyone trying for "industrial quality" ISA should avoid undefined behavior.'
  Mitch Alsup, <c17fcd89-f024-40e7-a594-88a85ac10d20o@googlegroups.com>
Technical Details
Message-ID: <2026Nov6.160841@mips.complang.tuwien.ac.at>
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: <10uks4f$1dqo$1@gal.iecc.com> <11cai8f$32ue7$1@dont-email.me> <11caljv$jeo$1@gal.iecc.com> <6ae8eec0.35320218@news.eternal-september.org> <11cjh58$1flq$4@gal.iecc.com> <2026Nov6.072410@mips.complang.tuwien.ac.at> <11ckcg9$2f7hg$1@dont-email.me>

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: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.arch
Subject: Re: two's complement and Flags (Re: arithmetic and endian history)
Date: Fri, 06 Nov 2026 15:08:41 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 102
Message-ID: <2026Nov6.160841@mips.complang.tuwien.ac.at>
References: <10uks4f$1dqo$1@gal.iecc.com> <11cai8f$32ue7$1@dont-email.me> <11caljv$jeo$1@gal.iecc.com> <6ae8eec0.35320218@news.eternal-september.org> <11cjh58$1flq$4@gal.iecc.com> <2026Nov6.072410@mips.complang.tuwien.ac.at> <11ckcg9$2f7hg$1@dont-email.me>
Injection-Date: Fri, 06 Nov 2026 16:29:31 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2838107"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18qikw+jW81pu/9ctwIsjDH"; posting-host="bc90cb9d5a3c358a3a306caf3ca0415d"
Cancel-Lock: sha1:7Gr19+Rc7Z36WQD91nFUe1f0rU8= sha256:QXKpR51zTyOGmcgRimTSEoWijB8wSUGIpwBL4HB71JI=
	sha1:GRA9BnRTlEARkrgx9Gk/6q5yHC4= sha256:0Vh+V4tNCYJIDlarjrARDiEg0HKKYPB6I+2ytygE8aU=
X-newsreader: xrn 10.11