f3/libutils.c
Michel Machado 751b5a609f add f3brew (experimental)
f3brew, f3 block read write, tests block devices
writing and reading blocks directly to devices.

This is a not-functional version of f3brew.
The main contribution of this patch is to reorganize libprobe.{h,c},
so f3brew, and any future application, can reuse the library.
2014-10-10 09:54:39 -04:00

42 lines
614 B
C

#include "libutils.h"
/* Count the number of 1 bits. */
static int pop(uint64_t x)
{
int n = 0;
while (x) {
n++;
x = x & (x - 1);
}
return n;
}
int ilog2(uint64_t x)
{
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >> 16);
x = x | (x >> 32);
return pop(x) - 1;
}
/* Least power of 2 greater than or equal to x. */
static uint64_t clp2(uint64_t x)
{
x = x - 1;
x = x | (x >> 1);
x = x | (x >> 2);
x = x | (x >> 4);
x = x | (x >> 8);
x = x | (x >> 16);
x = x | (x >> 32);
return x + 1;
}
int ceiling_log2(uint64_t x)
{
return ilog2(clp2(x));
}