Sujet : Re: Cray style vectors
De : tr.17687 (at) *nospam* z991.linuxsc.com (Tim Rentsch)
Groupes : comp.archDate : 13. Mar 2024, 04:12:20
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <86bk7irfjf.fsf@linuxsc.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
User-Agent : Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
mitchalsup@aol.com (MitchAlsup1) writes:
Tim Rentsch wrote:
>
Terje Mathisen <terje.mathisen@tmsw.no> writes:
>
Tim Rentsch wrote:
>
Terje Mathisen <terje.mathisen@tmsw.no> writes:
>
If I really had to write a 64x64->128 MUL, with no widening MUL or
MULH which returns the high half, then I would punt and do it using
32-bit parts (all variables are u64): [...]
>
I wrote some code along the same lines. A difference is you
are considering unsigned multiplication, and I am considering
signed multiplication.
>
Signed mul is just a special case of unsigned mul, right?
>
I.e. in case of a signed widening mul, you'd first extract the signs,
convert the inputs to unsigned, then do the unsigned widening mul,
before finally resotirng the sign as the XOR of the input signs?
>
There is a small gotcha if either of the inputs are of the 0x80000000
form, i.e. MININT, but the naive iabs() conversion will do the right
thing by leaving the input unchanged.
>
At the other end there cannot be any issues since restoring a negative
output sign cannot overflow/fail.
>
It isn't quite that simple. Some of what you describe has a risk
of running afoul of implementation-defined behavior or undefined
behavior (as for example abs( INT_MIN )). I'm pretty sure it's
possible to avoid those pitfalls, but it requires a fair amount
of care and careful thinking.
>
It would be supremely nice if we could go back in time before
computers and reserve an integer encoding that represents the
value of "there is no value here" and mandate if upon integer
arithmetic.
ISO C allows such an encoding, even for two's complement.
Sadly it appears that the latest C standard will be taking
away that allowance.
Note that my goal is only to avoid the possibility of undefined
behavior that comes from signed overflow. My approach is to safely
determine whether the signed multiplication would overflow, and if
it wouldn't then simply use signed arithmetic to get the result.
>
Double width multiplication cannot overflow. 2n = nxn then, ignoring
the top n bits gives you your non-overflowing multiply.
C does not guarantee that. The point of the exercise is to
write code assuming nothing more than what the C standard
mandates.