2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2014 Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_BITOPS_H_
18 #define incl_HPHP_BITOPS_H_
22 // GLIBC doesn't provide an fls primitive. Since we're rolling our own
23 // anyway, fix ffs's wacky offset-by-one historical implementation. These
24 // guys return success/failure (failure for input of all zeros) and the
25 // unoffset bit position in their reference param.
26 template<typename I64
>
27 inline bool ffs64(I64 input
, I64
&out
) {
29 #if defined(__x86_64__)
31 "bsfq %2, %1\n\t" // bit scan forward
32 "setnz %0\n\t": // zero retval if input == 0
33 "=r"(retval
), "=r"(out
):
37 #elif defined(__AARCH64EL__)
39 "rbit %2, %2\n\t" // reverse bits
40 "clz %1, %2\n\t" // count leading zeros
42 "cset %0, NE": // return (result != 64)
43 "=r"(retval
), "=r"(out
), "+r"(input
):
51 template<typename I64
>
52 inline bool fls64(I64 input
, I64
&out
) {
54 #if defined(__x86_64__)
56 "bsrq %2, %1\n\t" // bit scan reverse
57 "setnz %0\n\t": // zero retval if input == 0
58 "=r"(retval
), "=r"(out
):
62 #elif defined(__AARCH64EL__)
64 "clz %1, %2\n\t" // count leading zeros
66 "adds %1, %1, #63\n\t" // result = 63 - (# of leading zeros)
67 // "s" suffix sets condition flags
68 "cset %0, PL": // return (result >= 0)
69 // because result < 0 iff input == 0
70 "=r"(retval
), "=r"(out
):