return 0;
} /* decNumberToUInt32 */
+decNumber *decNumberFromInt64(decNumber *dn, int64_t in)
+{
+ uint64_t unsig = in;
+ if (in < 0) {
+ unsig = -unsig;
+ }
+
+ decNumberFromUInt64(dn, unsig);
+ if (in < 0) {
+ dn->bits = DECNEG; /* sign needed */
+ }
+ return dn;
+} /* decNumberFromInt64 */
+
+decNumber *decNumberFromUInt64(decNumber *dn, uint64_t uin)
+{
+ Unit *up; /* work pointer */
+ decNumberZero(dn); /* clean */
+ if (uin == 0) {
+ return dn; /* [or decGetDigits bad call] */
+ }
+ for (up = dn->lsu; uin > 0; up++) {
+ *up = (Unit)(uin % (DECDPUNMAX + 1));
+ uin = uin / (DECDPUNMAX + 1);
+ }
+ dn->digits = decGetDigits(dn->lsu, up-dn->lsu);
+ return dn;
+} /* decNumberFromUInt64 */
+
+/* ------------------------------------------------------------------ */
+/* to-int64 -- conversion to int64 */
+/* */
+/* dn is the decNumber to convert. dn is assumed to have been */
+/* rounded to a floating point integer value. */
+/* set is the context for reporting errors */
+/* returns the converted decNumber, or 0 if Invalid is set */
+/* */
+/* Invalid is set if the decNumber is a NaN, Infinite or is out of */
+/* range for a signed 64 bit integer. */
+/* ------------------------------------------------------------------ */
+
+int64_t decNumberIntegralToInt64(const decNumber *dn, decContext *set)
+{
+ if (decNumberIsSpecial(dn) || (dn->exponent < 0) ||
+ (dn->digits + dn->exponent > 19)) {
+ goto Invalid;
+ } else {
+ int64_t d; /* work */
+ const Unit *up; /* .. */
+ uint64_t hi = 0;
+ up = dn->lsu; /* -> lsu */
+
+ for (d = 1; d <= dn->digits; up++, d += DECDPUN) {
+ uint64_t prev = hi;
+ hi += *up * powers[d-1];
+ if ((hi < prev) || (hi > INT64_MAX)) {
+ goto Invalid;
+ }
+ }
+
+ uint64_t prev = hi;
+ hi *= (uint64_t)powers[dn->exponent];
+ if ((hi < prev) || (hi > INT64_MAX)) {
+ goto Invalid;
+ }
+ return (decNumberIsNegative(dn)) ? -((int64_t)hi) : (int64_t)hi;
+ }
+
+Invalid:
+ decContextSetStatus(set, DEC_Invalid_operation);
+ return 0;
+} /* decNumberIntegralToInt64 */
+
+
/* ------------------------------------------------------------------ */
/* to-scientific-string -- conversion to numeric string */
/* to-engineering-string -- conversion to numeric string */
/* and bcd[0] zero. */
/* ------------------------------------------------------------------ */
decNumber * decNumberSetBCD(decNumber *dn, const uByte *bcd, uInt n) {
- Unit *up=dn->lsu+D2U(dn->digits)-1; /* -> msu [target pointer] */
+ Unit *up = dn->lsu + D2U(n) - 1; /* -> msu [target pointer] */
const uByte *ub=bcd; /* -> source msd */
#if DECDPUN==1 /* trivial simple copy */
uByte bits; /* working sign */
Unit *target; /* work */
const Unit *source; /* .. */
- uInt const *pow; /* .. */
+ uLong const *pow; /* .. */
Int shift, cut; /* .. */
#if DECSUBSET
Int dropped; /* work */
/* 4. The working precisions for the static buffers are twice the */
/* obvious size to allow for calls from decNumberPower. */
/* ------------------------------------------------------------------ */
-decNumber * decExpOp(decNumber *res, const decNumber *rhs,
- decContext *set, uInt *status) {
+static decNumber *decExpOp(decNumber *res, const decNumber *rhs,
+ decContext *set, uInt *status) {
uInt ignore=0; /* working status */
Int h; /* adjusted exponent for 0.xxxx */
Int p; /* working precision */
/* where x is truncated (NB) into the range 10 through 99, */
/* and then c = k>>2 and e = k&3. */
/* ------------------------------------------------------------------ */
-const uShort LNnn[90]={9016, 8652, 8316, 8008, 7724, 7456, 7208,
+static const uShort LNnn[90] = {
+ 9016, 8652, 8316, 8008, 7724, 7456, 7208,
6972, 6748, 6540, 6340, 6148, 5968, 5792, 5628, 5464, 5312,
5164, 5020, 4884, 4748, 4620, 4496, 4376, 4256, 4144, 4032,
39233, 38181, 37157, 36157, 35181, 34229, 33297, 32389, 31501, 30629,
/* 5. The static buffers are larger than might be expected to allow */
/* for calls from decNumberPower. */
/* ------------------------------------------------------------------ */
-decNumber * decLnOp(decNumber *res, const decNumber *rhs,
- decContext *set, uInt *status) {
+static decNumber *decLnOp(decNumber *res, const decNumber *rhs,
+ decContext *set, uInt *status) {
uInt ignore=0; /* working status accumulator */
uInt needbytes; /* for space calculations */
Int residue; /* rounding residue */
/* The emphasis here is on speed for common cases, and avoiding */
/* coefficient comparison if possible. */
/* ------------------------------------------------------------------ */
-decNumber * decCompareOp(decNumber *res, const decNumber *lhs,
- const decNumber *rhs, decContext *set,
- Flag op, uInt *status) {
+static decNumber *decCompareOp(decNumber *res, const decNumber *lhs,
+ const decNumber *rhs, decContext *set,
+ Flag op, uInt *status) {
#if DECSUBSET
decNumber *alloclhs=NULL; /* non-NULL if rounded lhs allocated */
decNumber *allocrhs=NULL; /* .., rhs */
/* If total ordering then handle differing signs 'up front' */
if (op==COMPTOTAL) { /* total ordering */
- if (decNumberIsNegative(lhs) & !decNumberIsNegative(rhs)) {
+ if (decNumberIsNegative(lhs) && !decNumberIsNegative(rhs)) {
result=-1;
break;
}
- if (!decNumberIsNegative(lhs) & decNumberIsNegative(rhs)) {
+ if (!decNumberIsNegative(lhs) && decNumberIsNegative(rhs)) {
result=+1;
break;
}
/* ------------------------------------------------------------------ */
static void decSetSubnormal(decNumber *dn, decContext *set, Int *residue,
uInt *status) {
- Int dnexp; /* saves original exponent */
decContext workset; /* work */
Int etiny, adjust; /* .. */
/* adjust>0, so need to rescale the result so exponent becomes Etiny */
/* [this code is similar to that in rescale] */
- dnexp=dn->exponent; /* save exponent */
workset=*set; /* clone rounding, etc. */
workset.digits=dn->digits-adjust; /* set requested length */
workset.emin-=adjust; /* and adjust emin to match */