From c1d810d1d0c38ba14d3f7d21d381bcbda649a0fb Mon Sep 17 00:00:00 2001 From: Glenn Kasten Date: Thu, 15 Dec 2011 14:38:29 -0800 Subject: Replace loop by __builtin_ctz Using the builtin is faster on some platforms, for example on ARM it's 19 instructions instead of 13, and is O(1) instead of O(n). Of course, track creation is an inherently slow operation, so this doesn't matter much now. But if we add support for virtual tracks, then physical tracks will be allocated/freed more frequently. Also just on principle ... Change-Id: I3f590934092bd7a1869cbedbc7357928aa5cc8ff --- services/audioflinger/AudioMixer.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/services/audioflinger/AudioMixer.cpp b/services/audioflinger/AudioMixer.cpp index d994a87d88ea..fb1ca8766eb3 100644 --- a/services/audioflinger/AudioMixer.cpp +++ b/services/audioflinger/AudioMixer.cpp @@ -95,16 +95,11 @@ AudioMixer::~AudioMixer() int AudioMixer::getTrackName() { - uint32_t names = mTrackNames; - uint32_t mask = 1; - int n = 0; - while (names & mask) { - mask <<= 1; - n++; - } - if (mask) { + uint32_t names = ~mTrackNames; + if (names != 0) { + int n = __builtin_ctz(names); ALOGV("add track (%d)", n); - mTrackNames |= mask; + mTrackNames |= 1 << n; return TRACK0 + n; } return -1; -- cgit v1.2.3-59-g8ed1b