Liste des Groupes | Revenir à c arch |
On 05.09.24 19:04, Terje Mathisen wrote:I like that one!One of my alternatives areWhat about:
>
 unsigned u = start; // Cannot be less than zero
 if (u) {
   u++;
   do {
     u--;
     data[u]...
   while (u);
 }
>
This typically results in effectively the same asm code as the signed version, except for a bottom JGE (Jump (signed) Greater or Equal instead of JA (Jump Above or Equal, but my version is far more verbose.
>
Alternatively, if you don't need all N bits of the unsigned type, then you can subtract and check if the top bit is set in the result:
>
 for (unsigned u = start; (u & TOPBIT) == 0; u--)
>
Terje
>
for (unsigned u = start; u != ~0u; --u)
...That is the one that I've actually been using, i.e. casting to the corresponding signed type.
or even
for (unsigned u = start; (int)u >= 0; --u)
...Thanks!
?
I've compared all variants for x86_64 with -O3 -fexpensive-optimizations on godbolt.org:
- 32 bit version: https://godbolt.org/z/TMhhx3nch
- 64 bit version: https://godbolt.org/z/8oxzTf5Gf
Les messages affichés proviennent d'usenet.