xref: /netbsd-src/lib/libc/gdtoa/sum.c (revision ab6254493dcffa78881e2c72c5f5376a0e7f21ff)
1*ab625449Schristos /* $NetBSD: sum.c,v 1.2 2008/03/21 23:13:48 christos Exp $ */
27684d5e0Skleink 
37684d5e0Skleink /****************************************************************
47684d5e0Skleink 
57684d5e0Skleink The author of this software is David M. Gay.
67684d5e0Skleink 
77684d5e0Skleink Copyright (C) 1998 by Lucent Technologies
87684d5e0Skleink All Rights Reserved
97684d5e0Skleink 
107684d5e0Skleink Permission to use, copy, modify, and distribute this software and
117684d5e0Skleink its documentation for any purpose and without fee is hereby
127684d5e0Skleink granted, provided that the above copyright notice appear in all
137684d5e0Skleink copies and that both that the copyright notice and this
147684d5e0Skleink permission notice and warranty disclaimer appear in supporting
157684d5e0Skleink documentation, and that the name of Lucent or any of its entities
167684d5e0Skleink not be used in advertising or publicity pertaining to
177684d5e0Skleink distribution of the software without specific, written prior
187684d5e0Skleink permission.
197684d5e0Skleink 
207684d5e0Skleink LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
217684d5e0Skleink INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
227684d5e0Skleink IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
237684d5e0Skleink SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
247684d5e0Skleink WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
257684d5e0Skleink IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
267684d5e0Skleink ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
277684d5e0Skleink THIS SOFTWARE.
287684d5e0Skleink 
297684d5e0Skleink ****************************************************************/
307684d5e0Skleink 
317684d5e0Skleink /* Please send bug reports to David M. Gay (dmg at acm dot org,
327684d5e0Skleink  * with " at " changed at "@" and " dot " changed to ".").	*/
337684d5e0Skleink 
347684d5e0Skleink #include "gdtoaimp.h"
357684d5e0Skleink 
367684d5e0Skleink  Bigint *
377684d5e0Skleink #ifdef KR_headers
sum(a,b)387684d5e0Skleink sum(a, b) Bigint *a; Bigint *b;
397684d5e0Skleink #else
407684d5e0Skleink sum(Bigint *a, Bigint *b)
417684d5e0Skleink #endif
427684d5e0Skleink {
437684d5e0Skleink 	Bigint *c;
447684d5e0Skleink 	ULong carry, *xc, *xa, *xb, *xe, y;
457684d5e0Skleink #ifdef Pack_32
467684d5e0Skleink 	ULong z;
477684d5e0Skleink #endif
487684d5e0Skleink 
497684d5e0Skleink 	if (a->wds < b->wds) {
507684d5e0Skleink 		c = b; b = a; a = c;
517684d5e0Skleink 		}
527684d5e0Skleink 	c = Balloc(a->k);
53*ab625449Schristos 	if (c == NULL)
54*ab625449Schristos 		return NULL;
557684d5e0Skleink 	c->wds = a->wds;
567684d5e0Skleink 	carry = 0;
577684d5e0Skleink 	xa = a->x;
587684d5e0Skleink 	xb = b->x;
597684d5e0Skleink 	xc = c->x;
607684d5e0Skleink 	xe = xc + b->wds;
617684d5e0Skleink #ifdef Pack_32
627684d5e0Skleink 	do {
637684d5e0Skleink 		y = (*xa & 0xffff) + (*xb & 0xffff) + carry;
647684d5e0Skleink 		carry = (y & 0x10000) >> 16;
657684d5e0Skleink 		z = (*xa++ >> 16) + (*xb++ >> 16) + carry;
667684d5e0Skleink 		carry = (z & 0x10000) >> 16;
677684d5e0Skleink 		Storeinc(xc, z, y);
687684d5e0Skleink 		}
697684d5e0Skleink 		while(xc < xe);
707684d5e0Skleink 	xe += a->wds - b->wds;
717684d5e0Skleink 	while(xc < xe) {
727684d5e0Skleink 		y = (*xa & 0xffff) + carry;
737684d5e0Skleink 		carry = (y & 0x10000) >> 16;
747684d5e0Skleink 		z = (*xa++ >> 16) + carry;
757684d5e0Skleink 		carry = (z & 0x10000) >> 16;
767684d5e0Skleink 		Storeinc(xc, z, y);
777684d5e0Skleink 		}
787684d5e0Skleink #else
797684d5e0Skleink 	do {
807684d5e0Skleink 		y = *xa++ + *xb++ + carry;
817684d5e0Skleink 		carry = (y & 0x10000) >> 16;
827684d5e0Skleink 		*xc++ = y & 0xffff;
837684d5e0Skleink 		}
847684d5e0Skleink 		while(xc < xe);
857684d5e0Skleink 	xe += a->wds - b->wds;
867684d5e0Skleink 	while(xc < xe) {
877684d5e0Skleink 		y = *xa++ + carry;
887684d5e0Skleink 		carry = (y & 0x10000) >> 16;
897684d5e0Skleink 		*xc++ = y & 0xffff;
907684d5e0Skleink 		}
917684d5e0Skleink #endif
927684d5e0Skleink 	if (carry) {
937684d5e0Skleink 		if (c->wds == c->maxwds) {
947684d5e0Skleink 			b = Balloc(c->k + 1);
95*ab625449Schristos 			if (b == NULL)
96*ab625449Schristos 				return NULL;
977684d5e0Skleink 			Bcopy(b, c);
987684d5e0Skleink 			Bfree(c);
997684d5e0Skleink 			c = b;
1007684d5e0Skleink 			}
1017684d5e0Skleink 		c->x[c->wds++] = 1;
1027684d5e0Skleink 		}
1037684d5e0Skleink 	return c;
1047684d5e0Skleink 	}
105