📄 endian.h
字号:
/* $OpenBSD: endian.h,v 1.4 1999/07/21 05:58:25 csapuntz Exp $ *//*- * Copyright (c) 1997 Niklas Hallqvist. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Niklas Hallqvist. * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *//* * Generic definitions for little- and big-endian systems. Other endianesses * has to be dealt with in the specific machine/endian.h file for that port. * * This file is meant to be included from a little- or big-endian port's * machine/endian.h after setting BYTE_ORDER to either 1234 for little endian * or 4321 for big.. */ // TK: this file will certainly be moved to some better place or// even get rewritten. I added 64 bit support#ifndef _SYS_ENDIAN_H_#define _SYS_ENDIAN_H_#ifndef _POSIX_SOURCE#define BYTE_ORDER LITTLE_ENDIAN//#include <sys/cdefs.h>#define LITTLE_ENDIAN 1234#define BIG_ENDIAN 4321#define PDP_ENDIAN 3412#ifdef __GNUC__#define __swap16gen(x) ({ \ uint16 __swap16gen_x = (x); \ \ (uint16)((__swap16gen_x & 0xff) << 8 | \ (__swap16gen_x & 0xff00) >> 8); \})#define __swap32gen(x) ({ \ uint32 __swap32gen_x = (x); \ \ (uint32)((__swap32gen_x & 0xff) << 24 | \ (__swap32gen_x & 0xff00) << 8 | \ (__swap32gen_x & 0xff0000) >> 8 | \ (__swap32gen_x & 0xff000000) >> 24); \})#if 0// tried that way: gcc creates really stupid code#define __swap64gen(x) ({ \ uint64 __swap64gen_x = (x); \ \ (uint64)((__swap64gen_x & 0xff) << 56 | \ (__swap64gen_x & 0xff00) << 40 | \ (__swap64gen_x & 0xff0000) << 24 | \ (__swap64gen_x & 0xff000000) << 8 | \ (__swap64gen_x & 0xff00000000) >> 8 | \ (__swap64gen_x & 0xff0000000000) >> 24 | \ (__swap64gen_x & 0xff000000000000) >> 40 | \ (__swap64gen_x & 0xff00000000000000) >> 56); \})#else#define __swap64gen(x) ({ \ uint64 __swap64gen_x = (x); \ uint32 __swap64gen_low = __swap64gen_x & 0xffffffff; \ uint32 __swap64gen_high = __swap64gen_x >> 32; \ \ (uint64)(((uint64)(__swap32gen(__swap64gen_low)) << 32 | \ __swap32gen(__swap64gen_high))); \})#endif#else /* __GNUC__ *//* Note that these macros evaluate their arguments several times. */#define __swap16gen(x) \ (uint16)(((uint16)(x) & 0xff) << 8 | ((uint16)(x) & 0xff00) >> 8)#define __swap32gen(x) \ (uint32)(((uint32)(x) & 0xff) << 24 | \ ((uint32)(x) & 0xff00) << 8 | ((uint32)(x) & 0xff0000) >> 8 | \ ((uint32)(x) & 0xff000000) >> 24)#define __swap64gen(x) \ (uint64)(((uint64)(x) & 0xff) << 56 | \ ((uint64)(x) & 0xff00) << 40 | \ ((uint32)(x) & 0xff0000) << 24 | \ ((uint64)(x) & 0xff000000) << 8 | \ ((uint32)(x) & 0xff00000000) >> 8 | \ ((uint64)(x) & 0xff0000000000) >> 24 | \ ((uint32)(x) & 0xff000000000000) >> 40 | \ ((uint64)(x) & 0xff00000000000000) >> 56)#endif /* __GNUC__ *//* * Define MD_SWAP if you provide swap{16,32}md functions/macros that are * optimized for your architecture, These will be used for swap{16,32} * unless the argument is a constant and we are using GCC, where we can * take advantage of the CSE phase much better by using the generic version. */#ifdef MD_SWAP#if __GNUC__#define swap16(x) ({ \ uint16 __swap16_x = (x); \ \ __builtin_constant_p(x) ? __swap16gen(__swap16_x) : \ __swap16md(__swap16_x); \})#define swap32(x) ({ \ uint32 __swap32_x = (x); \ \ __builtin_constant_p(x) ? __swap32gen(__swap32_x) : \ __swap32md(__swap32_x); \})#define swap64(x) ({ \ uint64 __swap64_x = (x); \ \ __builtin_constant_p(x) ? __swap64gen(__swap64_x) : \ __swap64md(__swap64_x); \})#endif /* __GNUC__ */#else /* MD_SWAP */#define swap16 __swap16gen#define swap32 __swap32gen#define swap64 __swap64gen#endif /* MD_SWAP */#define swap16_multi(v, n) do { \ size_t __swap16_multi_n = (n); \ uint16 *__swap16_multi_v = (v); \ \ while (__swap16_multi_n) { \ *__swap16_multi_v = swap16(*__swap16_multi_v); \ __swap16_multi_v++; \ __swap16_multi_n--; \ } \} while (0)//__BEGIN_DECLS/*uint32 htobe32 __P((uint32));uint16 htobe16 __P((uint16));uint32 betoh32 __P((uint32));uint16 betoh16 __P((uint16));uint32 htole32 __P((uint32));uint16 htole16 __P((uint16));uint32 letoh32 __P((uint32));uint16 letoh16 __P((uint16));*///__END_DECLS#if BYTE_ORDER == LITTLE_ENDIAN/* Can be overridden by machine/endian.h before inclusion of this file. */#ifndef _QUAD_HIGHWORD#define _QUAD_HIGHWORD 1#endif#ifndef _QUAD_LOWWORD#define _QUAD_LOWWORD 0#endif#define htobe16 swap16#define htobe32 swap32#define htobe64 swap64#define betoh16 swap16#define betoh32 swap32#define betoh64 swap64#define htole16(x) (x)#define htole32(x) (x)#define htole64(x) (x)#define letoh16(x) (x)#define letoh32(x) (x)#define letoh64(x) (x)#endif /* BYTE_ORDER */#if BYTE_ORDER == BIG_ENDIAN/* Can be overridden by machine/endian.h before inclusion of this file. */#ifndef _QUAD_HIGHWORD#define _QUAD_HIGHWORD 0#endif#ifndef _QUAD_LOWWORD#define _QUAD_LOWWORD 1#endif#define htole16 swap16#define htole32 swap32#define htole64 swap64#define letoh16 swap16#define letoh32 swap32#define letoh64 swap64#define htobe16(x) (x)#define htobe32(x) (x)#define htobe64(x) (x)#define betoh16(x) (x)#define betoh32(x) (x)#define betoh64(x) (x)#endif /* BYTE_ORDER */#define htons htobe16#define htonl htobe32#define htonll htobe64#define ntohs betoh16#define ntohl betoh32#define ntohll betoh64#define NTOHLL(x) (x) = ntohll((uint64)(x))#define NTOHL(x) (x) = ntohl((uint32)(x))#define NTOHS(x) (x) = ntohs((uint16)(x))#define HTONLL(x) (x) = htonll((uint64)(x))#define HTONL(x) (x) = htonl((uint32)(x))#define HTONS(x) (x) = htons((uint16)(x))#endif /* _POSIX_SOURCE */#endif /* _SYS_ENDIAN_H_ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -