File: vec_base.hpp

package info (click to toggle)
supercollider 1%3A3.6.6~repack-2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,792 kB
  • ctags: 25,269
  • sloc: cpp: 177,129; lisp: 63,421; ansic: 11,297; python: 1,787; perl: 766; yacc: 311; sh: 286; lex: 181; ruby: 173; makefile: 168; xml: 13
file content (388 lines) | stat: -rw-r--r-- 10,083 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
//  vector base class
//
//  Copyright (C) 2011 Tim Blechmann
//
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program; see the file COPYING.  If not, write to
//  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//  Boston, MA 02111-1307, USA.

#ifndef VEC_BASE_HPP
#define VEC_BASE_HPP

#include <cassert>
#include <functional>
#include <cstring>

#include "detail/math.hpp"

#if defined(__GNUC__) && defined(NDEBUG)
#define always_inline inline  __attribute__((always_inline))
#else
#define always_inline inline
#endif

#include "stdint.h"

namespace nova {


/* vector base class
 *
 * requirements:
 * - WrappedType is the wrapped scalar type
 * - get_pointer(VecType) should return WrappedType*
 * - VecSize should be the number of WrappedType elements inside a VecType
 *
 */
template <typename WrappedType,
          typename VecType,
          int VecSize>
class vec_base
{
    typedef union {
        WrappedType f[VecSize];
        VecType vec;
    } cast_unit;

public:
    static const int size = VecSize;
    static const bool has_compare_bitmask = false;

protected:
    vec_base (void)
    {}

public:
    vec_base (VecType arg):
        data_(arg)
    {}

    operator VecType (void) const
    {
        return data_;
    }

public:
    /* @{ */
    /** io */
    void load(const WrappedType * src)
    {
        cast_unit u;
        for (int i = 0; i != size; ++i)
            u.f[i] = src[i];
        data_ = u.vec;
    }

    void load_first(const WrappedType * src)
    {
        cast_unit u;
        u.f[0] = *src;
        for (int i = 1; i != size; ++i)
            u.f[i] = 0;
        data_ = u.vec;
    }

    void load_aligned(const WrappedType * data)
    {
        load(data);
    }

    void store(WrappedType * dest) const
    {
        cast_unit u;
        u.vec = data_;
        for (int i = 0; i != size; ++i)
            dest[i] = u.f[i];
    }

    void store_aligned(WrappedType * dest) const
    {
        store(dest);
    }

    void store_aligned_stream(WrappedType * dest) const
    {
        store(dest);
    }

    void clear(void)
    {
        set_vec(0);
    }
    /* @} */


    /* @{ */
    /** element access */
    WrappedType get (int index) const
    {
        assert(index < size);
        cast_unit u;
        u.vec = data_;
        return u.f[index];
    }

    void set (int index, WrappedType arg)
    {
        cast_unit u;
        u.vec = data_;
        u.f[index] = arg;
        data_ = u.vec;
    }

    void set_vec (WrappedType value)
    {
        cast_unit u;
        for (int i = 0; i != size; ++i)
            u.f[i] = value;
        data_ = u.vec;
    }

    WrappedType set_slope(WrappedType start, WrappedType slope)
    {
        WrappedType diff = 0;
        cast_unit u;

        for (int i = 0; i != size; ++i)
        {
            u.f[i] = start + diff;
            diff += slope;
        }
        data_ = u.vec;
        return diff;
    }

    WrappedType set_exp(WrappedType start, WrappedType curve)
    {
        WrappedType value = start;
        cast_unit u;
        for (int i = 0; i != size; ++i)
        {
            u.f[i] = value;
            value *= curve;
        }
        data_ = u.vec;
        return value;
    }
    /* @} */

private:
    template <typename Functor>
    static always_inline VecType apply_unary(VecType const & arg, Functor const & f)
    {
        cast_unit u;
        u.vec = arg;

        for (int i = 0; i != VecSize; ++i)
            u.f[i] = f(u.f[i]);
        return u.vec;
    }

    template <typename Functor>
    static always_inline VecType apply_binary(VecType const & arg1, VecType const & arg2, Functor const & f)
    {
        cast_unit a1, a2, ret;
        a1.vec = arg1;
        a2.vec = arg2;

        for (int i = 0; i != VecSize; ++i)
            ret.f[i] = f(a1.f[i], a2.f[i]);
        return ret.vec;
    }

public:
    vec_base operator+(vec_base const & rhs) const
    {
        return vec_base::apply_binary(data_, rhs.data_, std::plus<WrappedType>());
    }

    vec_base operator-(vec_base const & rhs) const
    {
        return vec_base::apply_binary(data_, rhs.data_, std::minus<WrappedType>());
    }

    vec_base operator*(vec_base const & rhs) const
    {
        return vec_base::apply_binary(data_, rhs.data_, std::multiplies<WrappedType>());
    }

    vec_base operator/(vec_base const & rhs) const
    {
        return vec_base::apply_binary(data_, rhs.data_, std::divides<WrappedType>());
    }

    vec_base & operator+=(vec_base const & rhs)
    {
        data_ = vec_base::apply_binary(data_, rhs.data_, std::plus<WrappedType>());
        return *this;
    }

    vec_base & operator-=(vec_base const & rhs)
    {
        data_ = vec_base::apply_binary(data_, rhs.data_, std::minus<WrappedType>());
        return *this;
    }

    vec_base & operator*=(vec_base const & rhs)
    {
        data_ = vec_base::apply_binary(data_, rhs.data_, std::multiplies<WrappedType>());
        return *this;
    }

    vec_base & operator/=(vec_base const & rhs)
    {
        data_ = vec_base::apply_binary(data_, rhs.data_, std::divides<WrappedType>());
        return *this;
    }

    vec_base operator<(vec_base const & rhs) const
    {
        return vec_base::apply_binary(data_, rhs.data_, std::less<WrappedType>());
    }

    vec_base operator<=(vec_base const & rhs) const
    {
        return vec_base::apply_binary(data_, rhs.data_, std::less_equal<WrappedType>());
    }

    vec_base operator==(vec_base const & rhs) const
    {
        return vec_base::apply_binary(data_, rhs.data_, std::equal_to<WrappedType>());
    }

    vec_base operator!=(vec_base const & rhs) const
    {
        return vec_base::apply_binary(data_, rhs.data_, std::not_equal_to<WrappedType>());
    }

    vec_base operator>(vec_base const & rhs) const
    {
        return vec_base::apply_binary(data_, rhs.data_, std::greater<WrappedType>());
    }

    vec_base operator>=(vec_base const & rhs) const
    {
        return vec_base::apply_binary(data_, rhs.data_, std::greater_equal<WrappedType>());
    }

#define DEFINE_UNARY_STATIC(NAME, METHOD)                   \
    static always_inline VecType NAME(VecType const & arg)  \
    {                                                       \
       return apply_unary(arg, METHOD<WrappedType>);        \
    }

#define DEFINE_BINARY_STATIC(NAME, METHOD)                                          \
    static always_inline VecType NAME(VecType const & arg1, VecType const & arg2)   \
    {                                                                               \
       return apply_binary(arg1, arg2, METHOD<WrappedType>);                        \
    }


protected:
    DEFINE_UNARY_STATIC(reciprocal, detail::reciprocal)

    DEFINE_UNARY_STATIC(sin, detail::sin)
    DEFINE_UNARY_STATIC(cos, detail::cos)
    DEFINE_UNARY_STATIC(tan, detail::tan)

    DEFINE_UNARY_STATIC(asin, detail::asin)
    DEFINE_UNARY_STATIC(acos, detail::acos)
    DEFINE_UNARY_STATIC(atan, detail::atan)

    DEFINE_UNARY_STATIC(tanh, detail::tanh)

    DEFINE_UNARY_STATIC(log, detail::log)
    DEFINE_UNARY_STATIC(log2, detail::log2)
    DEFINE_UNARY_STATIC(log10, detail::log10)
    DEFINE_UNARY_STATIC(exp, detail::exp)
    DEFINE_UNARY_STATIC(signed_sqrt, detail::signed_sqrt)

    DEFINE_UNARY_STATIC(round, detail::round)
    DEFINE_UNARY_STATIC(ceil, detail::ceil)
    DEFINE_UNARY_STATIC(floor, detail::floor)
    DEFINE_UNARY_STATIC(frac, detail::frac)
    DEFINE_UNARY_STATIC(trunc, detail::trunc)

    DEFINE_BINARY_STATIC(pow, detail::pow)
    DEFINE_BINARY_STATIC(signed_pow, detail::signed_pow)

    DEFINE_UNARY_STATIC(abs, detail::fabs)
    DEFINE_UNARY_STATIC(sign, detail::sign)
    DEFINE_UNARY_STATIC(square, detail::square)
    DEFINE_UNARY_STATIC(cube, detail::cube)

    DEFINE_BINARY_STATIC(max_, detail::max)
    DEFINE_BINARY_STATIC(min_, detail::min)

    DEFINE_UNARY_STATIC(undenormalize, detail::undenormalize)

public:
    WrappedType horizontal_min(void) const
    {
        cast_unit u;
        u.vec = data_;
        return *std::min_element(u.f, u.f + size);
    }

    WrappedType horizontal_max(void) const
    {
        cast_unit u;
        u.vec = data_;
        return *std::max_element(u.f, u.f + size);
    }

    WrappedType horizontal_sum(void) const
    {
        cast_unit u;
        u.vec = data_;
        WrappedType ret = 0;
        for (int i = 0; i != size; ++i)
            ret += u.f[i];
        return ret;
    }

protected:
    VecType data_;
};

}

#define NOVA_SIMD_DELEGATE_UNARY_TO_BASE(NAME)  \
    inline friend vec NAME(vec const & arg)     \
    {                                           \
        return base::NAME(arg.data_);            \
    }

#define NOVA_SIMD_DELEGATE_OPERATOR_TO_BASE(NAME)  \
    inline vec NAME(vec const & rhs) const         \
    {                                              \
        return base::NAME(rhs.data_);               \
    }

#define NOVA_SIMD_DELEGATE_BINARY_TO_BASE(NAME)                 \
    inline friend vec NAME(vec const & arg1, vec const & arg2)  \
    {                                                           \
        return base::NAME(arg1.data_, arg2.data_);               \
    }

#define NOVA_SIMD_DEFINE_MADD                                   \
    inline friend vec madd(vec const & arg1, vec const & arg2, vec const & arg3)  \
    {                                                           \
        return arg1 * arg2 + arg3;                       \
    }

#undef always_inline


#endif /* VEC_BASE_HPP */