1 | // Copyright 2009-2021 Intel Corporation |
2 | // SPDX-License-Identifier: Apache-2.0 |
3 | |
4 | #pragma once |
5 | |
6 | #include "../sys/alloc.h" |
7 | #include "math.h" |
8 | #include "../simd/sse.h" |
9 | |
10 | namespace embree |
11 | { |
12 | //////////////////////////////////////////////////////////////////////////////// |
13 | /// SSE Vec3ia Type |
14 | //////////////////////////////////////////////////////////////////////////////// |
15 | |
16 | struct __aligned(16) Vec3ia |
17 | { |
18 | ALIGNED_STRUCT_(16); |
19 | |
20 | union { |
21 | __m128i m128; |
22 | struct { int x,y,z; }; |
23 | }; |
24 | |
25 | typedef int Scalar; |
26 | enum { N = 3 }; |
27 | |
28 | //////////////////////////////////////////////////////////////////////////////// |
29 | /// Constructors, Assignment & Cast Operators |
30 | //////////////////////////////////////////////////////////////////////////////// |
31 | |
32 | __forceinline Vec3ia( ) {} |
33 | __forceinline Vec3ia( const __m128i a ) : m128(a) {} |
34 | __forceinline Vec3ia( const Vec3ia& other ) : m128(other.m128) {} |
35 | __forceinline Vec3ia& operator =(const Vec3ia& other) { m128 = other.m128; return *this; } |
36 | |
37 | __forceinline explicit Vec3ia( const int a ) : m128(_mm_set1_epi32(i: a)) {} |
38 | __forceinline Vec3ia( const int x, const int y, const int z) : m128(_mm_set_epi32(i3: z, i2: z, i1: y, i0: x)) {} |
39 | __forceinline explicit Vec3ia( const __m128 a ) : m128(_mm_cvtps_epi32(a: a)) {} |
40 | |
41 | __forceinline operator const __m128i&() const { return m128; } |
42 | __forceinline operator __m128i&() { return m128; } |
43 | |
44 | //////////////////////////////////////////////////////////////////////////////// |
45 | /// Constants |
46 | //////////////////////////////////////////////////////////////////////////////// |
47 | |
48 | __forceinline Vec3ia( ZeroTy ) : m128(_mm_setzero_si128()) {} |
49 | __forceinline Vec3ia( OneTy ) : m128(_mm_set1_epi32(i: 1)) {} |
50 | __forceinline Vec3ia( PosInfTy ) : m128(_mm_set1_epi32(i: pos_inf)) {} |
51 | __forceinline Vec3ia( NegInfTy ) : m128(_mm_set1_epi32(i: neg_inf)) {} |
52 | |
53 | //////////////////////////////////////////////////////////////////////////////// |
54 | /// Array Access |
55 | //////////////////////////////////////////////////////////////////////////////// |
56 | |
57 | __forceinline const int& operator []( const size_t index ) const { assert(index < 3); return (&x)[index]; } |
58 | __forceinline int& operator []( const size_t index ) { assert(index < 3); return (&x)[index]; } |
59 | }; |
60 | |
61 | |
62 | //////////////////////////////////////////////////////////////////////////////// |
63 | /// Unary Operators |
64 | //////////////////////////////////////////////////////////////////////////////// |
65 | |
66 | __forceinline Vec3ia operator +( const Vec3ia& a ) { return a; } |
67 | __forceinline Vec3ia operator -( const Vec3ia& a ) { return _mm_sub_epi32(a: _mm_setzero_si128(), b: a.m128); } |
68 | #if defined(__SSSE3__) |
69 | __forceinline Vec3ia abs ( const Vec3ia& a ) { return _mm_abs_epi32(a.m128); } |
70 | #endif |
71 | |
72 | //////////////////////////////////////////////////////////////////////////////// |
73 | /// Binary Operators |
74 | //////////////////////////////////////////////////////////////////////////////// |
75 | |
76 | __forceinline Vec3ia operator +( const Vec3ia& a, const Vec3ia& b ) { return _mm_add_epi32(a: a.m128, b: b.m128); } |
77 | __forceinline Vec3ia operator +( const Vec3ia& a, const int b ) { return a+Vec3ia(b); } |
78 | __forceinline Vec3ia operator +( const int a, const Vec3ia& b ) { return Vec3ia(a)+b; } |
79 | |
80 | __forceinline Vec3ia operator -( const Vec3ia& a, const Vec3ia& b ) { return _mm_sub_epi32(a: a.m128, b: b.m128); } |
81 | __forceinline Vec3ia operator -( const Vec3ia& a, const int b ) { return a-Vec3ia(b); } |
82 | __forceinline Vec3ia operator -( const int a, const Vec3ia& b ) { return Vec3ia(a)-b; } |
83 | |
84 | #if defined(__SSE4_1__) |
85 | __forceinline Vec3ia operator *( const Vec3ia& a, const Vec3ia& b ) { return _mm_mullo_epi32(a.m128, b.m128); } |
86 | __forceinline Vec3ia operator *( const Vec3ia& a, const int b ) { return a * Vec3ia(b); } |
87 | __forceinline Vec3ia operator *( const int a, const Vec3ia& b ) { return Vec3ia(a) * b; } |
88 | #endif |
89 | |
90 | __forceinline Vec3ia operator &( const Vec3ia& a, const Vec3ia& b ) { return _mm_and_si128(a: a.m128, b: b.m128); } |
91 | __forceinline Vec3ia operator &( const Vec3ia& a, const int b ) { return a & Vec3ia(b); } |
92 | __forceinline Vec3ia operator &( const int a, const Vec3ia& b ) { return Vec3ia(a) & b; } |
93 | |
94 | __forceinline Vec3ia operator |( const Vec3ia& a, const Vec3ia& b ) { return _mm_or_si128(a: a.m128, b: b.m128); } |
95 | __forceinline Vec3ia operator |( const Vec3ia& a, const int b ) { return a | Vec3ia(b); } |
96 | __forceinline Vec3ia operator |( const int a, const Vec3ia& b ) { return Vec3ia(a) | b; } |
97 | |
98 | __forceinline Vec3ia operator ^( const Vec3ia& a, const Vec3ia& b ) { return _mm_xor_si128(a: a.m128, b: b.m128); } |
99 | __forceinline Vec3ia operator ^( const Vec3ia& a, const int b ) { return a ^ Vec3ia(b); } |
100 | __forceinline Vec3ia operator ^( const int a, const Vec3ia& b ) { return Vec3ia(a) ^ b; } |
101 | |
102 | __forceinline Vec3ia operator <<( const Vec3ia& a, const int n ) { return _mm_slli_epi32(a: a.m128, count: n); } |
103 | __forceinline Vec3ia operator >>( const Vec3ia& a, const int n ) { return _mm_srai_epi32(a: a.m128, count: n); } |
104 | |
105 | __forceinline Vec3ia sll ( const Vec3ia& a, const int b ) { return _mm_slli_epi32(a: a.m128, count: b); } |
106 | __forceinline Vec3ia sra ( const Vec3ia& a, const int b ) { return _mm_srai_epi32(a: a.m128, count: b); } |
107 | __forceinline Vec3ia srl ( const Vec3ia& a, const int b ) { return _mm_srli_epi32(a: a.m128, count: b); } |
108 | |
109 | //////////////////////////////////////////////////////////////////////////////// |
110 | /// Assignment Operators |
111 | //////////////////////////////////////////////////////////////////////////////// |
112 | |
113 | __forceinline Vec3ia& operator +=( Vec3ia& a, const Vec3ia& b ) { return a = a + b; } |
114 | __forceinline Vec3ia& operator +=( Vec3ia& a, const int& b ) { return a = a + b; } |
115 | |
116 | __forceinline Vec3ia& operator -=( Vec3ia& a, const Vec3ia& b ) { return a = a - b; } |
117 | __forceinline Vec3ia& operator -=( Vec3ia& a, const int& b ) { return a = a - b; } |
118 | |
119 | #if defined(__SSE4_1__) |
120 | __forceinline Vec3ia& operator *=( Vec3ia& a, const Vec3ia& b ) { return a = a * b; } |
121 | __forceinline Vec3ia& operator *=( Vec3ia& a, const int& b ) { return a = a * b; } |
122 | #endif |
123 | |
124 | __forceinline Vec3ia& operator &=( Vec3ia& a, const Vec3ia& b ) { return a = a & b; } |
125 | __forceinline Vec3ia& operator &=( Vec3ia& a, const int& b ) { return a = a & b; } |
126 | |
127 | __forceinline Vec3ia& operator |=( Vec3ia& a, const Vec3ia& b ) { return a = a | b; } |
128 | __forceinline Vec3ia& operator |=( Vec3ia& a, const int& b ) { return a = a | b; } |
129 | |
130 | __forceinline Vec3ia& operator <<=( Vec3ia& a, const int& b ) { return a = a << b; } |
131 | __forceinline Vec3ia& operator >>=( Vec3ia& a, const int& b ) { return a = a >> b; } |
132 | |
133 | //////////////////////////////////////////////////////////////////////////////// |
134 | /// Reductions |
135 | //////////////////////////////////////////////////////////////////////////////// |
136 | |
137 | __forceinline int reduce_add(const Vec3ia& v) { return v.x+v.y+v.z; } |
138 | __forceinline int reduce_mul(const Vec3ia& v) { return v.x*v.y*v.z; } |
139 | __forceinline int reduce_min(const Vec3ia& v) { return min(a: v.x,b: v.y,c: v.z); } |
140 | __forceinline int reduce_max(const Vec3ia& v) { return max(a: v.x,b: v.y,c: v.z); } |
141 | |
142 | //////////////////////////////////////////////////////////////////////////////// |
143 | /// Comparison Operators |
144 | //////////////////////////////////////////////////////////////////////////////// |
145 | |
146 | __forceinline bool operator ==( const Vec3ia& a, const Vec3ia& b ) { return (_mm_movemask_ps(a: _mm_castsi128_ps(a: _mm_cmpeq_epi32(a: a.m128, b: b.m128))) & 7) == 7; } |
147 | __forceinline bool operator !=( const Vec3ia& a, const Vec3ia& b ) { return (_mm_movemask_ps(a: _mm_castsi128_ps(a: _mm_cmpeq_epi32(a: a.m128, b: b.m128))) & 7) != 7; } |
148 | __forceinline bool operator < ( const Vec3ia& a, const Vec3ia& b ) { |
149 | if (a.x != b.x) return a.x < b.x; |
150 | if (a.y != b.y) return a.y < b.y; |
151 | if (a.z != b.z) return a.z < b.z; |
152 | return false; |
153 | } |
154 | |
155 | __forceinline Vec3ba eq_mask( const Vec3ia& a, const Vec3ia& b ) { return _mm_castsi128_ps(a: _mm_cmpeq_epi32 (a: a.m128, b: b.m128)); } |
156 | __forceinline Vec3ba lt_mask( const Vec3ia& a, const Vec3ia& b ) { return _mm_castsi128_ps(a: _mm_cmplt_epi32 (a: a.m128, b: b.m128)); } |
157 | __forceinline Vec3ba gt_mask( const Vec3ia& a, const Vec3ia& b ) { return _mm_castsi128_ps(a: _mm_cmpgt_epi32 (a: a.m128, b: b.m128)); } |
158 | |
159 | //////////////////////////////////////////////////////////////////////////////// |
160 | /// Select |
161 | //////////////////////////////////////////////////////////////////////////////// |
162 | |
163 | __forceinline Vec3ia select( const Vec3ba& m, const Vec3ia& t, const Vec3ia& f ) { |
164 | #if defined(__SSE4_1__) |
165 | return _mm_castps_si128(_mm_blendv_ps(_mm_castsi128_ps(f), _mm_castsi128_ps(t), m)); |
166 | #else |
167 | return _mm_or_si128(a: _mm_and_si128(a: _mm_castps_si128(a: m), b: t), b: _mm_andnot_si128(a: _mm_castps_si128(a: m), b: f)); |
168 | #endif |
169 | } |
170 | |
171 | #if defined(__SSE4_1__) |
172 | __forceinline Vec3ia min( const Vec3ia& a, const Vec3ia& b ) { return _mm_min_epi32(a.m128,b.m128); } |
173 | __forceinline Vec3ia max( const Vec3ia& a, const Vec3ia& b ) { return _mm_max_epi32(a.m128,b.m128); } |
174 | #else |
175 | __forceinline Vec3ia min( const Vec3ia& a, const Vec3ia& b ) { return select(m: lt_mask(a,b),t: a,f: b); } |
176 | __forceinline Vec3ia max( const Vec3ia& a, const Vec3ia& b ) { return select(m: gt_mask(a,b),t: a,f: b); } |
177 | #endif |
178 | |
179 | //////////////////////////////////////////////////////////////////////////////// |
180 | /// Output Operators |
181 | //////////////////////////////////////////////////////////////////////////////// |
182 | |
183 | __forceinline embree_ostream operator<<(embree_ostream cout, const Vec3ia& a) { |
184 | return cout << "(" << a.x << ", " << a.y << ", " << a.z << ")" ; |
185 | } |
186 | } |
187 | |