xref: /illumos-gate/usr/src/cmd/cdrw/byteorder.h (revision d48be21240dfd051b689384ce2b23479d757f2d8)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #ifndef	_BYTEORDER_H
28 #define	_BYTEORDER_H
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 #include <sys/types.h>
35 
36 #define	swap32(x) \
37 ((uint32_t)((((uint32_t)(x) & 0x000000ff) << 24) | \
38 (((uint32_t)(x) & 0x0000ff00) << 8) | \
39 (((uint32_t)(x) & 0x00ff0000) >> 8) | \
40 (((uint32_t)(x) & 0xff000000) >> 24)))
41 
42 #define	swap16(x) \
43 ((uint16_t)((((uint16_t)(x) & 0x00ff) << 8) | \
44 (((uint16_t)(x) & 0xff00) >> 8)))
45 
46 /*
47  * endianess issues
48  */
49 #if defined(_BIG_ENDIAN)
50 #define	CPU_TO_LE32(x)	swap32(x)
51 #define	CPU_TO_LE16(x)	swap16(x)
52 #define	CPU_TO_BE32(x)	(x)
53 #define	CPU_TO_BE16(x)	(x)
54 #else
55 #define	CPU_TO_LE32(x)	(x)
56 #define	CPU_TO_LE16(x)	(x)
57 #define	CPU_TO_BE32(x)	swap32(x)
58 #define	CPU_TO_BE16(x)	swap16(x)
59 #endif
60 
61 #define	LE32_TO_CPU(x)	CPU_TO_LE32(x)
62 #define	LE16_TO_CPU(x)	CPU_TO_LE16(x)
63 #define	BE32_TO_CPU(x)	CPU_TO_BE32(x)
64 #define	BE16_TO_CPU(x)	CPU_TO_BE16(x)
65 
66 #ifdef	__cplusplus
67 }
68 #endif
69 
70 #endif /* _BYTEORDER_H */
71