Add ffsl(3), ffsll(3).

POSIX added these GNU extensions for issue 8.

I've made these always inline without the usual "until API level X"
proviso because they're single instructions that the compiler can inline
and there's really no point providing these if they add function call
overhead --- everyone should just use __builtin_ffs() and friends
instead in that case.

Bug: https://austingroupbugs.net/view.php?id=617
Test: treehugger
Change-Id: I33fc4b8648ea25917329e81c1b4c60eb9a66d667
This commit is contained in:
Elliott Hughes 2020-03-25 10:34:46 -07:00
parent 8d1849930f
commit 4a6899ce0c
6 changed files with 86 additions and 55 deletions

View file

@ -26,8 +26,5 @@
* SUCH DAMAGE.
*/
#define __BIONIC_STRINGS_INLINE /* Out of line. */
#include <strings.h>
int ffs(int x) {
return __builtin_ffs(x);
}

View file

@ -1,44 +0,0 @@
/*
* Copyright (C) 2017 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.
*/
#pragma once
#include <sys/cdefs.h>
#if defined(__i386__) && __ANDROID_API__ < 18
#include <strings.h>
__BEGIN_DECLS
/* Everyone except x86 had ffs since the beginning. */
static __inline int ffs(int __n) { return __builtin_ffs(__n); }
__END_DECLS
#endif

View file

@ -49,6 +49,10 @@
#include <bits/strcasecmp.h>
#if !defined(__BIONIC_STRINGS_INLINE)
#define __BIONIC_STRINGS_INLINE static __inline
#endif
__BEGIN_DECLS
/** Deprecated. Use memmove() instead. */
@ -63,19 +67,41 @@ static __inline__ __always_inline void __bionic_bzero(void* b, size_t len) {
__builtin_memset(b, 0, len);
}
#if !defined(__i386__) || __ANDROID_API__ >= 18
/**
* [ffs(3)](http://man7.org/linux/man-pages/man3/ffs.3.html) finds the first set bit in `__i`.
* [ffs(3)](http://man7.org/linux/man-pages/man3/ffs.3.html) finds the
* first set bit in `__n`.
*
* Returns 0 if no bit is set, or the index of the lowest set bit (counting from 1) otherwise.
* Returns 0 if no bit is set, or the index of the lowest set bit (counting
* from 1) otherwise.
*/
int ffs(int __i) __INTRODUCED_IN_X86(18);
#endif
__BIONIC_STRINGS_INLINE int ffs(int __n) {
return __builtin_ffs(__n);
}
/**
* [ffsl(3)](http://man7.org/linux/man-pages/man3/ffsl.3.html) finds the
* first set bit in `__n`.
*
* Returns 0 if no bit is set, or the index of the lowest set bit (counting
* from 1) otherwise.
*/
__BIONIC_STRINGS_INLINE int ffsl(long __n) {
return __builtin_ffsl(__n);
}
/**
* [ffsll(3)](http://man7.org/linux/man-pages/man3/ffsll.3.html) finds the
* first set bit in `__n`.
*
* Returns 0 if no bit is set, or the index of the lowest set bit (counting
* from 1) otherwise.
*/
__BIONIC_STRINGS_INLINE int ffsll(long long __n) {
return __builtin_ffsll(__n);
}
#if defined(__BIONIC_INCLUDE_FORTIFY_HEADERS)
#include <bits/fortify/strings.h>
#endif
__END_DECLS
#include <android/legacy_strings_inlines.h>

View file

@ -1550,6 +1550,12 @@ LIBC_R { # introduced=R
_Unwind_VRS_Set; # apex llndk arm
} LIBC_Q;
LIBC_S { # introduced=31
global:
ffsl;
ffsll;
} LIBC_R;
LIBC_PRIVATE {
global:
__accept4; # arm x86

View file

@ -32,6 +32,10 @@
static void strings_h() {
FUNCTION(ffs, int (*f)(int));
#if !defined(__GLIBC__)
FUNCTION(ffsl, int (*f)(long));
FUNCTION(ffsll, int (*f)(long long));
#endif
FUNCTION(strcasecmp, int (*f)(const char*, const char*));
FUNCTION(strcasecmp_l, int (*f)(const char*, const char*, locale_t));
FUNCTION(strncasecmp, int (*f)(const char*, const char*, size_t));

View file

@ -38,6 +38,48 @@ TEST(STRINGS_TEST, ffs) {
ASSERT_EQ(32, ffs(0x80000000));
}
TEST(STRINGS_TEST, ffsl) {
ASSERT_EQ( 0, ffsl(0x00000000L));
ASSERT_EQ( 1, ffsl(0x00000001L));
ASSERT_EQ( 6, ffsl(0x00000020L));
ASSERT_EQ(11, ffsl(0x00000400L));
ASSERT_EQ(16, ffsl(0x00008000L));
ASSERT_EQ(17, ffsl(0x00010000L));
ASSERT_EQ(22, ffsl(0x00200000L));
ASSERT_EQ(27, ffsl(0x04000000L));
ASSERT_EQ(32, ffsl(0x80000000L));
#if defined(__LP64__)
ASSERT_EQ(33, ffsl(0x0000000100000000L));
ASSERT_EQ(38, ffsl(0x0000002000000000L));
ASSERT_EQ(43, ffsl(0x0000040000000000L));
ASSERT_EQ(48, ffsl(0x0000800000000000L));
ASSERT_EQ(49, ffsl(0x0001000000000000L));
ASSERT_EQ(54, ffsl(0x0020000000000000L));
ASSERT_EQ(59, ffsl(0x0400000000000000L));
ASSERT_EQ(64, ffsl(0x8000000000000000L));
#endif
}
TEST(STRINGS_TEST, ffsll) {
ASSERT_EQ( 0, ffsll(0x0000000000000000LL));
ASSERT_EQ( 1, ffsll(0x0000000000000001LL));
ASSERT_EQ( 6, ffsll(0x0000000000000020LL));
ASSERT_EQ(11, ffsll(0x0000000000000400LL));
ASSERT_EQ(16, ffsll(0x0000000000008000LL));
ASSERT_EQ(17, ffsll(0x0000000000010000LL));
ASSERT_EQ(22, ffsll(0x0000000000200000LL));
ASSERT_EQ(27, ffsll(0x0000000004000000LL));
ASSERT_EQ(32, ffsll(0x0000000080000000LL));
ASSERT_EQ(33, ffsll(0x0000000100000000LL));
ASSERT_EQ(38, ffsll(0x0000002000000000LL));
ASSERT_EQ(43, ffsll(0x0000040000000000LL));
ASSERT_EQ(48, ffsll(0x0000800000000000LL));
ASSERT_EQ(49, ffsll(0x0001000000000000LL));
ASSERT_EQ(54, ffsll(0x0020000000000000LL));
ASSERT_EQ(59, ffsll(0x0400000000000000LL));
ASSERT_EQ(64, ffsll(0x8000000000000000LL));
}
TEST(STRINGS_TEST, strcasecmp) {
ASSERT_EQ(0, strcasecmp("hello", "HELLO"));
ASSERT_LT(strcasecmp("hello1", "hello2"), 0);