summaryrefslogtreecommitdiff
path: root/imdi/cgen.c
blob: b186dd6b37cb5fe00e2fca8f877d5894332caa36 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150

/* Integer Multi-Dimensional Interpolation */

/*
 * Copyright 2000 - 2007 Graeme W. Gill
 * All rights reserved.
 *
 * This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
 * see the License.txt file for licencing details.
 */

/* 'C' code color transform kernel code generator. */

/*
   This module generates C code routines which implement
   an integer multi-channel transform. The input values
   are read, passed through per channel lookup tables,
   a multi-dimentional interpolation table, and then
   a per channel output lookup table, before being written.
*/


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdarg.h>
#include <string.h>

#include "imdi.h"
#include "imdi_tab.h"

#undef VERBOSE
#define INSTHRESH 4		/* Use inserion sort of di >= INSTHRESH for best performance. */
#undef ROUND			/* Round the division after accumulation */
						/* Improves accuracy at the cost of a little speed */

/* ------------------------------------ */
/* Generator context */
typedef struct {
	FILE *of;			/* Output file */
	int indt;			/* Indent */

	/* Other info */
	genspec *g;			/* Generation specifications */
	tabspec *t;			/* Table setup data */
	mach_arch *a;		/* Machine architecture and tuning data */

	/* Code generation information */
	/* if() conditions are for entry usage */

	/* Pixel read information */
	int ipt[IXDI];		/* Input pointer types */
	int nip;			/* Actual number of input pointers, accounting for pint */
	int chv_bits;		/* Bits in chv temp variable ?? */

	/* Input table entry */
	int itet;			/* Input table entry type */
	int itvt;			/* Input table variable type */
	int itmnb;			/* Input table minimum bits (actual is it_ab) */

	/* Interpolation index */
	int ixet;			/* Interpolation index entry type */
	int ixvt;			/* Interpolation index variable type */
	int ixmnb;			/* Interpolation index minimum bits (actual is ix_ab ???) */
	int ixmxres;		/* Interpolation table maximum resolution */

	/* Simplex index: if(!sort && it_xs) */
	int sxet;			/* Simplex index entry type  */
	int sxvt;			/* Simplex index variable type */
	int sxmnb;			/* Simplex index bits minimum (actual is sx_ab) */
	int sxmxres;		/* Simplex table maximum resolution (0 if sort) */

	/* Combination Weighting + Vertex offset values: if(it_xs && !wo_xs) */
	int woet;			/* Weighting+offset entry type  */
	int wovt;			/* Weighting+offset variable type */
	int womnb;			/* Weighting+offset index bits minimum (actual is wo_ab) */

	/* Weighting value: if(it_xs && wo_xs) */
	int weet;			/* Weighting entry type  */
	int wevt;			/* Weighting variable type */
	int wemnb;			/* Weighting index bits minimum (actual is we_ab) */

	/* Vertex offset value: if(it_xs && wo_xs) */
	int voet;			/* Vertex offset entry type  */
	int vovt;			/* Vertex offset variable type */
	int vomnb;			/* Vertex offset index bits minimum (actual is vo_ab) */

	/* Interpolation table entry: */
	int imovb;			/* Interpolation output value bits per channel required */
	int imfvt;			/* Interpolation full entry & variable type */
	int impvt;			/* Interpolation partial entry variable type */

	/* Interpolation accumulators: */
	int iaovb;			/* Interpolation output value bits per channel required */
	int iafvt;			/* Interpolation full entry & variable type */
	int iapvt;			/* Interpolation partial entry variable type */
	int ian;			/* Total number of accumulators */

	/* Output table lookup */
	int otit;			/* Output table index type */
	int otvt;			/* Output table value type (size is ot_ts bytes) */

	/* Write information */
	int opt[IXDO];		/* Output pointer types */
	int nop;			/* Actual number of output pointers, accounting for pint */

} fileo;

void line(fileo *f, char *fmt, ...);	/* Output one line */
void sline(fileo *f, char *fmt, ...);	/* Output start of line */
void mline(fileo *f, char *fmt, ...);	/* Output middle of line */
void eline(fileo *f, char *fmt, ...);	/* Output end of line */
void niline(fileo *f, char *fmt, ...);	/* Output one line, no indent */
void cr(fileo *f) { line(f,""); }		/* Output a blank line */
void inc(fileo *f) { f->indt++; }		/* Increment the indent level */
void dec(fileo *f) { f->indt--; }		/* Decrement the indent level */
void lineinc(fileo *f, char *fmt, ...);	/* Output one line and increment indent */
void decline(fileo *f, char *fmt, ...);	/* Decrement indent and output one line */
/* ------------------------------------ */

int findord(fileo *f, int bits);		/* Find ordinal with bits or more */
int nord(fileo *f, int ov);				/* Round ordinal type up to natural size */
int findnord(fileo *f, int bits);		/* Find ordinal with bits, or natural larger */
int findint(fileo *f, int bits);		/* Find integer with bits or more */
int nint(fileo *f, int iv);				/* Round integer type up to natural size */
int findnint(fileo *f, int bits);		/* Find integer with bits, or natural larger */
static void doheader(fileo *f);

static int calc_bits(int dim, int res);
static int calc_res(int dim, int bits);
static int calc_obits(int dim, int res, int esize);
static int calc_ores(int dim, int bits, int esize);


/* return a hexadecimal mask string */
/* take care of the case when bits >= 32 */
char *hmask(int bits) {
	static char buf[20];

	if (bits < 32) {
		sprintf(buf, "0x%x",(1 << bits)-1);
	} else if (bits == 32) {
		return "0xffffffff";
	} else if (bits == 64) {
		return "0xffffffffffffffff";
	} else {	/* Bits > 32 */
		sprintf(buf, "0x%xffffffff",(1 << (bits-32))-1);
	}
	return buf;
}

/* Generate a source file to implement the specified */
/* interpolation kernel. Fill in return values and return 0 if OK. */
/* g->opt should be set to opts_splx_sort or opts_sort_splx if both */
/* are being generated, but opts_splx is what actually chooses simplex */
/* when available, and is not recorded in the resulting table. */
/* Return 1 if this kernel could be generated with a simplex table algorithm, */
/* and some other non-zero on another error. */
int gen_c_kernel(
	genspec *g,				/* Specification of what to generate */
	tabspec *t,				/* Tablspec that will be filled in */
	mach_arch *a,
	FILE *fp,				/* File to write to */
	int index,				/* Identification index, 1 = first */
	genspec *og,			/* Previous tables genspec (for diff) */
	tabspec *ot				/* Previous tables tabspec (for diff) */
) {
	int frv = 0;		/* Function return value */
	unsigned char kk[] = { 0x43, 0x6F, 0x70, 0x79, 0x72, 0x69, 0x67, 0x68,
	                       0x74, 0x20, 0x32, 0x30, 0x30, 0x34, 0x20, 0x47,
	                       0x72, 0x61, 0x65, 0x6D, 0x65, 0x20, 0x57, 0x2E,
	                       0x20, 0x47, 0x69, 0x6C, 0x6C, 0x00 };
	fileo f[1];
	int e, i;
	int timp = 0;		/* Flag to use temporary imp pointer. */
						/* Seem to make x86 MSVC++ slower */
						/* Has no effect on x86 IBMCC */

	sprintf(g->kname, "imdi_k%d",index); /* Kernel routine base name */
	strcpy(g->kkeys, (char *)kk);		 /* Kernel keys for this session */
	
	/* Setup the file output context */
	f->of = fp;
	f->indt = 0;			/* Start with no indentation */
	f->g = g;
	f->t = t;
	f->a = a;

	/* (prec is currently permitted to be only 8 or 16) */
	if (g->prec == 8) {
		if (g->id <= 4) {		/* Simplex table can be used */
			frv = 1;			/* Signal caller that simplex is possible */
			if (g->opt & opts_splx)
				t->sort = 0;	/* Implicit sort using simplex table lookup */
			else
				t->sort = 1;	/* Explicit sort */
		} else {
			t->sort = 1;		/* Explicit sort */
		}

	} else if (g->prec == 16) {
		t->sort = 1;			/* Explit sort, no simplex table */

	} else {
		fprintf(stderr,"Can't cope with requested precision of %d bits\n",g->prec);
		exit(-1);
	}

	/* Compute input read and input table lookup stuff */

	/* Compute number of input pointers */
	if (g->in.pint != 0)	/* Pixel interleaved */
		f->nip = 1;
	else
		f->nip = g->id;

	/* Figure out the input pointer types */
	for (e = 0; e < f->nip; e++) {
		if ((f->ipt[e] = findord(f, g->in.bpch[e])) < 0) {
			fprintf(stderr,"Input channel size can't be handled\n");
			exit(-1);
		}
	}

	/* Do the rest of the input table size calculations after figuring */
	/* out simplex and interpolation table sizes. */

	/* Figure out the interpolation multi-dimentional table structure */
	/* and output accumulation variable sizes. Note that the accumulator */
	/* size needs to be greater than the basic precision by soem factor, */
	/* if we are not to get rounding errors due to each value being the sum */
	/* of di+1 parts with weighting that sum to 1.0. It's convenient in */
	/* C code case to simply double the basic precision size. */
	if (g->prec == 8
	 || (g->prec == 16 && a->ords[a->nords-1].bits >= (g->prec * 4))) {
		int tiby;		/* Total interpolation bytes needed */

		/* We assume that we can normally compute more than one */
		/* output value at a time, so we need to hold the interpolation */
		/* output data in the expanded fixed point format in both the */
		/* table and accumulator. */
		t->im_cd = 1;
		f->imovb = g->prec * 2;		/* 16 bits needed for 8 bit precision, */
		f->iaovb = g->prec * 2;		/* 32 bits needed for 16 bit precision */
		f->imfvt = a->nords-1;		/* Full variable entry type is biggest available */
		f->iafvt = a->nords-1;		/* Full variable accum. type is same */

		if (a->ords[f->imfvt].bits < f->imovb) {
			fprintf(stderr,"Interpolation table entry size can't be handled\n");
			exit(-1);
		}

		/* Compute details of table entry sizes, number */
		tiby = (f->imovb * g->od)/8;				/* Total table bytes needed */
		t->im_fs = a->ords[f->imfvt].bits/8;		/* Full entry bytes */
		t->im_fv = (t->im_fs * 8)/f->imovb;			/* output values per full entry . */
		t->im_fn = tiby/t->im_fs;					/* Number of full entries (may be 0) */
		t->im_ts = t->im_fn * t->im_fs;				/* Structure size so far */
		tiby -= t->im_fn * t->im_fs;				/* Remaining bytes */

		if (tiby <= 0) {
			t->im_pn = 0;		/* No partials */
			t->im_ps = 0;
			t->im_pv = 0;
			f->impvt = 0;
			f->iapvt = 0;

		} else {
			t->im_pn = 1;					/* Must be just 1 partial */
			t->im_pv = (tiby * 8)/f->imovb;	/* Partial holds remaining entries */ 

#ifdef NEVER	/* For better performance ??? */
			if ((f->impvt = findnord(f, tiby * 8)) < 0) {
#else			/* Better memory footprint - minimise multi-D entry sizes */
				/* (but only if structure is alowed to be mis-aligned!) */
			if ((f->impvt = findord(f, tiby * 8)) < 0) {
#endif
				fprintf(stderr,"Can't find partial interp table entry variable size\n");
				exit(-1);
			}
			f->iapvt = f->impvt;
			t->im_ps = a->ords[f->impvt].bits/8;/* Partial entry bytes */

			if (a->ords[f->imfvt].align)		/* If full entry's need to be aligned */
				t->im_ts += t->im_fs;			/* Round out struct size by full entry */
			else
				t->im_ts += t->im_ps;			/* Round out to natural size */
		}
	
	} else {
		/* One 16 bit output value per entry + 32 bit accumulator. */
		/* We can conserve table space by not holding the table data in expanded */
		/* fixed point format, but expanding it when it is read. */
		/* Without resorting to compicated code, this restricts us */
		/* to only computing one output value per accumulator. */
		t->im_cd = 0;
		f->imovb = g->prec;			/* Table holds 16 bit entries with no fractions */
		f->iaovb = g->prec * 2;		/* 32 bits needed for 16 bit precision in comp. */

		if ((f->imfvt = findord(f, f->imovb)) < 0) {
			fprintf(stderr,"Interpolation table entry size can't be handled\n");
			exit(-1);
		}
		if ((f->iafvt = findord(f, f->iaovb)) < 0) {
			fprintf(stderr,"Interpolation accumulator size can't be handled\n");
			exit(-1);
		}

		/* Compute details of table entry sizes, number */
		t->im_fs = a->ords[f->imfvt].bits/8;		/* Full entry bytes */
		t->im_fv = 1;								/* output values per full entry . */
		t->im_fn = g->od;							/* Number of full entries */
		t->im_ts = t->im_fn * t->im_fs;				/* Total structure size */

		t->im_pn = 0;		/* No partials */
		t->im_ps = 0;
		t->im_pv = 0;
		f->impvt = 0;
		f->iapvt = 0;
	}
	f->ian = t->im_fn + t->im_pn;			/* Total number of output accumulators */

	/* Figure out how much of the interpolation entry offset to put in the */
	/* vertex offset value, and how much to make explicit in accessing the */
	/* interpolation table enty. */
	if (a->oscale > 0) {		/* We have a scaled index mode */
		/* Use as much of the scaled index mode as possible */
		/* and then do the balance by scaling the simplex index entry. */
		for (t->im_oc = a->oscale; ; t->im_oc >>= 1) {
			t->vo_om = t->im_ts/t->im_oc;		/* Simplex index multiplier */
			if ((t->vo_om * t->im_oc) == t->im_ts)
				break;				/* Got appropriate offset scale */
		}
	} else if (a->smmul) {		/* Architecure supports fast small multiply */
		t->im_oc = t->im_ts;	/* Do scale by structure size explicitly */
		t->vo_om = 1;			/* Do none in the Simplex index */
	} else {					/* We have no fast tricks */
		t->im_oc = 1;			/* Do none explicitly */
		t->vo_om = t->im_ts;	/* Do all in Simplex index */
	}

	/* Compute the number of bits needed to hold an index into */
	/* the interpolation table (index is in terms of table entry size). */
	/* This value is used to figure out the room needed in the input */
	/* table to accumulate the interpolation cube base offset value. (IM_O macro) */
	f->ixmnb = calc_bits(g->id, g->itres);

#ifdef VERBOSE
	/* Summarise the interpolation table arrangements */
	printf("\n");
	printf("Interpolation table structure:\n");
	printf("  Minimum bits needed to index table %d\n", f->ixmnb);
	printf("  Entry total size %d bytes\n", t->im_ts);
	printf("  Simplex entry offset scale %d\n", t->vo_om);
	printf("  Explicit entry offset scale %d\n", t->im_oc);
	printf("  %d full entries, size %d bytes\n", t->im_fn, t->im_fs);
	printf("  %d partial entries, size %d bytes\n", t->im_pn, t->im_ps);
	printf("  to hold %d output values of %d bits\n", g->od, f->imovb);

#endif /* VERBOSE */

	/* Number of bits needed for the weighting value */
	f->wemnb = g->prec+1;	/* Need to hold a weighting factor of 0 - 256 for 8 bits */
							/* Need to hold a weighting factor of 0 - 65536 for 16 bits */

	/* Variable that would be used to hold it */
	if ((f->wevt = findnord(f, f->wemnb)) < 0) {
		fprintf(stderr,"Can't find entry size to hold weighting variable\n");
		exit(-1);
	}

	/* Number of bits needed for vertex offset value */
	f->vomnb = calc_obits(g->id, g->itres, t->vo_om);

	/* Variable that would be used to hold it */
	if ((f->vovt = findnord(f, f->vomnb)) < 0) {
		fprintf(stderr,"Can't find entry size to hold vertex offset variable\n");
		exit(-1);
	}

	if (t->sort) {
		/* If we are using an explicit sort, we need to figure how many */
		/* separate entries we need to use to hold the interpolation index, */
		/* weighting factor and vertex offset values in the input table. */

		/* First try all three in one entry */
		if ((f->itet = findord(f, f->ixmnb + f->wemnb + f->vomnb)) >= 0) {/* size to read */
			int rem;						/* Remainder bits */

			t->it_xs = 0;					/* Combined interp+weight+offset */
			t->wo_xs = 0;
			t->it_ab = a->ords[f->itet].bits;	/* Bits in combined input entry */
			rem = t->it_ab - f->ixmnb - f->wemnb - f->vomnb; /* Spair bits */
			t->we_ab = f->wemnb;				/* Get minimum weight bits */
			t->vo_ab = f->vomnb + rem/2;		/* vertex offset index bits actually available */
			t->ix_ab = t->it_ab - t->vo_ab - t->we_ab;	/* interp index bits actually available */
			t->wo_ab = t->we_ab + t->vo_ab;		/* Weight & offset total bits */
			t->it_ts = a->ords[f->itet].bits/8;	/* total size in bytes */
			f->itvt = nord(f, f->itet);			/* Variable type */

			if ((f->wovt = findnord(f, t->we_ab + t->vo_ab)) < 0) {
				fprintf(stderr,"Can't find variable size to hold weight/offset\n");
				exit(-1);
			}
			if ((f->wevt = findnord(f, t->we_ab)) < 0) {
				fprintf(stderr,"Can't find variable size to hold weighting factor\n");
				exit(-1);
			}
			if ((f->vovt = findnord(f, t->vo_ab)) < 0) {
				fprintf(stderr,"Can't find variable size to hold vertex offset index\n");
				exit(-1);
			}
			if ((f->ixvt = findnord(f, t->ix_ab)) < 0) {
				fprintf(stderr,"Interp index variable size can't be handled\n");
				exit(-1);
			}
		} else {	/* Interp index will be a separate entry */
			int wit, oft, bigt;		/* weighting type, offset type, biggest type */
			int combt;				/* Combined type */
			int sepbits, combits;	/* Total separate, combined bits */

			t->it_xs = 1;				/* Separate interp index and weighting+offset */
			if ((f->ixet = findord(f, f->ixmnb)) < 0) {
				fprintf(stderr,"Interp index entry size can't be handled\n");
				exit(-1);
			}
			f->ixvt = nord(f, f->ixet);		/* Variable type */
			t->ix_ab = a->ords[f->ixet].bits;
			t->ix_es = t->ix_ab/8;
			t->ix_eo = 0;
			t->it_ts = t->ix_es;			/* Input table size so far */

			/* Now figure weighting and vertex offset */

			/* See if we can fit them into separately readable entries, or whether */
			/* they should be combined to minimise overall table size. */

			if ((wit = findord(f, f->wemnb)) < 0) {
				fprintf(stderr,"Can't find entry size to hold weighting factor\n");
				exit(-1);
			}
			if ((oft = findord(f, f->vomnb)) < 0) {
				fprintf(stderr,"Can't find entry size to hold vertex offset index\n");
				exit(-1);
			}
			bigt = wit > oft ? wit : oft;			/* Bigest separate type */

			if ((combt = findord(f, f->wemnb + f->vomnb)) < 0) {/* Combined isn't possible */
				sepbits = 2 * a->ords[bigt].bits;		/* Total separate bits */
				combits = sepbits;						/* Force separate entries */
			} else {
				sepbits = 2 * a->ords[bigt].bits;		/* Total separate bits */
				combits = a->ords[combt].bits;			/* Total combined bits */
			}

			if (sepbits <= combits) {				/* We will use separate entries */
				t->wo_xs = 1;
				t->we_es = a->ords[bigt].bits/8;	/* size in bytes for weighting entry */
				t->we_ab = a->ords[bigt].bits;		/* bits available for weighting */
				t->we_eo = t->ix_es;				/* Entry offset in input table */
				t->vo_es = a->ords[bigt].bits/8;	/* size in bytes for vertex offset entry */
				t->vo_ab = a->ords[bigt].bits;		/* bits available for vertex offset */
				t->vo_eo = t->ix_es + t->we_es;		/* Entry offset in input table */
				t->wo_es = t->we_es + t->vo_es;		/* Total entry size for each vertex */
				t->it_ts += t->we_es + t->vo_es;	/* Total input entry size in bytes */

				f->weet = bigt;				/* Variable type for accessing weighting entry */
				f->voet = bigt;				/* Variable type for accessing vertex offset entry */
				f->wevt = nord(f, wit);		/* Variable type for holding weight value */
				f->vovt = nord(f, oft);		/* Variable type for holding offset value */

			} else {								/* We will combine the two entries */
				t->wo_xs = 0;
				t->wo_es = a->ords[combt].bits/8;	/* entry size in bytes for each entry */
				t->wo_ab = a->ords[combt].bits;		/* bits in weightig + offset */
				t->we_ab = f->wemnb;				/* bits available for weighting */
				t->vo_ab = t->wo_ab - t->we_ab;		/* Allow all spare bits to vertex offset */
				t->wo_eo = t->ix_es;				/* entry offset in input table */
				t->it_ts += t->wo_es;				/* Final input table size */

				f->woet = combt;			/* Variable type for accessing combined entry */
				f->wovt = nord(f, combt);	/* Variable type holding weight/offset read value */

				if ((f->wevt = findnord(f, t->we_ab)) < 0) {
					fprintf(stderr,"Can't find variable size to hold weighting factor\n");
					exit(-1);
				}
				if ((f->vovt = findnord(f, t->vo_ab)) < 0) {
					fprintf(stderr,"Can't find variable size to hold vertex offset index\n");
					exit(-1);
				}
			}
		}
#ifdef VERBOSE
		/* Summarise the input table arrangements */
		printf("\n");
		printf("Input table structure:\n");
		printf("  Input table entry size = %d bytes\n",t->it_ts);
		if (t->it_ix) {
			printf("  Input table extracts value from read values\n");
			if (t->wo_xs) {
				printf("  Separate Interp., Weighting and Offset values\n");
				printf("  Interp. index is at offset %d, size %d bytes\n",t->ix_eo, t->ix_es);
				printf("  Weighting is at offset %d, size %d bytes\n",t->we_eo, t->we_es);
				printf("  Vertex offset is at offset %d, size %d bytes\n",t->vo_eo, t->vo_es);
			} else {
				printf("  Separate Interp. index and Weightint+Offset value\n");
				printf("  Interp. index is at offset %d, size %d bytes\n",t->ix_eo, t->ix_es);
				printf("  Weighting+Offset is at offset %d, size %d bytes\n",t->wo_eo, t->wo_es);
				printf("  Weighting     = %d bits\n",t->we_ab);
				printf("  Vertex offset = %d bits\n",t->vo_ab);
			}
		} else {
			printf("  Combined InterpIndex+Weighting+Voffset values\n");
			printf("  Values are stored in size %d bytes\n",t->it_ts);
			printf("  Interp. index = %d bits\n",t->ix_ab);
			printf("  Weighting     = %d bits\n",t->we_ab);
			printf("  Vertex offset = %d bits\n",t->vo_ab);
		}
#endif /* VERBOSE */

	} else {	/* Simplex table */
		/* If we are going to use a simplex table, figure out how we */
		/* will store the weighting value and vertex offset values in it, */
		/* as well as the size of index we'll need to address it. */
		int wit, oft, bigt;		/* weighting type, offset type, biggest type */
		int combt;				/* Combined type */
		int sepbits, combits;	/* Total separate, combined bits */

		/* See if we can fit them into separately readable entries, or whether */
		/* they should be combined to minimise overall table size. */

		if ((wit = findord(f, f->wemnb)) < 0) {
			fprintf(stderr,"Can't find entry size to hold weighting factor\n");
			exit(-1);
		}
		if ((oft = findord(f, f->vomnb)) < 0) {
			fprintf(stderr,"Can't find entry size to hold vertex offset index\n");
			exit(-1);
		}
		bigt = wit > oft ? wit : oft;			/* Bigest separate type */

		if ((combt = findord(f, f->wemnb + f->vomnb)) < 0) {/* Combined isn't possible */
			sepbits = 2 * a->ords[bigt].bits;		/* Total separate bits */
			combits = sepbits;						/* Force separate entries */
		} else {
			sepbits = 2 * a->ords[bigt].bits;		/* Total separate bits */
			combits = a->ords[combt].bits;			/* Total combined bits */
		}

		if (sepbits <= combits) {				/* We will use separate entries */
			t->wo_xs = 1;
			t->we_es = a->ords[bigt].bits/8;	/* size in bytes for weighting entry */
			t->we_ab = a->ords[bigt].bits;		/* bits available for weighting */
			t->we_eo = 0;						/* Entry offset in simplex table */
			t->vo_es = a->ords[bigt].bits/8;	/* size in bytes for vertex offset entry */
			t->vo_ab = a->ords[bigt].bits;		/* bits available for vertex offset */
			t->vo_eo = t->we_es;				/* Entry offset in simplex table */
			t->wo_es = t->we_es + t->vo_es;		/* Total entry size for each vertex */
			t->sm_ts = (g->id + 1) * (t->we_es + t->vo_es) ;	/* Total size in bytes */

			f->weet = bigt;				/* Variable type for accessing weighting entry */
			f->voet = bigt;				/* Variable type for accessing vertex offset entry */
			f->wevt = nord(f, wit);		/* Variable type for holding weight value */
			f->vovt = nord(f, oft);		/* Variable type for holding offset value */

		} else {								/* We will combine the two entries */
			t->wo_xs = 0;
			t->wo_es = a->ords[combt].bits/8;	/* entry size in bytes for each entry */
			t->wo_ab = a->ords[combt].bits;		/* bits in weightig + offset */
			t->we_ab = f->wemnb;				/* bits available for weighting */
			t->vo_ab = t->wo_ab - t->we_ab;		/* Allow all spare bits to vertex offset */
			t->wo_eo = 0;						/* entry offset in simplex table */
			t->sm_ts = (g->id + 1) * t->wo_es;	/* Total size in bytes */

			f->woet = combt;			/* Variable type for accessing combined entry */
			f->wovt = nord(f, combt);	/* Variable type holding weight/offset read value */

			if ((f->wevt = findnord(f, t->we_ab)) < 0) {
				fprintf(stderr,"Can't find variable size to hold weighting factor\n");
				exit(-1);
			}
			if ((f->vovt = findnord(f, t->vo_ab)) < 0) {
				fprintf(stderr,"Can't find variable size to hold vertex offset index\n");
				exit(-1);
			}
		}

		/* Compute the number of bits needed to hold an index into */
		/* the simplex table (index is in terms of table entry size). */
		/* This value is used to figure out the room needed in the input */
		/* table to accumulate the simplex cube base offset value. (SW_O macro) */
		f->sxmnb = calc_bits(g->id, g->stres);

#ifdef VERBOSE
		/* Summarise the simplex table arrangements */
		printf("\n");
		printf("Simplex table structure:\n");
		printf("  Minimum bits needed to index table %d\n", f->sxmnb);
		printf("  Total simplex entry size %d bytes to hold %d entries\n",t->sm_ts, g->id+1);
		if (t->wo_xs) {
			printf("  Separate entries for offset and weight\n");
			printf("  Weighting entry size %d bytes\n",t->we_es);
			printf("  Offset entry size %d bytes\n",t->vo_es);
		} else {
			printf("  Combined offset and weight entries in %d bytes\n",t->wo_es);
			printf("  Weighting entry size %d bits\n",t->we_ab);
			printf("  Offset entry size %d bits\n",t->vo_ab);
		}
		printf("  Vertex offset scale factor %d\n", t->vo_om);
#endif /* VERBOSE */

		/* We known how big the interpolation and simplex */
		/* tables indexes are going to be, so complete figuring out */
		/* how big the input table entries have to be. */
		if ((f->itet = findord(f, f->sxmnb + f->ixmnb)) >= 0) {/* size to read */
			int rem;						/* Remainder bits */

			t->it_xs = 0;					/* Combined simplex+interp index */

			t->it_ab = a->ords[f->itet].bits;	/* Bits in combined input entry */
			rem = t->it_ab - f->sxmnb - f->ixmnb;
			t->sx_ab = f->sxmnb + rem/2;		/* simplex index bits actually available */
			t->ix_ab = t->it_ab - t->sx_ab;		/* interp index bits actually available */
			t->it_ts = a->ords[f->itet].bits/8;	/* total size in bytes */
			f->itvt = nord(f, f->itet);			/* Variable type */

			if ((f->sxvt = findnord(f, t->sx_ab)) < 0) {
				fprintf(stderr,"Simplex index variable size can't be handled\n");
				exit(-1);
			}
			if ((f->ixvt = findnord(f, t->ix_ab)) < 0) {
				fprintf(stderr,"Interp index variable size can't be handled\n");
				exit(-1);
			}
		} else {						/* Separate entries */
			int bbits;					/* Largest number of bits needed */

			t->it_xs = 1;				/* Separate simplex+interp indexes */
			bbits = f->sxmnb > f->ixmnb ? f->sxmnb : f->ixmnb;

			/* Allocate same size for both so that total structure size is power of 2 */
			if ((f->sxet = f->ixet = findord(f, bbits)) < 0) {
				fprintf(stderr,"Interp/Simplex index entry size can't be handled\n");
				exit(-1);
			}

			t->sx_ab = a->ords[f->sxet].bits;		/* Actual bits available */
			t->sx_es = t->sx_ab/8;					/* Entry size in bytes */
			t->ix_ab = a->ords[f->ixet].bits;
			t->ix_es = t->sx_ab/8;
			t->it_ts = t->sx_es + t->ix_es;		/* total size in bytes */
			t->sx_eo = 0;						/* simplex index offset in bytes */
			t->ix_eo = t->sx_es;				/* interp. index offset in bytes */
			f->sxvt = nord(f, f->sxet);			/* Variable type */
			f->ixvt = nord(f, f->ixet);			/* Variable type */
		}

#ifdef VERBOSE
		/* Summarise the input table arrangements */
		printf("\n");
		printf("Input table structure:\n");
		if (t->it_ix) {
			printf("  Input table extracts value from read values\n");
		} else {
			printf("  Value extraction read values is explicit\n");
		}
		printf("  Input table entry size = %d bytes\n",t->it_ts);
		if (t->it_xs) {
			printf("  Separate Interp. and Simplex index values\n");
			printf("  Interp. index is at offset %d, size %d bytes\n",t->ix_eo, t->ix_es);
			printf("  Simplex index is at offset %d, size %d bytes\n",t->sx_eo, t->sx_es);
		} else {
			printf("  Combined Interp. and Simplex index values\n");
			printf("  Values are size %d bytes\n",t->it_ts);
			printf("  Interp. index = %d bits\n",t->ix_ab);
			printf("  Simplex index = %d bits\n",t->sx_ab);
		}
#endif /* VERBOSE */
	}

	/* Figure out output table stuff */
	{
		/* A variable to hold the index into an output table */
		if ((f->otit = findord(f, g->prec)) < 0) {
			fprintf(stderr,"Can't find output table index size\n");
			exit(-1);
		}
		f->otit = nord(f,f->otit);				/* Make temp variable natural size */

		if (g->out.pint != 0)	/* Pixel interleaved */
			f->nop = 1;			/* Use same pointers for every pixel */
		else
			f->nop = g->od;		/* Use a separate pointer for each output value */
	
		/* Figure out the output pointer types */
		f->otvt = 0;			/* Output table value type */
		for (e = 0; e < f->nop; e++) {
			if ((f->opt[e] = findord(f, g->out.bpch[e])) < 0) {
				fprintf(stderr,"Output channel size can't be handled\n");
				exit(-1);
			}
			if (f->opt[e] > f->otvt)
				f->otvt = f->opt[e];	/* Make value type big enough for any channel size */
		}
		t->ot_ts = a->ords[f->otvt].bits/8;	/* Output table entry size in bytes */

		/* Setup information on data placement in output table entries */
		for (e = 0; e < g->od; e++) {
			t->ot_off[e] = g->out.bov[e];		/* Transfer info from generation spec. */
			t->ot_bits[e] = g->out.bpv[e];
		}
	}

#ifdef VERBOSE
	/* Summarise the output table arrangements */
	printf("Output table structure:\n");
	printf("  Entry size = %d bytes\n",t->ot_ts);
	printf("  Output value placement within each enry is:\n");
	for (e = 0; e < f->nop; e++) {
		printf("    %d: Offset %d bits, size %d bits\n", e, t->ot_off[e], t->ot_bits[e]);
	}
#endif /* VERBOSE */

	/* Compute the maximum interpolation table resolution we will be able to handle */
	{
		int res, ores;

		res = calc_res(g->id, t->ix_ab);
		ores = calc_ores(g->id, t->vo_ab, t->vo_om);
		f->ixmxres = res < ores ? res : ores;
	}

	/* Compute the maximum simplex table resolution we will be able to handle */
	if (t->sort) {
		f->sxmxres = 0;
	} else {
		f->sxmxres = calc_res(g->id, t->sx_ab);
	}

#ifdef VERBOSE
	printf("Emitting introductory code\n"); fflush(stdout);
#endif /* VERBOSE */

	/* Start of code generation */
	doheader(f);			/* Output the header comments */

	/* We need an include file */
	line(f,"#ifndef  IMDI_INCLUDED");
	line(f,"#include <memory.h>");
	line(f,"#include \"imdi_utl.h\"");
	line(f,"#define  IMDI_INCLUDED");
	line(f,"#endif  /* IMDI_INCLUDED */");
	cr(f);

	/* Declare our explicit pointer type */
	line(f,"#ifndef DEFINED_pointer");
	line(f,"#define DEFINED_pointer");
	line(f,"typedef unsigned char * pointer;");
	line(f,"#endif");
	cr(f);

	/* Declare our explicit structure access macros */

#ifdef VERBOSE
	printf("Declaring macros\n"); fflush(stdout);
#endif /* VERBOSE */

	/* Macros for accessing input table entries */
	if (t->sort) {
		if (t->it_xs) {
			line(f,"/* Input table interp. index */");
			line(f,"#define IT_IX(p, off) *((%s *)((p) + %d + (off) * %d))",
			     a->ords[f->ixet].name, t->ix_eo, t->it_ts);
			cr(f);
			if (t->wo_xs) {
				line(f,"/* Input table input weighting enty */");
				line(f,"#define IT_WE(p, off) *((%s *)((p) + %d + (off) * %d))",
				     a->ords[f->weet].name, t->we_eo, t->it_ts);
				cr(f);
				line(f,"/* Input table input offset value enty */");
				line(f,"#define IT_VO(p, off) *((%s *)((p) + %d + (off) * %d))",
				     a->ords[f->voet].name, t->vo_eo, t->it_ts);
				cr(f);
			} else {
				line(f,"/* Input table input weighting/offset value enty */");
				line(f,"#define IT_WO(p, off) *((%s *)((p) + %d + (off) * %d))",
				     a->ords[f->woet].name, t->wo_eo, t->it_ts);
				cr(f);
			}
		} else {
			line(f,"/* Input table interp index, weighting and vertex offset */");
			line(f,"#define IT_IT(p, off) *((%s *)((p) + %d + (off) * %d))",
			     a->ords[f->itet].name, 0, t->it_ts);
			cr(f);
		}

		/* Sort primitive macro's */
		line(f,"/* Sorting macros */");
		if (t->wo_xs) {
			line(f,"#define XFR(A, AA, B, BB) A = B; AA = BB;");
			line(f,"#define CEX(A, AA, B, BB) if (A < B) { \\");
			line(f,"            A ^= B; B ^= A; A ^= B; AA ^= BB; BB ^= AA; AA ^= BB; }");
			line(f,"#define CXJ(A, B, BB, D, DD, L) if (A >= B) { D = B; DD = BB; goto L; }");
		} else {
			line(f,"#define XFR(A, B) A = B;");
			line(f,"#define CEX(A, B) if (A < B) { A ^= B; B ^= A; A ^= B; }");
			line(f,"#define CXJ(A, B, D, L) if (A >= B) { D = B; goto L; }");
		}
		line(f,"#define CJ(A, B, L) if (A >= B) goto L;");
		cr(f);

	} else {	/* Simplex table */
		if (t->it_xs) {
			line(f,"/* Input table interp. index */");
			line(f,"#define IT_IX(p, off) *((%s *)((p) + %d + (off) * %d))",
			     a->ords[f->ixet].name, t->ix_eo, t->it_ts);
			cr(f);
			line(f,"/* Input table simplex index enty */");
			line(f,"#define IT_SX(p, off) *((%s *)((p) + %d + (off) * %d))",
			     a->ords[f->sxet].name, t->sx_eo, t->it_ts);
			cr(f);
		} else {
			line(f,"/* Input table inter & simplex indexes */");
			line(f,"#define IT_IT(p, off) *((%s *)((p) + %d + (off) * %d))",
			     a->ords[f->itet].name, 0, t->it_ts);
			cr(f);
		}
	}

	if (!t->sort) {
		/* Macro for computing a simplex table entry */
		line(f,"/* Simplex weighting table access */");
		line(f,"#define SW_O(off) ((off) * %d)", t->sm_ts);
		cr(f);

		/* Macros for accessing the contents of the simplex table */
		if (t->wo_xs) { 			/* If separate */
			line(f,"/* Simplex table - get weighting value */");
			line(f,"#define SX_WE(p, v) *((%s *)((p) + (v) * %d + %d))",
			     a->ords[f->weet].name, t->wo_es, t->we_eo);
			cr(f);

			line(f,"/* Simplex table - get offset value */");
			line(f,"#define SX_VO(p, v) *((%s *)((p) + (v) * %d + %d))",
			     a->ords[f->voet].name, t->wo_es, t->vo_eo);
			cr(f);

		} else {	/* Combined */
			line(f,"/* Simplex table - get weighting/offset value */");
			line(f,"#define SX_WO(p, v) *((%s *)((p) + (v) * %d))",
			     a->ords[f->woet].name, t->wo_es);
			cr(f);
		}
	}

	/* Macro for computing an interpolation table entry */
	line(f,"/* Interpolation multi-dim. table access */");
	line(f,"#define IM_O(off) ((off) * %d)", t->im_ts);
	cr(f);

	/* Macro for accessing an entry in the interpolation table */
	line(f,"/* Interpolation table - get vertex values */");

	if (t->im_fn > 0) {
		/* Arguments to macro are cell base address, vertex offset, data offset */

		if (f->imfvt == f->iafvt) {	/* Table and accumulator are the same size */
			if (!timp || t->im_fn == 1) 
				line(f,"#define IM_FE(p, v, c) *((%s *)((p) + (v) * %d + (c) * %d))",
				     a->ords[f->imfvt].name, t->im_oc, t->im_fs);
			else {
				line(f,"#define IM_TP(p, v) ((p) + (v) * %d)", t->im_oc);
				line(f,"#define IM_FE(p, c) *((%s *)((p) + (c) * %d))",
				     a->ords[f->imfvt].name, t->im_fs);
			}
		} else {					/* Expand single table entry to accumulator size */
			if (!timp || t->im_fn == 1) 
				line(f,"#define IM_FE(p, v, c) ((%s)*((%s *)((p) + (v) * %d + (c) * %d)))",
				     a->ords[f->iafvt].name,
				     a->ords[f->imfvt].name, t->im_oc, t->im_fs);
			else {
				line(f,"#define IM_TP(p, v) ((p) + (v) * %d)", t->im_oc);
				line(f,"#define IM_FE(p, c) ((%s)*((%s *)((p) + (c) * %d)))",
				     a->ords[f->iafvt].name,
				     a->ords[f->imfvt].name, t->im_fs);
			}
		}
	}
	if (t->im_pn > 0) {
		/* Arguments to macro are cell base address, vertex offset */
		/* There is no data offset since there can be only be one partial entry */

		if (f->imfvt == f->iafvt)	/* Table and accumulator are the same size */
			line(f,"#define IM_PE(p, v) *((%s *)((p) + %d + (v) * %d))",
			     a->ords[f->impvt].name, t->im_fn * t->im_fs, t->im_oc);
		else						/* Expand single table entry to accumulator size */
			line(f,"#define IM_PE(p, v) ((%s)*((%s *)((p) + %d + (v) * %d)))",
			     a->ords[f->iafvt].name,
			     a->ords[f->impvt].name, t->im_fn * t->im_fs, t->im_oc);
	}
	cr(f);

	/* Macro for accessing an output table entry */
	line(f,"/* Output table indexes */");
	line(f,"#define OT_E(p, off) *((%s *)((p) + (off) * %d))",
	     a->ords[f->otvt].name, t->ot_ts);
	cr(f);

	/* =============================================== */

#ifdef VERBOSE
	printf("Starting interpolation function\n"); fflush(stdout);
#endif /* VERBOSE */

	/* Declare the function */
	line(f,"void");
	line(f, "imdi_k%d(",index);
	line(f, "imdi *s,			/* imdi context */");
	line(f, "void **outp,		/* pointer to output pointers */");
	line(f, "int  ostride,		/* optional input component stride */");
	line(f, "void **inp,		/* pointer to input pointers */");
	line(f, "int  istride,		/* optional input component stride */");
	line(f, "unsigned int npix	/* Number of pixels to process */");
	line(f, ") {");
	inc(f);

	/* We need access to the imdi_imp */
	line(f, "imdi_imp *p = (imdi_imp *)(s->impl);");

	/* Declare the input pointers and init them */
	for (e = 0; e < f->nip; e++) {
		if (g->opt & opts_bwd) {
			if (g->opt & opts_istride)
				line(f, "%s *ip%d = (%s *)inp[%d] + (npix-1) * istride;",
				     a->ords[f->ipt[e]].name, e,
				     a->ords[f->ipt[e]].name, e);
			else
				line(f, "%s *ip%d = (%s *)inp[%d] + (npix-1) * %d;",
				     a->ords[f->ipt[e]].name, e,
				     a->ords[f->ipt[e]].name, e,
				     g->in.chi[e]);
		} else {
			g->opt |= opts_fwd;			/* Make sure it's marked for what it is */
			line(f, "%s *ip%d = (%s *)inp[%d];",
			     a->ords[f->ipt[e]].name, e, a->ords[f->ipt[e]].name, e);
		}
	}

	/* Declare the output pointers and init them */
	for (e = 0; e < f->nop; e++) {
		if (g->opt & opts_bwd) {
			if (g->opt & opts_ostride)
				line(f, "%s *op%d = (%s *)outp[%d] + (npix-1) * ostride;",
				     a->ords[f->opt[e]].name, e,
				     a->ords[f->opt[e]].name, e);
			else
				line(f, "%s *op%d = (%s *)outp[%d] + (npix-1) * %d;",
				     a->ords[f->opt[e]].name, e,
				     a->ords[f->opt[e]].name, e,
				     g->out.chi[e]);
		} else {
			line(f, "%s *op%d = (%s *)outp[%d];",
			     a->ords[f->opt[e]].name, e, a->ords[f->opt[e]].name, e);
		}
	}

	/* Declare and intialise the end pointer */
	if (g->opt & opts_bwd) {
		if (g->opt & opts_istride)
			line(f, "%s *ep = (%s *)inp[0] - istride ;",
				    a->ords[f->ipt[0]].name,
			        a->ords[f->ipt[0]].name);
		else
			line(f, "%s *ep = (%s *)inp[0] - %d ;",
				    a->ords[f->ipt[0]].name,
			        a->ords[f->ipt[0]].name, g->in.chi[0]);
	} else {
		if (g->opt & opts_istride)
			line(f, "%s *ep = (%s *)inp[0] + npix * istride ;",
				    a->ords[f->ipt[0]].name,
			        a->ords[f->ipt[0]].name);
		else
			line(f, "%s *ep = (%s *)inp[0] + npix * %d ;",
				    a->ords[f->ipt[0]].name,
			        a->ords[f->ipt[0]].name, g->in.chi[0]);
	}

	/* Declare and initialise the input table pointers */
	for (e = 0; e < g->id; e++)
		line(f,"pointer it%d = (pointer)p->in_tables[%d];",e,e);

	/* Declare and initialise the output table pointers */
	for (e = 0; e < g->od; e++)
		line(f,"pointer ot%d = (pointer)p->out_tables[%d];",e,e);

	if (!t->sort) {
		/* Declare and initialise the Simplex weighting base pointer */
		line(f,"pointer sw_base = (pointer)p->sw_table;");
	}

	/* Declare and initialise the Interpolation multidim base pointer */
	line(f,"pointer im_base = (pointer)p->im_table;");

	/* Figure out whether input channel reads can be used directly as table offsets */
	t->it_ix = 1;				/* Default use input table lookup to extract value */

	if (g->in.packed != 0)
		t->it_ix = 0;				/* Extract will be done explicitly */

	for (e = 0; e < g->id; e++) {
		int ee = (g->in.pint != 0) ? 0 : e;		/* bpch index */

		if ((g->in.bov[e] + g->in.bpv[e]) <= 12)
			continue;							/* Table can do extract */

		if (g->in.bov[e] != 0 || g->in.bpv[e] != g->in.bpch[ee]) {
			t->it_ix = 0;						/* Extract will be done explicitly */
			break;
		}
	}
	
	/* ------------------------------- */
#ifdef VERBOSE
	printf("Starting pixel processing loop\n"); fflush(stdout);
#endif /* VERBOSE */

	/* Start the pixel processing loop */
	cr(f);
	if (g->opt & opts_bwd) {
		sline(f, "for(;ip0 != ep;");

		if (g->opt & opts_istride)
			for (e = 0; e < f->nip; e++)
				mline(f, " ip%d -= istride,", e);
		else
			for (e = 0; e < f->nip; e++)
				mline(f, " ip%d -= %d,", e, g->in.chi[e]);

		if (g->opt & opts_ostride)
			for (e = 0; e < f->nop; e++)
				mline(f, " op%d -= ostride%s", e, ((e+1) < f->nop) ? "," : "");
		else
			for (e = 0; e < f->nop; e++)
				mline(f, " op%d -= %d%s", e, g->out.chi[e], ((e+1) < f->nop) ? "," : "");
	} else {
		sline(f, "for(;ip0 != ep;");

		if (g->opt & opts_istride)
			for (e = 0; e < f->nip; e++)
				mline(f, " ip%d += istride,", e);
		else
			for (e = 0; e < f->nip; e++)
				mline(f, " ip%d += %d,", e, g->in.chi[e]);

		if (g->opt & opts_ostride)
			for (e = 0; e < f->nop; e++)
				mline(f, " op%d += ostride%s", e, ((e+1) < f->nop) ? "," : "");
		else
			for (e = 0; e < f->nop; e++)
				mline(f, " op%d += %d%s", e, g->out.chi[e], ((e+1) < f->nop) ? "," : "");
	}
	eline(f, ") {");
	inc(f);

	/* Declare output value accumulator(s) */
	for (i = 0; i < t->im_fn; i++) {
		line(f,"%s ova%d;	/* Output value accumulator */",a->ords[f->iafvt].name,i);
	}
	for (; i < f->ian; i++) {
		line(f,"%s ova%d;	/* Output value partial accumulator */",a->ords[f->iapvt].name,i);
	}

	/* Context around interp/Simplex table lookup */
	line(f, "{");
	inc(f);

	if (!t->sort)
		line(f,"pointer swp;");		/* Declare Simplex weighting pointer */
	line(f,"pointer imp;");			/* Declare Interpolation multidim pointer */

	/* Declare the input weighting/vertex offset variables */
	if (t->sort) {
		for (e = 0; e < g->id; e++) {
			if (t->wo_xs) {
				line(f,"%s we%d;	/* Weighting value variable */",
				       a->ords[f->wevt].name, e);
				line(f,"%s vo%d;	/* Vertex offset variable */",
				       a->ords[f->vovt].name, e);
			} else {
				line(f,"%s wo%d;	/* Weighting value and vertex offset variable */",
				       a->ords[f->wovt].name, e);
			}
		}
	}

	/* Context around input table processing */
	line(f, "{");
	inc(f);

	/* Declare the table index variables/input weighting/vertex offset variables */
	if (t->sort) {
		if (!t->it_xs)
			line(f,"%s ti;		/* Input table entry variable */",a->ords[f->itvt].name);
		line(f,"%s ti_i;	/* Interpolation index variable */",a->ords[f->ixvt].name);
	} else {
		if (t->it_xs) {
			line(f,"%s ti_s;	/* Simplex index variable */",a->ords[f->sxvt].name);
			line(f,"%s ti_i;	/* Interpolation index variable */",a->ords[f->ixvt].name);
		} else {
			line(f,"%s ti;	/* Simplex+Interpolation index variable */",a->ords[f->itvt].name);
		}
	}

	if (g->in.packed != 0)	/* We need to unpack from a single read */
		line(f,"%s rdv;		/* Read value */",a->ords[f->ipt[0]].name);

	if (t->it_ix == 0) {
		int bv = 0;
		for (e = 0; e < f->nip; e++) {	/* Find largest input type */
			if (f->ipt[e] > bv)
				bv = f->ipt[e];
		}
		bv = nord(f, bv);
		line(f,"%s chv;	/* Channel value */",a->ords[bv].name);
		f->chv_bits = a->ords[bv].bits;
	}
	cr(f);

#ifdef VERBOSE
	printf("Read code\n"); fflush(stdout);
#endif /* VERBOSE */

	/* For all the input channels */
	for (e = 0; e < g->id; e++) {
		char rde[50];		/* Read expression */
		char toff[50];		/* Table offset expression */
		int ee = (g->in.pint != 0) ? 0 : e;		/* bpch index */

		if (g->in.pint != 0) 	/* Pixel interleaved */
			sprintf(rde,"ip0[%d]",e);	/* Offset from single pointer */
		else
			sprintf(rde,"*ip%d",e);		/* Pointer per channel */

		if (g->in.packed != 0) {
			if (e == 0)
				line(f,"rdv = %s;",rde);	/* Do single read */
			sprintf(rde,"rdv");				/* Use read value for extraction */
		}

		if (t->it_ix == 0) {
			if (g->in.bov[e] == 0 ) {				/* No offset */
				if (g->in.bpv[e] == g->in.bpch[ee])	/* No mask */
					line(f,"chv = %s;",rde);
				else								/* Just mask  */
					line(f,"chv = (%s & %s);",rde, hmask(g->in.bpv[e]));
			} else {								/* Offset */
				if ((g->in.bov[e] + g->in.bpv[e]) == g->in.bpch[ee])
					line(f,"chv = (%s >> %d);",rde, g->in.bov[e]);
				else {								/* Offset and mask */
					if (a->shfm || g->in.bpv[e] > 32) {
						/* Extract using just shifts */
						line(f,"chv = ((%s << %d) >> %d);", rde,
						        f->chv_bits - g->in.bpv[e] - g->in.bov[e],
						        f->chv_bits - g->in.bpv[e]);
					} else {
						/* Extract using shift and mask */
						line(f,"chv = ((%s >> %d) & %s);",
						        rde, g->in.bov[e], hmask(g->in.bpv[e]));
					}
				}
			}
			sprintf(toff,"chv");
		} else {									/* No extraction */
			sprintf(toff,"%s",rde);
		}

		if (t->sort) {
			if (t->it_xs) {
				line(f,"ti_i %s= IT_IX(it%d, %s);", e ? "+" : " ", e, toff);
				if (t->wo_xs) {
					line(f,"we%d   = IT_WE(it%d, %s);", e, e, toff);
					line(f,"vo%d   = IT_VO(it%d, %s);", e, e, toff);
				} else {
					line(f,"wo%d   = IT_WO(it%d, %s);", e, e, toff);
				}
			} else {	/* All three combined */
				line(f,"ti = IT_IT(it%d, %s);", e, toff);
				if (a->shfm || t->wo_ab > 32) {
					/* Extract using just shifts */
					line(f,"wo%d   = ((ti << %d) >> %d);	"
					     "/* Extract weighting/vertex offset value */",
					     e, a->ords[f->wovt].bits - t->wo_ab, a->ords[f->wovt].bits - t->wo_ab);
					line(f,"ti_i %s= (ti >> %d);	"
					     "/* Extract interpolation table value */",
					     e ? "+" : " ", t->wo_ab);
				} else {
					/* Extract using shift and mask */
					line(f,"wo%d   = (ti & %s);	"
					     "/* Extract weighting/vertex offset value */",
					     e, hmask(t->wo_ab));
					line(f,"ti_i %s= (ti >> %d);	"
					     "/* Extract interpolation table value */",
					     e ? "+" : " ", t->wo_ab);
				}
			}

		} else {	/* Simplex */
			if (t->it_xs) {
				/* ~~~~ should toff be forced to be a temp variable ?? */
				/* (ie. force use of rde (above) if t->it_xs is nonz) */ 
				line(f,"ti_i %s= IT_IX(it%d, %s);", e ? "+" : " ", e, toff);
				line(f,"ti_s %s= IT_SX(it%d, %s);", e ? "+" : " ", e, toff);
			} else {
				line(f,"ti %s= IT_IT(it%d, %s);", e ? "+" : " ", e, toff);
			}
		}
	}

#ifdef VERBOSE
	printf("Index extraction code\n"); fflush(stdout);
#endif /* VERBOSE */

	cr(f);

	if (t->sort) {
		/* Extract Simplex and Interpolation indexes from accumulator */
		line(f,"imp = im_base + IM_O(ti_i);		/* Compute interp. table entry pointer */");
	} else {
		if (t->it_xs) {		/* Extract Simplex and Interpolation indexes from accumulator */
			line(f,"swp = sw_base + SW_O(ti_s);		/* Compute simplex table entry pointer */");
			line(f,"imp = im_base + IM_O(ti_i);		/* Compute interp. table entry pointer */");
		} else {
			line(f,"imp = im_base + IM_O(ti >> %d);		"
			     "/* Extract interp. index and comp. entry */",
			     t->sx_ab);
			if (a->shfm || t->sx_ab > 32) {
				/* Extract using just shifts */
				line(f,"swp = sw_base + SW_O((ti << %d) >> %d);	"
				     "/* Extract simplex index & comp. entry */",
				     a->ords[f->itvt].bits - t->sx_ab, a->ords[f->itvt].bits - t->sx_ab);
			} else {
				/* Extract using shift and mask */
				line(f,"swp = sw_base + SW_O(ti & %s);	"
				     "/* Extract simplex index and comp. entry */",
				     hmask(t->sx_ab));
			}
		}
	}

	/* Do the explicit sort now */
	if (t->sort) {
		cr(f);
		/* Sort from largest to smallest */
		/* We can use a selection sort, or an insertions sort. */

		line(f,"/* Sort weighting values and vertex offset values */");

		if (g->id >= INSTHRESH) {
			/* We do an insertion sort */
			lineinc(f,"{");
			if (t->wo_xs) {
				line(f,"%s wet;	/* Sort temporary */", a->ords[f->wevt].name);
				line(f,"%s vot;	/* Sort temporary */", a->ords[f->vovt].name);
			} else
				line(f,"%s wot;	/* Sort temp variable */", a->ords[f->wovt].name);
			cr(f);

			for (i = 1; i < g->id; i++) {
				int j;

				j = i;
				if (j < 2) {	/* Only test & exchange needed */
					if (t->wo_xs)
						line(f,"CEX(we%d, vo%d, we%d, vo%d);",j-1,j-1,j,j);
					else
						line(f,"CEX(wo%d, wo%d);",j-1,j);

				} else {
					if (t->wo_xs)
						line(f,"XFR(wet, vot, we%d, vo%d);",j,j);
					else
						line(f,"XFR(wot, wo%d);",j);
					while (j > 0) {
						if (j == i) {		/* First test from i */
							if (t->wo_xs)
								line(f,"CJ(we%d, wet, shs%d);",j-1,i);
							else
								line(f,"CJ(wo%d, wot, shs%d);",j-1,i);
							if (t->wo_xs)
								line(f,"XFR(we%d, vo%d, we%d, vo%d);",j,j,j-1,j-1);
							else
								line(f,"XFR(wo%d, wo%d);",j,j-1);
						} else {
							if (t->wo_xs)
								line(f,"CXJ(we%d, wet, vot, we%d, vo%d, shs%d);",j-1,j,j,i);
							else
								line(f,"CXJ(wo%d, wot, wo%d, shs%d);",j-1,j,i);
							if (t->wo_xs)
								line(f,"XFR(we%d, vo%d, we%d, vo%d);",j,j,j-1,j-1);
							else
								line(f,"XFR(wo%d, wo%d);",j,j-1);
						}
						j--;
					}
					if (t->wo_xs)
						line(f,"XFR(we%d, vo%d, wet, vot);",j,j);
					else
						line(f,"XFR(wo%d, wot);",j);
					niline(f,"shs%d:;",i);
				}
			}
			decline(f,"}");

		} else {
			/* Use a selection sort */
			for (i = 0; i < (g->id-1); i++) {
				for (e = i+1; e < g->id; e++) {
					if (t->wo_xs)
						line(f,"CEX(we%d, vo%d, we%d, vo%d);",i,i,e,e);
					else
						line(f,"CEX(wo%d, wo%d);",i,e);
				}
			}
		}
	}

	/* End of input table processing context */
	dec(f);
	line(f,"}");

	line(f,"{");	/* Context around vertex lookup and accumulation */
	inc(f);

	/* Declare vertex offset and weight variables */
	if (t->sort && t->wo_xs == 0) {
		line(f,"%s nvof;	/* Next vertex offset value */",a->ords[f->vovt].name);
	} else {
		if (!t->wo_xs)	/* If combined in table */
			line(f,"%s vowr;	/* Vertex offset/weight value */",a->ords[f->wovt].name);
	}
	line(f,"%s vof;	/* Vertex offset value */",a->ords[f->vovt].name);
	line(f,"%s vwe;	/* Vertex weighting */",a->ords[f->wevt].name);
	if (timp && t->im_fn > 1) 
		line(f,"pointer timp;		/* Temporary interpolation table pointer */");
	cr(f);

#ifdef VERBOSE
	printf("Vertex offset and weight code\n"); fflush(stdout);
#endif /* VERBOSE */

	/* For each vertex in the simplex */
	for (e = 0; e < (g->id +1); e++) {

		if (t->sort) {

			if (e == 0) {
				line(f,"vof = 0;				/* First vertex offset is 0 */");
			} else {
				if (t->wo_xs)
					line(f,"vof += vo%d;			/* Move to next vertex */",e-1);
				else
					line(f,"vof += nvof;			/* Move to next vertex */");
			}

			/* Extract the vertex offset and weight values from the sorted input values */
			if (e < g->id && !t->wo_xs) {
				if (a->shfm || t->vo_ab > 32) {
					/* Extract using just shifts */
					line(f,"nvof = ((wo%d << %d) >> %d);	"
					     "/* Extract offset value */",
					     e, a->ords[f->vovt].bits - t->vo_ab, a->ords[f->vovt].bits - t->vo_ab);
					line(f,"wo%d = (wo%d >> %d);	"
					     "	/* Extract weighting value */",
					     e, e, t->vo_ab);
				} else {
					/* Extract using shift and mask */
					line(f,"nvof = (wo%d & %s);	"
					     "/* Extract offset value */",
					     e, hmask(t->vo_ab));
					line(f,"wo%d = (wo%d >> %d);	"
					     "	/* Extract weighting value */",
					     e, e, t->vo_ab);
				}
			}
			/* Compute the weighting value */
			if (!t->wo_xs) {
				if (e == 0) {
					line(f,"vwe = %d - wo%d;		/* Baricentric weighting */", 1 << g->prec, e);
				} else if (e < g->id) {
					line(f,"vwe = wo%d - wo%d;		/* Baricentric weighting */", e-1, e);
				} else {
					line(f,"vwe = wo%d;				/* Baricentric weighting */", e-1);
				}
			} else {
				if (e == 0) {
					line(f,"vwe = %d - we%d;		/* Baricentric weighting */", 1 << g->prec, e);
				} else if (e < g->id) {
					line(f,"vwe = we%d - we%d;		/* Baricentric weighting */", e-1, e);
				} else {
					line(f,"vwe = we%d;				/* Baricentric weighting */", e-1);
				}
			}

		} else {	/* Not sort */
			/* Read the vertex offset and weight values from the simplex table */
			if (t->wo_xs) { 			/* If separate */
				line(f,"vof = SX_VO(swp, %d);	/* Read vertex offset value */", e);
				line(f,"vwe = SX_WE(swp, %d);	/* Read vertex weighting value */", e);
			} else { 			/* If combined in table */
				line(f,"vowr = SX_WO(swp, %d);	/* Read vertex offset+weighting values */", e);
				if (a->shfm || t->vo_ab > 32) {
					/* Extract using just shifts */
					line(f,"vof = ((vowr << %d) >> %d);	"
					     "/* Extract offset value */",
					     a->ords[f->vovt].bits - t->vo_ab, a->ords[f->vovt].bits - t->vo_ab);
					line(f,"vwe = (vowr >> %d);	"
					     "/* Extract weighting value */",
					     t->vo_ab);
				} else {
					/* Extract using shift and mask */
					line(f,"vof = (vowr & %s);	"
					     "/* Extract offset value */",
					     hmask(t->vo_ab));
					line(f,"vwe = (vowr >> %d);	"
					     "/* Extract weighting value */",
					     t->vo_ab);
				}
			}
		}

		/* Lookup the vertex value, weight it, and accumulate it into output value */
		if (timp && t->im_fn > 1) 
			line(f,"timp = IM_TP(imp, vof);	/* Vertex address */");
		for (i = 0; i < f->ian; i++) {		/* For each output accumulation chunk */
			if (i < t->im_fn) { 	/* Full entry */
				if (!timp || t->im_fn == 1) 
					line(f,"ova%d %s= IM_FE(imp, vof, %d) * vwe;	"
					     "/* Accumulate weighted output values */",
					     i, e ? "+" : " ", i);
				else
					line(f,"ova%d %s= IM_FE(timp, %d) * vwe;	"
					     "/* Accumulate weighted output values */",
					     i, e ? "+" : " ", i);
			} else				/* One partial entry */
				line(f,"ova%d %s= IM_PE(imp, vof) * vwe;	"
				     "/* Accumulate last weighted output values */",
				     i, e ? "+" : " ");
		}
	}

	dec(f);
	line(f, "}"); 	/* End of output value lookup context */

	dec(f);
	line(f, "}"); 	/* End of output value accumulation context */

	/* Start of output lookup and write */
	line(f,"{");
	inc(f);

#ifdef VERBOSE
	printf("Output table code\n"); fflush(stdout);
#endif /* VERBOSE */

	{
		char wre[50];		/* Write destination expression */

		if (g->out.packed != 0)	/* We need to pack results into a single write */
			line(f,"%s wrv;		/* Write value */",a->ords[f->ipt[0]].name);
	
		/* Declare temporary to hold index into output lookup table */
		line(f,"%s oti;	/* Vertex offset value */",a->ords[f->otit].name);
		if (g->oopt & OOPTS_CHECK)
			line(f,"%s otv;	/* Output temporary value */",a->ords[f->otvt].name);
	
		/* For each accumulator value */
		/* (Assume they are in output order for the moment ?) */
		for (e = i = 0; i < f->ian; i++) {		/* For each output accumulation chunk */
			int vpa = i < t->im_fn ? t->im_fv : t->im_pv;		/* Chanel values per accumulator */
			int oat = i < t->im_fn ? f->iafvt : f->iapvt;		/* Output accumulator type */
			int ee;		/* Relative e to this accumulator */
	
			/* For each output value in this accumulator */
			for (ee = 0; ee < vpa && e < g->od; ee++, e++) {
				int off, size;		/* Bits to be extracted */
		
				/* Extract wanted 8 bits from the 8.8 bit result in accumulator */
				/* (or 16 bits from 16.16) */
				off = ee * f->iaovb + (f->iaovb - g->prec);	
				size = g->prec;
	
				if (e == 0 || g->out.packed == 0) {
					if (g->out.pint != 0) 			/* Pixel interleaved */
						sprintf(wre,"op0[%d]",e);	/* Offset from single pointer */
					else
						sprintf(wre,"*op%d",e);		/* Pointer per channel */
				}
	
				if (a->shfm || size > 32) {
					/* Extract using just shifts */
#ifdef ROUND
					line(f,"oti = (((ova%d + (1 << %d)) << %d) >> %d);	"
					     "/* Extract integer part of result */",
					     i, off-1, a->ords[oat].bits - off - size, a->ords[oat].bits - size);
#else
					line(f,"oti = ((ova%d << %d) >> %d);	"
					     "/* Extract integer part of result */",
					     i, a->ords[oat].bits - off - size, a->ords[oat].bits - size);
#endif
				} else {
					/* Extract using shift and mask */
#ifdef ROUND
					line(f,"oti = (((ova%d + 0x%x) >> %d) & %s);	"
					     "/* Extract integer part of result */",
					     i, (1 << off-1), off, hmask(size));
#else
					line(f,"oti = ((ova%d >> %d) & %s);	"
					     "/* Extract integer part of result */",
					     i, off, hmask(size));
#endif
				}
	
				if (g->oopt & OOPT(oopts_check,e)) {	/* Lookup with check */
					line(f,"otv = OT_E(ot%d, oti);	/* Fetch result */", e);
					line(f,"if (otv != p->checkv[%d])	/* Do output value check */", e);
					line(f,"	p->checkf |= (1 << %d);	/* Set check flag */", e);
					if (g->out.packed != 0) {
						if (g->oopt & OOPT(oopts_skip,e))
							return 2;		/* Error, can't skip on pixel interleaved */
						line(f,"wrv %s= otv;", e ? "+" : "", e);
					} else {
						if (g->oopt & OOPT(oopts_skip,e)) {
							line(f,"if ((p->skipf & (1 << %d)) == 0)	/* If not being skipped */", e);
							line(f,"	%s = otv;	/* Write result */", wre);
						} else
							line(f,"%s = otv;	/* Write result */", wre);
					}
				} else {		/* Normal lookup output table */
					/* Lookup in output table and write to destination */
					if (g->out.packed != 0) {
						if (g->oopt & OOPT(oopts_skip,e))
							return 2;		/* Error, can't skip on pixel interleaved */
						line(f,"wrv %s= OT_E(ot%d, oti);", e ? "+" : "", e);
					} else {
						if (g->oopt & OOPT(oopts_skip,e)) {
							line(f,"if ((p->skipf & (1 << %d)) == 0)	/* If not being skipped */", e);
							line(f,"	%s = OT_E(ot%d, oti);	/* Write result */", wre, e);
						} else
							line(f,"%s = OT_E(ot%d, oti);	/* Write result */", wre, e);
					}
				}
			}
		}
	
		if (g->out.packed != 0) {	/* Write out the accumulated value */
			line(f,"%s = wrv;	/* Write result */", wre);
		}
	}

	/* The end of the output lookup and write */
	dec(f);
	line(f, "}");

	/* The end of the pixel processing loop */
	dec(f);
	line(f, "}");

	/* The end of the function */
	dec(f);
	line(f, "}");

	/* Undefine all the macros */
	if (t->sort) {
		if (t->it_xs) {
			if (t->wo_xs) {
				line(f,"#undef IT_WE");
				line(f,"#undef IT_VO");
			} else
				line(f,"#undef IT_WO");
			line(f,"#undef IT_IX");
		} else {
			line(f,"#undef IT_IT");
		}
		line(f,"#undef CXJ");
		line(f,"#undef CJ");
		line(f,"#undef XFR");
		line(f,"#undef CEX");
	} else {
		if (t->it_xs) {
			line(f,"#undef IT_IX");
			line(f,"#undef IT_SX");
		} else {
			line(f,"#undef IT_IT");
		}

		line(f,"#undef SW_O");
		if (t->wo_xs) {
			line(f,"#undef SX_WE");
			line(f,"#undef SX_VO");
		} else {
			line(f,"#undef SX_WO");
		}
	}
	line(f,"#undef IM_O");
	if (t->im_fn > 0) {
		if (timp && t->im_fn > 1) 
			line(f,"#undef IM_TP");
		line(f,"#undef IM_FE");
	}
	if (t->im_pn > 0) {
		line(f,"#undef IM_PE");
	}
	line(f,"#undef OT_E");

	/* =============================================== */
#ifdef VERBOSE
	printf("Done interpolation code\n"); fflush(stdout);
#endif /* VERBOSE */

	/* =============================================== */

	/* !genspec and tabspec delta code! */
	/* We generate code that updates any entries in the genspec and */
	/* tabpsec strucures that are different for this kernel, */
	/* compared to the previously generated kernel. */
	/* In this way, we save a lot of space, at the price */
	/* of having to access the table of kernels sequentially. */

	/* If the genspec of tabspec structures are modified, */
	/* then corresponding changes need to be made to the code here. */
	{
		int i;
		int s_stres, s_itres;	/* Save values */
		imdi_options s_opt;

		s_stres = g->stres;
		s_itres = g->itres;
		s_opt = g->opt;
		g->stres = f->sxmxres;			/* Set maximum values */
		g->itres = f->ixmxres;
		g->opt &= ~opts_splx;			/* Don't care about this, only about opts_splx/sort */
		if (frv == 0) {					/* Simplex algorithm wasn't possible */
			g->opt &= ~opts_splx_sort;	/* Therefore we don't care about preference */
			g->opt &= ~opts_sort_splx;
		}
		
		/* Declare the genspec & tabspec update function */
		cr(f);
		line(f,"void");
		line(f, "imdi_k%d_gentab(",index);
		line(f, "genspec *g,		/* structure to be updated */");
		line(f, "tabspec *t		/* structure to be updated */");
		line(f, ") {");
		inc(f);
	
#define GSET_ENTRY(KEY) if (g->KEY != og->KEY) line(f, "g->%s = %d;",#KEY,g->KEY)
#define GSET_ARRAY(KEY,IX) if (g->KEY[IX] != og->KEY[IX]) line(f, "g->%s[%d] = %d;",#KEY,IX,g->KEY[IX])
#define TSET_ENTRY(KEY) if (t->KEY != ot->KEY) line(f, "t->%s = %d;",#KEY,t->KEY)
#define TSET_ARRAY(KEY,IX) if (t->KEY[IX] != ot->KEY[IX]) line(f, "t->%s[%d] = %d;",#KEY,IX,t->KEY[IX])

		/* Create code that updates the genspec structure from og to g */ 
		GSET_ENTRY(prec);
		GSET_ENTRY(id);
		GSET_ENTRY(od);
		GSET_ENTRY(irep);
		GSET_ENTRY(orep);
		GSET_ENTRY(in_signed);
		GSET_ENTRY(out_signed);

		/* pixlayout structure */
		for (i = 0; i < IXDIDO; i++) {
			GSET_ARRAY(in.bpch,i);
			GSET_ARRAY(in.chi,i);
			GSET_ARRAY(in.bov,i);
			GSET_ARRAY(in.bpv,i);
		}
		GSET_ENTRY(in.pint);
		GSET_ENTRY(in.packed);

		/* pixlayout structure */
		for (i = 0; i < IXDIDO; i++) {
			GSET_ARRAY(out.bpch,i);
			GSET_ARRAY(out.chi,i);
			GSET_ARRAY(out.bov,i);
			GSET_ARRAY(out.bpv,i);
		}
		GSET_ENTRY(out.pint);
		GSET_ENTRY(out.packed);
		
		GSET_ENTRY(oopt);
		GSET_ENTRY(opt);
		GSET_ENTRY(itres);
		GSET_ENTRY(stres);

		for (i = 0; i < 100; i++) {
			GSET_ARRAY(kkeys,i);
		}
		for (i = 0; i < 100; i++) {
			GSET_ARRAY(kdesc,i);
		}
		for (i = 0; i < 100; i++) {
			GSET_ARRAY(kname,i);
		}

		/* Create code that updates the tabspec structure from og to g */ 
		TSET_ENTRY(sort);
		TSET_ENTRY(it_xs);
		TSET_ENTRY(wo_xs);
		TSET_ENTRY(it_ix);
		TSET_ENTRY(it_ab);
		TSET_ENTRY(it_ts);
		TSET_ENTRY(ix_ab);
		TSET_ENTRY(ix_es);
		TSET_ENTRY(ix_eo);
		TSET_ENTRY(sx_ab);
		TSET_ENTRY(sx_es);
		TSET_ENTRY(sx_eo);
		TSET_ENTRY(sm_ts);
		TSET_ENTRY(wo_ab);
		TSET_ENTRY(wo_es);
		TSET_ENTRY(wo_eo);
		TSET_ENTRY(we_ab);
		TSET_ENTRY(we_es);
		TSET_ENTRY(we_eo);
		TSET_ENTRY(vo_ab);
		TSET_ENTRY(vo_es);
		TSET_ENTRY(vo_eo);
		TSET_ENTRY(vo_om);
		TSET_ENTRY(im_cd);
		TSET_ENTRY(im_ts);
		TSET_ENTRY(im_oc);
		TSET_ENTRY(im_fs);
		TSET_ENTRY(im_fn);
		TSET_ENTRY(im_fv);
		TSET_ENTRY(im_ps);
		TSET_ENTRY(im_pn);
		TSET_ENTRY(im_pv);
		TSET_ENTRY(ot_ts);
		for (i = 0; i < IXDO; i++) {
			TSET_ARRAY(ot_off, i);
		}
		for (i = 0; i < IXDO; i++) {
			TSET_ARRAY(ot_bits,i);
		}

#undef GSET_ENTRY
#undef GSET_ARRAY
#undef TSET_ENTRY
#undef TSET_ARRAY

		/* The end of the function */
		dec(f);
		line(f, "}");

		g->opt = s_opt;			/* Restore entry values */
		g->stres = s_stres;
		g->itres = s_itres;
	}

	/* =============================================== */

	cr(f); cr(f); cr(f); cr(f); cr(f); cr(f);

	return frv;
}


/* Return bits needed to store index into table of */
/* given resolution and dimensionality. */
static int
calc_bits(
int dim,
int res) {

	return (int)ceil(log((double)res) * (double)dim/log(2.0) - 1e-14);
}

/* Return maximum resolution possible given dimensionality */
/* and number of index bits. */
static int
calc_res(
int dim,
int bits) {
	double fres;

	fres = log(2.0) * (double)bits/(double)dim;
	if (fres > 12 || (fres = exp(fres)) > 65536.0)
		fres = 65536.0;		/* Limit to a sane value */
	return (int)(fres + 1e-14);
}

/* Return bits needed to store a relative offset of 1, */
/* into a table of given resolution, dimensionality , and */
/* entry size. */
static int
calc_obits(
int dim,
int res,
int esize) {
	double off;		/* Maximum diagonal offset value */
	int bits;

	if (res == 0 || res == 1)
		return 0;
	if (dim == 1)
		off = esize;
	else {
		off = (double)esize * floor(exp(log((double)res) * dim - log(res-1.0)));
	}

	bits = (int)ceil(log(off)/log(2.0) - 1e-14);
	return bits;
}

/* Return maximum resolution possible given dimensionality */
/* number of index bits, and entry size */
static int
calc_ores(
int dim,
int bits,
int esize) {
	int res;

	/* Find resolution. Stop at arbitrary 65536 */
	for (res = 1; res < 65537; res++) {
		int bn;
		bn = calc_obits(dim, res, esize);
		if (bn > bits) {
			return res-1;
		}
	}
	return res-1;
}



/* Output the introductory comments */
static void
doheader(
	fileo *f
) {
	genspec *g = f->g;
	tabspec *t = f->t;
	mach_arch *a  = f->a;
	int e;

	/* - - - - - - - - - - - - */
	/* Output file title block */
	line(f,"/* Integer Multi-Dimensional Interpolation */");
	line(f,"/* Interpolation Kernel Code */");
	line(f,"/* Generated by cgen */");
	line(f,"/* Copyright 2000 - 2007 Graeme W. Gill */");
	line(f,"/* All rights reserved. */");
	line(f,"/* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :- */\n");
	line(f,"/* see the License.txt file for licencing details.*/\n");
	cr(f);

	/* - - - - - - - - - - - - */
	/* Output the specification */
	line(f,"/*");
	line(f,"   Interpolation kernel specs:");
	cr(f);
	line(f,"   Input channels per pixel = %d",g->id);
	for (e = 0; e < g->id; e++) {
		line(f,"   Input channel %d bits = %d",e, g->in.bpch[e]);
		line(f,"   Input channel %d increment = %d",e, g->in.chi[e]);
	}
	if (g->in.pint != 0)
		line(f,"   Input is channel interleaved");
	else
		line(f,"   Input is plane interleaved");

	if (g->in.packed != 0)
		line(f,"   Input channels are packed into one word");
	else
		line(f,"   Input channels are separate words");

	if (t->it_ix)
		line(f,"   Input value extraction is done in input table lookup");
	cr(f);
	
	line(f,"   Output channels per pixel = %d",g->od);
	for (e = 0; e < g->od; e++) {
		line(f,"   Output channel %d bits = %d",e, g->out.bpch[e]);
		line(f,"   Output channel %d increment = %d",e, g->out.chi[e]);
		if (g->oopt & OOPT(oopts_check,e))
			line(f,"   Output channel %d has value check",e);
		if (g->oopt & OOPT(oopts_skip,e))
			line(f,"   Output channel %d has skip available",e);
	}
	if (g->out.pint != 0)
		line(f,"   Output is channel interleaved");
	else
		line(f,"   Output is plane interleaved");
	if (g->out.packed != 0)
		line(f,"   Output channels are packed into one word");
	else
		line(f,"   Output channels are separate words");
	cr(f);
	
	line(f,"   Basic Internal precision bits  = %d",g->prec);
	if (t->sort)
		line(f,"   Weight+voffset bits       = %d",t->sx_ab);
	else
		line(f,"   Simplex table index bits       = %d",t->sx_ab);
	line(f,"   Interpolation table index bits = %d",t->ix_ab);
	if (!t->sort)
		line(f,"   Simplex table max resolution = %d",f->sxmxres);
	line(f,"   Interpolation table max resolution = %d",f->ixmxres);
	cr(f);
	line(f,"   Processing direction is %s",g->opt & opts_bwd ? "backwards" : "forwards" );
	line(f,"   Input stride is %ssupported",g->opt & opts_istride ? "" : "not " );
	line(f,"   Output stride is %ssupported",g->opt & opts_ostride ? "" : "not " );
	if (g->opt & opts_splx_sort)
		line(f,"   Prefer simplex over sort algorithm");
	if (g->opt & opts_sort_splx)
		line(f,"   Prefer sort over simplex");
	line(f," */");
	cr(f);

	/* - - - - - - - - - - - - */
	line(f,"/*");
	line(f,"   Machine architecture specs:");
	cr(f);
	if (a->bigend != 0)
		line(f,"   Big Endian");
	else
		line(f,"   Little endian");

	if (a->uwa != 0)
		line(f,"   Using maximum sized memory accesses where possible");
	else
		line(f,"   Reading and writing pixel values separately");

	line(f,"   Pointer size = %d bits",a->pbits);
	cr(f);

	for (e = 0; e < a->nords; e++) {
		line(f,"   Ordinal size %2d bits is known as '%s'",
		        a->ords[e].bits,a->ords[e].name);
	}
	line(f,"   Natural ordinal is '%s'", a->ords[a->natord].name);
	cr(f);

	for (e = 0; e < a->nints; e++) {
		line(f,"   Integer size %2d bits is known as '%s'",
		        a->ints[e].bits,a->ints[e].name);
	}
	line(f,"   Natural integer is '%s'", a->ints[a->natint].name);
	cr(f);

	line(f," */");
	cr(f);
}


/* ---------------------------------------- */
/* Architecture support */
/* Find an ordinal with at least bits size */
/* Return -1 if failed */
int findord(
fileo *f,
int bits
) {
	mach_arch *a  = f->a;
	int i;

	for (i = 0; i < a->nords; i++) {
		if (a->ords[i].bits >= bits)
			return i;
	}
	return -1;
}

/* Round ordinal type up to natural size */
int nord(
	fileo *f,
	int ov
) {
	if (ov >= 0 && ov < f->a->natord)
		ov = f->a->natord;
	return ov;
}

/* Find an ordinal with at least bits size, */
/* or natural size, whichever is greater. */
/* Return -1 if failed */
int findnord(
	fileo *f,
	int bits
) {
	int ov;

	ov = findord(f, bits);
	ov = nord(f, ov);
	return ov;
}

/* Find an integer with at least bits size */
/* Return -1 if failed */
int findint(
	fileo *f,
	int bits
) {
	mach_arch *a  = f->a;
	int i;

	for (i = 0; i < a->nints; i++) {
		if (a->ints[i].bits >= bits)
			return i;
	}
	return -1;
}

/* Round integer type up to natural size */
int nint(
	fileo *f,
	int iv
) {
	if (iv >= 0 && iv < f->a->natint)
		iv = f->a->natint;
	return iv;
}

/* Find an interger with at least bits size, */
/* or natural size, whichever is greater. */
/* Return -1 if failed */
int findnint(
	fileo *f,
	int bits
) {
	int iv;

	iv = findint(f, bits);
	iv = nint(f, iv);
	return iv;
}


/* ------------------------------------ */
/* File output support */

/* Output a line to the file (including trailing \n) */
void
line(fileo *f, char *fmt, ...)
{
	int i;
	va_list args;

	/* Indent to the correct level */
	for (i = 0; i < f->indt; i++)
		fprintf(f->of,"	");
	
	va_start(args, fmt);
	vfprintf(f->of, fmt, args);
	va_end(args);
	fprintf(f->of, "\n");
}

/* Output the start of a line to the file) */
void
sline(fileo *f, char *fmt, ...)
{
	int i;
	va_list args;

	/* Indent to the correct level */
	for (i = 0; i < f->indt; i++)
		fprintf(f->of,"	");
	
	va_start(args, fmt);
	vfprintf(f->of, fmt, args);
	va_end(args);
}

/* Output the middle of a line to the file) */
void
mline(fileo *f, char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	vfprintf(f->of, fmt, args);
	va_end(args);
}

/* Output the end of a line to the file (including trailing \n) */
void
eline(fileo *f, char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	vfprintf(f->of, fmt, args);
	va_end(args);
	fprintf(f->of, "\n");
}

/* Output a line to the file (including trailing \n) */
/* No indent */
void
niline(fileo *f, char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	vfprintf(f->of, fmt, args);
	va_end(args);
	fprintf(f->of, "\n");
}

/* Output one line and increment indent */
void lineinc(fileo *f, char *fmt, ...) {
	int i;
	va_list args;

	/* Indent to the correct level */
	for (i = 0; i < f->indt; i++)
		fprintf(f->of,"	");
	
	va_start(args, fmt);
	vfprintf(f->of, fmt, args);
	va_end(args);
	fprintf(f->of, "\n");
	f->indt++;
}

/* Decrement indent and output one line */
void decline(fileo *f, char *fmt, ...) {
	int i;
	va_list args;

	f->indt--;
	/* Indent to the correct level */
	for (i = 0; i < f->indt; i++)
		fprintf(f->of,"	");
	
	va_start(args, fmt);
	vfprintf(f->of, fmt, args);
	va_end(args);
	fprintf(f->of, "\n");
}


/* ------------------------------------ */