All of lore.kernel.org
 help / color / mirror / Atom feed
From: Luis Pires <luis.pires@eldorado.org.br>
To: qemu-devel@nongnu.org, qemu-ppc@nongnu.org
Cc: Luis Pires <luis.pires@eldorado.org.br>,
	richard.henderson@linaro.org, groug@kaod.org,
	david@gibson.dropbear.id.au
Subject: [PATCH 11/19] libdecnumber: Introduce decNumberIntegralToInt128
Date: Tue, 24 Aug 2021 11:27:22 -0300	[thread overview]
Message-ID: <20210824142730.102421-12-luis.pires@eldorado.org.br> (raw)
In-Reply-To: <20210824142730.102421-1-luis.pires@eldorado.org.br>

This will be used to implement PowerPC's dctfixqq.

Signed-off-by: Luis Pires <luis.pires@eldorado.org.br>
---
 include/libdecnumber/decNumber.h      |  2 +
 include/libdecnumber/decNumberLocal.h |  2 +-
 libdecnumber/decContext.c             |  7 +-
 libdecnumber/decNumber.c              | 94 +++++++++++++++++++++++++++
 4 files changed, 101 insertions(+), 4 deletions(-)

diff --git a/include/libdecnumber/decNumber.h b/include/libdecnumber/decNumber.h
index 0cf69c7db2..41bc2a0d36 100644
--- a/include/libdecnumber/decNumber.h
+++ b/include/libdecnumber/decNumber.h
@@ -124,6 +124,8 @@
   uint32_t    decNumberToUInt32(const decNumber *, decContext *);
   int32_t     decNumberToInt32(const decNumber *, decContext *);
   int64_t     decNumberIntegralToInt64(const decNumber *dn, decContext *set);
+  void        decNumberIntegralToInt128(const decNumber *dn, decContext *set,
+        uint64_t *plow, uint64_t *phigh);
   uint8_t   * decNumberGetBCD(const decNumber *, uint8_t *);
   decNumber * decNumberSetBCD(decNumber *, const uint8_t *, uint32_t);
 
diff --git a/include/libdecnumber/decNumberLocal.h b/include/libdecnumber/decNumberLocal.h
index 4d53c077f2..6198ca8593 100644
--- a/include/libdecnumber/decNumberLocal.h
+++ b/include/libdecnumber/decNumberLocal.h
@@ -98,7 +98,7 @@
 
   /* Shared lookup tables					      */
   extern const uByte  DECSTICKYTAB[10]; /* re-round digits if sticky  */
-  extern const uLong  DECPOWERS[19];    /* powers of ten table        */
+  extern const uLong  DECPOWERS[20];    /* powers of ten table        */
   /* The following are included from decDPD.h			      */
   extern const uShort DPD2BIN[1024];	/* DPD -> 0-999		      */
   extern const uShort BIN2DPD[1000];	/* 0-999 -> DPD		      */
diff --git a/libdecnumber/decContext.c b/libdecnumber/decContext.c
index 7d97a65ac5..1956edf0a7 100644
--- a/libdecnumber/decContext.c
+++ b/libdecnumber/decContext.c
@@ -53,12 +53,13 @@ static	const  Flag *mfctop=(Flag *)&mfcone; /* -> top byte */
 const uByte DECSTICKYTAB[10]={1,1,2,3,4,6,6,7,8,9}; /* used if sticky */
 
 /* ------------------------------------------------------------------ */
-/* Powers of ten (powers[n]==10**n, 0<=n<=9)			      */
+/* Powers of ten (powers[n]==10**n, 0<=n<=19)                         */
 /* ------------------------------------------------------------------ */
-const uLong DECPOWERS[19] = {1, 10, 100, 1000, 10000, 100000, 1000000,
+const uLong DECPOWERS[20] = {1, 10, 100, 1000, 10000, 100000, 1000000,
   10000000, 100000000, 1000000000, 10000000000ULL, 100000000000ULL,
   1000000000000ULL, 10000000000000ULL, 100000000000000ULL, 1000000000000000ULL,
-  10000000000000000ULL, 100000000000000000ULL, 1000000000000000000ULL, };
+  10000000000000000ULL, 100000000000000000ULL, 1000000000000000000ULL,
+  10000000000000000000ULL,};
 
 /* ------------------------------------------------------------------ */
 /* decContextClearStatus -- clear bits in current status	      */
diff --git a/libdecnumber/decNumber.c b/libdecnumber/decNumber.c
index 75c09ba052..4474f0dd11 100644
--- a/libdecnumber/decNumber.c
+++ b/libdecnumber/decNumber.c
@@ -264,6 +264,7 @@ static decNumber * decTrim(decNumber *, decContext *, Flag, Int *);
 static Int	   decUnitAddSub(const Unit *, Int, const Unit *, Int, Int,
 			      Unit *, Int);
 static Int	   decUnitCompare(const Unit *, Int, const Unit *, Int, Int);
+static bool        mulUInt128ByPowOf10(uLong *, uLong *, uInt);
 
 #if !DECSUBSET
 /* decFinish == decFinalize when no subset arithmetic needed */
@@ -542,6 +543,67 @@ Invalid:
     return 0;
 } /* decNumberIntegralToInt64 */
 
+/* ------------------------------------------------------------------ */
+/* decNumberIntegralToInt128 -- conversion to int128                  */
+/*                                                                    */
+/*  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 via plow and phigh                */
+/*                                                                    */
+/* Invalid is set if the decNumber is a NaN, Infinite or is out of    */
+/* range for a signed 128 bit integer.                                */
+/* ------------------------------------------------------------------ */
+
+void decNumberIntegralToInt128(const decNumber *dn, decContext *set,
+        uint64_t *plow, uint64_t *phigh)
+{
+    if (decNumberIsSpecial(dn) || (dn->exponent < 0) ||
+       (dn->digits + dn->exponent > 39)) {
+        goto Invalid;
+    } else {
+        int d;        /* work */
+        const Unit *up;   /* .. */
+        uint64_t lo = 0, hi = 0;
+        up = dn->lsu;     /* -> lsu */
+
+        for (d = (dn->digits - 1) / DECDPUN; d >= 0; d--) {
+            if (mulu128(&lo, &hi, DECDPUNMAX + 1)) {
+                /* overflow */
+                goto Invalid;
+            }
+            if (uadd64_overflow(lo, up[d], &lo)) {
+                if (uadd64_overflow(hi, 1, &hi)) {
+                    /* overflow */
+                    goto Invalid;
+                }
+            }
+        }
+
+        if (mulUInt128ByPowOf10(&lo, &hi, dn->exponent)) {
+            /* overflow */
+            goto Invalid;
+        }
+
+        if (decNumberIsNegative(dn)) {
+            if (lo == 0) {
+                *phigh = -hi;
+                *plow = 0;
+            } else {
+                *phigh = ~hi;
+                *plow = -lo;
+            }
+        } else {
+            *plow = lo;
+            *phigh = hi;
+        }
+
+        return;
+    }
+
+Invalid:
+    decContextSetStatus(set, DEC_Invalid_operation);
+} /* decNumberIntegralToInt128 */
 
 /* ------------------------------------------------------------------ */
 /* to-scientific-string -- conversion to numeric string		      */
@@ -7885,6 +7947,38 @@ static Int decGetDigits(Unit *uar, Int len) {
   return digits;
   } /* decGetDigits */
 
+/* ------------------------------------------------------------------ */
+/* mulUInt128ByPowOf10 -- multiply a 128-bit unsigned integer by a    */
+/* power of 10.                                                       */
+/*                                                                    */
+/*   The 128-bit factor composed of plow and phigh is multiplied      */
+/*   by 10^exp.                                                       */
+/*                                                                    */
+/*   plow   pointer to the low 64 bits of the first factor            */
+/*   phigh  pointer to the high 64 bits of the first factor           */
+/*   exp    the exponent of the power of 10 of the second factor      */
+/*                                                                    */
+/* If the result fits in 128 bits, returns false and the              */
+/* multiplication result through plow and phigh.                      */
+/* Otherwise, returns true.                                           */
+/* ------------------------------------------------------------------ */
+static bool mulUInt128ByPowOf10(uLong *plow, uLong *phigh, uInt pow10)
+{
+    while (pow10 >= ARRAY_SIZE(powers)) {
+        if (mulu128(plow, phigh, powers[ARRAY_SIZE(powers) - 1])) {
+            /* Overflow */
+            return true;
+        }
+        pow10 -= ARRAY_SIZE(powers) - 1;
+    }
+
+    if (pow10 > 0) {
+        return mulu128(plow, phigh, powers[pow10]);
+    } else {
+        return false;
+    }
+}
+
 #if DECTRACE | DECCHECK
 /* ------------------------------------------------------------------ */
 /* decNumberShow -- display a number [debug aid]		      */
-- 
2.25.1



  parent reply	other threads:[~2021-08-24 14:44 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-24 14:27 [PATCH 00/19] target/ppc: DFP instructions using decodetree Luis Pires
2021-08-24 14:27 ` [PATCH 01/19] host-utils: Fix overflow detection in divu128() Luis Pires
2021-08-24 14:27 ` [PATCH 02/19] host-utils: move abs64() to host-utils Luis Pires
2021-08-25  3:43   ` David Gibson
2021-08-25 12:48     ` Luis Fernando Fujita Pires
2021-08-25 20:26       ` Eduardo Habkost
2021-08-25 20:37         ` Luis Fernando Fujita Pires
2021-08-25 21:18           ` Eduardo Habkost
2021-08-25 21:27             ` Philippe Mathieu-Daudé
2021-08-27 14:28               ` Luis Fernando Fujita Pires
2021-08-24 14:27 ` [PATCH 03/19] host-utils: move checks out of divu128/divs128 Luis Pires
2021-08-24 14:27 ` [PATCH 04/19] host-utils: add 128-bit quotient support to divu128/divs128 Luis Pires
2021-08-24 14:27 ` [PATCH 05/19] host-utils: add unit tests for divu128/divs128 Luis Pires
2021-08-24 14:27 ` [PATCH 06/19] libdecnumber: introduce decNumberFrom[U]Int128 Luis Pires
2021-08-24 14:27 ` [PATCH 07/19] target/ppc: Move REQUIRE_ALTIVEC/VECTOR to translate.c Luis Pires
2021-08-25  3:46   ` David Gibson
2021-08-24 14:27 ` [PATCH 08/19] target/ppc: Introduce REQUIRE_FPU Luis Pires
2021-08-25  3:46   ` David Gibson
2021-08-24 14:27 ` [PATCH 09/19] target/ppc: Implement DCFFIXQQ Luis Pires
2021-08-24 14:27 ` [PATCH 10/19] host-utils: Introduce mulu128 Luis Pires
2021-08-24 14:27 ` Luis Pires [this message]
2021-08-24 14:27 ` [PATCH 12/19] target/ppc: Implement DCTFIXQQ Luis Pires
2021-08-24 14:27 ` [PATCH 13/19] target/ppc: Move dtstdc[q]/dtstdg[q] to decodetree Luis Pires
2021-08-25 13:15   ` Philippe Mathieu-Daudé
2021-08-24 14:27 ` [PATCH 14/19] target/ppc: Move d{add, sub, mul, div, iex}[q] " Luis Pires
2021-08-25 13:16   ` Philippe Mathieu-Daudé
2021-08-24 14:27 ` [PATCH 15/19] target/ppc: Move dcmp{u, o}[q], dts{tex, tsf, tsfi}[q] " Luis Pires
2021-08-24 14:27 ` [PATCH 16/19] target/ppc: Move dquai[q], drint{x,n}[q] " Luis Pires
2021-08-25 13:18   ` Philippe Mathieu-Daudé
2021-08-24 14:27 ` [PATCH 17/19] target/ppc: Move dqua[q], drrnd[q] " Luis Pires
2021-08-25 13:18   ` Philippe Mathieu-Daudé
2021-08-24 14:27 ` [PATCH 18/19] target/ppc: Move dct{dp, qpq}, dr{sp, dpq}, dc{f, t}fix[q], dxex[q] " Luis Pires
2021-08-25 13:19   ` Philippe Mathieu-Daudé
2021-08-24 14:27 ` [PATCH 19/19] target/ppc: Move ddedpd[q], denbcd[q], dscli[q], dscri[q] " Luis Pires
2021-08-25 13:20   ` Philippe Mathieu-Daudé

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210824142730.102421-12-luis.pires@eldorado.org.br \
    --to=luis.pires@eldorado.org.br \
    --cc=david@gibson.dropbear.id.au \
    --cc=groug@kaod.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-ppc@nongnu.org \
    --cc=richard.henderson@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.