Jan Breuer
2016-02-20 ca33af45434be98d3c412e93fb6768aafa418158
Optimize #49 memory consumption of double to string conversion
1个文件已修改
49 ■■■■■ 已修改文件
libscpi/src/utils.c 49 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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];