-
Notifications
You must be signed in to change notification settings - Fork 56
/
circularsector.cpp
418 lines (348 loc) · 11.7 KB
/
circularsector.cpp
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include <cassert>
#define _USE_MATH_DEFINES
#include <cmath>
#include <iostream>
#include <ctime>
#include <emmintrin.h>
// Naive
bool IsPointInCircularSector(
float cx, float cy, float ux, float uy, float r, float theta,
float px, float py)
{
assert(theta > 0 && theta < M_PI);
assert(r > 0.0f);
// D = P - C
float dx = px - cx;
float dy = py - cy;
// |D| = (dx^2 + dy^2)^0.5
float length = sqrt(dx * dx + dy * dy);
// |D| > r
if (length > r)
return false;
// Normalize D
dx /= length;
dy /= length;
// acos(D dot U) < theta
return acos(dx * ux + dy * uy) < theta;
}
// Basic: use squareR and cosTheta as parameters, defer sqrt(), eliminate division
bool IsPointInCircularSector1(
float cx, float cy, float ux, float uy, float squaredR, float cosTheta,
float px, float py)
{
assert(cosTheta > -1 && cosTheta < 1);
assert(squaredR > 0.0f);
// D = P - C
float dx = px - cx;
float dy = py - cy;
// |D|^2 = (dx^2 + dy^2)
float squaredLength = dx * dx + dy * dy;
// |D|^2 > r^2
if (squaredLength > squaredR)
return false;
// |D|
float length = sqrt(squaredLength);
// D dot U > |D| cos(theta)
return dx * ux + dy * uy > length * cosTheta;
}
// Eliminate sqrt()
bool IsPointInCircularSector2(
float cx, float cy, float ux, float uy, float squaredR, float cosTheta,
float px, float py)
{
assert(cosTheta > -1 && cosTheta < 1);
assert(squaredR > 0.0f);
// D = P - C
float dx = px - cx;
float dy = py - cy;
// |D|^2 = (dx^2 + dy^2)
float squaredLength = dx * dx + dy * dy;
// |D|^2 > r^2
if (squaredLength > squaredR)
return false;
// D dot U
float DdotU = dx * ux + dy * uy;
// D dot U > |D| cos(theta)
// <=>
// (D dot U)^2 > |D|^2 (cos(theta))^2 if D dot U >= 0 and cos(theta) >= 0
// (D dot U)^2 < |D|^2 (cos(theta))^2 if D dot U < 0 and cos(theta) < 0
// true if D dot U >= 0 and cos(theta) < 0
// false if D dot U < 0 and cos(theta) >= 0
if (DdotU >= 0 && cosTheta >= 0)
return DdotU * DdotU > squaredLength * cosTheta * cosTheta;
else if (DdotU < 0 && cosTheta < 0)
return DdotU * DdotU < squaredLength * cosTheta * cosTheta;
else
return DdotU >= 0;
}
// Bit trick
bool IsPointInCircularSector3(
float cx, float cy, float ux, float uy, float squaredR, float cosTheta,
float px, float py)
{
assert(cosTheta > -1 && cosTheta < 1);
assert(squaredR > 0.0f);
// D = P - C
float dx = px - cx;
float dy = py - cy;
// |D|^2 = (dx^2 + dy^2)
float squaredLength = dx * dx + dy * dy;
// |D|^2 > r^2
if (squaredLength > squaredR)
return false;
// D dot U
float DdotU = dx * ux + dy * uy;
// D dot U > |D| cos(theta)
// <=>
// (D dot U)^2 > |D|^2 (cos(theta))^2 if D dot U >= 0 and cos(theta) >= 0
// (D dot U)^2 < |D|^2 (cos(theta))^2 if D dot U < 0 and cos(theta) < 0
// true if D dot U >= 0 and cos(theta) < 0
// false if D dot U < 0 and cos(theta) >= 0
const unsigned cSignMask = 0x80000000;
union {
float f;
unsigned u;
}a, b, lhs, rhs;
a.f = DdotU;
b.f = cosTheta;
unsigned asign = a.u & cSignMask;
unsigned bsign = b.u & cSignMask;
if (asign == bsign) {
lhs.f = DdotU * DdotU;
rhs.f = squaredLength * cosTheta * cosTheta;
lhs.u |= asign;
rhs.u |= asign;
return lhs.f > rhs.f;
}
else
return asign == 0;
}
const union VectorI32 {
unsigned u[4];
__m128 v;
}cSignMask = { 0x80000000, 0x80000000, 0x80000000, 0x80000000 };
// SSE2, SOA(struct of array) layout
__m128 IsPointInCircularSector4(
__m128 cx, __m128 cy, __m128 ux, const __m128& uy, const __m128& squaredR, const __m128& cosTheta,
const __m128& px, const __m128& py)
{
// D = P - C
__m128 dx = _mm_sub_ps(px, cx);
__m128 dy = _mm_sub_ps(py, cy);
// |D|^2 = (dx^2 + dy^2)
__m128 squaredLength = _mm_add_ps(_mm_mul_ps(dx, dx), _mm_mul_ps(dy, dy));
// |D|^2 < r^2
__m128 lengthResult = _mm_cmpgt_ps(squaredR, squaredLength);
// |D|
__m128 length = _mm_sqrt_ps(squaredLength);
// D dot U
__m128 DdotU = _mm_add_ps(_mm_mul_ps(dx, ux), _mm_mul_ps(dy, uy));
// D dot U > |D| cos(theta)
__m128 angularResult = _mm_cmpgt_ps(DdotU, _mm_mul_ps(length, cosTheta));
__m128 result = _mm_and_ps(lengthResult, angularResult);
return result;
}
// SSE2, SOA(struct of array) layout without sqrt()
__m128 IsPointInCircularSector5(
__m128 cx, __m128 cy, __m128 ux, const __m128& uy, const __m128& squaredR, const __m128& cosTheta,
const __m128& px, const __m128& py)
{
// D = P - C
__m128 dx = _mm_sub_ps(px, cx);
__m128 dy = _mm_sub_ps(py, cy);
// |D|^2 = (dx^2 + dy^2)
__m128 squaredLength = _mm_add_ps(_mm_mul_ps(dx, dx), _mm_mul_ps(dy, dy));
// |D|^2 < r^2
__m128 lengthResult = _mm_cmpgt_ps(squaredR, squaredLength);
// D dot U
__m128 DdotU = _mm_add_ps(_mm_mul_ps(dx, ux), _mm_mul_ps(dy, uy));
// D dot U > |D| cos(theta)
// <=>
// (D dot U)^2 > |D|^2 (cos(theta))^2 if D dot U >= 0 and cos(theta) >= 0
// (D dot U)^2 < |D|^2 (cos(theta))^2 if D dot U < 0 and cos(theta) < 0
// true if D dot U >= 0 and cos(theta) < 0
// false if D dot U < 0 and cos(theta) >= 0
__m128 asign = _mm_and_ps(DdotU, cSignMask.v);
__m128 bsign = _mm_and_ps(cosTheta, cSignMask.v);
__m128 equalsign = _mm_castsi128_ps(_mm_cmpeq_epi32(_mm_castps_si128(asign), _mm_castps_si128(bsign)));
__m128 lhs = _mm_or_ps(_mm_mul_ps(DdotU, DdotU), asign);
__m128 rhs = _mm_or_ps(_mm_mul_ps(squaredLength, _mm_mul_ps(cosTheta, cosTheta)), asign);
__m128 equalSignResult = _mm_cmpgt_ps(lhs, rhs);
__m128 unequalSignResult = _mm_castsi128_ps(_mm_cmpeq_epi32(_mm_castps_si128(asign), _mm_setzero_si128()));
__m128 result = _mm_and_ps(lengthResult, _mm_or_ps(
_mm_and_ps(equalsign, equalSignResult),
_mm_andnot_ps(equalsign, unequalSignResult)));
return result;
}
///////////////////////////////////////////////////////////////////////////////
// Tests
bool NoOperation(
float cx, float cy, float ux, float uy, float squaredR, float cosTheta,
float px, float py)
{
return false;
}
__m128 NoOperationSIMD(
__m128 cx, __m128 cy, __m128 ux, const __m128& uy, const __m128& squaredR, const __m128& cosTheta,
const __m128& px, const __m128& py)
{
return _mm_setzero_ps();
}
bool AreSame(
float cx, float cy, float ux, float uy, float squaredR, float cosTheta,
float px, float py)
{
bool result1 = IsPointInCircularSector1(
cx, cy, ux, uy, squaredR, cosTheta, px, py);
bool result2 = IsPointInCircularSector2(
cx, cy, ux, uy, squaredR, cosTheta, px, py);
bool result3 = IsPointInCircularSector3(
cx, cy, ux, uy, squaredR, cosTheta, px, py);
assert(result1 == result2);
assert(result1 == result3);
return result1;
}
void IsExpected(
bool expected,
float cx, float cy, float ux, float uy, float squaredR, float cosTheta,
float px, float py)
{
assert(expected == AreSame(cx, cy, ux, uy, squaredR, cosTheta, px, py));
}
// Test data
float Uniform(float minimum, float maximum) {
return rand() * (maximum - minimum) / RAND_MAX + minimum;
}
const unsigned N = 1000;
#if _DEBUG
const unsigned M = 1000;
#else
const unsigned M = 100000;
#endif
__declspec(align(128)) float gCx[N];
__declspec(align(128)) float gCy[N];
__declspec(align(128)) float gUx[N];
__declspec(align(128)) float gUy[N];
__declspec(align(128)) float gSquaredR[N];
__declspec(align(128)) float gCosTheta[N];
__declspec(align(128)) float gPx[M];
__declspec(align(128)) float gPy[M];
__declspec(align(128)) float gR[N]; // naive
__declspec(align(128)) float gTheta[N]; // naive
// Run test
template <bool naiveParam, typename TestFunc>
float Test(TestFunc f, float rmax = 2.0f) {
unsigned count = 0;
for (int i = 0; i < N; i++) {
float cx = gCx[i];
float cy = gCy[i];
float ux = gUx[i];
float uy = gUy[i];
float r = naiveParam ? gR[i] : gSquaredR[i];
float t = naiveParam ? gTheta[i] : gCosTheta[i];
for (int j = 0; j < M; j++) {
if (f(cx, cy, ux, uy, r, t, gPx[j], gPy[j]))
count++;
}
}
return (float)count / (N * M);
}
template <typename TestFunc>
float TestSIMD(TestFunc f, float rmax = 2.0f) {
static const unsigned cCountTable[] = {
0, // 0000
1, // 0001
1, // 0010
2, // 0011
1, // 0100
2, // 0101
2, // 0110
3, // 0111
1, // 1000
2, // 1001
2, // 1010
3, // 1011
2, // 1100
3, // 1101
3, // 1110
4, // 1111
};
unsigned count = 0;
for (int i = 0; i < N; i++) {
__m128 cx = _mm_set1_ps(gCx[i]);
__m128 cy = _mm_set1_ps(gCy[i]);
__m128 ux = _mm_set1_ps(gUx[i]);
__m128 uy = _mm_set1_ps(gUy[i]);
__m128 squaredR = _mm_set1_ps(gSquaredR[i]);
__m128 cosTheta = _mm_set1_ps(gCosTheta[i]);
for (int j = 0; j < M; j += 4) {
__m128 px = _mm_load_ps(&gPx[j]);
__m128 py = _mm_load_ps(&gPy[j]);
int mask = _mm_movemask_ps(f(cx, cy, ux, uy, squaredR, cosTheta, px, py));
count += cCountTable[mask];
}
}
return (float)count / (N * M);
}
template <bool naiveParam, typename TestFunc>
float Performance(const char* name, TestFunc f, float overhead = 0.0f) {
clock_t start = clock();
float hit = Test<naiveParam>(f);
clock_t end = clock();
float time = (float)(end - start) / CLOCKS_PER_SEC - overhead;
printf("%s hit=%g%% time=%gs\n", name, hit * 100.0f, time);
return time;
}
template <typename TestFunc>
float PerformanceSIMD(const char* name, TestFunc f, float overhead = 0.0f) {
clock_t start = clock();
float hit = TestSIMD(f);
clock_t end = clock();
float time = (float)(end - start) / CLOCKS_PER_SEC - overhead;
printf("%s hit=%g%% time=%gs\n", name, hit * 100.0f, time);
return time;
}
int main(int argc, char* argv[]) {
srand(0);
for (int i = 0; i < N; i++) {
gCx[i] = Uniform(-1.0f, 1.0f);
gCy[i] = Uniform(-1.0f, 1.0f);
float ux = Uniform(-1.0f, 1.0f);
float uy = Uniform(-1.0f, 1.0f);
float ru = 1.0f / (sqrt(ux * ux + uy * uy));
gUx[i] = ux * ru;
gUy[i] = uy * ru;
gR[i] = Uniform(0.0f, 2.0f);
gSquaredR[i] = gR[i] * gR[i];
gTheta[i] = Uniform(0.0f, float(M_PI));
gCosTheta[i] = cos(gTheta[i]);
}
for (int j = 0; j < N; j++) {
gPx[j] = Uniform(-1.0f, 1.0f);
gPy[j] = Uniform(-1.0f, 1.0f);
}
#ifdef _DEBUG
IsExpected(true, 0.0f, 0.0f, 1.0f, 0.0f, 4.0f, 0.5f, 1.0f, 0.0f);
IsExpected(false, 0.0f, 0.0f, 1.0f, 0.0f, 4.0f, 0.5f, 4.0f, 1.0f);
IsExpected(true, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.5f, 0.0f);
IsExpected(false, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, -0.5f, 0.0f);
Test<false>(AreSame);
#else
#define PERF(f, naiveParam, overhead) Performance<naiveParam>(#f, f, overhead)
#define PERFSIMD(f, overhead) PerformanceSIMD(#f, f, overhead)
float overhead = PERF(NoOperation, true, 0.0f);
PERF(IsPointInCircularSector, true, overhead);
printf("\n");
overhead = PERF(NoOperation, false, 0.0f);
PERF(IsPointInCircularSector1, false, overhead);
PERF(IsPointInCircularSector2, false, overhead);
PERF(IsPointInCircularSector3, false, overhead);
printf("\n");
overhead = PERFSIMD(NoOperationSIMD, 0.0f);
PERFSIMD(IsPointInCircularSector4, overhead);
PERFSIMD(IsPointInCircularSector5, overhead);
#undef PERF
#undef PERFSIMD
#endif
return 0;
}