Skip to content

Commit ecc040a

Browse files
committed
Simplify Ptr::mov()
* ra/expr.hh (Ptr::mov): Reuse std::ranges::advance.
1 parent ca1bde9 commit ecc040a

File tree

10 files changed

+39
-46
lines changed

10 files changed

+39
-46
lines changed

bench/bench-at.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,23 @@ int main()
4242
};
4343

4444
report("direct subscript",
45-
bm.run([&] {
45+
bm.run([&]{
4646
int val = 0;
4747
for (int i=0; i<O; ++i) {
4848
val += C(dim_t(I(i, 0)), dim_t(I(i, 1))); // conversions needed when I has runtime rank
4949
}
5050
val0 = val;
5151
}));
5252
report("at member + loop",
53-
bm.run([&] {
53+
bm.run([&]{
5454
int val = 0;
5555
for (int i=0; i<O; ++i) {
5656
val += C.at(I(i));
5757
}
5858
val0 = val;
5959
}));
6060
report("at op + iter",
61-
bm.run([&] {
61+
bm.run([&]{
6262
val0 = sum(at(C, iter<1>(I)));
6363
}));
6464
};

bench/bench-from.cc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,21 @@ int main()
3939
.test_eq(ra::iota(Isize)*Istep, B);
4040
};
4141
report("indexing on raw pointers",
42-
bm.run([&] {
42+
bm.run([&]{
4343
for (int i=0; i<Isize; ++i) {
4444
BB[i] = AA[II[i]];
4545
}
4646
}));
4747
report("vectorized selection",
48-
bm.run([&] {
48+
bm.run([&]{
4949
B = A(I);
5050
}));
5151
report("write out the indexing loop",
52-
bm.run([&] {
52+
bm.run([&]{
5353
for_each([&A](auto & b, auto i) { b = A(i); }, B, I);
5454
}));
5555
report("loop on scalar selection",
56-
bm.run([&]() {
56+
bm.run([&]{
5757
for (int i=0; i<Isize; ++i) {
5858
B(i) = A(I(i));
5959
}
@@ -97,15 +97,15 @@ int main()
9797
};
9898

9999
report("2D indexing on raw pointers",
100-
bm.run([&] {
100+
bm.run([&]{
101101
for (int i=0; i<Isize; ++i) {
102102
for (int j=0; j<Isize; ++j) {
103103
BB[i*Isize + j] = AA[II[i]*Asize + II[j]];
104104
}
105105
}
106106
}));
107107
report("vectorized selection",
108-
bm.run([&] {
108+
bm.run([&]{
109109
B = A(I, I);
110110
}));
111111
};
@@ -144,7 +144,7 @@ int main()
144144
};
145145

146146
report("3D indexing on raw pointers",
147-
bm.run([&] {
147+
bm.run([&]{
148148
for (int i=0; i<Isize; ++i) {
149149
for (int j=0; j<Isize; ++j) {
150150
for (int k=0; k<Isize; ++k) {
@@ -154,7 +154,7 @@ int main()
154154
}
155155
}));
156156
report("vectorized selection",
157-
bm.run([&] {
157+
bm.run([&]{
158158
B = A(I, I, I);
159159
}));
160160
};
@@ -184,7 +184,7 @@ int main()
184184
};
185185

186186
report("3D indexing on raw pointers",
187-
bm.run([&] {
187+
bm.run([&]{
188188
for (int i=0; i<Isize; ++i) {
189189
for (int j=0; j<Isize; ++j) {
190190
for (int k=0; k<Isize; ++k) {
@@ -196,11 +196,11 @@ int main()
196196
}
197197
}));
198198
report("vectorized selection",
199-
bm.run([&] {
199+
bm.run([&]{
200200
B = A(I, I, I, I);
201201
}));
202202
report("slice one axis at a time", // TODO one way A(i, i, i, i) could work
203-
bm.run([&] {
203+
bm.run([&]{
204204
for (int i=0; i<Isize; ++i) {
205205
for (int j=0; j<Isize; ++j) {
206206
for (int k=0; k<Isize; ++k) {

bench/bench-iterator.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ int main()
3434
.test_eq(ra::iota(s), A);
3535
};
3636
report("range for",
37-
bm.run([&] {
37+
bm.run([&]{
3838
int i = 0;
3939
for (auto & a: A) {
4040
a = i;
4141
++i;
4242
}
4343
}));
4444
report("ply undef",
45-
bm.run([&] {
45+
bm.run([&]{
4646
A = ra::_0;
4747
}));
4848
report("ply def",
49-
bm.run([&] {
49+
bm.run([&]{
5050
A = ra::iota(s);
5151
}));
5252
};
@@ -75,7 +75,7 @@ int main()
7575
.test_eq(ra::iota(s), B);
7676
};
7777
report("range for",
78-
bm.run([&] {
78+
bm.run([&]{
7979
int i = 0;
8080
for (auto & a: A) {
8181
a = i;

bench/bench-reduce-sqrm.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ int main()
4040
{
4141
real4 A(7.), B(3.);
4242
y = 0.;
43-
repeat([&] {
43+
repeat([&]{
4444
for (int j=0; j!=4; ++j) {
4545
y += sqrm(A(j)-B(j));
4646
}
@@ -52,7 +52,7 @@ int main()
5252
{
5353
real4 A(7.), B(3.);
5454
y = 0.;
55-
repeat([&] {
55+
repeat([&]{
5656
y += reduce_sqrm(A-B);
5757
});
5858
}));
@@ -63,7 +63,7 @@ int main()
6363
ra::Unique<real, 1> A(S1, 7.);
6464
ra::Unique<real, 1> B(S1, 3.);
6565
y = 0.;
66-
repeat([&] {
66+
repeat([&]{
6767
real const * a = A.data();
6868
real const * b = B.data();
6969
for (int j=0; j<S1[0]; ++j) {
@@ -75,15 +75,15 @@ int main()
7575
auto traversal = [&](auto && repeat, auto const & a, auto const & b)
7676
{
7777
y = 0.;
78-
repeat([&] {
78+
repeat([&]{
7979
for_each([&](real const a, real const b) { y += sqrm(a, b); }, a, b);
8080
});
8181
};
8282
// separate reduction: compare abstraction penalty with by_traversal.
8383
auto traversal2 = [&](auto && repeat, auto const & a, auto const & b)
8484
{
8585
y = 0.;
86-
repeat([&] {
86+
repeat([&]{
8787
for_each([&](real const a) { y += a; },
8888
map([](real const a, real const b) { return sqrm(a, b); },
8989
a, b));
@@ -98,7 +98,7 @@ int main()
9898
.once_f([&](auto && repeat)
9999
{
100100
y = 0.;
101-
repeat([&] {
101+
repeat([&]{
102102
for (int j=0; j<S1[0]; ++j) {
103103
y += sqrm(A(j)-B(j));
104104
}
@@ -114,7 +114,7 @@ int main()
114114
.once_f([&](auto && repeat)
115115
{
116116
y = 0.;
117-
repeat([&] {
117+
repeat([&]{
118118
for (int j=0; j<S2[0]; ++j) {
119119
for (int k=0; k<S2[1]; ++k) {
120120
y += sqrm(A(j, k)-B(j, k));
@@ -132,7 +132,7 @@ int main()
132132
.once_f([&](auto && repeat)
133133
{
134134
y = 0.;
135-
repeat([&] {
135+
repeat([&]{
136136
for (int j=0; j<S3[0]; ++j) {
137137
for (int k=0; k<S3[1]; ++k) {
138138
for (int l=0; l<S3[2]; ++l) {

ra/big.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ transpose(ViewBig<T, RANK> const & view, ilist_t<I ...>)
644644
if constexpr (ANY==RANK) {
645645
RA_CHECK(view.rank()==sizeof...(I), "Bad rank ", view.rank(), " for ", sizeof...(I), " axes.");
646646
} else {
647-
static_assert(RANK==sizeof...(I), "Bad rank."); // c++26
647+
static_assert(ANY==RANK || RANK==sizeof...(I), "Bad rank."); // c++26
648648
}
649649
constexpr std::array<dim_t, sizeof...(I)> s = { I ... };
650650
constexpr rank_t dstrank = 0==ra::size(s) ? 0 : 1 + std::ranges::max(s);

ra/expr.hh

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -171,20 +171,13 @@ struct Ptr final
171171
constexpr decltype(auto) operator*() const { return *i; }
172172
constexpr auto save() const { return i; }
173173
constexpr void load(I ii) { i = ii; }
174-
constexpr void mov(dim_t d)
175-
{
176-
if constexpr (std::random_access_iterator<I>) {
177-
i += d*s;
178-
} else {
179-
if (dim_t j=d*s; j>0) while (j>0) { ++i; --j; } else while (j<0) { --i; ++j; }
180-
}
181-
}
174+
constexpr void mov(dim_t d) { std::ranges::advance(i, d*s); }
182175
};
183176

184177
template <class X> using sarg = std::conditional_t<is_constant<std::decay_t<X>> || is_scalar<X>, std::decay_t<X>, X>;
185178

186-
template <class S>
187-
consteval auto maybe_step()
179+
template <class S> consteval auto
180+
maybe_step()
188181
{
189182
if constexpr (std::is_integral_v<S>) {
190183
return S(1);
@@ -287,7 +280,7 @@ reverse(Iota<w, I, N, S> const & i, K k = {})
287280
static_assert(i.nn!=UNB && k>=0 && k<=w, "Bad arguments to reverse(iota).");
288281
if constexpr (k==w) {
289282
return ra::iota<w>(i.n, i.i+(i.n-1)*i.s,
290-
[&i]() { if constexpr (is_constant<S>) return dim_c<-S {}> {}; else return -i.s; }());
283+
[&i]{ if constexpr (is_constant<S>) return dim_c<-S {}> {}; else return -i.s; }());
291284
} else {
292285
return i;
293286
}
@@ -393,7 +386,7 @@ template <Iterator ... P, int ... I>
393386
struct Match<std::tuple<P ...>, ilist_t<I ...>>
394387
{
395388
std::tuple<P ...> t;
396-
constexpr static rank_t rs = [] { rank_t r=UNB; return ((r=choose_rank(rank_s<P>(), r)), ...); }();
389+
constexpr static rank_t rs = []{ rank_t r=UNB; return ((r=choose_rank(rank_s<P>(), r)), ...); }();
397390

398391
constexpr Match(P ... p_): t(p_ ...) {} // [ra1]
399392

ra/test.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ struct TestRecorder
8686
TestRecorder & expectfail(bool s=true) { willexpectfail = s; return *this; }
8787

8888
#define RA_CURRENT_LOC std::source_location const loc = std::source_location::current()
89-
#define RA_LAZYINFO(...) [&] { return format(infos, (infos=="" ? "" : "; "), __VA_ARGS__); }
89+
#define RA_LAZYINFO(...) [&]{ return format(infos, (infos=="" ? "" : "; "), __VA_ARGS__); }
9090

9191
void
9292
test(bool c, auto && info_full, auto && info_min, RA_CURRENT_LOC)
@@ -151,7 +151,7 @@ struct TestRecorder
151151
test_comp(auto && a, auto && b, auto && comp, char const * msg, RA_CURRENT_LOC)
152152
{
153153
if (willstrictshape
154-
? [&] {
154+
? [&]{
155155
if constexpr (ra::rank_s(a)==ra::rank_s(b) || ra::rank_s(a)==ANY || ra::rank_s(b)==ANY) {
156156
return ra::rank(a)==ra::rank(b) && every(ra::start(ra::shape(a))==ra::shape(b));
157157
} else {

test/ra-9.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ int main()
109109
}
110110
tr.section("[ra35] - value");
111111
{
112-
auto fun1 = [] { return std::array {7, 2}; };
113-
auto fun2 = [] { return std::vector {5, 2}; };
112+
auto fun1 = []{ return std::array {7, 2}; };
113+
auto fun2 = []{ return std::vector {5, 2}; };
114114

115115
tr.test_eq(ra::start({7, 2}), ra::ptr(fun1()));
116116
tr.test_eq(ra::start({5, 2}), ra::ptr(fun2()));

test/vector-array.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ vec(V && v)
4646

4747
int main()
4848
{
49-
auto f1 = [] { return std::array {7, 2}; };
50-
auto f2 = [] { return std::vector {5, 2}; };
49+
auto f1 = []{ return std::array {7, 2}; };
50+
auto f2 = []{ return std::vector {5, 2}; };
5151
auto v1 = vec(f1());
5252
auto v2 = vec(f2());
5353

test/wrank.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ int main()
319319
}
320320
tr.section("no arguments -> zero rank");
321321
{
322-
int x = ra::from([] { return 3; });
322+
int x = ra::from([]{ return 3; });
323323
tr.test_eq(3, x);
324324
}
325325
tr.section("counting ops");

0 commit comments

Comments
 (0)