Liste des Groupes | Revenir à c arch |
On Mon, 9 Sep 2024 08:56:45 +0200
Terje Mathisen <terje.mathisen@tmsw.no> wrote:
David Brown wrote:On 05/09/2024 19:04, Terje Mathisen wrote:David Brown wrote:On 05/09/2024 11:12, Terje Mathisen wrote:>David Brown wrote:>Unsigned types are ideal for "raw" memory access or external>
data, for anything involving bit manipulation (use of &, |, ^,
<< and >> on signed types is usually wrong, IMHO), as building
blocks in extended arithmetic types, for the few occasions
when you want two's complement wrapping, and for the even
fewer occasions when you actually need that last bit of
range.
That last paragraph enumerates pretty much all the uses I have
for integer-type variables, with (like Mitch) a few apis that
use (-1) as an error signal that has to be handled with special
code.
You don't have loop counters, array indices, or integer
arithmetic?
Loop counters of the for (i= 0; i < LIMIT; i++) type are of
course fine with unsigned i, arrays always use a zero base so in
Rust the only array index type is usize, i.e the largest
supported unsigned type in the system, typically the same as
u64.
Loop counters can usually be signed or unsigned, and it usually
makes no difference. Array indices are also usually much the same
signed or unsigned, and it can feel more natural to use size_t
here (an unsigned type). It can make a difference to efficiency,
however. On x86-64, this code is 3 instructions with T as
"unsigned long int" or "long int", 4 with "int", and 5 with
"unsigned int".
int foo(int * p, T x) {
int a = p[x++];
int b = p[x++];
return a + b;
}
;; assume *p in rdi, x in rsi
mov rax,[rdi+rsi]
add rax,[rdi+rsi+8]
ret
more like
mov rax,[rdi+rsi*4]
add rax,[rdi+rsi*4+8]
ret
>
But that's not the point (==trap).
The point (==trap), I'd guess, is that for T=uint32_t code generator
has to account for possibility of x==2**32-1.
Les messages affichés proviennent d'usenet.