-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathcumulativesum_x86_packed.h
More file actions
197 lines (177 loc) · 5.6 KB
/
Copy pathcumulativesum_x86_packed.h
File metadata and controls
197 lines (177 loc) · 5.6 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
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
// Copyright 2026 Tencent
// SPDX-License-Identifier: BSD-3-Clause
#if NCNN_RUNTIME_CPU && NCNN_AVX2 && __AVX__ && !__AVX2__
int cumulative_sum_forward_inplace_avx2(Mat& bottom_top_blob, int axis, const Option& opt);
#endif
#if __SSE2__
static inline __m128 cumulative_sum_prefix_sum4_ps(__m128 v)
{
__m128 t = _mm_castsi128_ps(_mm_slli_si128(_mm_castps_si128(v), 4));
v = _mm_add_ps(v, t);
t = _mm_castsi128_ps(_mm_slli_si128(_mm_castps_si128(v), 8));
v = _mm_add_ps(v, t);
return v;
}
#endif
static void cumulative_sum_prefix_sum_row(float* ptr, int w)
{
int j = 0;
float sum = 0.f;
#if __AVX2__
__m256 base = _mm256_setzero_ps();
for (; j + 8 <= w; j += 8)
{
__m256 v = _mm256_loadu_ps(ptr + j);
__m256 t = _mm256_castsi256_ps(_mm256_slli_si256(_mm256_castps_si256(v), 4));
v = _mm256_add_ps(v, t);
t = _mm256_castsi256_ps(_mm256_slli_si256(_mm256_castps_si256(v), 8));
v = _mm256_add_ps(v, t);
__m256 lo = _mm256_permute2f128_ps(v, v, 0x08);
lo = _mm256_shuffle_ps(lo, lo, _MM_SHUFFLE(3, 3, 3, 3));
v = _mm256_add_ps(v, lo);
v = _mm256_add_ps(v, base);
_mm256_storeu_ps(ptr + j, v);
__m256 last = _mm256_permute2f128_ps(v, v, 0x11);
base = _mm256_shuffle_ps(last, last, _MM_SHUFFLE(3, 3, 3, 3));
}
if (j > 0)
sum = ptr[j - 1];
#elif __AVX__
for (; j + 8 <= w; j += 8)
{
__m256 v = _mm256_loadu_ps(ptr + j);
__m128 v0 = cumulative_sum_prefix_sum4_ps(_mm256_castps256_ps128(v));
__m128 v1 = cumulative_sum_prefix_sum4_ps(_mm256_extractf128_ps(v, 1));
v1 = _mm_add_ps(v1, _mm_shuffle_ps(v0, v0, _MM_SHUFFLE(3, 3, 3, 3)));
__m256 out = _mm256_castps128_ps256(v0);
out = _mm256_insertf128_ps(out, v1, 1);
out = _mm256_add_ps(out, _mm256_set1_ps(sum));
_mm256_storeu_ps(ptr + j, out);
sum = ptr[j + 7];
}
#elif __SSE2__
for (; j + 4 <= w; j += 4)
{
__m128 v = cumulative_sum_prefix_sum4_ps(_mm_loadu_ps(ptr + j));
v = _mm_add_ps(v, _mm_set1_ps(sum));
_mm_storeu_ps(ptr + j, v);
sum = ptr[j + 3];
}
#endif
for (; j < w; j++)
{
sum += ptr[j];
ptr[j] = sum;
}
}
static void cumulative_sum_add(const float* ptr, float* outptr, int size)
{
int i = 0;
#if __AVX__
for (; i + 7 < size; i += 8)
{
__m256 _p = _mm256_loadu_ps(ptr + i);
__m256 _outp = _mm256_loadu_ps(outptr + i);
_outp = _mm256_add_ps(_outp, _p);
_mm256_storeu_ps(outptr + i, _outp);
}
#elif __SSE2__
for (; i + 3 < size; i += 4)
{
__m128 _p = _mm_loadu_ps(ptr + i);
__m128 _outp = _mm_loadu_ps(outptr + i);
_outp = _mm_add_ps(_outp, _p);
_mm_storeu_ps(outptr + i, _outp);
}
#endif
for (; i < size; i++)
{
outptr[i] += ptr[i];
}
}
static int cumulative_sum_forward_inplace(Mat& bottom_top_blob, int axis, const Option& opt)
{
#if NCNN_RUNTIME_CPU && NCNN_AVX2 && __AVX__ && !__AVX2__
if (ncnn::cpu_support_x86_avx2())
{
return cumulative_sum_forward_inplace_avx2(bottom_top_blob, axis, opt);
}
#endif
const int dims = bottom_top_blob.dims;
const int positive_axis = axis < 0 ? dims + axis : axis;
if (dims == 1)
{
cumulative_sum_prefix_sum_row(bottom_top_blob, bottom_top_blob.w);
return 0;
}
if (dims == 2 && positive_axis == 0)
{
const int w = bottom_top_blob.w;
const int h = bottom_top_blob.h;
for (int i = 1; i < h; i++)
{
const float* prev_row = bottom_top_blob.row(i - 1);
float* this_row = bottom_top_blob.row(i);
cumulative_sum_add(prev_row, this_row, w);
}
return 0;
}
if (dims == 2 && positive_axis == 1)
{
const int w = bottom_top_blob.w;
const int h = bottom_top_blob.h;
#pragma omp parallel for num_threads(opt.num_threads)
for (int i = 0; i < h; i++)
{
cumulative_sum_prefix_sum_row(bottom_top_blob.row(i), w);
}
return 0;
}
if (dims == 3 && positive_axis == 0)
{
const int w = bottom_top_blob.w;
const int h = bottom_top_blob.h;
const int c = bottom_top_blob.c;
const int size = w * h;
for (int q = 1; q < c; q++)
{
const float* prev = bottom_top_blob.channel(q - 1);
float* cur = bottom_top_blob.channel(q);
cumulative_sum_add(prev, cur, size);
}
return 0;
}
if (dims == 3 && positive_axis == 1)
{
const int w = bottom_top_blob.w;
const int h = bottom_top_blob.h;
const int c = bottom_top_blob.c;
#pragma omp parallel for num_threads(opt.num_threads)
for (int q = 0; q < c; q++)
{
Mat this_channel = bottom_top_blob.channel(q);
for (int i = 1; i < h; i++)
{
const float* prev_row = this_channel.row(i - 1);
float* this_row = this_channel.row(i);
cumulative_sum_add(prev_row, this_row, w);
}
}
return 0;
}
if (dims == 3 && positive_axis == 2)
{
const int w = bottom_top_blob.w;
const int h = bottom_top_blob.h;
const int c = bottom_top_blob.c;
#pragma omp parallel for num_threads(opt.num_threads)
for (int idx = 0; idx < c * h; idx++)
{
const int q = idx / h;
const int i = idx - q * h;
cumulative_sum_prefix_sum_row(bottom_top_blob.channel(q).row(i), w);
}
return 0;
}
return -100;
}