
Hi, is there in boost a trait to describe current target capabilities, i.e. processor's available instruction set, size of registers (32, 64, ... bits) and any other usefull feature? To give an example, I'd like to write code like the following one, with different versions of the same function depending on target capabilities. Regards, Antoine. --- namespace detail { template <typename T, unsigned D, typename P = current_target_trait> struct _popcount; template <> struct _popcount<__m128i, 128, ssse3> { int operator()(const __m128i& x) { static const __m128i M = { 0x0f0f0f0f0f0f0f0fULL, 0x0f0f0f0f0f0f0f0fULL }; static const __m128i C = { 0x0302020102010100ULL, 0x0403030203020201ULL }; __m128i y = _mm_add_epi16 (_mm_shuffle_epi8 (C, _mm_and_si128 (x, M)), _mm_shuffle_epi8 (C, _mm_and_si128 (_mm_srai_epi16 (x, 4), M))); return _mm_cvtsi128_si32 (_mm_sad_epu8 (_mm_hadd_epi16 (y, y), _mm_setzero_si128())); } }; // Count bits in the low 64-bit quad word template <> struct _popcount<__m128i, 64, ssse3> { int operator()(const __m128i& x) { static const __m128i M = { 0x0f0f0f0f0f0f0f0fULL, 0x0f0f0f0f0f0f0f0fULL }; static const __m128i C = { 0x0302020102010100ULL, 0x0403030203020201ULL }; __m128i y = _mm_shuffle_epi8 (C, _mm_and_si128 (_mm_unpacklo_epi32 (x, _mm_srai_epi16 (x, 4)), M)); return _mm_cvtsi128_si32 (_mm_sad_epu8 (_mm_hadd_epi16 (y, y), _mm_setzero_si128())); } }; } template <typename T> int popcount (const T& data) { return detail::_popcount<T,sizeof(data)*8>()(data); }

Antoine de Maricourt wrote:
Hi,
is there in boost a trait to describe current target capabilities, i.e. processor's available instruction set, size of registers (32, 64, ... bits) and any other usefull feature?
There isn't.
To give an example, I'd like to write code like the following one, with different versions of the same function depending on target capabilities.
That isn't quite possible, since architecture-specific code would contain bits (such as compiler intrinsics) that are only valid when compiling for that target, using a specific compiler, yet there is no mechanism for masking those bits in your demo code. Usually this masking is achieved using the preprocessor. However, as it is very hard to come up with a complete processor / capability taxonomy, I doubt even there a generic and comprehensive set of macros exists. Regards, Stefan -- ...ich hab' noch einen Koffer in Berlin...
participants (2)
-
Antoine de Maricourt
-
Stefan Seefeld