-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil-simd.cpp
More file actions
86 lines (73 loc) · 2.41 KB
/
util-simd.cpp
File metadata and controls
86 lines (73 loc) · 2.41 KB
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
#include "util-math.h"
namespace util
{
// Convert memory layouts to and from SIMD-friendly (AOSOA) layout
void convertToAOSOA(
int numComponents,
int numVectors,
const void * pInput,
int inputStrideBytes,
void * pOutput,
int outputStrideBytes,
int vectorsPerChunk /*= 4*/)
{
ASSERT_ERR(numComponents > 0);
ASSERT_ERR(pInput);
ASSERT_ERR(inputStrideBytes >= sizeof(float) * numComponents);
ASSERT_ERR(pOutput);
ASSERT_ERR((size_t)pOutput % (vectorsPerChunk * sizeof(float)) == 0);
ASSERT_ERR(outputStrideBytes >= vectorsPerChunk * sizeof(float) * numComponents);
// Do the part that's a multiple of vectorsPerChunk
for (; numVectors >= vectorsPerChunk; numVectors -= vectorsPerChunk)
{
for (int i = 0; i < vectorsPerChunk; ++i)
{
for (int j = 0; j < numComponents; ++j)
((float *)pOutput)[vectorsPerChunk*j + i] = ((float *)pInput)[j];
pInput = offsetPtr(pInput, inputStrideBytes);
}
pOutput = offsetPtr(pOutput, outputStrideBytes);
}
// Do any part left over
for (int i = 0; i < numVectors; ++i)
{
for (int j = 0; j < numComponents; ++j)
((float *)pOutput)[vectorsPerChunk*j + i] = ((float *)pInput)[j];
pInput = offsetPtr(pInput, inputStrideBytes);
}
}
void convertFromAOSOA(
int numComponents,
int numVectors,
const void * pInput,
int inputStrideBytes,
void * pOutput,
int outputStrideBytes,
int vectorsPerChunk /*= 4*/)
{
ASSERT_ERR(numComponents > 0);
ASSERT_ERR(pInput);
ASSERT_ERR((size_t)pInput % (vectorsPerChunk * sizeof(float)) == 0);
ASSERT_ERR(inputStrideBytes >= vectorsPerChunk * sizeof(float) * numComponents);
ASSERT_ERR(pOutput);
ASSERT_ERR(outputStrideBytes >= sizeof(float) * numComponents);
// Do the part that's a multiple of vectorsPerChunk
for (; numVectors >= vectorsPerChunk; numVectors -= vectorsPerChunk)
{
for (int i = 0; i < vectorsPerChunk; ++i)
{
for (int j = 0; j < numComponents; ++j)
((float *)pOutput)[j] = ((float *)pInput)[vectorsPerChunk*j + i];
pOutput = offsetPtr(pOutput, outputStrideBytes);
}
pInput = offsetPtr(pInput, inputStrideBytes);
}
// Do any part left over
for (int i = 0; i < numVectors; ++i)
{
for (int j = 0; j < numComponents; ++j)
((float *)pOutput)[j] = ((float *)pInput)[vectorsPerChunk*j + i];
pOutput = offsetPtr(pOutput, outputStrideBytes);
}
}
}