File: simd.hpp

package info (click to toggle)
netgen 6.2.2601%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,076 kB
  • sloc: cpp: 166,627; tcl: 6,310; python: 2,868; sh: 528; makefile: 90
file content (160 lines) | stat: -rw-r--r-- 4,055 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#ifndef NETGEN_CORE_SIMD_HPP
#define NETGEN_CORE_SIMD_HPP

/**************************************************************************/
/* File:   simd.hpp                                                       */
/* Author: Joachim Schoeberl, Matthias Hochsteger                         */
/* Date:   25. Mar. 16                                                    */
/**************************************************************************/

#include <array>
#include <tuple>
#include "ngcore_api.hpp"
#include "simd_generic.hpp"


#ifndef __CUDA_ARCH__

#ifdef NETGEN_ARCH_AMD64
#ifndef __SSE__
#define __SSE__
#endif
#include "simd_sse.hpp"
#endif

#ifdef __AVX__
#include "simd_avx.hpp"
#endif

#ifdef __AVX512F__
#include "simd_avx512.hpp"
#endif

#ifdef __aarch64__
#include "simd_arm64.hpp"
#endif

#endif // __CUDA_ARCH__

namespace ngcore
{
#ifndef __CUDA_ARCH__
#ifdef NETGEN_ARCH_AMD64
  /*
  NETGEN_INLINE auto HSum (SIMD<double,2> v1, SIMD<double,2> v2, SIMD<double,2> v3, SIMD<double,2> v4)
  {
    SIMD<double,2> hsum1 = my_mm_hadd_pd (v1.Data(), v2.Data());
    SIMD<double,2> hsum2 = my_mm_hadd_pd (v3.Data(), v4.Data());
    return SIMD<double,4> (hsum1, hsum2);
  }
  */
  
  NETGEN_INLINE auto GetMaskFromBits( unsigned int i )
  {
    return SIMD<mask64>::GetMaskFromBits(i);
  }
#endif
#endif // __CUDA_ARCH__
  
  NETGEN_INLINE void SIMDTranspose (SIMD<double,4> a1, SIMD<double,4> a2, SIMD <double,4> a3, SIMD<double,4> a4,
                                    SIMD<double,4> & b1, SIMD<double,4> & b2, SIMD<double,4> & b3, SIMD<double,4> & b4)
  {
    if constexpr (sizeof(a1.Lo()) == 16)
      {
        auto [h1,h2] = Unpack(a1,a2);
        auto [h3,h4] = Unpack(a3,a4);
        b1 = SIMD<double,4> (h1.Lo(), h3.Lo());
        b2 = SIMD<double,4> (h2.Lo(), h4.Lo());
        b3 = SIMD<double,4> (h1.Hi(), h3.Hi());
        b4 = SIMD<double,4> (h2.Hi(), h4.Hi());
      }
    else
      {
        b1 = SIMD<double,4> (a1[0], a2[0], a3[0], a4[0]);
        b2 = SIMD<double,4> (a1[1], a2[1], a3[1], a4[1]);
        b3 = SIMD<double,4> (a1[2], a2[2], a3[2], a4[2]);
        b4 = SIMD<double,4> (a1[3], a2[3], a3[3], a4[3]);
      }
  }
  
  template<int N>
  NETGEN_INLINE auto HSum (SIMD<double,N> s1, SIMD<double,N> s2)
  {
    return SIMD<double,2>(HSum(s1), HSum(s2));
  }

  template<int N>
  NETGEN_INLINE auto HSum (SIMD<double,N> s1, SIMD<double,N> s2, SIMD<double,N> s3, SIMD<double,N> s4 )
  {
    // return SIMD<double,4>(HSum(s1), HSum(s2), HSum(s3), HSum(s4));
    return SIMD<double,4>(HSum(s1, s2), HSum(s3,s4));
  }



  template <typename T, size_t S> class MakeSimdCl;
  
  template <typename T, size_t S>
  auto MakeSimd (std::array<T,S> aa)  { return MakeSimdCl(aa).Get(); }

  
  template <typename T, size_t S>
  class MakeSimdCl
  {
    std::array<T,S> a;
  public:
    MakeSimdCl (std::array<T,S> aa) : a(aa)  { ; }
    auto Get() const
    {
      SIMD<T,S> sa( [this] (auto i) { return (this->a)[i]; });
      return sa;
    }
  };
  

  
  
  template <typename Tfirst, size_t S, typename ...Trest>
  class MakeSimdCl<std::tuple<Tfirst,Trest...>,S>
  {
    std::array<std::tuple<Tfirst,Trest...>,S> a;
  public:
    MakeSimdCl (std::array<std::tuple<Tfirst,Trest...>,S> aa) : a(aa)  { ; }
    auto Get() const
    {
      std::array<Tfirst,S> a0;
      for (int i = 0; i < S; i++)
        a0[i] = std::get<0> (a[i]);
      
      if constexpr (std::tuple_size<std::tuple<Tfirst,Trest...>>::value == 1)
        {
          return std::tuple(MakeSimd(a0));
        }
      else
        {
          std::array<std::tuple<Trest...>,S> arest;
          for (int i = 0; i < S; i++)
            arest[i] = skip_first(a[i]);
          
          return std::tuple_cat ( std::tuple (MakeSimd(a0)), MakeSimd(arest) );
        }
    }

    template <typename... Ts>
    static auto skip_first(const std::tuple<Ts...>& t) {
      return std::apply([](auto first, auto... rest) {
        return std::make_tuple(rest...);
      }, t);
    }
  };
  

  

  
}


#include "simd_math.hpp"

#endif // NETGEN_CORE_SIMD_HPP