plan9port

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

mptouv.c (991B)


      1 #include "os.h"
      2 #include <mp.h>
      3 #include "dat.h"
      4 
      5 #define VLDIGITS (sizeof(vlong)/sizeof(mpdigit))
      6 
      7 /*
      8  *  this code assumes that a vlong is an integral number of
      9  *  mpdigits long.
     10  */
     11 mpint*
     12 uvtomp(uvlong v, mpint *b)
     13 {
     14 	int s;
     15 
     16 	if(b == nil)
     17 		b = mpnew(VLDIGITS*sizeof(mpdigit));
     18 	else
     19 		mpbits(b, VLDIGITS*sizeof(mpdigit));
     20 	mpassign(mpzero, b);
     21 	if(v == 0)
     22 		return b;
     23 	for(s = 0; s < VLDIGITS && v != 0; s++){
     24 		b->p[s] = v;
     25 	/* !@*$&!@$ gcc gives warnings about the >> here
     26 	 * when running on 64-bit machines, even though
     27 	 * it's in dead code.  fake it out with two shifts.
     28 		if(sizeof(mpdigit) == sizeof(uvlong))
     29 			v = 0;
     30 		else
     31 			v >>= sizeof(mpdigit)*8;
     32 	*/
     33 		v >>= sizeof(mpdigit)*4;
     34 		v >>= sizeof(mpdigit)*4;
     35 	}
     36 	b->top = s;
     37 	return b;
     38 }
     39 
     40 uvlong
     41 mptouv(mpint *b)
     42 {
     43 	uvlong v;
     44 	int s;
     45 
     46 	if(b->top == 0)
     47 		return 0LL;
     48 
     49 	mpnorm(b);
     50 	if(b->top > VLDIGITS)
     51 		return MAXVLONG;
     52 
     53 	v = 0ULL;
     54 	for(s = 0; s < b->top; s++)
     55 		v |= (uvlong)b->p[s]<<(s*sizeof(mpdigit)*8);
     56 
     57 	return v;
     58 }