chiark / gitweb /
url: Allow `;' to separate key/value pairs in URL-encoded strings.
[mLib] / bits.h
diff --git a/bits.h b/bits.h
index c3499b618935c325eaaf0befbff81dfb91982caa..0f7ba0b97d6a269e96061b12a5a5bdf8961d4f31 100644 (file)
--- a/bits.h
+++ b/bits.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
 /* -*-c-*-
  *
- * $Id: bits.h,v 1.6 2000/07/16 12:28:28 mdw Exp $
+ * $Id$
  *
  * Portable bit-level manipulation macros
  *
  *
  * Portable bit-level manipulation macros
  *
  * MA 02111-1307, USA.
  */
 
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: bits.h,v $
- * Revision 1.6  2000/07/16 12:28:28  mdw
- * Add 64-bit support, with faked arithmetic on 32-bit hosts.
- *
- * Revision 1.5  2000/06/17 10:36:06  mdw
- * Support for 24-bit types.
- *
- * Revision 1.4  1999/12/10 23:42:04  mdw
- * Change header file guard names.
- *
- * Revision 1.3  1999/06/20 23:31:52  mdw
- * More portability enhancements.
- *
- * Revision 1.2  1999/06/17 00:12:46  mdw
- * Improve portability for shift and rotate macros.
- *
- * Revision 1.1  1999/06/01 09:46:19  mdw
- * New addition: bit manipulation macros.
- *
- */
-
 #ifndef MLIB_BITS_H
 #define MLIB_BITS_H
 
 #ifndef MLIB_BITS_H
 #define MLIB_BITS_H
 
 
 /*----- Decide on some types ----------------------------------------------*/
 
 
 /*----- Decide on some types ----------------------------------------------*/
 
+/* --- Make GNU C shut up --- */
+
+#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 91)
+#  define MLIB_BITS_EXTENSION __extension__
+#else
+#  define MLIB_BITS_EXTENSION
+#endif
+
 /* --- Decide on a 32-bit type --- *
  *
  * I want a type which is capable of expressing 32-bit numbers.  Because some
 /* --- Decide on a 32-bit type --- *
  *
  * I want a type which is capable of expressing 32-bit numbers.  Because some
 #endif
 
 #if UINT_MAX >> 31 > 0xffffffff
 #endif
 
 #if UINT_MAX >> 31 > 0xffffffff
-# define HAVE_UINT64
-  typedef unsigned int uint64;
+#  define HAVE_UINT64
+   typedef unsigned int uint64;
 #elif ULONG_MAX >> 31 > 0xffffffff
 #elif ULONG_MAX >> 31 > 0xffffffff
-# define HAVE_UINT64
-  typedef unsigned long uint64;
+#  define HAVE_UINT64
+   typedef unsigned long uint64;
 #elif defined(ULLONG_MAX)
 #elif defined(ULLONG_MAX)
-# define HAVE_UINT64
-  typedef unsigned long long uint64;
+#  define HAVE_UINT64
+   MLIB_BITS_EXTENSION typedef unsigned long long uint64;
 #endif
 
 #ifdef DEBUG64
 #endif
 
 #ifdef DEBUG64
  */
 
 typedef unsigned short uint16;
  */
 
 typedef unsigned short uint16;
-typedef unsigned char octet;
+typedef unsigned char octet, uint8;
 
 /* --- WARNING! --- *
  *
 
 /* --- WARNING! --- *
  *
@@ -141,11 +126,76 @@ typedef unsigned char octet;
 
 #define MASK8 0xffu
 #define MASK16 0xffffu
 
 #define MASK8 0xffu
 #define MASK16 0xffffu
+#define MASK16_L MASK16
+#define MASK16_B MASK16
 #define MASK24 0xffffffu
 #define MASK24 0xffffffu
+#define MASK24_L MASK24
+#define MASK24_B MASK24
 #define MASK32 0xffffffffu
 #define MASK32 0xffffffffu
+#define MASK32_L MASK32
+#define MASK32_B MASK32
+
+#ifdef HAVE_UINT64
+#  define MASK64 MLIB_BITS_EXTENSION 0xffffffffffffffffu
+#  define MASK64_L MASK64
+#  define MASK64_B MASK64
+#endif
+
+/* --- Sizes --- */
+
+#define SZ_8 1
+#define SZ_16 2
+#define SZ_16_L 2
+#define SZ_16_B 2
+#define SZ_24 3
+#define SZ_24_L 3
+#define SZ_24_B 3
+#define SZ_32 4
+#define SZ_32_L 4
+#define SZ_32_B 4
 
 #ifdef HAVE_UINT64
 
 #ifdef HAVE_UINT64
-#  define MASK64 0xffffffffffffffffull
+#  define SZ_64 8
+#  define SZ_64_L 8
+#  define SZ_64_B 8
+#endif
+
+/* --- Type aliases --- */
+
+#define TY_U8 octet
+#define TY_U16 uint16
+#define TY_U16_L uint16
+#define TY_U16_B uint16
+#define TY_U24 uint24
+#define TY_U24_L uint24
+#define TY_U24_B uint24
+#define TY_U32 uint32
+#define TY_U32_L uint32
+#define TY_U32_B uint32
+
+#ifdef HAVE_UINT64
+#  define TY_U64 uint64
+#  define TY_U64_L uint64
+#  define TY_U64_B uint64
+#endif
+
+/* --- List macros --- */
+
+#ifdef HAVE_UINT64
+#  define  DOUINTCONV(_)                                               \
+     _(8, 8, 8)                                                                \
+     _(16, 16, 16) _(16, 16_L, 16l) _(16, 16_B, 16b)                   \
+     _(24, 24, 24) _(24, 24_L, 24l) _(24, 24_B, 24b)                   \
+     _(32, 32, 32) _(32, 32_L, 32l) _(32, 32_B, 32b)                   \
+     _(64, 64, 64) _(64, 64_L, 64l) _(64, 64_B, 64b)
+#  define DOUINTSZ(_) _(8) _(16) _(24) _(32) _(64)
+#else
+#  define  DOUINTCONV(_)                                               \
+     _(8, 8, 8)                                                                \
+     _(16, 16, 16) _(16, 16_L, 16l) _(16, 16_B, 16b)                   \
+     _(24, 24, 24) _(24, 24_L, 24l) _(24, 24_B, 24b)                   \
+     _(32, 32, 32) _(32, 32_L, 32l) _(32, 32_B, 32b)
+#  define DOUINTSZ(_) _(8) _(16) _(24) _(32)
 #endif
 
 /* --- Type coercions --- */
 #endif
 
 /* --- Type coercions --- */
@@ -232,12 +282,15 @@ typedef unsigned char octet;
      unsigned _s = (s) & 63u;                                          \
      uint32 _l = (v).lo, _h = (v).hi;                                  \
      kludge64 *_d = &(d);                                              \
      unsigned _s = (s) & 63u;                                          \
      uint32 _l = (v).lo, _h = (v).hi;                                  \
      kludge64 *_d = &(d);                                              \
-     if (_s >= 32) {                                                   \
+     if (_s > 32) {                                                    \
        _d->hi = LSL32(_l, _s - 32u) | LSR32(_h, 64u - _s);             \
        _d->lo = LSL32(_h, _s - 32u) | LSR32(_l, 64u - _s);             \
      } else if (!_s) {                                                 \
        _d->lo = _l;                                                    \
        _d->hi = _h;                                                    \
        _d->hi = LSL32(_l, _s - 32u) | LSR32(_h, 64u - _s);             \
        _d->lo = LSL32(_h, _s - 32u) | LSR32(_l, 64u - _s);             \
      } else if (!_s) {                                                 \
        _d->lo = _l;                                                    \
        _d->hi = _h;                                                    \
+     } else if (_s == 32) {                                            \
+       _d->lo = _h;                                                    \
+       _d->hi = _l;                                                    \
      } else {                                                          \
        _d->hi = LSL32(_h, _s) | LSR32(_l, 32u - _s);                   \
        _d->lo = LSL32(_l, _s) | LSR32(_h, 32u - _s);                   \
      } else {                                                          \
        _d->hi = LSL32(_h, _s) | LSR32(_l, 32u - _s);                   \
        _d->lo = LSL32(_l, _s) | LSR32(_h, 32u - _s);                   \
@@ -247,12 +300,15 @@ typedef unsigned char octet;
      unsigned _s = (s) & 63u;                                          \
      uint32 _l = (v).lo, _h = (v).hi;                                  \
      kludge64 *_d = &(d);                                              \
      unsigned _s = (s) & 63u;                                          \
      uint32 _l = (v).lo, _h = (v).hi;                                  \
      kludge64 *_d = &(d);                                              \
-     if (_s >= 32) {                                                   \
+     if (_s > 32) {                                                    \
        _d->hi = LSR32(_l, _s - 32u) | LSL32(_h, 64u - _s);             \
        _d->lo = LSR32(_h, _s - 32u) | LSL32(_l, 64u - _s);             \
      } else if (!_s) {                                                 \
        _d->lo = _l;                                                    \
        _d->hi = _h;                                                    \
        _d->hi = LSR32(_l, _s - 32u) | LSL32(_h, 64u - _s);             \
        _d->lo = LSR32(_h, _s - 32u) | LSL32(_l, 64u - _s);             \
      } else if (!_s) {                                                 \
        _d->lo = _l;                                                    \
        _d->hi = _h;                                                    \
+     } else if (_s == 32) {                                            \
+       _d->lo = _h;                                                    \
+       _d->hi = _l;                                                    \
      } else {                                                          \
        _d->hi = LSR32(_h, _s) | LSL32(_l, 32u - _s);                   \
        _d->lo = LSR32(_l, _s) | LSL32(_h, 32u - _s);                   \
      } else {                                                          \
        _d->hi = LSR32(_h, _s) | LSL32(_l, 32u - _s);                   \
        _d->lo = LSR32(_l, _s) | LSL32(_h, 32u - _s);                   \
@@ -403,12 +459,14 @@ typedef unsigned char octet;
 #  define ASSIGN64(d, x) ((d).i = U64((x)))
 #  define HI64(x) U32((x).i >> 32)
 #  define LO64(x) U32((x).i)
 #  define ASSIGN64(d, x) ((d).i = U64((x)))
 #  define HI64(x) U32((x).i >> 32)
 #  define LO64(x) U32((x).i)
+#  define GET64(t, x) ((t)(x).i)
 #else
 #  define SET64(d, h, l) ((d).hi = U32(h), (d).lo = U32(l))
 #  define ASSIGN64(d, x)                                               \
     ((d).hi = ((x & ~MASK32) >> 16) >> 16, (d).lo = U32(x))
 #  define HI64(x) U32((x).hi)
 #  define LO64(x) U32((x).lo)
 #else
 #  define SET64(d, h, l) ((d).hi = U32(h), (d).lo = U32(l))
 #  define ASSIGN64(d, x)                                               \
     ((d).hi = ((x & ~MASK32) >> 16) >> 16, (d).lo = U32(x))
 #  define HI64(x) U32((x).hi)
 #  define LO64(x) U32((x).lo)
+#  define GET64(t, x) (((((t)HI64(x) << 16) << 16) & ~MASK32) | (t)LO64(x))
 #endif
 
 #ifdef HAVE_UINT64
 #endif
 
 #ifdef HAVE_UINT64
@@ -440,6 +498,14 @@ typedef unsigned char octet;
 #  define ZERO64(x) ((x).lo == 0 && (x).hi == 0)
 #endif
 
 #  define ZERO64(x) ((x).lo == 0 && (x).hi == 0)
 #endif
 
+/* --- Storing integers in tables --- */
+
+#ifdef HAVE_UINT64
+#  define X64(x, y) { 0x##x##y }
+#else
+#  define X64(x, y) { 0x##x, 0x##y }
+#endif
+    
 /*----- That's all, folks -------------------------------------------------*/
 
 #ifdef __cplusplus
 /*----- That's all, folks -------------------------------------------------*/
 
 #ifdef __cplusplus