Revert "Improve portability of <system/audio.h> to non-Android platforms"

This reverts commit a5dfd03383420c06fc05e667eb1dfd1e2842300f.

Reason for revert: broke build

Change-Id: Ib9a629ccafd0f871cebe15ddad2c0c9dce0c62b7
diff --git a/audio/include/system/audio.h b/audio/include/system/audio.h
index 4bc778f..c32a85f 100644
--- a/audio/include/system/audio.h
+++ b/audio/include/system/audio.h
@@ -25,6 +25,8 @@
 #include <sys/cdefs.h>
 #include <sys/types.h>
 
+#include <cutils/bitops.h>
+
 #include "audio-base.h"
 #include "audio-base-utils.h"
 
@@ -420,7 +422,7 @@
     int                  values[sizeof(audio_channel_mask_t) * 8]; /* gain values in millibels
                                                for each channel ordered from LSb to MSb in
                                                channel mask. The number of values is 1 in joint
-                                               mode or __builtin_popcount(channel_mask) */
+                                               mode or popcount(channel_mask) */
     unsigned int         ramp_duration_ms; /* ramp duration in ms */
 };
 
@@ -858,7 +860,7 @@
         bits &= AUDIO_CHANNEL_IN_ALL;
         FALLTHROUGH_INTENDED;
     case AUDIO_CHANNEL_REPRESENTATION_INDEX:
-        return __builtin_popcount(bits);
+        return popcount(bits);
     default:
         return 0;
     }
@@ -879,7 +881,7 @@
         bits &= AUDIO_CHANNEL_OUT_ALL;
         FALLTHROUGH_INTENDED;
     case AUDIO_CHANNEL_REPRESENTATION_INDEX:
-        return __builtin_popcount(bits);
+        return popcount(bits);
     default:
         return 0;
     }
@@ -1305,7 +1307,7 @@
     case AUDIO_GAIN_MODE_CHANNELS:
     case AUDIO_GAIN_MODE_RAMP:
         if (lhs->channel_mask != rhs->channel_mask) return false;
-        for (int i = 0; i < __builtin_popcount(lhs->channel_mask); ++i) {
+        for (int i = 0; i < popcount(lhs->channel_mask); ++i) {
             if (lhs->values[i] != rhs->values[i]) return false;
         }
         break;
diff --git a/audio_utils/primitives.c b/audio_utils/primitives.c
index dac2394..5bf40af 100644
--- a/audio_utils/primitives.c
+++ b/audio_utils/primitives.c
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <cutils/bitops.h>  /* for popcount() */
 #include <audio_utils/primitives.h>
 #include <string.h>
 #include "private/private.h"
@@ -428,14 +429,14 @@
 #if 0
     /* alternate way of handling memcpy_by_channel_mask by using the idxary */
     int8_t idxary[32];
-    uint32_t src_channels = __builtin_popcount(src_mask);
+    uint32_t src_channels = popcount(src_mask);
     uint32_t dst_channels =
             memcpy_by_index_array_initialization(idxary, 32, dst_mask, src_mask);
 
     memcpy_by_idxary(dst, dst_channels, src, src_channels, idxary, sample_size, count);
 #else
     if (dst_mask == src_mask) {
-        memcpy(dst, src, sample_size * __builtin_popcount(dst_mask) * count);
+        memcpy(dst, src, sample_size * popcount(dst_mask) * count);
         return;
     }
     switch (sample_size) {
@@ -543,12 +544,12 @@
             idxary[n++] = -1;
         }
     }
-    return n + __builtin_popcount(ormask & dst_mask);
+    return n + popcount(ormask & dst_mask);
 }
 
 size_t memcpy_by_index_array_initialization_src_index(int8_t *idxary, size_t idxcount,
         uint32_t dst_mask, uint32_t src_mask) {
-    size_t dst_count = __builtin_popcount(dst_mask);
+    size_t dst_count = popcount(dst_mask);
     if (idxcount == 0) {
         return dst_count;
     }
diff --git a/audio_utils/tests/primitives_tests.cpp b/audio_utils/tests/primitives_tests.cpp
index 46fb8ff..4389daa 100644
--- a/audio_utils/tests/primitives_tests.cpp
+++ b/audio_utils/tests/primitives_tests.cpp
@@ -374,7 +374,7 @@
     dst_mask = 0x8d;
     memset(u16ary, 0x99, 65536 * sizeof(u16ref[0]));
     memcpy_by_channel_mask(u16ary, dst_mask, u16ref, src_mask, sizeof(u16ref[0]),
-            65536 / __builtin_popcount(dst_mask));
+            65536 / popcount(dst_mask));
     EXPECT_EQ((size_t)0, nonZeroMono16((int16_t*)u16ary, 65530));
 
     // Test when dst_mask is 0.  Nothing should be copied.
@@ -389,7 +389,7 @@
     src_mask = dst_mask = 0x8d;
     memset(u16ary, 0x99, 65536 * sizeof(u16ref[0]));
     memcpy_by_channel_mask(u16ary, dst_mask, u16ref, src_mask, sizeof(u16ref[0]), 555);
-    EXPECT_EQ(0, memcmp(u16ary, u16ref, 555 * sizeof(u16ref[0]) * __builtin_popcount(dst_mask)));
+    EXPECT_EQ(0, memcmp(u16ary, u16ref, 555 * sizeof(u16ref[0]) * popcount(dst_mask)));
 
     // Test with a gap in source:
     // Input 3 samples, output 4 samples, one zero inserted.
@@ -397,7 +397,7 @@
     dst_mask = 0x8d;
     memset(u16ary, 0x9, 65536 * sizeof(u16ary[0]));
     memcpy_by_channel_mask(u16ary, dst_mask, u16ref, src_mask, sizeof(u16ref[0]),
-            65536 / __builtin_popcount(dst_mask));
+            65536 / popcount(dst_mask));
     checkMonotoneOrZero(u16ary, 65536);
     EXPECT_EQ((size_t)(65536 * 3 / 4 - 1), nonZeroMono16((int16_t*)u16ary, 65536));
 
@@ -407,7 +407,7 @@
     dst_mask = 0x8c;
     memset(u16ary, 0x9, 65536 * sizeof(u16ary[0]));
     memcpy_by_channel_mask(u16ary, dst_mask, u16ref, src_mask, sizeof(u16ref[0]),
-            65536 / __builtin_popcount(src_mask));
+            65536 / popcount(src_mask));
     checkMonotone(u16ary, 65536 * 3 / 4);
 
     delete[] u16ref;
@@ -418,7 +418,7 @@
         const void *src, uint32_t src_mask, size_t sample_size, size_t count)
 {
     int8_t idxary[32];
-    uint32_t src_channels = __builtin_popcount(src_mask);
+    uint32_t src_channels = popcount(src_mask);
     uint32_t dst_channels =
             memcpy_by_index_array_initialization(idxary, 32, dst_mask, src_mask);
 
@@ -452,7 +452,7 @@
     dst_mask = 0x8d;
     memset(u24ary, 0x99, 65536 * sizeof(u24ary[0]));
     memcpy_by_channel_mask2(u24ary, dst_mask, u24ref, src_mask, sizeof(u24ref[0]),
-            65536 / __builtin_popcount(dst_mask));
+            65536 / popcount(dst_mask));
     memcpy_to_i16_from_p24((int16_t*)u16ary, (uint8_t*)u24ary, 65536);
     EXPECT_EQ((size_t)0, nonZeroMono16((int16_t*)u16ary, 65530));
 
@@ -469,7 +469,7 @@
     src_mask = dst_mask = 0x8d;
     memset(u24ary, 0x99, 65536 * sizeof(u24ary[0]));
     memcpy_by_channel_mask2(u24ary, dst_mask, u24ref, src_mask, sizeof(u24ref[0]), 555);
-    EXPECT_EQ(0, memcmp(u24ary, u24ref, 555 * sizeof(u24ref[0]) * __builtin_popcount(dst_mask)));
+    EXPECT_EQ(0, memcmp(u24ary, u24ref, 555 * sizeof(u24ref[0]) * popcount(dst_mask)));
 
     // Test with a gap in source:
     // Input 3 samples, output 4 samples, one zero inserted.
@@ -477,7 +477,7 @@
     dst_mask = 0x8d;
     memset(u24ary, 0x9, 65536 * sizeof(u24ary[0]));
     memcpy_by_channel_mask2(u24ary, dst_mask, u24ref, src_mask, sizeof(u24ref[0]),
-            65536 / __builtin_popcount(dst_mask));
+            65536 / popcount(dst_mask));
     memcpy_to_i16_from_p24((int16_t*)u16ary, (uint8_t*)u24ary, 65536);
     checkMonotoneOrZero(u16ary, 65536);
     EXPECT_EQ((size_t)(65536 * 3 / 4 - 1), nonZeroMono16((int16_t*)u16ary, 65536));
@@ -488,7 +488,7 @@
     dst_mask = 0x8c;
     memset(u24ary, 0x9, 65536 * sizeof(u24ary[0]));
     memcpy_by_channel_mask2(u24ary, dst_mask, u24ref, src_mask, sizeof(u24ref[0]),
-            65536 / __builtin_popcount(src_mask));
+            65536 / popcount(src_mask));
     memcpy_to_i16_from_p24((int16_t*)u16ary, (uint8_t*)u24ary, 65536);
     checkMonotone(u16ary, 65536 * 3 / 4);
 
@@ -502,7 +502,7 @@
         const void *src, uint32_t src_mask, size_t sample_size, size_t count)
 {
     int8_t idxary[32];
-    uint32_t src_channels = __builtin_popcount(src_mask);
+    uint32_t src_channels = popcount(src_mask);
     uint32_t dst_channels =
             memcpy_by_index_array_initialization_dst_index(idxary, 32, dst_mask, src_mask);
 
@@ -536,7 +536,7 @@
     dst_mask = 0x8d;
     memset(u24ary, 0x99, 65536 * sizeof(u24ary[0]));
     memcpy_by_channel_mask_dst_index(u24ary, dst_mask, u24ref, src_mask, sizeof(u24ref[0]),
-            65536 / __builtin_popcount(dst_mask));
+            65536 / popcount(dst_mask));
     memcpy_to_i16_from_p24((int16_t*)u16ary, (uint8_t*)u24ary, 65536);
     EXPECT_EQ((size_t)0, nonZeroMono16((int16_t*)u16ary, 65530));
 
@@ -554,7 +554,7 @@
     dst_mask = 0x0f;
     memset(u24ary, 0x99, 65536 * sizeof(u24ary[0]));
     memcpy_by_channel_mask_dst_index(u24ary, dst_mask, u24ref, src_mask, sizeof(u24ref[0]), 555);
-    EXPECT_EQ(0, memcmp(u24ary, u24ref, 555 * sizeof(u24ref[0]) * __builtin_popcount(dst_mask)));
+    EXPECT_EQ(0, memcmp(u24ary, u24ref, 555 * sizeof(u24ref[0]) * popcount(dst_mask)));
 
     // Test with a gap in source:
     // Input 3 samples, output 4 samples, one zero inserted.
@@ -562,7 +562,7 @@
     dst_mask = 0x0f;
     memset(u24ary, 0x9, 65536 * sizeof(u24ary[0]));
     memcpy_by_channel_mask_dst_index(u24ary, dst_mask, u24ref, src_mask, sizeof(u24ref[0]),
-            65536 / __builtin_popcount(dst_mask));
+            65536 / popcount(dst_mask));
     memcpy_to_i16_from_p24((int16_t*)u16ary, (uint8_t*)u24ary, 65536);
     checkMonotoneOrZero(u16ary, 65536);
     EXPECT_EQ((size_t)(65536 * 3 / 4 - 1), nonZeroMono16((int16_t*)u16ary, 65536));
@@ -573,7 +573,7 @@
     dst_mask = 0x07;
     memset(u24ary, 0x9, 65536 * sizeof(u24ary[0]));
     memcpy_by_channel_mask_dst_index(u24ary, dst_mask, u24ref, src_mask, sizeof(u24ref[0]),
-            65536 / __builtin_popcount(src_mask));
+            65536 / popcount(src_mask));
     memcpy_to_i16_from_p24((int16_t*)u16ary, (uint8_t*)u24ary, 65536);
     checkMonotone(u16ary, 65536 * 3 / 4);
 
@@ -587,7 +587,7 @@
         const void *src, uint32_t src_mask, size_t sample_size, size_t count)
 {
     int8_t idxary[32];
-    uint32_t src_channels = __builtin_popcount(src_mask);
+    uint32_t src_channels = popcount(src_mask);
     uint32_t dst_channels =
             memcpy_by_index_array_initialization_src_index(idxary, 32, dst_mask, src_mask);
 
@@ -621,7 +621,7 @@
     dst_mask = 0x8d;
     memset(u24ary, 0x99, 65536 * sizeof(u24ary[0]));
     memcpy_by_channel_mask_src_index(u24ary, dst_mask, u24ref, src_mask, sizeof(u24ref[0]),
-            65536 / __builtin_popcount(dst_mask));
+            65536 / popcount(dst_mask));
     memcpy_to_i16_from_p24((int16_t*)u16ary, (uint8_t*)u24ary, 65536);
     EXPECT_EQ((size_t)0, nonZeroMono16((int16_t*)u16ary, 65530));
 
@@ -639,14 +639,14 @@
     dst_mask = 0xf;
     memset(u24ary, 0x99, 65536 * sizeof(u24ary[0]));
     memcpy_by_channel_mask_src_index(u24ary, dst_mask, u24ref, src_mask, sizeof(u24ref[0]), 555);
-    EXPECT_EQ(0, memcmp(u24ary, u24ref, 555 * sizeof(u24ref[0]) * __builtin_popcount(dst_mask)));
+    EXPECT_EQ(0, memcmp(u24ary, u24ref, 555 * sizeof(u24ref[0]) * popcount(dst_mask)));
 
     // Test when source mask must copy to dst mask.  One to one copy.
     src_mask = 0xf;
     dst_mask = 0x8d;
     memset(u24ary, 0x99, 65536 * sizeof(u24ary[0]));
     memcpy_by_channel_mask_src_index(u24ary, dst_mask, u24ref, src_mask, sizeof(u24ref[0]), 555);
-    EXPECT_EQ(0, memcmp(u24ary, u24ref, 555 * sizeof(u24ref[0]) * __builtin_popcount(dst_mask)));
+    EXPECT_EQ(0, memcmp(u24ary, u24ref, 555 * sizeof(u24ref[0]) * popcount(dst_mask)));
 
     // Test with a gap in source:
     // Input 3 samples, output 4 samples, one zero inserted.
@@ -654,7 +654,7 @@
     dst_mask = 0x8d;
     memset(u24ary, 0x9, 65536 * sizeof(u24ary[0]));
     memcpy_by_channel_mask_src_index(u24ary, dst_mask, u24ref, src_mask, sizeof(u24ref[0]),
-            65536 / __builtin_popcount(dst_mask));
+            65536 / popcount(dst_mask));
     memcpy_to_i16_from_p24((int16_t*)u16ary, (uint8_t*)u24ary, 65536);
     checkMonotoneOrZero(u16ary, 65536);
     EXPECT_EQ((size_t)(65536 * 3 / 4 - 1), nonZeroMono16((int16_t*)u16ary, 65536));
@@ -665,7 +665,7 @@
     dst_mask = 0x8c;
     memset(u24ary, 0x9, 65536 * sizeof(u24ary[0]));
     memcpy_by_channel_mask_src_index(u24ary, dst_mask, u24ref, src_mask, sizeof(u24ref[0]),
-            65536 / __builtin_popcount(src_mask));
+            65536 / popcount(src_mask));
     memcpy_to_i16_from_p24((int16_t*)u16ary, (uint8_t*)u24ary, 65536);
     checkMonotone(u16ary, 65536 * 3 / 4);