1 /* @(#)byteorder.h	1.8 09/05/05 Copyright 1998,1999 Heiko Eissfeldt, Copyright 2006-2009 J. Schilling */
2 
3 #if	!defined(_BYTEORDER_H) || !defined(MYBYTE_ORDER)
4 #undef	_BYTEORDER_H
5 #define	_BYTEORDER_H
6 #undef	MYBYTE_ORDER
7 #define	MYBYTE_ORDER 1
8 
9 #include <schily/btorder.h>
10 
11 
12 /*
13  * supply the byte order macros
14  */
15 /*
16  * The contents of this file are subject to the terms of the
17  * Common Development and Distribution License, Version 1.0 only
18  * (the "License").  You may not use this file except in compliance
19  * with the License.
20  *
21  * See the file CDDL.Schily.txt in this distribution for details.
22  * A copy of the CDDL is also available via the Internet at
23  * http://www.opensource.org/licenses/cddl1.txt
24  *
25  * When distributing Covered Code, include this CDDL HEADER in each
26  * file and include the License file CDDL.Schily.txt from this distribution.
27  */
28 
29 #if	defined(WORDS_BIGENDIAN)
30 #define	MY_BIG_ENDIAN		1
31 #define	MY_LITTLE_ENDIAN	0
32 #else
33 #define	MY_BIG_ENDIAN		0
34 #define	MY_LITTLE_ENDIAN	1
35 #endif
36 
37 #undef	cpu_to_le32
38 #undef	cpu_to_le16
39 #undef	cpu_to_be32
40 #undef	cpu_to_be16
41 #undef	le32_to_cpu
42 #undef	le16_to_cpu
43 #undef	be32_to_cpu
44 #undef	be16_to_cpu
45 
46 #define	revert4bytes(x) \
47 	((unsigned long int)   ((((unsigned long int)(x) & ULONG_C(0x000000ff)) << 24) | \
48 				(((unsigned long int)(x) & ULONG_C(0x0000ff00)) <<  8) | \
49 				(((unsigned long int)(x) & ULONG_C(0x00ff0000)) >>  8) | \
50 				(((unsigned long int)(x) & ULONG_C(0xff000000)) >> 24)))
51 #define	revert2bytes(x) \
52 	((unsigned short int)  ((((unsigned short int)(x) & 0x00ff) <<  8) | \
53 				(((unsigned short int)(x) & 0xff00) >>  8)))
54 
55 #if    MY_BIG_ENDIAN == 1
56 #define	cpu_to_le32(x)	revert4bytes(x)
57 #define	cpu_to_le16(x)	revert2bytes(x)
58 #define	le32_to_cpu(x)	cpu_to_le32(x)
59 #define	le16_to_cpu(x)	cpu_to_le16(x)
60 #define	cpu_to_be32(x)	(x)
61 #define	cpu_to_be16(x)	(x)
62 #define	be32_to_cpu(x)	(x)
63 #define	be16_to_cpu(x)	(x)
64 #else
65 #define	cpu_to_be32(x)	revert4bytes(x)
66 #define	cpu_to_be16(x)	revert2bytes(x)
67 #define	be32_to_cpu(x)	cpu_to_be32(x)
68 #define	be16_to_cpu(x)	cpu_to_be16(x)
69 #define	cpu_to_le32(x)	(x)
70 #define	cpu_to_le16(x)	(x)
71 #define	le32_to_cpu(x)	(x)
72 #define	le16_to_cpu(x)	(x)
73 #endif
74 
75 #define	GET_LE_UINT_FROM_CHARP(p)	((unsigned int)((*(p+3))<<24)|((*(p+2))<<16)|((*(p+1))<<8)|(*(p)))
76 #define	GET_BE_UINT_FROM_CHARP(p)	((unsigned int)((*(p))<<24)|((*(p+1))<<16)|((*(p+2))<<8)|(*(p+3)))
77 
78 #endif /* _BYTEORDER_H */
79