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