]> git.proxmox.com Git - mirror_edk2.git/blame - StdLib/Include/sys/bswap.h
StdLib: Clarify and improve comments.
[mirror_edk2.git] / StdLib / Include / sys / bswap.h
CommitLineData
2aa62f2b 1/* $NetBSD: bswap.h,v 1.12 2006/05/22 16:28:27 drochner Exp $ */\r
2\r
3/* Written by Manuel Bouyer. Public domain */\r
4\r
5#ifndef _SYS_BSWAP_H_\r
6#define _SYS_BSWAP_H_\r
7\r
8#ifndef _LOCORE\r
9#include <sys/EfiCdefs.h>\r
10#include <sys/types.h>\r
11\r
12#include <machine/bswap.h>\r
13\r
14__BEGIN_DECLS\r
15/* Always declare the functions in case their address is taken (etc) */\r
16#if !defined(__BSWAP_RENAME)\r
17uint16_t bswap16(uint16_t) __attribute__((__const__));\r
18uint32_t bswap32(uint32_t) __attribute__((__const__));\r
19#else\r
20uint16_t bswap16(uint16_t) __RENAME(__bswap16) __attribute__((__const__));\r
21uint32_t bswap32(uint32_t) __RENAME(__bswap32) __attribute__((__const__));\r
22#endif\r
23uint64_t bswap64(uint64_t) __attribute__((__const__));\r
24__END_DECLS\r
25\r
26#if defined(__GNUC__) && defined(__OPTIMIZE__)\r
27\r
28/* machine/byte_swap.h might have defined inline versions */\r
29#ifndef __BYTE_SWAP_U64_VARIABLE\r
30#define __BYTE_SWAP_U64_VARIABLE bswap64\r
31#endif\r
32\r
33#ifndef __BYTE_SWAP_U32_VARIABLE\r
34#define __BYTE_SWAP_U32_VARIABLE bswap32\r
35#endif\r
36\r
37#ifndef __BYTE_SWAP_U16_VARIABLE\r
38#define __BYTE_SWAP_U16_VARIABLE bswap16\r
39#endif\r
40\r
41#define __byte_swap_u64_constant(x) \\r
42 ((uint64_t) \\r
43 ((((x) & 0xff00000000000000ull) >> 56) | \\r
44 (((x) & 0x00ff000000000000ull) >> 40) | \\r
45 (((x) & 0x0000ff0000000000ull) >> 24) | \\r
46 (((x) & 0x000000ff00000000ull) >> 8) | \\r
47 (((x) & 0x00000000ff000000ull) << 8) | \\r
48 (((x) & 0x0000000000ff0000ull) << 24) | \\r
49 (((x) & 0x000000000000ff00ull) << 40) | \\r
50 (((x) & 0x00000000000000ffull) << 56)))\r
51\r
52#define __byte_swap_u32_constant(x) \\r
53 ((((x) & 0xff000000) >> 24) | \\r
54 (((x) & 0x00ff0000) >> 8) | \\r
55 (((x) & 0x0000ff00) << 8) | \\r
56 (((x) & 0x000000ff) << 24))\r
57\r
58#define __byte_swap_u16_constant(x) \\r
59 ((((x) & 0xff00) >> 8) | \\r
60 (((x) & 0x00ff) << 8))\r
61\r
62#define bswap64(x) \\r
63 (__builtin_constant_p((x)) ? \\r
64 __byte_swap_u64_constant(x) : __BYTE_SWAP_U64_VARIABLE(x))\r
65\r
66#define bswap32(x) \\r
67 (__builtin_constant_p((x)) ? \\r
68 __byte_swap_u32_constant(x) : __BYTE_SWAP_U32_VARIABLE(x))\r
69\r
70#define bswap16(x) \\r
71 (__builtin_constant_p((x)) ? \\r
72 __byte_swap_u16_constant(x) : __BYTE_SWAP_U16_VARIABLE(x))\r
73\r
74#endif /* __GNUC__ && __OPTIMIZE__ */\r
75#endif /* !_LOCORE */\r
76\r
77#endif /* !_SYS_BSWAP_H_ */\r