NetBSD/lib/libutil/bswap.c

49 lines
837 B
C
Raw Normal View History

1998-12-09 17:35:02 +03:00
/* $NetBSD: bswap.c,v 1.2 1998/12/09 14:35:02 christos Exp $ */
/*
* Written by Manuel Bouyer <bouyer@netbsd.org>.
* Public domain.
*/
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
1998-12-09 17:35:02 +03:00
__RCSID("$NetBSD: bswap.c,v 1.2 1998/12/09 14:35:02 christos Exp $");
#endif /* LIBC_SCCS and not lint */
#include <sys/types.h>
#undef bswap64
#ifndef bswap16
u_int16_t
bswap16(x)
u_int16_t x;
{
1998-12-09 17:35:02 +03:00
return (((u_int32_t)x << 8) & 0xff00) | (((u_int32_t)x >> 8) & 0x00ff);
}
#endif
#ifndef bswap32
u_int32_t
bswap32(x)
u_int32_t x;
{
return ((x << 24) & 0xff000000 ) |
((x << 8) & 0x00ff0000 ) |
((x >> 8) & 0x0000ff00 ) |
((x >> 24) & 0x000000ff );
}
#endif
u_int64_t
bswap64(x)
u_int64_t x;
{
1998-12-09 17:35:02 +03:00
u_int32_t *p = (u_int32_t*)(void *)&x;
u_int32_t t;
t = bswap32(p[0]);
p[0] = bswap32(p[1]);
p[1] = t;
return x;
}