3 * $Id: mpscan.h,v 1.5 2004/04/08 01:36:15 mdw Exp $
5 * Sequential bit scan of multiprecision integers
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 #ifndef CATACOMB_MPSCAN_H
31 #define CATACOMB_MPSCAN_H
37 /*----- Header files ------------------------------------------------------*/
39 #ifndef CATACOMB_MPW_H
43 /*----- Data structures ---------------------------------------------------*/
45 typedef struct mpscan
{
46 const mpw
*v
, *vl
; /* Vector of words to scan */
47 mpw w
; /* Current word to scan */
48 int bits
; /* Number of bits left in @w@ */
51 /*----- Right-to-left scanning --------------------------------------------*/
53 /* --- @mpscan_initx@ --- *
55 * Arguments: @mpscan *m@ = pointer to bitscanner structure
56 * @const mpw *v, *vl@ = vector of words to scan
60 * Use: Initializes a bitscanner from a low-level base-and-limit
61 * representation of an integer. Initially no bit is ready; you
62 * must call @mpscan_step@ before anything useful will come
66 #define MPSCAN_INITX(m_, v_, vl_) do { \
73 extern void mpscan_initx(mpscan */
*m*/
, const mpw */
*v*/
, const mpw */
*vl*/
);
75 /* --- @mpscan_step@ --- *
77 * Arguments: @mpscan *m@ = pointer to bitscanner
79 * Returns: Nonzero if there is another bit to read.
81 * Use: Steps on to the next bit in the integer. The macro version
82 * evaluates its argument multiple times.
85 #define MPSCAN_STEP(m) \
86 ((m)->bits ? ((m)->w >>= 1, (m)->bits--, 1) : \
87 (m)->v < (m)->vl ? ((m)->w = *(m)->v++, \
88 (m)->bits = MPW_BITS - 1, 1) : \
91 extern int mpscan_step(mpscan */
*m*/
);
93 /* --- @mpscan_bit@ --- *
95 * Arguments: @const mpscan *m@ = pointer to bitscanner
97 * Returns: The value of the current bit.
99 * Use: Reads the value of the current bit looked at by a
103 #define MPSCAN_BIT(m) ((m)->w & 1)
105 extern int mpscan_bit(const mpscan */
*m*/
);
107 /*----- Left-to right-scanning --------------------------------------------*/
109 /* --- @mpscan_rinitx@ --- *
111 * Arguments: @mpscan *m@ = pointer to bitscanner structure
112 * @const mpw *v, *vl@ = vector of words to scan
116 * Use: Initializes a reverse bitscanner from a low-level
117 * vector-and-length representation of an integer. Initially no
118 * bit is ready; you must call @mpscan_rstep@ before anything
119 * useful will come out.
122 #define MPSCAN_RINITX(m_, v_, vl_) do { \
126 while (_m->vl > _m->v && !_m->vl[-1]) \
131 extern void mpscan_rinitx(mpscan */
*m*/
,
132 const mpw */
*v*/
, const mpw */
*vl*/
);
134 /* --- @mpscan_rstep@ --- *
136 * Arguments: @mpscan *m@ = pointer to bitscanner
138 * Returns: Nonzero if there is another bit to read.
140 * Use: Steps on to the next bit in the integer. The macro version
141 * evaluates its argument multiple times.
144 #define MPSCAN_RSTEP(m) \
145 ((m)->bits ? ((m)->w <<= 1, (m)->bits--, 1) : \
146 (m)->vl > (m)->v ? ((m)->w = *--(m)->vl, \
147 (m)->bits = MPW_BITS - 1, 1) : \
150 extern int mpscan_rstep(mpscan */
*m*/
);
152 /* --- @mpscan_rbit@ --- *
154 * Arguments: @const mpscan *m@ = pointer to bitscanner
156 * Returns: The value of the current bit.
158 * Use: Reads the value of the current bit looked at by a
159 * reverse bitscanner.
162 #define MPSCAN_RBIT(m) (((m)->w >> (MPW_BITS - 1)) & 1)
164 extern int mpscan_rbit(const mpscan */
*m*/
);
166 /*----- That's all, folks -------------------------------------------------*/