Skip to content

Commit 44cb8fc

Browse files
remove modulo in 1D to ND index transformation
remove expensive modulo operation
1 parent 2604643 commit 44cb8fc

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

include/pmacc/dimensions/DataSpaceOperations.hpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ namespace pmacc
149149
template<class TVEC>
150150
static HDINLINE DataSpace<DIM2> map(uint32_t pos)
151151
{
152-
return DataSpace< DIM2 >(
153-
pos % TVEC::x::value,
154-
pos / TVEC::x::value
155-
);
152+
auto const y = pos / TVEC::x::value;
153+
auto const x = pos - y * TVEC::x::value;
154+
155+
return DataSpace< DIM2 >( x , y );
156156
}
157157

158158
template<class TVEC>
@@ -165,10 +165,10 @@ namespace pmacc
165165

166166
static HDINLINE DataSpace<DIM2> map(const DataSpace<DIM2>& size, uint32_t pos)
167167
{
168-
return DataSpace< DIM2 >(
169-
pos % size.x(),
170-
pos / size.x()
171-
);
168+
auto const y = pos / size.x();
169+
auto const x = pos - y * size.x();
170+
171+
return DataSpace< DIM2 >( x , y );
172172
}
173173

174174
static HDINLINE uint32_t map(const DataSpace<DIM2>& size, const DataSpace<DIM2>& pos)
@@ -266,27 +266,31 @@ namespace pmacc
266266
template<class TVEC>
267267
static HDINLINE DataSpace<DIM3> map(uint32_t pos)
268268
{
269-
return DataSpace< DIM3 >(
270-
pos % TVEC::x::value,
271-
pos / TVEC::x::value % TVEC::y::value,
272-
pos / ( TVEC::x::value * TVEC::y::value )
273-
);
269+
constexpr auto xyPlane = TVEC::x::value * TVEC::y::value;
270+
auto const z = pos / xyPlane;
271+
pos -= z * xyPlane;
272+
auto const y = pos / TVEC::x::value;
273+
auto const x = pos - y * TVEC::x::value;
274+
275+
return DataSpace< DIM3 >( x , y, z );
274276
}
275277

276278
static HDINLINE DataSpace<DIM3> map(const DataSpace<DIM3>& size, uint32_t pos)
277279
{
278-
return DataSpace< DIM3 >(
279-
pos % size.x(),
280-
pos / size.x() % size.y(),
281-
pos / ( size.x() * size.y() )
282-
);
280+
auto const xyPlane = size.x() * size.y();
281+
auto const z = pos / xyPlane;
282+
pos -= z * xyPlane;
283+
auto const y = pos / size.x();
284+
auto const x = pos - y * size.x();
285+
286+
return DataSpace< DIM3 >( x , y, z );
283287
}
284288

285289
template<class TVEC>
286290
static HDINLINE uint32_t map(const DataSpace<DIM3>& pos)
287291
{
288292
return
289-
pos.z() * TVEC::x::value * TVEC::y::value +
293+
pos.z() * ( TVEC::x::value * TVEC::y::value ) +
290294
pos.y() * TVEC::x::value +
291295
pos.x();
292296
}

0 commit comments

Comments
 (0)