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

Subversion Repositories openrisc

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

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

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

powered by: WebSVN 2.1.0

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