Skip to content

Commit 09f23d7

Browse files
committed
Refactor bit utils
1 parent 6d366dd commit 09f23d7

File tree

2 files changed

+66
-45
lines changed

2 files changed

+66
-45
lines changed

include/xsimd/utils/bits.hpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/***************************************************************************
2+
* Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and *
3+
* Martin Renou *
4+
* Copyright (c) QuantStack *
5+
* Copyright (c) Serge Guelton *
6+
* *
7+
* Distributed under the terms of the BSD 3-Clause License. *
8+
* *
9+
* The full license is in the file LICENSE, distributed with this software. *
10+
***************************************************************************/
11+
12+
#ifndef XSIMD_CPUID_UTILS_HPP
13+
#define XSIMD_CPUID_UTILS_HPP
14+
15+
namespace xsimd
16+
{
17+
namespace utils
18+
{
19+
template <typename I>
20+
constexpr I make_bit_mask(I bit)
21+
{
22+
return static_cast<I>(I { 1 } << bit);
23+
}
24+
25+
template <typename I, typename... Args>
26+
constexpr I make_bit_mask(I bit, Args... bits)
27+
{
28+
return make_bit_mask<I>(bit) | make_bit_mask<I>(static_cast<I>(bits)...);
29+
}
30+
31+
template <int... Bits, typename I>
32+
constexpr bool bit_is_set(I value)
33+
{
34+
constexpr I mask = make_bit_mask<I>(static_cast<I>(Bits)...);
35+
return (value & mask) == mask;
36+
}
37+
}
38+
}
39+
40+
#endif

include/xsimd/xsimd_cpu_features_x86.hpp

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,35 +15,16 @@
1515
#include <cstdint>
1616

1717
#include "./config/xsimd_config.hpp"
18+
#include "./utils/bits.hpp"
1819

1920
#if XSIMD_TARGET_X86 && defined(_MSC_VER)
20-
// Contains the definition of __cpuidex
21-
#include <intrin.h>
21+
#include <intrin.h> // Contains the definition of __cpuidex
2222
#endif
2323

2424
namespace xsimd
2525
{
2626
namespace detail
2727
{
28-
template <typename I>
29-
constexpr I make_bit_mask(I bit)
30-
{
31-
return static_cast<I>(I { 1 } << bit);
32-
}
33-
34-
template <typename I, typename... Args>
35-
constexpr I make_bit_mask(I bit, Args... bits)
36-
{
37-
return make_bit_mask<I>(bit) | make_bit_mask<I>(static_cast<I>(bits)...);
38-
}
39-
40-
template <int... Bits, typename I>
41-
constexpr bool bit_is_set(I value)
42-
{
43-
constexpr I mask = make_bit_mask<I>(static_cast<I>(Bits)...);
44-
return (value & mask) == mask;
45-
}
46-
4728
inline void get_cpuid(int reg[4], int level, int count = 0) noexcept;
4829

4930
inline std::uint32_t get_xcr0_low() noexcept;
@@ -83,20 +64,20 @@ namespace xsimd
8364

8465
constexpr bool sse_state_os_enabled() const noexcept
8566
{
86-
return detail::bit_is_set<1>(m_low);
67+
return utils::bit_is_set<1>(m_low);
8768
}
8869

8970
constexpr bool avx_state_os_enabled() const noexcept
9071
{
9172
// Check both SSE and AVX bits even though AVX must imply SSE
92-
return detail::bit_is_set<1, 2>(m_low);
73+
return utils::bit_is_set<1, 2>(m_low);
9374
}
9475

9576
constexpr bool avx512_state_os_enabled() const noexcept
9677
{
9778
// Check all SSE, AVX, and AVX52 bits even though AVX512 must
9879
// imply AVX and SSE
99-
return detail::bit_is_set<1, 2, 6>(m_low);
80+
return utils::bit_is_set<1, 2, 6>(m_low);
10081
}
10182

10283
private:
@@ -141,32 +122,32 @@ namespace xsimd
141122

142123
constexpr bool sse2() const noexcept
143124
{
144-
return detail::bit_is_set<26>(m_regs.reg1[3]);
125+
return utils::bit_is_set<26>(m_regs.reg1[3]);
145126
}
146127

147128
constexpr bool sse3() const noexcept
148129
{
149-
return detail::bit_is_set<0>(m_regs.reg1[2]);
130+
return utils::bit_is_set<0>(m_regs.reg1[2]);
150131
}
151132

152133
constexpr bool ssse3() const noexcept
153134
{
154-
return detail::bit_is_set<9>(m_regs.reg1[2]);
135+
return utils::bit_is_set<9>(m_regs.reg1[2]);
155136
}
156137

157138
constexpr bool sse4_1() const noexcept
158139
{
159-
return detail::bit_is_set<19>(m_regs.reg1[2]);
140+
return utils::bit_is_set<19>(m_regs.reg1[2]);
160141
}
161142

162143
constexpr bool sse4_2() const noexcept
163144
{
164-
return detail::bit_is_set<20>(m_regs.reg1[2]);
145+
return utils::bit_is_set<20>(m_regs.reg1[2]);
165146
}
166147

167148
constexpr bool fma3() const noexcept
168149
{
169-
return detail::bit_is_set<12>(m_regs.reg1[2]);
150+
return utils::bit_is_set<12>(m_regs.reg1[2]);
170151
}
171152

172153
/**
@@ -181,77 +162,77 @@ namespace xsimd
181162
*/
182163
constexpr bool osxsave() const noexcept
183164
{
184-
return detail::bit_is_set<27>(m_regs.reg1[2]);
165+
return utils::bit_is_set<27>(m_regs.reg1[2]);
185166
}
186167

187168
constexpr bool avx() const noexcept
188169
{
189-
return detail::bit_is_set<28>(m_regs.reg1[2]);
170+
return utils::bit_is_set<28>(m_regs.reg1[2]);
190171
}
191172

192173
constexpr bool avx2() const noexcept
193174
{
194-
return detail::bit_is_set<5>(m_regs.reg7[1]);
175+
return utils::bit_is_set<5>(m_regs.reg7[1]);
195176
}
196177

197178
constexpr bool avx512f() const noexcept
198179
{
199-
return detail::bit_is_set<16>(m_regs.reg7[1]);
180+
return utils::bit_is_set<16>(m_regs.reg7[1]);
200181
}
201182

202183
constexpr bool avx512dq() const noexcept
203184
{
204-
return detail::bit_is_set<17>(m_regs.reg7[1]);
185+
return utils::bit_is_set<17>(m_regs.reg7[1]);
205186
}
206187

207188
constexpr bool avx512ifma() const noexcept
208189
{
209-
return detail::bit_is_set<21>(m_regs.reg7[1]);
190+
return utils::bit_is_set<21>(m_regs.reg7[1]);
210191
}
211192

212193
constexpr bool avx512pf() const noexcept
213194
{
214-
return detail::bit_is_set<26>(m_regs.reg7[1]);
195+
return utils::bit_is_set<26>(m_regs.reg7[1]);
215196
}
216197

217198
constexpr bool avx512er() const noexcept
218199
{
219-
return detail::bit_is_set<27>(m_regs.reg7[1]);
200+
return utils::bit_is_set<27>(m_regs.reg7[1]);
220201
}
221202

222203
constexpr bool avx512cd() const noexcept
223204
{
224-
return detail::bit_is_set<28>(m_regs.reg7[1]);
205+
return utils::bit_is_set<28>(m_regs.reg7[1]);
225206
}
226207

227208
constexpr bool avx512bw() const noexcept
228209
{
229-
return detail::bit_is_set<30>(m_regs.reg7[1]);
210+
return utils::bit_is_set<30>(m_regs.reg7[1]);
230211
}
231212

232213
constexpr bool avx512vbmi() const noexcept
233214
{
234-
return detail::bit_is_set<1>(m_regs.reg7[2]);
215+
return utils::bit_is_set<1>(m_regs.reg7[2]);
235216
}
236217

237218
constexpr bool avx512vbmi2() const noexcept
238219
{
239-
return detail::bit_is_set<6>(m_regs.reg7[2]);
220+
return utils::bit_is_set<6>(m_regs.reg7[2]);
240221
}
241222

242223
constexpr bool avx512vnni_bw() const noexcept
243224
{
244-
return detail::bit_is_set<11>(m_regs.reg7[2]);
225+
return utils::bit_is_set<11>(m_regs.reg7[2]);
245226
}
246227

247228
constexpr bool avxvnni() const noexcept
248229
{
249-
return detail::bit_is_set<4>(m_regs.reg7a[0]);
230+
return utils::bit_is_set<4>(m_regs.reg7a[0]);
250231
}
251232

252233
constexpr bool fma4() const noexcept
253234
{
254-
return detail::bit_is_set<16>(m_regs.reg8[2]);
235+
return utils::bit_is_set<16>(m_regs.reg8[2]);
255236
}
256237

257238
private:

0 commit comments

Comments
 (0)