More portability enhancements.
[mLib] / bits.h
1 /* -*-c-*-
2 *
3 * $Id: bits.h,v 1.3 1999/06/20 23:31:52 mdw Exp $
4 *
5 * Portable bit-level manipulation macros
6 *
7 * (c) 1998 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of the mLib utilities library.
13 *
14 * mLib is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
18 *
19 * mLib is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
23 *
24 * You should have received a copy of the GNU Library General Public
25 * License along with mLib; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: bits.h,v $
33 * Revision 1.3 1999/06/20 23:31:52 mdw
34 * More portability enhancements.
35 *
36 * Revision 1.2 1999/06/17 00:12:46 mdw
37 * Improve portability for shift and rotate macros.
38 *
39 * Revision 1.1 1999/06/01 09:46:19 mdw
40 * New addition: bit manipulation macros.
41 *
42 */
43
44 #ifndef BITS_H
45 #define BITS_H
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 /*----- Header files ------------------------------------------------------*/
52
53 #include <limits.h>
54 #include <stddef.h>
55
56 /*----- Decide on some types ----------------------------------------------*/
57
58 /* --- Decide on a 32-bit type --- *
59 *
60 * I want a type which is capable of expressing 32-bit numbers. Because some
61 * implementations have 64-bit @long@s (infinitely preferable to the abortion
62 * that is @long long@), using @unsigned long@ regardless is wasteful. So,
63 * if @int@ appears to be good enough, then I'll go with that.
64 */
65
66 #if UINT_MAX >= 0xffffffffu
67 typedef unsigned int uint32;
68 #else
69 typedef unsigned long uint32;
70 #endif
71
72 /* --- Decide on 16-bit and 8-bit types --- *
73 *
74 * This is more for brevity than anything else.
75 */
76
77 typedef unsigned short uint16;
78 typedef unsigned char octet;
79
80 /* --- WARNING! --- *
81 *
82 * Never lose sight of the fact that the above types may be wider than the
83 * names suggest. Some architectures have 32-bit @short@s for example.
84 */
85
86 /*----- Macros ------------------------------------------------------------*/
87
88 /* --- Useful masks --- */
89
90 #define MASK8 0xffu
91 #define MASK16 0xffffu
92 #define MASK32 0xffffffffu
93
94 /* --- Type coercions --- */
95
96 #define U8(x) ((octet)((x) & MASK8))
97 #define U16(x) ((uint16)((x) & MASK16))
98 #define U32(x) ((uint32)((x) & MASK32))
99
100 /* --- Safe shifting macros --- */
101
102 #define LSL8(v, s) U8(U8(v) << ((s) & 7u))
103 #define LSR8(v, s) U8(U8(v) >> ((s) & 7u))
104 #define LSL16(v, s) U16(U16(v) << ((s) & 15u))
105 #define LSR16(v, s) U16(U16(v) >> ((s) & 15u))
106 #define LSL32(v, s) U32(U32(v) << ((s) & 31u))
107 #define LSR32(v, s) U32(U32(v) >> ((s) & 31u))
108
109 /* --- Rotation macros --- */
110
111 #define ROL8(v, s) (LSL8((v), (s)) | (LSR8((v), 8u - (s))))
112 #define ROR8(v, s) (LSR8((v), (s)) | (LSL8((v), 8u - (s))))
113 #define ROL16(v, s) (LSL16((v), (s)) | (LSR16((v), 16u - (s))))
114 #define ROR16(v, s) (LSR16((v), (s)) | (LSL16((v), 16u - (s))))
115 #define ROL32(v, s) (LSL32((v), (s)) | (LSR32((v), 32u - (s))))
116 #define ROR32(v, s) (LSR32((v), (s)) | (LSL32((v), 32u - (s))))
117
118 /* --- Storage and retrieval --- */
119
120 #define GETBYTE(p, o) (((octet *)(p))[o] & MASK8)
121 #define PUTBYTE(p, o, v) (((octet *)(p))[o] = U8((v)))
122
123 #define LOAD8(p) (GETBYTE((p), 0))
124 #define STORE8(p, v) (PUTBYTE((p), 0, (v)))
125
126 #define LOAD16_B(p) \
127 (((uint16)GETBYTE((p), 0) << 8) | \
128 ((uint16)GETBYTE((p), 1) << 0))
129 #define LOAD16_L(p) \
130 (((uint16)GETBYTE((p), 0) << 0) | \
131 ((uint16)GETBYTE((p), 1) << 8))
132 #define LOAD16(p) LOAD16_B((p))
133
134 #define STORE16_B(p, v) \
135 (PUTBYTE((p), 0, (uint16)(v) >> 8), \
136 PUTBYTE((p), 1, (uint16)(v) >> 0))
137 #define STORE16_L(p, v) \
138 (PUTBYTE((p), 0, (uint16)(v) >> 0), \
139 PUTBYTE((p), 1, (uint16)(v) >> 8))
140 #define STORE16(p, v) STORE16_B((p), (v))
141
142 #define LOAD32_B(p) \
143 (((uint32)GETBYTE((p), 0) << 24) | \
144 ((uint32)GETBYTE((p), 1) << 16) | \
145 ((uint32)GETBYTE((p), 2) << 8) | \
146 ((uint32)GETBYTE((p), 3) << 0))
147 #define LOAD32_L(p) \
148 (((uint32)GETBYTE((p), 0) << 0) | \
149 ((uint32)GETBYTE((p), 1) << 8) | \
150 ((uint32)GETBYTE((p), 2) << 16) | \
151 ((uint32)GETBYTE((p), 3) << 24))
152 #define LOAD32(p) LOAD32_B((p))
153
154 #define STORE32_B(p, v) \
155 (PUTBYTE((p), 0, (uint32)(v) >> 24), \
156 PUTBYTE((p), 1, (uint32)(v) >> 16), \
157 PUTBYTE((p), 2, (uint32)(v) >> 8), \
158 PUTBYTE((p), 3, (uint32)(v) >> 0))
159 #define STORE32_L(p, v) \
160 (PUTBYTE((p), 0, (uint32)(v) >> 0), \
161 PUTBYTE((p), 1, (uint32)(v) >> 8), \
162 PUTBYTE((p), 2, (uint32)(v) >> 16), \
163 PUTBYTE((p), 3, (uint32)(v) >> 24))
164 #define STORE32(p, v) STORE32_B((p), (v))
165
166 /*----- That's all, folks -------------------------------------------------*/
167
168 #ifdef __cplusplus
169 }
170 #endif
171
172 #endif