-
Notifications
You must be signed in to change notification settings - Fork 13
/
bcd2.c
238 lines (218 loc) · 5.59 KB
/
bcd2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* bcd.c: conversion routines for multi-byte arithmetic
*
* Sccsid: @(#)bcd2.c 9.1.1.12 12/11/95 15:32:19
*
* defined routines:
* bin_bcd2(long binary, long *low_res, long *high_res)
* bcd2_bin(long *dest, long bcd)
* bcd2_add(long *bcd_low, long *bcd_high, long addend)
* bcd2_sub(long *bcd_low, long *bcd_high, long subend)
* bcd2_mul(long *bcd_low, long *bcd_high, long multiplier)
* bcd2_div(long *bcd_low, long *bcd_high, long divisor)
* long bcd2_mod(long *bcd_low, long *bcd_high, long modulo)
* long bcd2_cmp(long *bcd_low, long *bcd_high, long compare)
*/
#include <stdio.h>
#include "bcd2.h" /* for function prototypes */
#define DIGITS_PER_LONG 7
#define WORD_DIVISOR 10000000
#define GET_DIGIT(num, low, high) \
((num) >= DIGITS_PER_LONG)? \
(high & (0xF << (4 * ((num) - DIGITS_PER_LONG)))) \
>> (((num) - DIGITS_PER_LONG) * 4): \
(low & (0xF << (4 * (num)))) >> ((num) * 4)
#define SET_DIGIT(value, num, low, high) \
if ((num) >= DIGITS_PER_LONG) \
{ \
*high &= \
(0xFFFFFFF ^ (0xF << (4 * ((num) - DIGITS_PER_LONG)))); \
*high |= (value << (4 * ((num) - DIGITS_PER_LONG))); \
} \
else \
{ \
*low = (*low & (0xFFFFFFF ^ (0xF << (4 * (num))))); \
*low |= (value << (4 * (num))); \
}
int
bin_bcd2(long binary, long *low_res, long *high_res)
{
char number[15],
*current;
int count;
long *dest;
*low_res = *high_res = 0;
sprintf(number, "%014ld", binary);
for (current = number, count=13; *current; current++, count--)
{
dest = (count < DIGITS_PER_LONG)?low_res:high_res;
*dest = *dest << 4;
*dest |= *current - '0';
}
return(0);
}
int
bcd2_bin(long *dest, long bcd)
{
int count;
long mask;
count = DIGITS_PER_LONG - 1;
mask = 0xF000000;
*dest = 0;
while (mask)
{
*dest *= 10;
*dest += (bcd & mask) >> (4 * count);
mask = mask >> 4;
count -= 1;
}
return(0);
}
int
bcd2_add(long *bcd_low, long *bcd_high, long addend)
{
long tmp_lo, tmp_hi, carry, res;
int digit;
bin_bcd2(addend, &tmp_lo, &tmp_hi);
carry = 0;
for (digit=0; digit < 14; digit++)
{
res = GET_DIGIT(digit, *bcd_low, *bcd_high);
res += GET_DIGIT(digit, tmp_lo, tmp_hi);
res += carry;
carry = res / 10;
res %= 10;
SET_DIGIT(res, digit, bcd_low, bcd_high);
}
return(carry);
}
int
bcd2_sub(long *bcd_low, long *bcd_high, long subend)
{
long tmp_lo, tmp_hi, carry, res;
int digit;
bin_bcd2(subend, &tmp_lo, &tmp_hi);
carry = 0;
for (digit=0; digit < 14; digit++)
{
res = GET_DIGIT(digit, *bcd_low, *bcd_high);
res -= GET_DIGIT(digit, tmp_lo, tmp_hi);
res -= carry;
if (res < 0)
{
res += 10;
carry = 1;
}
SET_DIGIT(res, digit, bcd_low, bcd_high);
}
return(carry);
}
int
bcd2_mul(long *bcd_low, long *bcd_high, long multiplier)
{
long tmp_lo, tmp_hi, carry, m_lo, m_hi, m1, m2;
int udigit, ldigit, res;
tmp_lo = *bcd_low;
tmp_hi = *bcd_high;
bin_bcd2(multiplier, &m_lo, &m_hi);
*bcd_low = 0;
*bcd_high = 0;
carry = 0;
for (ldigit=0; ldigit < 14; ldigit++)
{
m1 = GET_DIGIT(ldigit, m_lo, m_hi);
carry = 0;
for (udigit=0; udigit < 14; udigit++)
{
m2 = GET_DIGIT(udigit, tmp_lo, tmp_hi);
res = m1 * m2;
res += carry;
if (udigit + ldigit < 14)
{
carry = GET_DIGIT(udigit + ldigit, *bcd_low, *bcd_high);
res += carry;
}
carry = res / 10;
res %= 10;
if (udigit + ldigit < 14)
SET_DIGIT(res, udigit + ldigit, bcd_low, bcd_high);
}
}
return(carry);
}
int
bcd2_div(long *bcd_low, long *bcd_high, long divisor)
{
long tmp_lo, tmp_hi, carry, d1, res, digit;
carry = 0;
tmp_lo = *bcd_low;
tmp_hi = *bcd_high;
*bcd_low = *bcd_high = 0;
for (digit=13; digit >= 0; digit--)
{
d1 = GET_DIGIT(digit, tmp_lo, tmp_hi);
d1 += 10 * carry;
res = d1 / divisor;
carry = d1 % divisor;
SET_DIGIT(res, digit, bcd_low, bcd_high);
}
return(carry);
}
long
bcd2_mod(long *bcd_low, long *bcd_high, long modulo)
{
long tmp_low, tmp_high;
tmp_low = *bcd_low;
tmp_high = *bcd_high;
while (tmp_high || tmp_low > modulo)
bcd2_sub(&tmp_low, &tmp_high, modulo);
return(tmp_low);
}
long
bcd2_cmp(long *low1, long *high1, long comp)
{
long temp = 0;
bcd2_bin(&temp, *high1);
if (temp > 214)
return(1);
bcd2_bin(&temp, *low1);
return(temp - comp);
}
#ifdef TEST
#include <values.h>
main()
{
long bin, low_bcd, high_bcd;
int i;
bin = MAXINT;
printf("%ld\n", bin);
bin_bcd2(bin, &low_bcd, &high_bcd);
printf("%ld %ld\n", high_bcd, low_bcd);
bin = 0;
bcd2_bin(&bin, high_bcd);
bcd2_bin(&bin, low_bcd);
printf( "%ld\n", bin);
for (i=9; i >= 0; i--)
printf("%dth digit in %d is %d\n",
i, bin, GET_DIGIT(i, low_bcd, high_bcd));
bcd2_add(&low_bcd, &high_bcd, MAXINT);
bin = 0;
bcd2_bin(&bin, high_bcd);
high_bcd = bin;
bin = 0;
bcd2_bin(&bin, low_bcd);
low_bcd = bin;
printf( "%ld%07ld\n", high_bcd, low_bcd);
bin_bcd2(14, &low_bcd, &high_bcd);
bcd2_mul(&low_bcd, &high_bcd, 23L);
bin = 0;
bcd2_bin(&bin, high_bcd);
bcd2_bin(&bin, low_bcd);
printf( "%ld\n", bin);
bcd2_div(&low_bcd, &high_bcd, 10L);
bin = 0;
bcd2_bin(&bin, high_bcd);
bcd2_bin(&bin, low_bcd);
printf( "%ld\n", bin);
}
#endif /* TEST */