comparison maths/MathUtilities.cpp @ 55:7fe29d8a7eaf

* Various fixes related to the bar estimator code
author cannam
date Tue, 10 Feb 2009 16:37:11 +0000
parents 5bec06ecc88a
children d72fcd34d9a7
comparison
equal deleted inserted replaced
54:5bec06ecc88a 55:7fe29d8a7eaf
351 data[i] -= smoothed[i]; 351 data[i] -= smoothed[i];
352 if (data[i] < 0.0) data[i] = 0.0; 352 if (data[i] < 0.0) data[i] = 0.0;
353 } 353 }
354 } 354 }
355 355
356 356 bool
357 MathUtilities::isPowerOfTwo(int x)
358 {
359 if (x < 2) return false;
360 if (x & (x-1)) return false;
361 return true;
362 }
363
364 int
365 MathUtilities::nextPowerOfTwo(int x)
366 {
367 if (isPowerOfTwo(x)) return x;
368 int n = 1;
369 while (x) { x >>= 1; n <<= 1; }
370 return n;
371 }
372
373 int
374 MathUtilities::previousPowerOfTwo(int x)
375 {
376 if (isPowerOfTwo(x)) return x;
377 int n = 1;
378 x >>= 1;
379 while (x) { x >>= 1; n <<= 1; }
380 return n;
381 }
382
383 int
384 MathUtilities::nearestPowerOfTwo(int x)
385 {
386 if (isPowerOfTwo(x)) return x;
387 int n0 = previousPowerOfTwo(x), n1 = nearestPowerOfTwo(x);
388 if (x - n0 < n1 - x) return n0;
389 else return n1;
390 }
391