From ca33af45434be98d3c412e93fb6768aafa418158 Mon Sep 17 00:00:00 2001
From: Jan Breuer <jan.breuer@jaybee.cz>
Date: 周六, 20 2月 2016 23:59:50 +0800
Subject: [PATCH] Optimize #49 memory consumption of double to string conversion

---
 libscpi/src/utils.c |   51 +++++++++++++++++++++++++++++----------------------
 1 files changed, 29 insertions(+), 22 deletions(-)

diff --git a/libscpi/src/utils.c b/libscpi/src/utils.c
index 6d67793..dd8e042 100644
--- a/libscpi/src/utils.c
+++ b/libscpi/src/utils.c
@@ -780,67 +780,74 @@
 // SUCH DAMAGE.
 
 static char *scpi_ecvt(double arg, int ndigits, int *decpt, int *sign, char *buf, size_t bufsize) {
-    int r2;
+    int r1, r2;
     double fi, fj;
-    char *p, *p1;
+    int w1, w2;
 
     if (ndigits < 0) ndigits = 0;
     if (ndigits >= (int) (bufsize - 1)) ndigits = bufsize - 2;
+
     r2 = 0;
     *sign = 0;
-    p = &buf[0];
+    w1 = 0;
     if (arg < 0) {
         *sign = 1;
         arg = -arg;
     }
+    frexp(arg, &r1);
     arg = modf(arg, &fi);
-    p1 = &buf[bufsize];
 
     if (fi != 0) {
-        p1 = &buf[bufsize];
+        r1 = r1 * 308 / 1024 - ndigits;
+        w2 = bufsize;
+        while (r1 > 0) {
+            fj = modf(fi / 10, &fi);
+            r2++;
+            r1--;
+        }
         while (fi != 0) {
             fj = modf(fi / 10, &fi);
-            *--p1 = (int) ((fj + .03) * 10) + '0';
+            buf[--w2] = (int) ((fj + .03) * 10) + '0';
             r2++;
         }
-        while (p1 < &buf[bufsize]) *p++ = *p1++;
+        while (w2 < (int)bufsize) buf[w1++] = buf[w2++];
     } else if (arg > 0) {
         while ((fj = arg * 10) < 1) {
             arg = fj;
             r2--;
         }
     }
-    p1 = &buf[ndigits];
+    w2 = ndigits;
     *decpt = r2;
-    if (p1 < &buf[0]) {
+    if (w2 < 0) {
         buf[0] = '\0';
         return buf;
     }
-    while (p <= p1 && p < &buf[bufsize]) {
+    while (w1 <= w2 && w1 < (int)bufsize) {
         arg *= 10;
         arg = modf(arg, &fj);
-        *p++ = (int) fj + '0';
+        buf[w1++] = (int) fj + '0';
     }
-    if (p1 >= &buf[bufsize]) {
+    if (w2 >= (int)bufsize) {
         buf[bufsize - 1] = '\0';
         return buf;
     }
-    p = p1;
-    *p1 += 5;
-    while (*p1 > '9') {
-        *p1 = '0';
-        if (p1 > buf) {
-            ++*--p1;
+    w1 = w2;
+    buf[w2] += 5;
+    while (buf[w2] > '9') {
+        buf[w2] = '0';
+        if (w2 > 0) {
+            ++buf[--w2];
         } else {
-            *p1 = '1';
+            buf[w2] = '1';
             (*decpt)++;
         }
     }
-    *p = '\0';
+    buf[w1] = '\0';
     return buf;
 }
 
-#define SCPI_DTOSTRE_BUFFER_SIZE 310
+#define SCPI_DTOSTRE_BUFFER_SIZE 32
 
 char * SCPI_dtostre(double __val, char * __s, size_t __ssize, unsigned char __prec, unsigned char __flags) {
     char buffer[SCPI_DTOSTRE_BUFFER_SIZE];
@@ -974,4 +981,4 @@
             ((val & 0x0000FF0000000000ul) >> 24) |
             ((val & 0x00FF000000000000ul) >> 40) |
             ((val & 0xFF00000000000000ul) >> 56);
-}
\ No newline at end of file
+}

--
Gitblit v1.9.1