xref: /csrg-svn/usr.bin/pascal/libpc/CTTOT.c (revision 36533)
1 /* Copyright (c) 1979 Regents of the University of California */
2 
3 static char sccsid[] = "@(#)CTTOT.c 1.5 01/09/89";
4 
5 #include "whoami.h"
6 #include "h00vars.h"
7 
8 long	_mask[] = {
9 #		ifdef DEC11
10 		    0xffffffff , 0xfffffffe , 0xfffffffc , 0xfffffff8 ,
11 		    0xfffffff0 , 0xffffffe0 , 0xffffffc0 , 0xffffff80 ,
12 		    0xffffff00 , 0xfffffe00 , 0xfffffc00 , 0xfffff800 ,
13 		    0xfffff000 , 0xffffe000 , 0xffffc000 , 0xffff8000 ,
14 		    0xffff0000 , 0xfffe0000 , 0xfffc0000 , 0xfff80000 ,
15 		    0xfff00000 , 0xffe00000 , 0xffc00000 , 0xff800000 ,
16 		    0xff000000 , 0xfe000000 , 0xfc000000 , 0xf8000000 ,
17 		    0xf0000000 , 0xe0000000 , 0xc0000000 , 0x80000000 ,
18 		    0x00000000
19 #		else
20 		    0xffffffff , 0xfeffffff , 0xfcffffff , 0xf8ffffff ,
21 		    0xf0ffffff , 0xe0ffffff , 0xc0ffffff , 0x80ffffff ,
22 		    0x00ffffff , 0x00feffff , 0x00fcffff , 0x00f8ffff ,
23 		    0x00f0ffff , 0x00e0ffff , 0x00c0ffff , 0x0080ffff ,
24 		    0x0000ffff , 0x0000feff , 0x0000fcff , 0x0000f8ff ,
25 		    0x0000f0ff , 0x0000e0ff , 0x0000c0ff , 0x000080ff ,
26 		    0x000000ff , 0x000000fe , 0x000000fc , 0x000000f8 ,
27 		    0x000000f0 , 0x000000e0 , 0x000000c0 , 0x00000080 ,
28 		    0x00000000
29 #		endif DEC11
30 	    };
31 /*
32  * Constant set constructors.
33  *
34  * CTTOT is called from compiled Pascal.  It takes the list of ranges
35  * and single elements on the stack, varargs style.
36  *
37  * CTTOTA is called from the px interpreter.  It takes a pointer to the
38  * list of ranges and single elements.
39  *
40  * This was easier than changing the compiler to pass a pointer into
41  * its own partially-constructed stack, while working to make px portable.
42  */
43 
44 long *CTTOTA();
45 
46 long *
47 CTTOT(result, lwrbnd, uprbnd, paircnt, singcnt, data)
48 
49 	long	*result;	/* pointer to final set */
50 	long	lwrbnd;		/* lower bound of set */
51 	long	uprbnd;		/* upper - lower of set */
52 	long	paircnt;	/* number of pairs to construct */
53 	long	singcnt;	/* number of singles to construct */
54 	long	data;		/* paircnt plus singcnt sets of data */
55 {
56 	return CTTOTA(result, lwrbnd, uprbnd, paircnt, singcnt, &data);
57 }
58 
59 long *
60 CTTOTA(result, lwrbnd, uprbnd, paircnt, singcnt, dataptr)
61 
62 	register long	*result;	/* pointer to final set */
63 	long	lwrbnd;			/* lower bound of set */
64 	long	uprbnd;			/* upper - lower of set */
65 	long	paircnt;		/* number of pairs to construct */
66 	long	singcnt;		/* number of singles to construct */
67 	register long	*dataptr;	/* ->paircnt plus singcnt data values */
68 {
69 	int		lowerbnd = lwrbnd;
70 	int		upperbnd = uprbnd;
71 	register long	*lp;
72 	register char	*cp;
73 	register long	temp;
74 	long		*limit;
75 	int		lower;
76 	int		lowerdiv;
77 	int		lowermod;
78 	int		upper;
79 	int		upperdiv;
80 	int		uppermod;
81 	int		cnt;
82 
83 	limit = &result[(upperbnd + 1 + BITSPERLONG - 1) >> LG2BITSLONG];
84 	for (lp = result; lp < limit; )
85 		*lp++ = 0;
86 	for (cnt = 0; cnt < paircnt; cnt++) {
87 		upper = *dataptr++ - lowerbnd;
88 		if (upper < 0 || upper > upperbnd) {
89 			ERROR("Range upper bound of %D out of set bounds\n",
90 				*--dataptr);
91 		}
92 		lower = *dataptr++ - lowerbnd;
93 		if (lower < 0 || lower > upperbnd) {
94 			ERROR("Range lower bound of %D out of set bounds\n",
95 				*--dataptr);
96 		}
97 		if (lower > upper) {
98 			continue;
99 		}
100 		lowerdiv = lower >> LG2BITSLONG;
101 		lowermod = lower & MSKBITSLONG;
102 		upperdiv = upper >> LG2BITSLONG;
103 		uppermod = upper & MSKBITSLONG;
104 		temp = _mask [lowermod];
105 		if ( lowerdiv == upperdiv ) {
106 			temp &= ~_mask[ uppermod + 1 ];
107 		}
108 		result[ lowerdiv ] |= temp;
109 		limit = &result[ upperdiv-1 ];
110 		for ( lp = &result[ lowerdiv+1 ] ; lp <= limit ; lp++ ) {
111 			*lp |= ~0;
112 		}
113 		if ( lowerdiv != upperdiv ) {
114 			result[ upperdiv ] |= ~_mask[ uppermod + 1 ];
115 		}
116 	}
117 	for (cnt = 0, cp = (char *)result; cnt < singcnt; cnt++) {
118 		lower = *dataptr++ - lowerbnd;
119 		if (lower < 0 || lower > upperbnd) {
120 			ERROR("Value of %D out of set bounds\n", *--dataptr);
121 		}
122 		cp[ lower >> LG2BITSBYTE ] |= (1 << (lower & MSKBITSBYTE));
123 	}
124 	return(result);
125 }
126