sum.c revision 112158
1139743Simp/**************************************************************** 243412Snewton 343412SnewtonThe author of this software is David M. Gay. 443412Snewton 543412SnewtonCopyright (C) 1998 by Lucent Technologies 643412SnewtonAll Rights Reserved 743412Snewton 843412SnewtonPermission to use, copy, modify, and distribute this software and 943412Snewtonits documentation for any purpose and without fee is hereby 1043412Snewtongranted, provided that the above copyright notice appear in all 1143412Snewtoncopies and that both that the copyright notice and this 1243412Snewtonpermission notice and warranty disclaimer appear in supporting 1343412Snewtondocumentation, and that the name of Lucent or any of its entities 1443412Snewtonnot be used in advertising or publicity pertaining to 1543412Snewtondistribution of the software without specific, written prior 1643412Snewtonpermission. 1743412Snewton 1843412SnewtonLUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 1943412SnewtonINCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 2043412SnewtonIN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 2143412SnewtonSPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 2243412SnewtonWHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 2343412SnewtonIN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 2443412SnewtonARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 2543412SnewtonTHIS SOFTWARE. 2643412Snewton 2749267Snewton****************************************************************/ 2850477Speter 2943412Snewton/* Please send bug reports to 3043412Snewton David M. Gay 3143412Snewton Bell Laboratories, Room 2C-463 3243412Snewton 600 Mountain Avenue 3343412Snewton Murray Hill, NJ 07974-0636 3443412Snewton U.S.A. 3543412Snewton dmg@bell-labs.com 3643412Snewton */ 3743412Snewton 3843412Snewton#include "gdtoaimp.h" 3943412Snewton 4043412Snewton Bigint * 4143412Snewton#ifdef KR_headers 4243412Snewtonsum(a, b) Bigint *a; Bigint *b; 4343412Snewton#else 4443412Snewtonsum(Bigint *a, Bigint *b) 4543412Snewton#endif 4643412Snewton{ 4743412Snewton Bigint *c; 4843412Snewton ULong carry, *xc, *xa, *xb, *xe, y; 4943412Snewton#ifdef Pack_32 5043412Snewton ULong z; 5143412Snewton#endif 5243412Snewton 5343412Snewton if (a->wds < b->wds) { 5443412Snewton c = b; b = a; a = c; 5543412Snewton } 5643412Snewton c = Balloc(a->k); 5743412Snewton c->wds = a->wds; 5843412Snewton carry = 0; 5943412Snewton xa = a->x; 6043412Snewton xb = b->x; 6143412Snewton xc = c->x; 6243412Snewton xe = xc + b->wds; 6343412Snewton#ifdef Pack_32 6443412Snewton do { 6543412Snewton y = (*xa & 0xffff) + (*xb & 0xffff) + carry; 6643412Snewton carry = (y & 0x10000) >> 16; 6743412Snewton z = (*xa++ >> 16) + (*xb++ >> 16) + carry; 6843412Snewton carry = (z & 0x10000) >> 16; 6943412Snewton Storeinc(xc, z, y); 7043412Snewton } 7143412Snewton while(xc < xe); 7243412Snewton xe += a->wds - b->wds; 7343412Snewton while(xc < xe) { 7443412Snewton y = (*xa & 0xffff) + carry; 75 carry = (y & 0x10000) >> 16; 76 z = (*xa++ >> 16) + carry; 77 carry = (z & 0x10000) >> 16; 78 Storeinc(xc, z, y); 79 } 80#else 81 do { 82 y = *xa++ + *xb++ + carry; 83 carry = (y & 0x10000) >> 16; 84 *xc++ = y & 0xffff; 85 } 86 while(xc < xe); 87 xe += a->wds - b->wds; 88 while(xc < xe) { 89 y = *xa++ + carry; 90 carry = (y & 0x10000) >> 16; 91 *xc++ = y & 0xffff; 92 } 93#endif 94 if (carry) { 95 if (c->wds == c->maxwds) { 96 b = Balloc(c->k + 1); 97 Bcopy(b, c); 98 Bfree(c); 99 c = b; 100 } 101 c->x[c->wds++] = 1; 102 } 103 return c; 104 } 105