source: buchla-68k/ram/dec2fr.c@ 0c834c5

Last change on this file since 0c834c5 was 0580615, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Point of no return.

  • Property mode set to 100644
File size: 3.6 KB
Line 
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
14static 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
23static 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
44short dec2fr(char *s)
45{
46 register short i;
47
48 if (s[0] EQ '1') {
49
50 if (s[1] EQ '0') {
51
52 if (s[2] EQ '0') {
53
54 if (s[3] EQ '-')
55 return(0x8000); /* -100 */
56 else
57 return(0x7FFF); /* +100 */
58
59 } else {
60
61 return(0xFFFF); /* ERROR */
62 }
63
64 } else {
65
66 return(0xFFFF); /* ERROR */
67
68 }
69
70/*
71
72*/
73
74 } else if (s[0] EQ '0') {
75
76 i = (d2f[0][s[1] - '0'] + d2f[1][s[2] - '0']) >> 1;
77
78 if (s[3] EQ '-') {
79
80 if (i)
81 return(~i); /* negative number */
82 else
83 return(0); /* zero is always 0 */
84
85 } else {
86
87 return(i); /* positive number */
88 }
89 }
90
91 return(0xFFFF); /* ERROR */
92}
93
94/*
95
96*/
97
98/*
99 =============================================================================
100 fr2dec() -- convert a 2's complement fraction to decimal ASCII notation
101
102 The input is a 2's complement binary fractional value.
103 The result string is 4 characters in the range 100- to 100+ inclusive.
104 =============================================================================
105*/
106
107char *fr2dec(short v, char *s)
108{
109 register long acc, sc;
110 register short i, p;
111
112 if (v EQ 0x7FFF) { /* +100 */
113
114 sprintf(s, "100+");
115 return(s);
116 }
117
118 if (v EQ 0x8000) { /* -100 */
119
120 sprintf(s, "100-");
121 return(s);
122 }
123
124 if (v & 0x8000) {
125
126 v = ~v; /* negative number */
127 p = '-';
128
129 } else {
130
131 p = '+'; /* positive number */
132 }
133
134 acc = 0;
135
136 for (i = 0; i LT 15; i++)
137 if (v & (1 << (14 - i)))
138 acc += f2d[i];
139
140 sc = SFACT;
141 acc /= sc;
142 sprintf(s, "%03d%c", (short)acc, p);
143 return(s);
144}
145
146/*
147
148*/
149
150#if TESTER
151
152#include "stdio.h"
153
154extern int memcmp();
155
156char t[8], v[8];
157
158/*
159 =============================================================================
160 test program for fractional two's complement conversion functions
161 =============================================================================
162*/
163
164main()
165{
166 register short i;
167 register short u;
168
169 for (i = 0; i < 101; i++) {
170
171 if (i EQ 50)
172 printf("\f");
173
174 sprintf(t, "%03d+", i);
175 u = dec2fr(t);
176
177 if (u EQ 0xFFFF) {
178
179 printf("[%s] = ERROR = ?????? ", t);
180
181 } else {
182
183 fr2dec(u, v);
184 printf("[%s] = 0x%04x = [%s]", t, u, v);
185 }
186
187 printf(" %s ", memcmp(t, v, 4) ? "<--" : " ");
188
189 sprintf(t, "%03d-", i);
190 u = dec2fr(t);
191
192 if (u EQ 0xFFFF) {
193
194 printf(" [%s] = ERROR = ??????\n", t);
195
196 } else {
197
198 fr2dec(u, v);
199 printf(" [%s] = 0x%04x = [%s]", t, u, v);
200 }
201
202 printf(" %s\n", memcmp(t, v, 4) ? "<--" : "");
203
204 }
205}
206
207#endif
Note: See TracBrowser for help on using the repository browser.