| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: mpmul.c,v 1.4 2001/02/03 12:00:29 mdw Exp $ |
| 4 | * |
| 5 | * Multiply many small numbers together |
| 6 | * |
| 7 | * (c) 2000 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: mpmul.c,v $ |
| 33 | * Revision 1.4 2001/02/03 12:00:29 mdw |
| 34 | * Now @mp_drop@ checks its argument is non-NULL before attempting to free |
| 35 | * it. Note that the macro version @MP_DROP@ doesn't do this. |
| 36 | * |
| 37 | * Revision 1.3 2000/10/08 12:11:22 mdw |
| 38 | * Use @MP_EQ@ instead of @MP_CMP@. |
| 39 | * |
| 40 | * Revision 1.2 2000/07/09 21:31:10 mdw |
| 41 | * Fix bug, and add a test rig. |
| 42 | * |
| 43 | * Revision 1.1 2000/07/01 11:21:39 mdw |
| 44 | * New interface for computing products of many (small) integers. |
| 45 | * |
| 46 | */ |
| 47 | |
| 48 | /*----- Header files ------------------------------------------------------*/ |
| 49 | |
| 50 | #include "mp.h" |
| 51 | #include "mpint.h" |
| 52 | #include "mpmul.h" |
| 53 | |
| 54 | /*----- Main code ---------------------------------------------------------*/ |
| 55 | |
| 56 | /* --- @mpmul_init@ --- * |
| 57 | * |
| 58 | * Arguments: @mpmul *b@ = pointer to multiplier context to initialize |
| 59 | * |
| 60 | * Returns: --- |
| 61 | * |
| 62 | * Use: Initializes a big multiplier context for use. |
| 63 | */ |
| 64 | |
| 65 | void mpmul_init(mpmul *b) |
| 66 | { |
| 67 | b->i = 0; |
| 68 | } |
| 69 | |
| 70 | /* --- @mpmul_add@ --- * |
| 71 | * |
| 72 | * Arguments: @mpmul *b@ = pointer to multiplier context |
| 73 | * @mp *x@ = the next factor to multiply in |
| 74 | * |
| 75 | * Returns: --- |
| 76 | * |
| 77 | * Use: Contributes another factor to the mix. It's important that |
| 78 | * the integer lasts at least as long as the multiplication |
| 79 | * context; this sort of rules out @mp_build@ integers. |
| 80 | */ |
| 81 | |
| 82 | void mpmul_add(mpmul *b, mp *x) |
| 83 | { |
| 84 | size_t i = b->i; |
| 85 | |
| 86 | /* --- Now do the reduction step --- */ |
| 87 | |
| 88 | x = MP_COPY(x); |
| 89 | |
| 90 | while (i > 0) { |
| 91 | if (MP_LEN(b->v[i - 1]) > MP_LEN(x)) |
| 92 | break; |
| 93 | i--; |
| 94 | x = mp_mul(x, x, b->v[i]); |
| 95 | MP_DROP(b->v[i]); |
| 96 | } |
| 97 | |
| 98 | if (i > HWM) { |
| 99 | while (i > LWM || (i > 0 && MP_LEN(b->v[i - 1]) <= MP_LEN(x))) { |
| 100 | i--; |
| 101 | x = mp_mul(x, x, b->v[i]); |
| 102 | MP_DROP(b->v[i]); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | b->v[i++] = x; |
| 107 | b->i = i; |
| 108 | } |
| 109 | |
| 110 | /* --- @mpmul_done@ --- * |
| 111 | * |
| 112 | * Arguments: @mpmul *b@ = pointer to big multiplication context |
| 113 | * |
| 114 | * Returns: The product of all the numbers contributed. |
| 115 | * |
| 116 | * Use: Returns a (large) product of numbers. The context is |
| 117 | * deallocated. |
| 118 | */ |
| 119 | |
| 120 | mp *mpmul_done(mpmul *b) |
| 121 | { |
| 122 | size_t i = b->i; |
| 123 | mp *x; |
| 124 | |
| 125 | if (!i) |
| 126 | return (MP_ONE); |
| 127 | i--; |
| 128 | x = b->v[i]; |
| 129 | while (i > 0) { |
| 130 | i--; |
| 131 | x = mp_mul(x, x, b->v[i]); |
| 132 | MP_DROP(b->v[i]); |
| 133 | } |
| 134 | return (x); |
| 135 | } |
| 136 | |
| 137 | /* --- @mp_factorial@ --- * |
| 138 | * |
| 139 | * Arguments: @unsigned long i@ = number whose factorial should be |
| 140 | * computed. |
| 141 | * |
| 142 | * Returns: The requested factorial. |
| 143 | */ |
| 144 | |
| 145 | mp *mp_factorial(unsigned long i) |
| 146 | { |
| 147 | unsigned long j; |
| 148 | mp *x = MP_NEW; |
| 149 | mpmul b = MPMUL_INIT; |
| 150 | |
| 151 | for (j = 1; j <= i; j++) { |
| 152 | x = mp_fromulong(x, j); |
| 153 | mpmul_add(&b, x); |
| 154 | } |
| 155 | mp_drop(x); |
| 156 | return (mpmul_done(&b)); |
| 157 | } |
| 158 | |
| 159 | /*----- Test rig ----------------------------------------------------------*/ |
| 160 | |
| 161 | #ifdef TEST_RIG |
| 162 | |
| 163 | #include <mLib/testrig.h> |
| 164 | |
| 165 | static int vfact(dstr *v) |
| 166 | { |
| 167 | unsigned long x = *(unsigned long *)v[0].buf; |
| 168 | mp *fx = *(mp **)v[1].buf; |
| 169 | mp *y = mp_factorial(x); |
| 170 | int ok = 1; |
| 171 | if (!MP_EQ(fx, y)) { |
| 172 | fprintf(stderr, "factorial failed\n"); |
| 173 | MP_FPRINTF(stderr, (stderr, "%lu! = ", x), fx); |
| 174 | MP_EPRINT("result", y); |
| 175 | ok = 0; |
| 176 | } |
| 177 | mp_drop(fx); |
| 178 | mp_drop(y); |
| 179 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 180 | return (ok); |
| 181 | } |
| 182 | |
| 183 | static test_chunk tests[] = { |
| 184 | { "factorial", vfact, { &type_ulong, &type_mp, 0 } }, |
| 185 | { 0, 0, { 0 } } |
| 186 | }; |
| 187 | |
| 188 | int main(int argc, char *argv[]) |
| 189 | { |
| 190 | test_run(argc, argv, tests, SRCDIR "/tests/mp"); |
| 191 | return (0); |
| 192 | } |
| 193 | |
| 194 | #endif |
| 195 | |
| 196 | /*----- That's all, folks -------------------------------------------------*/ |