3 * Sequential bit scan of multiprecision integers
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 #ifndef CATACOMB_MPSCAN_H
29 #define CATACOMB_MPSCAN_H
35 /*----- Header files ------------------------------------------------------*/
37 #ifndef CATACOMB_MPW_H
41 /*----- Data structures ---------------------------------------------------*/
43 typedef struct mpscan {
44 const mpw *v, *vl; /* Vector of words to scan */
45 mpw w; /* Current word to scan */
46 int bits; /* Number of bits left in @w@ */
49 /*----- Right-to-left scanning --------------------------------------------*/
51 /* --- @mpscan_initx@ --- *
53 * Arguments: @mpscan *m@ = pointer to bitscanner structure
54 * @const mpw *v, *vl@ = vector of words to scan
58 * Use: Initializes a bitscanner from a low-level base-and-limit
59 * representation of an integer. Initially no bit is ready; you
60 * must call @mpscan_step@ before anything useful will come
64 #define MPSCAN_INITX(m_, v_, vl_) do { \
71 extern void mpscan_initx(mpscan */*m*/, const mpw */*v*/, const mpw */*vl*/);
73 /* --- @mpscan_step@ --- *
75 * Arguments: @mpscan *m@ = pointer to bitscanner
77 * Returns: Nonzero if there is another bit to read.
79 * Use: Steps on to the next bit in the integer. The macro version
80 * evaluates its argument multiple times.
83 #define MPSCAN_STEP(m) \
84 ((m)->bits ? ((m)->w >>= 1, (m)->bits--, 1) : \
85 (m)->v < (m)->vl ? ((m)->w = *(m)->v++, \
86 (m)->bits = MPW_BITS - 1, 1) : \
89 extern int mpscan_step(mpscan */*m*/);
91 /* --- @mpscan_bit@ --- *
93 * Arguments: @const mpscan *m@ = pointer to bitscanner
95 * Returns: The value of the current bit.
97 * Use: Reads the value of the current bit looked at by a
101 #define MPSCAN_BIT(m) ((m)->w & 1)
103 extern int mpscan_bit(const mpscan */*m*/);
105 /*----- Left-to right-scanning --------------------------------------------*/
107 /* --- @mpscan_rinitx@ --- *
109 * Arguments: @mpscan *m@ = pointer to bitscanner structure
110 * @const mpw *v, *vl@ = vector of words to scan
114 * Use: Initializes a reverse bitscanner from a low-level
115 * vector-and-length representation of an integer. Initially no
116 * bit is ready; you must call @mpscan_rstep@ before anything
117 * useful will come out.
120 #define MPSCAN_RINITX(m_, v_, vl_) do { \
124 while (_m->vl > _m->v && !_m->vl[-1]) \
129 extern void mpscan_rinitx(mpscan */*m*/,
130 const mpw */*v*/, const mpw */*vl*/);
132 /* --- @mpscan_rstep@ --- *
134 * Arguments: @mpscan *m@ = pointer to bitscanner
136 * Returns: Nonzero if there is another bit to read.
138 * Use: Steps on to the next bit in the integer. The macro version
139 * evaluates its argument multiple times.
142 #define MPSCAN_RSTEP(m) \
143 ((m)->bits ? ((m)->w <<= 1, (m)->bits--, 1) : \
144 (m)->vl > (m)->v ? ((m)->w = *--(m)->vl, \
145 (m)->bits = MPW_BITS - 1, 1) : \
148 extern int mpscan_rstep(mpscan */*m*/);
150 /* --- @mpscan_rbit@ --- *
152 * Arguments: @const mpscan *m@ = pointer to bitscanner
154 * Returns: The value of the current bit.
156 * Use: Reads the value of the current bit looked at by a
157 * reverse bitscanner.
160 #define MPSCAN_RBIT(m) (((m)->w >> (MPW_BITS - 1)) & 1)
162 extern int mpscan_rbit(const mpscan */*m*/);
164 /*----- That's all, folks -------------------------------------------------*/