chiark / gitweb /
Change header file guard names.
[mLib] / bits.h
CommitLineData
6b033bc4 1/* -*-c-*-
2 *
c6e0eaf0 3 * $Id: bits.h,v 1.4 1999/12/10 23:42:04 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 $
c6e0eaf0 33 * Revision 1.4 1999/12/10 23:42:04 mdw
34 * Change header file guard names.
35 *
0d4e268a 36 * Revision 1.3 1999/06/20 23:31:52 mdw
37 * More portability enhancements.
38 *
bc9f60b6 39 * Revision 1.2 1999/06/17 00:12:46 mdw
40 * Improve portability for shift and rotate macros.
41 *
6b033bc4 42 * Revision 1.1 1999/06/01 09:46:19 mdw
43 * New addition: bit manipulation macros.
44 *
45 */
46
c6e0eaf0 47#ifndef MLIB_BITS_H
48#define MLIB_BITS_H
6b033bc4 49
50#ifdef __cplusplus
51 extern "C" {
52#endif
53
54/*----- Header files ------------------------------------------------------*/
55
56#include <limits.h>
57#include <stddef.h>
58
59/*----- Decide on some types ----------------------------------------------*/
60
61/* --- Decide on a 32-bit type --- *
62 *
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.
67 */
68
69#if UINT_MAX >= 0xffffffffu
70 typedef unsigned int uint32;
71#else
72 typedef unsigned long uint32;
73#endif
74
75/* --- Decide on 16-bit and 8-bit types --- *
76 *
77 * This is more for brevity than anything else.
78 */
79
80typedef unsigned short uint16;
81typedef unsigned char octet;
82
83/* --- WARNING! --- *
84 *
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.
87 */
88
89/*----- Macros ------------------------------------------------------------*/
90
91/* --- Useful masks --- */
92
93#define MASK8 0xffu
94#define MASK16 0xffffu
95#define MASK32 0xffffffffu
96
97/* --- Type coercions --- */
98
99#define U8(x) ((octet)((x) & MASK8))
100#define U16(x) ((uint16)((x) & MASK16))
101#define U32(x) ((uint32)((x) & MASK32))
102
103/* --- Safe shifting macros --- */
104
bc9f60b6 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))
6b033bc4 111
112/* --- Rotation macros --- */
113
bc9f60b6 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))))
6b033bc4 120
121/* --- Storage and retrieval --- */
122
123#define GETBYTE(p, o) (((octet *)(p))[o] & MASK8)
124#define PUTBYTE(p, o, v) (((octet *)(p))[o] = U8((v)))
125
126#define LOAD8(p) (GETBYTE((p), 0))
127#define STORE8(p, v) (PUTBYTE((p), 0, (v)))
128
0d4e268a 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))
6b033bc4 135#define LOAD16(p) LOAD16_B((p))
136
0d4e268a 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))
6b033bc4 143#define STORE16(p, v) STORE16_B((p), (v))
144
145#define LOAD32_B(p) \
0d4e268a 146 (((uint32)GETBYTE((p), 0) << 24) | \
147 ((uint32)GETBYTE((p), 1) << 16) | \
148 ((uint32)GETBYTE((p), 2) << 8) | \
149 ((uint32)GETBYTE((p), 3) << 0))
6b033bc4 150#define LOAD32_L(p) \
0d4e268a 151 (((uint32)GETBYTE((p), 0) << 0) | \
152 ((uint32)GETBYTE((p), 1) << 8) | \
153 ((uint32)GETBYTE((p), 2) << 16) | \
154 ((uint32)GETBYTE((p), 3) << 24))
6b033bc4 155#define LOAD32(p) LOAD32_B((p))
156
157#define STORE32_B(p, v) \
0d4e268a 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))
6b033bc4 162#define STORE32_L(p, v) \
0d4e268a 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))
6b033bc4 167#define STORE32(p, v) STORE32_B((p), (v))
168
169/*----- That's all, folks -------------------------------------------------*/
170
171#ifdef __cplusplus
172 }
173#endif
174
175#endif