chiark / gitweb /
Add a slew of manual pages.
[mLib] / bits.h
... / ...
CommitLineData
1/* -*-c-*-
2 *
3 * $Id: bits.h,v 1.2 1999/06/17 00:12:46 mdw Exp $
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 $
33 * Revision 1.2 1999/06/17 00:12:46 mdw
34 * Improve portability for shift and rotate macros.
35 *
36 * Revision 1.1 1999/06/01 09:46:19 mdw
37 * New addition: bit manipulation macros.
38 *
39 */
40
41#ifndef BITS_H
42#define BITS_H
43
44#ifdef __cplusplus
45 extern "C" {
46#endif
47
48/*----- Header files ------------------------------------------------------*/
49
50#include <limits.h>
51#include <stddef.h>
52
53/*----- Decide on some types ----------------------------------------------*/
54
55/* --- Decide on a 32-bit type --- *
56 *
57 * I want a type which is capable of expressing 32-bit numbers. Because some
58 * implementations have 64-bit @long@s (infinitely preferable to the abortion
59 * that is @long long@), using @unsigned long@ regardless is wasteful. So,
60 * if @int@ appears to be good enough, then I'll go with that.
61 */
62
63#if UINT_MAX >= 0xffffffffu
64 typedef unsigned int uint32;
65#else
66 typedef unsigned long uint32;
67#endif
68
69/* --- Decide on 16-bit and 8-bit types --- *
70 *
71 * This is more for brevity than anything else.
72 */
73
74typedef unsigned short uint16;
75typedef unsigned char octet;
76
77/* --- WARNING! --- *
78 *
79 * Never lose sight of the fact that the above types may be wider than the
80 * names suggest. Some architectures have 32-bit @short@s for example.
81 */
82
83/*----- Macros ------------------------------------------------------------*/
84
85/* --- Useful masks --- */
86
87#define MASK8 0xffu
88#define MASK16 0xffffu
89#define MASK32 0xffffffffu
90
91/* --- Type coercions --- */
92
93#define U8(x) ((octet)((x) & MASK8))
94#define U16(x) ((uint16)((x) & MASK16))
95#define U32(x) ((uint32)((x) & MASK32))
96
97/* --- Safe shifting macros --- */
98
99#define LSL8(v, s) U8(U8(v) << ((s) & 7u))
100#define LSR8(v, s) U8(U8(v) >> ((s) & 7u))
101#define LSL16(v, s) U16(U16(v) << ((s) & 15u))
102#define LSR16(v, s) U16(U16(v) >> ((s) & 15u))
103#define LSL32(v, s) U32(U32(v) << ((s) & 31u))
104#define LSR32(v, s) U32(U32(v) >> ((s) & 31u))
105
106/* --- Rotation macros --- */
107
108#define ROL8(v, s) (LSL8((v), (s)) | (LSR8((v), 8u - (s))))
109#define ROR8(v, s) (LSR8((v), (s)) | (LSL8((v), 8u - (s))))
110#define ROL16(v, s) (LSL16((v), (s)) | (LSR16((v), 16u - (s))))
111#define ROR16(v, s) (LSR16((v), (s)) | (LSL16((v), 16u - (s))))
112#define ROL32(v, s) (LSL32((v), (s)) | (LSR32((v), 32u - (s))))
113#define ROR32(v, s) (LSR32((v), (s)) | (LSL32((v), 32u - (s))))
114
115/* --- Storage and retrieval --- */
116
117#define GETBYTE(p, o) (((octet *)(p))[o] & MASK8)
118#define PUTBYTE(p, o, v) (((octet *)(p))[o] = U8((v)))
119
120#define LOAD8(p) (GETBYTE((p), 0))
121#define STORE8(p, v) (PUTBYTE((p), 0, (v)))
122
123#define LOAD16_B(p) ((GETBYTE((p), 0) << 8) | GETBYTE((p), 1))
124#define LOAD16_L(p) (GETBYTE((p), 0) | (GETBYTE((p), 1) << 8))
125#define LOAD16(p) LOAD16_B((p))
126
127#define STORE16_B(p, v) (PUTBYTE((p), 0, (v) >> 8), PUTBYTE((p), 1, (v)))
128#define STORE16_L(p, v) (PUTBYTE((p), 0, (v)), PUTBYTE((p), 1, (v) >> 8))
129#define STORE16(p, v) STORE16_B((p), (v))
130
131#define LOAD32_B(p) \
132 ((GETBYTE((p), 0) << 24) | (GETBYTE((p), 1) << 16) | \
133 (GETBYTE((p), 2) << 8) | (GETBYTE((p), 3) << 0))
134#define LOAD32_L(p) \
135 ((GETBYTE((p), 0) << 0) | (GETBYTE((p), 1) << 8) | \
136 (GETBYTE((p), 2) << 16) | (GETBYTE((p), 3) << 24))
137#define LOAD32(p) LOAD32_B((p))
138
139#define STORE32_B(p, v) \
140 (PUTBYTE((p), 0, (v) >> 24), PUTBYTE((p), 1, (v) >> 16), \
141 PUTBYTE((p), 2, (v) >> 8), PUTBYTE((p), 3, (v) >> 0))
142#define STORE32_L(p, v) \
143 (PUTBYTE((p), 0, (v) >> 0), PUTBYTE((p), 1, (v) >> 8), \
144 PUTBYTE((p), 2, (v) >> 16), PUTBYTE((p), 3, (v) >> 24))
145#define STORE32(p, v) STORE32_B((p), (v))
146
147/*----- That's all, folks -------------------------------------------------*/
148
149#ifdef __cplusplus
150 }
151#endif
152
153#endif