freebsd-src/lib/libc/string/swab.c
rilysh bac2eea13a swab.c(libc): use a simplified version of byte swapping
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
2024-04-22 23:01:54 -06:00

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;
}
}