1 /* pow2, Copyright (c) 2016 Dave Odell <dmo2118@gmail.com>
3 * Permission to use, copy, modify, distribute, and sell this software and its
4 * documentation for any purpose is hereby granted without fee, provided that
5 * the above copyright notice appear in all copies and that both that
6 * copyright notice and this permission notice appear in supporting
7 * documentation. No representations are made about the suitability of this
8 * software for any purpose. It is provided "as is" without express or
19 /* -1 works best for to_pow2. */
23 /* GCC 3.4 also has this. */
24 # if defined __GNUC__ && __GNUC__ >= 4 || defined __clang__
25 return sizeof(long) * CHAR_BIT - __builtin_clzl(x) - 1;
28 unsigned bits = sizeof(x) * CHAR_BIT;
29 size_t mask = (size_t)-1;
30 unsigned result = bits - 1;
50 return !x ? 1 : 1 << (i_log2(x - 1) + 1);