diff options
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/asm.c | 38 | ||||
| -rw-r--r-- | src/common/asm.h | 3 | 
2 files changed, 41 insertions, 0 deletions
diff --git a/src/common/asm.c b/src/common/asm.c index 597013a..0e92938 100644 --- a/src/common/asm.c +++ b/src/common/asm.c @@ -65,3 +65,41 @@ bool msb_32(uint32_t v, unsigned int *p)      return true;  } + + +/****************************************************************************** +*                                                                             * +*  Paramètres  : v = valeur quelconque sur 64 bits.                           * +*                p = position du premier bit à 1 (poids fort). [OUT]          * +*                                                                             * +*  Description : Détermine l'indice du premier bit à 1, côté gauche.          * +*                                                                             * +*  Retour      : true si le nombre est différent de zéro, false sinon.        * +*                                                                             * +*  Remarques   : -                                                            * +*                                                                             * +******************************************************************************/ + +bool msb_64(uint64_t v, unsigned int *p) +{ +    static const unsigned int bval[] = { 0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 }; + +    /* S'il n'y a aucun bit à 1... */ +    if (v == 0) return false; + +    /** +     * Cf. msb_32(). +     */ + +    *p = 0; + +    if (v & 0xffffffff00000000ull) { *p += 32 / 1; v >>= 32 / 1; } +    if (v & 0x00000000ffff0000ull) { *p += 32 / 2; v >>= 32 / 2; } +    if (v & 0x000000000000ff00ull) { *p += 32 / 4; v >>= 32 / 4; } +    if (v & 0x00000000000000f0ull) { *p += 32 / 8; v >>= 32 / 8; } + +    *p += bval[v]; + +    return true; + +} diff --git a/src/common/asm.h b/src/common/asm.h index 047d8db..5207bf7 100644 --- a/src/common/asm.h +++ b/src/common/asm.h @@ -33,6 +33,9 @@  /* Détermine l'indice du premier bit à 1, côté gauche. */  bool msb_32(uint32_t, unsigned int *); +/* Détermine l'indice du premier bit à 1, côté gauche. */ +bool msb_64(uint64_t, unsigned int *); +  #endif  /* _COMMON_ASM_H */  | 
