xref: /onnv-gate/usr/src/cmd/eqn/shift.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
3*0Sstevel@tonic-gate 
4*0Sstevel@tonic-gate 
5*0Sstevel@tonic-gate /*
6*0Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
7*0Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
8*0Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
9*0Sstevel@tonic-gate  */
10*0Sstevel@tonic-gate 
11*0Sstevel@tonic-gate /*
12*0Sstevel@tonic-gate  * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc.
13*0Sstevel@tonic-gate  * All Rights Reserved.
14*0Sstevel@tonic-gate  */
15*0Sstevel@tonic-gate 
16*0Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.1	*/
17*0Sstevel@tonic-gate 
18*0Sstevel@tonic-gate # include "e.h"
19*0Sstevel@tonic-gate #include "e.def"
20*0Sstevel@tonic-gate 
21*0Sstevel@tonic-gate bshiftb(p1, dir, p2) int p1, dir, p2; {
22*0Sstevel@tonic-gate 	int shval, d1, h1, b1, h2, b2;
23*0Sstevel@tonic-gate #ifndef NEQN
24*0Sstevel@tonic-gate 	int diffps, effps, effps2;
25*0Sstevel@tonic-gate 	char *sh1, *sh2;
26*0Sstevel@tonic-gate #endif NEQN
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate 	yyval = p1;
29*0Sstevel@tonic-gate 	h1 = eht[p1];
30*0Sstevel@tonic-gate 	b1 = ebase[p1];
31*0Sstevel@tonic-gate 	h2 = eht[p2];
32*0Sstevel@tonic-gate 	b2 = ebase[p2];
33*0Sstevel@tonic-gate #ifndef NEQN
34*0Sstevel@tonic-gate 	effps = EFFPS(ps);
35*0Sstevel@tonic-gate 	effps2 = EFFPS(ps+deltaps);
36*0Sstevel@tonic-gate 	diffps = deltaps;
37*0Sstevel@tonic-gate 	sh1 = sh2 = "";
38*0Sstevel@tonic-gate #endif NEQN
39*0Sstevel@tonic-gate 	if( dir == SUB ) {	/* subscript */
40*0Sstevel@tonic-gate #ifndef NEQN
41*0Sstevel@tonic-gate 		/* top 1/2m above bottom of main box */
42*0Sstevel@tonic-gate 		d1 = VERT(EM(0.5, effps2));
43*0Sstevel@tonic-gate #else NEQN
44*0Sstevel@tonic-gate 		d1 = VERT(1);
45*0Sstevel@tonic-gate #endif NEQN
46*0Sstevel@tonic-gate 		shval = - d1 + h2 - b2;
47*0Sstevel@tonic-gate 		if( d1+b1 > h2 ) /* move little sub down */
48*0Sstevel@tonic-gate 			shval = b1-b2;
49*0Sstevel@tonic-gate 		ebase[yyval] = b1 + max(0, h2-b1-d1);
50*0Sstevel@tonic-gate 		eht[yyval] = h1 + max(0, h2-b1-d1);
51*0Sstevel@tonic-gate #ifndef NEQN
52*0Sstevel@tonic-gate 		if (rfont[p1] == ITAL && lfont[p2] == ROM)
53*0Sstevel@tonic-gate 			sh1 = "\\|";
54*0Sstevel@tonic-gate 		if (rfont[p2] == ITAL)
55*0Sstevel@tonic-gate 			sh2 = "\\|";
56*0Sstevel@tonic-gate #endif NEQN
57*0Sstevel@tonic-gate 	} else {	/* superscript */
58*0Sstevel@tonic-gate #ifndef NEQN
59*0Sstevel@tonic-gate 		/* 4/10 up main box */
60*0Sstevel@tonic-gate 		d1 = VERT(EM(0.2, effps));
61*0Sstevel@tonic-gate #else NEQN
62*0Sstevel@tonic-gate 		d1 = VERT(1);
63*0Sstevel@tonic-gate #endif NEQN
64*0Sstevel@tonic-gate 		ebase[yyval] = b1;
65*0Sstevel@tonic-gate #ifndef NEQN
66*0Sstevel@tonic-gate 		shval = -VERT( (4 * (h1-b1)) / 10 ) - b2;
67*0Sstevel@tonic-gate 		if( VERT(4*(h1-b1)/10) + h2 < h1-b1 )	/* raise little super */
68*0Sstevel@tonic-gate #else NEQN
69*0Sstevel@tonic-gate 		shval = -VERT(1) - b2;
70*0Sstevel@tonic-gate 		if( VERT(1) + h2 < h1-b1 )	/* raise little super */
71*0Sstevel@tonic-gate #endif NEQN
72*0Sstevel@tonic-gate 			shval = -(h1-b1) + h2-b2 - d1;
73*0Sstevel@tonic-gate #ifndef NEQN
74*0Sstevel@tonic-gate 		eht[yyval] = h1 + max(0, h2-VERT((6*(h1-b1))/10));
75*0Sstevel@tonic-gate 		if (rfont[p1] == ITAL)
76*0Sstevel@tonic-gate 			sh1 = "\\|";
77*0Sstevel@tonic-gate 		if (rfont[p2] == ITAL)
78*0Sstevel@tonic-gate 			sh2 = "\\|";
79*0Sstevel@tonic-gate #else NEQN
80*0Sstevel@tonic-gate 		eht[yyval] = h1 + max(0, h2 - VERT(1));
81*0Sstevel@tonic-gate #endif NEQN
82*0Sstevel@tonic-gate 	}
83*0Sstevel@tonic-gate 	if(dbg)printf(".\tb:b shift b: S%d <- S%d vert %d S%d vert %d; b=%d, h=%d\n",
84*0Sstevel@tonic-gate 		yyval, p1, shval, p2, -shval, ebase[yyval], eht[yyval]);
85*0Sstevel@tonic-gate #ifndef NEQN
86*0Sstevel@tonic-gate 	printf(".as %d \\v'%du'\\s-%d%s\\*(%d\\s+%d%s\\v'%du'\n",
87*0Sstevel@tonic-gate 		yyval, shval, diffps, sh1, p2, diffps, sh2, -shval);
88*0Sstevel@tonic-gate 	ps += deltaps;
89*0Sstevel@tonic-gate 	if (rfont[p2] == ITAL)
90*0Sstevel@tonic-gate 		rfont[p1] = 0;
91*0Sstevel@tonic-gate 	else
92*0Sstevel@tonic-gate 		rfont[p1] = rfont[p2];
93*0Sstevel@tonic-gate #else NEQN
94*0Sstevel@tonic-gate 	printf(".as %d \\v'%du'\\*(%d\\v'%du'\n",
95*0Sstevel@tonic-gate 		yyval, shval, p2, -shval);
96*0Sstevel@tonic-gate #endif NEQN
97*0Sstevel@tonic-gate 	ofree(p2);
98*0Sstevel@tonic-gate }
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate shift(p1) int p1; {
101*0Sstevel@tonic-gate 	ps -= deltaps;
102*0Sstevel@tonic-gate 	yyval = p1;
103*0Sstevel@tonic-gate 	if(dbg)printf(".\tshift: %d;ps=%d\n", yyval, ps);
104*0Sstevel@tonic-gate }
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate shift2(p1, p2, p3) int p1, p2, p3; {
107*0Sstevel@tonic-gate 	int effps, h1, h2, h3, b1, b2, b3, subsh, d1, d2, supsh, treg;
108*0Sstevel@tonic-gate #ifndef NEQN
109*0Sstevel@tonic-gate 	int effps2;
110*0Sstevel@tonic-gate #endif NEQN
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	treg = oalloc();
113*0Sstevel@tonic-gate 	yyval = p1;
114*0Sstevel@tonic-gate 	if(dbg)printf(".\tshift2 s%d <- %d %d %d\n", yyval, p1, p2, p3);
115*0Sstevel@tonic-gate 	effps = EFFPS(ps+deltaps);
116*0Sstevel@tonic-gate #ifndef NEQN
117*0Sstevel@tonic-gate 	eht[p3] = h3 = VERT( (eht[p3] * effps) / EFFPS(ps) );
118*0Sstevel@tonic-gate 	ps += deltaps;
119*0Sstevel@tonic-gate 	effps2 = EFFPS(ps+deltaps);
120*0Sstevel@tonic-gate #endif NEQN
121*0Sstevel@tonic-gate 	h1 = eht[p1]; b1 = ebase[p1];
122*0Sstevel@tonic-gate 	h2 = eht[p2]; b2 = ebase[p2];
123*0Sstevel@tonic-gate #ifndef NEQN
124*0Sstevel@tonic-gate 	b3 = ebase[p3];
125*0Sstevel@tonic-gate 	d1 = VERT(EM(0.5, effps2));
126*0Sstevel@tonic-gate #else NEQN
127*0Sstevel@tonic-gate 	h3 = eht[p3]; b3 = ebase[p3];
128*0Sstevel@tonic-gate 	d1 = VERT(1);
129*0Sstevel@tonic-gate #endif NEQN
130*0Sstevel@tonic-gate 	subsh = -d1+h2-b2;
131*0Sstevel@tonic-gate 	if( d1+b1 > h2 ) /* move little sub down */
132*0Sstevel@tonic-gate 		subsh = b1-b2;
133*0Sstevel@tonic-gate #ifndef NEQN
134*0Sstevel@tonic-gate 	supsh = -VERT( (4*(h1-b1))/10 ) - b3;
135*0Sstevel@tonic-gate 	d2 = VERT(EM(0.2, effps));
136*0Sstevel@tonic-gate 	if( VERT(4*(h1-b1)/10)+h3 < h1-b1 )
137*0Sstevel@tonic-gate #else NEQN
138*0Sstevel@tonic-gate 	supsh = - VERT(1) - b3;
139*0Sstevel@tonic-gate 	d2 = VERT(1);
140*0Sstevel@tonic-gate 	if( VERT(1)+h3 < h1-b1 )
141*0Sstevel@tonic-gate #endif NEQN
142*0Sstevel@tonic-gate 		supsh = -(h1-b1) + (h3-b3) - d2;
143*0Sstevel@tonic-gate #ifndef NEQN
144*0Sstevel@tonic-gate 	eht[yyval] = h1 + max(0, h3-VERT( (6*(h1-b1))/10 )) + max(0, h2-b1-d1);
145*0Sstevel@tonic-gate #else NEQN
146*0Sstevel@tonic-gate 	eht[yyval] = h1 + max(0, h3-VERT(1)) + max(0, h2-b1-d1);
147*0Sstevel@tonic-gate #endif NEQN
148*0Sstevel@tonic-gate 	ebase[yyval] = b1+max(0, h2-b1-d1);
149*0Sstevel@tonic-gate #ifndef NEQN
150*0Sstevel@tonic-gate 	if (rfont[p1] == ITAL && lfont[p2] == ROM)
151*0Sstevel@tonic-gate 		printf(".ds %d \\|\\*(%d\n", p2, p2);
152*0Sstevel@tonic-gate 	if (rfont[p2] == ITAL)
153*0Sstevel@tonic-gate 		printf(".as %d \\|\n", p2);
154*0Sstevel@tonic-gate #endif NEQN
155*0Sstevel@tonic-gate 	nrwid(p2, effps, p2);
156*0Sstevel@tonic-gate #ifndef NEQN
157*0Sstevel@tonic-gate 	if (rfont[p1] == ITAL && lfont[p3] == ROM)
158*0Sstevel@tonic-gate 		printf(".ds %d \\|\\|\\*(%d\n", p3, p3);
159*0Sstevel@tonic-gate 	else
160*0Sstevel@tonic-gate 		printf(".ds %d \\|\\*(%d\n", p3, p3);
161*0Sstevel@tonic-gate #endif NEQN
162*0Sstevel@tonic-gate 	nrwid(p3, effps, p3);
163*0Sstevel@tonic-gate 	printf(".nr %d \\n(%d\n", treg, p3);
164*0Sstevel@tonic-gate 	printf(".if \\n(%d>\\n(%d .nr %d \\n(%d\n", p2, treg, treg, p2);
165*0Sstevel@tonic-gate #ifndef NEQN
166*0Sstevel@tonic-gate 	printf(".as %d \\v'%du'\\s%d\\*(%d\\h'-\\n(%du'\\v'%du'\\\n",
167*0Sstevel@tonic-gate 		p1, subsh, effps, p2, p2, -subsh+supsh);
168*0Sstevel@tonic-gate 	printf("\\s%d\\*(%d\\h'-\\n(%du+\\n(%du'\\s%d\\v'%du'\n",
169*0Sstevel@tonic-gate 		effps, p3, p3, treg, effps2, -supsh);
170*0Sstevel@tonic-gate #else NEQN
171*0Sstevel@tonic-gate 	printf(".as %d \\v'%du'\\*(%d\\h'-\\n(%du'\\v'%du'\\\n",
172*0Sstevel@tonic-gate 		p1, subsh, p2, p2, -subsh+supsh);
173*0Sstevel@tonic-gate 	printf("\\*(%d\\h'-\\n(%du+\\n(%du'\\v'%du'\n",
174*0Sstevel@tonic-gate 		p3, p3, treg, -supsh);
175*0Sstevel@tonic-gate #endif NEQN
176*0Sstevel@tonic-gate 	ps += deltaps;
177*0Sstevel@tonic-gate #ifndef NEQN
178*0Sstevel@tonic-gate 	if (rfont[p2] == ITAL)
179*0Sstevel@tonic-gate 		rfont[yyval] = 0;	/* lie */
180*0Sstevel@tonic-gate #endif NEQN
181*0Sstevel@tonic-gate 	ofree(p2); ofree(p3); ofree(treg);
182*0Sstevel@tonic-gate }
183