2003-08-10 12:24:52 +04:00
|
|
|
/* $NetBSD: bswap64.c,v 1.5 2003/08/10 08:24:52 mrg Exp $ */
|
1999-01-15 16:31:15 +03:00
|
|
|
|
|
|
|
/*
|
2003-07-26 23:24:24 +04:00
|
|
|
* Written by Manuel Bouyer <bouyer@NetBSD.org>.
|
1999-01-15 16:31:15 +03:00
|
|
|
* Public domain.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
2003-08-10 12:24:52 +04:00
|
|
|
__RCSID("$NetBSD: bswap64.c,v 1.5 2003/08/10 08:24:52 mrg Exp $");
|
1999-01-15 16:31:15 +03:00
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <machine/bswap.h>
|
|
|
|
|
|
|
|
#undef bswap64
|
|
|
|
|
|
|
|
u_int64_t
|
|
|
|
bswap64(x)
|
|
|
|
u_int64_t x;
|
|
|
|
{
|
2003-08-10 02:37:49 +04:00
|
|
|
#ifdef _LP64
|
|
|
|
/*
|
|
|
|
* Assume we have wide enough registers to do it without touching
|
|
|
|
* memory.
|
|
|
|
*/
|
|
|
|
return ( (x << 56) & 0xff00000000000000UL ) |
|
|
|
|
( (x << 40) & 0x00ff000000000000UL ) |
|
|
|
|
( (x << 24) & 0x0000ff0000000000UL ) |
|
|
|
|
( (x << 8) & 0x000000ff00000000UL ) |
|
|
|
|
( (x >> 8) & 0x00000000ff000000UL ) |
|
|
|
|
( (x >> 24) & 0x0000000000ff0000UL ) |
|
|
|
|
( (x >> 40) & 0x000000000000ff00UL ) |
|
|
|
|
( (x >> 56) & 0x00000000000000ffUL );
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* Split the operation in two 32bit steps.
|
|
|
|
*/
|
|
|
|
u_int32_t tl, th;
|
|
|
|
|
2003-08-10 12:24:52 +04:00
|
|
|
th = bswap32((u_int32_t)(x & 0x00000000ffffffffULL));
|
|
|
|
tl = bswap32((u_int32_t)((x >> 32) & 0x00000000ffffffffULL));
|
2003-08-10 02:37:49 +04:00
|
|
|
return ((u_int64_t)th << 32) | tl;
|
|
|
|
#endif
|
1999-01-15 16:31:15 +03:00
|
|
|
}
|