bswap.c revision 221358
1/*
2 * Written by Manuel Bouyer <bouyer@netbsd.org>.
3 * Public domain.
4 */
5
6#include <sys/cdefs.h>
7__FBSDID("$FreeBSD: head/lib/libstand/bswap.c 221358 2011-05-03 04:44:50Z rodrigc $");
8
9#if defined(LIBC_SCCS) && !defined(lint)
10static char *rcsid = "$NetBSD: bswap32.c,v 1.1 1997/10/09 15:42:33 bouyer Exp $";
11static char *rcsid = "$NetBSD: bswap64.c,v 1.1 1997/10/09 15:42:33 bouyer Exp $";
12#endif
13
14#include <sys/types.h>
15
16#undef bswap32
17#undef bswap64
18
19u_int32_t bswap32(u_int32_t x);
20u_int64_t bswap64(u_int64_t x);
21
22u_int32_t
23bswap32(u_int32_t x)
24{
25	return  ((x << 24) & 0xff000000 ) |
26			((x <<  8) & 0x00ff0000 ) |
27			((x >>  8) & 0x0000ff00 ) |
28			((x >> 24) & 0x000000ff );
29}
30
31u_int64_t
32bswap64(u_int64_t x)
33{
34	u_int32_t *p = (u_int32_t*)&x;
35	u_int32_t t;
36	t = bswap32(p[0]);
37	p[0] = bswap32(p[1]);
38	p[1] = t;
39	return x;
40}
41
42