storin.{tests,debug}-ref: Ancient versions of the test output.
[storin] / bits.h
1 /* -*-c-*-
2 *
3 * $Id$
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 AUTHORS 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.2 2000/07/02 15:21:20 mdw
54 * Fix licence text.
55 *
56 * Revision 1.1 2000/05/21 11:28:30 mdw
57 * Initial check-in.
58 *
59 * --- Past lives (mLib) --- *
60 *
61 * Revision 1.4 1999/12/10 23:42:04 mdw
62 * Change header file guard names.
63 *
64 * Revision 1.3 1999/06/20 23:31:52 mdw
65 * More portability enhancements.
66 *
67 * Revision 1.2 1999/06/17 00:12:46 mdw
68 * Improve portability for shift and rotate macros.
69 *
70 * Revision 1.1 1999/06/01 09:46:19 mdw
71 * New addition: bit manipulation macros.
72 */
73
74 #ifndef BITS_H
75 #define BITS_H
76
77 #ifdef __cplusplus
78 extern "C" {
79 #endif
80
81 /*----- Header files ------------------------------------------------------*/
82
83 #include <limits.h>
84 #include <stddef.h>
85
86 /*----- Decide on some types ----------------------------------------------*/
87
88 /* --- Decide on a 32-bit type --- *
89 *
90 * I want a type which is capable of expressing 32-bit numbers. Because some
91 * implementations have 64-bit @long@s (infinitely preferable to the abortion
92 * that is @long long@), using @unsigned long@ regardless is wasteful. So,
93 * if @int@ appears to be good enough, then I'll go with that.
94 */
95
96 #if UINT_MAX >= 0xffffffffu
97 typedef unsigned int uint32;
98 #else
99 typedef unsigned long uint32;
100 #endif
101
102 /* --- Decide on a 24-bit type --- */
103
104 #if UINT_MAX >= 0x00ffffffu
105 typedef unsigned int uint24;
106 #else
107 typedef unsigned long uint24;
108 #endif
109
110 /* --- Decide on 16-bit and 8-bit types --- *
111 *
112 * This is more for brevity than anything else.
113 */
114
115 typedef unsigned short uint16;
116 typedef unsigned char octet;
117
118 /* --- WARNING! --- *
119 *
120 * Never lose sight of the fact that the above types may be wider than the
121 * names suggest. Some architectures have 32-bit @short@s for example.
122 */
123
124 /*----- Macros ------------------------------------------------------------*/
125
126 /* --- Useful masks --- */
127
128 #define MASK8 0xffu
129 #define MASK16 0xffffu
130 #define MASK24 0xffffffu
131 #define MASK32 0xffffffffu
132
133 /* --- Type coercions --- */
134
135 #define U8(x) ((octet)((x) & MASK8))
136 #define U16(x) ((uint16)((x) & MASK16))
137 #define U24(x) ((uint24)((x) & MASK24))
138 #define U32(x) ((uint32)((x) & MASK32))
139
140 /* --- Safe shifting macros --- */
141
142 #define LSL8(v, s) U8(U8(v) << ((s) & 7u))
143 #define LSR8(v, s) U8(U8(v) >> ((s) & 7u))
144 #define LSL16(v, s) U16(U16(v) << ((s) & 15u))
145 #define LSR16(v, s) U16(U16(v) >> ((s) & 15u))
146 #define LSL24(v, s) U24(U24(v) << ((s) % 24u))
147 #define LSR24(v, s) U24(U24(v) >> ((s) % 24u))
148 #define LSL32(v, s) U32(U32(v) << ((s) & 31u))
149 #define LSR32(v, s) U32(U32(v) >> ((s) & 31u))
150
151 /* --- Rotation macros --- */
152
153 #define ROL8(v, s) (LSL8((v), (s)) | (LSR8((v), 8u - (s))))
154 #define ROR8(v, s) (LSR8((v), (s)) | (LSL8((v), 8u - (s))))
155 #define ROL16(v, s) (LSL16((v), (s)) | (LSR16((v), 16u - (s))))
156 #define ROR16(v, s) (LSR16((v), (s)) | (LSL16((v), 16u - (s))))
157 #define ROL24(v, s) (LSL24((v), (s)) | (LSR24((v), 24u - (s))))
158 #define ROR24(v, s) (LSR24((v), (s)) | (LSL24((v), 24u - (s))))
159 #define ROL32(v, s) (LSL32((v), (s)) | (LSR32((v), 32u - (s))))
160 #define ROR32(v, s) (LSR32((v), (s)) | (LSL32((v), 32u - (s))))
161
162 /* --- Storage and retrieval --- */
163
164 #define GETBYTE(p, o) (((octet *)(p))[o] & MASK8)
165 #define PUTBYTE(p, o, v) (((octet *)(p))[o] = U8((v)))
166
167 #define LOAD8(p) (GETBYTE((p), 0))
168 #define STORE8(p, v) (PUTBYTE((p), 0, (v)))
169
170 #define LOAD16_B(p) \
171 (((uint16)GETBYTE((p), 0) << 8) | \
172 ((uint16)GETBYTE((p), 1) << 0))
173 #define LOAD16_L(p) \
174 (((uint16)GETBYTE((p), 0) << 0) | \
175 ((uint16)GETBYTE((p), 1) << 8))
176 #define LOAD16(p) LOAD16_B((p))
177
178 #define STORE16_B(p, v) \
179 (PUTBYTE((p), 0, (uint16)(v) >> 8), \
180 PUTBYTE((p), 1, (uint16)(v) >> 0))
181 #define STORE16_L(p, v) \
182 (PUTBYTE((p), 0, (uint16)(v) >> 0), \
183 PUTBYTE((p), 1, (uint16)(v) >> 8))
184 #define STORE16(p, v) STORE16_B((p), (v))
185
186 #define LOAD24_B(p) \
187 (((uint24)GETBYTE((p), 0) << 16) | \
188 ((uint24)GETBYTE((p), 1) << 8) | \
189 ((uint24)GETBYTE((p), 2) << 0))
190 #define LOAD24_L(p) \
191 (((uint24)GETBYTE((p), 0) << 0) | \
192 ((uint24)GETBYTE((p), 1) << 8) | \
193 ((uint24)GETBYTE((p), 2) << 16))
194 #define LOAD24(p) LOAD24_B((p))
195
196 #define STORE24_B(p, v) \
197 (PUTBYTE((p), 0, (uint24)(v) >> 16), \
198 PUTBYTE((p), 1, (uint24)(v) >> 8), \
199 PUTBYTE((p), 2, (uint24)(v) >> 0))
200 #define STORE24_L(p, v) \
201 (PUTBYTE((p), 0, (uint24)(v) >> 0), \
202 PUTBYTE((p), 1, (uint24)(v) >> 8), \
203 PUTBYTE((p), 2, (uint24)(v) >> 16))
204 #define STORE24(p, v) STORE24_B((p), (v))
205
206 #define LOAD32_B(p) \
207 (((uint32)GETBYTE((p), 0) << 24) | \
208 ((uint32)GETBYTE((p), 1) << 16) | \
209 ((uint32)GETBYTE((p), 2) << 8) | \
210 ((uint32)GETBYTE((p), 3) << 0))
211 #define LOAD32_L(p) \
212 (((uint32)GETBYTE((p), 0) << 0) | \
213 ((uint32)GETBYTE((p), 1) << 8) | \
214 ((uint32)GETBYTE((p), 2) << 16) | \
215 ((uint32)GETBYTE((p), 3) << 24))
216 #define LOAD32(p) LOAD32_B((p))
217
218 #define STORE32_B(p, v) \
219 (PUTBYTE((p), 0, (uint32)(v) >> 24), \
220 PUTBYTE((p), 1, (uint32)(v) >> 16), \
221 PUTBYTE((p), 2, (uint32)(v) >> 8), \
222 PUTBYTE((p), 3, (uint32)(v) >> 0))
223 #define STORE32_L(p, v) \
224 (PUTBYTE((p), 0, (uint32)(v) >> 0), \
225 PUTBYTE((p), 1, (uint32)(v) >> 8), \
226 PUTBYTE((p), 2, (uint32)(v) >> 16), \
227 PUTBYTE((p), 3, (uint32)(v) >> 24))
228 #define STORE32(p, v) STORE32_B((p), (v))
229
230 /*----- That's all, folks -------------------------------------------------*/
231
232 #ifdef __cplusplus
233 }
234 #endif
235
236 #endif