Chris@16
|
1 ///////////////////////////////////////////////////////////////
|
Chris@16
|
2 // Copyright 2013 John Maddock. Distributed under the Boost
|
Chris@16
|
3 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
|
Chris@16
|
5 //
|
Chris@16
|
6 // Comparison operators for cpp_int_backend:
|
Chris@16
|
7 //
|
Chris@16
|
8 #ifndef BOOST_MP_DETAIL_BITSCAN_HPP
|
Chris@16
|
9 #define BOOST_MP_DETAIL_BITSCAN_HPP
|
Chris@16
|
10
|
Chris@101
|
11 #if defined(BOOST_MSVC) && (defined(_M_IX86) || defined(_M_X64))
|
Chris@101
|
12 #include <Intrin.h>
|
Chris@101
|
13 #endif
|
Chris@101
|
14
|
Chris@16
|
15 namespace boost{ namespace multiprecision{ namespace detail{
|
Chris@16
|
16
|
Chris@16
|
17 template <class Unsigned>
|
Chris@16
|
18 inline unsigned find_lsb(Unsigned mask, const mpl::int_<0>&)
|
Chris@16
|
19 {
|
Chris@16
|
20 unsigned result = 0;
|
Chris@16
|
21 while(!(mask & 1u))
|
Chris@16
|
22 {
|
Chris@16
|
23 mask >>= 1;
|
Chris@16
|
24 ++result;
|
Chris@16
|
25 }
|
Chris@16
|
26 return result;
|
Chris@16
|
27 }
|
Chris@16
|
28
|
Chris@16
|
29 template <class Unsigned>
|
Chris@16
|
30 inline unsigned find_msb(Unsigned mask, const mpl::int_<0>&)
|
Chris@16
|
31 {
|
Chris@16
|
32 unsigned index = 0;
|
Chris@16
|
33 while(mask)
|
Chris@16
|
34 {
|
Chris@16
|
35 ++index;
|
Chris@16
|
36 mask >>= 1;
|
Chris@16
|
37 }
|
Chris@16
|
38 return --index;
|
Chris@16
|
39 }
|
Chris@16
|
40
|
Chris@16
|
41 #if defined(BOOST_MSVC) && (defined(_M_IX86) || defined(_M_X64))
|
Chris@101
|
42
|
Chris@101
|
43 #pragma intrinsic(_BitScanForward,_BitScanReverse)
|
Chris@101
|
44
|
Chris@16
|
45 BOOST_FORCEINLINE unsigned find_lsb(unsigned long mask, const mpl::int_<1>&)
|
Chris@16
|
46 {
|
Chris@16
|
47 unsigned long result;
|
Chris@16
|
48 _BitScanForward(&result, mask);
|
Chris@16
|
49 return result;
|
Chris@16
|
50 }
|
Chris@16
|
51
|
Chris@16
|
52 BOOST_FORCEINLINE unsigned find_msb(unsigned long mask, const mpl::int_<1>&)
|
Chris@16
|
53 {
|
Chris@16
|
54 unsigned long result;
|
Chris@16
|
55 _BitScanReverse(&result, mask);
|
Chris@16
|
56 return result;
|
Chris@16
|
57 }
|
Chris@16
|
58 #ifdef _M_X64
|
Chris@101
|
59
|
Chris@101
|
60 #pragma intrinsic(_BitScanForward64,_BitScanReverse64)
|
Chris@101
|
61
|
Chris@16
|
62 BOOST_FORCEINLINE unsigned find_lsb(unsigned __int64 mask, const mpl::int_<2>&)
|
Chris@16
|
63 {
|
Chris@16
|
64 unsigned long result;
|
Chris@16
|
65 _BitScanForward64(&result, mask);
|
Chris@16
|
66 return result;
|
Chris@16
|
67 }
|
Chris@16
|
68 template <class Unsigned>
|
Chris@16
|
69 BOOST_FORCEINLINE unsigned find_msb(Unsigned mask, const mpl::int_<2>&)
|
Chris@16
|
70 {
|
Chris@16
|
71 unsigned long result;
|
Chris@16
|
72 _BitScanReverse64(&result, mask);
|
Chris@16
|
73 return result;
|
Chris@16
|
74 }
|
Chris@16
|
75 #endif
|
Chris@16
|
76
|
Chris@16
|
77 template <class Unsigned>
|
Chris@16
|
78 BOOST_FORCEINLINE unsigned find_lsb(Unsigned mask)
|
Chris@16
|
79 {
|
Chris@16
|
80 typedef typename make_unsigned<Unsigned>::type ui_type;
|
Chris@16
|
81 typedef typename mpl::if_c<
|
Chris@16
|
82 sizeof(Unsigned) <= sizeof(unsigned long),
|
Chris@16
|
83 mpl::int_<1>,
|
Chris@16
|
84 #ifdef _M_X64
|
Chris@16
|
85 typename mpl::if_c<
|
Chris@16
|
86 sizeof(Unsigned) <= sizeof(__int64),
|
Chris@16
|
87 mpl::int_<2>,
|
Chris@16
|
88 mpl::int_<0>
|
Chris@16
|
89 >::type
|
Chris@16
|
90 #else
|
Chris@16
|
91 mpl::int_<0>
|
Chris@16
|
92 #endif
|
Chris@16
|
93 >::type tag_type;
|
Chris@16
|
94 return find_lsb(static_cast<ui_type>(mask), tag_type());
|
Chris@16
|
95 }
|
Chris@16
|
96
|
Chris@16
|
97 template <class Unsigned>
|
Chris@16
|
98 BOOST_FORCEINLINE unsigned find_msb(Unsigned mask)
|
Chris@16
|
99 {
|
Chris@16
|
100 typedef typename make_unsigned<Unsigned>::type ui_type;
|
Chris@16
|
101 typedef typename mpl::if_c<
|
Chris@16
|
102 sizeof(Unsigned) <= sizeof(unsigned long),
|
Chris@16
|
103 mpl::int_<1>,
|
Chris@16
|
104 #ifdef _M_X64
|
Chris@16
|
105 typename mpl::if_c<
|
Chris@16
|
106 sizeof(Unsigned) <= sizeof(__int64),
|
Chris@16
|
107 mpl::int_<2>,
|
Chris@16
|
108 mpl::int_<0>
|
Chris@16
|
109 >::type
|
Chris@16
|
110 #else
|
Chris@16
|
111 mpl::int_<0>
|
Chris@16
|
112 #endif
|
Chris@16
|
113 >::type tag_type;
|
Chris@16
|
114 return find_msb(static_cast<ui_type>(mask), tag_type());
|
Chris@16
|
115 }
|
Chris@16
|
116
|
Chris@16
|
117 #elif defined(BOOST_GCC) || defined(__clang__) || (defined(BOOST_INTEL) && defined(__GNUC__))
|
Chris@16
|
118
|
Chris@16
|
119 BOOST_FORCEINLINE unsigned find_lsb(unsigned mask, mpl::int_<1> const&)
|
Chris@16
|
120 {
|
Chris@16
|
121 return __builtin_ctz(mask);
|
Chris@16
|
122 }
|
Chris@16
|
123 BOOST_FORCEINLINE unsigned find_lsb(unsigned long mask, mpl::int_<2> const&)
|
Chris@16
|
124 {
|
Chris@16
|
125 return __builtin_ctzl(mask);
|
Chris@16
|
126 }
|
Chris@16
|
127 BOOST_FORCEINLINE unsigned find_lsb(unsigned long long mask, mpl::int_<3> const&)
|
Chris@16
|
128 {
|
Chris@16
|
129 return __builtin_ctzll(mask);
|
Chris@16
|
130 }
|
Chris@16
|
131 BOOST_FORCEINLINE unsigned find_msb(unsigned mask, mpl::int_<1> const&)
|
Chris@16
|
132 {
|
Chris@16
|
133 return sizeof(unsigned) * CHAR_BIT - 1 - __builtin_clz(mask);
|
Chris@16
|
134 }
|
Chris@16
|
135 BOOST_FORCEINLINE unsigned find_msb(unsigned long mask, mpl::int_<2> const&)
|
Chris@16
|
136 {
|
Chris@16
|
137 return sizeof(unsigned long) * CHAR_BIT - 1 - __builtin_clzl(mask);
|
Chris@16
|
138 }
|
Chris@16
|
139 BOOST_FORCEINLINE unsigned find_msb(unsigned long long mask, mpl::int_<3> const&)
|
Chris@16
|
140 {
|
Chris@16
|
141 return sizeof(unsigned long long) * CHAR_BIT - 1 - __builtin_clzll(mask);
|
Chris@16
|
142 }
|
Chris@16
|
143
|
Chris@16
|
144 template <class Unsigned>
|
Chris@16
|
145 BOOST_FORCEINLINE unsigned find_lsb(Unsigned mask)
|
Chris@16
|
146 {
|
Chris@16
|
147 typedef typename make_unsigned<Unsigned>::type ui_type;
|
Chris@16
|
148 typedef typename mpl::if_c<
|
Chris@16
|
149 sizeof(Unsigned) <= sizeof(unsigned),
|
Chris@16
|
150 mpl::int_<1>,
|
Chris@16
|
151 typename mpl::if_c<
|
Chris@16
|
152 sizeof(Unsigned) <= sizeof(unsigned long),
|
Chris@16
|
153 mpl::int_<2>,
|
Chris@16
|
154 typename mpl::if_c<
|
Chris@16
|
155 sizeof(Unsigned) <= sizeof(unsigned long long),
|
Chris@16
|
156 mpl::int_<3>,
|
Chris@16
|
157 mpl::int_<0>
|
Chris@16
|
158 >::type
|
Chris@16
|
159 >::type
|
Chris@16
|
160 >::type tag_type;
|
Chris@16
|
161 return find_lsb(static_cast<ui_type>(mask), tag_type());
|
Chris@16
|
162 }
|
Chris@16
|
163 template <class Unsigned>
|
Chris@16
|
164 BOOST_FORCEINLINE unsigned find_msb(Unsigned mask)
|
Chris@16
|
165 {
|
Chris@16
|
166 typedef typename make_unsigned<Unsigned>::type ui_type;
|
Chris@16
|
167 typedef typename mpl::if_c<
|
Chris@16
|
168 sizeof(Unsigned) <= sizeof(unsigned),
|
Chris@16
|
169 mpl::int_<1>,
|
Chris@16
|
170 typename mpl::if_c<
|
Chris@16
|
171 sizeof(Unsigned) <= sizeof(unsigned long),
|
Chris@16
|
172 mpl::int_<2>,
|
Chris@16
|
173 typename mpl::if_c<
|
Chris@16
|
174 sizeof(Unsigned) <= sizeof(unsigned long long),
|
Chris@16
|
175 mpl::int_<3>,
|
Chris@16
|
176 mpl::int_<0>
|
Chris@16
|
177 >::type
|
Chris@16
|
178 >::type
|
Chris@16
|
179 >::type tag_type;
|
Chris@16
|
180 return find_msb(static_cast<ui_type>(mask), tag_type());
|
Chris@16
|
181 }
|
Chris@16
|
182 #elif defined(BOOST_INTEL)
|
Chris@16
|
183 BOOST_FORCEINLINE unsigned find_lsb(unsigned mask, mpl::int_<1> const&)
|
Chris@16
|
184 {
|
Chris@16
|
185 return _bit_scan_forward(mask);
|
Chris@16
|
186 }
|
Chris@16
|
187 BOOST_FORCEINLINE unsigned find_msb(unsigned mask, mpl::int_<1> const&)
|
Chris@16
|
188 {
|
Chris@16
|
189 return _bit_scan_reverse(mask);
|
Chris@16
|
190 }
|
Chris@16
|
191 template <class Unsigned>
|
Chris@16
|
192 BOOST_FORCEINLINE unsigned find_lsb(Unsigned mask)
|
Chris@16
|
193 {
|
Chris@16
|
194 typedef typename make_unsigned<Unsigned>::type ui_type;
|
Chris@16
|
195 typedef typename mpl::if_c<
|
Chris@16
|
196 sizeof(Unsigned) <= sizeof(unsigned),
|
Chris@16
|
197 mpl::int_<1>,
|
Chris@16
|
198 mpl::int_<0>
|
Chris@16
|
199 >::type tag_type;
|
Chris@16
|
200 return find_lsb(static_cast<ui_type>(mask), tag_type());
|
Chris@16
|
201 }
|
Chris@16
|
202 template <class Unsigned>
|
Chris@16
|
203 BOOST_FORCEINLINE unsigned find_msb(Unsigned mask)
|
Chris@16
|
204 {
|
Chris@16
|
205 typedef typename make_unsigned<Unsigned>::type ui_type;
|
Chris@16
|
206 typedef typename mpl::if_c<
|
Chris@16
|
207 sizeof(Unsigned) <= sizeof(unsigned),
|
Chris@16
|
208 mpl::int_<1>,
|
Chris@16
|
209 mpl::int_<0>
|
Chris@16
|
210 >::type tag_type;
|
Chris@16
|
211 return find_msb(static_cast<ui_type>(mask), tag_type());
|
Chris@16
|
212 }
|
Chris@16
|
213 #else
|
Chris@16
|
214 template <class Unsigned>
|
Chris@16
|
215 BOOST_FORCEINLINE unsigned find_lsb(Unsigned mask)
|
Chris@16
|
216 {
|
Chris@16
|
217 return find_lsb(mask, mpl::int_<0>());
|
Chris@16
|
218 }
|
Chris@16
|
219 template <class Unsigned>
|
Chris@16
|
220 BOOST_FORCEINLINE unsigned find_msb(Unsigned mask)
|
Chris@16
|
221 {
|
Chris@16
|
222 return find_msb(mask, mpl::int_<0>());
|
Chris@16
|
223 }
|
Chris@16
|
224 #endif
|
Chris@16
|
225
|
Chris@16
|
226 }}}
|
Chris@16
|
227
|
Chris@16
|
228 #endif
|
Chris@16
|
229
|