From 10a54136f3a9a1d8b8ec899ae457a9a4d327e8cf Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Wed, 7 Oct 2009 18:26:59 +0200 Subject: [PATCH] src/mathutils.c: add aubio_is_power_of_two and aubio_next_power_of_two --- configure.ac | 2 +- src/aubio_priv.h | 4 ++++ src/mathutils.c | 19 +++++++++++++++++++ src/mathutils.h | 6 ++++++ wscript | 1 + 5 files changed, 31 insertions(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 308d266f..7fb40c69 100644 --- a/configure.ac +++ b/configure.ac @@ -114,7 +114,7 @@ dnl Check for required libraries AC_CHECK_LIB(pthread, pthread_create) dnl Check for header files -AC_CHECK_HEADERS([string.h stdlib.h stdio.h math.h errno.h stdarg.h unistd.h signal.h],,) +AC_CHECK_HEADERS([string.h stdlib.h stdio.h math.h limits.h errno.h stdarg.h unistd.h signal.h],,) AC_CHECK_HEADERS(fftw3.h,,AC_MSG_ERROR([Ouch! missing fftw3.h header])) AC_ARG_ENABLE(complex, AC_HELP_STRING([--enable-complex],[compile with complex.h [[default=auto]]]), diff --git a/src/aubio_priv.h b/src/aubio_priv.h index 683056fc..ab950429 100644 --- a/src/aubio_priv.h +++ b/src/aubio_priv.h @@ -62,6 +62,10 @@ #include #endif +#if HAVE_LIMITS_H +#include // for CHAR_BIT, in C99 standard +#endif + #include "types.h" /**** diff --git a/src/mathutils.c b/src/mathutils.c index 66dab58e..b99a96b4 100644 --- a/src/mathutils.c +++ b/src/mathutils.c @@ -409,6 +409,25 @@ aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize) return aubio_freqtobin (freq, samplerate, fftsize); } +uint_t +aubio_is_power_of_two(uint_t a) { + if ((a & a-1) == 0) { + return 1; + } else { + return 0; + } +} + +uint_t +aubio_next_power_of_two(uint_t a) { + uint_t i; + a--; + for (i = 0; i < sizeof(uint_t) * CHAR_BIT; i++ ) { + a = a | a >> 1; + } + return a+1; +} + smpl_t aubio_db_spl (fvec_t * o) { diff --git a/src/mathutils.h b/src/mathutils.h index d71cf77f..de970493 100644 --- a/src/mathutils.h +++ b/src/mathutils.h @@ -316,6 +316,12 @@ smpl_t aubio_freqtomidi (smpl_t freq); /** convert midi value (0-128) to frequency (Hz) */ smpl_t aubio_miditofreq (smpl_t midi); +/** return 1 if a is a power of 2, 0 otherwise */ +uint_t aubio_is_power_of_two(uint_t a); + +/** return the next power of power of 2 greater than a */ +uint_t aubio_next_power_of_two(uint_t a); + /** compute sound pressure level (SPL) in dB This quantity is often wrongly called 'loudness'. diff --git a/wscript b/wscript index d8fe5a40..58bd1c9b 100644 --- a/wscript +++ b/wscript @@ -53,6 +53,7 @@ def configure(conf): conf.check(header_name='stdio.h') conf.check(header_name='math.h') conf.check(header_name='string.h') + conf.check(header_name='limits.h') # optionally use complex.h if (Options.options.disable_complex == False): -- 2.11.0