From 1284c127f8e1f2ab693d523a009b7b2e553024a5 Mon Sep 17 00:00:00 2001 From: Nils Wallménius Date: Thu, 20 Nov 2008 20:20:43 +0000 Subject: Introduce likely() and unlikely() macros, use to give gcc hints about which branch is likely to be taken in a conditional, use them in the midi player for a small speedup, use instead of similar EXPECT macros in tremor and use in mpegplayer git-svn-id: svn://svn.rockbox.org/rockbox/trunk@19162 a1c6a512-1295-4272-9138-f99709370657 --- apps/codecs/libtremor/codebook.c | 10 +++++----- apps/codecs/libtremor/misc.h | 12 ------------ apps/plugins/midi/synth.c | 36 ++++++++++++++++++------------------ apps/plugins/mpegplayer/attributes.h | 7 ------- firmware/export/system.h | 9 +++++++++ 5 files changed, 32 insertions(+), 42 deletions(-) diff --git a/apps/codecs/libtremor/codebook.c b/apps/codecs/libtremor/codebook.c index 8c319ab49e..5ffd0e308b 100644 --- a/apps/codecs/libtremor/codebook.c +++ b/apps/codecs/libtremor/codebook.c @@ -154,9 +154,9 @@ STIN long decode_packed_entry_number(codebook *book, long lo,hi; long lok = oggpack_look(b,book->dec_firsttablen); - if (EXPECT(lok >= 0, 1)) { + if (likely(lok >= 0)) { long entry = book->dec_firsttable[lok]; - if(EXPECT(entry&0x80000000UL, 0)){ + if(unlikely(entry&0x80000000UL)){ lo=(entry>>15)&0x7fff; hi=book->used_entries-(entry&0x7fff); }else{ @@ -218,7 +218,7 @@ static long decode_packed_block(codebook *book, oggpack_buffer *b, bitend = ((adr&3)+b->headend)*8; while (bufptrdec_maxlength, 0)) { + if (unlikely(cachesizedec_maxlength)) { if (bit-cachesize+32>=bitend) break; bit-=cachesize; @@ -230,13 +230,13 @@ static long decode_packed_block(codebook *book, oggpack_buffer *b, } entry=book->dec_firsttable[cache&((1<dec_firsttablen)-1)]; - if(EXPECT(entry&0x80000000UL, 0)){ + if(unlikely(entry&0x80000000UL)){ lo=(entry>>15)&0x7fff; hi=book->used_entries-(entry&0x7fff); { ogg_uint32_t testword=bitreverse((ogg_uint32_t)cache); - while(EXPECT(hi-lo>1, 1)){ + while(likely(hi-lo>1)){ long p=(hi-lo)>>1; if (book->codelist[lo+p]>testword) hi-=p; diff --git a/apps/codecs/libtremor/misc.h b/apps/codecs/libtremor/misc.h index 7d8b846381..e94236c2a8 100644 --- a/apps/codecs/libtremor/misc.h +++ b/apps/codecs/libtremor/misc.h @@ -275,17 +275,5 @@ static inline ogg_int32_t VFLOAT_ADD(ogg_int32_t a,ogg_int32_t ap, return(a); } -#ifdef __GNUC__ -#if __GNUC__ >= 3 -#define EXPECT(a, b) __builtin_expect((a), (b)) -#else -#define EXPECT(a, b) (a) -#endif -#else -#define EXPECT(a, b) (a) #endif -#endif - - - diff --git a/apps/plugins/midi/synth.c b/apps/plugins/midi/synth.c index 0819722030..bae6149860 100644 --- a/apps/plugins/midi/synth.c +++ b/apps/plugins/midi/synth.c @@ -296,7 +296,7 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i while(samples-- > 0) { /* Is voice being ramped? */ - if(so->state == STATE_RAMPDOWN) + if(unlikely(so->state == STATE_RAMPDOWN)) { if(so->decay != 0) /* Ramp has been started */ { @@ -318,12 +318,12 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i s2 = getSample((cp_temp >> FRACTSIZE)+1, wf); - if(mode_mask28) + if(likely(mode_mask28)) { /* LOOP_REVERSE|LOOP_PINGPONG = 24 */ - if(mode_mask24 && so->loopState == STATE_LOOPING && (cp_temp < start_loop)) + if(unlikely(mode_mask24 && so->loopState == STATE_LOOPING && (cp_temp < start_loop))) { - if(mode_mask_looprev) + if(unlikely(mode_mask_looprev)) { cp_temp += diff_loop; s2=getSample((cp_temp >> FRACTSIZE), wf); @@ -334,10 +334,10 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i } } - if(cp_temp >= end_loop) + if(unlikely(cp_temp >= end_loop)) { so->loopState = STATE_LOOPING; - if(!mode_mask24) + if(unlikely(!mode_mask24)) { cp_temp -= diff_loop; s2=getSample((cp_temp >> FRACTSIZE), wf); @@ -350,7 +350,7 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i } /* Have we overrun? */ - if(cp_temp >= num_samples) + if(unlikely(cp_temp >= num_samples)) { cp_temp -= so->delta; s2 = getSample((cp_temp >> FRACTSIZE)+1, wf); @@ -362,21 +362,21 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i s1 +=((signed)((s2 - s1) * (cp_temp & ((1<>FRACTSIZE); - if(so->curRate == 0) + if(unlikely(so->curRate == 0)) { stopVoice(so); // so->isUsed = false; } - if(so->ch != 9 && so->state != STATE_RAMPDOWN) /* Stupid ADSR code... and don't do ADSR for drums */ + if(likely(so->ch != 9 && so->state != STATE_RAMPDOWN)) /* Stupid ADSR code... and don't do ADSR for drums */ { - if(so->curOffset < so->targetOffset) + if(unlikely(so->curOffset < so->targetOffset)) { so->curOffset += (so->curRate); - if(so -> curOffset > so->targetOffset && so->curPoint != 2) + if(unlikely(so -> curOffset > so->targetOffset && so->curPoint != 2)) { - if(so->curPoint != 5) + if(unlikely(so->curPoint != 5)) { setPoint(so, so->curPoint+1); } @@ -388,10 +388,9 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i } else { so->curOffset -= (so->curRate); - if(so -> curOffset < so->targetOffset && so->curPoint != 2) + if(unlikely(so -> curOffset < so->targetOffset && so->curPoint != 2)) { - - if(so->curPoint != 5) + if(unlikely(so->curPoint != 5)) { setPoint(so, so->curPoint+1); } @@ -404,7 +403,7 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i } } - if(so->curOffset < 0) + if(unlikely(so->curOffset < 0)) { so->curOffset = so->targetOffset; stopVoice(so); @@ -417,7 +416,7 @@ static inline void synthVoice(struct SynthObject * so, int32_t * out, unsigned i s1 = s1 * volscale >> 14; /* need to set ramp beginning */ - if(so->state == STATE_RAMPDOWN && so->decay == 0) + if(unlikely(so->state == STATE_RAMPDOWN && so->decay == 0)) { so->decay = s1; if(so->decay == 0) @@ -443,7 +442,7 @@ int32_t samp_buf[512] IBSS_ATTR; void synthSamples(int32_t *buf_ptr, unsigned int num_samples) ICODE_ATTR; void synthSamples(int32_t *buf_ptr, unsigned int num_samples) { - if (num_samples > 512) + if (unlikely(num_samples > 512)) DEBUGF("num_samples is too big!\n"); else { @@ -463,6 +462,7 @@ void synthSamples(int32_t *buf_ptr, unsigned int num_samples) rb->memcpy(buf_ptr, samp_buf, num_samples*4); } + /* TODO: Automatic Gain Control, anyone? */ /* Or, should this be implemented on the DSP's output volume instead? */ diff --git a/apps/plugins/mpegplayer/attributes.h b/apps/plugins/mpegplayer/attributes.h index 9d708061c0..7ad83a3e6e 100644 --- a/apps/plugins/mpegplayer/attributes.h +++ b/apps/plugins/mpegplayer/attributes.h @@ -32,10 +32,3 @@ #define ATTR_ALIGN(align) #endif -#ifdef HAVE_BUILTIN_EXPECT -#define likely(x) __builtin_expect ((x) != 0, 1) -#define unlikely(x) __builtin_expect ((x) != 0, 0) -#else -#define likely(x) (x) -#define unlikely(x) (x) -#endif diff --git a/firmware/export/system.h b/firmware/export/system.h index 71422e17d6..cce7df6884 100644 --- a/firmware/export/system.h +++ b/firmware/export/system.h @@ -168,6 +168,15 @@ int get_cpu_boost_counter(void); #define TYPE_FROM_MEMBER(type, memberptr, membername) \ ((type *)((intptr_t)(memberptr) - OFFSETOF(type, membername))) +/* Use to give gcc hints on which branch is most likely taken */ +#if defined(__GNUC__) && __GNUC__ >= 3 +#define likely(x) __builtin_expect(!!(x), 1) +#define unlikely(x) __builtin_expect(!!(x), 0) +#else +#define likely(x) (x) +#define unlikely(x) (x) +#endif + /* returns index of first set bit + 1 or 0 if no bits are set */ int find_first_set_bit(uint32_t val); -- cgit v1.2.3