xref: /plan9/sys/src/cmd/unix/drawterm/libmp/mptoui.c (revision 8ccd4a6360d974db7bd7bbd4f37e7018419ea908)
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*
uitomp(uint i,mpint * b)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
mptoui(mpint * b)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 }
36