#ifndef UTIL_H #define UTIL_H #include #include #include static inline uint32_t nlz(uint32_t x) { union { uint32_t asInt[2]; double asDouble; } tmp; tmp.asDouble = (double)x + 0.5; #if __BYTE_ORDER == __LITTLE_ENDIAN size_t n = 1054 - (tmp.asInt[1] >> 20); #elif __BYTE_ORDER == __BIG_ENDIAN size_t n = 1054 - (tmp.asInt[0] >> 20); #else #error Unknown endian #endif return n; } static inline uint32_t min(uint32_t x, uint32_t y) { return x < y ? x : y; } static inline uint64_t dilate(uint32_t x) { uint64_t r = x; r = (r | (r << 16)) & 0x0000FFFF0000FFFFul; r = (r | (r << 8)) & 0x00FF00FF00FF00FFul; r = (r | (r << 4)) & 0x0F0F0F0F0F0F0F0Ful; r = (r | (r << 2)) & 0x3333333333333333ul; r = (r | (r << 1)) & 0x5555555555555555ul; return r; } static inline uint32_t undilate(uint64_t x) { x = (x | (x >> 1)) & 0x3333333333333333ul; x = (x | (x >> 2)) & 0x0F0F0F0F0F0F0F0Ful; x = (x | (x >> 4)) & 0x00FF00FF00FF00FFul; x = (x | (x >> 8)) & 0x0000FFFF0000FFFFul; x = (x | (x >> 16)) &0x00000000FFFFFFFFul; return (uint32_t) x; } #endif