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

Subversion Repositories wdsp

[/] [wdsp/] [trunk/] [sw/] [IIR6/] [int2str.c] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 parrado
#include "../support/support.h"
2
#include "../support/board.h"
3
#include "../support/uart.h"
4
 
5
#include "../support/spr_defs.h"
6
 
7
/*
8
  Defines: int2str(), itoa(), ltoa()
9
 
10
  int2str(dst, radix, val)
11
  converts the (long) integer "val" to character form and moves it to
12
  the destination string "dst" followed by a terminating NUL.  The
13
  result is normally a pointer to this NUL character, but if the radix
14
  is dud the result will be NullS and nothing will be changed.
15
 
16
  If radix is -2..-36, val is taken to be SIGNED.
17
  If radix is  2.. 36, val is taken to be UNSIGNED.
18
  That is, val is signed if and only if radix is.  You will normally
19
  use radix -10 only through itoa and ltoa, for radix 2, 8, or 16
20
  unsigned is what you generally want.
21
 
22
  _dig_vec is public just in case someone has a use for it.
23
  The definitions of itoa and ltoa are actually macros in m_string.h,
24
  but this is where the code is.
25
 
26
  Note: The standard itoa() returns a pointer to the argument, when int2str
27
        returns the pointer to the end-null.
28
        itoa assumes that 10 -base numbers are allways signed and other arn't.
29
*/
30
 
31
//#include <global.h>
32
//#include "m_string.h"
33
 
34
char  _dig_vec[] =
35
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
36
 
37
 
38
char *int2str(register long int val, register char *dst, register int radix)
39
{
40
  char buffer[65];
41
  register char *p;
42
  long int new_val;
43
 
44
  if (radix < 0) {
45
    if (radix < -36 || radix > -2) return NULL;
46
    if (val < 0) {
47
      *dst++ = '-';
48
      val = -val;
49
    }
50
    radix = -radix;
51
  } else {
52
    if (radix > 36 || radix < 2) return NULL;
53
  }
54
  /*  The slightly contorted code which follows is due to the
55
      fact that few machines directly support unsigned long / and %.
56
      Certainly the VAX C compiler generates a subroutine call.  In
57
      the interests of efficiency (hollow laugh) I let this happen
58
      for the first digit only; after that "val" will be in range so
59
      that signed integer division will do.  Sorry 'bout that.
60
      CHECK THE CODE PRODUCED BY YOUR C COMPILER.  The first % and /
61
      should be unsigned, the second % and / signed, but C compilers
62
      tend to be extraordinarily sensitive to minor details of style.
63
      This works on a VAX, that's all I claim for it.
64
      */
65
  p = &buffer[sizeof(buffer)-1];
66
  *p = '\0';
67
  new_val=(unsigned long) val / (unsigned long) radix;
68
  *--p = _dig_vec[(unsigned char) ((unsigned long) val- (unsigned long) new_val*(unsigned long) radix)];
69
  val = new_val;
70
#ifdef HAVE_LDIV
71
  while (val != 0)
72
  {
73
    ldiv_t res;
74
    res=ldiv(val,radix);
75
    *--p = _dig_vec[res.rem];
76
    val= res.quot;
77
  }
78
#else
79
  while (val != 0)
80
  {
81
    new_val=val/radix;
82
    *--p = _dig_vec[(unsigned char) (val-new_val*radix)];
83
    val= new_val;
84
  }
85
#endif
86
  while ((*dst++ = *p++) != 0) ;
87
  return dst-1;
88
}
89
 
90
 
91
/*
92
  This is a faster version of the above optimized for the normal case of
93
   radix 10 / -10
94
*/
95
 
96
char *int10_to_str(long int val,char *dst,int radix)
97
{
98
  char buffer[65];
99
  register char *p;
100
  long int new_val;
101
 
102
  if (radix < 0)                         /* -10 */
103
  {
104
    if (val < 0)
105
    {
106
      *dst++ = '-';
107
      val = -val;
108
    }
109
  }
110
 
111
  p = &buffer[sizeof(buffer)-1];
112
  *p = '\0';
113
  new_val= (long) ((unsigned long int) val / 10);
114
  *--p = '0'+ (char) ((unsigned long int) val - (unsigned long) new_val * 10);
115
  val = new_val;
116
 
117
  while (val != 0)
118
  {
119
    new_val=val/10;
120
    *--p = '0' + (char) (val-new_val*10);
121
    val= new_val;
122
  }
123
  while ((*dst++ = *p++) != 0) ;
124
  return dst-1;
125
}
126
 
127
 
128
 
129
 
130
 

powered by: WebSVN 2.1.0

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