Skip to content

Commit ff4c7f3

Browse files
committed
Don't allow loops on a single line.
1 parent fc0f5dd commit ff4c7f3

17 files changed

+184
-62
lines changed

.clang-format

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ AllowShortFunctionsOnASingleLine: All
99
AllowShortCaseLabelsOnASingleLine: true
1010
AllowShortBlocksOnASingleLine: Always
1111
AllowShortIfStatementsOnASingleLine: Always
12-
AllowShortLoopsOnASingleLine: true
12+
AllowShortLoopsOnASingleLine: false
1313
ColumnLimit: 100
1414
PointerAlignment: Left

array.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,9 @@ NDARRAY_INLINE NDARRAY_HOST_DEVICE void advance(Ptr& ptr, Ptrs&... ptrs) {
12571257
template <class Fn, class... Ptrs>
12581258
NDARRAY_UNIQUE NDARRAY_HOST_DEVICE void for_each_value_in_order_inner_dense(
12591259
index_t extent, Fn&& fn, Ptrs... ptrs) {
1260-
for (index_t i = 0; i < extent; i++) { fn(*ptrs++...); }
1260+
for (index_t i = 0; i < extent; i++) {
1261+
fn(*ptrs++...);
1262+
}
12611263
}
12621264

12631265
// TODO: Try to use a variadic dims approach like for_each_index.
@@ -1549,7 +1551,9 @@ NDARRAY_HOST_DEVICE shape_of_rank<Shape::rank()> dynamic_optimize_shape(const Sh
15491551
for (size_t i = 0; i + 1 < rank;) {
15501552
if (can_fuse(dims[i], dims[i + 1])) {
15511553
dims[i] = fuse(dims[i], dims[i + 1]);
1552-
for (size_t j = i + 1; j + 1 < rank; j++) { dims[j] = dims[j + 1]; }
1554+
for (size_t j = i + 1; j + 1 < rank; j++) {
1555+
dims[j] = dims[j + 1];
1556+
}
15531557
rank--;
15541558
} else {
15551559
i++;
@@ -1580,7 +1584,9 @@ NDARRAY_HOST_DEVICE auto dynamic_optimize_copy_shapes(const ShapeSrc& src, const
15801584
dim<> dst;
15811585
};
15821586
std::array<copy_dims, rank> dims;
1583-
for (size_t i = 0; i < rank; i++) { dims[i] = {src_dims[i], dst_dims[i]}; }
1587+
for (size_t i = 0; i < rank; i++) {
1588+
dims[i] = {src_dims[i], dst_dims[i]};
1589+
}
15841590

15851591
// Sort the dims by the dst stride.
15861592
bubble_sort(dims.begin(), dims.end(),
@@ -1593,7 +1599,9 @@ NDARRAY_HOST_DEVICE auto dynamic_optimize_copy_shapes(const ShapeSrc& src, const
15931599
can_fuse(dims[i].dst, dims[i + 1].dst)) {
15941600
dims[i].src = fuse(dims[i].src, dims[i + 1].src);
15951601
dims[i].dst = fuse(dims[i].dst, dims[i + 1].dst);
1596-
for (size_t j = i + 1; j + 1 < new_rank; j++) { dims[j] = dims[j + 1]; }
1602+
for (size_t j = i + 1; j + 1 < new_rank; j++) {
1603+
dims[j] = dims[j + 1];
1604+
}
15971605
new_rank--;
15981606
} else {
15991607
i++;

examples/benchmark.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ double benchmark(F op) {
3030
long iterations = 1;
3131
for (int trials = 0; trials < max_trials; trials++) {
3232
auto t1 = std::chrono::high_resolution_clock::now();
33-
for (int j = 0; j < iterations; j++) { op(); }
33+
for (int j = 0; j < iterations; j++) {
34+
op();
35+
}
3436
auto t2 = std::chrono::high_resolution_clock::now();
3537
time_per_iteration_s =
3638
std::chrono::duration_cast<std::chrono::nanoseconds>(t2 - t1).count() / (iterations * 1e9);

examples/linear_algebra/conv2d.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ void conv2d_tiled(const Input& input, const Filter& filter, const Output& output
7373
}
7474
for (index_t y : output_tile.y()) {
7575
for (index_t x : output_tile.x()) {
76-
for (index_t c : output_tile.c()) { output_tile(x, y, c) = accumulator(x, y, c); }
76+
for (index_t c : output_tile.c()) {
77+
output_tile(x, y, c) = accumulator(x, y, c);
78+
}
7779
}
7880
}
7981
}

examples/linear_algebra/matrix.cpp

+30-10
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ __attribute__((noinline)) void multiply_reduce_cols(
3434
for (index_t i : c.i()) {
3535
for (index_t j : c.j()) {
3636
c(i, j) = 0;
37-
for (index_t k : a.j()) { c(i, j) += a(i, k) * b(k, j); }
37+
for (index_t k : a.j()) {
38+
c(i, j) += a(i, k) * b(k, j);
39+
}
3840
}
3941
}
4042
}
@@ -45,7 +47,9 @@ template <typename T>
4547
__attribute__((noinline)) void multiply_ein_reduce_cols(
4648
const_matrix_ref<T> a, const_matrix_ref<T> b, matrix_ref<T> c) {
4749
for (index_t i : c.i()) {
48-
for (index_t j : c.j()) { c(i, j) = make_ein_sum<T>(ein<k>(a(i, _)) * ein<k>(b(_, j))); }
50+
for (index_t j : c.j()) {
51+
c(i, j) = make_ein_sum<T>(ein<k>(a(i, _)) * ein<k>(b(_, j)));
52+
}
4953
}
5054
}
5155

@@ -57,7 +61,9 @@ __attribute__((noinline)) void multiply_ref(
5761
for (int i = 0; i < M; i++) {
5862
for (int j = 0; j < N; j++) {
5963
TC sum = 0;
60-
for (int k = 0; k < K; k++) { sum += a[i * K + k] * b[k * N + j]; }
64+
for (int k = 0; k < K; k++) {
65+
sum += a[i * K + k] * b[k * N + j];
66+
}
6167
c[i * N + j] = sum;
6268
}
6369
}
@@ -70,9 +76,13 @@ template <typename T>
7076
__attribute__((noinline)) void multiply_reduce_rows(
7177
const_matrix_ref<T> a, const_matrix_ref<T> b, matrix_ref<T> c) {
7278
for (index_t i : c.i()) {
73-
for (index_t j : c.j()) { c(i, j) = 0; }
79+
for (index_t j : c.j()) {
80+
c(i, j) = 0;
81+
}
7482
for (index_t k : a.j()) {
75-
for (index_t j : c.j()) { c(i, j) += a(i, k) * b(k, j); }
83+
for (index_t j : c.j()) {
84+
c(i, j) += a(i, k) * b(k, j);
85+
}
7686
}
7787
}
7888
}
@@ -92,11 +102,15 @@ template <typename T>
92102
__attribute__((noinline)) void multiply_reduce_matrix(
93103
const_matrix_ref<T> a, const_matrix_ref<T> b, matrix_ref<T> c) {
94104
for (index_t i : c.i()) {
95-
for (index_t j : c.j()) { c(i, j) = 0; }
105+
for (index_t j : c.j()) {
106+
c(i, j) = 0;
107+
}
96108
}
97109
for (index_t k : a.j()) {
98110
for (index_t i : c.i()) {
99-
for (index_t j : c.j()) { c(i, j) += a(i, k) * b(k, j); }
111+
for (index_t j : c.j()) {
112+
c(i, j) += a(i, k) * b(k, j);
113+
}
100114
}
101115
}
102116
}
@@ -180,7 +194,9 @@ __attribute__((noinline)) void multiply_reduce_tiles(
180194
// Perform the matrix multiplication for this tile.
181195
for (index_t k : a.j()) {
182196
for (index_t i : c_ijo.i()) {
183-
for (index_t j : c_ijo.j()) { accumulator(i, j) += a(i, k) * b(k, j); }
197+
for (index_t j : c_ijo.j()) {
198+
accumulator(i, j) += a(i, k) * b(k, j);
199+
}
184200
}
185201
}
186202

@@ -191,7 +207,9 @@ __attribute__((noinline)) void multiply_reduce_tiles(
191207
copy(accumulator, c_ijo);
192208
#else
193209
for (index_t i : c_ijo.i()) {
194-
for (index_t j : c_ijo.j()) { c_ijo(i, j) = accumulator(i, j); }
210+
for (index_t j : c_ijo.j()) {
211+
c_ijo(i, j) = accumulator(i, j);
212+
}
195213
}
196214
#endif
197215
#endif
@@ -238,7 +256,9 @@ __attribute__((noinline)) void multiply_ein_reduce_tiles(
238256
copy(accumulator, c_ijo);
239257
#else
240258
for (index_t i : c_ijo.i()) {
241-
for (index_t j : c_ijo.j()) { c_ijo(i, j) = accumulator(i, j); }
259+
for (index_t j : c_ijo.j()) {
260+
c_ijo(i, j) = accumulator(i, j);
261+
}
242262
}
243263
#endif
244264
#endif

examples/resample/resample.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,9 @@ inline kernel_array build_kernels(
143143
assert(extent > 0);
144144
assert(sum > 0.0f);
145145
dense_array<float, 1> kernel_x({{min, extent}});
146-
for (index_t rx : kernel_x.x()) { kernel_x(rx) = buffer(rx) / sum; }
146+
for (index_t rx : kernel_x.x()) {
147+
kernel_x(rx) = buffer(rx) / sum;
148+
}
147149

148150
// Make a copy without the padding, and store it in the kernel array.
149151
kernels(x) = std::move(kernel_x);
@@ -159,10 +161,14 @@ void resample_y(const TIn& in, const TOut& out, const kernel_array& kernels) {
159161
for (index_t y : out.y()) {
160162
const dense_array<float, 1>& kernel_y = kernels(y);
161163
for (index_t c : out.c()) {
162-
for (index_t x : out.x()) { out(x, y, c) = 0.0f; }
164+
for (index_t x : out.x()) {
165+
out(x, y, c) = 0.0f;
166+
}
163167
for (index_t ry : kernel_y.x()) {
164168
float kernel_y_ry = kernel_y(ry);
165-
for (index_t x : out.x()) { out(x, y, c) += in(x, ry, c) * kernel_y_ry; }
169+
for (index_t x : out.x()) {
170+
out(x, y, c) += in(x, ry, c) * kernel_y_ry;
171+
}
166172
}
167173
}
168174
}
@@ -172,7 +178,9 @@ template <class TIn, class TOut>
172178
void transpose(const TIn& in, const TOut& out) {
173179
for (index_t c : out.c()) {
174180
for (index_t y : out.y()) {
175-
for (index_t x : out.x()) { out(x, y, c) = in(y, x, c); }
181+
for (index_t x : out.x()) {
182+
out(x, y, c) = in(y, x, c);
183+
}
176184
}
177185
}
178186
}

image.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ void for_each_image_index(const Shape& s, Fn&& fn) {
5858
// communication in the c dimension).
5959
for (index_t y : s.y()) {
6060
for (index_t x : s.x()) {
61-
for (index_t c : s.c()) { fn(std::make_tuple(x, y, c)); }
61+
for (index_t c : s.c()) {
62+
fn(std::make_tuple(x, y, c));
63+
}
6264
}
6365
}
6466
}

matrix.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ using small_vector = array<T, vector_shape<Length>, auto_allocator<T, Length>>;
6161
template <class Shape, class Fn>
6262
void for_each_matrix_index(const Shape& s, Fn&& fn) {
6363
for (index_t i : s.i()) {
64-
for (index_t j : s.j()) { fn(std::make_tuple(i, j)); }
64+
for (index_t j : s.j()) {
65+
fn(std::make_tuple(i, j));
66+
}
6567
}
6668
}
6769

test/array.cpp

+42-14
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ namespace nda {
1919

2020
TEST(array_default_constructor) {
2121
dense_array<int, 1> a({10});
22-
for (int x = 0; x < 10; x++) { ASSERT_EQ(a(x), 0); }
22+
for (int x = 0; x < 10; x++) {
23+
ASSERT_EQ(a(x), 0);
24+
}
2325

2426
dense_array<int, 2> b({7, 3});
2527
ASSERT_EQ(b.width(), 7);
2628
ASSERT_EQ(b.height(), 3);
2729
for (int y = 0; y < 3; y++) {
28-
for (int x = 0; x < 7; x++) { ASSERT_EQ(b(x, y), 0); }
30+
for (int x = 0; x < 7; x++) {
31+
ASSERT_EQ(b(x, y), 0);
32+
}
2933
}
3034

3135
dense_array<int, 3> c({5, 9, 3});
@@ -34,15 +38,19 @@ TEST(array_default_constructor) {
3438
ASSERT_EQ(c.channels(), 3);
3539
for (int z = 0; z < 3; z++) {
3640
for (int y = 0; y < 9; y++) {
37-
for (int x = 0; x < 5; x++) { ASSERT_EQ(c(x, y, z), 0); }
41+
for (int x = 0; x < 5; x++) {
42+
ASSERT_EQ(c(x, y, z), 0);
43+
}
3844
}
3945
}
4046

4147
array<int, shape<dim<>, dim<>>> sparse({{-2, 5, 2}, {4, 10, 20}});
4248
ASSERT_EQ(sparse.rows(), 5);
4349
ASSERT_EQ(sparse.columns(), 10);
4450
for (int y = 4; y < 14; y++) {
45-
for (int x = -2; x < 3; x++) { ASSERT_EQ(sparse(x, y), 0); }
51+
for (int x = -2; x < 3; x++) {
52+
ASSERT_EQ(sparse(x, y), 0);
53+
}
4654
}
4755

4856
sparse.clear();
@@ -52,23 +60,31 @@ TEST(array_default_constructor) {
5260

5361
TEST(array_fill_constructor) {
5462
dense_array<int, 1> a({10}, 3);
55-
for (int x = 0; x < 10; x++) { ASSERT_EQ(a(x), 3); }
63+
for (int x = 0; x < 10; x++) {
64+
ASSERT_EQ(a(x), 3);
65+
}
5666

5767
dense_array<int, 2> b({7, 3}, 5);
5868
for (int y = 0; y < 3; y++) {
59-
for (int x = 0; x < 7; x++) { ASSERT_EQ(b(x, y), 5); }
69+
for (int x = 0; x < 7; x++) {
70+
ASSERT_EQ(b(x, y), 5);
71+
}
6072
}
6173

6274
dense_array<int, 3> c({5, 9, 3}, 7);
6375
for (int z = 0; z < 3; z++) {
6476
for (int y = 0; y < 9; y++) {
65-
for (int x = 0; x < 5; x++) { ASSERT_EQ(c(x, y, z), 7); }
77+
for (int x = 0; x < 5; x++) {
78+
ASSERT_EQ(c(x, y, z), 7);
79+
}
6680
}
6781
}
6882

6983
array<int, shape<dim<>, dim<>>> sparse({{-2, 5, 2}, {4, 10, 20}}, 13);
7084
for (int y = 4; y < 14; y++) {
71-
for (int x = -2; x < 3; x++) { ASSERT_EQ(sparse(x, y), 13); }
85+
for (int x = -2; x < 3; x++) {
86+
ASSERT_EQ(sparse(x, y), 13);
87+
}
7288
}
7389
}
7490

@@ -104,19 +120,25 @@ TEST(array_assign) {
104120
TEST(array_fill_assign) {
105121
dense_array<int, 1> a;
106122
a.assign({10}, 3);
107-
for (int x = 0; x < 10; x++) { ASSERT_EQ(a(x), 3); }
123+
for (int x = 0; x < 10; x++) {
124+
ASSERT_EQ(a(x), 3);
125+
}
108126

109127
dense_array<int, 2> b;
110128
b.assign({7, 3}, 5);
111129
for (int y = 0; y < 3; y++) {
112-
for (int x = 0; x < 7; x++) { ASSERT_EQ(b(x, y), 5); }
130+
for (int x = 0; x < 7; x++) {
131+
ASSERT_EQ(b(x, y), 5);
132+
}
113133
}
114134

115135
dense_array<int, 3> c;
116136
c.assign({5, 9, 3}, 7);
117137
for (int z = 0; z < 3; z++) {
118138
for (int y = 0; y < 9; y++) {
119-
for (int x = 0; x < 5; x++) { ASSERT_EQ(c(x, y, z), 7); }
139+
for (int x = 0; x < 5; x++) {
140+
ASSERT_EQ(c(x, y, z), 7);
141+
}
120142
}
121143
}
122144

@@ -126,7 +148,9 @@ TEST(array_fill_assign) {
126148

127149
sparse.assign(sparse_shape, 13);
128150
for (int y = 4; y < 14; y++) {
129-
for (int x = -2; x < 3; x++) { ASSERT_EQ(sparse(x, y), 13); }
151+
for (int x = -2; x < 3; x++) {
152+
ASSERT_EQ(sparse(x, y), 13);
153+
}
130154
}
131155
}
132156

@@ -175,13 +199,17 @@ TEST(sparse_array) {
175199

176200
array<int, shape<dim<>, dim<>>> sparse(sparse_shape);
177201
// Fill the storage with a constant.
178-
for (size_t i = 0; i < sparse_shape.flat_extent(); i++) { sparse.data()[i] = 7; }
202+
for (size_t i = 0; i < sparse_shape.flat_extent(); i++) {
203+
sparse.data()[i] = 7;
204+
}
179205
// Assign a different constant.
180206
sparse.assign(sparse_shape, 3);
181207

182208
// Check that we assigned all of the elements of the array.
183209
for (int y = 4; y < 14; y++) {
184-
for (int x = -2; x < 3; x++) { ASSERT_EQ(sparse(x, y), 3); }
210+
for (int x = -2; x < 3; x++) {
211+
ASSERT_EQ(sparse(x, y), 3);
212+
}
185213
}
186214

187215
// Check that only the elements of the array were assigned.

test/array_ref.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ namespace nda {
2121

2222
TEST(array_ref_indices) {
2323
int data[100];
24-
for (int i = 0; i < 100; i++) { data[i] = i; }
24+
for (int i = 0; i < 100; i++) {
25+
data[i] = i;
26+
}
2527

2628
dense_array_ref<int, 1> ref_1d(data, {100});
2729
for_all_indices(ref_1d.shape(), [&](int x) { ASSERT_EQ(ref_1d(x), x); });
@@ -60,7 +62,9 @@ TEST(reinterpret) {
6062

6163
TEST(array_ref_copy) {
6264
int data[100];
63-
for (int i = 0; i < 100; i++) { data[i] = i; }
65+
for (int i = 0; i < 100; i++) {
66+
data[i] = i;
67+
}
6468

6569
array_ref_of_rank<int, 1> evens(data, {dim<>(0, 50, 2)});
6670
dense_array<int, 1> evens_copy = make_dense_copy(evens);

0 commit comments

Comments
 (0)