array.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Copyright (C) 2014 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_SIMDPP_DETAIL_ARRAY_H
  7. #define LIBSIMDPP_SIMDPP_DETAIL_ARRAY_H
  8. #ifndef LIBSIMDPP_SIMD_H
  9. #error "This file must be included through simd.h"
  10. #endif
  11. #include <simdpp/types.h>
  12. namespace simdpp {
  13. namespace SIMDPP_ARCH_NAMESPACE {
  14. namespace detail {
  15. /* A compile-time array that uses variables instead of array for underlying
  16. storage when element count is small.
  17. Variables are used as a workaround because arrays are too opaque for most
  18. older compilers to be able to figure out whith store corresponds to which
  19. load.
  20. */
  21. template<class T, unsigned N>
  22. class vararray {
  23. public:
  24. SIMDPP_INL T& operator[](unsigned id) { return d[id]; }
  25. SIMDPP_INL const T& operator[](unsigned id) const { return d[id]; }
  26. private:
  27. T d[N];
  28. };
  29. template<class T>
  30. class vararray<T,2> {
  31. public:
  32. SIMDPP_INL T& operator[](unsigned id) { return id == 0 ? d0 : d1; }
  33. SIMDPP_INL const T& operator[](unsigned id) const { return id == 0 ? d0 : d1; }
  34. private:
  35. T d0, d1;
  36. };
  37. template<class T>
  38. class vararray<T,4> {
  39. public:
  40. SIMDPP_INL T& operator[](unsigned id)
  41. {
  42. switch (id) {
  43. case 0: return d0;
  44. case 1: return d1;
  45. case 2: return d2;
  46. default: return d3;
  47. }
  48. }
  49. SIMDPP_INL const T& operator[](unsigned id) const
  50. {
  51. switch (id) {
  52. case 0: return d0;
  53. case 1: return d1;
  54. case 2: return d2;
  55. default: return d3;
  56. }
  57. }
  58. private:
  59. T d0, d1, d2, d3;
  60. };
  61. } // namespace detail
  62. } // namespace SIMDPP_ARCH_NAMESPACE
  63. } // namespace simdpp
  64. #endif