Boost logo

Boost :

Subject: [boost] Processor traits
From: Antoine de Maricourt (antoine.de-maricourt_at_[hidden])
Date: 2008-12-22 09:48:35


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);
}

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk