-
Notifications
You must be signed in to change notification settings - Fork 0
/
kdiv.c
executable file
·896 lines (842 loc) · 23.2 KB
/
kdiv.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
/*
* File : kdiv.c
* Description: Generator and calculator for division by integer constant
* routines. Routines "magic" and "magicu" have been copied
* from Herny S. Warren's "Hacker's Delight".
* Author : Nikolaos Kavvadias <[email protected]>
* Copyright : (C) Nikolaos Kavvadias 2011-2021
* Website : http://www.nkavvadias.com
*
* This file is part of kdiv, and is distributed under the terms of the
* Modified BSD License.
*
* A copy of the Modified BSD License is included with this distrubution
* in the files COPYING.BSD.
* kdiv is free software: you can redistribute it and/or modify it under the
* terms of the Modified BSD License.
* kdiv 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 Modified BSD License for more details.
*
* You should have received a copy of the Modified BSD License along with
* kdiv. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
/* Absolute value of an integer. */
#define ABS(x) ((x) > 0 ? (x) : (-x))
// ------------------------------ cut ----------------------------------
struct mu {unsigned int M; // Magic number,
int a; // "add" indicator,
int s;}; // and shift amount.
struct ms {int M; // Magic number
int s;}; // and shift amount.
// ---------------------------- end cut --------------------------------
int divisor=1, width=32, lo=0, hi=65535;
int enable_debug=0, enable_errors=0;
int is_signed=0;
int enable_nac=1, enable_ansic=0;
/*! Print a configurable number of space characters to an output file (specified
* by the given filename; the file is assumed already opened).
*/
void print_spaces(FILE *f, int nspaces)
{
int i;
for (i = 0; i < nspaces; i++)
{
fprintf(f, " ");
}
}
/*! fprintf prefixed by a number of space characters.
*/
void pfprintf(FILE *f, int nspaces, char *fmt, ...)
{
va_list args;
print_spaces(f, nspaces);
va_start(args, fmt);
vfprintf(f, fmt, args);
va_end(args);
}
/*! Function to identify if the given unsigned integer is a power-of-2.
*/
int ispowof2(unsigned int v)
{
int f;
f = v && !(v & (v-1));
return (f);
}
/*! Function to calculate the ceiling of the binary logarithm of a given positive
* integer n.
*/
int log2ceil(unsigned int inpval)
{
unsigned int logval = 0;
if (inpval == 0)
{
fprintf(stderr, "Error: Result of log2 computation is MINUS_INFINITY.\n");
exit(1);
}
// inpval is positive
else
{
unsigned int max = 1; // exp=0 => max=2^0=1
// log computation loop
while (max < inpval)
{
// increment exponent
logval = logval + 1;
// max = 2^logval
max = max * 2;
}
}
// exponent that gives (2^logval) >= inpval
return (logval);
}
/*! Calculate integer power supporting results up to 64-bits.
*/
unsigned long long int ipowul(int base, int exponent)
{
unsigned long long int temp;
int i;
temp = 1;
for (i = 0; i < exponent; i++)
{
temp *= (unsigned int)base;
}
return (temp);
}
/*! Calculates the multiplicative inverse of an integer divisor for unsigned
* division.
*/
struct mu magicu(unsigned d, unsigned W) {
// Must have 1 <= d <= 2**32-1.
int p;
unsigned nc, delta, q1, r1, q2, r2;
struct mu magu;
magu.a = 0; // Initialize "add" indicator.
nc = -1 - (-d)%d; // Unsigned arithmetic here.
p = W-1; // Init. p.
q1 = (1 << (W-1))/nc; // Init. q1 = 2**p/nc.
r1 = (1 << (W-1)) - q1*nc;// Init. r1 = rem(2**p, nc).
q2 = ((1 << (W-1))-1)/d; // Init. q2 = (2**p - 1)/d.
r2 = ((1 << (W-1))-1) - q2*d; // Init. r2 = rem(2**p - 1, d).
do {
p = p + 1;
if (r1 >= nc - r1) {
q1 = 2*q1 + 1; // Update q1.
r1 = 2*r1 - nc;} // Update r1.
else {
q1 = 2*q1;
r1 = 2*r1;}
if (r2 + 1 >= d - r2) {
if (q2 >= (unsigned)((1 << (W-1))-1)) magu.a = 1;
q2 = 2*q2 + 1; // Update q2.
r2 = 2*r2 + 1 - d;} // Update r2.
else {
if (q2 >= (unsigned)(1 << (W-1))) magu.a = 1;
q2 = 2*q2;
r2 = 2*r2 + 1;}
delta = d - 1 - r2;
} while (p < 2*(int)W &&
(q1 < delta || (q1 == delta && r1 == 0)));
magu.M = q2 + 1; // Magic number
magu.s = p - W; // and shift amount to return
return magu; // (magu.a was set above).
}
/*! Calculates the multiplicative inverse of an integer divisor for signed
* division.
*/
struct ms magic(int d, unsigned W) { // Must have 2 <= d <= 2**31-1
// or -2**31 <= d <= -2.
int p;
unsigned ad, anc, delta, q1, r1, q2, r2, t;
const unsigned two31 = (1 << (W-1)); // 2**31.
struct ms mag;
ad = abs(d);
t = (1 << (W-1)) + ((unsigned)d >> (W-1));
anc = t - 1 - t%ad; // Absolute value of nc.
p = W-1; // Init. p.
q1 = two31/anc; // Init. q1 = 2**p/|nc|.
r1 = two31 - q1*anc; // Init. r1 = rem(2**p, |nc|).
q2 = two31/ad; // Init. q2 = 2**p/|d|.
r2 = two31 - q2*ad; // Init. r2 = rem(2**p, |d|).
do {
p = p + 1;
q1 = 2*q1; // Update q1 = 2**p/|nc|.
r1 = 2*r1; // Update r1 = rem(2**p, |nc|).
if (r1 >= anc) { // (Must be an unsigned
q1 = q1 + 1; // comparison here).
r1 = r1 - anc;}
q2 = 2*q2; // Update q2 = 2**p/|d|.
r2 = 2*r2; // Update r2 = rem(2**p, |d|).
if (r2 >= ad) { // (Must be an unsigned
q2 = q2 + 1; // comparison here).
r2 = r2 - ad;}
delta = ad - r2;
} while (q1 < delta || (q1 == delta && r1 == 0));
mag.M = q2 + 1;
if (d < 0) mag.M = -mag.M; // Magic number and
mag.s = p - W; // shift amount to return.
return mag;
}
/*!
NOTES on unsigned division by constant.
1) Unsigned division by powers-of-2, with d = 2^k
shr q, n, k
2) Unsigned division with a = 0 (additive factor)
li M, dinv // dinv = multiplicative inverse of d
mulhu q, M, n
shri q, q, s // s is the shift factor
3) Unsigned division with a = 1
li M, dinv
mulhu q, M, n
add q, q, n
shrxi q, q, s // an extended shr immediate using the carry and
// q (concatenated); then performing logical shift
*/
/*! Emit the NAC (generic assembly language) implementation of unsigned division
* by constant.
*/
void emit_kdivu_nac(FILE *f, unsigned int M, int a, int s, unsigned int d, unsigned int W)
{
pfprintf(f, 0, "procedure kdiv_u%d_p_%d (in u%d n, out u%d y)\n",
W, d, W, W);
pfprintf(f, 0, "{\n");
pfprintf(f, 2, "localvar u%d q, M;\n", W);
pfprintf(f, 2, "localvar u%d t0, t1;\n", 2*W);
if (a == 1)
{
pfprintf(f, 2, "localvar u%d n0;\n", 2*W);
}
pfprintf(f, 0, "S_1:\n");
if (ispowof2(d) == 1)
{
// shr q, n, k
pfprintf(f, 2, "q <= shr n, %d;\n", log2ceil(d));
}
else if (a == 0)
{
pfprintf(f, 2, "M <= ldc %u;\n", M);
// mulhu q, M, n
pfprintf(f, 2, "t0 <= mul M, n;\n");
pfprintf(f, 2, "t1 <= shr t0, %d;\n", W);
pfprintf(f, 2, "q <= trunc t1;\n");
// shri q, q, s
pfprintf(f, 2, "q <= shr q, %d;\n", s);
}
else if (a == 1)
{
pfprintf(f, 2, "M <= ldc %u;\n", M);
// mulhu q, M, n
pfprintf(f, 2, "t0 <= mul M, n;\n");
pfprintf(f, 2, "t1 <= shr t0, %d;\n", W);
pfprintf(f, 2, "q <= trunc t1;\n");
// add q, q, n
// t = q + n;
// q = t & 0xFFFFFFFF;
pfprintf(f, 2, "t0 <= zxt q;\n");
pfprintf(f, 2, "n0 <= zxt n;\n");
pfprintf(f, 2, "t0 <= add t0, n0;\n");
pfprintf(f, 2, "q <= trunc t0;\n");
// shrxi q, q, s // an extended shr immediate using the carry and
// q (concatenated); then performing logical shift
pfprintf(f, 2, "q <= shr q, %d;\n", s);
}
else
{
fprintf(stderr, "Error: Unsupported constant division.\n");
exit(1);
}
pfprintf(f, 2, "y <= mov q;\n");
pfprintf(f, 0, "}\n");
}
/*! Emit the ANSI C implementation of unsigned division by constant.
*/
void emit_kdivu_ansic(FILE *f, unsigned int M, int a, int s, unsigned int d, unsigned int W)
{
pfprintf(f, 0, "unsigned int kdiv_u%d_p_%d (unsigned int n)\n", W, d);
pfprintf(f, 0, "{\n");
pfprintf(f, 2, "unsigned int q, M=%u;\n", M);
pfprintf(f, 2, "unsigned long long int t;\n");
if (ispowof2(d) == 1)
{
pfprintf(f, 2, "q = n >> %d;\n", log2ceil(d));
}
else if (a == 0)
{
// mulhu q, M, n
pfprintf(f, 2, "t = (unsigned long long int)M * (unsigned long long int)n;\n");
pfprintf(f, 2, "q = t >> %d;\n", W);
if (s > 0)
{
// shri q, q, s
pfprintf(f, 2, "q = q >> %d;\n", s);
}
}
else if (a == 1)
{
// mulhu q, M, n
pfprintf(f, 2, "t = (unsigned long long int)M * (unsigned long long int)n;\n");
pfprintf(f, 2, "q = t >> %d;\n", W);
// add q, q, n
pfprintf(f, 2, "t = q + n;\n");
pfprintf(f, 2, "q = t & %d;\n", ipowul(2, W)-1);
// shrxi q, q, s // an extended shr immediate using the carry and
// q (concatenated); then performing logical shift
if (s > 0)
{
pfprintf(f, 2, "q = t >> %d;\n", s);
}
}
else
{
fprintf(stderr, "Error: Unsupported constant division.\n");
exit(1);
}
pfprintf(f, 2, "return (q);\n");
pfprintf(f, 0, "}\n");
}
/*! Perform an unsigned division by constant according to "Hacker's Delight"
* routines.
*/
unsigned int calculate_kdivu(unsigned int M, int a, int s, unsigned int n, unsigned int d, unsigned int W)
{
unsigned long long int t;
unsigned int q;
if (ispowof2(d) == 1)
{
// shr q, n, k
q = n >> log2ceil(d); // NOTE: log2ceil() accepts int and not unsigned int.
}
else if (a == 0)
{
// mulhu q, M, n
t = (unsigned long long int)M * (unsigned long long int)n;
q = t >> W;
// shri q, q, s
q = q >> s;
}
else if (a == 1)
{
// mulhu q, M, n
t = (unsigned long long int)M * (unsigned long long int)n;
q = t >> W;
// add q, q, n
t = q + n;
q = t & (ipowul(2, W) - 1);
// shrxi q, q, s // an extended shr immediate using the carry and
// q (concatenated); then performing logical shift
q = q >> s;
}
else
{
fprintf(stderr, "Error: Unsupported constant division.\n");
exit(1);
}
return (q);
}
/*!
NOTES on signed division by constant.
4) Signed division for non-powers-of-2
li M, dinv
mulhs q, M, n
add q, q, n // correction term only for d = 7
shrsi q, q, s
shri t, n, W-1 // W is the word length
add q, q, t
add q, q, 1 // for negative divisors (d < 0)
5) Signed division by powers-of-2, with d = 2^k
shrsi t, n, k-1
shri t, t, W-k
add t, n, t
shrsi q, t, k
neg q, q // for negative divisors (d < 0)
*/
/*! Emit the NAC (generic assembly language) implementation of signed division
* by constant.
*/
void emit_kdivs_nac(FILE *f, int M, int s, int d, unsigned int W)
{
int k;
pfprintf(f, 0, "procedure kdiv_s%d_", W);
if (d < 0)
{
fprintf(f, "m_");
}
else
{
fprintf(f, "p_");
}
fprintf(f, "%d (in s%u n, out s%u y)\n",
ABS(d), W, W);
pfprintf(f, 0, "{\n");
pfprintf(f, 2, "localvar s%u q, M, c;\n", W);
pfprintf(f, 2, "localvar s%u t, u, v;\n", 2*W);
pfprintf(f, 0, "S_1:\n");
k = log2ceil(ABS(d));
if (d == 1)
{
// mov q, n
pfprintf(f, 2, "q <= mov n;\n");
}
else if (d == -1)
{
// neg q, n
pfprintf(f, 2, "q <= neg n;\n");
}
else if (ispowof2(d) == 1)
{
// shrsi t, n, k-1
pfprintf(f, 2, "t <= sxt n;\n");
pfprintf(f, 2, "t <= shr t, %d;\n", k-1);
// shri t, t, W-k
pfprintf(f, 2, "u <= shr t, %d;\n", W-k);
// add t, n, t
pfprintf(f, 2, "v <= sxt n;\n");
pfprintf(f, 2, "t <= add v, u;\n");
// shrsi q, t, k
pfprintf(f, 2, "t <= shr t, %d;\n", k);
pfprintf(f, 2, "q <= trunc t;\n");
// neg q, q // for negative divisors (d < 0)
if (d < 0)
{
pfprintf(f, 2, "q <= neg q;\n");
}
}
else
{
pfprintf(f, 2, "M <= ldc %d;\n", M);
// mulhs q, M, n
pfprintf(f, 2, "t <= mul M, n;\n");
pfprintf(f, 2, "u <= shr t, %d;\n", W);
pfprintf(f, 2, "q <= trunc u;\n");
// add|sub q, q, n // correction term for certain divisors
if ((d > 0) && (M < 0))
{
pfprintf(f, 2, "q <= add q, n;\n");
}
else if ((d < 0) && (M > 0))
{
pfprintf(f, 2, "q <= sub q, n;\n");
}
// shrsi q, q, s
if (s > 0)
{
pfprintf(f, 2, "q <= shr q, %d;\n", s);
}
// shri t, n, W-1 // W is the word length
pfprintf(f, 2, "c <= shr n, %d;\n", W-1);
// add q, q, t
pfprintf(f, 2, "q <= add q, c;\n");
// add q, q, 1 // for negative divisors (d < 0) and (n != 0)
if (d < 0)
{
pfprintf(f, 2, "c <= setne n, 0;\n");
pfprintf(f, 2, "q <= add q, c;\n");
}
}
pfprintf(f, 2, "y <= mov q;\n");
pfprintf(f, 0, "}\n");
}
/*! Emit the ANSI C implementation of signed division by constant.
*/
void emit_kdivs_ansic(FILE *f, int M, int s, int d, unsigned int W)
{
int k;
pfprintf(f, 0, "signed int kdiv_s%d_", W);
if (d < 0)
{
fprintf(f, "m_");
}
else
{
fprintf(f, "p_");
}
fprintf(f, "%d (signed int n)\n", ABS(d));
pfprintf(f, 0, "{\n");
pfprintf(f, 2, "signed int q, M=%d, c;\n", M);
pfprintf(f, 2, "signed long long int t, u, v;\n");
k = log2ceil(ABS(d));
if (d == 1)
{
pfprintf(f, 2, "q = n;\n");
}
else if (d == -1)
{
pfprintf(f, 2, "q = -n;\n");
}
else if (ispowof2(d) == 1)
{
// shrsi t, n, k-1
pfprintf(f, 2, "t = n >> %d;\n", k-1);
// shri t, t, W-k
pfprintf(f, 2, "u = t >> %d;\n", W-k);
// add t, n, t
pfprintf(f, 2, "t = n + u;\n");
// shrsi q, t, k
pfprintf(f, 2, "q = t >> %d;\n", k);
// neg q, q // for negative divisors (d < 0)
if (d < 0)
{
pfprintf(f, 2, "q = -q;\n");
}
}
else
{
// mulhs q, M, n
pfprintf(f, 2, "t = (signed long long int)M * (signed long long int)n;\n");
pfprintf(f, 2, "q = t >> %d;\n", W);
// add|sub q, q, n // correction term for certain divisors
if ((d > 0) && (M < 0))
{
pfprintf(f, 2, "q = q + n;\n");
}
else if ((d < 0) && (M > 0))
{
pfprintf(f, 2, "q = q - n;\n");
}
if (s > 0)
{
// shrsi q, q, s
pfprintf(f, 2, "q = q >> %d;\n", s);
}
// shri t, n, W-1 // W is the word length
pfprintf(f, 2, "c = n >> %d;\n", W-1);
// add q, q, t
pfprintf(f, 2, "q = q + c;\n");
// add q, q, 1 // for negative divisors (d < 0) and (n != 0)
if (d < 0)
{
pfprintf(f, 2, "c = (n != 0);\n");
pfprintf(f, 2, "q = q + 1;\n");
}
}
pfprintf(f, 2, "return (q);\n");
pfprintf(f, 0, "}\n");
}
/* calculate_kdivs:
* Perform a signed division by constant according to "Hacker's Delight"
* routines.
*/
int calculate_kdivs(int M, int s, int n, int d, unsigned int W)
{
signed long long int t, u;
int q;
int k;
unsigned int c;
k = log2ceil(ABS(d));
if (d == 1)
{
q = n;
}
else if (d == -1)
{
q = -n;
}
else if (ispowof2(d) == 1)
{
// shrsi t, n, k-1
t = n >> (k-1);
// shri t, t, W-k
u = t >> (W-k);
// add t, n, t
t = n + u;
// shrsi q, t, k
q = t >> k;
// neg q, q // for negative divisors (d < 0)
if (d < 0)
{
q = -q;
}
}
else
{
// To use the results of this program, the compiler should generate the
// li and mulhs instructions, generate the add if d > 0 and M < 0, or
// the sub if d < 0 and M > 0, and generate the shrsi if s > 0. Then,
// the shri and final add must be generated.
// mulhs q, M, n
t = (signed long long int)M * (signed long long int)n;
q = t >> W;
// add|sub q, q, n // correction term for certain divisors
if ((d > 0) && (M < 0))
{
q = q + n;
}
else if ((d < 0) && (M > 0))
{
q = q - n;
}
// shrsi q, q, s
q = q >> s;
// shri t, n, W-1 // W is the word length
c = n >> (W-1);
// add q, q, t
q = q + c;
// add q, q, 1 // for negative divisors (d < 0) and (n != 0)
if (d < 0)
{
c = (n != 0);
q = q + c;
}
}
return (q);
}
/* print_usage:
* Print usage instructions for the "kdiv" program.
*/
static void print_usage()
{
printf("\n");
printf("* Usage:\n");
printf("* ./kdiv.exe [options]\n");
printf("* \n");
printf("* Options:\n");
printf("* \n");
printf("* -h:\n");
printf("* Print this help.\n");
printf("* -d:\n");
printf("* Enable debug/diagnostic output.\n");
printf("* -errors:\n");
printf("* Report only inconsistencies to the expected division results. Debug\n");
printf("* output (-d) must be enabled.\n");
printf("* -div <num>:\n");
printf("* Set the value of the divisor (an integer except zero). Default: 1.\n");
printf("* -width <num>:\n");
printf("* Set the bitwidth of all operands: dividend, divisor and\n");
printf("* quotient. Default: 32.\n");
printf("* -lo <num>:\n");
printf("* Set the lower integer bound for dividend testing. Debug output (-d)\n");
printf("* must be enabled. Default: 0.\n");
printf("* -hi <num>:\n");
printf("* Set the higher integer bound for dividend testing. Debug output (-d)\n");
printf("* must be enabled. Default: 65535.\n");
printf("* -signed:\n");
printf("* Construct optimized routine for signed division.\n");
printf("* -unsigned:\n");
printf("* Construct optimized routine for unsigned division (default).\n");
printf("* -nac:\n");
printf("* Emit software routine in the NAC general assembly language (default).\n");
printf("* -ansic:\n");
printf("* Emit software routine in ANSI C (only for width=32).\n");
printf("* \n");
printf("* For further information, please refer to the website:\n");
printf("* http://www.nkavvadias.com\n");
}
/*! Program entry.
*/
int main(int argc, char *argv[])
{
struct mu magu;
struct ms mags;
int i;
FILE *fout;
char *fout_name, suffix[4], ch='X';
// If no arguments are passed, exit with help
if (argc == 1)
{
print_usage();
exit(1);
}
// Read input arguments
for (i = 1; i < argc; i++)
{
if (strcmp("-h", argv[i]) == 0)
{
print_usage();
exit(1);
}
else if (strcmp("-d", argv[i]) == 0)
{
enable_debug = 1;
}
else if (strcmp("-errors", argv[i]) == 0)
{
enable_errors = 1;
}
else if (strcmp("-unsigned", argv[i]) == 0)
{
is_signed = 0;
}
else if (strcmp("-signed", argv[i]) == 0)
{
is_signed = 1;
}
else if (strcmp("-nac", argv[i]) == 0)
{
enable_nac = 1;
enable_ansic = 0;
}
else if (strcmp("-ansic", argv[i]) == 0)
{
enable_nac = 0;
enable_ansic = 1;
}
else if (strcmp("-div",argv[i]) == 0)
{
if ((i+1) < argc)
{
i++;
divisor = atoi(argv[i]);
}
}
else if (strcmp("-width",argv[i]) == 0)
{
if ((i+1) < argc)
{
i++;
width = atoi(argv[i]);
}
}
else if (strcmp("-lo",argv[i]) == 0)
{
if ((i+1) < argc)
{
i++;
if (argv[i][0] == '-')
{
lo = -atoi(argv[i]+1);
}
else
{
lo = atoi(argv[i]);
}
}
}
else if (strcmp("-hi",argv[i]) == 0)
{
if ((i+1) < argc)
{
i++;
if (argv[i][0] == '-')
{
hi = -atoi(argv[i]+1);
}
else
{
hi = atoi(argv[i]);
}
}
}
else
{
if (argv[i][0] != '-')
{
print_usage();
exit(1);
}
}
}
if (divisor == 0)
{
fprintf(stderr, "Error: Requested division by zero.\n");
exit(1);
}
if ((is_signed == 0) && (divisor < 0))
{
fprintf(stderr, "Error: Divisor must be positive for unsigned division.\n");
exit(1);
}
fout_name = malloc(25 * sizeof(char));
if (enable_nac == 1)
{
strcpy(suffix, "nac");
}
else if (enable_ansic == 1)
{
strcpy(suffix, "c");
}
ch = (is_signed == 0) ? 'u' : 's';
sprintf(fout_name, "kdiv_%c%d_%c_%d.%s", ch, width, ((divisor > 0) ? 'p' : 'm'), ABS(divisor), suffix);
/* Calculate magic numbers for unsigned and signed division */
magu = magicu(divisor, width);
mags = magic(divisor, width);
fout = fopen(fout_name, "w");
if (is_signed == 0)
{
if (enable_nac == 1)
{
emit_kdivu_nac(fout, magu.M, magu.a, magu.s, divisor, width);
}
else if (enable_ansic == 1)
{
emit_kdivu_ansic(fout, magu.M, magu.a, magu.s, divisor, width);
}
}
else
{
if (enable_nac == 1)
{
emit_kdivs_nac(fout, mags.M, mags.s, divisor, width);
}
else if (enable_ansic == 1)
{
emit_kdivs_ansic(fout, mags.M, mags.s, divisor, width);
}
}
if (enable_debug == 1)
{
unsigned int uquotapprox=0, uquotexact=0;
int squotapprox=0, squotexact=0;
for (i = lo; i <= hi; i++)
{
if (is_signed == 0)
{
uquotapprox = calculate_kdivu(magu.M, magu.a, magu.s, i, divisor, width);
uquotexact = i/divisor;
}
else
{
squotapprox = calculate_kdivs(mags.M, mags.s, i, divisor, width);
squotexact = i/divisor;
}
if ((is_signed == 0) && (uquotapprox != uquotexact))
{
if (enable_errors == 1)
{
printf("Result NOT exact: %d/%d = %u (%u)\n",
i, divisor, uquotapprox, uquotexact);
}
}
else if ((is_signed == 0) && (uquotapprox == uquotexact))
{
if (enable_errors == 0)
{
printf("%d/%d = %u (%u)\n", i, divisor, uquotapprox, uquotexact);
}
}
else if ((is_signed == 1) && (squotapprox != squotexact))
{
if (enable_errors == 1)
{
printf("Result NOT exact: %d/%d = %d (%d)\n",
i, divisor, squotapprox, squotexact);
}
}
else if ((is_signed == 1) && (squotapprox == squotexact))
{
if (enable_errors == 0)
{
printf("%d/%d = %d (%d)\n", i, divisor, squotapprox, squotexact);
}
}
}
}
#ifdef EMIT_TABLES
for (i = 1; i < 32; i++)
{
magu = magicu(i, width);
printf("%03d: M = %08x a = %d s = %d\n", i, magu.M, magu.a, magu.s);
}
for (i = 1; i < 32; i++)
{
mags = magic(i, width);
printf("%03d: M = %08x s = %d\n", i, mags.M, mags.s);
}
#endif
free(fout_name);
fclose(fout);
return 0;
}