1 /* @(#)intcvt.h	1.4 03/12/29 Copyright 1986-2003 J. Schilling */
2 /*
3  *	Definitions for conversion to/from integer data types of various size.
4  *
5  *	Copyright (c) 1986-2003 J. Schilling
6  */
7 /*
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * this program; see the file COPYING.  If not, write to the Free Software
20  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22 
23 #ifndef	_INTCVT_H
24 #define	_INTCVT_H
25 
26 #ifndef _MCONFIG_H
27 #include <mconfig.h>
28 #endif
29 
30 #define	i_to_2_byte(a, i)	(((Uchar *)(a))[0] = ((i) >> 8) & 0xFF,\
31 				    ((Uchar *)(a))[1] = (i) & 0xFF)
32 
33 #define	i_to_3_byte(a, i)	(((Uchar *)(a))[0] = ((i) >> 16)& 0xFF,\
34 				    ((Uchar *)(a))[1] = ((i) >> 8) & 0xFF,\
35 				    ((Uchar *)(a))[2] = (i) & 0xFF)
36 
37 #define	i_to_4_byte(a, i)	(((Uchar *)(a))[0] = ((i) >> 24)& 0xFF,\
38 				    ((Uchar *)(a))[1] = ((i) >> 16)& 0xFF,\
39 				    ((Uchar *)(a))[2] = ((i) >> 8) & 0xFF,\
40 				    ((Uchar *)(a))[3] = (i) & 0xFF)
41 
42 
43 
44 #define	a_to_byte(a)		(((Int8_t *) a)[0])
45 
46 #define	a_to_u_byte(a)		((UInt8_t) \
47 				(((Uchar *) a)[0]		& 0xFF))
48 
49 #define	a_to_u_2_byte(a)	((UInt16_t) \
50 				((((Uchar *) a)[1]		& 0xFF) | \
51 				    (((Uchar *) a)[0] << 8	& 0xFF00)))
52 
53 #define	a_to_2_byte(a)		(int)(Int16_t)a_to_u_2_byte(a)
54 
55 #define	a_to_u_3_byte(a)	((Ulong) \
56 				((((Uchar *) a)[2]		& 0xFF) | \
57 				    (((Uchar *) a)[1] << 8	& 0xFF00) | \
58 				    (((Uchar *) a)[0] << 16	& 0xFF0000)))
59 
60 #define	a_to_3_byte(a)		a_to_u_3_byte(a)	/* XXX Is there a signed version ? */
61 
62 #ifdef	__STDC__
63 #	define	__TOP_4BYTE	0xFF000000UL
64 #else
65 #	define	__TOP_4BYTE	0xFF000000
66 #endif
67 
68 #define	a_to_u_4_byte(a)	((Ulong) \
69 				((((Uchar*) a)[3]		& 0xFF) | \
70 				    (((Uchar*) a)[2] << 8	& 0xFF00) | \
71 				    (((Uchar*) a)[1] << 16	& 0xFF0000) | \
72 				    (((Uchar*) a)[0] << 24	& __TOP_4BYTE)))
73 
74 #define	a_to_4_byte(a)		(long)(Int32_t)a_to_u_4_byte(a)
75 
76 /*
77  * Little Endian versions of above macros
78  */
79 #define	li_to_2_byte(a, i)	(((Uchar *)(a))[1] = ((i) >> 8) & 0xFF,\
80 				    ((Uchar *)(a))[0] = (i) & 0xFF)
81 
82 #define	li_to_3_byte(a, i)	(((Uchar *)(a))[2] = ((i) >> 16)& 0xFF,\
83 				    ((Uchar *)(a))[1] = ((i) >> 8) & 0xFF,\
84 				    ((Uchar *)(a))[0] = (i) & 0xFF)
85 
86 #define	li_to_4_byte(a, i)	(((Uchar *)(a))[3] = ((i) >> 24)& 0xFF,\
87 				    ((Uchar *)(a))[2] = ((i) >> 16)& 0xFF,\
88 				    ((Uchar *)(a))[1] = ((i) >> 8) & 0xFF,\
89 				    ((Uchar *)(a))[0] = (i) & 0xFF)
90 
91 
92 #define	la_to_u_2_byte(a)	((UInt16_t) \
93 				((((Uchar*) a)[0]		& 0xFF) | \
94 				    (((Uchar*) a)[1] << 8	& 0xFF00)))
95 
96 #define	la_to_2_byte(a)		(int)(Int16_t)la_to_u_2_byte(a)
97 
98 #define	la_to_u_3_byte(a)	((Ulong) \
99 				((((Uchar*) a)[0]		& 0xFF) | \
100 				    (((Uchar*) a)[1] << 8	& 0xFF00) | \
101 				    (((Uchar*) a)[2] << 16	& 0xFF0000)))
102 
103 #define	la_to_3_byte(a)		la_to_u_3_byte(a)	/* XXX Is there a signed version ? */
104 
105 #define	la_to_u_4_byte(a)	((Ulong) \
106 				((((Uchar*) a)[0]		& 0xFF) | \
107 				    (((Uchar*) a)[1] << 8	& 0xFF00) | \
108 				    (((Uchar*) a)[2] << 16	& 0xFF0000) | \
109 				    (((Uchar*) a)[3] << 24	& __TOP_4BYTE)))
110 
111 #define	la_to_4_byte(a)		(long)(Int32_t)la_to_u_4_byte(a)
112 
113 #endif	/* _INTCVT_H */
114