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(a)) {} |
38 | __forceinline Vec3ia( const int x, const int y, const int z) : m128(_mm_set_epi32(z, z, y, x)) {} |
39 | __forceinline explicit Vec3ia( const __m128 a ) : m128(_mm_cvtps_epi32(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(1)) {} |
50 | __forceinline Vec3ia( PosInfTy ) : m128(_mm_set1_epi32(pos_inf)) {} |
51 | __forceinline Vec3ia( NegInfTy ) : m128(_mm_set1_epi32(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(_mm_setzero_si128(), a.m128); } |
68 | #if (defined(__aarch64__)) |
69 | __forceinline Vec3ia abs ( const Vec3ia& a ) { return vabsq_s32(a.m128); } |
70 | #elif defined(__SSSE3__) |
71 | __forceinline Vec3ia abs ( const Vec3ia& a ) { return _mm_abs_epi32(a.m128); } |
72 | #endif |
73 | |
74 | //////////////////////////////////////////////////////////////////////////////// |
75 | /// Binary Operators |
76 | //////////////////////////////////////////////////////////////////////////////// |
77 | |
78 | __forceinline Vec3ia operator +( const Vec3ia& a, const Vec3ia& b ) { return _mm_add_epi32(a.m128, b.m128); } |
79 | __forceinline Vec3ia operator +( const Vec3ia& a, const int b ) { return a+Vec3ia(b); } |
80 | __forceinline Vec3ia operator +( const int a, const Vec3ia& b ) { return Vec3ia(a)+b; } |
81 | |
82 | __forceinline Vec3ia operator -( const Vec3ia& a, const Vec3ia& b ) { return _mm_sub_epi32(a.m128, b.m128); } |
83 | __forceinline Vec3ia operator -( const Vec3ia& a, const int b ) { return a-Vec3ia(b); } |
84 | __forceinline Vec3ia operator -( const int a, const Vec3ia& b ) { return Vec3ia(a)-b; } |
85 | |
86 | #if defined(__aarch64__) || defined(__SSE4_1__) |
87 | __forceinline Vec3ia operator *( const Vec3ia& a, const Vec3ia& b ) { return _mm_mullo_epi32(a.m128, b.m128); } |
88 | __forceinline Vec3ia operator *( const Vec3ia& a, const int b ) { return a * Vec3ia(b); } |
89 | __forceinline Vec3ia operator *( const int a, const Vec3ia& b ) { return Vec3ia(a) * b; } |
90 | #endif |
91 | |
92 | __forceinline Vec3ia operator &( const Vec3ia& a, const Vec3ia& b ) { return _mm_and_si128(a.m128, b.m128); } |
93 | __forceinline Vec3ia operator &( const Vec3ia& a, const int b ) { return a & Vec3ia(b); } |
94 | __forceinline Vec3ia operator &( const int a, const Vec3ia& b ) { return Vec3ia(a) & b; } |
95 | |
96 | __forceinline Vec3ia operator |( const Vec3ia& a, const Vec3ia& b ) { return _mm_or_si128(a.m128, b.m128); } |
97 | __forceinline Vec3ia operator |( const Vec3ia& a, const int b ) { return a | Vec3ia(b); } |
98 | __forceinline Vec3ia operator |( const int a, const Vec3ia& b ) { return Vec3ia(a) | b; } |
99 | |
100 | __forceinline Vec3ia operator ^( const Vec3ia& a, const Vec3ia& b ) { return _mm_xor_si128(a.m128, b.m128); } |
101 | __forceinline Vec3ia operator ^( const Vec3ia& a, const int b ) { return a ^ Vec3ia(b); } |
102 | __forceinline Vec3ia operator ^( const int a, const Vec3ia& b ) { return Vec3ia(a) ^ b; } |
103 | |
104 | __forceinline Vec3ia operator <<( const Vec3ia& a, const int n ) { return _mm_slli_epi32(a.m128, n); } |
105 | __forceinline Vec3ia operator >>( const Vec3ia& a, const int n ) { return _mm_srai_epi32(a.m128, n); } |
106 | |
107 | __forceinline Vec3ia sll ( const Vec3ia& a, const int b ) { return _mm_slli_epi32(a.m128, b); } |
108 | __forceinline Vec3ia sra ( const Vec3ia& a, const int b ) { return _mm_srai_epi32(a.m128, b); } |
109 | __forceinline Vec3ia srl ( const Vec3ia& a, const int b ) { return _mm_srli_epi32(a.m128, b); } |
110 | |
111 | //////////////////////////////////////////////////////////////////////////////// |
112 | /// Assignment Operators |
113 | //////////////////////////////////////////////////////////////////////////////// |
114 | |
115 | __forceinline Vec3ia& operator +=( Vec3ia& a, const Vec3ia& b ) { return a = a + b; } |
116 | __forceinline Vec3ia& operator +=( Vec3ia& a, const int& b ) { return a = a + b; } |
117 | |
118 | __forceinline Vec3ia& operator -=( Vec3ia& a, const Vec3ia& b ) { return a = a - b; } |
119 | __forceinline Vec3ia& operator -=( Vec3ia& a, const int& b ) { return a = a - b; } |
120 | |
121 | #if defined(__aarch64__) || defined(__SSE4_1__) |
122 | __forceinline Vec3ia& operator *=( Vec3ia& a, const Vec3ia& b ) { return a = a * b; } |
123 | __forceinline Vec3ia& operator *=( Vec3ia& a, const int& b ) { return a = a * b; } |
124 | #endif |
125 | |
126 | __forceinline Vec3ia& operator &=( Vec3ia& a, const Vec3ia& b ) { return a = a & b; } |
127 | __forceinline Vec3ia& operator &=( Vec3ia& a, const int& b ) { return a = a & b; } |
128 | |
129 | __forceinline Vec3ia& operator |=( Vec3ia& a, const Vec3ia& b ) { return a = a | b; } |
130 | __forceinline Vec3ia& operator |=( Vec3ia& a, const int& b ) { return a = a | b; } |
131 | |
132 | #if !defined(__ARM_NEON) |
133 | __forceinline Vec3ia& operator <<=( Vec3ia& a, const int& b ) { return a = a << b; } |
134 | __forceinline Vec3ia& operator >>=( Vec3ia& a, const int& b ) { return a = a >> b; } |
135 | #endif |
136 | |
137 | //////////////////////////////////////////////////////////////////////////////// |
138 | /// Select |
139 | //////////////////////////////////////////////////////////////////////////////// |
140 | |
141 | __forceinline Vec3ia select( const Vec3ba& m, const Vec3ia& t, const Vec3ia& f ) { |
142 | #if defined(__aarch64__) || defined(__SSE4_1__) |
143 | return _mm_castps_si128(_mm_blendv_ps(_mm_castsi128_ps(f), _mm_castsi128_ps(t), m)); |
144 | #else |
145 | return _mm_or_si128(_mm_and_si128(_mm_castps_si128(m), t), _mm_andnot_si128(_mm_castps_si128(m), f)); |
146 | #endif |
147 | } |
148 | |
149 | //////////////////////////////////////////////////////////////////////////////// |
150 | /// Reductions |
151 | //////////////////////////////////////////////////////////////////////////////// |
152 | #if defined(__aarch64__) |
153 | __forceinline int reduce_add(const Vec3ia& v) { return vaddvq_s32(select(Vec3ba(1,1,1),v,Vec3ia(0))); } |
154 | __forceinline int reduce_mul(const Vec3ia& v) { return v.x*v.y*v.z; } |
155 | __forceinline int reduce_min(const Vec3ia& v) { return vminvq_s32(select(Vec3ba(1,1,1),v,Vec3ia(0x7FFFFFFF))); } |
156 | __forceinline int reduce_max(const Vec3ia& v) { return vmaxvq_s32(select(Vec3ba(1,1,1),v,Vec3ia(0x80000000))); } |
157 | #else |
158 | __forceinline int reduce_add(const Vec3ia& v) { return v.x+v.y+v.z; } |
159 | __forceinline int reduce_mul(const Vec3ia& v) { return v.x*v.y*v.z; } |
160 | __forceinline int reduce_min(const Vec3ia& v) { return min(v.x,v.y,v.z); } |
161 | __forceinline int reduce_max(const Vec3ia& v) { return max(v.x,v.y,v.z); } |
162 | #endif |
163 | |
164 | //////////////////////////////////////////////////////////////////////////////// |
165 | /// Comparison Operators |
166 | //////////////////////////////////////////////////////////////////////////////// |
167 | |
168 | __forceinline bool operator ==( const Vec3ia& a, const Vec3ia& b ) { return (_mm_movemask_ps(_mm_castsi128_ps(_mm_cmpeq_epi32(a.m128, b.m128))) & 7) == 7; } |
169 | __forceinline bool operator !=( const Vec3ia& a, const Vec3ia& b ) { return (_mm_movemask_ps(_mm_castsi128_ps(_mm_cmpeq_epi32(a.m128, b.m128))) & 7) != 7; } |
170 | __forceinline bool operator < ( const Vec3ia& a, const Vec3ia& b ) { |
171 | if (a.x != b.x) return a.x < b.x; |
172 | if (a.y != b.y) return a.y < b.y; |
173 | if (a.z != b.z) return a.z < b.z; |
174 | return false; |
175 | } |
176 | |
177 | __forceinline Vec3ba eq_mask( const Vec3ia& a, const Vec3ia& b ) { return _mm_castsi128_ps(_mm_cmpeq_epi32 (a.m128, b.m128)); } |
178 | __forceinline Vec3ba lt_mask( const Vec3ia& a, const Vec3ia& b ) { return _mm_castsi128_ps(_mm_cmplt_epi32 (a.m128, b.m128)); } |
179 | __forceinline Vec3ba gt_mask( const Vec3ia& a, const Vec3ia& b ) { return _mm_castsi128_ps(_mm_cmpgt_epi32 (a.m128, b.m128)); } |
180 | |
181 | #if defined(__aarch64__) || defined(__SSE4_1__) |
182 | __forceinline Vec3ia min( const Vec3ia& a, const Vec3ia& b ) { return _mm_min_epi32(a.m128,b.m128); } |
183 | __forceinline Vec3ia max( const Vec3ia& a, const Vec3ia& b ) { return _mm_max_epi32(a.m128,b.m128); } |
184 | #else |
185 | __forceinline Vec3ia min( const Vec3ia& a, const Vec3ia& b ) { return select(lt_mask(a,b),a,b); } |
186 | __forceinline Vec3ia max( const Vec3ia& a, const Vec3ia& b ) { return select(gt_mask(a,b),a,b); } |
187 | #endif |
188 | |
189 | //////////////////////////////////////////////////////////////////////////////// |
190 | /// Output Operators |
191 | //////////////////////////////////////////////////////////////////////////////// |
192 | |
193 | __forceinline embree_ostream operator<<(embree_ostream cout, const Vec3ia& a) { |
194 | return cout << "(" << a.x << ", " << a.y << ", " << a.z << ")" ; |
195 | } |
196 | } |
197 | |