Let the compiler worry about implementing ffs(3).

It does at least as good a job as our old hand-written assembly anyway.

Change-Id: If7c4a1ac508bace0b71ee7b67808caa6eabf11d2
diff --git a/libc/Android.mk b/libc/Android.mk
index ee09087..b77e394 100644
--- a/libc/Android.mk
+++ b/libc/Android.mk
@@ -222,6 +222,7 @@
     bionic/__errno.cpp \
     bionic/eventfd_read.cpp \
     bionic/eventfd_write.cpp \
+    bionic/ffs.cpp \
     bionic/fork.cpp \
     bionic/futimens.cpp \
     bionic/getauxval.cpp \
diff --git a/libc/arch-arm/arm.mk b/libc/arch-arm/arm.mk
index da54cb8..dea303a 100644
--- a/libc/arch-arm/arm.mk
+++ b/libc/arch-arm/arm.mk
@@ -4,7 +4,6 @@
     arch-arm/bionic/clone.S \
     arch-arm/bionic/eabi.c \
     arch-arm/bionic/_exit_with_stack_teardown.S \
-    arch-arm/bionic/ffs.S \
     arch-arm/bionic/futex_arm.S \
     arch-arm/bionic/__get_sp.S \
     arch-arm/bionic/kill.S \
diff --git a/libc/arch-arm/bionic/ffs.S b/libc/arch-arm/bionic/ffs.S
deleted file mode 100644
index 701af8c..0000000
--- a/libc/arch-arm/bionic/ffs.S
+++ /dev/null
@@ -1,41 +0,0 @@
-/*	$NetBSD: ffs.S,v 1.5 2003/04/05 23:08:52 bjh21 Exp $	*/
-/*
- * Copyright (c) 2001 Christopher Gilbert
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. The name of the company nor the name of the author may be used to
- *    endorse or promote products derived from this software without specific
- *    prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <machine/asm.h>
-#include <machine/cpu-features.h>
-
-ENTRY(ffs)
-	/* Standard trick to isolate bottom bit in r0 or 0 if r0 = 0 on entry */
- 	rsb     r1, r0, #0
- 	ands    r0, r0, r1
-	clzne	r0, r0
-	rsbne	r0, r0, #32
-	bx		lr
-END(ffs)
diff --git a/libc/arch-mips/bionic/ffs.S b/libc/arch-mips/bionic/ffs.S
deleted file mode 100644
index ef38b2a..0000000
--- a/libc/arch-mips/bionic/ffs.S
+++ /dev/null
@@ -1,92 +0,0 @@
-/*	$NetBSD: ffs.S,v 1.5 2003/04/05 23:08:52 bjh21 Exp $	*/
-/*
- * Copyright (c) 2001 Christopher Gilbert
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. The name of the company nor the name of the author may be used to
- *    endorse or promote products derived from this software without specific
- *    prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
- * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-/*
- * ffs - find first set bit, this algorithm isolates the first set
- * bit, then multiplies the number by 0x0450fbaf which leaves the top
- * 6 bits as an index into the table.  This algorithm should be a win
- * over the checking each bit in turn as per the C compiled version.
- *
- * This is the ffs algorithm devised by d.seal and posted to comp.sys.arm on
- * 16 Feb 1994.
- */
-
-	.text
-
-	.ent	ffs
-	.type	ffs, @function
-	.global	ffs
-	.align	4
-	.set noreorder
-ffs:
-#if (__mips==32)
-	subu	$t0,$0,$a0
-	and	$a0,$t0
-	clz	$t0,$a0
-	li	$v0,32
-	j	$ra	
-	 subu	$v0,$t0
-#else
-/* Size of the ffs routine in bytes
- * This is used to index .L_ffs_table from $t9
- * FIXME: there must be a better way to do this
- */
-#define FFSSIZE	12*4
-	subu	$t0,$0,$a0
-	and	$a0,$t0
-	/*
-	 * now a0 has at most one set bit, call this X
-	 * if X = 0, all further instructions are skipped
-	 */
-	sll	$t0,$a0,4	/* t0 = X * 0x00000010 */
-	or	$a0,$t0		/* a0 = X * 0x00000011 */ 
-	sll	$t0,$a0,6	/* t0 = X * 0x00000440 */
-	or	$a0,$t0		/* a0 = X * 0x00000451 */
-	sll	$t0,$a0,16	/* t0 = X * 0x04510000 */
-	subu	$a0,$t0,$a0	/* a0 = X * 0x0450fbaf */
-	
-	/* now lookup in table indexed on top 6 bits of a0 */
-	srl	$a0,25
-	addu	$t9,$a0
-	j	$ra
-	 lbu	$v0,FFSSIZE($t9)
-.L_ffs_table:
-/*               0   1   2   3   4   5   6   7           */
-	.byte	 0,  1,  2, 13,  3,  7,  0, 14  /*  0- 7 */
-	.byte	 4,  0,  8,  0,  0,  0,  0, 15  /*  8-15 */
-	.byte	11,  5,  0,  0,  9,  0,  0, 26  /* 16-23 */
-	.byte	 0,  0,  0,  0,  0, 22, 28, 16  /* 24-31 */
-	.byte	32, 12,  6,  0,  0,  0,  0,  0	/* 32-39 */
-	.byte	10,  0,  0, 25,  0,  0, 21, 27  /* 40-47 */
-	.byte	31,  0,  0,  0,  0, 24,  0, 20  /* 48-55 */
-	.byte   30,  0, 23, 19, 29, 18, 17,  0  /* 56-63 */
-	
-#endif
-	.end	ffs
diff --git a/libc/arch-mips/mips.mk b/libc/arch-mips/mips.mk
index 5b7d8ee..3ebedd0 100644
--- a/libc/arch-mips/mips.mk
+++ b/libc/arch-mips/mips.mk
@@ -1,20 +1,19 @@
 _LIBC_ARCH_COMMON_SRC_FILES := \
-    arch-mips/bionic/__get_sp.S \
-    arch-mips/bionic/__set_tls.c \
-    arch-mips/bionic/_exit_with_stack_teardown.S \
-    arch-mips/bionic/_setjmp.S \
-    arch-mips/bionic/futex_mips.S \
     arch-mips/bionic/bzero.S \
     arch-mips/bionic/cacheflush.cpp \
     arch-mips/bionic/clone.S \
-    arch-mips/bionic/ffs.S \
+    arch-mips/bionic/_exit_with_stack_teardown.S \
+    arch-mips/bionic/futex_mips.S \
+    arch-mips/bionic/__get_sp.S \
     arch-mips/bionic/memcmp16.S \
     arch-mips/bionic/memmove.c \
+    arch-mips/bionic/_setjmp.S \
     arch-mips/bionic/setjmp.S \
+    arch-mips/bionic/__set_tls.c \
     arch-mips/bionic/sigsetjmp.S \
     arch-mips/bionic/vfork.S \
-    arch-mips/string/memset.S \
     arch-mips/string/memcpy.S \
+    arch-mips/string/memset.S \
     arch-mips/string/mips_strlen.c \
 
 _LIBC_ARCH_STATIC_SRC_FILES := \
diff --git a/libc/arch-x86/string/ffs.S b/libc/arch-x86/string/ffs.S
deleted file mode 100644
index d819306..0000000
--- a/libc/arch-x86/string/ffs.S
+++ /dev/null
@@ -1,18 +0,0 @@
-/*	$OpenBSD: ffs.S,v 1.3 2005/08/07 11:30:38 espie Exp $ */
-/*
- * Written by J.T. Conklin <jtc@netbsd.org>.
- * Public domain.
- */
-
-#include <machine/asm.h>
-
-ENTRY(ffs)
-	bsfl	4(%esp),%eax
-	jz	L1	 		/* ZF is set if all bits are 0 */
-	incl	%eax			/* bits numbered from 1, not 0 */
-	ret
-
-	.align 2
-L1:	xorl	%eax,%eax		/* clear result */
-	ret
-END(ffs)
diff --git a/libc/arch-x86/x86.mk b/libc/arch-x86/x86.mk
index 4a63f5e..5911ad9 100644
--- a/libc/arch-x86/x86.mk
+++ b/libc/arch-x86/x86.mk
@@ -9,7 +9,6 @@
     arch-x86/bionic/sigsetjmp.S \
     arch-x86/bionic/syscall.S \
     arch-x86/bionic/vfork.S \
-    arch-x86/string/ffs.S
 
 ifeq ($(ARCH_X86_HAVE_SSSE3),true)
 _LIBC_ARCH_COMMON_SRC_FILES += \
diff --git a/libc/arch-x86_64/string/ffs.S b/libc/arch-x86_64/string/ffs.S
deleted file mode 100644
index 2666bc4..0000000
--- a/libc/arch-x86_64/string/ffs.S
+++ /dev/null
@@ -1,18 +0,0 @@
-/*	$OpenBSD: ffs.S,v 1.1 2004/01/28 01:44:45 mickey Exp $	*/
-/*
- * Written by J.T. Conklin <jtc@netbsd.org>.
- * Public domain.
- * Adapted for NetBSD/x86_64 by Frank van der Linden <fvdl@wasabisystems.com>
- */
-
-#include <machine/asm.h>
-
-ENTRY(ffs)
-	bsfl	%edi,%eax
-	jz	L1	 		/* ZF is set if all bits are 0 */
-	incl	%eax			/* bits numbered from 1, not 0 */
-	ret
-
-	_ALIGN_TEXT
-L1:	xorl	%eax,%eax		/* clear result */
-	ret
diff --git a/libc/arch-x86_64/x86_64.mk b/libc/arch-x86_64/x86_64.mk
index 212eebe..4f833c1 100644
--- a/libc/arch-x86_64/x86_64.mk
+++ b/libc/arch-x86_64/x86_64.mk
@@ -10,7 +10,6 @@
     arch-x86_64/bionic/sigsetjmp.S \
     arch-x86_64/bionic/syscall.S \
     arch-x86_64/bionic/vfork.S \
-    arch-x86_64/string/ffs.S \
     string/memcmp16.c \
 
 _LIBC_ARCH_STATIC_SRC_FILES := \
diff --git a/libc/bionic/ffs.cpp b/libc/bionic/ffs.cpp
new file mode 100644
index 0000000..b2270e5
--- /dev/null
+++ b/libc/bionic/ffs.cpp
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *  * Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ *  * Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in
+ *    the documentation and/or other materials provided with the
+ *    distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <strings.h>
+
+int ffs(int x) {
+  return __builtin_ffs(x);
+}