Re: Cray style vectors

Liste des GroupesRevenir à c arch 
Sujet : Re: Cray style vectors
De : tkoenig (at) *nospam* netcologne.de (Thomas Koenig)
Groupes : comp.arch
Date : 11. Mar 2024, 22:29:02
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <usnt2u$3r7bl$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
User-Agent : slrn/1.0.3 (Linux)
On 2024-02-25, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
Thomas Koenig <tkoenig@netcologne.de> writes:
>
Tim Rentsch <tr.17687@z991.linuxsc.com> schrieb:
>
Thomas Koenig <tkoenig@netcologne.de> writes:
>
Signed integer overflow is undefined behavior in C and prohibited
in Fortran.  Yet, there is no straightforward, standard-compliant
way to check for signed overflow (and handle this appropriately)
in either language.  [...]
>
It isn't hard to write standard C code to determine whether a
proposed addition or subtraction would overflow, and does so
safely and reliably.
>
Also efficiently and without resorting to implementation-
defined or undefined behavior (and without needing a bigger
type)?

[...]

Heavens to Betsy!  Are you impugning the quality and excellence
of my code?  Of *my* code?  I can only hope that you are suitably
chagrined and contrite.  ;)
>
It's a little bit tedious perhaps but not
difficult.  Checking code can be wrapped in an inline function
and invoke whatever handling is desired, within reason.
>
Maybe you could share such code?
>
Rather that do that I will explain.
>
An addition overflows if the two operands have the same sign and
the sign of an operand is the opposite of the sign of the sum
(taken mod the width of the operands).  Convert the signed
operands to their unsigned counterparts, and form the sum of the
unsigned values.  The sign is just the high-order bit in each
case.  Thus the overflow condition can be detected with a few
bitwise xors and ands.
>
Subtraction is similar except now overflow can occur only when
the operands have different signs and the sign of the sum is
the opposite of the sign of the first operand.
>
The above description works for two's complement hardware where
unsigned types have the same width as their corresponding signed
types.  I think for most people that's all they need.  The three
other possibilities are all doable with minor adjustments, and
code appropriate to each particular implementation can be
selected using C preprocessor conditional, as for example

...

but that's implementation-defined behavior, correct?

Date Sujet#  Auteur
11 Mar 24 * Re: Cray style vectors3Thomas Koenig
12 Mar 24 +- Re: Cray style vectors1David Brown
12 Mar 24 `- Re: Cray style vectors1Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal