C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / dgghd3.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__1 = 1;
516 static integer c_n1 = -1;
517 static doublereal c_b14 = 0.;
518 static doublereal c_b15 = 1.;
519 static integer c__2 = 2;
520 static integer c__3 = 3;
521 static integer c__16 = 16;
522
523 /* > \brief \b DGGHD3 */
524
525 /*  =========== DOCUMENTATION =========== */
526
527 /* Online html documentation available at */
528 /*            http://www.netlib.org/lapack/explore-html/ */
529
530 /* > \htmlonly */
531 /* > Download DGGHD3 + dependencies */
532 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dgghd3.
533 f"> */
534 /* > [TGZ]</a> */
535 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dgghd3.
536 f"> */
537 /* > [ZIP]</a> */
538 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dgghd3.
539 f"> */
540 /* > [TXT]</a> */
541 /* > \endhtmlonly */
542
543 /*  Definition: */
544 /*  =========== */
545
546 /*       SUBROUTINE DGGHD3( COMPQ, COMPZ, N, ILO, IHI, A, LDA, B, LDB, Q, */
547 /*                          LDQ, Z, LDZ, WORK, LWORK, INFO ) */
548
549 /*       CHARACTER          COMPQ, COMPZ */
550 /*       INTEGER            IHI, ILO, INFO, LDA, LDB, LDQ, LDZ, N, LWORK */
551 /*       DOUBLE PRECISION   A( LDA, * ), B( LDB, * ), Q( LDQ, * ), */
552 /*      $                   Z( LDZ, * ), WORK( * ) */
553
554
555 /* > \par Purpose: */
556 /*  ============= */
557 /* > */
558 /* > \verbatim */
559 /* > */
560 /* > DGGHD3 reduces a pair of real matrices (A,B) to generalized upper */
561 /* > Hessenberg form using orthogonal transformations, where A is a */
562 /* > general matrix and B is upper triangular.  The form of the */
563 /* > generalized eigenvalue problem is */
564 /* >    A*x = lambda*B*x, */
565 /* > and B is typically made upper triangular by computing its QR */
566 /* > factorization and moving the orthogonal matrix Q to the left side */
567 /* > of the equation. */
568 /* > */
569 /* > This subroutine simultaneously reduces A to a Hessenberg matrix H: */
570 /* >    Q**T*A*Z = H */
571 /* > and transforms B to another upper triangular matrix T: */
572 /* >    Q**T*B*Z = T */
573 /* > in order to reduce the problem to its standard form */
574 /* >    H*y = lambda*T*y */
575 /* > where y = Z**T*x. */
576 /* > */
577 /* > The orthogonal matrices Q and Z are determined as products of Givens */
578 /* > rotations.  They may either be formed explicitly, or they may be */
579 /* > postmultiplied into input matrices Q1 and Z1, so that */
580 /* > */
581 /* >      Q1 * A * Z1**T = (Q1*Q) * H * (Z1*Z)**T */
582 /* > */
583 /* >      Q1 * B * Z1**T = (Q1*Q) * T * (Z1*Z)**T */
584 /* > */
585 /* > If Q1 is the orthogonal matrix from the QR factorization of B in the */
586 /* > original equation A*x = lambda*B*x, then DGGHD3 reduces the original */
587 /* > problem to generalized Hessenberg form. */
588 /* > */
589 /* > This is a blocked variant of DGGHRD, using matrix-matrix */
590 /* > multiplications for parts of the computation to enhance performance. */
591 /* > \endverbatim */
592
593 /*  Arguments: */
594 /*  ========== */
595
596 /* > \param[in] COMPQ */
597 /* > \verbatim */
598 /* >          COMPQ is CHARACTER*1 */
599 /* >          = 'N': do not compute Q; */
600 /* >          = 'I': Q is initialized to the unit matrix, and the */
601 /* >                 orthogonal matrix Q is returned; */
602 /* >          = 'V': Q must contain an orthogonal matrix Q1 on entry, */
603 /* >                 and the product Q1*Q is returned. */
604 /* > \endverbatim */
605 /* > */
606 /* > \param[in] COMPZ */
607 /* > \verbatim */
608 /* >          COMPZ is CHARACTER*1 */
609 /* >          = 'N': do not compute Z; */
610 /* >          = 'I': Z is initialized to the unit matrix, and the */
611 /* >                 orthogonal matrix Z is returned; */
612 /* >          = 'V': Z must contain an orthogonal matrix Z1 on entry, */
613 /* >                 and the product Z1*Z is returned. */
614 /* > \endverbatim */
615 /* > */
616 /* > \param[in] N */
617 /* > \verbatim */
618 /* >          N is INTEGER */
619 /* >          The order of the matrices A and B.  N >= 0. */
620 /* > \endverbatim */
621 /* > */
622 /* > \param[in] ILO */
623 /* > \verbatim */
624 /* >          ILO is INTEGER */
625 /* > \endverbatim */
626 /* > */
627 /* > \param[in] IHI */
628 /* > \verbatim */
629 /* >          IHI is INTEGER */
630 /* > */
631 /* >          ILO and IHI mark the rows and columns of A which are to be */
632 /* >          reduced.  It is assumed that A is already upper triangular */
633 /* >          in rows and columns 1:ILO-1 and IHI+1:N.  ILO and IHI are */
634 /* >          normally set by a previous call to DGGBAL; otherwise they */
635 /* >          should be set to 1 and N respectively. */
636 /* >          1 <= ILO <= IHI <= N, if N > 0; ILO=1 and IHI=0, if N=0. */
637 /* > \endverbatim */
638 /* > */
639 /* > \param[in,out] A */
640 /* > \verbatim */
641 /* >          A is DOUBLE PRECISION array, dimension (LDA, N) */
642 /* >          On entry, the N-by-N general matrix to be reduced. */
643 /* >          On exit, the upper triangle and the first subdiagonal of A */
644 /* >          are overwritten with the upper Hessenberg matrix H, and the */
645 /* >          rest is set to zero. */
646 /* > \endverbatim */
647 /* > */
648 /* > \param[in] LDA */
649 /* > \verbatim */
650 /* >          LDA is INTEGER */
651 /* >          The leading dimension of the array A.  LDA >= f2cmax(1,N). */
652 /* > \endverbatim */
653 /* > */
654 /* > \param[in,out] B */
655 /* > \verbatim */
656 /* >          B is DOUBLE PRECISION array, dimension (LDB, N) */
657 /* >          On entry, the N-by-N upper triangular matrix B. */
658 /* >          On exit, the upper triangular matrix T = Q**T B Z.  The */
659 /* >          elements below the diagonal are set to zero. */
660 /* > \endverbatim */
661 /* > */
662 /* > \param[in] LDB */
663 /* > \verbatim */
664 /* >          LDB is INTEGER */
665 /* >          The leading dimension of the array B.  LDB >= f2cmax(1,N). */
666 /* > \endverbatim */
667 /* > */
668 /* > \param[in,out] Q */
669 /* > \verbatim */
670 /* >          Q is DOUBLE PRECISION array, dimension (LDQ, N) */
671 /* >          On entry, if COMPQ = 'V', the orthogonal matrix Q1, */
672 /* >          typically from the QR factorization of B. */
673 /* >          On exit, if COMPQ='I', the orthogonal matrix Q, and if */
674 /* >          COMPQ = 'V', the product Q1*Q. */
675 /* >          Not referenced if COMPQ='N'. */
676 /* > \endverbatim */
677 /* > */
678 /* > \param[in] LDQ */
679 /* > \verbatim */
680 /* >          LDQ is INTEGER */
681 /* >          The leading dimension of the array Q. */
682 /* >          LDQ >= N if COMPQ='V' or 'I'; LDQ >= 1 otherwise. */
683 /* > \endverbatim */
684 /* > */
685 /* > \param[in,out] Z */
686 /* > \verbatim */
687 /* >          Z is DOUBLE PRECISION array, dimension (LDZ, N) */
688 /* >          On entry, if COMPZ = 'V', the orthogonal matrix Z1. */
689 /* >          On exit, if COMPZ='I', the orthogonal matrix Z, and if */
690 /* >          COMPZ = 'V', the product Z1*Z. */
691 /* >          Not referenced if COMPZ='N'. */
692 /* > \endverbatim */
693 /* > */
694 /* > \param[in] LDZ */
695 /* > \verbatim */
696 /* >          LDZ is INTEGER */
697 /* >          The leading dimension of the array Z. */
698 /* >          LDZ >= N if COMPZ='V' or 'I'; LDZ >= 1 otherwise. */
699 /* > \endverbatim */
700 /* > */
701 /* > \param[out] WORK */
702 /* > \verbatim */
703 /* >          WORK is DOUBLE PRECISION array, dimension (LWORK) */
704 /* >          On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
705 /* > \endverbatim */
706 /* > */
707 /* > \param[in]  LWORK */
708 /* > \verbatim */
709 /* >          LWORK is INTEGER */
710 /* >          The length of the array WORK.  LWORK >= 1. */
711 /* >          For optimum performance LWORK >= 6*N*NB, where NB is the */
712 /* >          optimal blocksize. */
713 /* > */
714 /* >          If LWORK = -1, then a workspace query is assumed; the routine */
715 /* >          only calculates the optimal size of the WORK array, returns */
716 /* >          this value as the first entry of the WORK array, and no error */
717 /* >          message related to LWORK is issued by XERBLA. */
718 /* > \endverbatim */
719 /* > */
720 /* > \param[out] INFO */
721 /* > \verbatim */
722 /* >          INFO is INTEGER */
723 /* >          = 0:  successful exit. */
724 /* >          < 0:  if INFO = -i, the i-th argument had an illegal value. */
725 /* > \endverbatim */
726
727 /*  Authors: */
728 /*  ======== */
729
730 /* > \author Univ. of Tennessee */
731 /* > \author Univ. of California Berkeley */
732 /* > \author Univ. of Colorado Denver */
733 /* > \author NAG Ltd. */
734
735 /* > \date January 2015 */
736
737 /* > \ingroup doubleOTHERcomputational */
738
739 /* > \par Further Details: */
740 /*  ===================== */
741 /* > */
742 /* > \verbatim */
743 /* > */
744 /* >  This routine reduces A to Hessenberg form and maintains B in */
745 /* >  using a blocked variant of Moler and Stewart's original algorithm, */
746 /* >  as described by Kagstrom, Kressner, Quintana-Orti, and Quintana-Orti */
747 /* >  (BIT 2008). */
748 /* > \endverbatim */
749 /* > */
750 /*  ===================================================================== */
751 /* Subroutine */ int dgghd3_(char *compq, char *compz, integer *n, integer *
752         ilo, integer *ihi, doublereal *a, integer *lda, doublereal *b, 
753         integer *ldb, doublereal *q, integer *ldq, doublereal *z__, integer *
754         ldz, doublereal *work, integer *lwork, integer *info)
755 {
756     /* System generated locals */
757     integer a_dim1, a_offset, b_dim1, b_offset, q_dim1, q_offset, z_dim1, 
758             z_offset, i__1, i__2, i__3, i__4, i__5, i__6, i__7, i__8;
759     doublereal d__1;
760
761     /* Local variables */
762     logical blk22;
763     integer cola, jcol, ierr;
764     doublereal temp;
765     extern /* Subroutine */ int drot_(integer *, doublereal *, integer *, 
766             doublereal *, integer *, doublereal *, doublereal *);
767     integer jrow, topq, ppwo;
768     doublereal temp1, temp2, temp3, c__;
769     integer kacc22, i__, j, k;
770     doublereal s;
771     extern /* Subroutine */ int dgemm_(char *, char *, integer *, integer *, 
772             integer *, doublereal *, doublereal *, integer *, doublereal *, 
773             integer *, doublereal *, doublereal *, integer *);
774     extern logical lsame_(char *, char *);
775     extern /* Subroutine */ int dgemv_(char *, integer *, integer *, 
776             doublereal *, doublereal *, integer *, doublereal *, integer *, 
777             doublereal *, doublereal *, integer *);
778     integer nbmin;
779     extern /* Subroutine */ int dorm22_(char *, char *, integer *, integer *, 
780             integer *, integer *, doublereal *, integer *, doublereal *, 
781             integer *, doublereal *, integer *, integer *);
782     integer nblst;
783     logical initq;
784     doublereal c1, c2;
785     logical wantq;
786     integer j0;
787     extern /* Subroutine */ int dtrmv_(char *, char *, char *, integer *, 
788             doublereal *, integer *, doublereal *, integer *);
789     logical initz, wantz;
790     doublereal s1, s2;
791     char compq2[1], compz2[1];
792     integer nb, jj, nh;
793     extern /* Subroutine */ int dgghrd_(char *, char *, integer *, integer *, 
794             integer *, doublereal *, integer *, doublereal *, integer *, 
795             doublereal *, integer *, doublereal *, integer *, integer *);
796     integer nx, pw;
797     extern /* Subroutine */ int dlaset_(char *, integer *, integer *, 
798             doublereal *, doublereal *, doublereal *, integer *), 
799             dlartg_(doublereal *, doublereal *, doublereal *, doublereal *, 
800             doublereal *);
801     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
802             integer *, integer *, ftnlen, ftnlen);
803     extern /* Subroutine */ int dlacpy_(char *, integer *, integer *, 
804             doublereal *, integer *, doublereal *, integer *), 
805             xerbla_(char *, integer *, ftnlen);
806     integer lwkopt;
807     logical lquery;
808     integer nnb, len, top, ppw, n2nb;
809
810
811 /*  -- LAPACK computational routine (version 3.8.0) -- */
812 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
813 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
814 /*     January 2015 */
815
816
817
818 /* ===================================================================== */
819
820
821 /*     Decode and test the input parameters. */
822
823     /* Parameter adjustments */
824     a_dim1 = *lda;
825     a_offset = 1 + a_dim1 * 1;
826     a -= a_offset;
827     b_dim1 = *ldb;
828     b_offset = 1 + b_dim1 * 1;
829     b -= b_offset;
830     q_dim1 = *ldq;
831     q_offset = 1 + q_dim1 * 1;
832     q -= q_offset;
833     z_dim1 = *ldz;
834     z_offset = 1 + z_dim1 * 1;
835     z__ -= z_offset;
836     --work;
837
838     /* Function Body */
839     *info = 0;
840     nb = ilaenv_(&c__1, "DGGHD3", " ", n, ilo, ihi, &c_n1, (ftnlen)6, (ftnlen)
841             1);
842 /* Computing MAX */
843     i__1 = *n * 6 * nb;
844     lwkopt = f2cmax(i__1,1);
845     work[1] = (doublereal) lwkopt;
846     initq = lsame_(compq, "I");
847     wantq = initq || lsame_(compq, "V");
848     initz = lsame_(compz, "I");
849     wantz = initz || lsame_(compz, "V");
850     lquery = *lwork == -1;
851
852     if (! lsame_(compq, "N") && ! wantq) {
853         *info = -1;
854     } else if (! lsame_(compz, "N") && ! wantz) {
855         *info = -2;
856     } else if (*n < 0) {
857         *info = -3;
858     } else if (*ilo < 1) {
859         *info = -4;
860     } else if (*ihi > *n || *ihi < *ilo - 1) {
861         *info = -5;
862     } else if (*lda < f2cmax(1,*n)) {
863         *info = -7;
864     } else if (*ldb < f2cmax(1,*n)) {
865         *info = -9;
866     } else if (wantq && *ldq < *n || *ldq < 1) {
867         *info = -11;
868     } else if (wantz && *ldz < *n || *ldz < 1) {
869         *info = -13;
870     } else if (*lwork < 1 && ! lquery) {
871         *info = -15;
872     }
873     if (*info != 0) {
874         i__1 = -(*info);
875         xerbla_("DGGHD3", &i__1, (ftnlen)6);
876         return 0;
877     } else if (lquery) {
878         return 0;
879     }
880
881 /*     Initialize Q and Z if desired. */
882
883     if (initq) {
884         dlaset_("All", n, n, &c_b14, &c_b15, &q[q_offset], ldq);
885     }
886     if (initz) {
887         dlaset_("All", n, n, &c_b14, &c_b15, &z__[z_offset], ldz);
888     }
889
890 /*     Zero out lower triangle of B. */
891
892     if (*n > 1) {
893         i__1 = *n - 1;
894         i__2 = *n - 1;
895         dlaset_("Lower", &i__1, &i__2, &c_b14, &c_b14, &b[b_dim1 + 2], ldb);
896     }
897
898 /*     Quick return if possible */
899
900     nh = *ihi - *ilo + 1;
901     if (nh <= 1) {
902         work[1] = 1.;
903         return 0;
904     }
905
906 /*     Determine the blocksize. */
907
908     nbmin = ilaenv_(&c__2, "DGGHD3", " ", n, ilo, ihi, &c_n1, (ftnlen)6, (
909             ftnlen)1);
910     if (nb > 1 && nb < nh) {
911
912 /*        Determine when to use unblocked instead of blocked code. */
913
914 /* Computing MAX */
915         i__1 = nb, i__2 = ilaenv_(&c__3, "DGGHD3", " ", n, ilo, ihi, &c_n1, (
916                 ftnlen)6, (ftnlen)1);
917         nx = f2cmax(i__1,i__2);
918         if (nx < nh) {
919
920 /*           Determine if workspace is large enough for blocked code. */
921
922             if (*lwork < lwkopt) {
923
924 /*              Not enough workspace to use optimal NB:  determine the */
925 /*              minimum value of NB, and reduce NB or force use of */
926 /*              unblocked code. */
927
928 /* Computing MAX */
929                 i__1 = 2, i__2 = ilaenv_(&c__2, "DGGHD3", " ", n, ilo, ihi, &
930                         c_n1, (ftnlen)6, (ftnlen)1);
931                 nbmin = f2cmax(i__1,i__2);
932                 if (*lwork >= *n * 6 * nbmin) {
933                     nb = *lwork / (*n * 6);
934                 } else {
935                     nb = 1;
936                 }
937             }
938         }
939     }
940
941     if (nb < nbmin || nb >= nh) {
942
943 /*        Use unblocked code below */
944
945         jcol = *ilo;
946
947     } else {
948
949 /*        Use blocked code */
950
951         kacc22 = ilaenv_(&c__16, "DGGHD3", " ", n, ilo, ihi, &c_n1, (ftnlen)6,
952                  (ftnlen)1);
953         blk22 = kacc22 == 2;
954         i__1 = *ihi - 2;
955         i__2 = nb;
956         for (jcol = *ilo; i__2 < 0 ? jcol >= i__1 : jcol <= i__1; jcol += 
957                 i__2) {
958 /* Computing MIN */
959             i__3 = nb, i__4 = *ihi - jcol - 1;
960             nnb = f2cmin(i__3,i__4);
961
962 /*           Initialize small orthogonal factors that will hold the */
963 /*           accumulated Givens rotations in workspace. */
964 /*           N2NB   denotes the number of 2*NNB-by-2*NNB factors */
965 /*           NBLST  denotes the (possibly smaller) order of the last */
966 /*                  factor. */
967
968             n2nb = (*ihi - jcol - 1) / nnb - 1;
969             nblst = *ihi - jcol - n2nb * nnb;
970             dlaset_("All", &nblst, &nblst, &c_b14, &c_b15, &work[1], &nblst);
971             pw = nblst * nblst + 1;
972             i__3 = n2nb;
973             for (i__ = 1; i__ <= i__3; ++i__) {
974                 i__4 = nnb << 1;
975                 i__5 = nnb << 1;
976                 i__6 = nnb << 1;
977                 dlaset_("All", &i__4, &i__5, &c_b14, &c_b15, &work[pw], &i__6);
978                 pw += (nnb << 2) * nnb;
979             }
980
981 /*           Reduce columns JCOL:JCOL+NNB-1 of A to Hessenberg form. */
982
983             i__3 = jcol + nnb - 1;
984             for (j = jcol; j <= i__3; ++j) {
985
986 /*              Reduce Jth column of A. Store cosines and sines in Jth */
987 /*              column of A and B, respectively. */
988
989                 i__4 = j + 2;
990                 for (i__ = *ihi; i__ >= i__4; --i__) {
991                     temp = a[i__ - 1 + j * a_dim1];
992                     dlartg_(&temp, &a[i__ + j * a_dim1], &c__, &s, &a[i__ - 1 
993                             + j * a_dim1]);
994                     a[i__ + j * a_dim1] = c__;
995                     b[i__ + j * b_dim1] = s;
996                 }
997
998 /*              Accumulate Givens rotations into workspace array. */
999
1000                 ppw = (nblst + 1) * (nblst - 2) - j + jcol + 1;
1001                 len = j + 2 - jcol;
1002                 jrow = j + n2nb * nnb + 2;
1003                 i__4 = jrow;
1004                 for (i__ = *ihi; i__ >= i__4; --i__) {
1005                     c__ = a[i__ + j * a_dim1];
1006                     s = b[i__ + j * b_dim1];
1007                     i__5 = ppw + len - 1;
1008                     for (jj = ppw; jj <= i__5; ++jj) {
1009                         temp = work[jj + nblst];
1010                         work[jj + nblst] = c__ * temp - s * work[jj];
1011                         work[jj] = s * temp + c__ * work[jj];
1012                     }
1013                     ++len;
1014                     ppw = ppw - nblst - 1;
1015                 }
1016
1017                 ppwo = nblst * nblst + (nnb + j - jcol - 1 << 1) * nnb + nnb;
1018                 j0 = jrow - nnb;
1019                 i__4 = j + 2;
1020                 i__5 = -nnb;
1021                 for (jrow = j0; i__5 < 0 ? jrow >= i__4 : jrow <= i__4; jrow 
1022                         += i__5) {
1023                     ppw = ppwo;
1024                     len = j + 2 - jcol;
1025                     i__6 = jrow;
1026                     for (i__ = jrow + nnb - 1; i__ >= i__6; --i__) {
1027                         c__ = a[i__ + j * a_dim1];
1028                         s = b[i__ + j * b_dim1];
1029                         i__7 = ppw + len - 1;
1030                         for (jj = ppw; jj <= i__7; ++jj) {
1031                             temp = work[jj + (nnb << 1)];
1032                             work[jj + (nnb << 1)] = c__ * temp - s * work[jj];
1033                             work[jj] = s * temp + c__ * work[jj];
1034                         }
1035                         ++len;
1036                         ppw = ppw - (nnb << 1) - 1;
1037                     }
1038                     ppwo += (nnb << 2) * nnb;
1039                 }
1040
1041 /*              TOP denotes the number of top rows in A and B that will */
1042 /*              not be updated during the next steps. */
1043
1044                 if (jcol <= 2) {
1045                     top = 0;
1046                 } else {
1047                     top = jcol;
1048                 }
1049
1050 /*              Propagate transformations through B and replace stored */
1051 /*              left sines/cosines by right sines/cosines. */
1052
1053                 i__5 = j + 1;
1054                 for (jj = *n; jj >= i__5; --jj) {
1055
1056 /*                 Update JJth column of B. */
1057
1058 /* Computing MIN */
1059                     i__4 = jj + 1;
1060                     i__6 = j + 2;
1061                     for (i__ = f2cmin(i__4,*ihi); i__ >= i__6; --i__) {
1062                         c__ = a[i__ + j * a_dim1];
1063                         s = b[i__ + j * b_dim1];
1064                         temp = b[i__ + jj * b_dim1];
1065                         b[i__ + jj * b_dim1] = c__ * temp - s * b[i__ - 1 + 
1066                                 jj * b_dim1];
1067                         b[i__ - 1 + jj * b_dim1] = s * temp + c__ * b[i__ - 1 
1068                                 + jj * b_dim1];
1069                     }
1070
1071 /*                 Annihilate B( JJ+1, JJ ). */
1072
1073                     if (jj < *ihi) {
1074                         temp = b[jj + 1 + (jj + 1) * b_dim1];
1075                         dlartg_(&temp, &b[jj + 1 + jj * b_dim1], &c__, &s, &b[
1076                                 jj + 1 + (jj + 1) * b_dim1]);
1077                         b[jj + 1 + jj * b_dim1] = 0.;
1078                         i__6 = jj - top;
1079                         drot_(&i__6, &b[top + 1 + (jj + 1) * b_dim1], &c__1, &
1080                                 b[top + 1 + jj * b_dim1], &c__1, &c__, &s);
1081                         a[jj + 1 + j * a_dim1] = c__;
1082                         b[jj + 1 + j * b_dim1] = -s;
1083                     }
1084                 }
1085
1086 /*              Update A by transformations from right. */
1087 /*              Explicit loop unrolling provides better performance */
1088 /*              compared to DLASR. */
1089 /*               CALL DLASR( 'Right', 'Variable', 'Backward', IHI-TOP, */
1090 /*     $                     IHI-J, A( J+2, J ), B( J+2, J ), */
1091 /*     $                     A( TOP+1, J+1 ), LDA ) */
1092
1093                 jj = (*ihi - j - 1) % 3;
1094                 i__5 = jj + 1;
1095                 for (i__ = *ihi - j - 3; i__ >= i__5; i__ += -3) {
1096                     c__ = a[j + 1 + i__ + j * a_dim1];
1097                     s = -b[j + 1 + i__ + j * b_dim1];
1098                     c1 = a[j + 2 + i__ + j * a_dim1];
1099                     s1 = -b[j + 2 + i__ + j * b_dim1];
1100                     c2 = a[j + 3 + i__ + j * a_dim1];
1101                     s2 = -b[j + 3 + i__ + j * b_dim1];
1102
1103                     i__6 = *ihi;
1104                     for (k = top + 1; k <= i__6; ++k) {
1105                         temp = a[k + (j + i__) * a_dim1];
1106                         temp1 = a[k + (j + i__ + 1) * a_dim1];
1107                         temp2 = a[k + (j + i__ + 2) * a_dim1];
1108                         temp3 = a[k + (j + i__ + 3) * a_dim1];
1109                         a[k + (j + i__ + 3) * a_dim1] = c2 * temp3 + s2 * 
1110                                 temp2;
1111                         temp2 = -s2 * temp3 + c2 * temp2;
1112                         a[k + (j + i__ + 2) * a_dim1] = c1 * temp2 + s1 * 
1113                                 temp1;
1114                         temp1 = -s1 * temp2 + c1 * temp1;
1115                         a[k + (j + i__ + 1) * a_dim1] = c__ * temp1 + s * 
1116                                 temp;
1117                         a[k + (j + i__) * a_dim1] = -s * temp1 + c__ * temp;
1118                     }
1119                 }
1120
1121                 if (jj > 0) {
1122                     for (i__ = jj; i__ >= 1; --i__) {
1123                         i__5 = *ihi - top;
1124                         d__1 = -b[j + 1 + i__ + j * b_dim1];
1125                         drot_(&i__5, &a[top + 1 + (j + i__ + 1) * a_dim1], &
1126                                 c__1, &a[top + 1 + (j + i__) * a_dim1], &c__1,
1127                                  &a[j + 1 + i__ + j * a_dim1], &d__1);
1128                     }
1129                 }
1130
1131 /*              Update (J+1)th column of A by transformations from left. */
1132
1133                 if (j < jcol + nnb - 1) {
1134                     len = j + 1 - jcol;
1135
1136 /*                 Multiply with the trailing accumulated orthogonal */
1137 /*                 matrix, which takes the form */
1138
1139 /*                        [  U11  U12  ] */
1140 /*                    U = [            ], */
1141 /*                        [  U21  U22  ] */
1142
1143 /*                 where U21 is a LEN-by-LEN matrix and U12 is lower */
1144 /*                 triangular. */
1145
1146                     jrow = *ihi - nblst + 1;
1147                     dgemv_("Transpose", &nblst, &len, &c_b15, &work[1], &
1148                             nblst, &a[jrow + (j + 1) * a_dim1], &c__1, &c_b14,
1149                              &work[pw], &c__1);
1150                     ppw = pw + len;
1151                     i__5 = jrow + nblst - len - 1;
1152                     for (i__ = jrow; i__ <= i__5; ++i__) {
1153                         work[ppw] = a[i__ + (j + 1) * a_dim1];
1154                         ++ppw;
1155                     }
1156                     i__5 = nblst - len;
1157                     dtrmv_("Lower", "Transpose", "Non-unit", &i__5, &work[len 
1158                             * nblst + 1], &nblst, &work[pw + len], &c__1);
1159                     i__5 = nblst - len;
1160                     dgemv_("Transpose", &len, &i__5, &c_b15, &work[(len + 1) *
1161                              nblst - len + 1], &nblst, &a[jrow + nblst - len 
1162                             + (j + 1) * a_dim1], &c__1, &c_b15, &work[pw + 
1163                             len], &c__1);
1164                     ppw = pw;
1165                     i__5 = jrow + nblst - 1;
1166                     for (i__ = jrow; i__ <= i__5; ++i__) {
1167                         a[i__ + (j + 1) * a_dim1] = work[ppw];
1168                         ++ppw;
1169                     }
1170
1171 /*                 Multiply with the other accumulated orthogonal */
1172 /*                 matrices, which take the form */
1173
1174 /*                        [  U11  U12   0  ] */
1175 /*                        [                ] */
1176 /*                    U = [  U21  U22   0  ], */
1177 /*                        [                ] */
1178 /*                        [   0    0    I  ] */
1179
1180 /*                 where I denotes the (NNB-LEN)-by-(NNB-LEN) identity */
1181 /*                 matrix, U21 is a LEN-by-LEN upper triangular matrix */
1182 /*                 and U12 is an NNB-by-NNB lower triangular matrix. */
1183
1184                     ppwo = nblst * nblst + 1;
1185                     j0 = jrow - nnb;
1186                     i__5 = jcol + 1;
1187                     i__6 = -nnb;
1188                     for (jrow = j0; i__6 < 0 ? jrow >= i__5 : jrow <= i__5; 
1189                             jrow += i__6) {
1190                         ppw = pw + len;
1191                         i__4 = jrow + nnb - 1;
1192                         for (i__ = jrow; i__ <= i__4; ++i__) {
1193                             work[ppw] = a[i__ + (j + 1) * a_dim1];
1194                             ++ppw;
1195                         }
1196                         ppw = pw;
1197                         i__4 = jrow + nnb + len - 1;
1198                         for (i__ = jrow + nnb; i__ <= i__4; ++i__) {
1199                             work[ppw] = a[i__ + (j + 1) * a_dim1];
1200                             ++ppw;
1201                         }
1202                         i__4 = nnb << 1;
1203                         dtrmv_("Upper", "Transpose", "Non-unit", &len, &work[
1204                                 ppwo + nnb], &i__4, &work[pw], &c__1);
1205                         i__4 = nnb << 1;
1206                         dtrmv_("Lower", "Transpose", "Non-unit", &nnb, &work[
1207                                 ppwo + (len << 1) * nnb], &i__4, &work[pw + 
1208                                 len], &c__1);
1209                         i__4 = nnb << 1;
1210                         dgemv_("Transpose", &nnb, &len, &c_b15, &work[ppwo], &
1211                                 i__4, &a[jrow + (j + 1) * a_dim1], &c__1, &
1212                                 c_b15, &work[pw], &c__1);
1213                         i__4 = nnb << 1;
1214                         dgemv_("Transpose", &len, &nnb, &c_b15, &work[ppwo + (
1215                                 len << 1) * nnb + nnb], &i__4, &a[jrow + nnb 
1216                                 + (j + 1) * a_dim1], &c__1, &c_b15, &work[pw 
1217                                 + len], &c__1);
1218                         ppw = pw;
1219                         i__4 = jrow + len + nnb - 1;
1220                         for (i__ = jrow; i__ <= i__4; ++i__) {
1221                             a[i__ + (j + 1) * a_dim1] = work[ppw];
1222                             ++ppw;
1223                         }
1224                         ppwo += (nnb << 2) * nnb;
1225                     }
1226                 }
1227             }
1228
1229 /*           Apply accumulated orthogonal matrices to A. */
1230
1231             cola = *n - jcol - nnb + 1;
1232             j = *ihi - nblst + 1;
1233             dgemm_("Transpose", "No Transpose", &nblst, &cola, &nblst, &c_b15,
1234                      &work[1], &nblst, &a[j + (jcol + nnb) * a_dim1], lda, &
1235                     c_b14, &work[pw], &nblst);
1236             dlacpy_("All", &nblst, &cola, &work[pw], &nblst, &a[j + (jcol + 
1237                     nnb) * a_dim1], lda);
1238             ppwo = nblst * nblst + 1;
1239             j0 = j - nnb;
1240             i__3 = jcol + 1;
1241             i__6 = -nnb;
1242             for (j = j0; i__6 < 0 ? j >= i__3 : j <= i__3; j += i__6) {
1243                 if (blk22) {
1244
1245 /*                 Exploit the structure of */
1246
1247 /*                        [  U11  U12  ] */
1248 /*                    U = [            ] */
1249 /*                        [  U21  U22  ], */
1250
1251 /*                 where all blocks are NNB-by-NNB, U21 is upper */
1252 /*                 triangular and U12 is lower triangular. */
1253
1254                     i__5 = nnb << 1;
1255                     i__4 = nnb << 1;
1256                     i__7 = *lwork - pw + 1;
1257                     dorm22_("Left", "Transpose", &i__5, &cola, &nnb, &nnb, &
1258                             work[ppwo], &i__4, &a[j + (jcol + nnb) * a_dim1], 
1259                             lda, &work[pw], &i__7, &ierr);
1260                 } else {
1261
1262 /*                 Ignore the structure of U. */
1263
1264                     i__5 = nnb << 1;
1265                     i__4 = nnb << 1;
1266                     i__7 = nnb << 1;
1267                     i__8 = nnb << 1;
1268                     dgemm_("Transpose", "No Transpose", &i__5, &cola, &i__4, &
1269                             c_b15, &work[ppwo], &i__7, &a[j + (jcol + nnb) * 
1270                             a_dim1], lda, &c_b14, &work[pw], &i__8);
1271                     i__5 = nnb << 1;
1272                     i__4 = nnb << 1;
1273                     dlacpy_("All", &i__5, &cola, &work[pw], &i__4, &a[j + (
1274                             jcol + nnb) * a_dim1], lda);
1275                 }
1276                 ppwo += (nnb << 2) * nnb;
1277             }
1278
1279 /*           Apply accumulated orthogonal matrices to Q. */
1280
1281             if (wantq) {
1282                 j = *ihi - nblst + 1;
1283                 if (initq) {
1284 /* Computing MAX */
1285                     i__6 = 2, i__3 = j - jcol + 1;
1286                     topq = f2cmax(i__6,i__3);
1287                     nh = *ihi - topq + 1;
1288                 } else {
1289                     topq = 1;
1290                     nh = *n;
1291                 }
1292                 dgemm_("No Transpose", "No Transpose", &nh, &nblst, &nblst, &
1293                         c_b15, &q[topq + j * q_dim1], ldq, &work[1], &nblst, &
1294                         c_b14, &work[pw], &nh);
1295                 dlacpy_("All", &nh, &nblst, &work[pw], &nh, &q[topq + j * 
1296                         q_dim1], ldq);
1297                 ppwo = nblst * nblst + 1;
1298                 j0 = j - nnb;
1299                 i__6 = jcol + 1;
1300                 i__3 = -nnb;
1301                 for (j = j0; i__3 < 0 ? j >= i__6 : j <= i__6; j += i__3) {
1302                     if (initq) {
1303 /* Computing MAX */
1304                         i__5 = 2, i__4 = j - jcol + 1;
1305                         topq = f2cmax(i__5,i__4);
1306                         nh = *ihi - topq + 1;
1307                     }
1308                     if (blk22) {
1309
1310 /*                    Exploit the structure of U. */
1311
1312                         i__5 = nnb << 1;
1313                         i__4 = nnb << 1;
1314                         i__7 = *lwork - pw + 1;
1315                         dorm22_("Right", "No Transpose", &nh, &i__5, &nnb, &
1316                                 nnb, &work[ppwo], &i__4, &q[topq + j * q_dim1]
1317                                 , ldq, &work[pw], &i__7, &ierr);
1318                     } else {
1319
1320 /*                    Ignore the structure of U. */
1321
1322                         i__5 = nnb << 1;
1323                         i__4 = nnb << 1;
1324                         i__7 = nnb << 1;
1325                         dgemm_("No Transpose", "No Transpose", &nh, &i__5, &
1326                                 i__4, &c_b15, &q[topq + j * q_dim1], ldq, &
1327                                 work[ppwo], &i__7, &c_b14, &work[pw], &nh);
1328                         i__5 = nnb << 1;
1329                         dlacpy_("All", &nh, &i__5, &work[pw], &nh, &q[topq + 
1330                                 j * q_dim1], ldq);
1331                     }
1332                     ppwo += (nnb << 2) * nnb;
1333                 }
1334             }
1335
1336 /*           Accumulate right Givens rotations if required. */
1337
1338             if (wantz || top > 0) {
1339
1340 /*              Initialize small orthogonal factors that will hold the */
1341 /*              accumulated Givens rotations in workspace. */
1342
1343                 dlaset_("All", &nblst, &nblst, &c_b14, &c_b15, &work[1], &
1344                         nblst);
1345                 pw = nblst * nblst + 1;
1346                 i__3 = n2nb;
1347                 for (i__ = 1; i__ <= i__3; ++i__) {
1348                     i__6 = nnb << 1;
1349                     i__5 = nnb << 1;
1350                     i__4 = nnb << 1;
1351                     dlaset_("All", &i__6, &i__5, &c_b14, &c_b15, &work[pw], &
1352                             i__4);
1353                     pw += (nnb << 2) * nnb;
1354                 }
1355
1356 /*              Accumulate Givens rotations into workspace array. */
1357
1358                 i__3 = jcol + nnb - 1;
1359                 for (j = jcol; j <= i__3; ++j) {
1360                     ppw = (nblst + 1) * (nblst - 2) - j + jcol + 1;
1361                     len = j + 2 - jcol;
1362                     jrow = j + n2nb * nnb + 2;
1363                     i__6 = jrow;
1364                     for (i__ = *ihi; i__ >= i__6; --i__) {
1365                         c__ = a[i__ + j * a_dim1];
1366                         a[i__ + j * a_dim1] = 0.;
1367                         s = b[i__ + j * b_dim1];
1368                         b[i__ + j * b_dim1] = 0.;
1369                         i__5 = ppw + len - 1;
1370                         for (jj = ppw; jj <= i__5; ++jj) {
1371                             temp = work[jj + nblst];
1372                             work[jj + nblst] = c__ * temp - s * work[jj];
1373                             work[jj] = s * temp + c__ * work[jj];
1374                         }
1375                         ++len;
1376                         ppw = ppw - nblst - 1;
1377                     }
1378
1379                     ppwo = nblst * nblst + (nnb + j - jcol - 1 << 1) * nnb + 
1380                             nnb;
1381                     j0 = jrow - nnb;
1382                     i__6 = j + 2;
1383                     i__5 = -nnb;
1384                     for (jrow = j0; i__5 < 0 ? jrow >= i__6 : jrow <= i__6; 
1385                             jrow += i__5) {
1386                         ppw = ppwo;
1387                         len = j + 2 - jcol;
1388                         i__4 = jrow;
1389                         for (i__ = jrow + nnb - 1; i__ >= i__4; --i__) {
1390                             c__ = a[i__ + j * a_dim1];
1391                             a[i__ + j * a_dim1] = 0.;
1392                             s = b[i__ + j * b_dim1];
1393                             b[i__ + j * b_dim1] = 0.;
1394                             i__7 = ppw + len - 1;
1395                             for (jj = ppw; jj <= i__7; ++jj) {
1396                                 temp = work[jj + (nnb << 1)];
1397                                 work[jj + (nnb << 1)] = c__ * temp - s * work[
1398                                         jj];
1399                                 work[jj] = s * temp + c__ * work[jj];
1400                             }
1401                             ++len;
1402                             ppw = ppw - (nnb << 1) - 1;
1403                         }
1404                         ppwo += (nnb << 2) * nnb;
1405                     }
1406                 }
1407             } else {
1408
1409                 i__3 = *ihi - jcol - 1;
1410                 dlaset_("Lower", &i__3, &nnb, &c_b14, &c_b14, &a[jcol + 2 + 
1411                         jcol * a_dim1], lda);
1412                 i__3 = *ihi - jcol - 1;
1413                 dlaset_("Lower", &i__3, &nnb, &c_b14, &c_b14, &b[jcol + 2 + 
1414                         jcol * b_dim1], ldb);
1415             }
1416
1417 /*           Apply accumulated orthogonal matrices to A and B. */
1418
1419             if (top > 0) {
1420                 j = *ihi - nblst + 1;
1421                 dgemm_("No Transpose", "No Transpose", &top, &nblst, &nblst, &
1422                         c_b15, &a[j * a_dim1 + 1], lda, &work[1], &nblst, &
1423                         c_b14, &work[pw], &top);
1424                 dlacpy_("All", &top, &nblst, &work[pw], &top, &a[j * a_dim1 + 
1425                         1], lda);
1426                 ppwo = nblst * nblst + 1;
1427                 j0 = j - nnb;
1428                 i__3 = jcol + 1;
1429                 i__5 = -nnb;
1430                 for (j = j0; i__5 < 0 ? j >= i__3 : j <= i__3; j += i__5) {
1431                     if (blk22) {
1432
1433 /*                    Exploit the structure of U. */
1434
1435                         i__6 = nnb << 1;
1436                         i__4 = nnb << 1;
1437                         i__7 = *lwork - pw + 1;
1438                         dorm22_("Right", "No Transpose", &top, &i__6, &nnb, &
1439                                 nnb, &work[ppwo], &i__4, &a[j * a_dim1 + 1], 
1440                                 lda, &work[pw], &i__7, &ierr);
1441                     } else {
1442
1443 /*                    Ignore the structure of U. */
1444
1445                         i__6 = nnb << 1;
1446                         i__4 = nnb << 1;
1447                         i__7 = nnb << 1;
1448                         dgemm_("No Transpose", "No Transpose", &top, &i__6, &
1449                                 i__4, &c_b15, &a[j * a_dim1 + 1], lda, &work[
1450                                 ppwo], &i__7, &c_b14, &work[pw], &top);
1451                         i__6 = nnb << 1;
1452                         dlacpy_("All", &top, &i__6, &work[pw], &top, &a[j * 
1453                                 a_dim1 + 1], lda);
1454                     }
1455                     ppwo += (nnb << 2) * nnb;
1456                 }
1457
1458                 j = *ihi - nblst + 1;
1459                 dgemm_("No Transpose", "No Transpose", &top, &nblst, &nblst, &
1460                         c_b15, &b[j * b_dim1 + 1], ldb, &work[1], &nblst, &
1461                         c_b14, &work[pw], &top);
1462                 dlacpy_("All", &top, &nblst, &work[pw], &top, &b[j * b_dim1 + 
1463                         1], ldb);
1464                 ppwo = nblst * nblst + 1;
1465                 j0 = j - nnb;
1466                 i__5 = jcol + 1;
1467                 i__3 = -nnb;
1468                 for (j = j0; i__3 < 0 ? j >= i__5 : j <= i__5; j += i__3) {
1469                     if (blk22) {
1470
1471 /*                    Exploit the structure of U. */
1472
1473                         i__6 = nnb << 1;
1474                         i__4 = nnb << 1;
1475                         i__7 = *lwork - pw + 1;
1476                         dorm22_("Right", "No Transpose", &top, &i__6, &nnb, &
1477                                 nnb, &work[ppwo], &i__4, &b[j * b_dim1 + 1], 
1478                                 ldb, &work[pw], &i__7, &ierr);
1479                     } else {
1480
1481 /*                    Ignore the structure of U. */
1482
1483                         i__6 = nnb << 1;
1484                         i__4 = nnb << 1;
1485                         i__7 = nnb << 1;
1486                         dgemm_("No Transpose", "No Transpose", &top, &i__6, &
1487                                 i__4, &c_b15, &b[j * b_dim1 + 1], ldb, &work[
1488                                 ppwo], &i__7, &c_b14, &work[pw], &top);
1489                         i__6 = nnb << 1;
1490                         dlacpy_("All", &top, &i__6, &work[pw], &top, &b[j * 
1491                                 b_dim1 + 1], ldb);
1492                     }
1493                     ppwo += (nnb << 2) * nnb;
1494                 }
1495             }
1496
1497 /*           Apply accumulated orthogonal matrices to Z. */
1498
1499             if (wantz) {
1500                 j = *ihi - nblst + 1;
1501                 if (initq) {
1502 /* Computing MAX */
1503                     i__3 = 2, i__5 = j - jcol + 1;
1504                     topq = f2cmax(i__3,i__5);
1505                     nh = *ihi - topq + 1;
1506                 } else {
1507                     topq = 1;
1508                     nh = *n;
1509                 }
1510                 dgemm_("No Transpose", "No Transpose", &nh, &nblst, &nblst, &
1511                         c_b15, &z__[topq + j * z_dim1], ldz, &work[1], &nblst,
1512                          &c_b14, &work[pw], &nh);
1513                 dlacpy_("All", &nh, &nblst, &work[pw], &nh, &z__[topq + j * 
1514                         z_dim1], ldz);
1515                 ppwo = nblst * nblst + 1;
1516                 j0 = j - nnb;
1517                 i__3 = jcol + 1;
1518                 i__5 = -nnb;
1519                 for (j = j0; i__5 < 0 ? j >= i__3 : j <= i__3; j += i__5) {
1520                     if (initq) {
1521 /* Computing MAX */
1522                         i__6 = 2, i__4 = j - jcol + 1;
1523                         topq = f2cmax(i__6,i__4);
1524                         nh = *ihi - topq + 1;
1525                     }
1526                     if (blk22) {
1527
1528 /*                    Exploit the structure of U. */
1529
1530                         i__6 = nnb << 1;
1531                         i__4 = nnb << 1;
1532                         i__7 = *lwork - pw + 1;
1533                         dorm22_("Right", "No Transpose", &nh, &i__6, &nnb, &
1534                                 nnb, &work[ppwo], &i__4, &z__[topq + j * 
1535                                 z_dim1], ldz, &work[pw], &i__7, &ierr);
1536                     } else {
1537
1538 /*                    Ignore the structure of U. */
1539
1540                         i__6 = nnb << 1;
1541                         i__4 = nnb << 1;
1542                         i__7 = nnb << 1;
1543                         dgemm_("No Transpose", "No Transpose", &nh, &i__6, &
1544                                 i__4, &c_b15, &z__[topq + j * z_dim1], ldz, &
1545                                 work[ppwo], &i__7, &c_b14, &work[pw], &nh);
1546                         i__6 = nnb << 1;
1547                         dlacpy_("All", &nh, &i__6, &work[pw], &nh, &z__[topq 
1548                                 + j * z_dim1], ldz);
1549                     }
1550                     ppwo += (nnb << 2) * nnb;
1551                 }
1552             }
1553         }
1554     }
1555
1556 /*     Use unblocked code to reduce the rest of the matrix */
1557 /*     Avoid re-initialization of modified Q and Z. */
1558
1559     *(unsigned char *)compq2 = *(unsigned char *)compq;
1560     *(unsigned char *)compz2 = *(unsigned char *)compz;
1561     if (jcol != *ilo) {
1562         if (wantq) {
1563             *(unsigned char *)compq2 = 'V';
1564         }
1565         if (wantz) {
1566             *(unsigned char *)compz2 = 'V';
1567         }
1568     }
1569
1570     if (jcol < *ihi) {
1571         dgghrd_(compq2, compz2, n, &jcol, ihi, &a[a_offset], lda, &b[b_offset]
1572                 , ldb, &q[q_offset], ldq, &z__[z_offset], ldz, &ierr);
1573     }
1574     work[1] = (doublereal) lwkopt;
1575
1576     return 0;
1577
1578 /*     End of DGGHD3 */
1579
1580 } /* dgghd3_ */
1581