aboutsummaryrefslogtreecommitdiffstats
path: root/lib/compiler-rt/builtins/popcountsi2.c
diff options
context:
space:
mode:
authorLionel Debieve <lionel.debieve@st.com>2019-10-02 09:52:11 +0200
committerLionel Debieve <lionel.debieve@st.com>2020-01-20 11:32:59 +0100
commit162fc183cf439efe029a581fbde8e4f936815f6d (patch)
tree80818142ca372089f24ee78a33698089458e824d /lib/compiler-rt/builtins/popcountsi2.c
parenta13550d0ec5d5ad5035bb7120d8ddac4a4fb2113 (diff)
downloadplatform_external_arm-trusted-firmware-162fc183cf439efe029a581fbde8e4f936815f6d.tar.gz
platform_external_arm-trusted-firmware-162fc183cf439efe029a581fbde8e4f936815f6d.tar.bz2
platform_external_arm-trusted-firmware-162fc183cf439efe029a581fbde8e4f936815f6d.zip
compiler_rt: Import popcountdi2.c and popcountsi2.c files
Imported from the LLVM compiler_rt library on master branch as of 30 Oct 2018 (SVN revision: r345645). This is to get the __popcountsi2(si_int a) and __popcountdi2(di_int a) builtin which are required by a driver that uses a __builtin_popcount(). Change-Id: I8e0d97cebdd90d224690c8ce1b02e657acdddb25 Signed-off-by: Lionel Debieve <lionel.debieve@st.com>
Diffstat (limited to 'lib/compiler-rt/builtins/popcountsi2.c')
-rw-r--r--lib/compiler-rt/builtins/popcountsi2.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/lib/compiler-rt/builtins/popcountsi2.c b/lib/compiler-rt/builtins/popcountsi2.c
new file mode 100644
index 000000000..44544ff49
--- /dev/null
+++ b/lib/compiler-rt/builtins/popcountsi2.c
@@ -0,0 +1,33 @@
+/* ===-- popcountsi2.c - Implement __popcountsi2 ---------------------------===
+ *
+ * The LLVM Compiler Infrastructure
+ *
+ * This file is dual licensed under the MIT and the University of Illinois Open
+ * Source Licenses. See LICENSE.TXT for details.
+ *
+ * ===----------------------------------------------------------------------===
+ *
+ * This file implements __popcountsi2 for the compiler_rt library.
+ *
+ * ===----------------------------------------------------------------------===
+ */
+
+#include "int_lib.h"
+
+/* Returns: count of 1 bits */
+
+COMPILER_RT_ABI si_int
+__popcountsi2(si_int a)
+{
+ su_int x = (su_int)a;
+ x = x - ((x >> 1) & 0x55555555);
+ /* Every 2 bits holds the sum of every pair of bits */
+ x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
+ /* Every 4 bits holds the sum of every 4-set of bits (3 significant bits) */
+ x = (x + (x >> 4)) & 0x0F0F0F0F;
+ /* Every 8 bits holds the sum of every 8-set of bits (4 significant bits) */
+ x = (x + (x >> 16));
+ /* The lower 16 bits hold two 8 bit sums (5 significant bits).*/
+ /* Upper 16 bits are garbage */
+ return (x + (x >> 8)) & 0x0000003F; /* (6 significant bits) */
+}