ref: a9b51fb3ace7e855670e737257fd4e15344cd5ca
dir: /3rd/mp/mptov.c/
#include "platform.h" #define VLDIGITS (int)(sizeof(int64_t)/Dbytes) /* * this code assumes that a int64_t is an integral number of * mpdigits long. */ mpint* vtomp(int64_t v, mpint *b) { int s; uint64_t uv; if(b == nil){ b = mpnew(VLDIGITS*Dbits); }else mpbits(b, VLDIGITS*Dbits); b->sign = (v >> (sizeof(v)*8 - 1)) | 1; uv = v * b->sign; for(s = 0; s < VLDIGITS; s++){ b->p[s] = uv; #ifndef BITS64 uv >>= Dbits; #endif } b->top = s; return mpnorm(b); } int64_t mptov(mpint *b) { uint64_t v; uint32_t s; if(b->top == 0) return 0LL; if(b->top > VLDIGITS) return b->sign > 0 ? INT64_MAX : INT64_MIN; v = 0ULL; for(s = 0; s < b->top; s++) v |= (uint64_t)b->p[s]<<(s*Dbits); if(b->sign > 0){ if(v > INT64_MAX) v = INT64_MAX; }else{ v = v > INT64_MAX ? INT64_MIN : -(int64_t)v; } return (int64_t)v; }