Skip to content

Commit a2d6a0d

Browse files
committed
Updated memswap implementation
1 parent 98d3f9f commit a2d6a0d

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

src/main/base_backend.cpp

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* along with lsp-r3d-base-lib. If not, see <https://www.gnu.org/licenses/>.
2020
*/
2121

22+
#include <lsp-plug.in/common/types.h>
2223
#include <lsp-plug.in/r3d/base/backend.h>
2324
#include <lsp-plug.in/stdlib/string.h>
2425

@@ -161,6 +162,52 @@ namespace lsp
161162

162163
void base_backend_t::memswap(void *a, void *b, size_t bytes)
163164
{
165+
#ifdef ARCH_64BIT
166+
// 32-byte blocks
167+
uint64_t *xa = reinterpret_cast<uint64_t *>(a);
168+
uint64_t *xb = reinterpret_cast<uint64_t *>(b);
169+
170+
for (; bytes >= 32 ; bytes -= 32, xa += 4, xb += 4)
171+
{
172+
uint64_t t0 = xa[0];
173+
uint64_t t1 = xa[1];
174+
uint64_t t2 = xa[2];
175+
uint64_t t3 = xa[3];
176+
177+
xa[0] = xb[0];
178+
xa[1] = xb[1];
179+
xa[2] = xb[2];
180+
xa[3] = xb[3];
181+
182+
xb[0] = t0;
183+
xb[1] = t1;
184+
xb[2] = t2;
185+
xb[3] = t3;
186+
}
187+
188+
// 4-byte blocks
189+
uint32_t *ya = reinterpret_cast<uint32_t *>(xa);
190+
uint32_t *yb = reinterpret_cast<uint32_t *>(xb);
191+
192+
for (; bytes >= 4 ; bytes -= 4, ++ya, ++yb)
193+
{
194+
uint32_t t0 = ya[0];
195+
ya[0] = yb[0];
196+
yb[0] = t0;
197+
}
198+
199+
// 1-byte blocks
200+
uint8_t *za = reinterpret_cast<uint8_t *>(ya);
201+
uint8_t *zb = reinterpret_cast<uint8_t *>(yb);
202+
203+
for (size_t i=0; i<bytes; ++i)
204+
{
205+
uint8_t t0 = za[i];
206+
za[i] = zb[i];
207+
zb[i] = t0;
208+
}
209+
#else
210+
// 16-byte blocks
164211
uint32_t *xa = reinterpret_cast<uint32_t *>(a);
165212
uint32_t *xb = reinterpret_cast<uint32_t *>(b);
166213
for (; bytes >= 16 ; bytes -= 16, xa += 4, xb += 4)
@@ -181,13 +228,15 @@ namespace lsp
181228
xb[3] = t3;
182229
}
183230

184-
for (; bytes >= 4 ; bytes -= 4, xa += 1, xb += 1)
231+
// 4-byte blocks
232+
for (; bytes >= 4 ; bytes -= 4, ++xa, ++xb)
185233
{
186234
uint32_t t0 = xa[0];
187235
xa[0] = xb[0];
188236
xb[0] = t0;
189237
}
190238

239+
// 1-byte blocks
191240
uint8_t *ya = reinterpret_cast<uint8_t *>(xa);
192241
uint8_t *yb = reinterpret_cast<uint8_t *>(xb);
193242

@@ -197,6 +246,7 @@ namespace lsp
197246
ya[i] = yb[i];
198247
yb[i] = t0;
199248
}
249+
#endif /* ARCH_64BIT */
200250
}
201251

202252
void base_backend_t::swap_rows(void *buf, size_t rows, size_t bytes_per_row)

0 commit comments

Comments
 (0)