PatchworkOS  69292a3
A non-POSIX operating system.
Loading...
Searching...
No Matches
new_pow2.c
Go to the documentation of this file.
1#include <sys/math.h>
2
4{
5 if (n <= 1)
6 {
7 return 2;
8 }
9
10 if ((n > 0 && (n & (n - 1)) == 0))
11 {
12 return n;
13 }
14
15 n--;
16 n |= n >> 1;
17 n |= n >> 2;
18 n |= n >> 4;
19 n |= n >> 8;
20 n |= n >> 16;
21 n |= n >> 32;
22 n++;
23
24 if (n == 0)
25 {
26 return (UINT64_MAX >> 1) + 1;
27 }
28
29 return n;
30}
uint64_t next_pow2(uint64_t n)
Definition new_pow2.c:3
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
#define UINT64_MAX
Definition stdint.h:74