--- /dev/null
+#ifndef _BITS_STRINGS_H
+#define _BITS_STRINGS_H
+
+FILE_LICENCE ( GPL2_OR_LATER );
+
+/**
+ * Find last (i.e. most significant) set bit
+ *
+ * @v value Value
+ * @ret msb Most significant bit set in value (LSB=1), or zero
+ */
+static inline __attribute__ (( always_inline )) int __flsl ( long value ) {
+ long msb_minus_one;
+
+ /* If the input value is zero, the BSR instruction returns
+ * ZF=1 and leaves an undefined value in the output register.
+ * Perform this check in C rather than asm so that it can be
+ * omitted in cases where the compiler is able to prove that
+ * the input is non-zero.
+ */
+ if ( value ) {
+ __asm__ ( "bsrl %1, %0"
+ : "=r" ( msb_minus_one )
+ : "rm" ( value ) );
+ return ( msb_minus_one + 1 );
+ } else {
+ return 0;
+ }
+}
+
+/**
+ * Find last (i.e. most significant) set bit
+ *
+ * @v value Value
+ * @ret msb Most significant bit set in value (LSB=1), or zero
+ */
+static inline __attribute__ (( always_inline )) int __flsll ( long long value ){
+ unsigned long high = ( value >> 32 );
+ unsigned long low = ( value >> 0 );
+
+ if ( high ) {
+ return ( 32 + __flsl ( high ) );
+ } else if ( low ) {
+ return ( __flsl ( low ) );
+ } else {
+ return 0;
+ }
+}
+
+#endif /* _BITS_STRINGS_H */
* @v value Value
* @ret msb Most significant bit set in value (LSB=1), or zero
*/
-static inline __attribute__ (( always_inline )) int __flsl ( long value ) {
- long msb_minus_one;
+static inline __attribute__ (( always_inline )) int __flsll ( long long value ){
+ long long msb_minus_one;
/* If the input value is zero, the BSR instruction returns
* ZF=1 and leaves an undefined value in the output register.
* the input is non-zero.
*/
if ( value ) {
- __asm__ ( "bsr %1, %0"
+ __asm__ ( "bsrq %1, %0"
: "=r" ( msb_minus_one )
: "rm" ( value ) );
return ( msb_minus_one + 1 );
}
}
+/**
+ * Find last (i.e. most significant) set bit
+ *
+ * @v value Value
+ * @ret msb Most significant bit set in value (LSB=1), or zero
+ */
+static inline __attribute__ (( always_inline )) int __flsl ( long value ) {
+
+ return __flsll ( value );
+}
+
#endif /* _BITS_STRINGS_H */
#include <bits/strings.h>
static inline __attribute__ (( always_inline )) int
-__constant_flsl ( unsigned long x ) {
+__constant_flsll ( unsigned long long x ) {
int r = 0;
-#if ULONG_MAX > 0xffffffff
- if ( x & 0xffffffff00000000UL ) {
+ if ( x & 0xffffffff00000000ULL ) {
x >>= 32;
r += 32;
}
-#endif
if ( x & 0xffff0000UL ) {
x >>= 16;
r += 16;
return r;
}
+static inline __attribute__ (( always_inline )) int
+__constant_flsl ( unsigned long x ) {
+ return __constant_flsll ( x );
+}
+
+int __flsll ( long long x );
int __flsl ( long x );
+#define flsll( x ) \
+ ( __builtin_constant_p ( x ) ? __constant_flsll ( x ) : __flsll ( x ) )
+
#define flsl( x ) \
( __builtin_constant_p ( x ) ? __constant_flsl ( x ) : __flsl ( x ) )
return flsl ( value );
}
+/**
+ * Force a call to the non-constant implementation of flsll()
+ *
+ * @v value Value
+ * @ret msb Most significant bit set in value (LSB=1), or zero
+ */
+__attribute__ (( noinline )) int flsll_var ( long long value ) {
+ return flsll ( value );
+}
+
/**
* Check current stack pointer
*
}
#define flsl_ok( value, msb ) flsl_okx ( value, msb, __FILE__, __LINE__ )
+/**
+ * Report a flsll() test result
+ *
+ * @v value Value
+ * @v msb Expected MSB
+ * @v file Test code file
+ * @v line Test code line
+ */
+static inline __attribute__ (( always_inline )) void
+flsll_okx ( long long value, int msb, const char *file, unsigned int line ) {
+
+ /* Verify as a constant (requires to be inlined) */
+ okx ( flsll ( value ) == msb, file, line );
+
+ /* Verify as a non-constant */
+ okx ( flsll_var ( value ) == msb, file, line );
+}
+#define flsll_ok( value, msb ) flsll_okx ( value, msb, __FILE__, __LINE__ )
+
/**
* Report a 64-bit unsigned integer division test result
*
flsl_ok ( -1U, ( 8 * sizeof ( int ) ) );
flsl_ok ( -1UL, ( 8 * sizeof ( long ) ) );
+ /* Test flsll() */
+ flsll_ok ( 0, 0 );
+ flsll_ok ( 1, 1 );
+ flsll_ok ( 0x6d63623330ULL, 39 );
+ flsll_ok ( -1U, ( 8 * sizeof ( int ) ) );
+ flsll_ok ( -1UL, ( 8 * sizeof ( long ) ) );
+ flsll_ok ( -1ULL, ( 8 * sizeof ( long long ) ) );
+
/* Test 64-bit arithmetic
*
* On a 64-bit machine, these tests are fairly meaningless.