1 1.2 christos /* $NetBSD: sum.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */ 2 1.1 kleink 3 1.1 kleink /**************************************************************** 4 1.1 kleink 5 1.1 kleink The author of this software is David M. Gay. 6 1.1 kleink 7 1.1 kleink Copyright (C) 1998 by Lucent Technologies 8 1.1 kleink All Rights Reserved 9 1.1 kleink 10 1.1 kleink Permission to use, copy, modify, and distribute this software and 11 1.1 kleink its documentation for any purpose and without fee is hereby 12 1.1 kleink granted, provided that the above copyright notice appear in all 13 1.1 kleink copies and that both that the copyright notice and this 14 1.1 kleink permission notice and warranty disclaimer appear in supporting 15 1.1 kleink documentation, and that the name of Lucent or any of its entities 16 1.1 kleink not be used in advertising or publicity pertaining to 17 1.1 kleink distribution of the software without specific, written prior 18 1.1 kleink permission. 19 1.1 kleink 20 1.1 kleink LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 21 1.1 kleink INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 22 1.1 kleink IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 23 1.1 kleink SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 24 1.1 kleink WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 25 1.1 kleink IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 26 1.1 kleink ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 27 1.1 kleink THIS SOFTWARE. 28 1.1 kleink 29 1.1 kleink ****************************************************************/ 30 1.1 kleink 31 1.1 kleink /* Please send bug reports to David M. Gay (dmg at acm dot org, 32 1.1 kleink * with " at " changed at "@" and " dot " changed to "."). */ 33 1.1 kleink 34 1.1 kleink #include "gdtoaimp.h" 35 1.1 kleink 36 1.1 kleink Bigint * 37 1.1 kleink #ifdef KR_headers 38 1.1 kleink sum(a, b) Bigint *a; Bigint *b; 39 1.1 kleink #else 40 1.1 kleink sum(Bigint *a, Bigint *b) 41 1.1 kleink #endif 42 1.1 kleink { 43 1.1 kleink Bigint *c; 44 1.1 kleink ULong carry, *xc, *xa, *xb, *xe, y; 45 1.1 kleink #ifdef Pack_32 46 1.1 kleink ULong z; 47 1.1 kleink #endif 48 1.1 kleink 49 1.1 kleink if (a->wds < b->wds) { 50 1.1 kleink c = b; b = a; a = c; 51 1.1 kleink } 52 1.1 kleink c = Balloc(a->k); 53 1.2 christos if (c == NULL) 54 1.2 christos return NULL; 55 1.1 kleink c->wds = a->wds; 56 1.1 kleink carry = 0; 57 1.1 kleink xa = a->x; 58 1.1 kleink xb = b->x; 59 1.1 kleink xc = c->x; 60 1.1 kleink xe = xc + b->wds; 61 1.1 kleink #ifdef Pack_32 62 1.1 kleink do { 63 1.1 kleink y = (*xa & 0xffff) + (*xb & 0xffff) + carry; 64 1.1 kleink carry = (y & 0x10000) >> 16; 65 1.1 kleink z = (*xa++ >> 16) + (*xb++ >> 16) + carry; 66 1.1 kleink carry = (z & 0x10000) >> 16; 67 1.1 kleink Storeinc(xc, z, y); 68 1.1 kleink } 69 1.1 kleink while(xc < xe); 70 1.1 kleink xe += a->wds - b->wds; 71 1.1 kleink while(xc < xe) { 72 1.1 kleink y = (*xa & 0xffff) + carry; 73 1.1 kleink carry = (y & 0x10000) >> 16; 74 1.1 kleink z = (*xa++ >> 16) + carry; 75 1.1 kleink carry = (z & 0x10000) >> 16; 76 1.1 kleink Storeinc(xc, z, y); 77 1.1 kleink } 78 1.1 kleink #else 79 1.1 kleink do { 80 1.1 kleink y = *xa++ + *xb++ + carry; 81 1.1 kleink carry = (y & 0x10000) >> 16; 82 1.1 kleink *xc++ = y & 0xffff; 83 1.1 kleink } 84 1.1 kleink while(xc < xe); 85 1.1 kleink xe += a->wds - b->wds; 86 1.1 kleink while(xc < xe) { 87 1.1 kleink y = *xa++ + carry; 88 1.1 kleink carry = (y & 0x10000) >> 16; 89 1.1 kleink *xc++ = y & 0xffff; 90 1.1 kleink } 91 1.1 kleink #endif 92 1.1 kleink if (carry) { 93 1.1 kleink if (c->wds == c->maxwds) { 94 1.1 kleink b = Balloc(c->k + 1); 95 1.2 christos if (b == NULL) 96 1.2 christos return NULL; 97 1.1 kleink Bcopy(b, c); 98 1.1 kleink Bfree(c); 99 1.1 kleink c = b; 100 1.1 kleink } 101 1.1 kleink c->x[c->wds++] = 1; 102 1.1 kleink } 103 1.1 kleink return c; 104 1.1 kleink } 105