chiark / gitweb /
New addition: bit manipulation macros.
authormdw <mdw>
Tue, 1 Jun 1999 09:46:19 +0000 (09:46 +0000)
committermdw <mdw>
Tue, 1 Jun 1999 09:46:19 +0000 (09:46 +0000)
bits.h [new file with mode: 0644]

diff --git a/bits.h b/bits.h
new file mode 100644 (file)
index 0000000..9d7c6d0
--- /dev/null
+++ b/bits.h
@@ -0,0 +1,150 @@
+/* -*-c-*-
+ *
+ * $Id: bits.h,v 1.1 1999/06/01 09:46:19 mdw Exp $
+ *
+ * Portable bit-level manipulation macros
+ *
+ * (c) 1998 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of the mLib utilities library.
+ *
+ * mLib is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * mLib is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Library General Public
+ * License along with mLib; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: bits.h,v $
+ * Revision 1.1  1999/06/01 09:46:19  mdw
+ * New addition: bit manipulation macros.
+ *
+ */
+
+#ifndef BITS_H
+#define BITS_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <limits.h>
+#include <stddef.h>
+
+/*----- Decide on some types ----------------------------------------------*/
+
+/* --- Decide on a 32-bit type --- *
+ *
+ * I want a type which is capable of expressing 32-bit numbers.  Because some
+ * implementations have 64-bit @long@s (infinitely preferable to the abortion
+ * that is @long long@), using @unsigned long@ regardless is wasteful.  So,
+ * if @int@ appears to be good enough, then I'll go with that.
+ */
+
+#if UINT_MAX >= 0xffffffffu
+  typedef unsigned int uint32;
+#else
+  typedef unsigned long uint32;
+#endif
+
+/* --- Decide on 16-bit and 8-bit types --- *
+ *
+ * This is more for brevity than anything else.
+ */
+
+typedef unsigned short uint16;
+typedef unsigned char octet;
+
+/* --- WARNING! --- *
+ *
+ * Never lose sight of the fact that the above types may be wider than the
+ * names suggest.  Some architectures have 32-bit @short@s for example.
+ */
+
+/*----- Macros ------------------------------------------------------------*/
+
+/* --- Useful masks --- */
+
+#define MASK8 0xffu
+#define MASK16 0xffffu
+#define MASK32 0xffffffffu
+
+/* --- Type coercions --- */
+
+#define U8(x) ((octet)((x) & MASK8))
+#define U16(x) ((uint16)((x) & MASK16))
+#define U32(x) ((uint32)((x) & MASK32))
+
+/* --- Safe shifting macros --- */
+
+#define LSL8(v, s) (U8(v) << ((s) & 7u))
+#define LSR8(v, s) (U8(v) >> ((s) & 7u))
+#define LSL16(v, s) (U16(v) << ((s) & 15u))
+#define LSR16(v, s) (U16(v) >> ((s) & 15u))
+#define LSL32(v, s) (U32(v) << ((s) & 31u))
+#define LSR32(v, s) (U32(v) >> ((s) & 31u))
+
+/* --- Rotation macros --- */
+
+#define ROL8(v, s) (LSL8((v), (s)) | (LSR8((v), 8 - (s))))
+#define ROR8(v, s) (LSR8((v), (s)) | (LSL8((v), 8 - (s))))
+#define ROL16(v, s) (LSL16((v), (s)) | (LSR16((v), 16 - (s))))
+#define ROR16(v, s) (LSR16((v), (s)) | (LSL16((v), 16 - (s))))
+#define ROL32(v, s) (LSL32((v), (s)) | (LSR32((v), 32 - (s))))
+#define ROR32(v, s) (LSR32((v), (s)) | (LSL32((v), 32 - (s))))
+
+/* --- Storage and retrieval --- */
+
+#define GETBYTE(p, o) (((octet *)(p))[o] & MASK8)
+#define PUTBYTE(p, o, v) (((octet *)(p))[o] = U8((v)))
+
+#define LOAD8(p) (GETBYTE((p), 0))
+#define STORE8(p, v) (PUTBYTE((p), 0, (v)))
+
+#define LOAD16_B(p) ((GETBYTE((p), 0) << 8) | GETBYTE((p), 1))
+#define LOAD16_L(p) (GETBYTE((p), 0) | (GETBYTE((p), 1) << 8))
+#define LOAD16(p) LOAD16_B((p))
+
+#define STORE16_B(p, v) (PUTBYTE((p), 0, (v) >> 8), PUTBYTE((p), 1, (v)))
+#define STORE16_L(p, v) (PUTBYTE((p), 0, (v)), PUTBYTE((p), 1, (v) >> 8))
+#define STORE16(p, v) STORE16_B((p), (v))
+
+#define LOAD32_B(p)                                                    \
+  ((GETBYTE((p), 0) << 24) | (GETBYTE((p), 1) << 16) |                 \
+   (GETBYTE((p), 2) <<  8) | (GETBYTE((p), 3) <<  0))
+#define LOAD32_L(p)                                                    \
+  ((GETBYTE((p), 0) <<  0) | (GETBYTE((p), 1) <<  8) |                 \
+   (GETBYTE((p), 2) << 16) | (GETBYTE((p), 3) << 24))
+#define LOAD32(p) LOAD32_B((p))
+
+#define STORE32_B(p, v)                                                        \
+  (PUTBYTE((p), 0, (v) >> 24), PUTBYTE((p), 1, (v) >> 16),             \
+   PUTBYTE((p), 2, (v) >>  8), PUTBYTE((p), 3, (v) >>  0))
+#define STORE32_L(p, v)                                                        \
+  (PUTBYTE((p), 0, (v) >>  0), PUTBYTE((p), 1, (v) >>  8),             \
+   PUTBYTE((p), 2, (v) >> 16), PUTBYTE((p), 3, (v) >> 24))
+#define STORE32(p, v) STORE32_B((p), (v))
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif