Home | History | Annotate | Line # | Download | only in sys
      1 /*	$NetBSD: byteorder.h,v 1.6 2026/01/07 08:23:36 nia 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  * $FreeBSD: head/sys/cddl/compat/opensolaris/sys/byteorder.h 246586 2013-02-09 06:39:28Z delphij $
     24  */
     25 
     26 /*
     27  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
     28  * Use is subject to license terms.
     29  */
     30 
     31 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
     32 /*	  All Rights Reserved  	*/
     33 
     34 /*
     35  * University Copyright- Copyright (c) 1982, 1986, 1988
     36  * The Regents of the University of California
     37  * All Rights Reserved
     38  *
     39  * University Acknowledgment- Portions of this document are derived from
     40  * software developed by the University of California, Berkeley, and its
     41  * contributors.
     42  */
     43 
     44 #ifndef _OPENSOLARIS_SYS_BYTEORDER_H_
     45 #define	_OPENSOLARIS_SYS_BYTEORDER_H_
     46 #include <sys/endian.h>
     47 
     48 /*
     49  * Macros to reverse byte order
     50  */
     51 #define	BSWAP_8(x)	((x) & 0xff)
     52 #define	BSWAP_16(x)	((BSWAP_8(x) << 8) | BSWAP_8((x) >> 8))
     53 #define	BSWAP_32(x)	((BSWAP_16(x) << 16) | BSWAP_16((x) >> 16))
     54 #define	BSWAP_64(x)	((BSWAP_32(x) << 32) | BSWAP_32((x) >> 32))
     55 
     56 #define	BMASK_8(x)	((x) & 0xff)
     57 #define	BMASK_16(x)	((x) & 0xffff)
     58 #define	BMASK_32(x)	((x) & 0xffffffff)
     59 #define	BMASK_64(x)	(x)
     60 
     61 /*
     62  * Macros to convert from a specific byte order to/from native byte order
     63  */
     64 #if BYTE_ORDER == BIG_ENDIAN
     65 #define	BE_8(x)		BMASK_8(x)
     66 #define	BE_16(x)	BMASK_16(x)
     67 #define	BE_32(x)	BMASK_32(x)
     68 #define	BE_64(x)	BMASK_64(x)
     69 #define	LE_8(x)		BSWAP_8(x)
     70 #define	LE_16(x)	BSWAP_16(x)
     71 #define	LE_32(x)	BSWAP_32(x)
     72 #define	LE_64(x)	BSWAP_64(x)
     73 #else
     74 #define	LE_8(x)		BMASK_8(x)
     75 #define	LE_16(x)	BMASK_16(x)
     76 #define	LE_32(x)	BMASK_32(x)
     77 #define	LE_64(x)	BMASK_64(x)
     78 #define	BE_8(x)		BSWAP_8(x)
     79 #define	BE_16(x)	BSWAP_16(x)
     80 #define	BE_32(x)	BSWAP_32(x)
     81 #define	BE_64(x)	BSWAP_64(x)
     82 #endif
     83 
     84 #if BYTE_ORDER == BIG_ENDIAN
     85 #define	htonll(x)	BMASK_64(x)
     86 #define	ntohll(x)	BMASK_64(x)
     87 #else
     88 #define	htonll(x)	BSWAP_64(x)
     89 #define	ntohll(x)	BSWAP_64(x)
     90 #endif
     91 
     92 #define BE_IN32(xa)	htonl(*((uint32_t *)(void *)(xa)))
     93 
     94 #endif /* _OPENSOLARIS_SYS_BYTEORDER_H_ */
     95