diff --git a/src/util/Makefile.am b/src/util/Makefile.am index 19785d7d93..7bbb5ba131 100644 --- a/src/util/Makefile.am +++ b/src/util/Makefile.am @@ -13,6 +13,7 @@ headers = \ argv.h \ cmd_line.h \ few.h \ + hibit.h \ if.h \ output.h \ path.h \ diff --git a/src/util/hibit.h b/src/util/hibit.h new file mode 100644 index 0000000000..6942d74ea0 --- /dev/null +++ b/src/util/hibit.h @@ -0,0 +1,38 @@ +/* + * $HEADER$ + */ + +#ifndef LAM_HIBIT_H +#define LAM_HIBIT_H + +/** + * Calculates the highest bit in an integer + * + * @param value The integer value to examine + * @param start Position to start looking + * + * @returns pos Position of highest-set integer or -1 if none are set. + * + * Look at the integer "value" starting at position "start", and move + * to the right. Return the index of the highest bit that is set to + * 1. + * + * WARNING: *NO* error checking is performed. This is meant to be a + * fast inline function. + */ +static inline int lam_hibit(int value, int start) +{ + unsigned int mask; + + --start; + mask = 1 << start; + + for (; start >= 0; --start, mask >>= 1) { + if (value & mask) + break; + } + + return start; +} + +#endif /* LAM_HIBIT_H */