File: scatter.cpp

package info (click to toggle)
vc 1.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,552 kB
  • sloc: cpp: 19,220; ansic: 15,669; sh: 453; xml: 186; makefile: 30
file content (213 lines) | stat: -rw-r--r-- 7,431 bytes parent folder | download | duplicates (2)
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
/*  This file is part of the Vc library. {{{
Copyright © 2009-2014 Matthias Kretz <kretz@kde.org>

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the names of contributing organizations nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

}}}*/
// includes {{{1
#include "unittest.h"
#include <iostream>
#include <cstring>
#include <Vc/array>

using namespace Vc;

TEST_TYPES(Vec, scatterArray, AllTypes) //{{{1
{
    typedef typename Vec::EntryType T;
    typedef typename Vec::IndexType It;
    constexpr int count = 31999;
    Vc::array<T, count> array, out;
    for (int i = 0; i < count; ++i) {
        array[i] = i;
        if (!std::is_integral<T>::value || !std::is_unsigned<T>::value) {
            array[i] -= 100;
        }
    }
    typename It::Mask mask;
    for (It i(IndexesFromZero); !(mask = (i < count)).isEmpty(); i += Vec::Size) {
        auto castedMask = simd_cast<typename Vec::Mask>(mask);
        if (all_of(castedMask)) {
            Vec a(&array[0], i);
            a += 1;
            a.scatter(&out[0], i);
        } else {
            Vec a(&array[0], i, castedMask);
            a += 1;
            a.scatter(&out[0], i, castedMask);
        }
    }
    for (int i = 0; i < count; ++i) {
        array[i] += 1;
        COMPARE(array[i], out[i]);
    }
    COMPARE(0, std::memcmp(&array[0], &out[0], count * sizeof(typename Vec::EntryType)));

    for (It i(IndexesFromZero); !(mask = (i < count)).isEmpty(); i += Vec::Size) {
        auto castedMask = simd_cast<typename Vec::Mask>(mask);
        if (all_of(castedMask)) {
            Vec a = array[i];
            out[i] = a + 1;
        } else {
            Vec a;
            where(castedMask) | a = array[i];
            where(castedMask) | out[i] = a + 1;
        }
    }
    for (int i = 0; i < count; ++i) {
        array[i] += 1;
        COMPARE(array[i], out[i]);
    }
    COMPARE(0, std::memcmp(&array[0], &out[0], count * sizeof(typename Vec::EntryType)));
}

TEST_TYPES(Vec, maskedScatterArray, AllTypes) //{{{1
{
    typedef typename Vec::IndexType It;
    typedef typename Vec::EntryType T;

    Vc::array<T, Vec::Size> mem;
    const Vec v = Vec([](T n) { return n + 1; });

    withRandomMask<Vec>([&](typename Vec::mask_type m) {
        Vec(0).store(&mem[0], Vc::Unaligned);
        where(m) | mem[It([](int n) { return n; })] = v;

        Vec reference = v;
        reference.setZeroInverted(m);

        COMPARE(Vec(&mem[0], Vc::Unaligned), reference) << "m = " << m;
    });
}

//struct Struct {{{1
template <typename T, size_t Align = std::is_arithmetic<T>::value ? sizeof(T) : alignof(T)>
struct alignas(Align > alignof(short) ? Align : alignof(short)) Struct
{
    T a;
    char x;
    T b;
    short y;
    T c;
    char z;
};

TEST_TYPES(Vec, scatterStruct, AllTypes) //{{{1
{
    typedef typename Vec::IndexType It;
    typedef Struct<typename Vec::EntryType> S;
    constexpr int count = 3999;
    Vc::array<S, count> array, out;
    memset(&array[0], 0, count * sizeof(S));
    memset(&out[0], 0, count * sizeof(S));
    for (int i = 0; i < count; ++i) {
        array[i].a = i;
        array[i].b = i + 1;
        array[i].c = i + 2;
    }
    typename It::Mask mask;
    for (It i(IndexesFromZero); !(mask = (i < count)).isEmpty(); i += Vec::Size) {
        auto castedMask = simd_cast<typename Vec::Mask>(mask);
        Vec a; a(castedMask) = array[i][&S::a];
        where(castedMask) | out[i][&S::a] = a;
        Vec b; b(castedMask) = array[i][&S::b];
        where(castedMask) | out[i][&S::b] = b;
        Vec c; c(castedMask) = array[i][&S::c];
        where(castedMask) | out[i][&S::c] = c;
    }
    VERIFY(0 == memcmp(&array[0], &out[0], count * sizeof(S)));
}

template<typename T> struct Struct2 //{{{1
{
    char x;
    Struct<T> b;
    short y;
};

constexpr int scatterStruct2Count = 97;

template<typename T>
static std::ostream &operator<<(std::ostream &out, const Struct2<T> &s)
{
    return out << '{' << s.b.a << ' ' << s.b.b << ' ' << s.b.c << '}';
}

template<typename T>
static std::ostream &operator<<(std::ostream &out, const Struct2<T> *s)
{
    for (int i = 0; i < scatterStruct2Count; ++i) {
        out << s[i];
    }
    return out;
}

template <typename T, std::size_t N>
static std::ostream &operator<<(std::ostream &out, const Vc::array<T, N> &x)
{
    out << x[0];
    for (std::size_t i = 1; i < N; ++i) {
        out << ' ' << x[i];
    }
    return out;
}

template<typename V> V makeReference(V v, typename V::Mask m)
{
    v.setZero(!m);
    return v;
}
TEST_TYPES(Vec, scatterStruct2, AllTypes) //{{{1
{
    typedef typename Vec::IndexType It;
    typedef Struct2<typename Vec::EntryType> S1;
    typedef Struct<typename Vec::EntryType> S2;
    Vc::array<S1, scatterStruct2Count> array, out;
    memset(&array[0], 0, scatterStruct2Count * sizeof(S1));
    memset(&out[0], 0, scatterStruct2Count * sizeof(S1));
    for (int i = 0; i < scatterStruct2Count; ++i) {
        array[i].b.a = i + 0;
        array[i].b.b = i + 1;
        array[i].b.c = i + 2;
    }
    typename It::Mask mask;
    typename Vec::Mask castedMask;
    for (It i(IndexesFromZero); !(mask = (i < scatterStruct2Count)).isEmpty(); i += Vec::Size) {
        castedMask = simd_cast<decltype(castedMask)>(mask);
        Vec a = Vec(); a(castedMask) = array[i][&S1::b][&S2::a];
        Vec b = Vec(); b(castedMask) = array[i][&S1::b][&S2::b];
        Vec c = Vec(); c(castedMask) = array[i][&S1::b][&S2::c];
        COMPARE(a, Vc::simd_cast<Vec>(makeReference(i, mask)));
        COMPARE(b, Vc::simd_cast<Vec>(makeReference(i + 1, mask)));
        COMPARE(c, Vc::simd_cast<Vec>(makeReference(i + 2, mask)));
        where(castedMask) | out[i][&S1::b][&S2::a] = a;
        where(castedMask) | out[i][&S1::b][&S2::b] = b;
        where(castedMask) | out[i][&S1::b][&S2::c] = c;
    }
    // castedmask != mask here because mask is changed in the for loop, but castedmask has the value
    // from the previous iteration
    VERIFY(0 == memcmp(&array[0], &out[0], scatterStruct2Count * sizeof(S1))) << mask << ' ' << castedMask << '\n'
        << array << '\n' << out;
}

// vim: foldmethod=marker