diff options
Diffstat (limited to 'mysys')
-rw-r--r-- | mysys/mf_keycache.c | 10 | ||||
-rw-r--r-- | mysys/my_bit.c | 30 |
2 files changed, 32 insertions, 8 deletions
diff --git a/mysys/mf_keycache.c b/mysys/mf_keycache.c index bf8986fe05b..1dab9a47ed8 100644 --- a/mysys/mf_keycache.c +++ b/mysys/mf_keycache.c @@ -262,15 +262,9 @@ static int keycache_pthread_cond_signal(pthread_cond_t *cond); #define keycache_pthread_cond_signal pthread_cond_signal #endif /* defined(KEYCACHE_DEBUG) */ -static uint next_power(uint value) +static inline uint next_power(uint value) { - uint old_value= 1; - while (value) - { - old_value= value; - value&= value-1; - } - return (old_value << 1); + return (uint) my_round_up_to_next_power((uint32) value) << 1; } diff --git a/mysys/my_bit.c b/mysys/my_bit.c index 01c9b5ea68d..6ef0e171695 100644 --- a/mysys/my_bit.c +++ b/mysys/my_bit.c @@ -76,3 +76,33 @@ uint my_count_bits_ushort(ushort v) return nbits[v]; } + +/* + Next highest power of two + + SYNOPSIS + my_round_up_to_next_power() + v Value to check + + RETURN + Next or equal power of 2 + Note: 0 will return 0 + + NOTES + Algorithm by Sean Anderson, according to: + http://graphics.stanford.edu/~seander/bithacks.html + (Orignal code public domain) + + Comments shows how this works with 01100000000000000000000000001011 +*/ + +uint32 my_round_up_to_next_power(uint32 v) +{ + v--; /* 01100000000000000000000000001010 */ + v|= v >> 1; /* 01110000000000000000000000001111 */ + v|= v >> 2; /* 01111100000000000000000000001111 */ + v|= v >> 4; /* 01111111110000000000000000001111 */ + v|= v >> 8; /* 01111111111111111100000000001111 */ + v|= v >> 16; /* 01111111111111111111111111111111 */ + return v+1; /* 10000000000000000000000000000000 */ +} |