forked from berthubert/hello-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fvector.hh
214 lines (189 loc) · 3.66 KB
/
fvector.hh
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
#pragma once
#include <array>
#include <numeric>
#include <iostream>
template<unsigned int W>
struct fvector
{
float v __attribute__((vector_size (4*W)));
fvector()
{}
fvector(float v0)
{
for(unsigned int n=0 ; n < W; ++n)
v[n] = v0;
}
// be VERY careful, fvector<8>{0.0} will call this
fvector(const std::initializer_list<float>& in)
{
unsigned char ctr=0;
for(const auto& val: in)
v[ctr++] = val;
for(; ctr < W; ++ctr)
v[ctr] = 0.0;
}
fvector& operator=(float val)
{
*this = fvector(val);
return *this;
}
fvector operator+(const fvector& rhs)
{
fvector ret = *this;
ret.v += rhs.v;
return ret;
}
fvector operator-(const fvector& rhs) const
{
fvector ret = *this;
ret.v -= rhs.v;
return ret;
}
fvector operator-() const
{
fvector ret = *this;
ret.v = -ret.v;
return ret;
}
fvector operator!() const
{
fvector ret = *this;
for(unsigned int n = 0 ; n < W; ++n)
ret.v[n] = !v[n];
return ret;
}
fvector operator*(const float& rhs) const
{
fvector ret = *this;
ret.v *= rhs;
return ret;
}
fvector operator*(const fvector& rhs) const
{
fvector ret = *this;
ret.v *= rhs.v;
return ret;
}
fvector operator/(const fvector& rhs) const
{
fvector ret = *this;
ret.v /= rhs.v;
return ret;
}
fvector& operator/=(const fvector& rhs)
{
v /= rhs.v;
return *this;
}
fvector& operator*=(float rhs)
{
v *= rhs;
return *this;
}
fvector& operator*=(const fvector& rhs)
{
v *= rhs.v;
return *this;
}
fvector& operator+=(const fvector& rhs)
{
v += rhs.v;
return *this;
}
fvector& operator+=(float rhs)
{
v += rhs;
return *this;
}
fvector operator<(const fvector& rhs) const
{
fvector ret;
// you'd think you'd be able to do this:
// ret.v = (v < rhs.v);
// but the output is Weird - if the comparison is valid, the output is 0 (!)
// and if invalid, it is NaN (!!) https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
for(unsigned int i = 0 ; i < W; ++i)
ret.v[i] = v[i] < rhs.v[i];
return ret;
}
bool operator==(const fvector& rhs) const
{
for(unsigned int n=0; n < W; ++n)
if(v[n] != rhs.v[n])
return false;
return true;
}
float sum() const
{
float ret = 0;
for(unsigned int n=0; n < W; ++n)
ret += v[n];
return ret;
}
};
template<unsigned int T>
auto operator*(float v, const fvector<T>& rhs)
{
return rhs * v;
}
template<unsigned int T>
auto operator/(float v, const fvector<T>& rhs)
{
fvector<T> ret = v;
return ret/rhs;
}
template<unsigned int T>
auto operator+(float v, const fvector<T>& rhs)
{
fvector<T> ret = v;
return ret+rhs;
}
template<unsigned int W>
auto exp(const fvector<W>& v)
{
fvector<W> ret;
for(size_t i = 0; i < W ; ++i) {
ret.v[i] = expf(v.v[i]);
}
return ret;
}
template<unsigned int W>
auto log(const fvector<W>& v)
{
fvector<W> ret;
for(size_t i = 0; i < W ; ++i) {
ret.v[i] = logf(v.v[i]);
}
return ret;
}
template<unsigned int W>
auto tanh(const fvector<W>& v)
{
fvector<W> ret;
for(size_t i = 0; i < W ; ++i) {
ret.v[i] = tanhf(v.v[i]);
}
return ret;
}
template<unsigned int W>
std::ostream& operator<<(std::ostream& os, const fvector<W>& o)
{
for(unsigned int n=0 ; n < W; ++n)
os << o.v[n] <<" ";
return os;
}
/*
template<unsigned int W>
void setZero(fvector<W>& o)
{
o=0.0;
}
*/
template<unsigned int W>
auto maxFunc(const fvector<W>& lhs, const fvector<W>& rhs)
{
fvector<W> ret;
for(unsigned int i = 0 ; i < W; ++i)
ret.v[i] = std::max(lhs.v[i], rhs.v[i]);
return ret;
}