1 #if !defined HAVE_BITHIGH_EDGE_H__
2 #define      HAVE_BITHIGH_EDGE_H__
3 // This file is part of the FXT library.
4 // Copyright (C) 2010, 2012 Joerg Arndt
5 // License: GNU General Public License version 3 or later,
6 // see the file COPYING.txt in the main directory.
7 
8 #include "fxttypes.h"
9 #include "bits/bitsperlong.h"
10 #include "bits/bitasm.h"
11 
12 
highest_one_01edge(ulong x)13 static inline ulong highest_one_01edge(ulong x)
14 // Return word where all bits from (including) the
15 //   highest set bit to bit 0 are set.
16 // Return 0 if no bit is set.
17 //
18 // Feed the result into bit_count() to get
19 //   the index of the highest bit set.
20 {
21 #if defined  BITS_USE_ASM
22 
23     if ( 0==x )  return 0;
24     x = asm_bsr(x);
25     return  (2UL<<x) - 1;
26 
27 #else  // BITS_USE_ASM
28 
29     x |= x>>1;
30     x |= x>>2;
31     x |= x>>4;
32     x |= x>>8;
33     x |= x>>16;
34 #if  BITS_PER_LONG >= 64
35     x |= x>>32;
36 #endif
37     return  x;
38 #endif  // BITS_USE_ASM
39 }
40 // -------------------------
41 
highest_one_10edge(ulong x)42 static inline ulong highest_one_10edge(ulong x)
43 // Return word where all bits from  (including) the
44 //   highest set bit to most significant bit are set.
45 // Return 0 if no bit is set.
46 {
47     if ( 0==x )  return 0;
48     x = highest_one_01edge(x);
49     return  ~(x>>1);
50 }
51 // -------------------------
52 
53 
54 #endif  // !defined HAVE_BITHIGH_EDGE_H__
55