3 * $Id: bits.h,v 1.4 1999/12/10 23:42:04 mdw Exp $
5 * Portable bit-level manipulation macros
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the mLib utilities library.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.4 1999/12/10 23:42:04 mdw
34 * Change header file guard names.
36 * Revision 1.3 1999/06/20 23:31:52 mdw
37 * More portability enhancements.
39 * Revision 1.2 1999/06/17 00:12:46 mdw
40 * Improve portability for shift and rotate macros.
42 * Revision 1.1 1999/06/01 09:46:19 mdw
43 * New addition: bit manipulation macros.
54 /*----- Header files ------------------------------------------------------*/
59 /*----- Decide on some types ----------------------------------------------*/
61 /* --- Decide on a 32-bit type --- *
63 * I want a type which is capable of expressing 32-bit numbers. Because some
64 * implementations have 64-bit @long@s (infinitely preferable to the abortion
65 * that is @long long@), using @unsigned long@ regardless is wasteful. So,
66 * if @int@ appears to be good enough, then I'll go with that.
69 #if UINT_MAX >= 0xffffffffu
70 typedef unsigned int uint32;
72 typedef unsigned long uint32;
75 /* --- Decide on 16-bit and 8-bit types --- *
77 * This is more for brevity than anything else.
80 typedef unsigned short uint16;
81 typedef unsigned char octet;
85 * Never lose sight of the fact that the above types may be wider than the
86 * names suggest. Some architectures have 32-bit @short@s for example.
89 /*----- Macros ------------------------------------------------------------*/
91 /* --- Useful masks --- */
94 #define MASK16 0xffffu
95 #define MASK32 0xffffffffu
97 /* --- Type coercions --- */
99 #define U8(x) ((octet)((x) & MASK8))
100 #define U16(x) ((uint16)((x) & MASK16))
101 #define U32(x) ((uint32)((x) & MASK32))
103 /* --- Safe shifting macros --- */
105 #define LSL8(v, s) U8(U8(v) << ((s) & 7u))
106 #define LSR8(v, s) U8(U8(v) >> ((s) & 7u))
107 #define LSL16(v, s) U16(U16(v) << ((s) & 15u))
108 #define LSR16(v, s) U16(U16(v) >> ((s) & 15u))
109 #define LSL32(v, s) U32(U32(v) << ((s) & 31u))
110 #define LSR32(v, s) U32(U32(v) >> ((s) & 31u))
112 /* --- Rotation macros --- */
114 #define ROL8(v, s) (LSL8((v), (s)) | (LSR8((v), 8u - (s))))
115 #define ROR8(v, s) (LSR8((v), (s)) | (LSL8((v), 8u - (s))))
116 #define ROL16(v, s) (LSL16((v), (s)) | (LSR16((v), 16u - (s))))
117 #define ROR16(v, s) (LSR16((v), (s)) | (LSL16((v), 16u - (s))))
118 #define ROL32(v, s) (LSL32((v), (s)) | (LSR32((v), 32u - (s))))
119 #define ROR32(v, s) (LSR32((v), (s)) | (LSL32((v), 32u - (s))))
121 /* --- Storage and retrieval --- */
123 #define GETBYTE(p, o) (((octet *)(p))[o] & MASK8)
124 #define PUTBYTE(p, o, v) (((octet *)(p))[o] = U8((v)))
126 #define LOAD8(p) (GETBYTE((p), 0))
127 #define STORE8(p, v) (PUTBYTE((p), 0, (v)))
129 #define LOAD16_B(p) \
130 (((uint16)GETBYTE((p), 0) << 8) | \
131 ((uint16)GETBYTE((p), 1) << 0))
132 #define LOAD16_L(p) \
133 (((uint16)GETBYTE((p), 0) << 0) | \
134 ((uint16)GETBYTE((p), 1) << 8))
135 #define LOAD16(p) LOAD16_B((p))
137 #define STORE16_B(p, v) \
138 (PUTBYTE((p), 0, (uint16)(v) >> 8), \
139 PUTBYTE((p), 1, (uint16)(v) >> 0))
140 #define STORE16_L(p, v) \
141 (PUTBYTE((p), 0, (uint16)(v) >> 0), \
142 PUTBYTE((p), 1, (uint16)(v) >> 8))
143 #define STORE16(p, v) STORE16_B((p), (v))
145 #define LOAD32_B(p) \
146 (((uint32)GETBYTE((p), 0) << 24) | \
147 ((uint32)GETBYTE((p), 1) << 16) | \
148 ((uint32)GETBYTE((p), 2) << 8) | \
149 ((uint32)GETBYTE((p), 3) << 0))
150 #define LOAD32_L(p) \
151 (((uint32)GETBYTE((p), 0) << 0) | \
152 ((uint32)GETBYTE((p), 1) << 8) | \
153 ((uint32)GETBYTE((p), 2) << 16) | \
154 ((uint32)GETBYTE((p), 3) << 24))
155 #define LOAD32(p) LOAD32_B((p))
157 #define STORE32_B(p, v) \
158 (PUTBYTE((p), 0, (uint32)(v) >> 24), \
159 PUTBYTE((p), 1, (uint32)(v) >> 16), \
160 PUTBYTE((p), 2, (uint32)(v) >> 8), \
161 PUTBYTE((p), 3, (uint32)(v) >> 0))
162 #define STORE32_L(p, v) \
163 (PUTBYTE((p), 0, (uint32)(v) >> 0), \
164 PUTBYTE((p), 1, (uint32)(v) >> 8), \
165 PUTBYTE((p), 2, (uint32)(v) >> 16), \
166 PUTBYTE((p), 3, (uint32)(v) >> 24))
167 #define STORE32(p, v) STORE32_B((p), (v))
169 /*----- That's all, folks -------------------------------------------------*/