Welcome to little lamb

Code » test-msb » next » tree

[next] / src / check.c

#include <stdint.h>
#include <stdio.h>

typedef uint64_t u64;

extern int msb(u64 v);

int main(void)
{
    int rc = 0;

    for(int n = 0; n < 64; ++n) {
        u64 v = 1ULL << n;
        int r = msb(v - 1);
        if (r != n) {
            ++rc;
            printf("ERROR: msb(%zu)=%d [expected:%d]\n", v - 1, r, n);
        }
        r = msb(v);
        if (r != n + 1) {
            ++rc;
            printf("ERROR: msb(%zu)=%d [expected:%d]\n", v, r, n + 1);
        }
        if (v + 1 != 2) {
            r = msb(v + 1);
            if (r != n + 1) {
                ++rc;
                printf("ERROR: msb(%zu)=%d [expected:%d]\n", v + 1, r, n + 1);
            }
        }
    }

    return rc;
}