xref: /plan9/sys/src/libmp/port/mptoui.c (revision 53293322fe00aa40b36db08693d8966ace41141d)
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 if(b->top > 1 || (sizeof(mpdigit) > sizeof(uint) && x > MAXUINT))
31 		x =  MAXUINT;
32 	return x;
33 }
34