mirror of
https://github.com/freebsd/freebsd-src
synced 2024-10-07 09:00:28 +00:00
bac2eea13a
This version of swab function simplifies the logic of swapping adjacent bytes. Previous version of swab() used an arbitrary unrolling, which was relevant back in the day but unnecessary for modern compilers, as if the input size is known at compile time, they can do it automatically. This version of swab() is inspired by musl. A similar version can be found at: https://github.com/openbsd/src/blob/master/lib/libc/string/swab.c Signed-off-by: rilysh <nightquick@proton.me> Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/1086
23 lines
344 B
C
23 lines
344 B
C
/*-
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
* Copyright (c) 2024 rilysh <nightquick@proton.me>
|
|
*/
|
|
|
|
#include <unistd.h>
|
|
|
|
void
|
|
swab(const void * __restrict from, void * __restrict to, ssize_t len)
|
|
{
|
|
const unsigned char *f = from;
|
|
unsigned char *t = to;
|
|
|
|
while (len > 1) {
|
|
t[0] = f[1];
|
|
t[1] = f[0];
|
|
|
|
f += 2;
|
|
t += 2;
|
|
len -= 2;
|
|
}
|
|
}
|