mptouv.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include "os.h"
  10. #include <mp.h>
  11. #include "dat.h"
  12. #define VLDIGITS (sizeof(int64_t)/sizeof(mpdigit))
  13. /*
  14. * this code assumes that a vlong is an integral number of
  15. * mpdigits long.
  16. */
  17. mpint*
  18. uvtomp(uint64_t v, mpint *b)
  19. {
  20. int s;
  21. if(b == nil)
  22. b = mpnew(VLDIGITS*sizeof(mpdigit));
  23. else
  24. mpbits(b, VLDIGITS*sizeof(mpdigit));
  25. mpassign(mpzero, b);
  26. if(v == 0)
  27. return b;
  28. for(s = 0; s < VLDIGITS && v != 0; s++){
  29. b->p[s] = v;
  30. v >>= sizeof(mpdigit)*8;
  31. }
  32. b->top = s;
  33. return b;
  34. }
  35. uint64_t
  36. mptouv(mpint *b)
  37. {
  38. uint64_t v;
  39. int s;
  40. if(b->top == 0)
  41. return 0LL;
  42. mpnorm(b);
  43. if(b->top > VLDIGITS)
  44. return MAXVLONG;
  45. v = 0ULL;
  46. for(s = 0; s < b->top; s++)
  47. v |= (uint64_t)b->p[s]<<(s*sizeof(mpdigit)*8);
  48. return v;
  49. }