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