-
Notifications
You must be signed in to change notification settings - Fork 0
/
int.c
1646 lines (1412 loc) · 34.2 KB
/
int.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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This file is part of 34S.
*
* 34S is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 34S is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 34S. If not, see <http://www.gnu.org/licenses/>.
*/
#include "int.h"
#include "xeq.h"
#include "serial.h"
static int check_intmode(void) {
if (! is_intmode())
return err(ERR_BAD_MODE);
return 0;
}
/* Some utility routines to extract bits of long longs */
unsigned int int_base(void) {
const unsigned int b = UState.int_base + 1;
if (b < 2)
return 10;
return b;
}
enum arithmetic_modes int_mode(void) {
return (enum arithmetic_modes) UState.int_mode;
}
unsigned int word_size(void) {
unsigned int il = UState.int_len;
if (il >= MAX_WORD_SIZE || il == 0)
return MAX_WORD_SIZE;
return il;
}
int get_carry(void) {
return get_user_flag(CARRY_FLAG);
}
void set_carry(int c) {
#ifndef TINY_BUILD
if (c)
set_user_flag(CARRY_FLAG);
else
clr_user_flag(CARRY_FLAG);
#endif
}
int get_overflow(void) {
return get_user_flag(OVERFLOW_FLAG);
}
void set_overflow(int o) {
#ifndef TINY_BUILD
if (o)
set_user_flag(OVERFLOW_FLAG);
else
clr_user_flag(OVERFLOW_FLAG);
#endif
}
#ifndef TINY_BUILD
/* Some utility routines for saving and restoring carry and overflow.
* Some operations don't change these flags but their subcomponents might.
*/
static int save_flags(void) {
return (get_overflow() << 1) | get_carry();
}
static void restore_flags(int co) {
set_carry(co & 1);
set_overflow(co & 2);
}
#endif
/* Utility routine for trimming a value to the current word size
*/
long long int mask_value(const long long int v) {
#ifndef TINY_BUILD
const unsigned int ws = word_size();
long long int mask;
if (MAX_WORD_SIZE == 64 && ws == 64)
return v;
mask = (1LL << ws) - 1;
return v & mask;
#else
return v;
#endif
}
#ifndef TINY_BUILD
/* Ulility routine for returning a bit mask to get the topmost (sign)
* bit from a number.
*/
static long long int topbit_mask(void) {
const unsigned int ws = word_size();
long long int bit = 1LL << (ws - 1);
return bit;
}
#endif
/* Utility routine to convert a binary integer into separate sign and
* value components. The sign returned is 1 for negative and 0 for positive.
*/
unsigned long long int extract_value(const long long int val, int *const sign) {
const enum arithmetic_modes mode = int_mode();
long long int v = mask_value(val);
long long int tbm;
if (mode == MODE_UNSIGNED) {
*sign = 0;
return v;
}
tbm = topbit_mask();
if (v & tbm) {
*sign = 1;
if (mode == MODE_2COMP)
v = -v;
else if (mode == MODE_1COMP)
v = ~v;
else // if (mode == MODE_SGNMANT)
v ^= tbm;
} else
*sign = 0;
return mask_value(v);
}
/* Helper routine to construct a value from the magnitude and sign
*/
long long int build_value(const unsigned long long int x, const int sign) {
#ifndef TINY_BUILD
const enum arithmetic_modes mode = int_mode();
long long int v = mask_value(x);
if (sign == 0 || mode == MODE_UNSIGNED)
return v;
if (mode == MODE_2COMP)
return mask_value(-(signed long long int)v);
if (mode == MODE_1COMP)
return mask_value(~v);
return v | topbit_mask();
#else
return x;
#endif
}
/* Utility routine to check if a value has overflowed or not */
int check_overflow(long long int x) {
return mask_value(x) != x ||
(int_mode() != MODE_UNSIGNED && (x & topbit_mask()) != 0);
}
#ifndef TINY_BUILD
/* Helper routine for addition and subtraction that detemines the proper
* setting for the overflow bit. This routine should only be called when
* the signs of the operands are the same for addition and different
* for subtraction. Overflow isn't possible if the signs are opposite.
* The arguments of the operator should be passed in after conversion
* to positive unsigned quantities nominally in two's complement.
*/
static int calc_overflow(unsigned long long int xv,
unsigned long long int yv, enum arithmetic_modes mode, int neg) {
unsigned long long int tbm = topbit_mask();
unsigned long long int u;
int i;
switch (mode) {
case MODE_UNSIGNED:
// C doesn't expose the processor's status bits to us so we
// break the addition down so we don't lose the overflow.
u = (yv & (tbm-1)) + (xv & (tbm-1));
i = ((u & tbm)?1:0) + ((xv & tbm)?1:0) + ((yv & tbm)?1:0);
if (i > 1)
break;
return 0;
case MODE_2COMP:
u = xv + yv;
if (neg && u == tbm)
return 0;
if (tbm & u)
break;
if ((xv == tbm && yv !=0) || (yv == tbm && xv != 0))
break;
return 0;
case MODE_SGNMANT:
case MODE_1COMP:
if (tbm & (xv + yv))
break;
return 0;
}
set_overflow(1);
return 1;
}
#endif
long long int intAdd(long long int y, long long int x) {
#ifndef TINY_BUILD
int sx, sy;
unsigned long long int xv = extract_value(x, &sx);
unsigned long long int yv = extract_value(y, &sy);
const enum arithmetic_modes mode = int_mode();
long long int v;
int overflow;
set_overflow(0);
if (sx == sy)
overflow = calc_overflow(xv, yv, mode, sx);
else
overflow = 0;
if (mode == MODE_SGNMANT) {
const long long int tbm = topbit_mask();
const long long int x2 = (x & tbm)?-(x ^ tbm):x;
const long long int y2 = (y & tbm)?-(y ^ tbm):y;
set_carry(overflow);
v = y2 + x2;
if (v & tbm)
v = -v | tbm;
} else {
int carry;
const unsigned long long int u = mask_value(y + x);
if (u < (unsigned long long int)mask_value(y)) {
set_carry(1);
carry = 1;
} else {
set_carry(0);
carry = 0;
}
v = y + x;
if (carry && mode == MODE_1COMP)
v++;
}
return mask_value(v);
#else
return y+x;
#endif
}
long long int intSubtract(long long int y, long long int x) {
#ifndef TINY_BUILD
int sx, sy;
unsigned long long int xv = extract_value(x, &sx);
unsigned long long int yv = extract_value(y, &sy);
const enum arithmetic_modes mode = int_mode();
long long int v, tbm;
set_overflow(0);
if (sx != sy)
calc_overflow(xv, yv, mode, sy);
if (mode == MODE_SGNMANT) {
long long int x2, y2;
set_carry((sx == 0 && sy == 0 && xv > yv) ||
(sx != 0 && sy != 0 && xv < yv));
tbm = topbit_mask();
x2 = (x & tbm)?-(x ^ tbm):x;
y2 = (y & tbm)?-(y ^ tbm):y;
v = y2 - x2;
if (v & tbm)
v = -v | tbm;
} else {
int borrow;
if ((unsigned long long int)y < (unsigned long long int)x) {
set_carry(1);
if (mode == MODE_UNSIGNED)
set_overflow(1);
borrow = 1;
} else {
set_carry(0);
borrow = 0;
}
v = y - x;
if (borrow && mode == MODE_1COMP)
v--;
}
return mask_value(v);
#else
return y-x;
#endif
}
static unsigned long long int multiply_with_overflow(unsigned long long int x, unsigned long long int y, int *overflow) {
const unsigned long long int t = mask_value(x * y);
if (! *overflow && y != 0) {
const enum arithmetic_modes mode = int_mode();
const unsigned long long int tbm = (mode == MODE_UNSIGNED) ? 0 : topbit_mask();
if ((t & tbm) != 0 || t / y != x)
*overflow = 1;
}
return t;
}
long long int intMultiply(long long int y, long long int x) {
#ifndef TINY_BUILD
unsigned long long int u;
int sx, sy;
unsigned long long int xv = extract_value(x, &sx);
unsigned long long int yv = extract_value(y, &sy);
int overflow = 0;
u = multiply_with_overflow(xv, yv, &overflow);
set_overflow(overflow);
if (int_mode() == MODE_UNSIGNED)
return u;
return build_value(u & ~topbit_mask(), sx ^ sy);
#else
return x*y;
#endif
}
#ifndef TINY_BUILD
static void err_div0(unsigned long long int num, int sn, int sd) {
if (num == 0)
err(ERR_DOMAIN);
else if (sn == sd)
err(ERR_INFINITY);
else
err(ERR_MINFINITY);
}
#endif
long long int intDivide(long long int y, long long int x) {
#ifndef TINY_BUILD
const enum arithmetic_modes mode = int_mode();
int sx, sy;
unsigned long long int xv = extract_value(x, &sx);
unsigned long long int yv = extract_value(y, &sy);
unsigned long long int r;
long long int tbm;
if (xv == 0) {
err_div0(yv, sy, sx);
return 0;
}
set_overflow(0);
r = mask_value(yv / xv);
// Set carry if there is a remainder
set_carry(r * xv != yv);
if (mode != MODE_UNSIGNED) {
tbm = topbit_mask();
if (r & tbm)
set_carry(1);
// Special case for 0x8000...00 / -1 in 2's complement
if (mode == MODE_2COMP && sx && xv == 1 && y == tbm)
set_overflow(1);
}
return build_value(r, sx ^ sy);
#else
return y/x;
#endif
}
long long int intMod(long long int y, long long int x) {
#ifndef TINY_BUILD
int sx, sy;
unsigned long long int xv = extract_value(x, &sx);
unsigned long long int yv = extract_value(y, &sy);
unsigned long long int r;
if (xv == 0) {
err_div0(yv, sy, sx);
return 0;
}
r = yv % xv;
#ifdef INCLUDE_MOD41
if (XeqOpCode == (OP_DYA | OP_MOD41) && sx != sy) {
if (r != 0)
r = xv - r;
sy = sx;
}
#endif
return build_value(r, sy);
#else
return y%x;
#endif
}
long long int intMin(long long int y, long long int x) {
#ifndef TINY_BUILD
int sx, sy;
const unsigned long long int xv = extract_value(x, &sx);
const unsigned long long int yv = extract_value(y, &sy);
if (sx != sy) { // different signs
if (sx)
return x;
} else if (sx) { // both negative
if (xv > yv)
return x;
} else { // both positive
if (xv < yv)
return x;
}
return y;
#else
return 0;
#endif
}
long long int intMax(long long int y, long long int x) {
#ifndef TINY_BUILD
int sx, sy;
unsigned long long int xv = extract_value(x, &sx);
unsigned long long int yv = extract_value(y, &sy);
if (sx != sy) { // different signs
if (sx)
return y;
} else if (sx) { // both negative
if (xv > yv)
return y;
} else { // both positive
if (xv < yv)
return y;
}
return x;
#else
return 0;
#endif
}
#ifdef INCLUDE_MULADD
long long int intMAdd(long long int z, long long int y, long long int x) {
#ifndef TINY_BUILD
long long int t = intMultiply(x, y);
const int of = get_overflow();
t = intAdd(t, z);
if (of)
set_overflow(1);
return t;
#else
return 0;
#endif
}
#endif
static unsigned long long int int_gcd(unsigned long long int a, unsigned long long int b) {
while (b != 0) {
const unsigned long long int t = b;
b = a % b;
a = t;
}
return a;
}
long long int intGCD(long long int y, long long int x) {
int s;
unsigned long long int xv = extract_value(x, &s);
unsigned long long int yv = extract_value(y, &s);
unsigned long long int v;
if (xv == 0)
v = yv;
else if (yv == 0)
v = xv;
else
v = int_gcd(xv, yv);
return build_value(v, 0);
}
long long int intLCM(long long int y, long long int x) {
int s;
unsigned long long int xv = extract_value(x, &s);
unsigned long long int yv = extract_value(y, &s);
unsigned long long int gcd;
if (xv == 0 || yv == 0)
return 0;
gcd = int_gcd(xv, yv);
return intMultiply(mask_value(xv / gcd), build_value(yv, 0));
}
long long int intSqr(long long int x) {
return intMultiply(x, x);
}
long long int intCube(long long int x) {
#ifndef TINY_BUILD
long long int y = intMultiply(x, x);
int overflow = get_overflow();
y = intMultiply(x, y);
if (overflow)
set_overflow(1);
return y;
#else
return 0;
#endif
}
long long int intChs(long long int x) {
#ifndef TINY_BUILD
const enum arithmetic_modes mode = int_mode();
int sx;
unsigned long long int xv = extract_value(x, &sx);
if (mode == MODE_UNSIGNED || (mode == MODE_2COMP && x == topbit_mask())) {
set_overflow(1);
return mask_value(-(signed long long int)xv);
}
set_overflow(0);
return build_value(xv, !sx);
#else
return x;
#endif
}
long long int intAbs(long long int x) {
#ifndef TINY_BUILD
int sx;
unsigned long long int xv = extract_value(x, &sx);
set_overflow(0);
if (int_mode() == MODE_2COMP && x == topbit_mask()) {
set_overflow(1);
return x;
}
return build_value(xv, 0);
#else
return x;
#endif
}
#ifndef TINY_BUILD
static void breakup(unsigned long long int x, unsigned short xv[4]) {
xv[0] = x & 0xffff;
xv[1] = (x >> 16) & 0xffff;
xv[2] = (x >> 32) & 0xffff;
xv[3] = (x >> 48) & 0xffff;
}
static unsigned long long int packup(unsigned short int x[4]) {
return (((unsigned long long int)x[3]) << 48) |
(((unsigned long long int)x[2]) << 32) |
(((unsigned long int)x[1]) << 16) |
x[0];
}
#endif
void intDblMul(enum nilop op) {
#ifndef TINY_BUILD
const enum arithmetic_modes mode = int_mode();
unsigned long long int xv, yv;
int s;
unsigned short int xa[4], ya[4];
unsigned int t[8];
unsigned short int r[8];
int i, j;
{
long long int xr, yr;
int sx, sy;
xr = getX_int();
yr = get_reg_n_int(regY_idx);
xv = extract_value(xr, &sx);
yv = extract_value(yr, &sy);
s = sx != sy;
}
/* Do the multiplication by breaking the values into unsigned shorts
* multiplying them all out and accumulating into unsigned ints.
* Then perform a second pass over the ints to propogate carry.
* Finally, repack into unsigned long long ints.
*
* This isn't terribly efficient especially for shorter word
* sizes but it works. Special cases for WS <= 16 and/or WS <= 32
* might be worthwhile since the CPU supports these multiplications
* natively.
*/
breakup(xv, xa);
breakup(yv, ya);
for (i=0; i<8; i++)
t[i] = 0;
for (i=0; i<4; i++)
for (j=0; j<4; j++)
t[i+j] += xa[i] * ya[j];
for (i=0; i<8; i++) {
if (t[i] >= 65536)
t[i+1] += t[i] >> 16;
r[i] = t[i];
}
yv = packup(r);
xv = packup(r+4);
i = word_size();
if (i != 64)
xv = (xv << (64-i)) | (yv >> i);
setlastX();
if (s != 0) {
if (mode == MODE_2COMP) {
yv = mask_value(1 + ~yv);
xv = ~xv;
if (yv == 0)
xv++;
} else if (mode == MODE_1COMP) {
yv = ~yv;
xv = ~xv;
} else
xv |= topbit_mask();
}
set_reg_n_int(regY_idx, mask_value(yv));
setX_int(mask_value(xv));
set_overflow(0);
#endif
}
#ifndef TINY_BUILD
static int nlz(unsigned short int x) {
int n;
if (x == 0)
return 16;
n = 0;
if (x <= 0x00ff) {n = n + 8; x = x << 8;}
if (x <= 0x0fff) {n = n + 4; x = x << 4;}
if (x <= 0x3fff) {n = n + 2; x = x << 2;}
if (x <= 0x7fff) {n = n + 1;}
return n;
}
/* q[0], r[0], u[0], and v[0] contain the LEAST significant halfwords.
(The sequence is in little-endian order).
This first version is a fairly precise implementation of Knuth's
Algorithm D, for a binary computer with base b = 2**16. The caller
supplies
1. Space q for the quotient, m - n + 1 halfwords (at least one).
2. Space r for the remainder (optional), n halfwords.
3. The dividend u, m halfwords, m >= 1.
4. The divisor v, n halfwords, n >= 2.
The most significant digit of the divisor, v[n-1], must be nonzero. The
dividend u may have leading zeros; this just makes the algorithm take
longer and makes the quotient contain more leading zeros. A value of
NULL may be given for the address of the remainder to signify that the
caller does not want the remainder.
The program does not alter the input parameters u and v.
The quotient and remainder returned may have leading zeros.
For now, we must have m >= n. Knuth's Algorithm D also requires
that the dividend be at least as long as the divisor. (In his terms,
m >= 0 (unstated). Therefore m+n >= n.) */
static void divmnu(unsigned short q[], unsigned short r[],
const unsigned short u[], const unsigned short v[],
const int m, const int n) {
const unsigned int b = 65536; // Number base (16 bits).
unsigned qhat; // Estimated quotient digit.
unsigned rhat; // A remainder.
unsigned p; // Product of two digits.
int s, i, j, t, k;
unsigned short vn[8]; // Normalised denominator
unsigned short un[18]; // Normalised numerator
if (n == 1) { // Take care of
k = 0; // the case of a
for (j = m - 1; j >= 0; j--) { // single-digit
q[j] = (k*b + u[j])/v[0]; // divisor here.
k = (k*b + u[j]) - q[j]*v[0];
}
r[0] = k;
return;
}
// Normalize by shifting v left just enough so that
// its high-order bit is on, and shift u left the
// same amount. We may have to append a high-order
// digit on the dividend; we do that unconditionally.
s = nlz(v[n-1]); // 0 <= s <= 16.
for (i = n - 1; i > 0; i--)
vn[i] = (v[i] << s) | (v[i-1] >> (16-s));
vn[0] = v[0] << s;
un[m] = u[m-1] >> (16-s);
for (i = m - 1; i > 0; i--)
un[i] = (u[i] << s) | (u[i-1] >> (16-s));
un[0] = u[0] << s;
for (j = m - n; j >= 0; j--) { // Main loop.
// Compute estimate qhat of q[j].
qhat = (un[j+n]*b + un[j+n-1])/vn[n-1];
rhat = (un[j+n]*b + un[j+n-1]) - qhat*vn[n-1];
again:
if (qhat >= b || qhat*vn[n-2] > b*rhat + un[j+n-2]) {
qhat = qhat - 1;
rhat = rhat + vn[n-1];
if (rhat < b) goto again;
}
// Multiply and subtract.
k = 0;
for (i = 0; i < n; i++) {
p = qhat*vn[i];
t = un[i+j] - k - (p & 0xFFFF);
un[i+j] = t;
k = (p >> 16) - (t >> 16);
}
t = un[j+n] - k;
un[j+n] = t;
q[j] = qhat; // Store quotient digit.
if (t < 0) { // If we subtracted too
q[j] = q[j] - 1; // much, add back.
k = 0;
for (i = 0; i < n; i++) {
t = un[i+j] + vn[i] + k;
un[i+j] = t;
k = t >> 16;
}
un[j+n] = un[j+n] + k;
}
} // End j.
// Unnormalize remainder
for (i = 0; i < n; i++)
r[i] = (un[i] >> s) | (un[i+1] << (16-s));
}
static unsigned long long int divmod(const long long int z, const long long int y,
const long long int x, int *sx, int *sy, unsigned long long *rem) {
const enum arithmetic_modes mode = int_mode();
const unsigned int ws = word_size();
const long long int tbm = topbit_mask();
unsigned long long int d, h, l;
unsigned short denom[4];
unsigned short numer[8];
unsigned short quot[5];
unsigned short rmdr[4];
int num_denom;
int num_numer;
l = (unsigned long long int)z; // Numerator low
h = (unsigned long long int)y; // Numerator high
if (mode != MODE_UNSIGNED && (h & tbm) != 0) {
if (mode == MODE_2COMP) {
l = mask_value(1 + ~l);
h = ~h;
if (l == 0)
h++;
h = mask_value(h);
} else if (mode == MODE_1COMP) {
l = mask_value(~l);
h = mask_value(~h);
} else {
h ^= tbm;
}
*sy = 1;
} else
*sy = 0;
d = extract_value(x, sx); // Demonimator
if (d == 0) {
err_div0(h|l, *sx, *sy);
return 0;
}
if (ws != 64) {
l |= h << ws;
h >>= (64 - ws);
}
if (h == 0 && l == 0) { // zero over
*rem = 0;
return 0;
}
xset(quot, 0, sizeof(quot));
xset(rmdr, 0, sizeof(rmdr));
breakup(d, denom);
breakup(l, numer);
breakup(h, numer+4);
for (num_denom = 4; num_denom > 1 && denom[num_denom-1] == 0; num_denom--);
for (num_numer = 8; num_numer > num_denom && numer[num_numer-1] == 0; num_numer--);
divmnu(quot, rmdr, numer, denom, num_numer, num_denom);
*rem = packup(rmdr);
return packup(quot);
}
#endif
long long int intDblDiv(long long int z, long long int y, long long int x) {
#ifndef TINY_BUILD
unsigned long long int q, r;
int sx, sy;
q = divmod(z, y, x, &sx, &sy, &r);
set_overflow(0);
set_carry(r != 0);
return build_value(q, sx != sy);
#else
return 0;
#endif
}
long long int intDblRmdr(long long int z, long long int y, long long int x) {
#ifndef TINY_BUILD
unsigned long long int r;
int sx, sy;
divmod(z, y, x, &sx, &sy, &r);
return build_value(r, sy);
#else
return 0;
#endif
}
long long int intNot(long long int x) {
return mask_value(~x);
}
long long int intBooleanOp(long long int y, long long int x) {
long long result;
const int op = XeqOpCode - (OP_DYA | OP_LAND);
const int not = op >= 3 ? 3 : 0;
switch (op - not) {
case 0: result = y & x; break;
case 1: result = y | x; break;
default: result = y ^ x; break;
}
if (not)
result = ~result;
return mask_value(result);
}
/* Fraction and integer parts are very easy for integers.
*/
long long int intFP(long long int x) {
return 0;
}
long long int intSign(long long int x) {
int sgn;
unsigned long long int v = extract_value(x, &sgn);
if (v == 0)
sgn = 0;
else
v = 1;
return build_value(v, sgn);
}
/* Single bit shifts are special internal version.
* The multi-bit shifts vector through these.
*/
#ifndef TINY_BUILD
static long long int intLSL(long long int x) {
set_carry(0 != (topbit_mask() & x));
return mask_value((x << 1) & ~1);
}
static long long int intLSR(long long int x) {
set_carry(0 != (x & 1));
return mask_value((x >> 1) & ~topbit_mask());
}
static long long int intASR(long long int x) {
const enum arithmetic_modes mode = int_mode();
const long long int tbm = topbit_mask();
long long int y;
set_carry(x & 1);
if (mode == MODE_SGNMANT)
return ((x & ~tbm) >> 1) | tbm;
y = x >> 1;
if (mode != MODE_UNSIGNED && (x & tbm) != 0)
y |= tbm;
return y;
}
static long long int intRL(long long int x) {
const int cry = (topbit_mask() & x)?1:0;
set_carry(cry);
return mask_value(intLSL(x) | cry);
}
static long long int intRR(long long int x) {
const int cry = x & 1;
set_carry(cry);
x = intLSR(x);
if (cry)
x |= topbit_mask();
return mask_value(x);
}
static long long int intRLC(long long int x) {
const int cin = get_carry();
set_carry((topbit_mask() & x)?1:0);
return mask_value(intLSL(x) | cin);
}
static long long int intRRC(long long int x) {
const int cin = get_carry();
set_carry(x&1);
x = intLSR(x);
if (cin)
x |= topbit_mask();
return mask_value(x);
}
#endif
/* Like the above but taking the count argument from the opcode.
* Also possibly register indirect but that is dealt with elsewhere.
*/
void introt(unsigned int arg, enum rarg op) {
#ifndef TINY_BUILD
long long int (*f)(long long int);
unsigned int mod;
unsigned int ws;
long long int x;
unsigned int i;
if (check_intmode())
return;
ws = word_size();
x = getX_int();
if (arg != 0) {
switch (op) {
case RARG_RL: f = &intRL; mod = ws; break;
case RARG_RR: f = &intRR; mod = ws; break;
case RARG_RLC: f = &intRLC; mod = ws + 1; break;
case RARG_RRC: f = &intRRC; mod = ws + 1; break;
case RARG_SL: f = &intLSL; mod = 0; break;
case RARG_SR: f = &intLSR; mod = 0; break;
case RARG_ASR: f = &intASR; mod = 0; break;
default:
return;
}
if (arg > ws) {
if (mod)
arg = arg % mod;
else
arg = ws;
}
for (i=0; i<arg; i++)
x = (*f)(x);
}
setlastX();
setX_int(mask_value(x));
#endif
}