~ubuntu-branches/ubuntu/raring/vc/raring-proposed

« back to all changes in this revision

Viewing changes to tests/scalaraccess.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-03-08 12:50:59 UTC
  • Revision ID: package-import@ubuntu.com-20130308125059-2vpu3hm02kgrqv96
Tags: upstream-0.7.0
ImportĀ upstreamĀ versionĀ 0.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  This file is part of the Vc library.
 
2
 
 
3
    Copyright (C) 2010-2012 Matthias Kretz <kretz@kde.org>
 
4
 
 
5
    Vc is free software: you can redistribute it and/or modify
 
6
    it under the terms of the GNU Lesser General Public License as
 
7
    published by the Free Software Foundation, either version 3 of
 
8
    the License, or (at your option) any later version.
 
9
 
 
10
    Vc is distributed in the hope that it will be useful, but
 
11
    WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
    GNU Lesser General Public License for more details.
 
14
 
 
15
    You should have received a copy of the GNU Lesser General Public
 
16
    License along with Vc.  If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
*/
 
19
 
 
20
#include <Vc/Vc>
 
21
#include "unittest.h"
 
22
 
 
23
using namespace Vc;
 
24
 
 
25
template<typename V> void reads()
 
26
{
 
27
    typedef typename V::EntryType T;
 
28
    typedef typename V::IndexType I;
 
29
 
 
30
    V a = V::Zero();
 
31
    const T zero = 0;
 
32
    for (int i = 0; i < V::Size; ++i) {
 
33
        const T x = a[i];
 
34
        COMPARE(x, zero);
 
35
    }
 
36
    a = static_cast<V>(I::IndexesFromZero());
 
37
    for (int i = 0; i < V::Size; ++i) {
 
38
        const T x = a[i];
 
39
        const T y = i;
 
40
        COMPARE(x, y);
 
41
    }
 
42
}
 
43
 
 
44
template<typename V, size_t Index>
 
45
inline void readsConstantIndexTest(VC_ALIGNED_PARAMETER(V) a, VC_ALIGNED_PARAMETER(V) b)
 
46
{
 
47
    typedef typename V::EntryType T;
 
48
    {
 
49
        const T x = a[Index];
 
50
        const T zero = 0;
 
51
        COMPARE(x, zero) << Index;
 
52
    }{
 
53
        const T x = b[Index];
 
54
        const T y = Index;
 
55
        COMPARE(x, y) << Index;
 
56
    }
 
57
}
 
58
 
 
59
template<typename V, size_t Index>
 
60
struct ReadsConstantIndex
 
61
{
 
62
    ReadsConstantIndex(VC_ALIGNED_PARAMETER(V) a, VC_ALIGNED_PARAMETER(V) b)
 
63
    {
 
64
        readsConstantIndexTest<V, Index>(a, b);
 
65
        ReadsConstantIndex<V, Index - 1>(a, b);
 
66
    }
 
67
};
 
68
 
 
69
 
 
70
template<typename V>
 
71
struct ReadsConstantIndex<V, 0>
 
72
{
 
73
    ReadsConstantIndex(VC_ALIGNED_PARAMETER(V) a, VC_ALIGNED_PARAMETER(V) b)
 
74
    {
 
75
        readsConstantIndexTest<V, 0>(a, b);
 
76
    }
 
77
};
 
78
 
 
79
template<typename V> void readsConstantIndex()
 
80
{
 
81
    typedef typename V::IndexType I;
 
82
 
 
83
    V a = V::Zero();
 
84
    V b = static_cast<V>(I::IndexesFromZero());
 
85
    ReadsConstantIndex<V, V::Size - 1>(a, b);
 
86
}
 
87
 
 
88
template<typename V> void writes()
 
89
{
 
90
    typedef typename V::EntryType T;
 
91
    typedef typename V::IndexType I;
 
92
 
 
93
    V a;
 
94
    for (int i = 0; i < V::Size; ++i) {
 
95
        a[i] = static_cast<T>(i);
 
96
    }
 
97
    V b = static_cast<V>(I::IndexesFromZero());
 
98
    COMPARE(a, b);
 
99
 
 
100
    const T one = 1;
 
101
    const T two = 2;
 
102
 
 
103
    if (V::Size == 1) {
 
104
        a(a == 0) += one;
 
105
        a[0] += one;
 
106
        a(a == 0) += one;
 
107
        COMPARE(a, V(2));
 
108
    } else if (V::Size == 4) {
 
109
        a(a == 1) += two;
 
110
        a[2] += one;
 
111
        a(a == 3) += one;
 
112
        b(b == 1) += one;
 
113
        b(b == 2) += one;
 
114
        b(b == 3) += one;
 
115
        COMPARE(a, b);
 
116
    } else if (V::Size == 8 || V::Size == 16) {
 
117
        a(a == 2) += two;
 
118
        a[3] += one;
 
119
        a(a == 4) += one;
 
120
        b(b == 2) += one;
 
121
        b(b == 3) += one;
 
122
        b(b == 4) += one;
 
123
        COMPARE(a, b);
 
124
    } else if (V::Size == 2) { // a = [0, 1]; b = [0, 1]
 
125
        a(a == 0) += two;      // a = [2, 1]
 
126
        a[1] += one;           // a = [2, 2]
 
127
        a(a == 2) += one;      // a = [3, 3]
 
128
        b(b == 0) += one;      // b = [1, 1]
 
129
        b(b == 1) += one;      // b = [2, 2]
 
130
        b(b == 2) += one;      // b = [3, 3]
 
131
        COMPARE(a, b);
 
132
    } else {
 
133
        FAIL() << "unsupported Vector::Size";
 
134
    }
 
135
}
 
136
 
 
137
int main(int argc, char **argv)
 
138
{
 
139
    initTest(argc, argv);
 
140
 
 
141
    testAllTypes(reads);
 
142
    testAllTypes(writes);
 
143
    testAllTypes(readsConstantIndex);
 
144
    //testAllTypes(writesConstantIndex);
 
145
 
 
146
    return 0;
 
147
}