Re: Cray style vectors

Liste des GroupesRevenir à c arch 
Sujet : Re: Cray style vectors
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.arch
Date : 12. Mar 2024, 08:32:52
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <usp0f5$5r2f$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
User-Agent : Mozilla Thunderbird
On 11/03/2024 22:29, Thomas Koenig wrote:
On 2024-02-25, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:

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?
It is, as far as I understand it.  (Tim knows these things better than I do, so if he finds something in the standards to contradict me, he's probably right.)
For a signed integer type T in C, you have N value bits, P padding bits, and a single sign bit.  The N value bits can hold values between 0 and 2^N - 1.  The sign bit can either indicate a negative value (sign and magnitude), -2^N (two's complement), or -(2^N - 1) (ones' complement). There will also be a corresponding unsigned type, which has at least N value bits and takes the same number of bytes, and has no sign bit.  It can still have padding bits (except for unsigned char, which has no padding).  But all non-negative integers that can be represented in the signed type have exactly the same significant bits in both the signed and unsigned types.
So an implementation can have a 32-bit two's complement "int", and use that for unsigned types too, treating the MSB as a padding bit for unsigned usage.  (Of course, doing so would be inefficient in use on most cpus - you'd have to keep masking off the padding bit when using the value, or somehow guarantee that it is never non-zero.)
In such an implementation, converting an int to an unsigned int would mask off the top bit (fully defined by the standard), and converting from an unsigned int it to a signed int would leave it unchanged (implementation dependent in the standard).  That means (int)(unsigned int)(-1) would be positive 0x7fffffff, not -1.
Real-world systems, of course, all use two's complement types with no padding, and Tim's description will work fine.
(And in C23, only two's complement representations will be allowed.  I can't remember if padding bits are still allowed, however.  And of course signed integer overflow is still UB.)

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