C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / dtgex2.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 integer c__4 = 4;
516 static doublereal c_b5 = 0.;
517 static integer c__1 = 1;
518 static integer c__2 = 2;
519 static doublereal c_b42 = 1.;
520 static doublereal c_b48 = -1.;
521 static integer c__0 = 0;
522
523 /* > \brief \b DTGEX2 swaps adjacent diagonal blocks in an upper (quasi) triangular matrix pair by an orthogon
524 al equivalence transformation. */
525
526 /*  =========== DOCUMENTATION =========== */
527
528 /* Online html documentation available at */
529 /*            http://www.netlib.org/lapack/explore-html/ */
530
531 /* > \htmlonly */
532 /* > Download DTGEX2 + dependencies */
533 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dtgex2.
534 f"> */
535 /* > [TGZ]</a> */
536 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dtgex2.
537 f"> */
538 /* > [ZIP]</a> */
539 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dtgex2.
540 f"> */
541 /* > [TXT]</a> */
542 /* > \endhtmlonly */
543
544 /*  Definition: */
545 /*  =========== */
546
547 /*       SUBROUTINE DTGEX2( WANTQ, WANTZ, N, A, LDA, B, LDB, Q, LDQ, Z, */
548 /*                          LDZ, J1, N1, N2, WORK, LWORK, INFO ) */
549
550 /*       LOGICAL            WANTQ, WANTZ */
551 /*       INTEGER            INFO, J1, LDA, LDB, LDQ, LDZ, LWORK, N, N1, N2 */
552 /*       DOUBLE PRECISION   A( LDA, * ), B( LDB, * ), Q( LDQ, * ), */
553 /*      $                   WORK( * ), Z( LDZ, * ) */
554
555
556 /* > \par Purpose: */
557 /*  ============= */
558 /* > */
559 /* > \verbatim */
560 /* > */
561 /* > DTGEX2 swaps adjacent diagonal blocks (A11, B11) and (A22, B22) */
562 /* > of size 1-by-1 or 2-by-2 in an upper (quasi) triangular matrix pair */
563 /* > (A, B) by an orthogonal equivalence transformation. */
564 /* > */
565 /* > (A, B) must be in generalized real Schur canonical form (as returned */
566 /* > by DGGES), i.e. A is block upper triangular with 1-by-1 and 2-by-2 */
567 /* > diagonal blocks. B is upper triangular. */
568 /* > */
569 /* > Optionally, the matrices Q and Z of generalized Schur vectors are */
570 /* > updated. */
571 /* > */
572 /* >        Q(in) * A(in) * Z(in)**T = Q(out) * A(out) * Z(out)**T */
573 /* >        Q(in) * B(in) * Z(in)**T = Q(out) * B(out) * Z(out)**T */
574 /* > */
575 /* > \endverbatim */
576
577 /*  Arguments: */
578 /*  ========== */
579
580 /* > \param[in] WANTQ */
581 /* > \verbatim */
582 /* >          WANTQ is LOGICAL */
583 /* >          .TRUE. : update the left transformation matrix Q; */
584 /* >          .FALSE.: do not update Q. */
585 /* > \endverbatim */
586 /* > */
587 /* > \param[in] WANTZ */
588 /* > \verbatim */
589 /* >          WANTZ is LOGICAL */
590 /* >          .TRUE. : update the right transformation matrix Z; */
591 /* >          .FALSE.: do not update Z. */
592 /* > \endverbatim */
593 /* > */
594 /* > \param[in] N */
595 /* > \verbatim */
596 /* >          N is INTEGER */
597 /* >          The order of the matrices A and B. N >= 0. */
598 /* > \endverbatim */
599 /* > */
600 /* > \param[in,out] A */
601 /* > \verbatim */
602 /* >          A is DOUBLE PRECISION array, dimensions (LDA,N) */
603 /* >          On entry, the matrix A in the pair (A, B). */
604 /* >          On exit, the updated matrix A. */
605 /* > \endverbatim */
606 /* > */
607 /* > \param[in] LDA */
608 /* > \verbatim */
609 /* >          LDA is INTEGER */
610 /* >          The leading dimension of the array A. LDA >= f2cmax(1,N). */
611 /* > \endverbatim */
612 /* > */
613 /* > \param[in,out] B */
614 /* > \verbatim */
615 /* >          B is DOUBLE PRECISION array, dimensions (LDB,N) */
616 /* >          On entry, the matrix B in the pair (A, B). */
617 /* >          On exit, the updated matrix B. */
618 /* > \endverbatim */
619 /* > */
620 /* > \param[in] LDB */
621 /* > \verbatim */
622 /* >          LDB is INTEGER */
623 /* >          The leading dimension of the array B. LDB >= f2cmax(1,N). */
624 /* > \endverbatim */
625 /* > */
626 /* > \param[in,out] Q */
627 /* > \verbatim */
628 /* >          Q is DOUBLE PRECISION array, dimension (LDQ,N) */
629 /* >          On entry, if WANTQ = .TRUE., the orthogonal matrix Q. */
630 /* >          On exit, the updated matrix Q. */
631 /* >          Not referenced if WANTQ = .FALSE.. */
632 /* > \endverbatim */
633 /* > */
634 /* > \param[in] LDQ */
635 /* > \verbatim */
636 /* >          LDQ is INTEGER */
637 /* >          The leading dimension of the array Q. LDQ >= 1. */
638 /* >          If WANTQ = .TRUE., LDQ >= N. */
639 /* > \endverbatim */
640 /* > */
641 /* > \param[in,out] Z */
642 /* > \verbatim */
643 /* >          Z is DOUBLE PRECISION array, dimension (LDZ,N) */
644 /* >          On entry, if WANTZ =.TRUE., the orthogonal matrix Z. */
645 /* >          On exit, the updated matrix Z. */
646 /* >          Not referenced if WANTZ = .FALSE.. */
647 /* > \endverbatim */
648 /* > */
649 /* > \param[in] LDZ */
650 /* > \verbatim */
651 /* >          LDZ is INTEGER */
652 /* >          The leading dimension of the array Z. LDZ >= 1. */
653 /* >          If WANTZ = .TRUE., LDZ >= N. */
654 /* > \endverbatim */
655 /* > */
656 /* > \param[in] J1 */
657 /* > \verbatim */
658 /* >          J1 is INTEGER */
659 /* >          The index to the first block (A11, B11). 1 <= J1 <= N. */
660 /* > \endverbatim */
661 /* > */
662 /* > \param[in] N1 */
663 /* > \verbatim */
664 /* >          N1 is INTEGER */
665 /* >          The order of the first block (A11, B11). N1 = 0, 1 or 2. */
666 /* > \endverbatim */
667 /* > */
668 /* > \param[in] N2 */
669 /* > \verbatim */
670 /* >          N2 is INTEGER */
671 /* >          The order of the second block (A22, B22). N2 = 0, 1 or 2. */
672 /* > \endverbatim */
673 /* > */
674 /* > \param[out] WORK */
675 /* > \verbatim */
676 /* >          WORK is DOUBLE PRECISION array, dimension (MAX(1,LWORK)). */
677 /* > \endverbatim */
678 /* > */
679 /* > \param[in] LWORK */
680 /* > \verbatim */
681 /* >          LWORK is INTEGER */
682 /* >          The dimension of the array WORK. */
683 /* >          LWORK >=  MAX( 1, N*(N2+N1), (N2+N1)*(N2+N1)*2 ) */
684 /* > \endverbatim */
685 /* > */
686 /* > \param[out] INFO */
687 /* > \verbatim */
688 /* >          INFO is INTEGER */
689 /* >            =0: Successful exit */
690 /* >            >0: If INFO = 1, the transformed matrix (A, B) would be */
691 /* >                too far from generalized Schur form; the blocks are */
692 /* >                not swapped and (A, B) and (Q, Z) are unchanged. */
693 /* >                The problem of swapping is too ill-conditioned. */
694 /* >            <0: If INFO = -16: LWORK is too small. Appropriate value */
695 /* >                for LWORK is returned in WORK(1). */
696 /* > \endverbatim */
697
698 /*  Authors: */
699 /*  ======== */
700
701 /* > \author Univ. of Tennessee */
702 /* > \author Univ. of California Berkeley */
703 /* > \author Univ. of Colorado Denver */
704 /* > \author NAG Ltd. */
705
706 /* > \date December 2016 */
707
708 /* > \ingroup doubleGEauxiliary */
709
710 /* > \par Further Details: */
711 /*  ===================== */
712 /* > */
713 /* >  In the current code both weak and strong stability tests are */
714 /* >  performed. The user can omit the strong stability test by changing */
715 /* >  the internal logical parameter WANDS to .FALSE.. See ref. [2] for */
716 /* >  details. */
717
718 /* > \par Contributors: */
719 /*  ================== */
720 /* > */
721 /* >     Bo Kagstrom and Peter Poromaa, Department of Computing Science, */
722 /* >     Umea University, S-901 87 Umea, Sweden. */
723
724 /* > \par References: */
725 /*  ================ */
726 /* > */
727 /* > \verbatim */
728 /* > */
729 /* >  [1] B. Kagstrom; A Direct Method for Reordering Eigenvalues in the */
730 /* >      Generalized Real Schur Form of a Regular Matrix Pair (A, B), in */
731 /* >      M.S. Moonen et al (eds), Linear Algebra for Large Scale and */
732 /* >      Real-Time Applications, Kluwer Academic Publ. 1993, pp 195-218. */
733 /* > */
734 /* >  [2] B. Kagstrom and P. Poromaa; Computing Eigenspaces with Specified */
735 /* >      Eigenvalues of a Regular Matrix Pair (A, B) and Condition */
736 /* >      Estimation: Theory, Algorithms and Software, */
737 /* >      Report UMINF - 94.04, Department of Computing Science, Umea */
738 /* >      University, S-901 87 Umea, Sweden, 1994. Also as LAPACK Working */
739 /* >      Note 87. To appear in Numerical Algorithms, 1996. */
740 /* > \endverbatim */
741 /* > */
742 /*  ===================================================================== */
743 /* Subroutine */ int dtgex2_(logical *wantq, logical *wantz, integer *n, 
744         doublereal *a, integer *lda, doublereal *b, integer *ldb, doublereal *
745         q, integer *ldq, doublereal *z__, integer *ldz, integer *j1, integer *
746         n1, integer *n2, doublereal *work, integer *lwork, integer *info)
747 {
748     /* System generated locals */
749     integer a_dim1, a_offset, b_dim1, b_offset, q_dim1, q_offset, z_dim1, 
750             z_offset, i__1, i__2;
751     doublereal d__1;
752
753     /* Local variables */
754     logical weak;
755     doublereal ddum;
756     integer idum;
757     doublereal taul[4], dsum;
758     extern /* Subroutine */ int drot_(integer *, doublereal *, integer *, 
759             doublereal *, integer *, doublereal *, doublereal *);
760     doublereal taur[4], scpy[16]        /* was [4][4] */, tcpy[16]      /* 
761             was [4][4] */, f, g;
762     integer i__, m;
763     doublereal s[16]    /* was [4][4] */, t[16] /* was [4][4] */;
764     extern /* Subroutine */ int dscal_(integer *, doublereal *, doublereal *, 
765             integer *);
766     doublereal scale, bqra21, brqa21;
767     extern /* Subroutine */ int dgemm_(char *, char *, integer *, integer *, 
768             integer *, doublereal *, doublereal *, integer *, doublereal *, 
769             integer *, doublereal *, doublereal *, integer *);
770     doublereal licop[16]        /* was [4][4] */;
771     integer linfo;
772     doublereal ircop[16]        /* was [4][4] */, dnorm;
773     integer iwork[4];
774     extern /* Subroutine */ int dlagv2_(doublereal *, integer *, doublereal *,
775              integer *, doublereal *, doublereal *, doublereal *, doublereal *
776             , doublereal *, doublereal *, doublereal *), dgeqr2_(integer *, 
777             integer *, doublereal *, integer *, doublereal *, doublereal *, 
778             integer *), dgerq2_(integer *, integer *, doublereal *, integer *,
779              doublereal *, doublereal *, integer *), dorg2r_(integer *, 
780             integer *, integer *, doublereal *, integer *, doublereal *, 
781             doublereal *, integer *), dorgr2_(integer *, integer *, integer *,
782              doublereal *, integer *, doublereal *, doublereal *, integer *), 
783             dorm2r_(char *, char *, integer *, integer *, integer *, 
784             doublereal *, integer *, doublereal *, doublereal *, integer *, 
785             doublereal *, integer *), dormr2_(char *, char *, 
786             integer *, integer *, integer *, doublereal *, integer *, 
787             doublereal *, doublereal *, integer *, doublereal *, integer *);
788     doublereal be[2], ai[2];
789     extern /* Subroutine */ int dtgsy2_(char *, integer *, integer *, integer 
790             *, doublereal *, integer *, doublereal *, integer *, doublereal *,
791              integer *, doublereal *, integer *, doublereal *, integer *, 
792             doublereal *, integer *, doublereal *, doublereal *, doublereal *,
793              integer *, integer *, integer *);
794     doublereal ar[2], sa, sb, li[16]    /* was [4][4] */;
795     extern doublereal dlamch_(char *);
796     doublereal dscale, ir[16]   /* was [4][4] */, ss, ws;
797     extern /* Subroutine */ int dlacpy_(char *, integer *, integer *, 
798             doublereal *, integer *, doublereal *, integer *), 
799             dlartg_(doublereal *, doublereal *, doublereal *, doublereal *, 
800             doublereal *), dlaset_(char *, integer *, integer *, doublereal *,
801              doublereal *, doublereal *, integer *), dlassq_(integer *
802             , doublereal *, integer *, doublereal *, doublereal *);
803     logical dtrong;
804     doublereal thresh, smlnum, eps;
805
806
807 /*  -- LAPACK auxiliary routine (version 3.7.0) -- */
808 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
809 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
810 /*     December 2016 */
811
812
813 /*  ===================================================================== */
814 /*  Replaced various illegal calls to DCOPY by calls to DLASET, or by DO */
815 /*  loops. Sven Hammarling, 1/5/02. */
816
817
818     /* Parameter adjustments */
819     a_dim1 = *lda;
820     a_offset = 1 + a_dim1 * 1;
821     a -= a_offset;
822     b_dim1 = *ldb;
823     b_offset = 1 + b_dim1 * 1;
824     b -= b_offset;
825     q_dim1 = *ldq;
826     q_offset = 1 + q_dim1 * 1;
827     q -= q_offset;
828     z_dim1 = *ldz;
829     z_offset = 1 + z_dim1 * 1;
830     z__ -= z_offset;
831     --work;
832
833     /* Function Body */
834     *info = 0;
835
836 /*     Quick return if possible */
837
838     if (*n <= 1 || *n1 <= 0 || *n2 <= 0) {
839         return 0;
840     }
841     if (*n1 > *n || *j1 + *n1 > *n) {
842         return 0;
843     }
844     m = *n1 + *n2;
845 /* Computing MAX */
846     i__1 = 1, i__2 = *n * m, i__1 = f2cmax(i__1,i__2), i__2 = m * m << 1;
847     if (*lwork < f2cmax(i__1,i__2)) {
848         *info = -16;
849 /* Computing MAX */
850         i__1 = 1, i__2 = *n * m, i__1 = f2cmax(i__1,i__2), i__2 = m * m << 1;
851         work[1] = (doublereal) f2cmax(i__1,i__2);
852         return 0;
853     }
854
855     weak = FALSE_;
856     dtrong = FALSE_;
857
858 /*     Make a local copy of selected block */
859
860     dlaset_("Full", &c__4, &c__4, &c_b5, &c_b5, li, &c__4);
861     dlaset_("Full", &c__4, &c__4, &c_b5, &c_b5, ir, &c__4);
862     dlacpy_("Full", &m, &m, &a[*j1 + *j1 * a_dim1], lda, s, &c__4);
863     dlacpy_("Full", &m, &m, &b[*j1 + *j1 * b_dim1], ldb, t, &c__4);
864
865 /*     Compute threshold for testing acceptance of swapping. */
866
867     eps = dlamch_("P");
868     smlnum = dlamch_("S") / eps;
869     dscale = 0.;
870     dsum = 1.;
871     dlacpy_("Full", &m, &m, s, &c__4, &work[1], &m);
872     i__1 = m * m;
873     dlassq_(&i__1, &work[1], &c__1, &dscale, &dsum);
874     dlacpy_("Full", &m, &m, t, &c__4, &work[1], &m);
875     i__1 = m * m;
876     dlassq_(&i__1, &work[1], &c__1, &dscale, &dsum);
877     dnorm = dscale * sqrt(dsum);
878
879 /*     THRES has been changed from */
880 /*        THRESH = MAX( TEN*EPS*SA, SMLNUM ) */
881 /*     to */
882 /*        THRESH = MAX( TWENTY*EPS*SA, SMLNUM ) */
883 /*     on 04/01/10. */
884 /*     "Bug" reported by Ondra Kamenik, confirmed by Julie Langou, fixed by */
885 /*     Jim Demmel and Guillaume Revy. See forum post 1783. */
886
887 /* Computing MAX */
888     d__1 = eps * 20. * dnorm;
889     thresh = f2cmax(d__1,smlnum);
890
891     if (m == 2) {
892
893 /*        CASE 1: Swap 1-by-1 and 1-by-1 blocks. */
894
895 /*        Compute orthogonal QL and RQ that swap 1-by-1 and 1-by-1 blocks */
896 /*        using Givens rotations and perform the swap tentatively. */
897
898         f = s[5] * t[0] - t[5] * s[0];
899         g = s[5] * t[4] - t[5] * s[4];
900         sb = abs(t[5]);
901         sa = abs(s[5]);
902         dlartg_(&f, &g, &ir[4], ir, &ddum);
903         ir[1] = -ir[4];
904         ir[5] = ir[0];
905         drot_(&c__2, s, &c__1, &s[4], &c__1, ir, &ir[1]);
906         drot_(&c__2, t, &c__1, &t[4], &c__1, ir, &ir[1]);
907         if (sa >= sb) {
908             dlartg_(s, &s[1], li, &li[1], &ddum);
909         } else {
910             dlartg_(t, &t[1], li, &li[1], &ddum);
911         }
912         drot_(&c__2, s, &c__4, &s[1], &c__4, li, &li[1]);
913         drot_(&c__2, t, &c__4, &t[1], &c__4, li, &li[1]);
914         li[5] = li[0];
915         li[4] = -li[1];
916
917 /*        Weak stability test: */
918 /*           |S21| + |T21| <= O(EPS * F-norm((S, T))) */
919
920         ws = abs(s[1]) + abs(t[1]);
921         weak = ws <= thresh;
922         if (! weak) {
923             goto L70;
924         }
925
926         if (TRUE_) {
927
928 /*           Strong stability test: */
929 /*             F-norm((A-QL**T*S*QR, B-QL**T*T*QR)) <= O(EPS*F-norm((A,B))) */
930
931             dlacpy_("Full", &m, &m, &a[*j1 + *j1 * a_dim1], lda, &work[m * m 
932                     + 1], &m);
933             dgemm_("N", "N", &m, &m, &m, &c_b42, li, &c__4, s, &c__4, &c_b5, &
934                     work[1], &m);
935             dgemm_("N", "T", &m, &m, &m, &c_b48, &work[1], &m, ir, &c__4, &
936                     c_b42, &work[m * m + 1], &m);
937             dscale = 0.;
938             dsum = 1.;
939             i__1 = m * m;
940             dlassq_(&i__1, &work[m * m + 1], &c__1, &dscale, &dsum);
941
942             dlacpy_("Full", &m, &m, &b[*j1 + *j1 * b_dim1], ldb, &work[m * m 
943                     + 1], &m);
944             dgemm_("N", "N", &m, &m, &m, &c_b42, li, &c__4, t, &c__4, &c_b5, &
945                     work[1], &m);
946             dgemm_("N", "T", &m, &m, &m, &c_b48, &work[1], &m, ir, &c__4, &
947                     c_b42, &work[m * m + 1], &m);
948             i__1 = m * m;
949             dlassq_(&i__1, &work[m * m + 1], &c__1, &dscale, &dsum);
950             ss = dscale * sqrt(dsum);
951             dtrong = ss <= thresh;
952             if (! dtrong) {
953                 goto L70;
954             }
955         }
956
957 /*        Update (A(J1:J1+M-1, M+J1:N), B(J1:J1+M-1, M+J1:N)) and */
958 /*               (A(1:J1-1, J1:J1+M), B(1:J1-1, J1:J1+M)). */
959
960         i__1 = *j1 + 1;
961         drot_(&i__1, &a[*j1 * a_dim1 + 1], &c__1, &a[(*j1 + 1) * a_dim1 + 1], 
962                 &c__1, ir, &ir[1]);
963         i__1 = *j1 + 1;
964         drot_(&i__1, &b[*j1 * b_dim1 + 1], &c__1, &b[(*j1 + 1) * b_dim1 + 1], 
965                 &c__1, ir, &ir[1]);
966         i__1 = *n - *j1 + 1;
967         drot_(&i__1, &a[*j1 + *j1 * a_dim1], lda, &a[*j1 + 1 + *j1 * a_dim1], 
968                 lda, li, &li[1]);
969         i__1 = *n - *j1 + 1;
970         drot_(&i__1, &b[*j1 + *j1 * b_dim1], ldb, &b[*j1 + 1 + *j1 * b_dim1], 
971                 ldb, li, &li[1]);
972
973 /*        Set  N1-by-N2 (2,1) - blocks to ZERO. */
974
975         a[*j1 + 1 + *j1 * a_dim1] = 0.;
976         b[*j1 + 1 + *j1 * b_dim1] = 0.;
977
978 /*        Accumulate transformations into Q and Z if requested. */
979
980         if (*wantz) {
981             drot_(n, &z__[*j1 * z_dim1 + 1], &c__1, &z__[(*j1 + 1) * z_dim1 + 
982                     1], &c__1, ir, &ir[1]);
983         }
984         if (*wantq) {
985             drot_(n, &q[*j1 * q_dim1 + 1], &c__1, &q[(*j1 + 1) * q_dim1 + 1], 
986                     &c__1, li, &li[1]);
987         }
988
989 /*        Exit with INFO = 0 if swap was successfully performed. */
990
991         return 0;
992
993     } else {
994
995 /*        CASE 2: Swap 1-by-1 and 2-by-2 blocks, or 2-by-2 */
996 /*                and 2-by-2 blocks. */
997
998 /*        Solve the generalized Sylvester equation */
999 /*                 S11 * R - L * S22 = SCALE * S12 */
1000 /*                 T11 * R - L * T22 = SCALE * T12 */
1001 /*        for R and L. Solutions in LI and IR. */
1002
1003         dlacpy_("Full", n1, n2, &t[(*n1 + 1 << 2) - 4], &c__4, li, &c__4);
1004         dlacpy_("Full", n1, n2, &s[(*n1 + 1 << 2) - 4], &c__4, &ir[*n2 + 1 + (
1005                 *n1 + 1 << 2) - 5], &c__4);
1006         dtgsy2_("N", &c__0, n1, n2, s, &c__4, &s[*n1 + 1 + (*n1 + 1 << 2) - 5]
1007                 , &c__4, &ir[*n2 + 1 + (*n1 + 1 << 2) - 5], &c__4, t, &c__4, &
1008                 t[*n1 + 1 + (*n1 + 1 << 2) - 5], &c__4, li, &c__4, &scale, &
1009                 dsum, &dscale, iwork, &idum, &linfo);
1010
1011 /*        Compute orthogonal matrix QL: */
1012
1013 /*                    QL**T * LI = [ TL ] */
1014 /*                                 [ 0  ] */
1015 /*        where */
1016 /*                    LI =  [      -L              ] */
1017 /*                          [ SCALE * identity(N2) ] */
1018
1019         i__1 = *n2;
1020         for (i__ = 1; i__ <= i__1; ++i__) {
1021             dscal_(n1, &c_b48, &li[(i__ << 2) - 4], &c__1);
1022             li[*n1 + i__ + (i__ << 2) - 5] = scale;
1023 /* L10: */
1024         }
1025         dgeqr2_(&m, n2, li, &c__4, taul, &work[1], &linfo);
1026         if (linfo != 0) {
1027             goto L70;
1028         }
1029         dorg2r_(&m, &m, n2, li, &c__4, taul, &work[1], &linfo);
1030         if (linfo != 0) {
1031             goto L70;
1032         }
1033
1034 /*        Compute orthogonal matrix RQ: */
1035
1036 /*                    IR * RQ**T =   [ 0  TR], */
1037
1038 /*         where IR = [ SCALE * identity(N1), R ] */
1039
1040         i__1 = *n1;
1041         for (i__ = 1; i__ <= i__1; ++i__) {
1042             ir[*n2 + i__ + (i__ << 2) - 5] = scale;
1043 /* L20: */
1044         }
1045         dgerq2_(n1, &m, &ir[*n2], &c__4, taur, &work[1], &linfo);
1046         if (linfo != 0) {
1047             goto L70;
1048         }
1049         dorgr2_(&m, &m, n1, ir, &c__4, taur, &work[1], &linfo);
1050         if (linfo != 0) {
1051             goto L70;
1052         }
1053
1054 /*        Perform the swapping tentatively: */
1055
1056         dgemm_("T", "N", &m, &m, &m, &c_b42, li, &c__4, s, &c__4, &c_b5, &
1057                 work[1], &m);
1058         dgemm_("N", "T", &m, &m, &m, &c_b42, &work[1], &m, ir, &c__4, &c_b5, 
1059                 s, &c__4);
1060         dgemm_("T", "N", &m, &m, &m, &c_b42, li, &c__4, t, &c__4, &c_b5, &
1061                 work[1], &m);
1062         dgemm_("N", "T", &m, &m, &m, &c_b42, &work[1], &m, ir, &c__4, &c_b5, 
1063                 t, &c__4);
1064         dlacpy_("F", &m, &m, s, &c__4, scpy, &c__4);
1065         dlacpy_("F", &m, &m, t, &c__4, tcpy, &c__4);
1066         dlacpy_("F", &m, &m, ir, &c__4, ircop, &c__4);
1067         dlacpy_("F", &m, &m, li, &c__4, licop, &c__4);
1068
1069 /*        Triangularize the B-part by an RQ factorization. */
1070 /*        Apply transformation (from left) to A-part, giving S. */
1071
1072         dgerq2_(&m, &m, t, &c__4, taur, &work[1], &linfo);
1073         if (linfo != 0) {
1074             goto L70;
1075         }
1076         dormr2_("R", "T", &m, &m, &m, t, &c__4, taur, s, &c__4, &work[1], &
1077                 linfo);
1078         if (linfo != 0) {
1079             goto L70;
1080         }
1081         dormr2_("L", "N", &m, &m, &m, t, &c__4, taur, ir, &c__4, &work[1], &
1082                 linfo);
1083         if (linfo != 0) {
1084             goto L70;
1085         }
1086
1087 /*        Compute F-norm(S21) in BRQA21. (T21 is 0.) */
1088
1089         dscale = 0.;
1090         dsum = 1.;
1091         i__1 = *n2;
1092         for (i__ = 1; i__ <= i__1; ++i__) {
1093             dlassq_(n1, &s[*n2 + 1 + (i__ << 2) - 5], &c__1, &dscale, &dsum);
1094 /* L30: */
1095         }
1096         brqa21 = dscale * sqrt(dsum);
1097
1098 /*        Triangularize the B-part by a QR factorization. */
1099 /*        Apply transformation (from right) to A-part, giving S. */
1100
1101         dgeqr2_(&m, &m, tcpy, &c__4, taul, &work[1], &linfo);
1102         if (linfo != 0) {
1103             goto L70;
1104         }
1105         dorm2r_("L", "T", &m, &m, &m, tcpy, &c__4, taul, scpy, &c__4, &work[1]
1106                 , info);
1107         dorm2r_("R", "N", &m, &m, &m, tcpy, &c__4, taul, licop, &c__4, &work[
1108                 1], info);
1109         if (linfo != 0) {
1110             goto L70;
1111         }
1112
1113 /*        Compute F-norm(S21) in BQRA21. (T21 is 0.) */
1114
1115         dscale = 0.;
1116         dsum = 1.;
1117         i__1 = *n2;
1118         for (i__ = 1; i__ <= i__1; ++i__) {
1119             dlassq_(n1, &scpy[*n2 + 1 + (i__ << 2) - 5], &c__1, &dscale, &
1120                     dsum);
1121 /* L40: */
1122         }
1123         bqra21 = dscale * sqrt(dsum);
1124
1125 /*        Decide which method to use. */
1126 /*          Weak stability test: */
1127 /*             F-norm(S21) <= O(EPS * F-norm((S, T))) */
1128
1129         if (bqra21 <= brqa21 && bqra21 <= thresh) {
1130             dlacpy_("F", &m, &m, scpy, &c__4, s, &c__4);
1131             dlacpy_("F", &m, &m, tcpy, &c__4, t, &c__4);
1132             dlacpy_("F", &m, &m, ircop, &c__4, ir, &c__4);
1133             dlacpy_("F", &m, &m, licop, &c__4, li, &c__4);
1134         } else if (brqa21 >= thresh) {
1135             goto L70;
1136         }
1137
1138 /*        Set lower triangle of B-part to zero */
1139
1140         i__1 = m - 1;
1141         i__2 = m - 1;
1142         dlaset_("Lower", &i__1, &i__2, &c_b5, &c_b5, &t[1], &c__4);
1143
1144         if (TRUE_) {
1145
1146 /*           Strong stability test: */
1147 /*              F-norm((A-QL*S*QR**T, B-QL*T*QR**T)) <= O(EPS*F-norm((A,B))) */
1148
1149             dlacpy_("Full", &m, &m, &a[*j1 + *j1 * a_dim1], lda, &work[m * m 
1150                     + 1], &m);
1151             dgemm_("N", "N", &m, &m, &m, &c_b42, li, &c__4, s, &c__4, &c_b5, &
1152                     work[1], &m);
1153             dgemm_("N", "N", &m, &m, &m, &c_b48, &work[1], &m, ir, &c__4, &
1154                     c_b42, &work[m * m + 1], &m);
1155             dscale = 0.;
1156             dsum = 1.;
1157             i__1 = m * m;
1158             dlassq_(&i__1, &work[m * m + 1], &c__1, &dscale, &dsum);
1159
1160             dlacpy_("Full", &m, &m, &b[*j1 + *j1 * b_dim1], ldb, &work[m * m 
1161                     + 1], &m);
1162             dgemm_("N", "N", &m, &m, &m, &c_b42, li, &c__4, t, &c__4, &c_b5, &
1163                     work[1], &m);
1164             dgemm_("N", "N", &m, &m, &m, &c_b48, &work[1], &m, ir, &c__4, &
1165                     c_b42, &work[m * m + 1], &m);
1166             i__1 = m * m;
1167             dlassq_(&i__1, &work[m * m + 1], &c__1, &dscale, &dsum);
1168             ss = dscale * sqrt(dsum);
1169             dtrong = ss <= thresh;
1170             if (! dtrong) {
1171                 goto L70;
1172             }
1173
1174         }
1175
1176 /*        If the swap is accepted ("weakly" and "strongly"), apply the */
1177 /*        transformations and set N1-by-N2 (2,1)-block to zero. */
1178
1179         dlaset_("Full", n1, n2, &c_b5, &c_b5, &s[*n2], &c__4);
1180
1181 /*        copy back M-by-M diagonal block starting at index J1 of (A, B) */
1182
1183         dlacpy_("F", &m, &m, s, &c__4, &a[*j1 + *j1 * a_dim1], lda)
1184                 ;
1185         dlacpy_("F", &m, &m, t, &c__4, &b[*j1 + *j1 * b_dim1], ldb)
1186                 ;
1187         dlaset_("Full", &c__4, &c__4, &c_b5, &c_b5, t, &c__4);
1188
1189 /*        Standardize existing 2-by-2 blocks. */
1190
1191         dlaset_("Full", &m, &m, &c_b5, &c_b5, &work[1], &m);
1192         work[1] = 1.;
1193         t[0] = 1.;
1194         idum = *lwork - m * m - 2;
1195         if (*n2 > 1) {
1196             dlagv2_(&a[*j1 + *j1 * a_dim1], lda, &b[*j1 + *j1 * b_dim1], ldb, 
1197                     ar, ai, be, &work[1], &work[2], t, &t[1]);
1198             work[m + 1] = -work[2];
1199             work[m + 2] = work[1];
1200             t[*n2 + (*n2 << 2) - 5] = t[0];
1201             t[4] = -t[1];
1202         }
1203         work[m * m] = 1.;
1204         t[m + (m << 2) - 5] = 1.;
1205
1206         if (*n1 > 1) {
1207             dlagv2_(&a[*j1 + *n2 + (*j1 + *n2) * a_dim1], lda, &b[*j1 + *n2 + 
1208                     (*j1 + *n2) * b_dim1], ldb, taur, taul, &work[m * m + 1], 
1209                     &work[*n2 * m + *n2 + 1], &work[*n2 * m + *n2 + 2], &t[*
1210                     n2 + 1 + (*n2 + 1 << 2) - 5], &t[m + (m - 1 << 2) - 5]);
1211             work[m * m] = work[*n2 * m + *n2 + 1];
1212             work[m * m - 1] = -work[*n2 * m + *n2 + 2];
1213             t[m + (m << 2) - 5] = t[*n2 + 1 + (*n2 + 1 << 2) - 5];
1214             t[m - 1 + (m << 2) - 5] = -t[m + (m - 1 << 2) - 5];
1215         }
1216         dgemm_("T", "N", n2, n1, n2, &c_b42, &work[1], &m, &a[*j1 + (*j1 + *
1217                 n2) * a_dim1], lda, &c_b5, &work[m * m + 1], n2);
1218         dlacpy_("Full", n2, n1, &work[m * m + 1], n2, &a[*j1 + (*j1 + *n2) * 
1219                 a_dim1], lda);
1220         dgemm_("T", "N", n2, n1, n2, &c_b42, &work[1], &m, &b[*j1 + (*j1 + *
1221                 n2) * b_dim1], ldb, &c_b5, &work[m * m + 1], n2);
1222         dlacpy_("Full", n2, n1, &work[m * m + 1], n2, &b[*j1 + (*j1 + *n2) * 
1223                 b_dim1], ldb);
1224         dgemm_("N", "N", &m, &m, &m, &c_b42, li, &c__4, &work[1], &m, &c_b5, &
1225                 work[m * m + 1], &m);
1226         dlacpy_("Full", &m, &m, &work[m * m + 1], &m, li, &c__4);
1227         dgemm_("N", "N", n2, n1, n1, &c_b42, &a[*j1 + (*j1 + *n2) * a_dim1], 
1228                 lda, &t[*n2 + 1 + (*n2 + 1 << 2) - 5], &c__4, &c_b5, &work[1],
1229                  n2);
1230         dlacpy_("Full", n2, n1, &work[1], n2, &a[*j1 + (*j1 + *n2) * a_dim1], 
1231                 lda);
1232         dgemm_("N", "N", n2, n1, n1, &c_b42, &b[*j1 + (*j1 + *n2) * b_dim1], 
1233                 ldb, &t[*n2 + 1 + (*n2 + 1 << 2) - 5], &c__4, &c_b5, &work[1],
1234                  n2);
1235         dlacpy_("Full", n2, n1, &work[1], n2, &b[*j1 + (*j1 + *n2) * b_dim1], 
1236                 ldb);
1237         dgemm_("T", "N", &m, &m, &m, &c_b42, ir, &c__4, t, &c__4, &c_b5, &
1238                 work[1], &m);
1239         dlacpy_("Full", &m, &m, &work[1], &m, ir, &c__4);
1240
1241 /*        Accumulate transformations into Q and Z if requested. */
1242
1243         if (*wantq) {
1244             dgemm_("N", "N", n, &m, &m, &c_b42, &q[*j1 * q_dim1 + 1], ldq, li,
1245                      &c__4, &c_b5, &work[1], n);
1246             dlacpy_("Full", n, &m, &work[1], n, &q[*j1 * q_dim1 + 1], ldq);
1247
1248         }
1249
1250         if (*wantz) {
1251             dgemm_("N", "N", n, &m, &m, &c_b42, &z__[*j1 * z_dim1 + 1], ldz, 
1252                     ir, &c__4, &c_b5, &work[1], n);
1253             dlacpy_("Full", n, &m, &work[1], n, &z__[*j1 * z_dim1 + 1], ldz);
1254
1255         }
1256
1257 /*        Update (A(J1:J1+M-1, M+J1:N), B(J1:J1+M-1, M+J1:N)) and */
1258 /*                (A(1:J1-1, J1:J1+M), B(1:J1-1, J1:J1+M)). */
1259
1260         i__ = *j1 + m;
1261         if (i__ <= *n) {
1262             i__1 = *n - i__ + 1;
1263             dgemm_("T", "N", &m, &i__1, &m, &c_b42, li, &c__4, &a[*j1 + i__ * 
1264                     a_dim1], lda, &c_b5, &work[1], &m);
1265             i__1 = *n - i__ + 1;
1266             dlacpy_("Full", &m, &i__1, &work[1], &m, &a[*j1 + i__ * a_dim1], 
1267                     lda);
1268             i__1 = *n - i__ + 1;
1269             dgemm_("T", "N", &m, &i__1, &m, &c_b42, li, &c__4, &b[*j1 + i__ * 
1270                     b_dim1], ldb, &c_b5, &work[1], &m);
1271             i__1 = *n - i__ + 1;
1272             dlacpy_("Full", &m, &i__1, &work[1], &m, &b[*j1 + i__ * b_dim1], 
1273                     ldb);
1274         }
1275         i__ = *j1 - 1;
1276         if (i__ > 0) {
1277             dgemm_("N", "N", &i__, &m, &m, &c_b42, &a[*j1 * a_dim1 + 1], lda, 
1278                     ir, &c__4, &c_b5, &work[1], &i__);
1279             dlacpy_("Full", &i__, &m, &work[1], &i__, &a[*j1 * a_dim1 + 1], 
1280                     lda);
1281             dgemm_("N", "N", &i__, &m, &m, &c_b42, &b[*j1 * b_dim1 + 1], ldb, 
1282                     ir, &c__4, &c_b5, &work[1], &i__);
1283             dlacpy_("Full", &i__, &m, &work[1], &i__, &b[*j1 * b_dim1 + 1], 
1284                     ldb);
1285         }
1286
1287 /*        Exit with INFO = 0 if swap was successfully performed. */
1288
1289         return 0;
1290
1291     }
1292
1293 /*     Exit with INFO = 1 if swap was rejected. */
1294
1295 L70:
1296
1297     *info = 1;
1298     return 0;
1299
1300 /*     End of DTGEX2 */
1301
1302 } /* dtgex2_ */
1303