C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / shgeqz.c
1 #include <math.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <complex.h>
6 #ifdef complex
7 #undef complex
8 #endif
9 #ifdef I
10 #undef I
11 #endif
12
13 #if defined(_WIN64)
14 typedef long long BLASLONG;
15 typedef unsigned long long BLASULONG;
16 #else
17 typedef long BLASLONG;
18 typedef unsigned long BLASULONG;
19 #endif
20
21 #ifdef LAPACK_ILP64
22 typedef BLASLONG blasint;
23 #if defined(_WIN64)
24 #define blasabs(x) llabs(x)
25 #else
26 #define blasabs(x) labs(x)
27 #endif
28 #else
29 typedef int blasint;
30 #define blasabs(x) abs(x)
31 #endif
32
33 typedef blasint integer;
34
35 typedef unsigned int uinteger;
36 typedef char *address;
37 typedef short int shortint;
38 typedef float real;
39 typedef double doublereal;
40 typedef struct { real r, i; } complex;
41 typedef struct { doublereal r, i; } doublecomplex;
42 #ifdef _MSC_VER
43 static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
44 static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
45 static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
46 static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
47 #else
48 static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
49 static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
50 static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
51 static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
52 #endif
53 #define pCf(z) (*_pCf(z))
54 #define pCd(z) (*_pCd(z))
55 typedef int logical;
56 typedef short int shortlogical;
57 typedef char logical1;
58 typedef char integer1;
59
60 #define TRUE_ (1)
61 #define FALSE_ (0)
62
63 /* Extern is for use with -E */
64 #ifndef Extern
65 #define Extern extern
66 #endif
67
68 /* I/O stuff */
69
70 typedef int flag;
71 typedef int ftnlen;
72 typedef int ftnint;
73
74 /*external read, write*/
75 typedef struct
76 {       flag cierr;
77         ftnint ciunit;
78         flag ciend;
79         char *cifmt;
80         ftnint cirec;
81 } cilist;
82
83 /*internal read, write*/
84 typedef struct
85 {       flag icierr;
86         char *iciunit;
87         flag iciend;
88         char *icifmt;
89         ftnint icirlen;
90         ftnint icirnum;
91 } icilist;
92
93 /*open*/
94 typedef struct
95 {       flag oerr;
96         ftnint ounit;
97         char *ofnm;
98         ftnlen ofnmlen;
99         char *osta;
100         char *oacc;
101         char *ofm;
102         ftnint orl;
103         char *oblnk;
104 } olist;
105
106 /*close*/
107 typedef struct
108 {       flag cerr;
109         ftnint cunit;
110         char *csta;
111 } cllist;
112
113 /*rewind, backspace, endfile*/
114 typedef struct
115 {       flag aerr;
116         ftnint aunit;
117 } alist;
118
119 /* inquire */
120 typedef struct
121 {       flag inerr;
122         ftnint inunit;
123         char *infile;
124         ftnlen infilen;
125         ftnint  *inex;  /*parameters in standard's order*/
126         ftnint  *inopen;
127         ftnint  *innum;
128         ftnint  *innamed;
129         char    *inname;
130         ftnlen  innamlen;
131         char    *inacc;
132         ftnlen  inacclen;
133         char    *inseq;
134         ftnlen  inseqlen;
135         char    *indir;
136         ftnlen  indirlen;
137         char    *infmt;
138         ftnlen  infmtlen;
139         char    *inform;
140         ftnint  informlen;
141         char    *inunf;
142         ftnlen  inunflen;
143         ftnint  *inrecl;
144         ftnint  *innrec;
145         char    *inblank;
146         ftnlen  inblanklen;
147 } inlist;
148
149 #define VOID void
150
151 union Multitype {       /* for multiple entry points */
152         integer1 g;
153         shortint h;
154         integer i;
155         /* longint j; */
156         real r;
157         doublereal d;
158         complex c;
159         doublecomplex z;
160         };
161
162 typedef union Multitype Multitype;
163
164 struct Vardesc {        /* for Namelist */
165         char *name;
166         char *addr;
167         ftnlen *dims;
168         int  type;
169         };
170 typedef struct Vardesc Vardesc;
171
172 struct Namelist {
173         char *name;
174         Vardesc **vars;
175         int nvars;
176         };
177 typedef struct Namelist Namelist;
178
179 #define abs(x) ((x) >= 0 ? (x) : -(x))
180 #define dabs(x) (fabs(x))
181 #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
182 #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
183 #define dmin(a,b) (f2cmin(a,b))
184 #define dmax(a,b) (f2cmax(a,b))
185 #define bit_test(a,b)   ((a) >> (b) & 1)
186 #define bit_clear(a,b)  ((a) & ~((uinteger)1 << (b)))
187 #define bit_set(a,b)    ((a) |  ((uinteger)1 << (b)))
188
189 #define abort_() { sig_die("Fortran abort routine called", 1); }
190 #define c_abs(z) (cabsf(Cf(z)))
191 #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
192 #ifdef _MSC_VER
193 #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
194 #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/df(b)._Val[1]);}
195 #else
196 #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
197 #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
198 #endif
199 #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
200 #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
201 #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
202 //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
203 #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
204 #define d_abs(x) (fabs(*(x)))
205 #define d_acos(x) (acos(*(x)))
206 #define d_asin(x) (asin(*(x)))
207 #define d_atan(x) (atan(*(x)))
208 #define d_atn2(x, y) (atan2(*(x),*(y)))
209 #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
210 #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
211 #define d_cos(x) (cos(*(x)))
212 #define d_cosh(x) (cosh(*(x)))
213 #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
214 #define d_exp(x) (exp(*(x)))
215 #define d_imag(z) (cimag(Cd(z)))
216 #define r_imag(z) (cimagf(Cf(z)))
217 #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
218 #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
219 #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
220 #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
221 #define d_log(x) (log(*(x)))
222 #define d_mod(x, y) (fmod(*(x), *(y)))
223 #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
224 #define d_nint(x) u_nint(*(x))
225 #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
226 #define d_sign(a,b) u_sign(*(a),*(b))
227 #define r_sign(a,b) u_sign(*(a),*(b))
228 #define d_sin(x) (sin(*(x)))
229 #define d_sinh(x) (sinh(*(x)))
230 #define d_sqrt(x) (sqrt(*(x)))
231 #define d_tan(x) (tan(*(x)))
232 #define d_tanh(x) (tanh(*(x)))
233 #define i_abs(x) abs(*(x))
234 #define i_dnnt(x) ((integer)u_nint(*(x)))
235 #define i_len(s, n) (n)
236 #define i_nint(x) ((integer)u_nint(*(x)))
237 #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
238 #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
239 #define pow_si(B,E) spow_ui(*(B),*(E))
240 #define pow_ri(B,E) spow_ui(*(B),*(E))
241 #define pow_di(B,E) dpow_ui(*(B),*(E))
242 #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
243 #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
244 #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
245 #define s_cat(lpp, rpp, rnp, np, llp) {         ftnlen i, nc, ll; char *f__rp, *lp;     ll = (llp); lp = (lpp);         for(i=0; i < (int)*(np); ++i) {                 nc = ll;                if((rnp)[i] < nc) nc = (rnp)[i];                ll -= nc;               f__rp = (rpp)[i];               while(--nc >= 0) *lp++ = *(f__rp)++;         }  while(--ll >= 0) *lp++ = ' '; }
246 #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
247 #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
248 #define sig_die(s, kill) { exit(1); }
249 #define s_stop(s, n) {exit(0);}
250 static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
251 #define z_abs(z) (cabs(Cd(z)))
252 #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
253 #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
254 #define myexit_() break;
255 #define mycycle() continue;
256 #define myceiling(w) {ceil(w)}
257 #define myhuge(w) {HUGE_VAL}
258 //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
259 #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
260
261 /* procedure parameter types for -A and -C++ */
262
263 #define F2C_proc_par_types 1
264 #ifdef __cplusplus
265 typedef logical (*L_fp)(...);
266 #else
267 typedef logical (*L_fp)();
268 #endif
269
270 static float spow_ui(float x, integer n) {
271         float pow=1.0; unsigned long int u;
272         if(n != 0) {
273                 if(n < 0) n = -n, x = 1/x;
274                 for(u = n; ; ) {
275                         if(u & 01) pow *= x;
276                         if(u >>= 1) x *= x;
277                         else break;
278                 }
279         }
280         return pow;
281 }
282 static double dpow_ui(double x, integer n) {
283         double pow=1.0; unsigned long int u;
284         if(n != 0) {
285                 if(n < 0) n = -n, x = 1/x;
286                 for(u = n; ; ) {
287                         if(u & 01) pow *= x;
288                         if(u >>= 1) x *= x;
289                         else break;
290                 }
291         }
292         return pow;
293 }
294 #ifdef _MSC_VER
295 static _Fcomplex cpow_ui(complex x, integer n) {
296         complex pow={1.0,0.0}; unsigned long int u;
297                 if(n != 0) {
298                 if(n < 0) n = -n, x.r = 1/x.r, x.i=1/x.i;
299                 for(u = n; ; ) {
300                         if(u & 01) pow.r *= x.r, pow.i *= x.i;
301                         if(u >>= 1) x.r *= x.r, x.i *= x.i;
302                         else break;
303                 }
304         }
305         _Fcomplex p={pow.r, pow.i};
306         return p;
307 }
308 #else
309 static _Complex float cpow_ui(_Complex float x, integer n) {
310         _Complex float pow=1.0; unsigned long int u;
311         if(n != 0) {
312                 if(n < 0) n = -n, x = 1/x;
313                 for(u = n; ; ) {
314                         if(u & 01) pow *= x;
315                         if(u >>= 1) x *= x;
316                         else break;
317                 }
318         }
319         return pow;
320 }
321 #endif
322 #ifdef _MSC_VER
323 static _Dcomplex zpow_ui(_Dcomplex x, integer n) {
324         _Dcomplex pow={1.0,0.0}; unsigned long int u;
325         if(n != 0) {
326                 if(n < 0) n = -n, x._Val[0] = 1/x._Val[0], x._Val[1] =1/x._Val[1];
327                 for(u = n; ; ) {
328                         if(u & 01) pow._Val[0] *= x._Val[0], pow._Val[1] *= x._Val[1];
329                         if(u >>= 1) x._Val[0] *= x._Val[0], x._Val[1] *= x._Val[1];
330                         else break;
331                 }
332         }
333         _Dcomplex p = {pow._Val[0], pow._Val[1]};
334         return p;
335 }
336 #else
337 static _Complex double zpow_ui(_Complex double x, integer n) {
338         _Complex double pow=1.0; unsigned long int u;
339         if(n != 0) {
340                 if(n < 0) n = -n, x = 1/x;
341                 for(u = n; ; ) {
342                         if(u & 01) pow *= x;
343                         if(u >>= 1) x *= x;
344                         else break;
345                 }
346         }
347         return pow;
348 }
349 #endif
350 static integer pow_ii(integer x, integer n) {
351         integer pow; unsigned long int u;
352         if (n <= 0) {
353                 if (n == 0 || x == 1) pow = 1;
354                 else if (x != -1) pow = x == 0 ? 1/x : 0;
355                 else n = -n;
356         }
357         if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
358                 u = n;
359                 for(pow = 1; ; ) {
360                         if(u & 01) pow *= x;
361                         if(u >>= 1) x *= x;
362                         else break;
363                 }
364         }
365         return pow;
366 }
367 static integer dmaxloc_(double *w, integer s, integer e, integer *n)
368 {
369         double m; integer i, mi;
370         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
371                 if (w[i-1]>m) mi=i ,m=w[i-1];
372         return mi-s+1;
373 }
374 static integer smaxloc_(float *w, integer s, integer e, integer *n)
375 {
376         float m; integer i, mi;
377         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
378                 if (w[i-1]>m) mi=i ,m=w[i-1];
379         return mi-s+1;
380 }
381 static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
382         integer n = *n_, incx = *incx_, incy = *incy_, i;
383 #ifdef _MSC_VER
384         _Fcomplex zdotc = {0.0, 0.0};
385         if (incx == 1 && incy == 1) {
386                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
387                         zdotc._Val[0] += conjf(Cf(&x[i]))._Val[0] * Cf(&y[i])._Val[0];
388                         zdotc._Val[1] += conjf(Cf(&x[i]))._Val[1] * Cf(&y[i])._Val[1];
389                 }
390         } else {
391                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
392                         zdotc._Val[0] += conjf(Cf(&x[i*incx]))._Val[0] * Cf(&y[i*incy])._Val[0];
393                         zdotc._Val[1] += conjf(Cf(&x[i*incx]))._Val[1] * Cf(&y[i*incy])._Val[1];
394                 }
395         }
396         pCf(z) = zdotc;
397 }
398 #else
399         _Complex float zdotc = 0.0;
400         if (incx == 1 && incy == 1) {
401                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
402                         zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
403                 }
404         } else {
405                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
406                         zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
407                 }
408         }
409         pCf(z) = zdotc;
410 }
411 #endif
412 static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
413         integer n = *n_, incx = *incx_, incy = *incy_, i;
414 #ifdef _MSC_VER
415         _Dcomplex zdotc = {0.0, 0.0};
416         if (incx == 1 && incy == 1) {
417                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
418                         zdotc._Val[0] += conj(Cd(&x[i]))._Val[0] * Cd(&y[i])._Val[0];
419                         zdotc._Val[1] += conj(Cd(&x[i]))._Val[1] * Cd(&y[i])._Val[1];
420                 }
421         } else {
422                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
423                         zdotc._Val[0] += conj(Cd(&x[i*incx]))._Val[0] * Cd(&y[i*incy])._Val[0];
424                         zdotc._Val[1] += conj(Cd(&x[i*incx]))._Val[1] * Cd(&y[i*incy])._Val[1];
425                 }
426         }
427         pCd(z) = zdotc;
428 }
429 #else
430         _Complex double zdotc = 0.0;
431         if (incx == 1 && incy == 1) {
432                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
433                         zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
434                 }
435         } else {
436                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
437                         zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
438                 }
439         }
440         pCd(z) = zdotc;
441 }
442 #endif  
443 static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
444         integer n = *n_, incx = *incx_, incy = *incy_, i;
445 #ifdef _MSC_VER
446         _Fcomplex zdotc = {0.0, 0.0};
447         if (incx == 1 && incy == 1) {
448                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
449                         zdotc._Val[0] += Cf(&x[i])._Val[0] * Cf(&y[i])._Val[0];
450                         zdotc._Val[1] += Cf(&x[i])._Val[1] * Cf(&y[i])._Val[1];
451                 }
452         } else {
453                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
454                         zdotc._Val[0] += Cf(&x[i*incx])._Val[0] * Cf(&y[i*incy])._Val[0];
455                         zdotc._Val[1] += Cf(&x[i*incx])._Val[1] * Cf(&y[i*incy])._Val[1];
456                 }
457         }
458         pCf(z) = zdotc;
459 }
460 #else
461         _Complex float zdotc = 0.0;
462         if (incx == 1 && incy == 1) {
463                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
464                         zdotc += Cf(&x[i]) * Cf(&y[i]);
465                 }
466         } else {
467                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
468                         zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
469                 }
470         }
471         pCf(z) = zdotc;
472 }
473 #endif
474 static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
475         integer n = *n_, incx = *incx_, incy = *incy_, i;
476 #ifdef _MSC_VER
477         _Dcomplex zdotc = {0.0, 0.0};
478         if (incx == 1 && incy == 1) {
479                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
480                         zdotc._Val[0] += Cd(&x[i])._Val[0] * Cd(&y[i])._Val[0];
481                         zdotc._Val[1] += Cd(&x[i])._Val[1] * Cd(&y[i])._Val[1];
482                 }
483         } else {
484                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
485                         zdotc._Val[0] += Cd(&x[i*incx])._Val[0] * Cd(&y[i*incy])._Val[0];
486                         zdotc._Val[1] += Cd(&x[i*incx])._Val[1] * Cd(&y[i*incy])._Val[1];
487                 }
488         }
489         pCd(z) = zdotc;
490 }
491 #else
492         _Complex double zdotc = 0.0;
493         if (incx == 1 && incy == 1) {
494                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
495                         zdotc += Cd(&x[i]) * Cd(&y[i]);
496                 }
497         } else {
498                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
499                         zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
500                 }
501         }
502         pCd(z) = zdotc;
503 }
504 #endif
505 /*  -- translated by f2c (version 20000121).
506    You must link the resulting object file with the libraries:
507         -lf2c -lm   (in that order)
508 */
509
510
511
512
513 /* Table of constant values */
514
515 static real c_b12 = 0.f;
516 static real c_b13 = 1.f;
517 static integer c__1 = 1;
518 static integer c__3 = 3;
519
520 /* > \brief \b SHGEQZ */
521
522 /*  =========== DOCUMENTATION =========== */
523
524 /* Online html documentation available at */
525 /*            http://www.netlib.org/lapack/explore-html/ */
526
527 /* > \htmlonly */
528 /* > Download SHGEQZ + dependencies */
529 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/shgeqz.
530 f"> */
531 /* > [TGZ]</a> */
532 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/shgeqz.
533 f"> */
534 /* > [ZIP]</a> */
535 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/shgeqz.
536 f"> */
537 /* > [TXT]</a> */
538 /* > \endhtmlonly */
539
540 /*  Definition: */
541 /*  =========== */
542
543 /*       SUBROUTINE SHGEQZ( JOB, COMPQ, COMPZ, N, ILO, IHI, H, LDH, T, LDT, */
544 /*                          ALPHAR, ALPHAI, BETA, Q, LDQ, Z, LDZ, WORK, */
545 /*                          LWORK, INFO ) */
546
547 /*       CHARACTER          COMPQ, COMPZ, JOB */
548 /*       INTEGER            IHI, ILO, INFO, LDH, LDQ, LDT, LDZ, LWORK, N */
549 /*       REAL               ALPHAI( * ), ALPHAR( * ), BETA( * ), */
550 /*      $                   H( LDH, * ), Q( LDQ, * ), T( LDT, * ), */
551 /*      $                   WORK( * ), Z( LDZ, * ) */
552
553
554 /* > \par Purpose: */
555 /*  ============= */
556 /* > */
557 /* > \verbatim */
558 /* > */
559 /* > SHGEQZ computes the eigenvalues of a real matrix pair (H,T), */
560 /* > where H is an upper Hessenberg matrix and T is upper triangular, */
561 /* > using the double-shift QZ method. */
562 /* > Matrix pairs of this type are produced by the reduction to */
563 /* > generalized upper Hessenberg form of a real matrix pair (A,B): */
564 /* > */
565 /* >    A = Q1*H*Z1**T,  B = Q1*T*Z1**T, */
566 /* > */
567 /* > as computed by SGGHRD. */
568 /* > */
569 /* > If JOB='S', then the Hessenberg-triangular pair (H,T) is */
570 /* > also reduced to generalized Schur form, */
571 /* > */
572 /* >    H = Q*S*Z**T,  T = Q*P*Z**T, */
573 /* > */
574 /* > where Q and Z are orthogonal matrices, P is an upper triangular */
575 /* > matrix, and S is a quasi-triangular matrix with 1-by-1 and 2-by-2 */
576 /* > diagonal blocks. */
577 /* > */
578 /* > The 1-by-1 blocks correspond to real eigenvalues of the matrix pair */
579 /* > (H,T) and the 2-by-2 blocks correspond to complex conjugate pairs of */
580 /* > eigenvalues. */
581 /* > */
582 /* > Additionally, the 2-by-2 upper triangular diagonal blocks of P */
583 /* > corresponding to 2-by-2 blocks of S are reduced to positive diagonal */
584 /* > form, i.e., if S(j+1,j) is non-zero, then P(j+1,j) = P(j,j+1) = 0, */
585 /* > P(j,j) > 0, and P(j+1,j+1) > 0. */
586 /* > */
587 /* > Optionally, the orthogonal matrix Q from the generalized Schur */
588 /* > factorization may be postmultiplied into an input matrix Q1, and the */
589 /* > orthogonal matrix Z may be postmultiplied into an input matrix Z1. */
590 /* > If Q1 and Z1 are the orthogonal matrices from SGGHRD that reduced */
591 /* > the matrix pair (A,B) to generalized upper Hessenberg form, then the */
592 /* > output matrices Q1*Q and Z1*Z are the orthogonal factors from the */
593 /* > generalized Schur factorization of (A,B): */
594 /* > */
595 /* >    A = (Q1*Q)*S*(Z1*Z)**T,  B = (Q1*Q)*P*(Z1*Z)**T. */
596 /* > */
597 /* > To avoid overflow, eigenvalues of the matrix pair (H,T) (equivalently, */
598 /* > of (A,B)) are computed as a pair of values (alpha,beta), where alpha is */
599 /* > complex and beta real. */
600 /* > If beta is nonzero, lambda = alpha / beta is an eigenvalue of the */
601 /* > generalized nonsymmetric eigenvalue problem (GNEP) */
602 /* >    A*x = lambda*B*x */
603 /* > and if alpha is nonzero, mu = beta / alpha is an eigenvalue of the */
604 /* > alternate form of the GNEP */
605 /* >    mu*A*y = B*y. */
606 /* > Real eigenvalues can be read directly from the generalized Schur */
607 /* > form: */
608 /* >   alpha = S(i,i), beta = P(i,i). */
609 /* > */
610 /* > Ref: C.B. Moler & G.W. Stewart, "An Algorithm for Generalized Matrix */
611 /* >      Eigenvalue Problems", SIAM J. Numer. Anal., 10(1973), */
612 /* >      pp. 241--256. */
613 /* > \endverbatim */
614
615 /*  Arguments: */
616 /*  ========== */
617
618 /* > \param[in] JOB */
619 /* > \verbatim */
620 /* >          JOB is CHARACTER*1 */
621 /* >          = 'E': Compute eigenvalues only; */
622 /* >          = 'S': Compute eigenvalues and the Schur form. */
623 /* > \endverbatim */
624 /* > */
625 /* > \param[in] COMPQ */
626 /* > \verbatim */
627 /* >          COMPQ is CHARACTER*1 */
628 /* >          = 'N': Left Schur vectors (Q) are not computed; */
629 /* >          = 'I': Q is initialized to the unit matrix and the matrix Q */
630 /* >                 of left Schur vectors of (H,T) is returned; */
631 /* >          = 'V': Q must contain an orthogonal matrix Q1 on entry and */
632 /* >                 the product Q1*Q is returned. */
633 /* > \endverbatim */
634 /* > */
635 /* > \param[in] COMPZ */
636 /* > \verbatim */
637 /* >          COMPZ is CHARACTER*1 */
638 /* >          = 'N': Right Schur vectors (Z) are not computed; */
639 /* >          = 'I': Z is initialized to the unit matrix and the matrix Z */
640 /* >                 of right Schur vectors of (H,T) is returned; */
641 /* >          = 'V': Z must contain an orthogonal matrix Z1 on entry and */
642 /* >                 the product Z1*Z is returned. */
643 /* > \endverbatim */
644 /* > */
645 /* > \param[in] N */
646 /* > \verbatim */
647 /* >          N is INTEGER */
648 /* >          The order of the matrices H, T, Q, and Z.  N >= 0. */
649 /* > \endverbatim */
650 /* > */
651 /* > \param[in] ILO */
652 /* > \verbatim */
653 /* >          ILO is INTEGER */
654 /* > \endverbatim */
655 /* > */
656 /* > \param[in] IHI */
657 /* > \verbatim */
658 /* >          IHI is INTEGER */
659 /* >          ILO and IHI mark the rows and columns of H which are in */
660 /* >          Hessenberg form.  It is assumed that A is already upper */
661 /* >          triangular in rows and columns 1:ILO-1 and IHI+1:N. */
662 /* >          If N > 0, 1 <= ILO <= IHI <= N; if N = 0, ILO=1 and IHI=0. */
663 /* > \endverbatim */
664 /* > */
665 /* > \param[in,out] H */
666 /* > \verbatim */
667 /* >          H is REAL array, dimension (LDH, N) */
668 /* >          On entry, the N-by-N upper Hessenberg matrix H. */
669 /* >          On exit, if JOB = 'S', H contains the upper quasi-triangular */
670 /* >          matrix S from the generalized Schur factorization. */
671 /* >          If JOB = 'E', the diagonal blocks of H match those of S, but */
672 /* >          the rest of H is unspecified. */
673 /* > \endverbatim */
674 /* > */
675 /* > \param[in] LDH */
676 /* > \verbatim */
677 /* >          LDH is INTEGER */
678 /* >          The leading dimension of the array H.  LDH >= f2cmax( 1, N ). */
679 /* > \endverbatim */
680 /* > */
681 /* > \param[in,out] T */
682 /* > \verbatim */
683 /* >          T is REAL array, dimension (LDT, N) */
684 /* >          On entry, the N-by-N upper triangular matrix T. */
685 /* >          On exit, if JOB = 'S', T contains the upper triangular */
686 /* >          matrix P from the generalized Schur factorization; */
687 /* >          2-by-2 diagonal blocks of P corresponding to 2-by-2 blocks of S */
688 /* >          are reduced to positive diagonal form, i.e., if H(j+1,j) is */
689 /* >          non-zero, then T(j+1,j) = T(j,j+1) = 0, T(j,j) > 0, and */
690 /* >          T(j+1,j+1) > 0. */
691 /* >          If JOB = 'E', the diagonal blocks of T match those of P, but */
692 /* >          the rest of T is unspecified. */
693 /* > \endverbatim */
694 /* > */
695 /* > \param[in] LDT */
696 /* > \verbatim */
697 /* >          LDT is INTEGER */
698 /* >          The leading dimension of the array T.  LDT >= f2cmax( 1, N ). */
699 /* > \endverbatim */
700 /* > */
701 /* > \param[out] ALPHAR */
702 /* > \verbatim */
703 /* >          ALPHAR is REAL array, dimension (N) */
704 /* >          The real parts of each scalar alpha defining an eigenvalue */
705 /* >          of GNEP. */
706 /* > \endverbatim */
707 /* > */
708 /* > \param[out] ALPHAI */
709 /* > \verbatim */
710 /* >          ALPHAI is REAL array, dimension (N) */
711 /* >          The imaginary parts of each scalar alpha defining an */
712 /* >          eigenvalue of GNEP. */
713 /* >          If ALPHAI(j) is zero, then the j-th eigenvalue is real; if */
714 /* >          positive, then the j-th and (j+1)-st eigenvalues are a */
715 /* >          complex conjugate pair, with ALPHAI(j+1) = -ALPHAI(j). */
716 /* > \endverbatim */
717 /* > */
718 /* > \param[out] BETA */
719 /* > \verbatim */
720 /* >          BETA is REAL array, dimension (N) */
721 /* >          The scalars beta that define the eigenvalues of GNEP. */
722 /* >          Together, the quantities alpha = (ALPHAR(j),ALPHAI(j)) and */
723 /* >          beta = BETA(j) represent the j-th eigenvalue of the matrix */
724 /* >          pair (A,B), in one of the forms lambda = alpha/beta or */
725 /* >          mu = beta/alpha.  Since either lambda or mu may overflow, */
726 /* >          they should not, in general, be computed. */
727 /* > \endverbatim */
728 /* > */
729 /* > \param[in,out] Q */
730 /* > \verbatim */
731 /* >          Q is REAL array, dimension (LDQ, N) */
732 /* >          On entry, if COMPQ = 'V', the orthogonal matrix Q1 used in */
733 /* >          the reduction of (A,B) to generalized Hessenberg form. */
734 /* >          On exit, if COMPQ = 'I', the orthogonal matrix of left Schur */
735 /* >          vectors of (H,T), and if COMPQ = 'V', the orthogonal matrix */
736 /* >          of left Schur vectors of (A,B). */
737 /* >          Not referenced if COMPQ = 'N'. */
738 /* > \endverbatim */
739 /* > */
740 /* > \param[in] LDQ */
741 /* > \verbatim */
742 /* >          LDQ is INTEGER */
743 /* >          The leading dimension of the array Q.  LDQ >= 1. */
744 /* >          If COMPQ='V' or 'I', then LDQ >= N. */
745 /* > \endverbatim */
746 /* > */
747 /* > \param[in,out] Z */
748 /* > \verbatim */
749 /* >          Z is REAL array, dimension (LDZ, N) */
750 /* >          On entry, if COMPZ = 'V', the orthogonal matrix Z1 used in */
751 /* >          the reduction of (A,B) to generalized Hessenberg form. */
752 /* >          On exit, if COMPZ = 'I', the orthogonal matrix of */
753 /* >          right Schur vectors of (H,T), and if COMPZ = 'V', the */
754 /* >          orthogonal matrix of right Schur vectors of (A,B). */
755 /* >          Not referenced if COMPZ = 'N'. */
756 /* > \endverbatim */
757 /* > */
758 /* > \param[in] LDZ */
759 /* > \verbatim */
760 /* >          LDZ is INTEGER */
761 /* >          The leading dimension of the array Z.  LDZ >= 1. */
762 /* >          If COMPZ='V' or 'I', then LDZ >= N. */
763 /* > \endverbatim */
764 /* > */
765 /* > \param[out] WORK */
766 /* > \verbatim */
767 /* >          WORK is REAL array, dimension (MAX(1,LWORK)) */
768 /* >          On exit, if INFO >= 0, WORK(1) returns the optimal LWORK. */
769 /* > \endverbatim */
770 /* > */
771 /* > \param[in] LWORK */
772 /* > \verbatim */
773 /* >          LWORK is INTEGER */
774 /* >          The dimension of the array WORK.  LWORK >= f2cmax(1,N). */
775 /* > */
776 /* >          If LWORK = -1, then a workspace query is assumed; the routine */
777 /* >          only calculates the optimal size of the WORK array, returns */
778 /* >          this value as the first entry of the WORK array, and no error */
779 /* >          message related to LWORK is issued by XERBLA. */
780 /* > \endverbatim */
781 /* > */
782 /* > \param[out] INFO */
783 /* > \verbatim */
784 /* >          INFO is INTEGER */
785 /* >          = 0: successful exit */
786 /* >          < 0: if INFO = -i, the i-th argument had an illegal value */
787 /* >          = 1,...,N: the QZ iteration did not converge.  (H,T) is not */
788 /* >                     in Schur form, but ALPHAR(i), ALPHAI(i), and */
789 /* >                     BETA(i), i=INFO+1,...,N should be correct. */
790 /* >          = N+1,...,2*N: the shift calculation failed.  (H,T) is not */
791 /* >                     in Schur form, but ALPHAR(i), ALPHAI(i), and */
792 /* >                     BETA(i), i=INFO-N+1,...,N should be correct. */
793 /* > \endverbatim */
794
795 /*  Authors: */
796 /*  ======== */
797
798 /* > \author Univ. of Tennessee */
799 /* > \author Univ. of California Berkeley */
800 /* > \author Univ. of Colorado Denver */
801 /* > \author NAG Ltd. */
802
803 /* > \date June 2016 */
804
805 /* > \ingroup realGEcomputational */
806
807 /* > \par Further Details: */
808 /*  ===================== */
809 /* > */
810 /* > \verbatim */
811 /* > */
812 /* >  Iteration counters: */
813 /* > */
814 /* >  JITER  -- counts iterations. */
815 /* >  IITER  -- counts iterations run since ILAST was last */
816 /* >            changed.  This is therefore reset only when a 1-by-1 or */
817 /* >            2-by-2 block deflates off the bottom. */
818 /* > \endverbatim */
819 /* > */
820 /*  ===================================================================== */
821 /* Subroutine */ int shgeqz_(char *job, char *compq, char *compz, integer *n, 
822         integer *ilo, integer *ihi, real *h__, integer *ldh, real *t, integer 
823         *ldt, real *alphar, real *alphai, real *beta, real *q, integer *ldq, 
824         real *z__, integer *ldz, real *work, integer *lwork, integer *info)
825 {
826     /* System generated locals */
827     integer h_dim1, h_offset, q_dim1, q_offset, t_dim1, t_offset, z_dim1, 
828             z_offset, i__1, i__2, i__3, i__4;
829     real r__1, r__2, r__3, r__4;
830
831     /* Local variables */
832     real ad11l, ad12l, ad21l, ad22l, ad32l, wabs, atol, btol, temp;
833     extern /* Subroutine */ int srot_(integer *, real *, integer *, real *, 
834             integer *, real *, real *), slag2_(real *, integer *, real *, 
835             integer *, real *, real *, real *, real *, real *, real *);
836     real temp2, s1inv, c__;
837     integer j;
838     real s, v[3], scale;
839     extern logical lsame_(char *, char *);
840     integer iiter, ilast, jiter;
841     real anorm, bnorm;
842     integer maxit;
843     real tempi, tempr, s1, s2, t1, u1, u2;
844     logical ilazr2;
845     real a11, a12, a21, a22, b11, b22, c12, c21;
846     extern real slapy2_(real *, real *);
847     integer jc;
848     extern real slapy3_(real *, real *, real *);
849     real an, bn, cl;
850     extern /* Subroutine */ int slasv2_(real *, real *, real *, real *, real *
851             , real *, real *, real *, real *);
852     real cq, cr;
853     integer in;
854     real ascale, bscale, u12, w11;
855     integer jr;
856     real cz, sl, w12, w21, w22, wi, sr;
857     extern real slamch_(char *);
858     real vs, wr, safmin;
859     extern /* Subroutine */ int slarfg_(integer *, real *, real *, integer *, 
860             real *);
861     real safmax;
862     extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
863     real eshift;
864     logical ilschr;
865     real b1a, b2a;
866     integer icompq, ilastm;
867     extern real slanhs_(char *, integer *, real *, integer *, real *);
868     real a1i;
869     integer ischur;
870     real a2i, b1i;
871     logical ilazro;
872     integer icompz, ifirst, ifrstm;
873     real a1r;
874     integer istart;
875     logical ilpivt;
876     real a2r, b1r, b2i, b2r;
877     extern /* Subroutine */ int slartg_(real *, real *, real *, real *, real *
878             ), slaset_(char *, integer *, integer *, real *, real *, real *, 
879             integer *);
880     logical lquery;
881     real wr2, ad11, ad12, ad21, ad22, c11i, c22i;
882     integer jch;
883     real c11r, c22r;
884     logical ilq;
885     real u12l, tau, sqi;
886     logical ilz;
887     real ulp, sqr, szi, szr;
888
889
890 /*  -- LAPACK computational routine (version 3.7.0) -- */
891 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
892 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
893 /*     June 2016 */
894
895
896 /*  ===================================================================== */
897
898 /*    $                     SAFETY = 1.0E+0 ) */
899
900 /*     Decode JOB, COMPQ, COMPZ */
901
902     /* Parameter adjustments */
903     h_dim1 = *ldh;
904     h_offset = 1 + h_dim1 * 1;
905     h__ -= h_offset;
906     t_dim1 = *ldt;
907     t_offset = 1 + t_dim1 * 1;
908     t -= t_offset;
909     --alphar;
910     --alphai;
911     --beta;
912     q_dim1 = *ldq;
913     q_offset = 1 + q_dim1 * 1;
914     q -= q_offset;
915     z_dim1 = *ldz;
916     z_offset = 1 + z_dim1 * 1;
917     z__ -= z_offset;
918     --work;
919
920     /* Function Body */
921     if (lsame_(job, "E")) {
922         ilschr = FALSE_;
923         ischur = 1;
924     } else if (lsame_(job, "S")) {
925         ilschr = TRUE_;
926         ischur = 2;
927     } else {
928         ischur = 0;
929     }
930
931     if (lsame_(compq, "N")) {
932         ilq = FALSE_;
933         icompq = 1;
934     } else if (lsame_(compq, "V")) {
935         ilq = TRUE_;
936         icompq = 2;
937     } else if (lsame_(compq, "I")) {
938         ilq = TRUE_;
939         icompq = 3;
940     } else {
941         icompq = 0;
942     }
943
944     if (lsame_(compz, "N")) {
945         ilz = FALSE_;
946         icompz = 1;
947     } else if (lsame_(compz, "V")) {
948         ilz = TRUE_;
949         icompz = 2;
950     } else if (lsame_(compz, "I")) {
951         ilz = TRUE_;
952         icompz = 3;
953     } else {
954         icompz = 0;
955     }
956
957 /*     Check Argument Values */
958
959     *info = 0;
960     work[1] = (real) f2cmax(1,*n);
961     lquery = *lwork == -1;
962     if (ischur == 0) {
963         *info = -1;
964     } else if (icompq == 0) {
965         *info = -2;
966     } else if (icompz == 0) {
967         *info = -3;
968     } else if (*n < 0) {
969         *info = -4;
970     } else if (*ilo < 1) {
971         *info = -5;
972     } else if (*ihi > *n || *ihi < *ilo - 1) {
973         *info = -6;
974     } else if (*ldh < *n) {
975         *info = -8;
976     } else if (*ldt < *n) {
977         *info = -10;
978     } else if (*ldq < 1 || ilq && *ldq < *n) {
979         *info = -15;
980     } else if (*ldz < 1 || ilz && *ldz < *n) {
981         *info = -17;
982     } else if (*lwork < f2cmax(1,*n) && ! lquery) {
983         *info = -19;
984     }
985     if (*info != 0) {
986         i__1 = -(*info);
987         xerbla_("SHGEQZ", &i__1, (ftnlen)6);
988         return 0;
989     } else if (lquery) {
990         return 0;
991     }
992
993 /*     Quick return if possible */
994
995     if (*n <= 0) {
996         work[1] = 1.f;
997         return 0;
998     }
999
1000 /*     Initialize Q and Z */
1001
1002     if (icompq == 3) {
1003         slaset_("Full", n, n, &c_b12, &c_b13, &q[q_offset], ldq);
1004     }
1005     if (icompz == 3) {
1006         slaset_("Full", n, n, &c_b12, &c_b13, &z__[z_offset], ldz);
1007     }
1008
1009 /*     Machine Constants */
1010
1011     in = *ihi + 1 - *ilo;
1012     safmin = slamch_("S");
1013     safmax = 1.f / safmin;
1014     ulp = slamch_("E") * slamch_("B");
1015     anorm = slanhs_("F", &in, &h__[*ilo + *ilo * h_dim1], ldh, &work[1]);
1016     bnorm = slanhs_("F", &in, &t[*ilo + *ilo * t_dim1], ldt, &work[1]);
1017 /* Computing MAX */
1018     r__1 = safmin, r__2 = ulp * anorm;
1019     atol = f2cmax(r__1,r__2);
1020 /* Computing MAX */
1021     r__1 = safmin, r__2 = ulp * bnorm;
1022     btol = f2cmax(r__1,r__2);
1023     ascale = 1.f / f2cmax(safmin,anorm);
1024     bscale = 1.f / f2cmax(safmin,bnorm);
1025
1026 /*     Set Eigenvalues IHI+1:N */
1027
1028     i__1 = *n;
1029     for (j = *ihi + 1; j <= i__1; ++j) {
1030         if (t[j + j * t_dim1] < 0.f) {
1031             if (ilschr) {
1032                 i__2 = j;
1033                 for (jr = 1; jr <= i__2; ++jr) {
1034                     h__[jr + j * h_dim1] = -h__[jr + j * h_dim1];
1035                     t[jr + j * t_dim1] = -t[jr + j * t_dim1];
1036 /* L10: */
1037                 }
1038             } else {
1039                 h__[j + j * h_dim1] = -h__[j + j * h_dim1];
1040                 t[j + j * t_dim1] = -t[j + j * t_dim1];
1041             }
1042             if (ilz) {
1043                 i__2 = *n;
1044                 for (jr = 1; jr <= i__2; ++jr) {
1045                     z__[jr + j * z_dim1] = -z__[jr + j * z_dim1];
1046 /* L20: */
1047                 }
1048             }
1049         }
1050         alphar[j] = h__[j + j * h_dim1];
1051         alphai[j] = 0.f;
1052         beta[j] = t[j + j * t_dim1];
1053 /* L30: */
1054     }
1055
1056 /*     If IHI < ILO, skip QZ steps */
1057
1058     if (*ihi < *ilo) {
1059         goto L380;
1060     }
1061
1062 /*     MAIN QZ ITERATION LOOP */
1063
1064 /*     Initialize dynamic indices */
1065
1066 /*     Eigenvalues ILAST+1:N have been found. */
1067 /*        Column operations modify rows IFRSTM:whatever. */
1068 /*        Row operations modify columns whatever:ILASTM. */
1069
1070 /*     If only eigenvalues are being computed, then */
1071 /*        IFRSTM is the row of the last splitting row above row ILAST; */
1072 /*        this is always at least ILO. */
1073 /*     IITER counts iterations since the last eigenvalue was found, */
1074 /*        to tell when to use an extraordinary shift. */
1075 /*     MAXIT is the maximum number of QZ sweeps allowed. */
1076
1077     ilast = *ihi;
1078     if (ilschr) {
1079         ifrstm = 1;
1080         ilastm = *n;
1081     } else {
1082         ifrstm = *ilo;
1083         ilastm = *ihi;
1084     }
1085     iiter = 0;
1086     eshift = 0.f;
1087     maxit = (*ihi - *ilo + 1) * 30;
1088
1089     i__1 = maxit;
1090     for (jiter = 1; jiter <= i__1; ++jiter) {
1091
1092 /*        Split the matrix if possible. */
1093
1094 /*        Two tests: */
1095 /*           1: H(j,j-1)=0  or  j=ILO */
1096 /*           2: T(j,j)=0 */
1097
1098         if (ilast == *ilo) {
1099
1100 /*           Special case: j=ILAST */
1101
1102             goto L80;
1103         } else {
1104             if ((r__1 = h__[ilast + (ilast - 1) * h_dim1], abs(r__1)) <= atol)
1105                      {
1106                 h__[ilast + (ilast - 1) * h_dim1] = 0.f;
1107                 goto L80;
1108             }
1109         }
1110
1111         if ((r__1 = t[ilast + ilast * t_dim1], abs(r__1)) <= btol) {
1112             t[ilast + ilast * t_dim1] = 0.f;
1113             goto L70;
1114         }
1115
1116 /*        General case: j<ILAST */
1117
1118         i__2 = *ilo;
1119         for (j = ilast - 1; j >= i__2; --j) {
1120
1121 /*           Test 1: for H(j,j-1)=0 or j=ILO */
1122
1123             if (j == *ilo) {
1124                 ilazro = TRUE_;
1125             } else {
1126                 if ((r__1 = h__[j + (j - 1) * h_dim1], abs(r__1)) <= atol) {
1127                     h__[j + (j - 1) * h_dim1] = 0.f;
1128                     ilazro = TRUE_;
1129                 } else {
1130                     ilazro = FALSE_;
1131                 }
1132             }
1133
1134 /*           Test 2: for T(j,j)=0 */
1135
1136             if ((r__1 = t[j + j * t_dim1], abs(r__1)) < btol) {
1137                 t[j + j * t_dim1] = 0.f;
1138
1139 /*              Test 1a: Check for 2 consecutive small subdiagonals in A */
1140
1141                 ilazr2 = FALSE_;
1142                 if (! ilazro) {
1143                     temp = (r__1 = h__[j + (j - 1) * h_dim1], abs(r__1));
1144                     temp2 = (r__1 = h__[j + j * h_dim1], abs(r__1));
1145                     tempr = f2cmax(temp,temp2);
1146                     if (tempr < 1.f && tempr != 0.f) {
1147                         temp /= tempr;
1148                         temp2 /= tempr;
1149                     }
1150                     if (temp * (ascale * (r__1 = h__[j + 1 + j * h_dim1], abs(
1151                             r__1))) <= temp2 * (ascale * atol)) {
1152                         ilazr2 = TRUE_;
1153                     }
1154                 }
1155
1156 /*              If both tests pass (1 & 2), i.e., the leading diagonal */
1157 /*              element of B in the block is zero, split a 1x1 block off */
1158 /*              at the top. (I.e., at the J-th row/column) The leading */
1159 /*              diagonal element of the remainder can also be zero, so */
1160 /*              this may have to be done repeatedly. */
1161
1162                 if (ilazro || ilazr2) {
1163                     i__3 = ilast - 1;
1164                     for (jch = j; jch <= i__3; ++jch) {
1165                         temp = h__[jch + jch * h_dim1];
1166                         slartg_(&temp, &h__[jch + 1 + jch * h_dim1], &c__, &s,
1167                                  &h__[jch + jch * h_dim1]);
1168                         h__[jch + 1 + jch * h_dim1] = 0.f;
1169                         i__4 = ilastm - jch;
1170                         srot_(&i__4, &h__[jch + (jch + 1) * h_dim1], ldh, &
1171                                 h__[jch + 1 + (jch + 1) * h_dim1], ldh, &c__, 
1172                                 &s);
1173                         i__4 = ilastm - jch;
1174                         srot_(&i__4, &t[jch + (jch + 1) * t_dim1], ldt, &t[
1175                                 jch + 1 + (jch + 1) * t_dim1], ldt, &c__, &s);
1176                         if (ilq) {
1177                             srot_(n, &q[jch * q_dim1 + 1], &c__1, &q[(jch + 1)
1178                                      * q_dim1 + 1], &c__1, &c__, &s);
1179                         }
1180                         if (ilazr2) {
1181                             h__[jch + (jch - 1) * h_dim1] *= c__;
1182                         }
1183                         ilazr2 = FALSE_;
1184                         if ((r__1 = t[jch + 1 + (jch + 1) * t_dim1], abs(r__1)
1185                                 ) >= btol) {
1186                             if (jch + 1 >= ilast) {
1187                                 goto L80;
1188                             } else {
1189                                 ifirst = jch + 1;
1190                                 goto L110;
1191                             }
1192                         }
1193                         t[jch + 1 + (jch + 1) * t_dim1] = 0.f;
1194 /* L40: */
1195                     }
1196                     goto L70;
1197                 } else {
1198
1199 /*                 Only test 2 passed -- chase the zero to T(ILAST,ILAST) */
1200 /*                 Then process as in the case T(ILAST,ILAST)=0 */
1201
1202                     i__3 = ilast - 1;
1203                     for (jch = j; jch <= i__3; ++jch) {
1204                         temp = t[jch + (jch + 1) * t_dim1];
1205                         slartg_(&temp, &t[jch + 1 + (jch + 1) * t_dim1], &c__,
1206                                  &s, &t[jch + (jch + 1) * t_dim1]);
1207                         t[jch + 1 + (jch + 1) * t_dim1] = 0.f;
1208                         if (jch < ilastm - 1) {
1209                             i__4 = ilastm - jch - 1;
1210                             srot_(&i__4, &t[jch + (jch + 2) * t_dim1], ldt, &
1211                                     t[jch + 1 + (jch + 2) * t_dim1], ldt, &
1212                                     c__, &s);
1213                         }
1214                         i__4 = ilastm - jch + 2;
1215                         srot_(&i__4, &h__[jch + (jch - 1) * h_dim1], ldh, &
1216                                 h__[jch + 1 + (jch - 1) * h_dim1], ldh, &c__, 
1217                                 &s);
1218                         if (ilq) {
1219                             srot_(n, &q[jch * q_dim1 + 1], &c__1, &q[(jch + 1)
1220                                      * q_dim1 + 1], &c__1, &c__, &s);
1221                         }
1222                         temp = h__[jch + 1 + jch * h_dim1];
1223                         slartg_(&temp, &h__[jch + 1 + (jch - 1) * h_dim1], &
1224                                 c__, &s, &h__[jch + 1 + jch * h_dim1]);
1225                         h__[jch + 1 + (jch - 1) * h_dim1] = 0.f;
1226                         i__4 = jch + 1 - ifrstm;
1227                         srot_(&i__4, &h__[ifrstm + jch * h_dim1], &c__1, &h__[
1228                                 ifrstm + (jch - 1) * h_dim1], &c__1, &c__, &s)
1229                                 ;
1230                         i__4 = jch - ifrstm;
1231                         srot_(&i__4, &t[ifrstm + jch * t_dim1], &c__1, &t[
1232                                 ifrstm + (jch - 1) * t_dim1], &c__1, &c__, &s)
1233                                 ;
1234                         if (ilz) {
1235                             srot_(n, &z__[jch * z_dim1 + 1], &c__1, &z__[(jch 
1236                                     - 1) * z_dim1 + 1], &c__1, &c__, &s);
1237                         }
1238 /* L50: */
1239                     }
1240                     goto L70;
1241                 }
1242             } else if (ilazro) {
1243
1244 /*              Only test 1 passed -- work on J:ILAST */
1245
1246                 ifirst = j;
1247                 goto L110;
1248             }
1249
1250 /*           Neither test passed -- try next J */
1251
1252 /* L60: */
1253         }
1254
1255 /*        (Drop-through is "impossible") */
1256
1257         *info = *n + 1;
1258         goto L420;
1259
1260 /*        T(ILAST,ILAST)=0 -- clear H(ILAST,ILAST-1) to split off a */
1261 /*        1x1 block. */
1262
1263 L70:
1264         temp = h__[ilast + ilast * h_dim1];
1265         slartg_(&temp, &h__[ilast + (ilast - 1) * h_dim1], &c__, &s, &h__[
1266                 ilast + ilast * h_dim1]);
1267         h__[ilast + (ilast - 1) * h_dim1] = 0.f;
1268         i__2 = ilast - ifrstm;
1269         srot_(&i__2, &h__[ifrstm + ilast * h_dim1], &c__1, &h__[ifrstm + (
1270                 ilast - 1) * h_dim1], &c__1, &c__, &s);
1271         i__2 = ilast - ifrstm;
1272         srot_(&i__2, &t[ifrstm + ilast * t_dim1], &c__1, &t[ifrstm + (ilast - 
1273                 1) * t_dim1], &c__1, &c__, &s);
1274         if (ilz) {
1275             srot_(n, &z__[ilast * z_dim1 + 1], &c__1, &z__[(ilast - 1) * 
1276                     z_dim1 + 1], &c__1, &c__, &s);
1277         }
1278
1279 /*        H(ILAST,ILAST-1)=0 -- Standardize B, set ALPHAR, ALPHAI, */
1280 /*                              and BETA */
1281
1282 L80:
1283         if (t[ilast + ilast * t_dim1] < 0.f) {
1284             if (ilschr) {
1285                 i__2 = ilast;
1286                 for (j = ifrstm; j <= i__2; ++j) {
1287                     h__[j + ilast * h_dim1] = -h__[j + ilast * h_dim1];
1288                     t[j + ilast * t_dim1] = -t[j + ilast * t_dim1];
1289 /* L90: */
1290                 }
1291             } else {
1292                 h__[ilast + ilast * h_dim1] = -h__[ilast + ilast * h_dim1];
1293                 t[ilast + ilast * t_dim1] = -t[ilast + ilast * t_dim1];
1294             }
1295             if (ilz) {
1296                 i__2 = *n;
1297                 for (j = 1; j <= i__2; ++j) {
1298                     z__[j + ilast * z_dim1] = -z__[j + ilast * z_dim1];
1299 /* L100: */
1300                 }
1301             }
1302         }
1303         alphar[ilast] = h__[ilast + ilast * h_dim1];
1304         alphai[ilast] = 0.f;
1305         beta[ilast] = t[ilast + ilast * t_dim1];
1306
1307 /*        Go to next block -- exit if finished. */
1308
1309         --ilast;
1310         if (ilast < *ilo) {
1311             goto L380;
1312         }
1313
1314 /*        Reset counters */
1315
1316         iiter = 0;
1317         eshift = 0.f;
1318         if (! ilschr) {
1319             ilastm = ilast;
1320             if (ifrstm > ilast) {
1321                 ifrstm = *ilo;
1322             }
1323         }
1324         goto L350;
1325
1326 /*        QZ step */
1327
1328 /*        This iteration only involves rows/columns IFIRST:ILAST. We */
1329 /*        assume IFIRST < ILAST, and that the diagonal of B is non-zero. */
1330
1331 L110:
1332         ++iiter;
1333         if (! ilschr) {
1334             ifrstm = ifirst;
1335         }
1336
1337 /*        Compute single shifts. */
1338
1339 /*        At this point, IFIRST < ILAST, and the diagonal elements of */
1340 /*        T(IFIRST:ILAST,IFIRST,ILAST) are larger than BTOL (in */
1341 /*        magnitude) */
1342
1343         if (iiter / 10 * 10 == iiter) {
1344
1345 /*           Exceptional shift.  Chosen for no particularly good reason. */
1346 /*           (Single shift only.) */
1347
1348             if ((real) maxit * safmin * (r__1 = h__[ilast + (ilast - 1) * 
1349                     h_dim1], abs(r__1)) < (r__2 = t[ilast - 1 + (ilast - 1) * 
1350                     t_dim1], abs(r__2))) {
1351                 eshift = h__[ilast + (ilast - 1) * h_dim1] / t[ilast - 1 + (
1352                         ilast - 1) * t_dim1];
1353             } else {
1354                 eshift += 1.f / (safmin * (real) maxit);
1355             }
1356             s1 = 1.f;
1357             wr = eshift;
1358
1359         } else {
1360
1361 /*           Shifts based on the generalized eigenvalues of the */
1362 /*           bottom-right 2x2 block of A and B. The first eigenvalue */
1363 /*           returned by SLAG2 is the Wilkinson shift (AEP p.512), */
1364
1365             r__1 = safmin * 100.f;
1366             slag2_(&h__[ilast - 1 + (ilast - 1) * h_dim1], ldh, &t[ilast - 1 
1367                     + (ilast - 1) * t_dim1], ldt, &r__1, &s1, &s2, &wr, &wr2, 
1368                     &wi);
1369
1370             if ((r__1 = wr / s1 * t[ilast + ilast * t_dim1] - h__[ilast + 
1371                     ilast * h_dim1], abs(r__1)) > (r__2 = wr2 / s2 * t[ilast 
1372                     + ilast * t_dim1] - h__[ilast + ilast * h_dim1], abs(r__2)
1373                     )) {
1374                 temp = wr;
1375                 wr = wr2;
1376                 wr2 = temp;
1377                 temp = s1;
1378                 s1 = s2;
1379                 s2 = temp;
1380             }
1381 /* Computing MAX */
1382 /* Computing MAX */
1383             r__3 = 1.f, r__4 = abs(wr), r__3 = f2cmax(r__3,r__4), r__4 = abs(wi);
1384             r__1 = s1, r__2 = safmin * f2cmax(r__3,r__4);
1385             temp = f2cmax(r__1,r__2);
1386             if (wi != 0.f) {
1387                 goto L200;
1388             }
1389         }
1390
1391 /*        Fiddle with shift to avoid overflow */
1392
1393         temp = f2cmin(ascale,1.f) * (safmax * .5f);
1394         if (s1 > temp) {
1395             scale = temp / s1;
1396         } else {
1397             scale = 1.f;
1398         }
1399
1400         temp = f2cmin(bscale,1.f) * (safmax * .5f);
1401         if (abs(wr) > temp) {
1402 /* Computing MIN */
1403             r__1 = scale, r__2 = temp / abs(wr);
1404             scale = f2cmin(r__1,r__2);
1405         }
1406         s1 = scale * s1;
1407         wr = scale * wr;
1408
1409 /*        Now check for two consecutive small subdiagonals. */
1410
1411         i__2 = ifirst + 1;
1412         for (j = ilast - 1; j >= i__2; --j) {
1413             istart = j;
1414             temp = (r__1 = s1 * h__[j + (j - 1) * h_dim1], abs(r__1));
1415             temp2 = (r__1 = s1 * h__[j + j * h_dim1] - wr * t[j + j * t_dim1],
1416                      abs(r__1));
1417             tempr = f2cmax(temp,temp2);
1418             if (tempr < 1.f && tempr != 0.f) {
1419                 temp /= tempr;
1420                 temp2 /= tempr;
1421             }
1422             if ((r__1 = ascale * h__[j + 1 + j * h_dim1] * temp, abs(r__1)) <=
1423                      ascale * atol * temp2) {
1424                 goto L130;
1425             }
1426 /* L120: */
1427         }
1428
1429         istart = ifirst;
1430 L130:
1431
1432 /*        Do an implicit single-shift QZ sweep. */
1433
1434 /*        Initial Q */
1435
1436         temp = s1 * h__[istart + istart * h_dim1] - wr * t[istart + istart * 
1437                 t_dim1];
1438         temp2 = s1 * h__[istart + 1 + istart * h_dim1];
1439         slartg_(&temp, &temp2, &c__, &s, &tempr);
1440
1441 /*        Sweep */
1442
1443         i__2 = ilast - 1;
1444         for (j = istart; j <= i__2; ++j) {
1445             if (j > istart) {
1446                 temp = h__[j + (j - 1) * h_dim1];
1447                 slartg_(&temp, &h__[j + 1 + (j - 1) * h_dim1], &c__, &s, &h__[
1448                         j + (j - 1) * h_dim1]);
1449                 h__[j + 1 + (j - 1) * h_dim1] = 0.f;
1450             }
1451
1452             i__3 = ilastm;
1453             for (jc = j; jc <= i__3; ++jc) {
1454                 temp = c__ * h__[j + jc * h_dim1] + s * h__[j + 1 + jc * 
1455                         h_dim1];
1456                 h__[j + 1 + jc * h_dim1] = -s * h__[j + jc * h_dim1] + c__ * 
1457                         h__[j + 1 + jc * h_dim1];
1458                 h__[j + jc * h_dim1] = temp;
1459                 temp2 = c__ * t[j + jc * t_dim1] + s * t[j + 1 + jc * t_dim1];
1460                 t[j + 1 + jc * t_dim1] = -s * t[j + jc * t_dim1] + c__ * t[j 
1461                         + 1 + jc * t_dim1];
1462                 t[j + jc * t_dim1] = temp2;
1463 /* L140: */
1464             }
1465             if (ilq) {
1466                 i__3 = *n;
1467                 for (jr = 1; jr <= i__3; ++jr) {
1468                     temp = c__ * q[jr + j * q_dim1] + s * q[jr + (j + 1) * 
1469                             q_dim1];
1470                     q[jr + (j + 1) * q_dim1] = -s * q[jr + j * q_dim1] + c__ *
1471                              q[jr + (j + 1) * q_dim1];
1472                     q[jr + j * q_dim1] = temp;
1473 /* L150: */
1474                 }
1475             }
1476
1477             temp = t[j + 1 + (j + 1) * t_dim1];
1478             slartg_(&temp, &t[j + 1 + j * t_dim1], &c__, &s, &t[j + 1 + (j + 
1479                     1) * t_dim1]);
1480             t[j + 1 + j * t_dim1] = 0.f;
1481
1482 /* Computing MIN */
1483             i__4 = j + 2;
1484             i__3 = f2cmin(i__4,ilast);
1485             for (jr = ifrstm; jr <= i__3; ++jr) {
1486                 temp = c__ * h__[jr + (j + 1) * h_dim1] + s * h__[jr + j * 
1487                         h_dim1];
1488                 h__[jr + j * h_dim1] = -s * h__[jr + (j + 1) * h_dim1] + c__ *
1489                          h__[jr + j * h_dim1];
1490                 h__[jr + (j + 1) * h_dim1] = temp;
1491 /* L160: */
1492             }
1493             i__3 = j;
1494             for (jr = ifrstm; jr <= i__3; ++jr) {
1495                 temp = c__ * t[jr + (j + 1) * t_dim1] + s * t[jr + j * t_dim1]
1496                         ;
1497                 t[jr + j * t_dim1] = -s * t[jr + (j + 1) * t_dim1] + c__ * t[
1498                         jr + j * t_dim1];
1499                 t[jr + (j + 1) * t_dim1] = temp;
1500 /* L170: */
1501             }
1502             if (ilz) {
1503                 i__3 = *n;
1504                 for (jr = 1; jr <= i__3; ++jr) {
1505                     temp = c__ * z__[jr + (j + 1) * z_dim1] + s * z__[jr + j *
1506                              z_dim1];
1507                     z__[jr + j * z_dim1] = -s * z__[jr + (j + 1) * z_dim1] + 
1508                             c__ * z__[jr + j * z_dim1];
1509                     z__[jr + (j + 1) * z_dim1] = temp;
1510 /* L180: */
1511                 }
1512             }
1513 /* L190: */
1514         }
1515
1516         goto L350;
1517
1518 /*        Use Francis double-shift */
1519
1520 /*        Note: the Francis double-shift should work with real shifts, */
1521 /*              but only if the block is at least 3x3. */
1522 /*              This code may break if this point is reached with */
1523 /*              a 2x2 block with real eigenvalues. */
1524
1525 L200:
1526         if (ifirst + 1 == ilast) {
1527
1528 /*           Special case -- 2x2 block with complex eigenvectors */
1529
1530 /*           Step 1: Standardize, that is, rotate so that */
1531
1532 /*                       ( B11  0  ) */
1533 /*                   B = (         )  with B11 non-negative. */
1534 /*                       (  0  B22 ) */
1535
1536             slasv2_(&t[ilast - 1 + (ilast - 1) * t_dim1], &t[ilast - 1 + 
1537                     ilast * t_dim1], &t[ilast + ilast * t_dim1], &b22, &b11, &
1538                     sr, &cr, &sl, &cl);
1539
1540             if (b11 < 0.f) {
1541                 cr = -cr;
1542                 sr = -sr;
1543                 b11 = -b11;
1544                 b22 = -b22;
1545             }
1546
1547             i__2 = ilastm + 1 - ifirst;
1548             srot_(&i__2, &h__[ilast - 1 + (ilast - 1) * h_dim1], ldh, &h__[
1549                     ilast + (ilast - 1) * h_dim1], ldh, &cl, &sl);
1550             i__2 = ilast + 1 - ifrstm;
1551             srot_(&i__2, &h__[ifrstm + (ilast - 1) * h_dim1], &c__1, &h__[
1552                     ifrstm + ilast * h_dim1], &c__1, &cr, &sr);
1553
1554             if (ilast < ilastm) {
1555                 i__2 = ilastm - ilast;
1556                 srot_(&i__2, &t[ilast - 1 + (ilast + 1) * t_dim1], ldt, &t[
1557                         ilast + (ilast + 1) * t_dim1], ldt, &cl, &sl);
1558             }
1559             if (ifrstm < ilast - 1) {
1560                 i__2 = ifirst - ifrstm;
1561                 srot_(&i__2, &t[ifrstm + (ilast - 1) * t_dim1], &c__1, &t[
1562                         ifrstm + ilast * t_dim1], &c__1, &cr, &sr);
1563             }
1564
1565             if (ilq) {
1566                 srot_(n, &q[(ilast - 1) * q_dim1 + 1], &c__1, &q[ilast * 
1567                         q_dim1 + 1], &c__1, &cl, &sl);
1568             }
1569             if (ilz) {
1570                 srot_(n, &z__[(ilast - 1) * z_dim1 + 1], &c__1, &z__[ilast * 
1571                         z_dim1 + 1], &c__1, &cr, &sr);
1572             }
1573
1574             t[ilast - 1 + (ilast - 1) * t_dim1] = b11;
1575             t[ilast - 1 + ilast * t_dim1] = 0.f;
1576             t[ilast + (ilast - 1) * t_dim1] = 0.f;
1577             t[ilast + ilast * t_dim1] = b22;
1578
1579 /*           If B22 is negative, negate column ILAST */
1580
1581             if (b22 < 0.f) {
1582                 i__2 = ilast;
1583                 for (j = ifrstm; j <= i__2; ++j) {
1584                     h__[j + ilast * h_dim1] = -h__[j + ilast * h_dim1];
1585                     t[j + ilast * t_dim1] = -t[j + ilast * t_dim1];
1586 /* L210: */
1587                 }
1588
1589                 if (ilz) {
1590                     i__2 = *n;
1591                     for (j = 1; j <= i__2; ++j) {
1592                         z__[j + ilast * z_dim1] = -z__[j + ilast * z_dim1];
1593 /* L220: */
1594                     }
1595                 }
1596                 b22 = -b22;
1597             }
1598
1599 /*           Step 2: Compute ALPHAR, ALPHAI, and BETA (see refs.) */
1600
1601 /*           Recompute shift */
1602
1603             r__1 = safmin * 100.f;
1604             slag2_(&h__[ilast - 1 + (ilast - 1) * h_dim1], ldh, &t[ilast - 1 
1605                     + (ilast - 1) * t_dim1], ldt, &r__1, &s1, &temp, &wr, &
1606                     temp2, &wi);
1607
1608 /*           If standardization has perturbed the shift onto real line, */
1609 /*           do another (real single-shift) QR step. */
1610
1611             if (wi == 0.f) {
1612                 goto L350;
1613             }
1614             s1inv = 1.f / s1;
1615
1616 /*           Do EISPACK (QZVAL) computation of alpha and beta */
1617
1618             a11 = h__[ilast - 1 + (ilast - 1) * h_dim1];
1619             a21 = h__[ilast + (ilast - 1) * h_dim1];
1620             a12 = h__[ilast - 1 + ilast * h_dim1];
1621             a22 = h__[ilast + ilast * h_dim1];
1622
1623 /*           Compute complex Givens rotation on right */
1624 /*           (Assume some element of C = (sA - wB) > unfl ) */
1625 /*                            __ */
1626 /*           (sA - wB) ( CZ   -SZ ) */
1627 /*                     ( SZ    CZ ) */
1628
1629             c11r = s1 * a11 - wr * b11;
1630             c11i = -wi * b11;
1631             c12 = s1 * a12;
1632             c21 = s1 * a21;
1633             c22r = s1 * a22 - wr * b22;
1634             c22i = -wi * b22;
1635
1636             if (abs(c11r) + abs(c11i) + abs(c12) > abs(c21) + abs(c22r) + abs(
1637                     c22i)) {
1638                 t1 = slapy3_(&c12, &c11r, &c11i);
1639                 cz = c12 / t1;
1640                 szr = -c11r / t1;
1641                 szi = -c11i / t1;
1642             } else {
1643                 cz = slapy2_(&c22r, &c22i);
1644                 if (cz <= safmin) {
1645                     cz = 0.f;
1646                     szr = 1.f;
1647                     szi = 0.f;
1648                 } else {
1649                     tempr = c22r / cz;
1650                     tempi = c22i / cz;
1651                     t1 = slapy2_(&cz, &c21);
1652                     cz /= t1;
1653                     szr = -c21 * tempr / t1;
1654                     szi = c21 * tempi / t1;
1655                 }
1656             }
1657
1658 /*           Compute Givens rotation on left */
1659
1660 /*           (  CQ   SQ ) */
1661 /*           (  __      )  A or B */
1662 /*           ( -SQ   CQ ) */
1663
1664             an = abs(a11) + abs(a12) + abs(a21) + abs(a22);
1665             bn = abs(b11) + abs(b22);
1666             wabs = abs(wr) + abs(wi);
1667             if (s1 * an > wabs * bn) {
1668                 cq = cz * b11;
1669                 sqr = szr * b22;
1670                 sqi = -szi * b22;
1671             } else {
1672                 a1r = cz * a11 + szr * a12;
1673                 a1i = szi * a12;
1674                 a2r = cz * a21 + szr * a22;
1675                 a2i = szi * a22;
1676                 cq = slapy2_(&a1r, &a1i);
1677                 if (cq <= safmin) {
1678                     cq = 0.f;
1679                     sqr = 1.f;
1680                     sqi = 0.f;
1681                 } else {
1682                     tempr = a1r / cq;
1683                     tempi = a1i / cq;
1684                     sqr = tempr * a2r + tempi * a2i;
1685                     sqi = tempi * a2r - tempr * a2i;
1686                 }
1687             }
1688             t1 = slapy3_(&cq, &sqr, &sqi);
1689             cq /= t1;
1690             sqr /= t1;
1691             sqi /= t1;
1692
1693 /*           Compute diagonal elements of QBZ */
1694
1695             tempr = sqr * szr - sqi * szi;
1696             tempi = sqr * szi + sqi * szr;
1697             b1r = cq * cz * b11 + tempr * b22;
1698             b1i = tempi * b22;
1699             b1a = slapy2_(&b1r, &b1i);
1700             b2r = cq * cz * b22 + tempr * b11;
1701             b2i = -tempi * b11;
1702             b2a = slapy2_(&b2r, &b2i);
1703
1704 /*           Normalize so beta > 0, and Im( alpha1 ) > 0 */
1705
1706             beta[ilast - 1] = b1a;
1707             beta[ilast] = b2a;
1708             alphar[ilast - 1] = wr * b1a * s1inv;
1709             alphai[ilast - 1] = wi * b1a * s1inv;
1710             alphar[ilast] = wr * b2a * s1inv;
1711             alphai[ilast] = -(wi * b2a) * s1inv;
1712
1713 /*           Step 3: Go to next block -- exit if finished. */
1714
1715             ilast = ifirst - 1;
1716             if (ilast < *ilo) {
1717                 goto L380;
1718             }
1719
1720 /*           Reset counters */
1721
1722             iiter = 0;
1723             eshift = 0.f;
1724             if (! ilschr) {
1725                 ilastm = ilast;
1726                 if (ifrstm > ilast) {
1727                     ifrstm = *ilo;
1728                 }
1729             }
1730             goto L350;
1731         } else {
1732
1733 /*           Usual case: 3x3 or larger block, using Francis implicit */
1734 /*                       double-shift */
1735
1736 /*                                    2 */
1737 /*           Eigenvalue equation is  w  - c w + d = 0, */
1738
1739 /*                                         -1 2        -1 */
1740 /*           so compute 1st column of  (A B  )  - c A B   + d */
1741 /*           using the formula in QZIT (from EISPACK) */
1742
1743 /*           We assume that the block is at least 3x3 */
1744
1745             ad11 = ascale * h__[ilast - 1 + (ilast - 1) * h_dim1] / (bscale * 
1746                     t[ilast - 1 + (ilast - 1) * t_dim1]);
1747             ad21 = ascale * h__[ilast + (ilast - 1) * h_dim1] / (bscale * t[
1748                     ilast - 1 + (ilast - 1) * t_dim1]);
1749             ad12 = ascale * h__[ilast - 1 + ilast * h_dim1] / (bscale * t[
1750                     ilast + ilast * t_dim1]);
1751             ad22 = ascale * h__[ilast + ilast * h_dim1] / (bscale * t[ilast + 
1752                     ilast * t_dim1]);
1753             u12 = t[ilast - 1 + ilast * t_dim1] / t[ilast + ilast * t_dim1];
1754             ad11l = ascale * h__[ifirst + ifirst * h_dim1] / (bscale * t[
1755                     ifirst + ifirst * t_dim1]);
1756             ad21l = ascale * h__[ifirst + 1 + ifirst * h_dim1] / (bscale * t[
1757                     ifirst + ifirst * t_dim1]);
1758             ad12l = ascale * h__[ifirst + (ifirst + 1) * h_dim1] / (bscale * 
1759                     t[ifirst + 1 + (ifirst + 1) * t_dim1]);
1760             ad22l = ascale * h__[ifirst + 1 + (ifirst + 1) * h_dim1] / (
1761                     bscale * t[ifirst + 1 + (ifirst + 1) * t_dim1]);
1762             ad32l = ascale * h__[ifirst + 2 + (ifirst + 1) * h_dim1] / (
1763                     bscale * t[ifirst + 1 + (ifirst + 1) * t_dim1]);
1764             u12l = t[ifirst + (ifirst + 1) * t_dim1] / t[ifirst + 1 + (ifirst 
1765                     + 1) * t_dim1];
1766
1767             v[0] = (ad11 - ad11l) * (ad22 - ad11l) - ad12 * ad21 + ad21 * u12 
1768                     * ad11l + (ad12l - ad11l * u12l) * ad21l;
1769             v[1] = (ad22l - ad11l - ad21l * u12l - (ad11 - ad11l) - (ad22 - 
1770                     ad11l) + ad21 * u12) * ad21l;
1771             v[2] = ad32l * ad21l;
1772
1773             istart = ifirst;
1774
1775             slarfg_(&c__3, v, &v[1], &c__1, &tau);
1776             v[0] = 1.f;
1777
1778 /*           Sweep */
1779
1780             i__2 = ilast - 2;
1781             for (j = istart; j <= i__2; ++j) {
1782
1783 /*              All but last elements: use 3x3 Householder transforms. */
1784
1785 /*              Zero (j-1)st column of A */
1786
1787                 if (j > istart) {
1788                     v[0] = h__[j + (j - 1) * h_dim1];
1789                     v[1] = h__[j + 1 + (j - 1) * h_dim1];
1790                     v[2] = h__[j + 2 + (j - 1) * h_dim1];
1791
1792                     slarfg_(&c__3, &h__[j + (j - 1) * h_dim1], &v[1], &c__1, &
1793                             tau);
1794                     v[0] = 1.f;
1795                     h__[j + 1 + (j - 1) * h_dim1] = 0.f;
1796                     h__[j + 2 + (j - 1) * h_dim1] = 0.f;
1797                 }
1798
1799                 i__3 = ilastm;
1800                 for (jc = j; jc <= i__3; ++jc) {
1801                     temp = tau * (h__[j + jc * h_dim1] + v[1] * h__[j + 1 + 
1802                             jc * h_dim1] + v[2] * h__[j + 2 + jc * h_dim1]);
1803                     h__[j + jc * h_dim1] -= temp;
1804                     h__[j + 1 + jc * h_dim1] -= temp * v[1];
1805                     h__[j + 2 + jc * h_dim1] -= temp * v[2];
1806                     temp2 = tau * (t[j + jc * t_dim1] + v[1] * t[j + 1 + jc * 
1807                             t_dim1] + v[2] * t[j + 2 + jc * t_dim1]);
1808                     t[j + jc * t_dim1] -= temp2;
1809                     t[j + 1 + jc * t_dim1] -= temp2 * v[1];
1810                     t[j + 2 + jc * t_dim1] -= temp2 * v[2];
1811 /* L230: */
1812                 }
1813                 if (ilq) {
1814                     i__3 = *n;
1815                     for (jr = 1; jr <= i__3; ++jr) {
1816                         temp = tau * (q[jr + j * q_dim1] + v[1] * q[jr + (j + 
1817                                 1) * q_dim1] + v[2] * q[jr + (j + 2) * q_dim1]
1818                                 );
1819                         q[jr + j * q_dim1] -= temp;
1820                         q[jr + (j + 1) * q_dim1] -= temp * v[1];
1821                         q[jr + (j + 2) * q_dim1] -= temp * v[2];
1822 /* L240: */
1823                     }
1824                 }
1825
1826 /*              Zero j-th column of B (see SLAGBC for details) */
1827
1828 /*              Swap rows to pivot */
1829
1830                 ilpivt = FALSE_;
1831 /* Computing MAX */
1832                 r__3 = (r__1 = t[j + 1 + (j + 1) * t_dim1], abs(r__1)), r__4 =
1833                          (r__2 = t[j + 1 + (j + 2) * t_dim1], abs(r__2));
1834                 temp = f2cmax(r__3,r__4);
1835 /* Computing MAX */
1836                 r__3 = (r__1 = t[j + 2 + (j + 1) * t_dim1], abs(r__1)), r__4 =
1837                          (r__2 = t[j + 2 + (j + 2) * t_dim1], abs(r__2));
1838                 temp2 = f2cmax(r__3,r__4);
1839                 if (f2cmax(temp,temp2) < safmin) {
1840                     scale = 0.f;
1841                     u1 = 1.f;
1842                     u2 = 0.f;
1843                     goto L250;
1844                 } else if (temp >= temp2) {
1845                     w11 = t[j + 1 + (j + 1) * t_dim1];
1846                     w21 = t[j + 2 + (j + 1) * t_dim1];
1847                     w12 = t[j + 1 + (j + 2) * t_dim1];
1848                     w22 = t[j + 2 + (j + 2) * t_dim1];
1849                     u1 = t[j + 1 + j * t_dim1];
1850                     u2 = t[j + 2 + j * t_dim1];
1851                 } else {
1852                     w21 = t[j + 1 + (j + 1) * t_dim1];
1853                     w11 = t[j + 2 + (j + 1) * t_dim1];
1854                     w22 = t[j + 1 + (j + 2) * t_dim1];
1855                     w12 = t[j + 2 + (j + 2) * t_dim1];
1856                     u2 = t[j + 1 + j * t_dim1];
1857                     u1 = t[j + 2 + j * t_dim1];
1858                 }
1859
1860 /*              Swap columns if nec. */
1861
1862                 if (abs(w12) > abs(w11)) {
1863                     ilpivt = TRUE_;
1864                     temp = w12;
1865                     temp2 = w22;
1866                     w12 = w11;
1867                     w22 = w21;
1868                     w11 = temp;
1869                     w21 = temp2;
1870                 }
1871
1872 /*              LU-factor */
1873
1874                 temp = w21 / w11;
1875                 u2 -= temp * u1;
1876                 w22 -= temp * w12;
1877                 w21 = 0.f;
1878
1879 /*              Compute SCALE */
1880
1881                 scale = 1.f;
1882                 if (abs(w22) < safmin) {
1883                     scale = 0.f;
1884                     u2 = 1.f;
1885                     u1 = -w12 / w11;
1886                     goto L250;
1887                 }
1888                 if (abs(w22) < abs(u2)) {
1889                     scale = (r__1 = w22 / u2, abs(r__1));
1890                 }
1891                 if (abs(w11) < abs(u1)) {
1892 /* Computing MIN */
1893                     r__2 = scale, r__3 = (r__1 = w11 / u1, abs(r__1));
1894                     scale = f2cmin(r__2,r__3);
1895                 }
1896
1897 /*              Solve */
1898
1899                 u2 = scale * u2 / w22;
1900                 u1 = (scale * u1 - w12 * u2) / w11;
1901
1902 L250:
1903                 if (ilpivt) {
1904                     temp = u2;
1905                     u2 = u1;
1906                     u1 = temp;
1907                 }
1908
1909 /*              Compute Householder Vector */
1910
1911 /* Computing 2nd power */
1912                 r__1 = scale;
1913 /* Computing 2nd power */
1914                 r__2 = u1;
1915 /* Computing 2nd power */
1916                 r__3 = u2;
1917                 t1 = sqrt(r__1 * r__1 + r__2 * r__2 + r__3 * r__3);
1918                 tau = scale / t1 + 1.f;
1919                 vs = -1.f / (scale + t1);
1920                 v[0] = 1.f;
1921                 v[1] = vs * u1;
1922                 v[2] = vs * u2;
1923
1924 /*              Apply transformations from the right. */
1925
1926 /* Computing MIN */
1927                 i__4 = j + 3;
1928                 i__3 = f2cmin(i__4,ilast);
1929                 for (jr = ifrstm; jr <= i__3; ++jr) {
1930                     temp = tau * (h__[jr + j * h_dim1] + v[1] * h__[jr + (j + 
1931                             1) * h_dim1] + v[2] * h__[jr + (j + 2) * h_dim1]);
1932                     h__[jr + j * h_dim1] -= temp;
1933                     h__[jr + (j + 1) * h_dim1] -= temp * v[1];
1934                     h__[jr + (j + 2) * h_dim1] -= temp * v[2];
1935 /* L260: */
1936                 }
1937                 i__3 = j + 2;
1938                 for (jr = ifrstm; jr <= i__3; ++jr) {
1939                     temp = tau * (t[jr + j * t_dim1] + v[1] * t[jr + (j + 1) *
1940                              t_dim1] + v[2] * t[jr + (j + 2) * t_dim1]);
1941                     t[jr + j * t_dim1] -= temp;
1942                     t[jr + (j + 1) * t_dim1] -= temp * v[1];
1943                     t[jr + (j + 2) * t_dim1] -= temp * v[2];
1944 /* L270: */
1945                 }
1946                 if (ilz) {
1947                     i__3 = *n;
1948                     for (jr = 1; jr <= i__3; ++jr) {
1949                         temp = tau * (z__[jr + j * z_dim1] + v[1] * z__[jr + (
1950                                 j + 1) * z_dim1] + v[2] * z__[jr + (j + 2) * 
1951                                 z_dim1]);
1952                         z__[jr + j * z_dim1] -= temp;
1953                         z__[jr + (j + 1) * z_dim1] -= temp * v[1];
1954                         z__[jr + (j + 2) * z_dim1] -= temp * v[2];
1955 /* L280: */
1956                     }
1957                 }
1958                 t[j + 1 + j * t_dim1] = 0.f;
1959                 t[j + 2 + j * t_dim1] = 0.f;
1960 /* L290: */
1961             }
1962
1963 /*           Last elements: Use Givens rotations */
1964
1965 /*           Rotations from the left */
1966
1967             j = ilast - 1;
1968             temp = h__[j + (j - 1) * h_dim1];
1969             slartg_(&temp, &h__[j + 1 + (j - 1) * h_dim1], &c__, &s, &h__[j + 
1970                     (j - 1) * h_dim1]);
1971             h__[j + 1 + (j - 1) * h_dim1] = 0.f;
1972
1973             i__2 = ilastm;
1974             for (jc = j; jc <= i__2; ++jc) {
1975                 temp = c__ * h__[j + jc * h_dim1] + s * h__[j + 1 + jc * 
1976                         h_dim1];
1977                 h__[j + 1 + jc * h_dim1] = -s * h__[j + jc * h_dim1] + c__ * 
1978                         h__[j + 1 + jc * h_dim1];
1979                 h__[j + jc * h_dim1] = temp;
1980                 temp2 = c__ * t[j + jc * t_dim1] + s * t[j + 1 + jc * t_dim1];
1981                 t[j + 1 + jc * t_dim1] = -s * t[j + jc * t_dim1] + c__ * t[j 
1982                         + 1 + jc * t_dim1];
1983                 t[j + jc * t_dim1] = temp2;
1984 /* L300: */
1985             }
1986             if (ilq) {
1987                 i__2 = *n;
1988                 for (jr = 1; jr <= i__2; ++jr) {
1989                     temp = c__ * q[jr + j * q_dim1] + s * q[jr + (j + 1) * 
1990                             q_dim1];
1991                     q[jr + (j + 1) * q_dim1] = -s * q[jr + j * q_dim1] + c__ *
1992                              q[jr + (j + 1) * q_dim1];
1993                     q[jr + j * q_dim1] = temp;
1994 /* L310: */
1995                 }
1996             }
1997
1998 /*           Rotations from the right. */
1999
2000             temp = t[j + 1 + (j + 1) * t_dim1];
2001             slartg_(&temp, &t[j + 1 + j * t_dim1], &c__, &s, &t[j + 1 + (j + 
2002                     1) * t_dim1]);
2003             t[j + 1 + j * t_dim1] = 0.f;
2004
2005             i__2 = ilast;
2006             for (jr = ifrstm; jr <= i__2; ++jr) {
2007                 temp = c__ * h__[jr + (j + 1) * h_dim1] + s * h__[jr + j * 
2008                         h_dim1];
2009                 h__[jr + j * h_dim1] = -s * h__[jr + (j + 1) * h_dim1] + c__ *
2010                          h__[jr + j * h_dim1];
2011                 h__[jr + (j + 1) * h_dim1] = temp;
2012 /* L320: */
2013             }
2014             i__2 = ilast - 1;
2015             for (jr = ifrstm; jr <= i__2; ++jr) {
2016                 temp = c__ * t[jr + (j + 1) * t_dim1] + s * t[jr + j * t_dim1]
2017                         ;
2018                 t[jr + j * t_dim1] = -s * t[jr + (j + 1) * t_dim1] + c__ * t[
2019                         jr + j * t_dim1];
2020                 t[jr + (j + 1) * t_dim1] = temp;
2021 /* L330: */
2022             }
2023             if (ilz) {
2024                 i__2 = *n;
2025                 for (jr = 1; jr <= i__2; ++jr) {
2026                     temp = c__ * z__[jr + (j + 1) * z_dim1] + s * z__[jr + j *
2027                              z_dim1];
2028                     z__[jr + j * z_dim1] = -s * z__[jr + (j + 1) * z_dim1] + 
2029                             c__ * z__[jr + j * z_dim1];
2030                     z__[jr + (j + 1) * z_dim1] = temp;
2031 /* L340: */
2032                 }
2033             }
2034
2035 /*           End of Double-Shift code */
2036
2037         }
2038
2039         goto L350;
2040
2041 /*        End of iteration loop */
2042
2043 L350:
2044 /* L360: */
2045         ;
2046     }
2047
2048 /*     Drop-through = non-convergence */
2049
2050     *info = ilast;
2051     goto L420;
2052
2053 /*     Successful completion of all QZ steps */
2054
2055 L380:
2056
2057 /*     Set Eigenvalues 1:ILO-1 */
2058
2059     i__1 = *ilo - 1;
2060     for (j = 1; j <= i__1; ++j) {
2061         if (t[j + j * t_dim1] < 0.f) {
2062             if (ilschr) {
2063                 i__2 = j;
2064                 for (jr = 1; jr <= i__2; ++jr) {
2065                     h__[jr + j * h_dim1] = -h__[jr + j * h_dim1];
2066                     t[jr + j * t_dim1] = -t[jr + j * t_dim1];
2067 /* L390: */
2068                 }
2069             } else {
2070                 h__[j + j * h_dim1] = -h__[j + j * h_dim1];
2071                 t[j + j * t_dim1] = -t[j + j * t_dim1];
2072             }
2073             if (ilz) {
2074                 i__2 = *n;
2075                 for (jr = 1; jr <= i__2; ++jr) {
2076                     z__[jr + j * z_dim1] = -z__[jr + j * z_dim1];
2077 /* L400: */
2078                 }
2079             }
2080         }
2081         alphar[j] = h__[j + j * h_dim1];
2082         alphai[j] = 0.f;
2083         beta[j] = t[j + j * t_dim1];
2084 /* L410: */
2085     }
2086
2087 /*     Normal Termination */
2088
2089     *info = 0;
2090
2091 /*     Exit (other than argument error) -- return optimal workspace size */
2092
2093 L420:
2094     work[1] = (real) (*n);
2095     return 0;
2096
2097 /*     End of SHGEQZ */
2098
2099 } /* shgeqz_ */
2100