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
17 /* -1 works best for to_pow2. */
21 /* GCC 3.4 also has this. */
22 /* The preprocessor criteria here must match what's in pow2.h, to prevent
25 # if defined __GNUC__ && __GNUC__ >= 4 || defined __clang__
26 return i_log2_fast(x);
29 unsigned bits = sizeof(x) * CHAR_BIT;
30 size_t mask = (size_t)-1;
31 unsigned result = bits - 1;
51 return !x ? 1 : 1 << (i_log2(x - 1) + 1);