3 * $Id: bits.h,v 1.3 1999/06/20 23:31:52 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.3 1999/06/20 23:31:52 mdw
34 * More portability enhancements.
36 * Revision 1.2 1999/06/17 00:12:46 mdw
37 * Improve portability for shift and rotate macros.
39 * Revision 1.1 1999/06/01 09:46:19 mdw
40 * New addition: bit manipulation macros.
51 /*----- Header files ------------------------------------------------------*/
56 /*----- Decide on some types ----------------------------------------------*/
58 /* --- Decide on a 32-bit type --- *
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.
66 #if UINT_MAX >= 0xffffffffu
67 typedef unsigned int uint32
;
69 typedef unsigned long uint32
;
72 /* --- Decide on 16-bit and 8-bit types --- *
74 * This is more for brevity than anything else.
77 typedef unsigned short uint16
;
78 typedef unsigned char octet
;
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.
86 /*----- Macros ------------------------------------------------------------*/
88 /* --- Useful masks --- */
91 #define MASK16 0xffffu
92 #define MASK32 0xffffffffu
94 /* --- Type coercions --- */
96 #define U8(x) ((octet)((x) & MASK8))
97 #define U16(x) ((uint16)((x) & MASK16))
98 #define U32(x) ((uint32)((x) & MASK32))
100 /* --- Safe shifting macros --- */
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))
109 /* --- Rotation macros --- */
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))))
118 /* --- Storage and retrieval --- */
120 #define GETBYTE(p, o) (((octet *)(p))[o] & MASK8)
121 #define PUTBYTE(p, o, v) (((octet *)(p))[o] = U8((v)))
123 #define LOAD8(p) (GETBYTE((p), 0))
124 #define STORE8(p, v) (PUTBYTE((p), 0, (v)))
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))
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))
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))
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))
166 /*----- That's all, folks -------------------------------------------------*/