OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [bootloaders/] [orpmon/] [coremark/] [cvt.c] - Blame information for rev 857

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 355 julius
//#include <math.h>
2
 
3 406 julius
double modf(double x, double *iptr);    // Our local version of modf()
4 355 julius
 
5
#define CVTBUFSIZE 80
6
static char CVTBUF[CVTBUFSIZE];
7
 
8 406 julius
static char *cvt(double arg, int ndigits, int *decpt, int *sign, char *buf,
9
                 int eflag)
10 355 julius
{
11 406 julius
        int r2;
12
        double fi, fj;
13
        char *p, *p1;
14 355 julius
 
15 406 julius
        if (ndigits < 0)
16
                ndigits = 0;
17
        if (ndigits >= CVTBUFSIZE - 1)
18
                ndigits = CVTBUFSIZE - 2;
19
        r2 = 0;
20
        *sign = 0;
21
        p = &buf[0];
22
        if (arg < 0) {
23
                *sign = 1;
24
                arg = -arg;
25
        }
26
        arg = modf(arg, &fi);
27
        p1 = &buf[CVTBUFSIZE];
28 355 julius
 
29 406 julius
        if (fi != 0) {
30
                p1 = &buf[CVTBUFSIZE];
31
                while (fi != 0) {
32
                        fj = modf(fi / 10, &fi);
33
                        *--p1 = (int)((fj + .03) * 10) + '0';
34
                        r2++;
35
                }
36
                while (p1 < &buf[CVTBUFSIZE])
37
                        *p++ = *p1++;
38
        } else if (arg > 0) {
39
                while ((fj = arg * 10) < 1) {
40
                        arg = fj;
41
                        r2--;
42
                }
43
        }
44
        p1 = &buf[ndigits];
45
        if (eflag == 0)
46
                p1 += r2;
47
        *decpt = r2;
48
        if (p1 < &buf[0]) {
49
                buf[0] = '\0';
50
                return buf;
51
        }
52
        while (p <= p1 && p < &buf[CVTBUFSIZE]) {
53
                arg *= 10;
54
                arg = modf(arg, &fj);
55
                *p++ = (int)fj + '0';
56
        }
57
        if (p1 >= &buf[CVTBUFSIZE]) {
58
                buf[CVTBUFSIZE - 1] = '\0';
59
                return buf;
60
        }
61
        p = p1;
62
        *p1 += 5;
63
        while (*p1 > '9') {
64
                *p1 = '0';
65
                if (p1 > buf)
66
                        ++ * --p1;
67
                else {
68
                        *p1 = '1';
69
                        (*decpt)++;
70
                        if (eflag == 0) {
71
                                if (p > buf)
72
                                        *p = '0';
73
                                p++;
74
                        }
75
                }
76
        }
77
        *p = '\0';
78
        return buf;
79 355 julius
}
80
 
81
char *ecvt(double arg, int ndigits, int *decpt, int *sign)
82
{
83 406 julius
        return cvt(arg, ndigits, decpt, sign, CVTBUF, 1);
84 355 julius
}
85
 
86
char *ecvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf)
87
{
88 406 julius
        return cvt(arg, ndigits, decpt, sign, buf, 1);
89 355 julius
}
90
 
91
char *fcvt(double arg, int ndigits, int *decpt, int *sign)
92
{
93 406 julius
        return cvt(arg, ndigits, decpt, sign, CVTBUF, 0);
94 355 julius
}
95
 
96
char *fcvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf)
97
{
98 406 julius
        return cvt(arg, ndigits, decpt, sign, buf, 0);
99 355 julius
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.