Commit | Line | Data |
---|---|---|
e6e0e332 MW |
1 | /* -*-c-*- |
2 | * | |
3 | * $Id: bits.h,v 1.1 2000/05/21 11:28:30 mdw Exp $ | |
4 | * | |
5 | * Portable bit-level manipulation macros | |
6 | * | |
7 | * (c) 1998 Straylight/Edgeware | |
8 | * (c) 2000 Mark Wooding | |
9 | */ | |
10 | ||
11 | /*----- Licensing notice --------------------------------------------------* | |
12 | * | |
13 | * Copyright (c) 2000 Mark Wooding | |
14 | * All rights reserved. | |
15 | * | |
16 | * Redistribution and use in source and binary forms, with or without | |
17 | * modification, are permitted provided that the following conditions are | |
18 | * met: | |
19 | * | |
20 | * 1. Redistributions of source code must retain the above copyright | |
21 | * notice, this list of conditions and the following disclaimer. | |
22 | * | |
23 | * 2, Redistributions in binary form must reproduce the above copyright | |
24 | * notice, this list of conditions and the following disclaimer in the | |
25 | * documentation and/or other materials provided with the distribution. | |
26 | * | |
27 | * 3. The name of the authors may not be used to endorse or promote | |
28 | * products derived from this software without specific prior written | |
29 | * permission. | |
30 | * | |
31 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
32 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
33 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN | |
34 | * NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
35 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
36 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
37 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
38 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
39 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
40 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
41 | * POSSIBILITY OF SUCH DAMAGE. | |
42 | * | |
43 | * Instead of accepting the above terms, you may redistribute and/or modify | |
44 | * this software under the terms of either the GNU General Public License, | |
45 | * or the GNU Library General Public License, published by the Free | |
46 | * Software Foundation; either version 2 of the License, or (at your | |
47 | * option) any later version. | |
48 | */ | |
49 | ||
50 | /*----- Revision history --------------------------------------------------* | |
51 | * | |
52 | * $Log: bits.h,v $ | |
53 | * Revision 1.1 2000/05/21 11:28:30 mdw | |
54 | * Initial check-in. | |
55 | * | |
56 | * --- Past lives (mLib) --- * | |
57 | * | |
58 | * Revision 1.4 1999/12/10 23:42:04 mdw | |
59 | * Change header file guard names. | |
60 | * | |
61 | * Revision 1.3 1999/06/20 23:31:52 mdw | |
62 | * More portability enhancements. | |
63 | * | |
64 | * Revision 1.2 1999/06/17 00:12:46 mdw | |
65 | * Improve portability for shift and rotate macros. | |
66 | * | |
67 | * Revision 1.1 1999/06/01 09:46:19 mdw | |
68 | * New addition: bit manipulation macros. | |
69 | */ | |
70 | ||
71 | #ifndef BITS_H | |
72 | #define BITS_H | |
73 | ||
74 | #ifdef __cplusplus | |
75 | extern "C" { | |
76 | #endif | |
77 | ||
78 | /*----- Header files ------------------------------------------------------*/ | |
79 | ||
80 | #include <limits.h> | |
81 | #include <stddef.h> | |
82 | ||
83 | /*----- Decide on some types ----------------------------------------------*/ | |
84 | ||
85 | /* --- Decide on a 32-bit type --- * | |
86 | * | |
87 | * I want a type which is capable of expressing 32-bit numbers. Because some | |
88 | * implementations have 64-bit @long@s (infinitely preferable to the abortion | |
89 | * that is @long long@), using @unsigned long@ regardless is wasteful. So, | |
90 | * if @int@ appears to be good enough, then I'll go with that. | |
91 | */ | |
92 | ||
93 | #if UINT_MAX >= 0xffffffffu | |
94 | typedef unsigned int uint32; | |
95 | #else | |
96 | typedef unsigned long uint32; | |
97 | #endif | |
98 | ||
99 | /* --- Decide on a 24-bit type --- */ | |
100 | ||
101 | #if UINT_MAX >= 0x00ffffffu | |
102 | typedef unsigned int uint24; | |
103 | #else | |
104 | typedef unsigned long uint24; | |
105 | #endif | |
106 | ||
107 | /* --- Decide on 16-bit and 8-bit types --- * | |
108 | * | |
109 | * This is more for brevity than anything else. | |
110 | */ | |
111 | ||
112 | typedef unsigned short uint16; | |
113 | typedef unsigned char octet; | |
114 | ||
115 | /* --- WARNING! --- * | |
116 | * | |
117 | * Never lose sight of the fact that the above types may be wider than the | |
118 | * names suggest. Some architectures have 32-bit @short@s for example. | |
119 | */ | |
120 | ||
121 | /*----- Macros ------------------------------------------------------------*/ | |
122 | ||
123 | /* --- Useful masks --- */ | |
124 | ||
125 | #define MASK8 0xffu | |
126 | #define MASK16 0xffffu | |
127 | #define MASK24 0xffffffu | |
128 | #define MASK32 0xffffffffu | |
129 | ||
130 | /* --- Type coercions --- */ | |
131 | ||
132 | #define U8(x) ((octet)((x) & MASK8)) | |
133 | #define U16(x) ((uint16)((x) & MASK16)) | |
134 | #define U24(x) ((uint24)((x) & MASK24)) | |
135 | #define U32(x) ((uint32)((x) & MASK32)) | |
136 | ||
137 | /* --- Safe shifting macros --- */ | |
138 | ||
139 | #define LSL8(v, s) U8(U8(v) << ((s) & 7u)) | |
140 | #define LSR8(v, s) U8(U8(v) >> ((s) & 7u)) | |
141 | #define LSL16(v, s) U16(U16(v) << ((s) & 15u)) | |
142 | #define LSR16(v, s) U16(U16(v) >> ((s) & 15u)) | |
143 | #define LSL24(v, s) U24(U24(v) << ((s) % 24u)) | |
144 | #define LSR24(v, s) U24(U24(v) >> ((s) % 24u)) | |
145 | #define LSL32(v, s) U32(U32(v) << ((s) & 31u)) | |
146 | #define LSR32(v, s) U32(U32(v) >> ((s) & 31u)) | |
147 | ||
148 | /* --- Rotation macros --- */ | |
149 | ||
150 | #define ROL8(v, s) (LSL8((v), (s)) | (LSR8((v), 8u - (s)))) | |
151 | #define ROR8(v, s) (LSR8((v), (s)) | (LSL8((v), 8u - (s)))) | |
152 | #define ROL16(v, s) (LSL16((v), (s)) | (LSR16((v), 16u - (s)))) | |
153 | #define ROR16(v, s) (LSR16((v), (s)) | (LSL16((v), 16u - (s)))) | |
154 | #define ROL24(v, s) (LSL24((v), (s)) | (LSR24((v), 24u - (s)))) | |
155 | #define ROR24(v, s) (LSR24((v), (s)) | (LSL24((v), 24u - (s)))) | |
156 | #define ROL32(v, s) (LSL32((v), (s)) | (LSR32((v), 32u - (s)))) | |
157 | #define ROR32(v, s) (LSR32((v), (s)) | (LSL32((v), 32u - (s)))) | |
158 | ||
159 | /* --- Storage and retrieval --- */ | |
160 | ||
161 | #define GETBYTE(p, o) (((octet *)(p))[o] & MASK8) | |
162 | #define PUTBYTE(p, o, v) (((octet *)(p))[o] = U8((v))) | |
163 | ||
164 | #define LOAD8(p) (GETBYTE((p), 0)) | |
165 | #define STORE8(p, v) (PUTBYTE((p), 0, (v))) | |
166 | ||
167 | #define LOAD16_B(p) \ | |
168 | (((uint16)GETBYTE((p), 0) << 8) | \ | |
169 | ((uint16)GETBYTE((p), 1) << 0)) | |
170 | #define LOAD16_L(p) \ | |
171 | (((uint16)GETBYTE((p), 0) << 0) | \ | |
172 | ((uint16)GETBYTE((p), 1) << 8)) | |
173 | #define LOAD16(p) LOAD16_B((p)) | |
174 | ||
175 | #define STORE16_B(p, v) \ | |
176 | (PUTBYTE((p), 0, (uint16)(v) >> 8), \ | |
177 | PUTBYTE((p), 1, (uint16)(v) >> 0)) | |
178 | #define STORE16_L(p, v) \ | |
179 | (PUTBYTE((p), 0, (uint16)(v) >> 0), \ | |
180 | PUTBYTE((p), 1, (uint16)(v) >> 8)) | |
181 | #define STORE16(p, v) STORE16_B((p), (v)) | |
182 | ||
183 | #define LOAD24_B(p) \ | |
184 | (((uint24)GETBYTE((p), 0) << 16) | \ | |
185 | ((uint24)GETBYTE((p), 1) << 8) | \ | |
186 | ((uint24)GETBYTE((p), 2) << 0)) | |
187 | #define LOAD24_L(p) \ | |
188 | (((uint24)GETBYTE((p), 0) << 0) | \ | |
189 | ((uint24)GETBYTE((p), 1) << 8) | \ | |
190 | ((uint24)GETBYTE((p), 2) << 16)) | |
191 | #define LOAD24(p) LOAD24_B((p)) | |
192 | ||
193 | #define STORE24_B(p, v) \ | |
194 | (PUTBYTE((p), 0, (uint24)(v) >> 16), \ | |
195 | PUTBYTE((p), 1, (uint24)(v) >> 8), \ | |
196 | PUTBYTE((p), 2, (uint24)(v) >> 0)) | |
197 | #define STORE24_L(p, v) \ | |
198 | (PUTBYTE((p), 0, (uint24)(v) >> 0), \ | |
199 | PUTBYTE((p), 1, (uint24)(v) >> 8), \ | |
200 | PUTBYTE((p), 2, (uint24)(v) >> 16)) | |
201 | #define STORE24(p, v) STORE24_B((p), (v)) | |
202 | ||
203 | #define LOAD32_B(p) \ | |
204 | (((uint32)GETBYTE((p), 0) << 24) | \ | |
205 | ((uint32)GETBYTE((p), 1) << 16) | \ | |
206 | ((uint32)GETBYTE((p), 2) << 8) | \ | |
207 | ((uint32)GETBYTE((p), 3) << 0)) | |
208 | #define LOAD32_L(p) \ | |
209 | (((uint32)GETBYTE((p), 0) << 0) | \ | |
210 | ((uint32)GETBYTE((p), 1) << 8) | \ | |
211 | ((uint32)GETBYTE((p), 2) << 16) | \ | |
212 | ((uint32)GETBYTE((p), 3) << 24)) | |
213 | #define LOAD32(p) LOAD32_B((p)) | |
214 | ||
215 | #define STORE32_B(p, v) \ | |
216 | (PUTBYTE((p), 0, (uint32)(v) >> 24), \ | |
217 | PUTBYTE((p), 1, (uint32)(v) >> 16), \ | |
218 | PUTBYTE((p), 2, (uint32)(v) >> 8), \ | |
219 | PUTBYTE((p), 3, (uint32)(v) >> 0)) | |
220 | #define STORE32_L(p, v) \ | |
221 | (PUTBYTE((p), 0, (uint32)(v) >> 0), \ | |
222 | PUTBYTE((p), 1, (uint32)(v) >> 8), \ | |
223 | PUTBYTE((p), 2, (uint32)(v) >> 16), \ | |
224 | PUTBYTE((p), 3, (uint32)(v) >> 24)) | |
225 | #define STORE32(p, v) STORE32_B((p), (v)) | |
226 | ||
227 | /*----- That's all, folks -------------------------------------------------*/ | |
228 | ||
229 | #ifdef __cplusplus | |
230 | } | |
231 | #endif | |
232 | ||
233 | #endif |