Skip to content

Commit 21d7826

Browse files
Luis Piresdgibson
authored andcommitted
libdecnumber: Introduce decNumberIntegralToInt128
This will be used to implement PowerPC's dctfixqq. Signed-off-by: Luis Pires <[email protected]> Reviewed-by: Richard Henderson <[email protected]> Message-Id: <[email protected]> Signed-off-by: David Gibson <[email protected]>
1 parent e06049f commit 21d7826

File tree

4 files changed

+102
-4
lines changed

4 files changed

+102
-4
lines changed

include/libdecnumber/decNumber.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@
124124
uint32_t decNumberToUInt32(const decNumber *, decContext *);
125125
int32_t decNumberToInt32(const decNumber *, decContext *);
126126
int64_t decNumberIntegralToInt64(const decNumber *dn, decContext *set);
127+
void decNumberIntegralToInt128(const decNumber *dn, decContext *set,
128+
uint64_t *plow, uint64_t *phigh);
127129
uint8_t * decNumberGetBCD(const decNumber *, uint8_t *);
128130
decNumber * decNumberSetBCD(decNumber *, const uint8_t *, uint32_t);
129131

include/libdecnumber/decNumberLocal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898

9999
/* Shared lookup tables */
100100
extern const uByte DECSTICKYTAB[10]; /* re-round digits if sticky */
101-
extern const uLong DECPOWERS[19]; /* powers of ten table */
101+
extern const uLong DECPOWERS[20]; /* powers of ten table */
102102
/* The following are included from decDPD.h */
103103
extern const uShort DPD2BIN[1024]; /* DPD -> 0-999 */
104104
extern const uShort BIN2DPD[1000]; /* 0-999 -> DPD */

libdecnumber/decContext.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,13 @@ static const Flag *mfctop=(Flag *)&mfcone; /* -> top byte */
5353
const uByte DECSTICKYTAB[10]={1,1,2,3,4,6,6,7,8,9}; /* used if sticky */
5454

5555
/* ------------------------------------------------------------------ */
56-
/* Powers of ten (powers[n]==10**n, 0<=n<=9) */
56+
/* Powers of ten (powers[n]==10**n, 0<=n<=19) */
5757
/* ------------------------------------------------------------------ */
58-
const uLong DECPOWERS[19] = {1, 10, 100, 1000, 10000, 100000, 1000000,
58+
const uLong DECPOWERS[20] = {1, 10, 100, 1000, 10000, 100000, 1000000,
5959
10000000, 100000000, 1000000000, 10000000000ULL, 100000000000ULL,
6060
1000000000000ULL, 10000000000000ULL, 100000000000000ULL, 1000000000000000ULL,
61-
10000000000000000ULL, 100000000000000000ULL, 1000000000000000000ULL, };
61+
10000000000000000ULL, 100000000000000000ULL, 1000000000000000000ULL,
62+
10000000000000000000ULL,};
6263

6364
/* ------------------------------------------------------------------ */
6465
/* decContextClearStatus -- clear bits in current status */

libdecnumber/decNumber.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ static decNumber * decTrim(decNumber *, decContext *, Flag, Int *);
264264
static Int decUnitAddSub(const Unit *, Int, const Unit *, Int, Int,
265265
Unit *, Int);
266266
static Int decUnitCompare(const Unit *, Int, const Unit *, Int, Int);
267+
static bool mulUInt128ByPowOf10(uLong *, uLong *, uInt);
267268

268269
#if !DECSUBSET
269270
/* decFinish == decFinalize when no subset arithmetic needed */
@@ -542,6 +543,68 @@ int64_t decNumberIntegralToInt64(const decNumber *dn, decContext *set)
542543
return 0;
543544
} /* decNumberIntegralToInt64 */
544545

546+
/* ------------------------------------------------------------------ */
547+
/* decNumberIntegralToInt128 -- conversion to int128 */
548+
/* */
549+
/* dn is the decNumber to convert. dn is assumed to have been */
550+
/* rounded to a floating point integer value. */
551+
/* set is the context for reporting errors */
552+
/* returns the converted decNumber via plow and phigh */
553+
/* */
554+
/* Invalid is set if the decNumber is a NaN, Infinite or is out of */
555+
/* range for a signed 128 bit integer. */
556+
/* ------------------------------------------------------------------ */
557+
558+
void decNumberIntegralToInt128(const decNumber *dn, decContext *set,
559+
uint64_t *plow, uint64_t *phigh)
560+
{
561+
int d; /* work */
562+
const Unit *up; /* .. */
563+
uint64_t lo = 0, hi = 0;
564+
565+
if (decNumberIsSpecial(dn) || (dn->exponent < 0) ||
566+
(dn->digits + dn->exponent > 39)) {
567+
goto Invalid;
568+
}
569+
570+
up = dn->lsu; /* -> lsu */
571+
572+
for (d = (dn->digits - 1) / DECDPUN; d >= 0; d--) {
573+
if (mulu128(&lo, &hi, DECDPUNMAX + 1)) {
574+
/* overflow */
575+
goto Invalid;
576+
}
577+
if (uadd64_overflow(lo, up[d], &lo)) {
578+
if (uadd64_overflow(hi, 1, &hi)) {
579+
/* overflow */
580+
goto Invalid;
581+
}
582+
}
583+
}
584+
585+
if (mulUInt128ByPowOf10(&lo, &hi, dn->exponent)) {
586+
/* overflow */
587+
goto Invalid;
588+
}
589+
590+
if (decNumberIsNegative(dn)) {
591+
if (lo == 0) {
592+
*phigh = -hi;
593+
*plow = 0;
594+
} else {
595+
*phigh = ~hi;
596+
*plow = -lo;
597+
}
598+
} else {
599+
*plow = lo;
600+
*phigh = hi;
601+
}
602+
603+
return;
604+
605+
Invalid:
606+
decContextSetStatus(set, DEC_Invalid_operation);
607+
} /* decNumberIntegralToInt128 */
545608

546609
/* ------------------------------------------------------------------ */
547610
/* to-scientific-string -- conversion to numeric string */
@@ -7885,6 +7948,38 @@ static Int decGetDigits(Unit *uar, Int len) {
78857948
return digits;
78867949
} /* decGetDigits */
78877950

7951+
/* ------------------------------------------------------------------ */
7952+
/* mulUInt128ByPowOf10 -- multiply a 128-bit unsigned integer by a */
7953+
/* power of 10. */
7954+
/* */
7955+
/* The 128-bit factor composed of plow and phigh is multiplied */
7956+
/* by 10^exp. */
7957+
/* */
7958+
/* plow pointer to the low 64 bits of the first factor */
7959+
/* phigh pointer to the high 64 bits of the first factor */
7960+
/* exp the exponent of the power of 10 of the second factor */
7961+
/* */
7962+
/* If the result fits in 128 bits, returns false and the */
7963+
/* multiplication result through plow and phigh. */
7964+
/* Otherwise, returns true. */
7965+
/* ------------------------------------------------------------------ */
7966+
static bool mulUInt128ByPowOf10(uLong *plow, uLong *phigh, uInt pow10)
7967+
{
7968+
while (pow10 >= ARRAY_SIZE(powers)) {
7969+
if (mulu128(plow, phigh, powers[ARRAY_SIZE(powers) - 1])) {
7970+
/* Overflow */
7971+
return true;
7972+
}
7973+
pow10 -= ARRAY_SIZE(powers) - 1;
7974+
}
7975+
7976+
if (pow10 > 0) {
7977+
return mulu128(plow, phigh, powers[pow10]);
7978+
} else {
7979+
return false;
7980+
}
7981+
}
7982+
78887983
#if DECTRACE | DECCHECK
78897984
/* ------------------------------------------------------------------ */
78907985
/* decNumberShow -- display a number [debug aid] */

0 commit comments

Comments
 (0)