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

Subversion Repositories steelcore

[/] [coremark/] [steel/] [cvt.c] - Blame information for rev 11

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 11 rafaelcalc
/*
2
Copyright 2018 Embedded Microprocessor Benchmark Consortium (EEMBC)
3
 
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7
 
8
    http://www.apache.org/licenses/LICENSE-2.0
9
 
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16
#include <math.h>
17
#define CVTBUFSIZE 80
18
static char CVTBUF[CVTBUFSIZE];
19
 
20
static char *
21
cvt(double arg, int ndigits, int *decpt, int *sign, char *buf, int eflag)
22
{
23
    int    r2;
24
    double fi, fj;
25
    char * p, *p1;
26
 
27
    if (ndigits < 0)
28
        ndigits = 0;
29
    if (ndigits >= CVTBUFSIZE - 1)
30
        ndigits = CVTBUFSIZE - 2;
31
    r2    = 0;
32
    *sign = 0;
33
    p     = &buf[0];
34
    if (arg < 0)
35
    {
36
        *sign = 1;
37
        arg   = -arg;
38
    }
39
    arg = modf(arg, &fi);
40
    p1  = &buf[CVTBUFSIZE];
41
 
42
    if (fi != 0)
43
    {
44
        p1 = &buf[CVTBUFSIZE];
45
        while (fi != 0)
46
        {
47
            fj    = modf(fi / 10, &fi);
48
            *--p1 = (int)((fj + .03) * 10) + '0';
49
            r2++;
50
        }
51
        while (p1 < &buf[CVTBUFSIZE])
52
            *p++ = *p1++;
53
    }
54
    else if (arg > 0)
55
    {
56
        while ((fj = arg * 10) < 1)
57
        {
58
            arg = fj;
59
            r2--;
60
        }
61
    }
62
    p1 = &buf[ndigits];
63
    if (eflag == 0)
64
        p1 += r2;
65
    *decpt = r2;
66
    if (p1 < &buf[0])
67
    {
68
        buf[0] = '\0';
69
        return buf;
70
    }
71
    while (p <= p1 && p < &buf[CVTBUFSIZE])
72
    {
73
        arg *= 10;
74
        arg  = modf(arg, &fj);
75
        *p++ = (int)fj + '0';
76
    }
77
    if (p1 >= &buf[CVTBUFSIZE])
78
    {
79
        buf[CVTBUFSIZE - 1] = '\0';
80
        return buf;
81
    }
82
    p = p1;
83
    *p1 += 5;
84
    while (*p1 > '9')
85
    {
86
        *p1 = '0';
87
        if (p1 > buf)
88
            ++*--p1;
89
        else
90
        {
91
            *p1 = '1';
92
            (*decpt)++;
93
            if (eflag == 0)
94
            {
95
                if (p > buf)
96
                    *p = '0';
97
                p++;
98
            }
99
        }
100
    }
101
    *p = '\0';
102
    return buf;
103
}
104
 
105
char *
106
ecvt(double arg, int ndigits, int *decpt, int *sign)
107
{
108
    return cvt(arg, ndigits, decpt, sign, CVTBUF, 1);
109
}
110
 
111
char *
112
ecvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf)
113
{
114
    return cvt(arg, ndigits, decpt, sign, buf, 1);
115
}
116
 
117
char *
118
fcvt(double arg, int ndigits, int *decpt, int *sign)
119
{
120
    return cvt(arg, ndigits, decpt, sign, CVTBUF, 0);
121
}
122
 
123
char *
124
fcvtbuf(double arg, int ndigits, int *decpt, int *sign, char *buf)
125
{
126
    return cvt(arg, ndigits, decpt, sign, buf, 0);
127
}

powered by: WebSVN 2.1.0

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