diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-12-16 13:25:07 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-12-16 13:25:07 +0400 |
commit | 2253b63c979fbfbbb6f06c93ede85d9b9b6caddf (patch) | |
tree | 6fe58202f96a7edd8448d7fbf17a14df6098d0f7 /source/blender/blenlib | |
parent | 91f14ddf3da140156c722e347b6f188210903629 (diff) |
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
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r-- | source/blender/blenlib/BLI_math_base.h | 5 | ||||
-rw-r--r-- | source/blender/blenlib/intern/math_base_inline.c | 26 |
2 files changed, 31 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_math_base.h b/source/blender/blenlib/BLI_math_base.h index 204ec9b5159..53db77dc203 100644 --- a/source/blender/blenlib/BLI_math_base.h +++ b/source/blender/blenlib/BLI_math_base.h @@ -167,6 +167,11 @@ MINLINE float signf(float f); MINLINE float power_of_2(float f); +/* these dont really fit anywhere but were being copied about a lot */ +MINLINE int is_power_of_2_i(int n); +MINLINE int power_of_2_max_i(int n); +MINLINE int power_of_2_min_i(int n); + MINLINE float shell_angle_to_dist(float angle); #if (defined(WIN32) || defined(WIN64)) && !defined(FREE_WINDOWS) 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 */ |