aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/simd/simd_scalar/simd_scalar.h
blob: 379e2d6a8c677e82c91d6b11e45502d487208a33 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Scalar emulation of SIMD
* (C) 2009,2013 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#ifndef BOTAN_SIMD_SCALAR_H__
#define BOTAN_SIMD_SCALAR_H__

#include <botan/loadstor.h>
#include <botan/bswap.h>

namespace Botan {

/**
* Fake SIMD, using plain scalar operations
* Often still faster than iterative on superscalar machines
*/
template<typename T, size_t N>
class SIMD_Scalar
   {
   public:
      static bool enabled() { return true; }

      static size_t size() { return N; }

      SIMD_Scalar() { /* uninitialized */ }

      SIMD_Scalar(const T B[N])
         {
         for(size_t i = 0; i != size(); ++i)
            m_v[i] = B[i];
         }

      SIMD_Scalar(T B)
         {
         for(size_t i = 0; i != size(); ++i)
            m_v[i] = B;
         }

      static SIMD_Scalar<T,N> load_le(const void* in)
         {
         SIMD_Scalar<T,N> out;
         const byte* in_b = static_cast<const byte*>(in);

         for(size_t i = 0; i != size(); ++i)
            out.m_v[i] = Botan::load_le<T>(in_b, i);

         return out;
         }

      static SIMD_Scalar<T,N> load_be(const void* in)
         {
         SIMD_Scalar<T,N> out;
         const byte* in_b = static_cast<const byte*>(in);

         for(size_t i = 0; i != size(); ++i)
            out.m_v[i] = Botan::load_be<T>(in_b, i);

         return out;
         }

      void store_le(byte out[]) const
         {
         for(size_t i = 0; i != size(); ++i)
            Botan::store_le(m_v[i], out + i*sizeof(T));
         }

      void store_be(byte out[]) const
         {
         for(size_t i = 0; i != size(); ++i)
            Botan::store_be(m_v[i], out + i*sizeof(T));
         }

      void rotate_left(size_t rot)
         {
         for(size_t i = 0; i != size(); ++i)
            m_v[i] = Botan::rotate_left(m_v[i], rot);
         }

      void rotate_right(size_t rot)
         {
         for(size_t i = 0; i != size(); ++i)
            m_v[i] = Botan::rotate_right(m_v[i], rot);
         }

      void operator+=(const SIMD_Scalar<T,N>& other)
         {
         for(size_t i = 0; i != size(); ++i)
            m_v[i] += other.m_v[i];
         }

      void operator-=(const SIMD_Scalar<T,N>& other)
         {
         for(size_t i = 0; i != size(); ++i)
            m_v[i] -= other.m_v[i];
         }

      SIMD_Scalar<T,N> operator+(const SIMD_Scalar<T,N>& other) const
         {
         SIMD_Scalar<T,N> out = *this;
         out += other;
         return out;
         }

      SIMD_Scalar<T,N> operator-(const SIMD_Scalar<T,N>& other) const
         {
         SIMD_Scalar<T,N> out = *this;
         out -= other;
         return out;
         }

      void operator^=(const SIMD_Scalar<T,N>& other)
         {
         for(size_t i = 0; i != size(); ++i)
            m_v[i] ^= other.m_v[i];
         }

      SIMD_Scalar<T,N> operator^(const SIMD_Scalar<T,N>& other) const
         {
         SIMD_Scalar<T,N> out = *this;
         out ^= other;
         return out;
         }

      void operator|=(const SIMD_Scalar<T,N>& other)
         {
         for(size_t i = 0; i != size(); ++i)
            m_v[i] |= other.m_v[i];
         }

      void operator&=(const SIMD_Scalar<T,N>& other)
         {
         for(size_t i = 0; i != size(); ++i)
            m_v[i] &= other.m_v[i];
         }

      SIMD_Scalar<T,N> operator&(const SIMD_Scalar<T,N>& other)
         {
         SIMD_Scalar<T,N> out = *this;
         out &= other;
         return out;
         }

      SIMD_Scalar<T,N> operator<<(size_t shift) const
         {
         SIMD_Scalar<T,N> out = *this;
         for(size_t i = 0; i != size(); ++i)
            out.m_v[i] <<= shift;
         return out;
         }

      SIMD_Scalar<T,N> operator>>(size_t shift) const
         {
         SIMD_Scalar<T,N> out = *this;
         for(size_t i = 0; i != size(); ++i)
            out.m_v[i] >>= shift;
         return out;
         }

      SIMD_Scalar<T,N> operator~() const
         {
         SIMD_Scalar<T,N> out = *this;
         for(size_t i = 0; i != size(); ++i)
            out.m_v[i] = ~out.m_v[i];
         return out;
         }

      // (~reg) & other
      SIMD_Scalar<T,N> andc(const SIMD_Scalar<T,N>& other)
         {
         SIMD_Scalar<T,N> out;
         for(size_t i = 0; i != size(); ++i)
            out.m_v[i] = (~m_v[i]) & other.m_v[i];
         return out;
         }

      SIMD_Scalar<T,N> bswap() const
         {
         SIMD_Scalar<T,N> out;
         for(size_t i = 0; i != size(); ++i)
            out.m_v[i] = reverse_bytes(m_v[i]);
         return out;
         }

      static void transpose(SIMD_Scalar<T,N>& B0, SIMD_Scalar<T,N>& B1,
                            SIMD_Scalar<T,N>& B2, SIMD_Scalar<T,N>& B3)
         {
         static_assert(N == 4, "4x4 transpose");
         SIMD_Scalar<T,N> T0({B0.m_v[0], B1.m_v[0], B2.m_v[0], B3.m_v[0]});
         SIMD_Scalar<T,N> T1({B0.m_v[1], B1.m_v[1], B2.m_v[1], B3.m_v[1]});
         SIMD_Scalar<T,N> T2({B0.m_v[2], B1.m_v[2], B2.m_v[2], B3.m_v[2]});
         SIMD_Scalar<T,N> T3({B0.m_v[3], B1.m_v[3], B2.m_v[3], B3.m_v[3]});

         B0 = T0;
         B1 = T1;
         B2 = T2;
         B3 = T3;
         }

   private:
      SIMD_Scalar(std::initializer_list<T> B)
         {
         size_t i = 0;
         for(auto v = B.begin(); v != B.end(); ++v)
            m_v[i++] = *v;
         }

      T m_v[N];
   };

}

#endif