summaryrefslogtreecommitdiff
path: root/src/simd/Simd4i.h
blob: 803c8e5eff066119f6ea162f8c953e53805cf891 (plain) (blame)
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
// This code contains NVIDIA Confidential Information and is disclosed to you
// under a form of NVIDIA software license agreement provided separately to you.
//
// Notice
// NVIDIA Corporation and its licensors retain all intellectual property and
// proprietary rights in and to this software and related documentation and
// any modifications thereto. Any use, reproduction, disclosure, or
// distribution of this software and related documentation without an express
// license agreement from NVIDIA Corporation is strictly prohibited.
//
// ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
// NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
// THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
// MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
//
// Information and code furnished is believed to be accurate and reliable.
// However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
// information or for any infringement of patents or other rights of third parties that may
// result from its use. No license is granted by implication or otherwise under any patent
// or patent rights of NVIDIA Corporation. Details are subject to change without notice.
// This code supersedes and replaces all information previously supplied.
// NVIDIA Corporation products are not authorized for use as critical
// components in life support devices or systems without express written approval of
// NVIDIA Corporation.
//
// Copyright (c) 2008-2014 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.

#pragma once

#include "SimdTypes.h"

template <typename T>
struct Simd4iFactory
{
	Simd4iFactory(T v_) : v(v_)
	{
	}
	inline operator Simd4i() const;
	inline operator Scalar4i() const;
	Simd4iFactory& operator=(const Simd4iFactory&); // not implemented
	T v;
};

template <>
struct Simd4iFactory<detail::FourTuple>
{
	Simd4iFactory(int x, int y, int z, int w)
	{
		v[0] = x, v[1] = y, v[2] = z, v[3] = w;
	}
	Simd4iFactory(const Simd4iFactory<const int&>& f)
	{
		v[3] = v[2] = v[1] = v[0] = f.v;
	}
	inline operator Simd4i() const;
	inline operator Scalar4i() const;
	Simd4iFactory& operator=(const Simd4iFactory&); // not implemented
	NVMATH_ALIGN(16, int) v[4];
};

template <int i>
struct Simd4iFactory<detail::IntType<i> >
{
	inline operator Simd4i() const;
	inline operator Scalar4i() const;
};

// forward declaration
template <typename>
struct Simd4fFactory;

// map Simd4f/Scalar4f to Simd4i/Scalar4i
template <typename>
struct Simd4fToSimd4i;
template <>
struct Simd4fToSimd4i<Simd4f>
{
	typedef Simd4i Type;
};
template <>
struct Simd4fToSimd4i<Scalar4f>
{
	typedef Scalar4i Type;
};

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// expression template
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#if NVMATH_DISTINCT_TYPES
inline Simd4i operator&(const ComplementExpr<Simd4i>&, const Simd4i&);
inline Simd4i operator&(const Simd4i&, const ComplementExpr<Simd4i>&);
#endif

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// operators
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#if NVMATH_DISTINCT_TYPES

/*! \brief Vector bit-wise NOT operator
* \return A vector holding the bit-negate of \a v.
* \relates Simd4i */
inline ComplementExpr<Simd4i> operator~(const Simd4i& v);

/*! \brief Vector bit-wise AND operator
* \return A vector holding the bit-wise AND of \a v0 and \a v1.
* \relates Simd4i */
inline Simd4i operator&(const Simd4i& v0, const Simd4i& v1);

/*! \brief Vector bit-wise OR operator
* \return A vector holding the bit-wise OR of \a v0 and \a v1.
* \relates Simd4i */
inline Simd4i operator|(const Simd4i& v0, const Simd4i& v1);

/*! \brief Vector bit-wise XOR operator
* \return A vector holding the bit-wise XOR of \a v0 and \a v1.
* \relates Simd4i */
inline Simd4i operator^(const Simd4i& v0, const Simd4i& v1);

/*! \brief Vector logical left shift.
* \return A vector with 4 elements of \a v0, each shifted left by \a shift bits.
* \relates Simd4i */
inline Simd4i operator<<(const Simd4i& v, int shift);

/*! \brief Vector logical right shift.
* \return A vector with 4 elements of \a v0, each shifted right by \a shift bits.
* \relates Simd4i */
inline Simd4i operator>>(const Simd4i& v, int shift);

#if NVMATH_SHIFT_BY_VECTOR

/*! \brief Vector logical left shift.
* \return A vector with 4 elements of \a v0, each shifted left by \a shift bits.
* \relates Simd4i */
inline Simd4i operator<<(const Simd4i& v, const Simd4i& shift);

/*! \brief Vector logical right shift.
* \return A vector with 4 elements of \a v0, each shifted right by \a shift bits.
* \relates Simd4i */
inline Simd4i operator>>(const Simd4i& v, const Simd4i& shift);

#endif // NVMATH_SHIFT_BY_VECTOR

#endif // NVMATH_DISTINCT_TYPES

namespace simdi // disambiguate for VMX
{
// note: operator?= missing because they don't have corresponding intrinsics.

/*! \brief Test for equality of two vectors.
* \return Vector of per element result mask (all bits set for 'true', none set for 'false').
* \relates Simd4i */
inline Simd4i operator==(const Simd4i& v0, const Simd4i& v1);

// no !=, <=, >= because VMX128/SSE don't support it, use ~equal etc.

/*! \brief Less-compare all elements of two *signed* vectors.
* \return Vector of per element result mask (all bits set for 'true', none set for 'false').
* \relates Simd4i */
inline Simd4i operator<(const Simd4i& v0, const Simd4i& v1);

/*! \brief Greater-compare all elements of two *signed* vectors.
* \return Vector of per element result mask (all bits set for 'true', none set for 'false').
* \relates Simd4i */
inline Simd4i operator>(const Simd4i& v0, const Simd4i& v1);

/*! \brief Vector addition operator
* \return A vector holding the component-wise sum of \a v0 and \a v1.
* \relates Simd4i */
inline Simd4i operator+(const Simd4i& v0, const Simd4i& v1);

/*! \brief Unary vector negation operator.
* \return A vector holding the component-wise negation of \a v.
* \relates Simd4i */
inline Simd4i operator-(const Simd4i& v);

/*! \brief Vector subtraction operator.
* \return A vector holding the component-wise difference of \a v0 and \a v1.
* \relates Simd4i */
inline Simd4i operator-(const Simd4i& v0, const Simd4i& v1);

} // namespace simdi

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// functions
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

/*! \brief Load int value into all vector components.
* \relates Simd4i */
inline Simd4iFactory<const int&> simd4i(const int& s)
{
	return Simd4iFactory<const int&>(s);
}

/*! \brief Load 4 int values into vector.
* \relates Simd4i */
inline Simd4iFactory<detail::FourTuple> simd4i(int x, int y, int z, int w)
{
	return Simd4iFactory<detail::FourTuple>(x, y, z, w);
}

/*! \brief Create vector from literal.
* \return Vector with all elements set to \c i.
* \relates Simd4i */
template <int i>
inline Simd4iFactory<detail::IntType<i> > simd4i(const detail::IntType<i>&)
{
	return Simd4iFactory<detail::IntType<i> >();
}

template <>
inline Simd4iFactory<detail::IntType<1> > simd4i(const detail::IntType<1>&)
{
	return Simd4iFactory<detail::IntType<1> >();
}

template <>
inline Simd4iFactory<detail::IntType<int(0x80000000)> > simd4i(const detail::IntType<int(0x80000000)>&)
{
	return Simd4iFactory<detail::IntType<int(0x80000000)> >();
}

template <>
inline Simd4iFactory<detail::IntType<-1> > simd4i(const detail::IntType<-1>&)
{
	return Simd4iFactory<detail::IntType<-1> >();
}

/*! \brief Reinterpret Simd4f as Simd4i.
* \return A copy of \a v, but reinterpreted as Simd4i.
* \relates Simd4i */
inline Simd4i simd4i(const Simd4f& v);

/*! \brief Reinterpret Simd4fFactory as Simd4iFactory.
* \relates Simd4i */
template <typename T>
inline Simd4iFactory<T> simd4i(const Simd4fFactory<T>& v)
{
	return reinterpret_cast<const Simd4iFactory<T>&>(v);
}

/*! \brief Truncate Simd4f to Simd4i.
* \relates Simd4i */
inline Simd4i truncate(const Simd4f& v);

namespace simdi
{

/*! \brief return reference to contiguous array of vector elements
* \relates Simd4i */
inline int (&array(Simd4i& v))[4];

/*! \brief return constant reference to contiguous array of vector elements
* \relates Simd4i */
inline const int (&array(const Simd4i& v))[4];

} // namespace simdi

/*! \brief Create vector from int array.
* \relates Simd4i */
inline Simd4iFactory<const int*> load(const int* ptr)
{
	return ptr;
}

/*! \brief Create vector from aligned int array.
* \note \a ptr needs to be 16 byte aligned.
* \relates Simd4i */
inline Simd4iFactory<detail::AlignedPointer<int> > loadAligned(const int* ptr)
{
	return detail::AlignedPointer<int>(ptr);
}

/*! \brief Create vector from aligned float array.
* \param offset pointer offset in bytes.
* \note \a ptr+offset needs to be 16 byte aligned.
* \relates Simd4i */
inline Simd4iFactory<detail::OffsetPointer<int> > loadAligned(const int* ptr, unsigned int offset)
{
	return detail::OffsetPointer<int>(ptr, offset);
}

/*! \brief Store vector \a v to int array \a ptr.
* \relates Simd4i */
inline void store(int* ptr, const Simd4i& v);

/*! \brief Store vector \a v to aligned int array \a ptr.
* \note \a ptr needs to be 16 byte aligned.
* \relates Simd4i */
inline void storeAligned(int* ptr, const Simd4i& v);

/*! \brief Store vector \a v to aligned int array \a ptr.
* \param offset pointer offset in bytes.
* \note \a ptr+offset needs to be 16 byte aligned.
* \relates Simd4i */
inline void storeAligned(int* ptr, unsigned int offset, const Simd4i& v);

#if NVMATH_DISTINCT_TYPES

/*! \brief replicate i-th component into all vector components.
* \return Vector with all elements set to \a v[i].
* \relates Simd4i */
template <size_t i>
inline Simd4i splat(const Simd4i& v);

/*! \brief Select \a v0 or \a v1 based on \a mask.
* \return mask ? v0 : v1
* \relates Simd4i */
inline Simd4i select(const Simd4i& mask, const Simd4i& v0, const Simd4i& v1);

#endif // NVMATH_DISTINCT_TYPES

namespace simdi // disambiguate for VMX
{

/*! \brief returns non-zero if all elements or \a v0 and \a v1 are equal
* \relates Simd4i */
inline int allEqual(const Simd4i& v0, const Simd4i& v1);

/*! \brief returns non-zero if all elements or \a v0 and \a v1 are equal
* \param outMask holds the result of \a v0 == \a v1.
* \relates Simd4i */
inline int allEqual(const Simd4i& v0, const Simd4i& v1, Simd4i& outMask);

/*! \brief returns non-zero if any elements or \a v0 and \a v1 are equal
* \relates Simd4i */
inline int anyEqual(const Simd4i& v0, const Simd4i& v1);

/*! \brief returns non-zero if any elements or \a v0 and \a v1 are equal
* \param outMask holds the result of \a v0 == \a v1.
* \relates Simd4i */
inline int anyEqual(const Simd4i& v0, const Simd4i& v1, Simd4i& outMask);

/*! \brief returns non-zero if all *signed* elements or \a v0 and \a v1 are greater
* \relates Simd4i */
inline int allGreater(const Simd4i& v0, const Simd4i& v1);

/*! \brief returns non-zero if all *signed* elements or \a v0 and \a v1 are greater
* \param outMask holds the result of \a v0 == \a v1.
* \relates Simd4i */
inline int allGreater(const Simd4i& v0, const Simd4i& v1, Simd4i& outMask);

/*! \brief returns non-zero if any elements or \a v0 and \a v1 are greater
* \relates Simd4i */
inline int anyGreater(const Simd4i& v0, const Simd4i& v1);

/*! \brief returns non-zero if any elements or \a v0 and \a v1 are greater
* \param outMask holds the result of \a v0 == \a v1.
* \relates Simd4i */
inline int anyGreater(const Simd4i& v0, const Simd4i& v1, Simd4i& outMask);

} // namespace simdi

#if NVMATH_DISTINCT_TYPES

/*! \brief returns non-zero if all elements are true
* \note undefined if parameter is not result of a comparison.
* \relates Simd4i */
inline int allTrue(const Simd4i& v);

/*! \brief returns non-zero if any element is true
* \note undefined if parameter is not result of a comparison.
* \relates Simd4i */
inline int anyTrue(const Simd4i& v);

#endif // NVMATH_DISTINCT_TYPES

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// platform specific includes
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

#if NVMATH_SSE2
#include "sse2/Simd4i.h"
#elif NVMATH_VMX128
#include "xbox360/Simd4i.h"
#elif NVMATH_ALTIVEC
#include "ps3/Simd4i.h"
#elif NVMATH_NEON
#include "neon/Simd4i.h"
#endif

#if NVMATH_SCALAR
#include "scalar/Simd4i.h"
#endif