#include "game_types.h"

/* Bignum_CountLeadingBits @ 0x0801CCA4 (real 88B) -- quirky bit-scan saturating at 32. Mirrors src/matched/Bignum_CountLeadingBits.c. */
/* Probes 16/8/4/2-bit groups shifting left, then the sign: negatives
   return the count, one more when bit 30 is set, else 32. Small values
   saturate at 32 (verbatim behavior, not textbook CLZ). */
int Bignum_CountLeadingBits(unsigned x)
{
    int n = 0;
    if ((x & 0xFFFF0000) == 0) {
        n = 16;
        x <<= 16;
    }
    if ((x & 0xFF000000) == 0) {
        n += 8;
        x <<= 8;
    }
    if ((x & 0xF0000000) == 0) {
        n += 4;
        x <<= 4;
    }
    if ((x & 0xC0000000) == 0) {
        n += 2;
        x <<= 2;
    }
    if ((int)x < 0)
        return n;
    n++;
    if ((x & 0x40000000) != 0)
        return n;
    return 32;
}
