chiark / gitweb /
Version bump.
[catacomb] / mpscan.h
1 /* -*-c-*-
2  *
3  * $Id: mpscan.h,v 1.3 1999/12/10 23:29:48 mdw Exp $
4  *
5  * Sequential bit scan of multiprecision integers
6  *
7  * (c) 1999 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of Catacomb.
13  *
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.
18  * 
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.
23  * 
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,
27  * MA 02111-1307, USA.
28  */
29
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: mpscan.h,v $
33  * Revision 1.3  1999/12/10 23:29:48  mdw
34  * Change header file guard names.
35  *
36  * Revision 1.2  1999/11/13 01:55:10  mdw
37  * Fixed so that they compile.  Minor interface changes.
38  *
39  * Revision 1.1  1999/09/03 08:41:12  mdw
40  * Initial import.
41  *
42  */
43
44 #ifndef CATACOMB_MPSCAN_H
45 #define CATACOMB_MPSCAN_H
46
47 #ifdef __cplusplus
48   extern "C" {
49 #endif
50
51 /*----- Header files ------------------------------------------------------*/
52
53 #ifndef CATACOMB_MPW_H
54 #  include "mpw.h"
55 #endif
56
57 /*----- Data structures ---------------------------------------------------*/
58
59 typedef struct mpscan {
60   const mpw *v, *vl;                    /* Vector of words to scan */
61   mpw w;                                /* Current word to scan */
62   int bits;                             /* Number of bits left in @w@ */
63 } mpscan;
64
65 /*----- Functions provided ------------------------------------------------*/
66
67 /* --- @mpscan_initx@ --- *
68  *
69  * Arguments:   @mpscan *m@ = pointer to bitscanner structure
70  *              @const mpw *v, *vl@ = vector of words to scan
71  *
72  * Returns:     ---
73  *
74  * Use:         Initializes a bitscanner from a low-level base-and-limit
75  *              representation of an integer.  Initially no bit is ready; you
76  *              must call @mpscan_step@ before anything useful will come
77  *              out.
78  */
79
80 #define MPSCAN_INITX(m_, v_, vl_) do {                                  \
81   mpscan *_m = (m_);                                                    \
82   _m->v = (v_);                                                         \
83   _m->vl = (vl_);                                                       \
84   _m->bits = 0;                                                         \
85 } while (0)
86
87 extern void mpscan_initx(mpscan */*m*/, const mpw */*v*/, const mpw */*vl*/);
88
89 /* --- @mpscan_step@ --- *
90  *
91  * Arguments:   @mpscan *m@ = pointer to bitscanner
92  *
93  * Returns:     Nonzero if there is another bit to read.
94  *
95  * Use:         Steps on to the next bit in the integer.  The macro version
96  *              evaluates its argument multiple times.
97  */
98
99 #define MPSCAN_STEP(m)                                                  \
100   ((m)->bits ? ((m)->w >>= 1,                                           \
101                 (m)->bits--, 1) :                                       \
102    (m)->v < (m)->vl ? ((m)->w = *(m)->v++,                              \
103                        (m)->bits = MPW_BITS - 1, 1) :                   \
104    0)
105
106 extern int mpscan_step(mpscan */*m*/);
107
108 /* --- @mpscan_bit@ --- *
109  *
110  * Arguments:   @const mpscan *m@ = pointer to bitscanner
111  *
112  * Returns:     The value of the current bit.
113  *
114  * Use:         Reads the value of the current bit looked at by a
115  *              bitscanner.
116  */
117
118 #define MPSCAN_BIT(m) ((m)->w & 1)
119
120 extern int mpscan_bit(const mpscan */*m*/);
121
122 /*----- That's all, folks -------------------------------------------------*/
123
124 #ifdef __cplusplus
125   }
126 #endif
127
128 #endif