1 | /*
|
---|
2 | =============================================================================
|
---|
3 | dec2fr.c -- Buchla 700 fractional 2's complement conversion functions
|
---|
4 | Version 10 -- 1987-12-06 -- D.N. Lynx Crowe
|
---|
5 | =============================================================================
|
---|
6 | */
|
---|
7 |
|
---|
8 | #define TESTER 0
|
---|
9 |
|
---|
10 | #include "stddefs.h"
|
---|
11 |
|
---|
12 | #define SFACT 1000L /* scale factor */
|
---|
13 |
|
---|
14 | static long d2f[][10] = { /* decimal to fraction table */
|
---|
15 |
|
---|
16 | {0x00000000L, 0x000019A0L, 0x00003336L, 0x00004CD0L, 0x00006668L,
|
---|
17 | 0x00008000L, 0x000099A0L, 0x0000B336L, 0x0000CCCEL, 0x0000E668L},
|
---|
18 |
|
---|
19 | {0x00000000L, 0x00000290L, 0x00000520L, 0x000007B0L, 0x00000A40L,
|
---|
20 | 0x00000CD0L, 0x00000F60L, 0x000011F0L, 0x00001480L, 0x00001710L}
|
---|
21 | };
|
---|
22 |
|
---|
23 | static long f2d[] = { /* fraction to decimal table */
|
---|
24 |
|
---|
25 | 50000L, 25000L, 12500L, 6250L,
|
---|
26 | 3125L, 1562L, 781L, 390L,
|
---|
27 | 195L, 97L, 48L, 24L,
|
---|
28 | 12L, 6L, 3L, 1L
|
---|
29 | };
|
---|
30 |
|
---|
31 | /* |
---|
32 |
|
---|
33 | */
|
---|
34 |
|
---|
35 | /*
|
---|
36 | =============================================================================
|
---|
37 | dec2fr() -- convert an ASCII decimal string to fractional binary
|
---|
38 |
|
---|
39 | The input string is 4 characters in the range 100- to 100+ inclusive.
|
---|
40 | The result is a 2's complement binary fractional value.
|
---|
41 | =============================================================================
|
---|
42 | */
|
---|
43 |
|
---|
44 | short
|
---|
45 | dec2fr(s)
|
---|
46 | register char *s;
|
---|
47 | {
|
---|
48 | register short i;
|
---|
49 |
|
---|
50 | if (s[0] EQ '1') {
|
---|
51 |
|
---|
52 | if (s[1] EQ '0') {
|
---|
53 |
|
---|
54 | if (s[2] EQ '0') {
|
---|
55 |
|
---|
56 | if (s[3] EQ '-')
|
---|
57 | return(0x8000); /* -100 */
|
---|
58 | else
|
---|
59 | return(0x7FFF); /* +100 */
|
---|
60 |
|
---|
61 | } else {
|
---|
62 |
|
---|
63 | return(0xFFFF); /* ERROR */
|
---|
64 | }
|
---|
65 |
|
---|
66 | } else {
|
---|
67 |
|
---|
68 | return(0xFFFF); /* ERROR */
|
---|
69 |
|
---|
70 | }
|
---|
71 |
|
---|
72 | /* |
---|
73 |
|
---|
74 | */
|
---|
75 |
|
---|
76 | } else if (s[0] EQ '0') {
|
---|
77 |
|
---|
78 | i = (d2f[0][s[1] - '0'] + d2f[1][s[2] - '0']) >> 1;
|
---|
79 |
|
---|
80 | if (s[3] EQ '-') {
|
---|
81 |
|
---|
82 | if (i)
|
---|
83 | return(~i); /* negative number */
|
---|
84 | else
|
---|
85 | return(0); /* zero is always 0 */
|
---|
86 |
|
---|
87 | } else {
|
---|
88 |
|
---|
89 | return(i); /* positive number */
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | return(0xFFFF); /* ERROR */
|
---|
94 | }
|
---|
95 |
|
---|
96 | /* |
---|
97 |
|
---|
98 | */
|
---|
99 |
|
---|
100 | /*
|
---|
101 | =============================================================================
|
---|
102 | fr2dec() -- convert a 2's complement fraction to decimal ASCII notation
|
---|
103 |
|
---|
104 | The input is a 2's complement binary fractional value.
|
---|
105 | The result string is 4 characters in the range 100- to 100+ inclusive.
|
---|
106 | =============================================================================
|
---|
107 | */
|
---|
108 |
|
---|
109 | char *
|
---|
110 | fr2dec(v, s)
|
---|
111 | register short v;
|
---|
112 | register char *s;
|
---|
113 | {
|
---|
114 | register long acc, sc;
|
---|
115 | register short i, p;
|
---|
116 |
|
---|
117 | if (v EQ 0x7FFF) { /* +100 */
|
---|
118 |
|
---|
119 | sprintf(s, "100+");
|
---|
120 | return(s);
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (v EQ 0x8000) { /* -100 */
|
---|
124 |
|
---|
125 | sprintf(s, "100-");
|
---|
126 | return(s);
|
---|
127 | }
|
---|
128 |
|
---|
129 | if (v & 0x8000) {
|
---|
130 |
|
---|
131 | v = ~v; /* negative number */
|
---|
132 | p = '-';
|
---|
133 |
|
---|
134 | } else {
|
---|
135 |
|
---|
136 | p = '+'; /* positive number */
|
---|
137 | }
|
---|
138 |
|
---|
139 | acc = 0;
|
---|
140 |
|
---|
141 | for (i = 0; i LT 15; i++)
|
---|
142 | if (v & (1 << (14 - i)))
|
---|
143 | acc += f2d[i];
|
---|
144 |
|
---|
145 | sc = SFACT;
|
---|
146 | acc /= sc;
|
---|
147 | sprintf(s, "%03d%c", (short)acc, p);
|
---|
148 | return(s);
|
---|
149 | }
|
---|
150 |
|
---|
151 | /* |
---|
152 |
|
---|
153 | */
|
---|
154 |
|
---|
155 | #if TESTER
|
---|
156 |
|
---|
157 | #include "stdio.h"
|
---|
158 |
|
---|
159 | extern int memcmp();
|
---|
160 |
|
---|
161 | char t[8], v[8];
|
---|
162 |
|
---|
163 | /*
|
---|
164 | =============================================================================
|
---|
165 | test program for fractional two's complement conversion functions
|
---|
166 | =============================================================================
|
---|
167 | */
|
---|
168 |
|
---|
169 | main()
|
---|
170 | {
|
---|
171 | register short i;
|
---|
172 | register short u;
|
---|
173 |
|
---|
174 | for (i = 0; i < 101; i++) {
|
---|
175 |
|
---|
176 | if (i EQ 50)
|
---|
177 | printf("\f");
|
---|
178 |
|
---|
179 | sprintf(t, "%03d+", i);
|
---|
180 | u = dec2fr(t);
|
---|
181 |
|
---|
182 | if (u EQ 0xFFFF) {
|
---|
183 |
|
---|
184 | printf("[%s] = ERROR = ?????? ", t);
|
---|
185 |
|
---|
186 | } else {
|
---|
187 |
|
---|
188 | fr2dec(u, v);
|
---|
189 | printf("[%s] = 0x%04x = [%s]", t, u, v);
|
---|
190 | }
|
---|
191 |
|
---|
192 | printf(" %s ", memcmp(t, v, 4) ? "<--" : " ");
|
---|
193 |
|
---|
194 | sprintf(t, "%03d-", i);
|
---|
195 | u = dec2fr(t);
|
---|
196 |
|
---|
197 | if (u EQ 0xFFFF) {
|
---|
198 |
|
---|
199 | printf(" [%s] = ERROR = ??????\n", t);
|
---|
200 |
|
---|
201 | } else {
|
---|
202 |
|
---|
203 | fr2dec(u, v);
|
---|
204 | printf(" [%s] = 0x%04x = [%s]", t, u, v);
|
---|
205 | }
|
---|
206 |
|
---|
207 | printf(" %s\n", memcmp(t, v, 4) ? "<--" : "");
|
---|
208 |
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | #endif
|
---|