File: vec_generic.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 (192 lines) | stat: -rw-r--r-- 4,380 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
//  generic vector class
//
//  Copyright (C) 2010 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_GENERIC_HPP
#define VEC_GENERIC_HPP

#include <cmath>
#include <algorithm>
#include <numeric>

#include "wrap_arguments.hpp"
#include "detail/math.hpp"
#include "vec_base.hpp"

namespace nova
{

namespace detail
{

template <typename T, int size>
struct array
{
    friend T* get_pointer(array & arg)
    {
        return arg.data;
    }

    friend const T * get_pointer(array const & arg)
    {
        return arg.data;
    }

    T operator[](int index) const
    {
        return data[index];
    }

    T & operator[](int index)
    {
        return data[index];
    }

    T data[size];
};

}

template <typename FloatType>
class vec:
    public vec_base<FloatType, detail::array<FloatType, 4>, 4>
{
    typedef vec_base<FloatType, detail::array<FloatType, 4>, 4> base;

public:
    typedef FloatType float_type;

    static const int size = 4;
    static const int objects_per_cacheline = 64/sizeof(float_type);

    /* @{ */
    /** constructors */
    vec(void)
    {}

    vec(double f)
    {
        base::set_vec(f);
    }

    vec(float f)
    {
        base::set_vec(f);
    }

    vec(vec const & rhs):
        base(rhs.data_)
    {}

    vec(detail::array<FloatType, 4> const & rhs)
    {
        base::data_ = rhs;
    }

    vec(base const & arg):
        base(arg)
    {}

    static bool is_aligned(FloatType * ptr)
    {
        return ((intptr_t)(ptr) & (intptr_t)(sizeof(float) - 1)) == 0;
    }

    static vec gen_zero()
    {
        return vec(0.0);
    }

    static vec gen_ones()
    {
        return vec(1.0);
    }

public:
    /* @} */

    NOVA_SIMD_DELEGATE_OPERATOR_TO_BASE(operator+)
    NOVA_SIMD_DELEGATE_OPERATOR_TO_BASE(operator-)
    NOVA_SIMD_DELEGATE_OPERATOR_TO_BASE(operator*)
    NOVA_SIMD_DELEGATE_OPERATOR_TO_BASE(operator/)

    /* @{ */
    /** unary functions */
    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(abs)
    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(sign)
    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(square)
    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(cube)
    /* @} */

    /* @{ */
    NOVA_SIMD_DELEGATE_BINARY_TO_BASE(max_)
    NOVA_SIMD_DELEGATE_BINARY_TO_BASE(min_)
    /* @} */


    /* @{ */
    /** rounding functions */
    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(round)
    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(frac)
    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(floor)
    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(ceil)
    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(trunc)
    /* @} */

    /* @{ */
    NOVA_SIMD_DEFINE_MADD

    friend inline vec select(vec lhs, vec rhs, vec bitmask)
    {
        vec ret;
        for (int i = 0; i != size; ++i) {
            ret.data_[i] = bitmask.data_[i] != 0 ? rhs.data_[i]
                                                 : lhs.data_[i];
        }
        return ret;
    }

    /** mathematical functions */
    NOVA_SIMD_DELEGATE_BINARY_TO_BASE(pow)
    NOVA_SIMD_DELEGATE_BINARY_TO_BASE(signed_pow)

    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(reciprocal)

    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(log)
    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(log2)
    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(log10)
    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(exp)

    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(sin)
    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(cos)
    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(tan)

    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(asin)
    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(acos)
    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(atan)

    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(tanh)

    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(signed_sqrt)
    NOVA_SIMD_DELEGATE_UNARY_TO_BASE(undenormalize)
    /* @} */
};

} /* namespace nova */

#endif /* VEC_GENERIC_HPP */