From 2253b63c979fbfbbb6f06c93ede85d9b9b6caddf Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 16 Dec 2011 09:25:07 +0000 Subject: static functions for getting power of 2 values were being copied about too much, add to the BLI_math api. - is_power_of_2_i - power_of_2_min_i - power_of_2_max_i --- source/blender/blenlib/intern/math_base_inline.c | 26 ++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'source/blender/blenlib/intern') diff --git a/source/blender/blenlib/intern/math_base_inline.c b/source/blender/blenlib/intern/math_base_inline.c index 0b2411af009..7e04e0ae566 100644 --- a/source/blender/blenlib/intern/math_base_inline.c +++ b/source/blender/blenlib/intern/math_base_inline.c @@ -115,6 +115,31 @@ MINLINE float power_of_2(float val) return (float)pow(2.0, ceil(log((double)val) / M_LN2)); } +MINLINE int is_power_of_2_i(int n) +{ + return (n & (n - 1)) == 0; +} + +MINLINE int power_of_2_max_i(int n) +{ + if (is_power_of_2_i(n)) + return n; + + while(!is_power_of_2_i(n)) + n = n & (n - 1); + + return n * 2; +} + +MINLINE int power_of_2_min_i(int n) +{ + while (!is_power_of_2_i(n)) + n = n & (n - 1); + + return n; +} + + MINLINE float minf(float a, float b) { return (a < b)? a: b; @@ -130,5 +155,6 @@ MINLINE float signf(float f) return (f < 0.f)? -1.f: 1.f; } + #endif /* BLI_MATH_BASE_INLINE_H */ -- cgit v1.2.3