General robustification.
[u/mdw/catacomb] / mp.h
diff --git a/mp.h b/mp.h
index c405bca..bfed14a 100644 (file)
--- a/mp.h
+++ b/mp.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mp.h,v 1.13 2002/10/06 22:52:50 mdw Exp $
+ * $Id: mp.h,v 1.18 2004/04/03 03:32:05 mdw Exp $
  *
  * Simple multiprecision arithmetic
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: mp.h,v $
+ * Revision 1.18  2004/04/03 03:32:05  mdw
+ * General robustification.
+ *
+ * Revision 1.17  2003/05/16 09:09:24  mdw
+ * Fix @mp_lsl2c@.  Turns out to be surprisingly tricky.
+ *
+ * Revision 1.16  2002/10/15 22:57:22  mdw
+ * Handy new comparison macros.
+ *
+ * Revision 1.15  2002/10/15 19:18:31  mdw
+ * New operation to negate numbers.
+ *
+ * Revision 1.14  2002/10/15 00:19:40  mdw
+ * Bit setting and clearing functions.
+ *
  * Revision 1.13  2002/10/06 22:52:50  mdw
  * Pile of changes for supporting two's complement properly.
  *
@@ -653,78 +668,17 @@ extern mp *mp_loadb2c(mp */*d*/, const void */*pv*/, size_t /*sz*/);
 
 extern void mp_storeb2c(const mp */*m*/, void */*pv*/, size_t /*sz*/);
 
-/*----- Simple arithmetic -------------------------------------------------*/
+/*----- Bit operations ----------------------------------------------------*/
 
-/* --- @mp_lsl@, @mp_lsr@ --- *
- *
- * Arguments:  @mp *d@ = destination
- *             @mp *a@ = source
- *             @size_t n@ = number of bits to move
- *
- * Returns:    Result, @a@ shifted left or right by @n@.
- */
-
-extern mp *mp_lsl(mp */*d*/, mp */*a*/, size_t /*n*/);
-extern mp *mp_lsr(mp */*d*/, mp */*a*/, size_t /*n*/);
-
-/* --- @mp_lsl2c@, @mp_lsr2c@ --- *
+/* --- @mp_not@ --- *
  *
  * Arguments:  @mp *d@ = destination
  *             @mp *a@ = source
- *             @size_t n@ = number of bits to move
  *
- * Returns:    Result, @a@ shifted left or right by @n@.  Handles the
- *             pretence of sign-extension for negative numbers.
- */
-
-extern mp *mp_lsl2c(mp */*d*/, mp */*a*/, size_t /*n*/);
-extern mp *mp_lsr2c(mp */*d*/, mp */*a*/, size_t /*n*/);
-
-/* --- @mp_testbit@ --- *
- *
- * Arguments:  @mp *x@ = a large integer
- *             @size_t n@ = which bit to test
- *
- * Returns:    Nonzero if the bit is set, zero if not.
- */
-
-extern int mp_testbit(mp */*x*/, size_t /*n*/);
-
-/* --- @mp_testbit2c@ --- *
- *
- * Arguments:  @mp *x@ = a large integer
- *             @size_t n@ = which bit to test
- *
- * Returns:    Nonzero if the bit is set, zero if not.  Fakes up two's
- *             complement representation.
- */
-
-extern int mp_testbit2c(mp */*x*/, size_t /*n*/);
-
-/* --- @mp_eq@ --- *
- *
- * Arguments:  @const mp *a, *b@ = two numbers
- *
- * Returns:    Nonzero if the numbers are equal.
- */
-
-extern int mp_eq(const mp */*a*/, const mp */*b*/);
-
-#define MP_EQ(a, b)                                                    \
-  ((((a)->f ^ (b)->f) & MP_NEG) == 0 &&                                        \
-   mpx_ueq((a)->v, (a)->vl, (b)->v, (b)->vl))
-
-/* --- @mp_cmp@ --- *
- *
- * Arguments:  @const mp *a, *b@ = two numbers
- *
- * Returns:    Less than, equal to or greater than zero, according to
- *             whether @a@ is less than, equal to or greater than @b@.
- */
-
-extern int mp_cmp(const mp */*a*/, const mp */*b*/);
+ * Returns:    The bitwise complement of the source.
+ */ 
 
-#define MP_CMP(a, op, b) (mp_cmp((a), (b)) op 0)
+extern mp *mp_not(mp */*d*/, mp */*a*/);
 
 /* --- @mp_bitop@ --- *
  *
@@ -756,15 +710,55 @@ MPX_DOBIN(MP_BITDECL)
 #define mp_nor  mp_bit1000
 #define mp_xor  mp_bit0110
 
-/* --- @mp_not@ --- *
+/* --- @mp_testbit@ --- *
+ *
+ * Arguments:  @mp *x@ = a large integer
+ *             @unsigned long n@ = which bit to test
+ *
+ * Returns:    Nonzero if the bit is set, zero if not.
+ */
+
+extern int mp_testbit(mp */*x*/, unsigned long /*n*/);
+
+/* --- @mp_setbit@, @mp_clearbit@ --- *
+ *
+ * Arguments:  @mp *d@ = a destination
+ *             @mp *x@ = a large integer
+ *             @unsigned long n@ = which bit to modify
+ *
+ * Returns:    The argument @x@, with the appropriate bit set or cleared.
+ */
+
+extern mp *mp_setbit(mp */*d*/, mp */*x*/, unsigned long /*n*/);
+extern mp *mp_clearbit(mp */*d*/, mp */*x*/, unsigned long /*n*/);
+
+/* --- @mp_lsl@, @mp_lslc@, @mp_lsr@ --- *
  *
  * Arguments:  @mp *d@ = destination
  *             @mp *a@ = source
+ *             @size_t n@ = number of bits to move
  *
- * Returns:    The bitwise complement of the source.
- */ 
+ * Returns:    Result, @a@ shifted left or right by @n@.
+ *
+ * Use:                Bitwise shift operators.  @mp_lslc@ fills the bits introduced
+ *             on the right with ones instead of zeroes: it's used
+ *             internally by @mp_lsl2c@, though it may be useful on its
+ *             own.
+ */
 
-extern mp *mp_not(mp */*d*/, mp */*a*/);
+extern mp *mp_lsl(mp */*d*/, mp */*a*/, size_t /*n*/);
+extern mp *mp_lslc(mp */*d*/, mp */*a*/, size_t /*n*/);
+extern mp *mp_lsr(mp */*d*/, mp */*a*/, size_t /*n*/);
+
+/* --- @mp_not2c@ --- *
+ *
+ * Arguments:  @mp *d@ = destination
+ *             @mp *a@ = source
+ *
+ * Returns:    The sign-extended complement of the argument.
+ */
+
+extern mp *mp_not2c(mp */*d*/, mp */*a*/);
 
 /* --- @mp_bitop2c@ --- *
  *
@@ -796,15 +790,91 @@ MPX_DOBIN(MP_BIT2CDECL)
 #define mp_nor2c  mp_bit10002c
 #define mp_xor2c  mp_bit01102c
 
-/* --- @mp_not2c@ --- *
+/* --- @mp_lsl2c@, @mp_lsr2c@ --- *
  *
  * Arguments:  @mp *d@ = destination
  *             @mp *a@ = source
+ *             @size_t n@ = number of bits to move
  *
- * Returns:    The sign-extended complement of the argument.
+ * Returns:    Result, @a@ shifted left or right by @n@.  Handles the
+ *             pretence of sign-extension for negative numbers.
  */
 
-extern mp *mp_not2c(mp */*d*/, mp */*a*/);
+extern mp *mp_lsl2c(mp */*d*/, mp */*a*/, size_t /*n*/);
+extern mp *mp_lsr2c(mp */*d*/, mp */*a*/, size_t /*n*/);
+
+/* --- @mp_testbit2c@ --- *
+ *
+ * Arguments:  @mp *x@ = a large integer
+ *             @unsigned long n@ = which bit to test
+ *
+ * Returns:    Nonzero if the bit is set, zero if not.  Fakes up two's
+ *             complement representation.
+ */
+
+extern int mp_testbit2c(mp */*x*/, unsigned long /*n*/);
+
+/* --- @mp_setbit2c@, @mp_clearbit2c@ --- *
+ *
+ * Arguments:  @mp *d@ = a destination
+ *             @mp *x@ = a large integer
+ *             @unsigned long n@ = which bit to modify
+ *
+ * Returns:    The argument @x@, with the appropriate bit set or cleared.
+ *             Fakes up two's complement representation.
+ */
+
+extern mp *mp_setbit2c(mp */*d*/, mp */*x*/, unsigned long /*n*/);
+extern mp *mp_clearbit2c(mp */*d*/, mp */*x*/, unsigned long /*n*/);
+
+/*----- Comparisons -------------------------------------------------------*/
+
+/* --- @mp_eq@ --- *
+ *
+ * Arguments:  @const mp *a, *b@ = two numbers
+ *
+ * Returns:    Nonzero if the numbers are equal.
+ */
+
+extern int mp_eq(const mp */*a*/, const mp */*b*/);
+
+#define MP_EQ(a, b)                                                    \
+  ((((a)->f ^ (b)->f) & MP_NEG) == 0 &&                                        \
+   mpx_ueq((a)->v, (a)->vl, (b)->v, (b)->vl))
+
+/* --- @mp_cmp@ --- *
+ *
+ * Arguments:  @const mp *a, *b@ = two numbers
+ *
+ * Returns:    Less than, equal to or greater than zero, according to
+ *             whether @a@ is less than, equal to or greater than @b@.
+ */
+
+extern int mp_cmp(const mp */*a*/, const mp */*b*/);
+
+#define MP_CMP(a, op, b) (mp_cmp((a), (b)) op 0)
+
+/* --- Other handy macros --- */
+
+#define MP_ISNEG(x) ((x)->f & MP_NEG)
+#define MP_ISZERO(x) (!MP_LEN(x))
+#define MP_ISPOS(x) (!MP_ISNEG(x) && !MP_ISZERO(x))
+#define MP_ISODD(x) (!MP_ISZERO(x) && ((x)->v[0] & 1u))
+#define MP_ISEVEN(x) (!MP_ISODD(x))
+
+/*----- Arithmetic operations ---------------------------------------------*/
+
+/* --- @mp_neg@ --- *
+ *
+ * Arguments:  @mp *d@ = destination
+ *             @mp *a@ = argument
+ *
+ * Returns:    The negation of the argument.
+ *
+ * Use:                Negates its argument.
+ */
+
+extern mp *mp_neg(mp */*d*/, mp */*a*/);
 
 /* --- @mp_add@ --- *
  *