Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #pragma once
00015
00019 #include <cassert>
00020
00021 #include "Types.h"
00022
00023 #ifdef LINUX
00024 #include <stdint.h>
00025 #endif
00026
00027 namespace kNet
00028 {
00029
00031 #define IS_POW2(value) (((value) & ((value)-1)) == 0)
00032
00034 inline bool IsPow2Aligned(uintptr_t pointer, u32 alignment)
00035 {
00036 assert(IS_POW2(alignment));
00037 return (pointer & (alignment - 1)) == 0;
00038 }
00039
00041 inline uintptr_t AlignUpPow2(uintptr_t pointer, u32 alignment)
00042 {
00043 assert(IS_POW2(alignment));
00044 return (pointer + alignment - 1) & ~((uintptr_t)alignment - 1);
00045 }
00046
00048 inline uintptr_t AlignDownPow2(uintptr_t pointer, u32 alignment)
00049 {
00050 assert(IS_POW2(alignment));
00051 return pointer & ~((uintptr_t)alignment - 1);
00052 }
00053
00054 inline u32 RoundUpToNextPow2(u32 x)
00055 {
00056 x = x - 1;
00057 x = x | (x >> 1);
00058 x = x | (x >> 2);
00059 x = x | (x >> 4);
00060 x = x | (x >> 8);
00061 x = x | (x >> 16);
00062 return x + 1;
00063 }
00064
00065 inline u64 RoundUpToNextPow2(u64 x)
00066 {
00067 x = x - 1;
00068 x = x | (x >> 1);
00069 x = x | (x >> 2);
00070 x = x | (x >> 4);
00071 x = x | (x >> 8);
00072 x = x | (x >> 16);
00073 x = x | (x >> 32);
00074 return x + 1;
00075 }
00076
00077 }