Jonathan Lennox
2015-Aug-04 16:04 UTC
[opus] [PATCH] Simplify and generalize implementation of align(). Should be very efficient on sensible platforms, and correct everywhere.
--- src/opus_private.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/opus_private.h b/src/opus_private.h index 63338fe..5bbd7dc 100644 --- a/src/opus_private.h +++ b/src/opus_private.h @@ -33,6 +33,8 @@ #include "opus.h" #include "celt.h" +#include <stddef.h> /* offsetof */ + struct OpusRepacketizer { unsigned char toc; int nb_frames; @@ -110,15 +112,13 @@ int opus_decode_native(OpusDecoder *st, const unsigned char *data, opus_int32 le /* Make sure everything is properly aligned. */ static OPUS_INLINE int align(int i) { - int size; - /* Alignment is determined by the max size of void*, opus_int32 and opus_val32, - rounded up to the nearest power of two. */ - int tmp = (sizeof(opus_int32)-1)|(sizeof(opus_val32)-1)|(sizeof(void*)-1); - if (tmp == 0) - size = 1; - else - size = 1 << EC_ILOG(tmp); - return (i+size-1)&-size; + struct foo {char c; union { void* p; opus_int32 i; opus_val32 v; } u;}; + + int alignment = offsetof(struct foo, u); + + /* Optimizing compilers should optimize div and multiply into and + for all sensible alignment values. */ + return ((i + alignment - 1) / alignment) * alignment; } int opus_packet_parse_impl(const unsigned char *data, opus_int32 len, -- 2.3.2 (Apple Git-55)
Mark Harris
2015-Aug-05 00:53 UTC
[opus] [PATCH] Eliminate signed division overhead in align()
--- src/opus_private.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/opus_private.h b/src/opus_private.h index 5bbd7dc..3b62eed 100644 --- a/src/opus_private.h +++ b/src/opus_private.h @@ -114,7 +114,7 @@ static OPUS_INLINE int align(int i) { struct foo {char c; union { void* p; opus_int32 i; opus_val32 v; } u;}; - int alignment = offsetof(struct foo, u); + unsigned int alignment = offsetof(struct foo, u); /* Optimizing compilers should optimize div and multiply into and for all sensible alignment values. */ -- 2.4.5