Plan 9 from Bell Labs’s /usr/web/sources/plan9/sys/src/libmp/port/mpdigdiv.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include "os.h"
#include <mp.h>
#include "dat.h"

//
//	divide two digits by one and return quotient
//
void
mpdigdiv(mpdigit *dividend, mpdigit divisor, mpdigit *quotient)
{
	mpdigit hi, lo, q, x, y;
	int i;

	hi = dividend[1];
	lo = dividend[0];

	// return highest digit value if the result >= 2**32
	if(hi >= divisor || divisor == 0){
		divisor = 0;
		*quotient = ~divisor;
		return;
	}

	// at this point we know that hi < divisor
	// just shift and subtract till we're done
	q = 0;
	x = divisor;
	for(i = Dbits-1; hi > 0 && i >= 0; i--){
		x >>= 1;
		if(x > hi)
			continue;
		y = divisor<<i;
		if(x == hi && y > lo)
			continue;
		if(y > lo)
			hi--;
		lo -= y;
		hi -= x;
		q |= 1<<i;
	}
	q += lo/divisor;
	*quotient = q;
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.