plan9port

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

mptoui.c (413B)


      1 #include "os.h"
      2 #include <mp.h>
      3 #include "dat.h"
      4 
      5 /*
      6  *  this code assumes that mpdigit is at least as
      7  *  big as an int.
      8  */
      9 
     10 mpint*
     11 uitomp(uint i, mpint *b)
     12 {
     13 	if(b == nil)
     14 		b = mpnew(0);
     15 	mpassign(mpzero, b);
     16 	if(i != 0)
     17 		b->top = 1;
     18 	*b->p = i;
     19 	return b;
     20 }
     21 
     22 uint
     23 mptoui(mpint *b)
     24 {
     25 	uint x;
     26 
     27 	x = *b->p;
     28 	if(b->sign < 0){
     29 		x = 0;
     30 	} else {
     31 		if(b->top > 1 || x > MAXUINT)
     32 			x =  MAXUINT;
     33 	}
     34 	return x;
     35 }