Thrill  0.1
ffs.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/math/ffs.hpp
3  *
4  * ffs() find first set bit in integer - mainly for portability as ffs() is a
5  * glibc extension and not available on Visual Studio.
6  *
7  * Part of tlx - http://panthema.net/tlx
8  *
9  * Copyright (C) 2007-2017 Timo Bingmann <[email protected]>
10  *
11  * All rights reserved. Published under the Boost Software License, Version 1.0
12  ******************************************************************************/
13 
14 #ifndef TLX_MATH_FFS_HEADER
15 #define TLX_MATH_FFS_HEADER
16 
17 namespace tlx {
18 
19 //! \addtogroup tlx_math
20 //! \{
21 
22 /******************************************************************************/
23 // ffs() - find first set bit in integer
24 
25 //! ffs (find first set bit) - generic implementation
26 template <typename Integral>
27 static inline unsigned ffs_template(Integral x) {
28  if (x == 0) return 0u;
29  unsigned r = 1;
30  while ((x & 1) == 0)
31  x >>= 1, ++r;
32  return r;
33 }
34 
35 /******************************************************************************/
36 
37 #if defined(__GNUC__) || defined(__clang__)
38 
39 //! find first set bit in integer, or zero if none are set.
40 static inline
41 unsigned ffs(int i) {
42  return static_cast<unsigned>(__builtin_ffs(i));
43 }
44 
45 //! find first set bit in integer, or zero if none are set.
46 static inline
47 unsigned ffs(unsigned i) {
48  return ffs(static_cast<int>(i));
49 }
50 
51 //! find first set bit in integer, or zero if none are set.
52 static inline
53 unsigned ffs(long i) {
54  return static_cast<unsigned>(__builtin_ffsl(i));
55 }
56 
57 //! find first set bit in integer, or zero if none are set.
58 static inline
59 unsigned ffs(unsigned long i) {
60  return ffs(static_cast<long>(i));
61 }
62 
63 //! find first set bit in integer, or zero if none are set.
64 static inline
65 unsigned ffs(long long i) {
66  return static_cast<unsigned>(__builtin_ffsll(i));
67 }
68 
69 //! find first set bit in integer, or zero if none are set.
70 static inline
71 unsigned ffs(unsigned long long i) {
72  return ffs(static_cast<long long>(i));
73 }
74 
75 #else
76 
77 //! find first set bit in integer, or zero if none are set.
78 static inline
79 unsigned ffs(int i) {
80  return ffs_template(i);
81 }
82 
83 //! find first set bit in integer, or zero if none are set.
84 static inline
85 unsigned ffs(unsigned int i) {
86  return ffs_template(i);
87 }
88 
89 //! find first set bit in integer, or zero if none are set.
90 static inline
91 unsigned ffs(long i) {
92  return ffs_template(i);
93 }
94 
95 //! find first set bit in integer, or zero if none are set.
96 static inline
97 unsigned ffs(unsigned long i) {
98  return ffs_template(i);
99 }
100 
101 //! find first set bit in integer, or zero if none are set.
102 static inline
103 unsigned ffs(long long i) {
104  return ffs_template(i);
105 }
106 
107 //! find first set bit in integer, or zero if none are set.
108 static inline
109 unsigned ffs(unsigned long long i) {
110  return ffs_template(i);
111 }
112 
113 #endif
114 
115 //! \}
116 
117 } // namespace tlx
118 
119 #endif // !TLX_MATH_FFS_HEADER
120 
121 /******************************************************************************/
static unsigned ffs(int i)
find first set bit in integer, or zero if none are set.
Definition: ffs.hpp:79
list x
Definition: gen_data.py:39
static unsigned ffs_template(Integral x)
ffs (find first set bit) - generic implementation
Definition: ffs.hpp:27