plan9port/src/libmp/port/mptouv.c

59 lines
991 B
C
Raw Normal View History

2004-03-21 14:06:38 +00:00
#include "os.h"
#include <mp.h>
#include "dat.h"
#define VLDIGITS (sizeof(vlong)/sizeof(mpdigit))
/*
* this code assumes that a vlong is an integral number of
* mpdigits long.
*/
mpint*
uvtomp(uvlong v, mpint *b)
{
int s;
if(b == nil)
b = mpnew(VLDIGITS*sizeof(mpdigit));
else
mpbits(b, VLDIGITS*sizeof(mpdigit));
mpassign(mpzero, b);
if(v == 0)
return b;
for(s = 0; s < VLDIGITS && v != 0; s++){
b->p[s] = v;
2006-04-20 21:41:16 +00:00
/* !@*$&!@$ gcc gives warnings about the >> here
* when running on 64-bit machines, even though
2006-04-20 21:41:16 +00:00
* it's in dead code. fake it out with two shifts.
2005-12-30 17:06:50 +00:00
if(sizeof(mpdigit) == sizeof(uvlong))
v = 0;
else
v >>= sizeof(mpdigit)*8;
2006-04-20 21:41:16 +00:00
*/
v >>= sizeof(mpdigit)*4;
v >>= sizeof(mpdigit)*4;
2004-03-21 14:06:38 +00:00
}
b->top = s;
return b;
}
uvlong
mptouv(mpint *b)
{
uvlong v;
int s;
if(b->top == 0)
return 0LL;
mpnorm(b);
if(b->top > VLDIGITS)
return MAXVLONG;
v = 0ULL;
for(s = 0; s < b->top; s++)
2006-03-05 20:45:56 +00:00
v |= (uvlong)b->p[s]<<(s*sizeof(mpdigit)*8);
2004-03-21 14:06:38 +00:00
return v;
}