forked from besm6/dubna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharithmetic.cpp
364 lines (331 loc) · 10.6 KB
/
arithmetic.cpp
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
//
// BESM-6 arithmetics.
//
// Copyright (c) 2022-2023 Leonid Broukhis, Serge Vakulenko
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include "besm6_arch.h"
#include "processor.h"
//
// Сложение и все варианты вычитаний.
// Исходные значения: регистр ACC и аргумент 'val'.
// Результат помещается в регистр ACC и 40-1 разряды RMR.
//
void Processor::arith_add(Word val, bool negate_acc, bool negate_val)
{
MantissaExponent acc(core.ACC);
MantissaExponent word(val);
if (!negate_acc) {
if (!negate_val) {
// Сложение
} else {
// Вычитание
word.negate();
}
} else {
if (!negate_val) {
// Обратное вычитание
acc.negate();
} else {
// Вычитание модулей
if (acc.is_negative())
acc.negate();
if (!word.is_negative())
word.negate();
}
}
MantissaExponent a1, a2;
int diff = acc.exponent - word.exponent;
if (diff < 0) {
diff = -diff;
a1 = acc;
a2 = word;
} else {
a1 = word;
a2 = acc;
}
Word mr = 0;
bool neg = a1.is_negative();
bool round_flag = false;
if (diff == 0) {
// Nothing to do.
} else if (diff <= 40) {
mr = (a1.mantissa << (40 - diff)) & BITS40;
round_flag = (mr != 0);
a1.mantissa = ((a1.mantissa >> diff) | (neg ? (~0ll << (40 - diff)) : 0)) & BITS42;
} else if (diff <= 80) {
diff -= 40;
round_flag = (a1.mantissa != 0);
mr = ((a1.mantissa >> diff) | (neg ? (~0ll << (40 - diff)) : 0)) & BITS40;
if (neg) {
a1.mantissa = BITS42;
} else {
a1.mantissa = 0;
}
} else {
round_flag = (a1.mantissa != 0);
if (neg) {
mr = BITS40;
a1.mantissa = BITS42;
} else {
mr = a1.mantissa = 0;
}
}
acc.exponent = a2.exponent;
acc.mantissa = a1.mantissa + a2.mantissa;
// Если требуется нормализация вправо, биты 42:41
// принимают значение 01 или 10.
if (acc.is_denormal()) {
round_flag |= acc.mantissa & 1;
mr = (mr >> 1) | ((acc.mantissa & 1) << 39);
acc.normalize_to_the_right();
}
arith_normalize_and_round(acc, mr, round_flag);
}
//
// Нормализация и округление.
// Результат помещается в регистры ACC и 40-1 разряды RMR.
// 48-41 разряды RMR сохраняются.
//
void Processor::arith_normalize_and_round(MantissaExponent acc, Word mr, bool round_flag)
{
Word rr = 0;
int i;
Word r;
if (core.RAU & RAU_NORM_DISABLE)
goto chk_rnd;
i = (acc.mantissa >> 39) & 3;
if (i == 0) {
r = acc.mantissa & BITS40;
if (r) {
int cnt = besm6_highest_bit(r) - 9;
r <<= cnt;
rr = mr >> (40 - cnt);
acc.mantissa = r | rr;
mr <<= cnt;
acc.exponent -= cnt;
goto chk_zero;
}
r = mr & BITS40;
if (r) {
int cnt = besm6_highest_bit(r) - 9;
rr = mr;
r <<= cnt;
acc.mantissa = r;
mr = 0;
acc.exponent -= 40 + cnt;
goto chk_zero;
}
goto zero;
} else if (i == 3) {
r = ~acc.mantissa & BITS40;
if (r) {
int cnt = besm6_highest_bit(r) - 9;
r = (r << cnt) | ((1LL << cnt) - 1);
rr = mr >> (40 - cnt);
acc.mantissa = BIT41 | (~r & BITS40) | rr;
mr <<= cnt;
acc.exponent -= cnt;
goto chk_zero;
}
r = ~mr & BITS40;
if (r) {
int cnt = besm6_highest_bit(r) - 9;
rr = mr;
r = (r << cnt) | ((1LL << cnt) - 1);
acc.mantissa = BIT41 | (~r & BITS40);
mr = 0;
acc.exponent -= 40 + cnt;
goto chk_zero;
} else {
rr = 1;
acc.mantissa = BIT41;
mr = 0;
acc.exponent -= 80;
goto chk_zero;
}
}
chk_zero:
if (rr)
round_flag = false;
chk_rnd:
if (acc.exponent & 0x8000)
goto zero;
if (!(core.RAU & RAU_ROUND_DISABLE) && round_flag) {
acc.mantissa |= 1;
}
if (!acc.mantissa && !(core.RAU & RAU_NORM_DISABLE)) {
zero:
core.ACC = 0;
core.RMR &= ~BITS40;
return;
}
core.ACC = (Word)(acc.exponent & BITS(7)) << 41 | (acc.mantissa & BITS41);
core.RMR = (core.RMR & ~BITS40) | (mr & BITS40);
// При переполнении мантисса и младшие разряды порядка верны
if (acc.exponent & 0x80) {
if (!(core.RAU & RAU_OVF_DISABLE)) {
throw Exception("Arithmetic overflow");
}
}
}
//
// Изменение порядка числа на сумматоре ACC.
// Результат помещается в регистр ACC, RMR гасится.
//
void Processor::arith_add_exponent(int val)
{
MantissaExponent acc(core.ACC);
acc.exponent += val;
core.RMR = 0;
arith_normalize_and_round(acc, 0, 0);
}
//
// Изменение знака числа на сумматоре ACC.
// Результат помещается в регистр ACC, RMR гасится.
//
void Processor::arith_change_sign(bool negate_acc)
{
MantissaExponent acc(core.ACC);
if (negate_acc) {
acc.negate();
if (acc.is_denormal()) {
acc.normalize_to_the_right();
}
}
core.RMR = 0;
arith_normalize_and_round(acc, 0, 0);
}
//
// Умножение.
// Исходные значения: регистр ACC и аргумент 'val'.
// Результат помещается в регистр ACC и 40-1 разряды RMR.
//
void Processor::arith_multiply(Word val)
{
if (!core.ACC || !val) {
// multiplication by zero is zero
core.ACC = 0;
core.RMR &= ~BITS40;
return;
}
MantissaExponent acc(core.ACC);
MantissaExponent word(val);
Word mr;
//
// Multiply two signed 41-bit integers a and b, giving a 81-bit result.
// Put upper 41 bits into signed *hi.
// Put lower 40 bits into unsigned *lo.
//
__int128 result = (__int128)acc.mantissa * word.mantissa;
acc.mantissa = (int64_t)(result >> 40);
mr = (Word)result & BITS40;
acc.exponent += word.exponent - 64;
if (acc.is_denormal()) {
acc.normalize_to_the_right();
}
arith_normalize_and_round(acc, mr, mr != 0);
}
//
// non-restoring division
//
static inline MantissaExponent nrdiv(MantissaExponent n, MantissaExponent d)
{
MantissaExponent quot;
if (d.mantissa == BIT40) {
// Divide by a positive power of 2.
quot.mantissa = n.mantissa;
quot.exponent = n.exponent - d.exponent + 64 + 1;
return quot;
}
// to compensate for potential normalization to the right
n.mantissa <<= 1;
d.mantissa <<= 1;
if (llabs(n.mantissa) >= llabs(d.mantissa)) {
n.normalize_to_the_right();
}
// Compute exponent.
quot.exponent = n.exponent - d.exponent + 64;
// Compute mantissa.
quot.mantissa = 0;
for (int64_t bitmask = BIT40; bitmask > 0; bitmask >>= 1) {
if (n.mantissa == 0)
break;
if (llabs(n.mantissa) < BIT40) {
// magic shortcut
n.mantissa *= 2;
} else if ((n.mantissa > 0) == (d.mantissa > 0)) {
// Same sign
quot.mantissa += bitmask;
n.mantissa *= 2;
n.mantissa -= d.mantissa;
} else {
// Different sign
quot.mantissa -= bitmask;
n.mantissa *= 2;
n.mantissa += d.mantissa;
}
}
return quot;
}
//
// Деление.
// Исходные значения: регистр ACC и аргумент 'val'.
// Результат помещается в регистр ACC, содержимое RMR не определено.
//
void Processor::arith_divide(Word val)
{
if (((val ^ (val << 1)) & BIT41) == 0) {
// Ненормализованный делитель: деление на ноль.
throw Exception("Division by zero");
}
MantissaExponent dividend(core.ACC);
MantissaExponent divisor(val);
MantissaExponent acc = nrdiv(dividend, divisor);
arith_normalize_and_round(acc, 0, 0);
}
//
// Сдвиг сумматора ACC с выдвижением в регистр младших разрядов RMR.
// Величина сдвига находится в диапазоне -64..63.
//
void Processor::arith_shift(int nbits)
{
core.RMR = 0;
if (nbits > 0) {
// Сдвиг вправо.
if (nbits < 48) {
core.RMR = (core.ACC << (48 - nbits)) & BITS48;
core.ACC >>= nbits;
} else {
core.RMR = core.ACC >> (nbits - 48);
core.ACC = 0;
}
} else if (nbits < 0) {
// Сдвиг влево.
nbits = -nbits;
if (nbits < 48) {
core.RMR = core.ACC >> (48 - nbits);
core.ACC = (core.ACC << nbits) & BITS48;
} else {
core.RMR = (core.ACC << (nbits - 48)) & BITS48;
core.ACC = 0;
}
}
}