From fe3a21f6a5958a6e54d5bcf7c162767cfdf5f9db Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 17 Aug 2009 19:15:33 +0200 Subject: macro: add PA_ROUND_UP/PA_ROUND_DOWN macros --- src/pulsecore/macro.h | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'src/pulsecore/macro.h') diff --git a/src/pulsecore/macro.h b/src/pulsecore/macro.h index cf662510..aa06359a 100644 --- a/src/pulsecore/macro.h +++ b/src/pulsecore/macro.h @@ -91,7 +91,8 @@ static inline size_t pa_page_align(size_t l) { #ifdef __GNUC__ #define PA_MAX(a,b) \ - __extension__ ({ typeof(a) _a = (a); \ + __extension__ ({ \ + typeof(a) _a = (a); \ typeof(b) _b = (b); \ _a > _b ? _a : _b; \ }) @@ -101,7 +102,8 @@ static inline size_t pa_page_align(size_t l) { #ifdef __GNUC__ #define PA_MIN(a,b) \ - __extension__ ({ typeof(a) _a = (a); \ + __extension__ ({ \ + typeof(a) _a = (a); \ typeof(b) _b = (b); \ _a < _b ? _a : _b; \ }) @@ -111,7 +113,8 @@ static inline size_t pa_page_align(size_t l) { #ifdef __GNUC__ #define PA_CLAMP(x, low, high) \ - __extension__ ({ typeof(x) _x = (x); \ + __extension__ ({ \ + typeof(x) _x = (x); \ typeof(low) _low = (low); \ typeof(high) _high = (high); \ ((_x > _high) ? _high : ((_x < _low) ? _low : _x)); \ @@ -122,7 +125,8 @@ static inline size_t pa_page_align(size_t l) { #ifdef __GNUC__ #define PA_CLAMP_UNLIKELY(x, low, high) \ - __extension__ ({ typeof(x) _x = (x); \ + __extension__ ({ \ + typeof(x) _x = (x); \ typeof(low) _low = (low); \ typeof(high) _high = (high); \ (PA_UNLIKELY(_x > _high) ? _high : (PA_UNLIKELY(_x < _low) ? _low : _x)); \ @@ -135,6 +139,28 @@ static inline size_t pa_page_align(size_t l) { * make sense: we cannot know if it is more likely that the values is * lower or greater than the boundaries.*/ +#ifdef __GNUC__ +#define PA_ROUND_UP(a, b) \ + __extension__ ({ \ + typeof(a) _a = (a); \ + typeof(b) _b = (b); \ + ((_a + _b - 1) / _b) * _b; \ + }) +#else +#define PA_ROUND_UP(a, b) ((((a) + (b) - 1) / (b)) * (b)) +#endif + +#ifdef __GNUC__ +#define PA_ROUND_DOWN(a, b) \ + __extension__ ({ \ + typeof(a) _a = (a); \ + typeof(b) _b = (b); \ + (_a / _b) * _b; \ + }) +#else +#define PA_ROUND_DOWN(a, b) (((a) / (b)) * (b)) +#endif + /* This type is not intended to be used in exported APIs! Use classic "int" there! */ #ifdef HAVE_STD_BOOL typedef _Bool pa_bool_t; -- cgit From 8f928b2e572cd7bf26517afddd62ceecb78edfdc Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 17 Aug 2009 19:17:10 +0200 Subject: macro: simplify page/word alignment macros a bit --- src/pulsecore/macro.h | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'src/pulsecore/macro.h') diff --git a/src/pulsecore/macro.h b/src/pulsecore/macro.h index aa06359a..3c560bc6 100644 --- a/src/pulsecore/macro.h +++ b/src/pulsecore/macro.h @@ -59,28 +59,24 @@ #endif /* Rounds down */ -static inline void* pa_align_ptr(const void *p) { - return (void*) (((size_t) p) & ~(sizeof(void*)-1)); +static inline void* PA_ALIGN_PTR(const void *p) { + return (void*) (((size_t) p) & ~(sizeof(void*) - 1)); } -#define PA_ALIGN_PTR(x) (pa_align_ptr(x)) /* Rounds up */ -static inline size_t pa_align(size_t l) { - return (((l + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*)); +static inline size_t PA_ALIGN(size_t l) { + return ((l + sizeof(void*) - 1) & ~(sizeof(void*) - 1)); } -#define PA_ALIGN(x) (pa_align(x)) /* Rounds down */ -static inline void* pa_page_align_ptr(const void *p) { - return (void*) (((size_t) p) & ~(PA_PAGE_SIZE-1)); +static inline void* PA_PAGE_ALIGN_PTR(const void *p) { + return (void*) (((size_t) p) & ~(PA_PAGE_SIZE - 1)); } -#define PA_PAGE_ALIGN_PTR(x) (pa_page_align_ptr(x)) /* Rounds up */ -static inline size_t pa_page_align(size_t l) { - return ((l + PA_PAGE_SIZE - 1) / PA_PAGE_SIZE) * PA_PAGE_SIZE; +static inline size_t PA_PAGE_ALIGN(size_t l) { + return (l + PA_PAGE_SIZE - 1) & ~(PA_PAGE_SIZE - 1); } -#define PA_PAGE_ALIGN(x) (pa_page_align(x)) #define PA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0])) -- cgit From 2838b78e59ee7c8ea42fec6880cc4c2b2a2c9485 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 19 Aug 2009 01:03:09 +0200 Subject: macro: extend comments a bit --- src/pulsecore/macro.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/pulsecore/macro.h') diff --git a/src/pulsecore/macro.h b/src/pulsecore/macro.h index 3c560bc6..ce88c1b9 100644 --- a/src/pulsecore/macro.h +++ b/src/pulsecore/macro.h @@ -80,10 +80,10 @@ static inline size_t PA_PAGE_ALIGN(size_t l) { #define PA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0])) -/* The users of PA_MIN and PA_MAX should be aware that these macros on - * non-GCC executed code with side effects twice. It is thus - * considered misuse to use code with side effects as arguments to MIN - * and MAX. */ +/* The users of PA_MIN and PA_MAX, PA_CLAMP, PA_ROUND_UP should be + * aware that these macros on non-GCC executed code with side effects + * twice. It is thus considered misuse to use code with side effects + * as arguments to MIN and MAX. */ #ifdef __GNUC__ #define PA_MAX(a,b) \ -- cgit From 827ae07c1ec73c8fe48dc4182726c38c3e9c992f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 26 Aug 2009 01:41:34 +0200 Subject: macro: add PA_CLIP_SUB() for saturated subtraction --- src/pulsecore/macro.h | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/pulsecore/macro.h') diff --git a/src/pulsecore/macro.h b/src/pulsecore/macro.h index ce88c1b9..87684ad3 100644 --- a/src/pulsecore/macro.h +++ b/src/pulsecore/macro.h @@ -157,6 +157,17 @@ static inline size_t PA_PAGE_ALIGN(size_t l) { #define PA_ROUND_DOWN(a, b) (((a) / (b)) * (b)) #endif +#ifdef __GNUC__ +#define PA_CLIP_SUB(a, b) \ + __extension__ ({ \ + typeof(a) _a = (a); \ + typeof(b) _b = (b); \ + _a > _b ? _a - _b : 0; \ + }) +#else +#define PA_CLIP_SUB(a, b) ((a) > (b) ? (a) - (b) : 0) +#endif + /* This type is not intended to be used in exported APIs! Use classic "int" there! */ #ifdef HAVE_STD_BOOL typedef _Bool pa_bool_t; -- cgit From 27bfb7628c709eb6802c9ec73a6feb995fda8375 Mon Sep 17 00:00:00 2001 From: Wim Taymans Date: Wed, 26 Aug 2009 19:29:56 +0200 Subject: macro: add macro to align variables --- src/pulsecore/macro.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/pulsecore/macro.h') diff --git a/src/pulsecore/macro.h b/src/pulsecore/macro.h index 87684ad3..bffcc264 100644 --- a/src/pulsecore/macro.h +++ b/src/pulsecore/macro.h @@ -80,6 +80,12 @@ static inline size_t PA_PAGE_ALIGN(size_t l) { #define PA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0])) +#if defined(__GNUC__) + #define PA_DECLARE_ALIGNED(n,t,v) t v __attribute__ ((aligned (n))) +#else + #define PA_DECLARE_ALIGNED(n,t,v) t v +#endif + /* The users of PA_MIN and PA_MAX, PA_CLAMP, PA_ROUND_UP should be * aware that these macros on non-GCC executed code with side effects * twice. It is thus considered misuse to use code with side effects -- cgit