plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

mpvecsub.c (526B)


      1 #include "os.h"
      2 #include <mp.h>
      3 #include "dat.h"
      4 
      5 /* prereq: a >= b, alen >= blen, diff has at least alen digits */
      6 void
      7 mpvecsub(mpdigit *a, int alen, mpdigit *b, int blen, mpdigit *diff)
      8 {
      9 	int i, borrow;
     10 	mpdigit x, y;
     11 
     12 	borrow = 0;
     13 	for(i = 0; i < blen; i++){
     14 		x = *a++;
     15 		y = *b++;
     16 		y += borrow;
     17 		if(y < borrow)
     18 			borrow = 1;
     19 		else
     20 			borrow = 0;
     21 		if(x < y)
     22 			borrow++;
     23 		*diff++ = x - y;
     24 	}
     25 	for(; i < alen; i++){
     26 		x = *a++;
     27 		y = x - borrow;
     28 		if(y > x)
     29 			borrow = 1;
     30 		else
     31 			borrow = 0;
     32 		*diff++ = y;
     33 	}
     34 }