/* -*-c-*-
*
- * $Id: mpscan.h,v 1.2 1999/11/13 01:55:10 mdw Exp $
+ * $Id: mpscan.h,v 1.4 2000/07/29 17:03:31 mdw Exp $
*
* Sequential bit scan of multiprecision integers
*
/*----- Revision history --------------------------------------------------*
*
* $Log: mpscan.h,v $
+ * Revision 1.4 2000/07/29 17:03:31 mdw
+ * Add support for left-to-right bitscanning, for use in modular
+ * exponentiation.
+ *
+ * Revision 1.3 1999/12/10 23:29:48 mdw
+ * Change header file guard names.
+ *
* Revision 1.2 1999/11/13 01:55:10 mdw
* Fixed so that they compile. Minor interface changes.
*
*
*/
-#ifndef MPSCAN_H
-#define MPSCAN_H
+#ifndef CATACOMB_MPSCAN_H
+#define CATACOMB_MPSCAN_H
#ifdef __cplusplus
extern "C" {
/*----- Header files ------------------------------------------------------*/
-#ifndef MPW_H
+#ifndef CATACOMB_MPW_H
# include "mpw.h"
#endif
int bits; /* Number of bits left in @w@ */
} mpscan;
-/*----- Functions provided ------------------------------------------------*/
+/*----- Right-to-left scanning --------------------------------------------*/
/* --- @mpscan_initx@ --- *
*
*/
#define MPSCAN_STEP(m) \
- ((m)->bits ? ((m)->w >>= 1, \
- (m)->bits--, 1) : \
+ ((m)->bits ? ((m)->w >>= 1, (m)->bits--, 1) : \
(m)->v < (m)->vl ? ((m)->w = *(m)->v++, \
(m)->bits = MPW_BITS - 1, 1) : \
0)
extern int mpscan_bit(const mpscan */*m*/);
+/*----- Left-to right-scanning --------------------------------------------*/
+
+/* --- @mpscan_rinitx@ --- *
+ *
+ * Arguments: @mpscan *m@ = pointer to bitscanner structure
+ * @const mpw *v, *vl@ = vector of words to scan
+ *
+ * Returns: ---
+ *
+ * Use: Initializes a reverse bitscanner from a low-level
+ * vector-and-length representation of an integer. Initially no
+ * bit is ready; you must call @mpscan_rstep@ before anything
+ * useful will come out.
+ */
+
+#define MPSCAN_RINITX(m_, v_, vl_) do { \
+ mpscan *_m = (m_); \
+ _m->v = (v_); \
+ _m->vl = (vl_); \
+ while (_m->vl > _m->v && !_m->vl[-1]) \
+ _m->vl--; \
+ _m->bits = 0; \
+} while (0)
+
+extern void mpscan_rinitx(mpscan */*m*/,
+ const mpw */*v*/, const mpw */*vl*/);
+
+/* --- @mpscan_rstep@ --- *
+ *
+ * Arguments: @mpscan *m@ = pointer to bitscanner
+ *
+ * Returns: Nonzero if there is another bit to read.
+ *
+ * Use: Steps on to the next bit in the integer. The macro version
+ * evaluates its argument multiple times.
+ */
+
+#define MPSCAN_RSTEP(m) \
+ ((m)->bits ? ((m)->w <<= 1, (m)->bits--, 1) : \
+ (m)->vl > (m)->v ? ((m)->w = *--(m)->vl, \
+ (m)->bits = MPW_BITS - 1, 1) : \
+ 0)
+
+extern int mpscan_rstep(mpscan */*m*/);
+
+/* --- @mpscan_rbit@ --- *
+ *
+ * Arguments: @const mpscan *m@ = pointer to bitscanner
+ *
+ * Returns: The value of the current bit.
+ *
+ * Use: Reads the value of the current bit looked at by a
+ * reverse bitscanner.
+ */
+
+#define MPSCAN_RBIT(m) (((m)->w >> (MPW_BITS - 1)) & 1)
+
+extern int mpscan_rbit(const mpscan */*m*/);
+
/*----- That's all, folks -------------------------------------------------*/
#ifdef __cplusplus