| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: mpx.h,v 1.2 1999/11/11 17:47:55 mdw Exp $ |
| 4 | * |
| 5 | * Low level multiprecision arithmetic |
| 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: mpx.h,v $ |
| 33 | * Revision 1.2 1999/11/11 17:47:55 mdw |
| 34 | * Minor changes for different `mptypes.h' format. |
| 35 | * |
| 36 | * Revision 1.1 1999/09/03 08:41:12 mdw |
| 37 | * Initial import. |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | #ifndef MPX_H |
| 42 | #define MPX_H |
| 43 | |
| 44 | #ifdef __cplusplus |
| 45 | extern "C" { |
| 46 | #endif |
| 47 | |
| 48 | /*----- The idea ----------------------------------------------------------* |
| 49 | * |
| 50 | * This file provides functions and macros which work on vectors of words as |
| 51 | * unsigned multiprecision integers. The interface works in terms of base |
| 52 | * and limit pointers (i.e., a pointer to the start of a vector, and a |
| 53 | * pointer just past its end) rather than base pointer and length, because |
| 54 | * that requires more arithmetic and state to work on. |
| 55 | * |
| 56 | * The interfaces are slightly bizarre in other ways. Try to use the |
| 57 | * higher-level functions where you can: they're rather better designed to |
| 58 | * actually be friendly and useful. |
| 59 | */ |
| 60 | |
| 61 | /*----- Header files ------------------------------------------------------*/ |
| 62 | |
| 63 | #include <string.h> |
| 64 | |
| 65 | #ifndef MPTYPES_H |
| 66 | # include "mptypes.h" |
| 67 | #endif |
| 68 | |
| 69 | /*----- Basic macros ------------------------------------------------------*/ |
| 70 | |
| 71 | /* --- @MPW@ --- * |
| 72 | * |
| 73 | * Arguments: @x@ = an unsigned value |
| 74 | * |
| 75 | * Use: Expands to the value of @x@ masked and typecast to a |
| 76 | * multiprecision integer word. |
| 77 | */ |
| 78 | |
| 79 | #define MPW(x) ((mpw)((x) & MPW_MAX)) |
| 80 | |
| 81 | /* --- @MPWS@ --- * |
| 82 | * |
| 83 | * Arguments: @n@ = number of words |
| 84 | * |
| 85 | * Use: Expands to the number of bytes occupied by a given number of |
| 86 | * words. |
| 87 | */ |
| 88 | |
| 89 | #define MPWS(n) ((n) * sizeof(mpw)) |
| 90 | |
| 91 | /*----- General manipulation ----------------------------------------------*/ |
| 92 | |
| 93 | /* --- @MPX_SHRINK@ --- * |
| 94 | * |
| 95 | * Arguments: @const mpw *v@ = pointer to vector of words |
| 96 | * @const mpw *vl@ = (updated) current limit of vector |
| 97 | * |
| 98 | * Use: Shrinks down the limit of a multiprecision integer vector. |
| 99 | */ |
| 100 | |
| 101 | #define MPX_SHRINK(v, vl) do { \ |
| 102 | const mpw *_v = (v), *_vl = (vl); \ |
| 103 | while (_vl > _v && *--_vl) \ |
| 104 | ; \ |
| 105 | (vl) = _vl; \ |
| 106 | } while (0) |
| 107 | |
| 108 | /* --- @MPX_BITS@ --- * |
| 109 | * |
| 110 | * Arguments: @unsigned long b@ = result variable |
| 111 | * @const mpw *v@ = pointer to array of words |
| 112 | * @const mpw *vl@ = limit of vector (from @MPX_SHRINK@) |
| 113 | * |
| 114 | * Use: Calculates the number of bits in a multiprecision value. |
| 115 | */ |
| 116 | |
| 117 | #define MPX_BITS(b, v, vl) do { \ |
| 118 | const mpw *_v = (v), *_vl = (vl); \ |
| 119 | if (_v == _vl) \ |
| 120 | (b) = 0; \ |
| 121 | else { \ |
| 122 | unsigned long _b = MPW_BITS * (_vl - _v - 1) + 1; \ |
| 123 | mpw _w = _vl[-1]; \ |
| 124 | unsigned _k = MPW_BITS / 2; \ |
| 125 | while (_k) { \ |
| 126 | if (_w >> _k) { \ |
| 127 | _w >>= _k; \ |
| 128 | _b += _k; \ |
| 129 | } \ |
| 130 | _k >>= 1; \ |
| 131 | } \ |
| 132 | (b) = _b; \ |
| 133 | } \ |
| 134 | } while (0) |
| 135 | |
| 136 | /* --- @MPX_OCTETS@ --- * |
| 137 | * |
| 138 | * Arguments: @size_t o@ = result variable |
| 139 | * @const mpw *v@ = pointer to array of words |
| 140 | * @size_t len@ = length of the array (from @MPX_LEN@) |
| 141 | * |
| 142 | * Use: Calculates the number of octets in a multiprecision value. |
| 143 | */ |
| 144 | |
| 145 | #define MPX_OCTETS(o, v, len) do { \ |
| 146 | const mpw *_v = (v), *_vl = (vl); \ |
| 147 | if (_v == _vl) \ |
| 148 | (o) = 0; \ |
| 149 | else { \ |
| 150 | _size_t _o = (MPW_BITS / 8) * (_vl - _v - 1); \ |
| 151 | mpw _w = _vl[-1]; \ |
| 152 | unsigned _k = MPW_BITS / 2; \ |
| 153 | while (_k > 3) { \ |
| 154 | if (_w >> _k) { \ |
| 155 | _w >>= _k; \ |
| 156 | _o += _k - 3; \ |
| 157 | } \ |
| 158 | _k >>= 1; \ |
| 159 | } \ |
| 160 | if (_w) \ |
| 161 | _o++; \ |
| 162 | (o) = _o; \ |
| 163 | } \ |
| 164 | } while (0) |
| 165 | |
| 166 | /* --- @MPX_COPY@ --- * |
| 167 | * |
| 168 | * Arguments: @dv, dvl@ = destination vector base and limit |
| 169 | * @av, avl@ = source vector base and limit |
| 170 | * |
| 171 | * Use: Copies a multiprecision integer. |
| 172 | */ |
| 173 | |
| 174 | #define MPX_COPY(dv, dvl, av, dvl) do { \ |
| 175 | mpw *_dv = (dv); \ |
| 176 | size_t _dn = (dvl) - _dv; \ |
| 177 | const mpw *_av = (av); \ |
| 178 | size_t _an = (avl) - _av; \ |
| 179 | if (_av == _dv) { \ |
| 180 | if (_dvl > _avl) \ |
| 181 | memset(_avl, 0, MPWS(_dn - _an)); \ |
| 182 | } else if (_an >= _dn) \ |
| 183 | memmove(_dv, _av, MPWS(_dn)); \ |
| 184 | else { \ |
| 185 | memmove(_dv, _av, MPWS(_an)); \ |
| 186 | memset(_dv + _an, 0, MPWS(_dn - _an)); \ |
| 187 | } \ |
| 188 | } while (0) |
| 189 | |
| 190 | /* --- @MPX_ZERO@ --- * |
| 191 | * |
| 192 | * Arguments: @v, vl@ = base and limit of vector to clear |
| 193 | * |
| 194 | * Use: Zeroes the area between the two vector pointers. |
| 195 | */ |
| 196 | |
| 197 | #define MPX_ZERO(v, vl) { \ |
| 198 | mpw *_v = (v), *_vl = (vl); \ |
| 199 | memset(_v, 0, MPWS(_vl - _v)); \ |
| 200 | } while (0) |
| 201 | |
| 202 | /*----- Loading and storing -----------------------------------------------*/ |
| 203 | |
| 204 | /* --- @mpx_storel@ --- * |
| 205 | * |
| 206 | * Arguments: @const mpw *v, *vl@ = base and limit of source vector |
| 207 | * @octet *p@ = pointer to octet array |
| 208 | * @size_t sz@ = size of octet array |
| 209 | * |
| 210 | * Returns: --- |
| 211 | * |
| 212 | * Use: Stores an MP in an octet array, least significant octet |
| 213 | * first. High-end octets are silently discarded if there |
| 214 | * isn't enough space for them. |
| 215 | */ |
| 216 | |
| 217 | extern void mpx_storel(const mpw */*v*/, const mpw */*vl*/, |
| 218 | octet */*p*/, size_t /*sz*/); |
| 219 | |
| 220 | /* --- @mpx_loadl@ --- * |
| 221 | * |
| 222 | * Arguments: @mpw *v, *vl@ = base and limit of destination vector |
| 223 | * @const octet *p@ = pointer to octet array |
| 224 | * @size_t sz@ = size of octet array |
| 225 | * |
| 226 | * Returns: --- |
| 227 | * |
| 228 | * Use: Loads an MP in an octet array, least significant octet |
| 229 | * first. High-end octets are ignored if there isn't enough |
| 230 | * space for them. |
| 231 | */ |
| 232 | |
| 233 | extern void mpx_loadl(mpw */*v*/, mpw */*vl*/, |
| 234 | const octet */*p*/, size_t /*sz*/); |
| 235 | |
| 236 | /* --- @mpx_storeb@ --- * |
| 237 | * |
| 238 | * Arguments: @const mpw *v, *vl@ = base and limit of source vector |
| 239 | * @octet *p@ = pointer to octet array |
| 240 | * @size_t sz@ = size of octet array |
| 241 | * |
| 242 | * Returns: --- |
| 243 | * |
| 244 | * Use: Stores an MP in an octet array, most significant octet |
| 245 | * first. High-end octets are silently discarded if there |
| 246 | * isn't enough space for them. |
| 247 | */ |
| 248 | |
| 249 | extern void mpx_storeb(const mpw */*v*/, const mpw */*vl*/, |
| 250 | octet */*p*/, size_t /*sz*/); |
| 251 | |
| 252 | /* --- @mpx_loadb@ --- * |
| 253 | * |
| 254 | * Arguments: @mpw *v, *vl@ = base and limit of destination vector |
| 255 | * @const octet *p@ = pointer to octet array |
| 256 | * @size_t sz@ = size of octet array |
| 257 | * |
| 258 | * Returns: --- |
| 259 | * |
| 260 | * Use: Loads an MP in an octet array, most significant octet |
| 261 | * first. High-end octets are ignored if there isn't enough |
| 262 | * space for them. |
| 263 | */ |
| 264 | |
| 265 | extern void mpx_loadb(mpw */*v*/, mpw */*vl*/, |
| 266 | const octet */*p*/, size_t /*sz*/); |
| 267 | |
| 268 | /*----- Logical shifting --------------------------------------------------*/ |
| 269 | |
| 270 | /* --- @mpx_lsl@ --- * |
| 271 | * |
| 272 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit |
| 273 | * @const mpw *av, *avl@ = source vector base and limit |
| 274 | * @size_t n@ = number of bit positions to shift by |
| 275 | * |
| 276 | * Returns: --- |
| 277 | * |
| 278 | * Use: Performs a logical shift left operation on an integer. |
| 279 | */ |
| 280 | |
| 281 | extern void mpx_lsl(mpw */*dv*/, mpw */*dvl*/, |
| 282 | const mpw */*av*/, const mpw */*avl*/, |
| 283 | size_t /*n*/); |
| 284 | |
| 285 | /* --- @mpx_lsr@ --- * |
| 286 | * |
| 287 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit |
| 288 | * @const mpw *av, *avl@ = source vector base and limit |
| 289 | * @size_t n@ = number of bit positions to shift by |
| 290 | * |
| 291 | * Returns: --- |
| 292 | * |
| 293 | * Use: Performs a logical shift right operation on an integer. |
| 294 | */ |
| 295 | |
| 296 | extern void mpx_lsr(mpw */*dv*/, mpw */*dvl*/, |
| 297 | const mpw */*av*/, const mpw */*avl*/, |
| 298 | size_t /*n*/); |
| 299 | |
| 300 | /*----- Unsigned arithmetic -----------------------------------------------*/ |
| 301 | |
| 302 | /* --- @mpx_ucmp@ --- * |
| 303 | * |
| 304 | * Arguments: @const mpw *av, *avl@ = first argument vector base and limit |
| 305 | * @const mpw *bv, *bvl@ = second argument vector base and limit |
| 306 | * |
| 307 | * Returns: Less than, equal to, or greater than zero depending on |
| 308 | * whether @a@ is less than, equal to or greater than @b@, |
| 309 | * respectively. |
| 310 | * |
| 311 | * Use: Performs an unsigned integer comparison. |
| 312 | */ |
| 313 | |
| 314 | #define MPX_UCMP(av, avl, op, dv, dvl) \ |
| 315 | (mpx_ucmp((av), (avl), (dv), (dvl)) op 0) |
| 316 | |
| 317 | extern int mpx_ucmp(const mpw */*av*/, const mpw */*avl*/, |
| 318 | const mpw */*bv*/, const mpw */*bvl*/); |
| 319 | |
| 320 | /* --- @mpx_uadd@ --- * |
| 321 | * |
| 322 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit |
| 323 | * @const mpw *av, *avl@ = first addend vector base and limit |
| 324 | * @const mpw *bv, *bvl@ = second addend vector base and limit |
| 325 | * |
| 326 | * Returns: --- |
| 327 | * |
| 328 | * Use: Performs unsigned integer addition. If the result overflows |
| 329 | * the destination vector, high-order bits are discarded. This |
| 330 | * means that two's complement addition happens more or less for |
| 331 | * free, although that's more a side-effect than anything else. |
| 332 | * The result vector may be equal to either or both source |
| 333 | * vectors, but may not otherwise overlap them. |
| 334 | */ |
| 335 | |
| 336 | extern void mpx_uadd(mpw */*dv*/, mpw */*dvl*/, |
| 337 | const mpw */*av*/, const mpw */*avl*/, |
| 338 | const mpw */*bv*/, const mpw */*bvl*/); |
| 339 | |
| 340 | /* --- @mpx_usub@ --- * |
| 341 | * |
| 342 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit |
| 343 | * @const mpw *av, *avl@ = first argument vector base and limit |
| 344 | * @const mpw *bv, *bvl@ = second argument vector base and limit |
| 345 | * |
| 346 | * Returns: --- |
| 347 | * |
| 348 | * Use: Performs unsigned integer subtraction. If the result |
| 349 | * overflows the destination vector, high-order bits are |
| 350 | * discarded. This means that two's complement subtraction |
| 351 | * happens more or less for free, althuogh that's more a side- |
| 352 | * effect than anything else. The result vector may be equal to |
| 353 | * either or both source vectors, but may not otherwise overlap |
| 354 | * them. |
| 355 | */ |
| 356 | |
| 357 | extern void mpx_usub(mpw */*dv*/, mpw */*dvl*/, |
| 358 | const mpw */*av*/, const mpw */*avl*/, |
| 359 | const mpw */*bv*/, const mpw */*bvl*/); |
| 360 | |
| 361 | /* --- @MPX_UMULN@ --- * |
| 362 | * |
| 363 | * Arguments: @dv, dvl@ = destination vector base and limit |
| 364 | * @av, avl@ = multiplicand vector base and limit |
| 365 | * @m@ = multiplier |
| 366 | * |
| 367 | * Use: Multiplies a multiprecision integer by a single-word value. |
| 368 | * The destination and source may be equal. The destination |
| 369 | * is completely cleared after use. |
| 370 | */ |
| 371 | |
| 372 | #define MPX_UMULN(dv, dvl, av, avl, m) do { \ |
| 373 | mpw *_dv = (dv), *_dvl = (dvl); \ |
| 374 | const mpw *_av = (av), *_avl = (avl); \ |
| 375 | mpw _c = 0; \ |
| 376 | mpd _m = (m); \ |
| 377 | \ |
| 378 | while (_av < _avl) { \ |
| 379 | mpd _x; \ |
| 380 | if (_dv >= _dvl) \ |
| 381 | break; \ |
| 382 | _x = _m * *_av++ + c; \ |
| 383 | *_dv++ = MPW(_x); \ |
| 384 | _c = _x >> MPW_BITS; \ |
| 385 | } \ |
| 386 | if (_dv < _dvl) { \ |
| 387 | *_dv++ = MPW(_c); \ |
| 388 | MPX_ZERO(_dv, _dvl); \ |
| 389 | } \ |
| 390 | } while (0) |
| 391 | |
| 392 | /* --- @MPX_UMLAN@ --- * |
| 393 | * |
| 394 | * Arguments: @dv, dvl@ = destination/accumulator vector base and limit |
| 395 | * @av, avl@ = multiplicand vector base and limit |
| 396 | * @m@ = multiplier |
| 397 | * |
| 398 | * Use: Multiplies a multiprecision integer by a single-word value |
| 399 | * and adds the result to an accumulator. |
| 400 | */ |
| 401 | |
| 402 | #define MPX_UMLAN(dv, dvl, av, avl, m) do { \ |
| 403 | mpw *_dv = (dv), *_dvl = (dvl); \ |
| 404 | const mpw *_av = (av), *_avl = (avl); \ |
| 405 | mpw _c = 0; \ |
| 406 | mpd _m = (m); \ |
| 407 | \ |
| 408 | while (_av < _avl) { \ |
| 409 | mpd _x; \ |
| 410 | if (_dv >= _dvl) \ |
| 411 | break; \ |
| 412 | _x = *_dv + _m * *_av++ + _c; \ |
| 413 | *_dv++ = MPW(_x); \ |
| 414 | _c = _x >> MPW_BITS; \ |
| 415 | } \ |
| 416 | if (_dv < _dvl) { \ |
| 417 | *_dv++ = MPW(_c); \ |
| 418 | MPX_ZERO(_dv, _dvl); \ |
| 419 | } \ |
| 420 | } while (0) |
| 421 | |
| 422 | /* --- @mpx_umul@ --- * |
| 423 | * |
| 424 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit |
| 425 | * @const mpw *av, *avl@ = multiplicand vector base and limit |
| 426 | * @const mpw *bv, *bvl@ = multiplier vector base and limit |
| 427 | * |
| 428 | * Returns: --- |
| 429 | * |
| 430 | * Use: Performs unsigned integer multiplication. If the result |
| 431 | * overflows the desination vector, high-order bits are |
| 432 | * discarded. The result vector may not overlap the argument |
| 433 | * vectors in any way. |
| 434 | */ |
| 435 | |
| 436 | extern void mpx_umul(mpw */*dv*/, mpw */*dvl*/, |
| 437 | const mpw */*av*/, const mpw */*avl*/, |
| 438 | const mpw */*bv*/, const mpw */*bvl*/); |
| 439 | |
| 440 | /* --- @mpx_udiv@ --- * |
| 441 | * |
| 442 | * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit |
| 443 | * @mpw *rv, *rvl@ = dividend/remainder vector base and limit |
| 444 | * @const mpw *dv, *dvl@ = divisor vector base and limit |
| 445 | * |
| 446 | * Returns: --- |
| 447 | * |
| 448 | * Use: Performs unsigned integer division. If the result overflows |
| 449 | * the quotient vector, high-order bits are discarded. (Clearly |
| 450 | * the remainder vector can't overflow.) The various vectors |
| 451 | * may not overlap in any way. Yes, I know it's a bit odd |
| 452 | * requiring the dividend to be in the result position but it |
| 453 | * does make some sense really. |
| 454 | */ |
| 455 | |
| 456 | extern void mpx_udiv(mpw */*qv*/, mpw */*qvl*/, mpw */*rv*/, mpw */*rvl*/, |
| 457 | const mpw */*dv*/, const mpw */*dvl*/); |
| 458 | |
| 459 | /*----- That's all, folks -------------------------------------------------*/ |
| 460 | |
| 461 | #ifdef __cplusplus |
| 462 | } |
| 463 | #endif |
| 464 | |
| 465 | #endif |