1 /* $NetBSD: byteorder.h,v 1.4 2010/02/28 14:45:47 haad Exp $ */ 2 3 /* 4 * CDDL HEADER START 5 * 6 * The contents of this file are subject to the terms of the 7 * Common Development and Distribution License (the "License"). 8 * You may not use this file except in compliance with the License. 9 * 10 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 11 * or http://www.opensolaris.org/os/licensing. 12 * See the License for the specific language governing permissions 13 * and limitations under the License. 14 * 15 * When distributing Covered Code, include this CDDL HEADER in each 16 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 17 * If applicable, add the following below this CDDL HEADER, with the 18 * fields enclosed by brackets "[]" replaced with your own identifying 19 * information: Portions Copyright [yyyy] [name of copyright owner] 20 * 21 * CDDL HEADER END 22 */ 23 24 /* 25 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 26 * Use is subject to license terms. 27 */ 28 29 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 30 /* All Rights Reserved */ 31 32 /* 33 * University Copyright- Copyright (c) 1982, 1986, 1988 34 * The Regents of the University of California 35 * All Rights Reserved 36 * 37 * University Acknowledgment- Portions of this document are derived from 38 * software developed by the University of California, Berkeley, and its 39 * contributors. 40 */ 41 42 #ifndef _OPENSOLARIS_SYS_BYTEORDER_H_ 43 #define _OPENSOLARIS_SYS_BYTEORDER_H_ 44 45 /* 46 * Macros to reverse byte order 47 */ 48 #define BSWAP_8(x) ((x) & 0xff) 49 #define BSWAP_16(x) ((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8)) 50 #define BSWAP_32(x) ((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16)) 51 #define BSWAP_64(x) ((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32)) 52 53 #define BMASK_8(x) ((x) & 0xff) 54 #define BMASK_16(x) ((x) & 0xffff) 55 #define BMASK_32(x) ((x) & 0xffffffff) 56 #define BMASK_64(x) (x) 57 58 /* 59 * Macros to convert from a specific byte order to/from native byte order 60 */ 61 #if _BYTE_ORDER == _BIG_ENDIAN 62 #define LE_64(x) BSWAP_64(x) 63 #else 64 #define LE_64(x) BMASK_64(x) 65 #endif 66 67 /* 68 * Macros to convert from a specific byte order to/from native byte order 69 */ 70 #ifdef _BIG_ENDIAN 71 #define BE_8(x) BMASK_8(x) 72 #define BE_16(x) BMASK_16(x) 73 #define BE_32(x) BMASK_32(x) 74 #define BE_64(x) BMASK_64(x) 75 #define LE_8(x) BSWAP_8(x) 76 #define LE_16(x) BSWAP_16(x) 77 #define LE_32(x) BSWAP_32(x) 78 #define LE_64(x) BSWAP_64(x) 79 #else 80 #define LE_8(x) BMASK_8(x) 81 #define LE_16(x) BMASK_16(x) 82 #define LE_32(x) BMASK_32(x) 83 #define LE_64(x) BMASK_64(x) 84 #define BE_8(x) BSWAP_8(x) 85 #define BE_16(x) BSWAP_16(x) 86 #define BE_32(x) BSWAP_32(x) 87 #define BE_64(x) BSWAP_64(x) 88 #endif 89 90 #endif /* _OPENSOLARIS_SYS_BYTEORDER_H_ */ 91