From f19b4d2f61aeafa443e45cee1229c9463e62fc8d Mon Sep 17 00:00:00 2001 From: Eric Andersen Date: Sat, 6 Apr 2002 00:07:58 +0000 Subject: Faster implementation from Miles Bader --- libc/unistd/swab.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'libc/unistd/swab.c') diff --git a/libc/unistd/swab.c b/libc/unistd/swab.c index c63ca7284..41845d4cb 100644 --- a/libc/unistd/swab.c +++ b/libc/unistd/swab.c @@ -1,18 +1,18 @@ #include #include +#include -/* swab() swaps the position of two adjacent bytes, every two bytes. - * Contributed by Kensuke Otake */ +/* Updated implementation based on byteswap.h from Miles Bader + * . This should be much faster on arches with machine + * specific, optimized definitions in include/bits/byteswap.h (i.e. on + * x86, use the bswap instruction on i486 and better boxes). For + * platforms that lack such support, this should be no slower than it + * was before... */ +void swab (const void *source, void *dest, ssize_t count) +{ + const unsigned short *from = source, *from_end = from + (count >> 1); + unsigned short *to = dest; -void swab(const void *source, void *dest, ssize_t count) { - const char *from = (const char *)source; - char *to = (char *)dest; - - count &= ~((ssize_t)1); - - while (count > 1) { - const char b0 = from[--count], b1 = from[--count]; - to[count] = b0; - to[count + 1] = b1; - } + while (from < from_end) + *to++ = bswap_16 (*from++); } -- cgit v1.2.3