extract_bits.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Copyright (C) 2011-2017 Povilas Kanapickas <[email protected]>
  2. Distributed under the Boost Software License, Version 1.0.
  3. (See accompanying file LICENSE_1_0.txt or copy at
  4. http://www.boost.org/LICENSE_1_0.txt)
  5. */
  6. #ifndef LIBSIMDPP_SIMD_CORE_EXTRACT_BITS_H
  7. #define LIBSIMDPP_SIMD_CORE_EXTRACT_BITS_H
  8. #ifndef LIBSIMDPP_SIMD_H
  9. #error "This file must be included through simd.h"
  10. #endif
  11. #include <simdpp/types.h>
  12. #include <simdpp/detail/insn/extract_bits.h>
  13. namespace simdpp {
  14. namespace SIMDPP_ARCH_NAMESPACE {
  15. /** Extracts a bit from each byte of each element of a vector containing 8-bit
  16. elements.
  17. This operation is only sensible if each byte within the vector is either
  18. 0x00 or 0xff.
  19. @code
  20. r = ((a[0] & 0x??) ? 0x01 : 0) |
  21. ((a[1] & 0x??) ? 0x02 : 0) |
  22. ...
  23. ((a[15] & 0x??) ? 0x80 : 0)
  24. @endcode
  25. */
  26. SIMDPP_INL uint16_t extract_bits_any(const uint8<16>& a)
  27. {
  28. return detail::insn::i_extract_bits_any(a);
  29. }
  30. SIMDPP_INL uint32_t extract_bits_any(const uint8<32>& a)
  31. {
  32. return detail::insn::i_extract_bits_any(a);
  33. }
  34. /** Extracts specific bit from each byte of each element of a int8x16 vector.
  35. @code
  36. r = (a[0] & 0x80 >> 7) | (a[1] & 0x80 >> 6) | ... | (a[15] & 0x80 << 8)
  37. @endcode
  38. */
  39. template<unsigned id> SIMDPP_INL
  40. uint16_t extract_bits(const uint8<16>& a)
  41. {
  42. static_assert(id < 8, "index out of bounds");
  43. return detail::insn::i_extract_bits<id>(a);
  44. }
  45. template<unsigned id> SIMDPP_INL
  46. uint32_t extract_bits(const uint8<32>& a)
  47. {
  48. static_assert(id < 8, "index out of bounds");
  49. return detail::insn::i_extract_bits<id>(a);
  50. }
  51. } // namespace SIMDPP_ARCH_NAMESPACE
  52. } // namespace simdpp
  53. #endif