C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / sgghd3.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 real c_b14 = 0.f;
518 static real c_b15 = 1.f;
519 static integer c__2 = 2;
520 static integer c__3 = 3;
521 static integer c__16 = 16;
522
523 /* > \brief \b SGGHD3 */
524
525 /*  =========== DOCUMENTATION =========== */
526
527 /* Online html documentation available at */
528 /*            http://www.netlib.org/lapack/explore-html/ */
529
530 /* > \htmlonly */
531 /* > Download SGGHRD + dependencies */
532 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgghd3.
533 f"> */
534 /* > [TGZ]</a> */
535 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgghd3.
536 f"> */
537 /* > [ZIP]</a> */
538 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgghd3.
539 f"> */
540 /* > [TXT]</a> */
541 /* > \endhtmlonly */
542
543 /*  Definition: */
544 /*  =========== */
545
546 /*       SUBROUTINE SGGHD3( 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 /*       REAL               A( LDA, * ), B( LDB, * ), Q( LDQ, * ), */
552 /*      $                   Z( LDZ, * ), WORK( * ) */
553
554
555 /* > \par Purpose: */
556 /*  ============= */
557 /* > */
558 /* > \verbatim */
559 /* > */
560 /* > SGGHD3 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 SGGHD3 reduces the original */
587 /* > problem to generalized Hessenberg form. */
588 /* > */
589 /* > This is a blocked variant of SGGHRD, 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 SGGBAL; 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 REAL 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 REAL 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 REAL 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 REAL 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 REAL 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 realOTHERcomputational */
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 sgghd3_(char *compq, char *compz, integer *n, integer *
752         ilo, integer *ihi, real *a, integer *lda, real *b, integer *ldb, real 
753         *q, integer *ldq, real *z__, integer *ldz, real *work, integer *lwork,
754          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     real r__1;
760
761     /* Local variables */
762     logical blk22;
763     integer cola, jcol, ierr;
764     real temp;
765     integer jrow, topq, ppwo;
766     extern /* Subroutine */ int srot_(integer *, real *, integer *, real *, 
767             integer *, real *, real *);
768     real temp1, temp2, temp3, c__;
769     integer kacc22, i__, j, k;
770     real s;
771     extern logical lsame_(char *, char *);
772     integer nbmin;
773     extern /* Subroutine */ int sgemm_(char *, char *, integer *, integer *, 
774             integer *, real *, real *, integer *, real *, integer *, real *, 
775             real *, integer *), sgemv_(char *, integer *, 
776             integer *, real *, real *, integer *, real *, integer *, real *, 
777             real *, integer *);
778     integer nblst;
779     logical initq;
780     real c1, c2;
781     extern /* Subroutine */ int sorm22_(char *, char *, integer *, integer *, 
782             integer *, integer *, real *, integer *, real *, integer *, real *
783             , integer *, integer *);
784     logical wantq;
785     integer j0;
786     logical initz, wantz;
787     real s1, s2;
788     extern /* Subroutine */ int strmv_(char *, char *, char *, integer *, 
789             real *, integer *, real *, integer *);
790     char compq2[1], compz2[1];
791     integer nb, jj, nh, nx, pw;
792     extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
793     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
794             integer *, integer *, ftnlen, ftnlen);
795     extern /* Subroutine */ int sgghrd_(char *, char *, integer *, integer *, 
796             integer *, real *, integer *, real *, integer *, real *, integer *
797             , real *, integer *, integer *), slaset_(char *, 
798             integer *, integer *, real *, real *, real *, integer *), 
799             slartg_(real *, real *, real *, real *, real *), slacpy_(char *, 
800             integer *, integer *, real *, integer *, real *, integer *);
801     integer lwkopt;
802     logical lquery;
803     integer nnb, len, top, ppw, n2nb;
804
805
806 /*  -- LAPACK computational routine (version 3.8.0) -- */
807 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
808 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
809 /*     January 2015 */
810
811
812
813 /*  ===================================================================== */
814
815
816 /*     Decode and test the input parameters. */
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     nb = ilaenv_(&c__1, "SGGHD3", " ", n, ilo, ihi, &c_n1, (ftnlen)6, (ftnlen)
836             1);
837 /* Computing MAX */
838     i__1 = *n * 6 * nb;
839     lwkopt = f2cmax(i__1,1);
840     work[1] = (real) lwkopt;
841     initq = lsame_(compq, "I");
842     wantq = initq || lsame_(compq, "V");
843     initz = lsame_(compz, "I");
844     wantz = initz || lsame_(compz, "V");
845     lquery = *lwork == -1;
846
847     if (! lsame_(compq, "N") && ! wantq) {
848         *info = -1;
849     } else if (! lsame_(compz, "N") && ! wantz) {
850         *info = -2;
851     } else if (*n < 0) {
852         *info = -3;
853     } else if (*ilo < 1) {
854         *info = -4;
855     } else if (*ihi > *n || *ihi < *ilo - 1) {
856         *info = -5;
857     } else if (*lda < f2cmax(1,*n)) {
858         *info = -7;
859     } else if (*ldb < f2cmax(1,*n)) {
860         *info = -9;
861     } else if (wantq && *ldq < *n || *ldq < 1) {
862         *info = -11;
863     } else if (wantz && *ldz < *n || *ldz < 1) {
864         *info = -13;
865     } else if (*lwork < 1 && ! lquery) {
866         *info = -15;
867     }
868     if (*info != 0) {
869         i__1 = -(*info);
870         xerbla_("SGGHD3", &i__1, (ftnlen)6);
871         return 0;
872     } else if (lquery) {
873         return 0;
874     }
875
876 /*     Initialize Q and Z if desired. */
877
878     if (initq) {
879         slaset_("All", n, n, &c_b14, &c_b15, &q[q_offset], ldq);
880     }
881     if (initz) {
882         slaset_("All", n, n, &c_b14, &c_b15, &z__[z_offset], ldz);
883     }
884
885 /*     Zero out lower triangle of B. */
886
887     if (*n > 1) {
888         i__1 = *n - 1;
889         i__2 = *n - 1;
890         slaset_("Lower", &i__1, &i__2, &c_b14, &c_b14, &b[b_dim1 + 2], ldb);
891     }
892
893 /*     Quick return if possible */
894
895     nh = *ihi - *ilo + 1;
896     if (nh <= 1) {
897         work[1] = 1.f;
898         return 0;
899     }
900
901 /*     Determine the blocksize. */
902
903     nbmin = ilaenv_(&c__2, "SGGHD3", " ", n, ilo, ihi, &c_n1, (ftnlen)6, (
904             ftnlen)1);
905     if (nb > 1 && nb < nh) {
906
907 /*        Determine when to use unblocked instead of blocked code. */
908
909 /* Computing MAX */
910         i__1 = nb, i__2 = ilaenv_(&c__3, "SGGHD3", " ", n, ilo, ihi, &c_n1, (
911                 ftnlen)6, (ftnlen)1);
912         nx = f2cmax(i__1,i__2);
913         if (nx < nh) {
914
915 /*           Determine if workspace is large enough for blocked code. */
916
917             if (*lwork < lwkopt) {
918
919 /*              Not enough workspace to use optimal NB:  determine the */
920 /*              minimum value of NB, and reduce NB or force use of */
921 /*              unblocked code. */
922
923 /* Computing MAX */
924                 i__1 = 2, i__2 = ilaenv_(&c__2, "SGGHD3", " ", n, ilo, ihi, &
925                         c_n1, (ftnlen)6, (ftnlen)1);
926                 nbmin = f2cmax(i__1,i__2);
927                 if (*lwork >= *n * 6 * nbmin) {
928                     nb = *lwork / (*n * 6);
929                 } else {
930                     nb = 1;
931                 }
932             }
933         }
934     }
935
936     if (nb < nbmin || nb >= nh) {
937
938 /*        Use unblocked code below */
939
940         jcol = *ilo;
941
942     } else {
943
944 /*        Use blocked code */
945
946         kacc22 = ilaenv_(&c__16, "SGGHD3", " ", n, ilo, ihi, &c_n1, (ftnlen)6,
947                  (ftnlen)1);
948         blk22 = kacc22 == 2;
949         i__1 = *ihi - 2;
950         i__2 = nb;
951         for (jcol = *ilo; i__2 < 0 ? jcol >= i__1 : jcol <= i__1; jcol += 
952                 i__2) {
953 /* Computing MIN */
954             i__3 = nb, i__4 = *ihi - jcol - 1;
955             nnb = f2cmin(i__3,i__4);
956
957 /*           Initialize small orthogonal factors that will hold the */
958 /*           accumulated Givens rotations in workspace. */
959 /*           N2NB   denotes the number of 2*NNB-by-2*NNB factors */
960 /*           NBLST  denotes the (possibly smaller) order of the last */
961 /*                  factor. */
962
963             n2nb = (*ihi - jcol - 1) / nnb - 1;
964             nblst = *ihi - jcol - n2nb * nnb;
965             slaset_("All", &nblst, &nblst, &c_b14, &c_b15, &work[1], &nblst);
966             pw = nblst * nblst + 1;
967             i__3 = n2nb;
968             for (i__ = 1; i__ <= i__3; ++i__) {
969                 i__4 = nnb << 1;
970                 i__5 = nnb << 1;
971                 i__6 = nnb << 1;
972                 slaset_("All", &i__4, &i__5, &c_b14, &c_b15, &work[pw], &i__6);
973                 pw += (nnb << 2) * nnb;
974             }
975
976 /*           Reduce columns JCOL:JCOL+NNB-1 of A to Hessenberg form. */
977
978             i__3 = jcol + nnb - 1;
979             for (j = jcol; j <= i__3; ++j) {
980
981 /*              Reduce Jth column of A. Store cosines and sines in Jth */
982 /*              column of A and B, respectively. */
983
984                 i__4 = j + 2;
985                 for (i__ = *ihi; i__ >= i__4; --i__) {
986                     temp = a[i__ - 1 + j * a_dim1];
987                     slartg_(&temp, &a[i__ + j * a_dim1], &c__, &s, &a[i__ - 1 
988                             + j * a_dim1]);
989                     a[i__ + j * a_dim1] = c__;
990                     b[i__ + j * b_dim1] = s;
991                 }
992
993 /*              Accumulate Givens rotations into workspace array. */
994
995                 ppw = (nblst + 1) * (nblst - 2) - j + jcol + 1;
996                 len = j + 2 - jcol;
997                 jrow = j + n2nb * nnb + 2;
998                 i__4 = jrow;
999                 for (i__ = *ihi; i__ >= i__4; --i__) {
1000                     c__ = a[i__ + j * a_dim1];
1001                     s = b[i__ + j * b_dim1];
1002                     i__5 = ppw + len - 1;
1003                     for (jj = ppw; jj <= i__5; ++jj) {
1004                         temp = work[jj + nblst];
1005                         work[jj + nblst] = c__ * temp - s * work[jj];
1006                         work[jj] = s * temp + c__ * work[jj];
1007                     }
1008                     ++len;
1009                     ppw = ppw - nblst - 1;
1010                 }
1011
1012                 ppwo = nblst * nblst + (nnb + j - jcol - 1 << 1) * nnb + nnb;
1013                 j0 = jrow - nnb;
1014                 i__4 = j + 2;
1015                 i__5 = -nnb;
1016                 for (jrow = j0; i__5 < 0 ? jrow >= i__4 : jrow <= i__4; jrow 
1017                         += i__5) {
1018                     ppw = ppwo;
1019                     len = j + 2 - jcol;
1020                     i__6 = jrow;
1021                     for (i__ = jrow + nnb - 1; i__ >= i__6; --i__) {
1022                         c__ = a[i__ + j * a_dim1];
1023                         s = b[i__ + j * b_dim1];
1024                         i__7 = ppw + len - 1;
1025                         for (jj = ppw; jj <= i__7; ++jj) {
1026                             temp = work[jj + (nnb << 1)];
1027                             work[jj + (nnb << 1)] = c__ * temp - s * work[jj];
1028                             work[jj] = s * temp + c__ * work[jj];
1029                         }
1030                         ++len;
1031                         ppw = ppw - (nnb << 1) - 1;
1032                     }
1033                     ppwo += (nnb << 2) * nnb;
1034                 }
1035
1036 /*              TOP denotes the number of top rows in A and B that will */
1037 /*              not be updated during the next steps. */
1038
1039                 if (jcol <= 2) {
1040                     top = 0;
1041                 } else {
1042                     top = jcol;
1043                 }
1044
1045 /*              Propagate transformations through B and replace stored */
1046 /*              left sines/cosines by right sines/cosines. */
1047
1048                 i__5 = j + 1;
1049                 for (jj = *n; jj >= i__5; --jj) {
1050
1051 /*                 Update JJth column of B. */
1052
1053 /* Computing MIN */
1054                     i__4 = jj + 1;
1055                     i__6 = j + 2;
1056                     for (i__ = f2cmin(i__4,*ihi); i__ >= i__6; --i__) {
1057                         c__ = a[i__ + j * a_dim1];
1058                         s = b[i__ + j * b_dim1];
1059                         temp = b[i__ + jj * b_dim1];
1060                         b[i__ + jj * b_dim1] = c__ * temp - s * b[i__ - 1 + 
1061                                 jj * b_dim1];
1062                         b[i__ - 1 + jj * b_dim1] = s * temp + c__ * b[i__ - 1 
1063                                 + jj * b_dim1];
1064                     }
1065
1066 /*                 Annihilate B( JJ+1, JJ ). */
1067
1068                     if (jj < *ihi) {
1069                         temp = b[jj + 1 + (jj + 1) * b_dim1];
1070                         slartg_(&temp, &b[jj + 1 + jj * b_dim1], &c__, &s, &b[
1071                                 jj + 1 + (jj + 1) * b_dim1]);
1072                         b[jj + 1 + jj * b_dim1] = 0.f;
1073                         i__6 = jj - top;
1074                         srot_(&i__6, &b[top + 1 + (jj + 1) * b_dim1], &c__1, &
1075                                 b[top + 1 + jj * b_dim1], &c__1, &c__, &s);
1076                         a[jj + 1 + j * a_dim1] = c__;
1077                         b[jj + 1 + j * b_dim1] = -s;
1078                     }
1079                 }
1080
1081 /*              Update A by transformations from right. */
1082 /*              Explicit loop unrolling provides better performance */
1083 /*              compared to SLASR. */
1084 /*               CALL SLASR( 'Right', 'Variable', 'Backward', IHI-TOP, */
1085 /*     $                     IHI-J, A( J+2, J ), B( J+2, J ), */
1086 /*     $                     A( TOP+1, J+1 ), LDA ) */
1087
1088                 jj = (*ihi - j - 1) % 3;
1089                 i__5 = jj + 1;
1090                 for (i__ = *ihi - j - 3; i__ >= i__5; i__ += -3) {
1091                     c__ = a[j + 1 + i__ + j * a_dim1];
1092                     s = -b[j + 1 + i__ + j * b_dim1];
1093                     c1 = a[j + 2 + i__ + j * a_dim1];
1094                     s1 = -b[j + 2 + i__ + j * b_dim1];
1095                     c2 = a[j + 3 + i__ + j * a_dim1];
1096                     s2 = -b[j + 3 + i__ + j * b_dim1];
1097
1098                     i__6 = *ihi;
1099                     for (k = top + 1; k <= i__6; ++k) {
1100                         temp = a[k + (j + i__) * a_dim1];
1101                         temp1 = a[k + (j + i__ + 1) * a_dim1];
1102                         temp2 = a[k + (j + i__ + 2) * a_dim1];
1103                         temp3 = a[k + (j + i__ + 3) * a_dim1];
1104                         a[k + (j + i__ + 3) * a_dim1] = c2 * temp3 + s2 * 
1105                                 temp2;
1106                         temp2 = -s2 * temp3 + c2 * temp2;
1107                         a[k + (j + i__ + 2) * a_dim1] = c1 * temp2 + s1 * 
1108                                 temp1;
1109                         temp1 = -s1 * temp2 + c1 * temp1;
1110                         a[k + (j + i__ + 1) * a_dim1] = c__ * temp1 + s * 
1111                                 temp;
1112                         a[k + (j + i__) * a_dim1] = -s * temp1 + c__ * temp;
1113                     }
1114                 }
1115
1116                 if (jj > 0) {
1117                     for (i__ = jj; i__ >= 1; --i__) {
1118                         i__5 = *ihi - top;
1119                         r__1 = -b[j + 1 + i__ + j * b_dim1];
1120                         srot_(&i__5, &a[top + 1 + (j + i__ + 1) * a_dim1], &
1121                                 c__1, &a[top + 1 + (j + i__) * a_dim1], &c__1,
1122                                  &a[j + 1 + i__ + j * a_dim1], &r__1);
1123                     }
1124                 }
1125
1126 /*              Update (J+1)th column of A by transformations from left. */
1127
1128                 if (j < jcol + nnb - 1) {
1129                     len = j + 1 - jcol;
1130
1131 /*                 Multiply with the trailing accumulated orthogonal */
1132 /*                 matrix, which takes the form */
1133
1134 /*                        [  U11  U12  ] */
1135 /*                    U = [            ], */
1136 /*                        [  U21  U22  ] */
1137
1138 /*                 where U21 is a LEN-by-LEN matrix and U12 is lower */
1139 /*                 triangular. */
1140
1141                     jrow = *ihi - nblst + 1;
1142                     sgemv_("Transpose", &nblst, &len, &c_b15, &work[1], &
1143                             nblst, &a[jrow + (j + 1) * a_dim1], &c__1, &c_b14,
1144                              &work[pw], &c__1);
1145                     ppw = pw + len;
1146                     i__5 = jrow + nblst - len - 1;
1147                     for (i__ = jrow; i__ <= i__5; ++i__) {
1148                         work[ppw] = a[i__ + (j + 1) * a_dim1];
1149                         ++ppw;
1150                     }
1151                     i__5 = nblst - len;
1152                     strmv_("Lower", "Transpose", "Non-unit", &i__5, &work[len 
1153                             * nblst + 1], &nblst, &work[pw + len], &c__1);
1154                     i__5 = nblst - len;
1155                     sgemv_("Transpose", &len, &i__5, &c_b15, &work[(len + 1) *
1156                              nblst - len + 1], &nblst, &a[jrow + nblst - len 
1157                             + (j + 1) * a_dim1], &c__1, &c_b15, &work[pw + 
1158                             len], &c__1);
1159                     ppw = pw;
1160                     i__5 = jrow + nblst - 1;
1161                     for (i__ = jrow; i__ <= i__5; ++i__) {
1162                         a[i__ + (j + 1) * a_dim1] = work[ppw];
1163                         ++ppw;
1164                     }
1165
1166 /*                 Multiply with the other accumulated orthogonal */
1167 /*                 matrices, which take the form */
1168
1169 /*                        [  U11  U12   0  ] */
1170 /*                        [                ] */
1171 /*                    U = [  U21  U22   0  ], */
1172 /*                        [                ] */
1173 /*                        [   0    0    I  ] */
1174
1175 /*                 where I denotes the (NNB-LEN)-by-(NNB-LEN) identity */
1176 /*                 matrix, U21 is a LEN-by-LEN upper triangular matrix */
1177 /*                 and U12 is an NNB-by-NNB lower triangular matrix. */
1178
1179                     ppwo = nblst * nblst + 1;
1180                     j0 = jrow - nnb;
1181                     i__5 = jcol + 1;
1182                     i__6 = -nnb;
1183                     for (jrow = j0; i__6 < 0 ? jrow >= i__5 : jrow <= i__5; 
1184                             jrow += i__6) {
1185                         ppw = pw + len;
1186                         i__4 = jrow + nnb - 1;
1187                         for (i__ = jrow; i__ <= i__4; ++i__) {
1188                             work[ppw] = a[i__ + (j + 1) * a_dim1];
1189                             ++ppw;
1190                         }
1191                         ppw = pw;
1192                         i__4 = jrow + nnb + len - 1;
1193                         for (i__ = jrow + nnb; i__ <= i__4; ++i__) {
1194                             work[ppw] = a[i__ + (j + 1) * a_dim1];
1195                             ++ppw;
1196                         }
1197                         i__4 = nnb << 1;
1198                         strmv_("Upper", "Transpose", "Non-unit", &len, &work[
1199                                 ppwo + nnb], &i__4, &work[pw], &c__1);
1200                         i__4 = nnb << 1;
1201                         strmv_("Lower", "Transpose", "Non-unit", &nnb, &work[
1202                                 ppwo + (len << 1) * nnb], &i__4, &work[pw + 
1203                                 len], &c__1);
1204                         i__4 = nnb << 1;
1205                         sgemv_("Transpose", &nnb, &len, &c_b15, &work[ppwo], &
1206                                 i__4, &a[jrow + (j + 1) * a_dim1], &c__1, &
1207                                 c_b15, &work[pw], &c__1);
1208                         i__4 = nnb << 1;
1209                         sgemv_("Transpose", &len, &nnb, &c_b15, &work[ppwo + (
1210                                 len << 1) * nnb + nnb], &i__4, &a[jrow + nnb 
1211                                 + (j + 1) * a_dim1], &c__1, &c_b15, &work[pw 
1212                                 + len], &c__1);
1213                         ppw = pw;
1214                         i__4 = jrow + len + nnb - 1;
1215                         for (i__ = jrow; i__ <= i__4; ++i__) {
1216                             a[i__ + (j + 1) * a_dim1] = work[ppw];
1217                             ++ppw;
1218                         }
1219                         ppwo += (nnb << 2) * nnb;
1220                     }
1221                 }
1222             }
1223
1224 /*           Apply accumulated orthogonal matrices to A. */
1225
1226             cola = *n - jcol - nnb + 1;
1227             j = *ihi - nblst + 1;
1228             sgemm_("Transpose", "No Transpose", &nblst, &cola, &nblst, &c_b15,
1229                      &work[1], &nblst, &a[j + (jcol + nnb) * a_dim1], lda, &
1230                     c_b14, &work[pw], &nblst);
1231             slacpy_("All", &nblst, &cola, &work[pw], &nblst, &a[j + (jcol + 
1232                     nnb) * a_dim1], lda);
1233             ppwo = nblst * nblst + 1;
1234             j0 = j - nnb;
1235             i__3 = jcol + 1;
1236             i__6 = -nnb;
1237             for (j = j0; i__6 < 0 ? j >= i__3 : j <= i__3; j += i__6) {
1238                 if (blk22) {
1239
1240 /*                 Exploit the structure of */
1241
1242 /*                        [  U11  U12  ] */
1243 /*                    U = [            ] */
1244 /*                        [  U21  U22  ], */
1245
1246 /*                 where all blocks are NNB-by-NNB, U21 is upper */
1247 /*                 triangular and U12 is lower triangular. */
1248
1249                     i__5 = nnb << 1;
1250                     i__4 = nnb << 1;
1251                     i__7 = *lwork - pw + 1;
1252                     sorm22_("Left", "Transpose", &i__5, &cola, &nnb, &nnb, &
1253                             work[ppwo], &i__4, &a[j + (jcol + nnb) * a_dim1], 
1254                             lda, &work[pw], &i__7, &ierr);
1255                 } else {
1256
1257 /*                 Ignore the structure of U. */
1258
1259                     i__5 = nnb << 1;
1260                     i__4 = nnb << 1;
1261                     i__7 = nnb << 1;
1262                     i__8 = nnb << 1;
1263                     sgemm_("Transpose", "No Transpose", &i__5, &cola, &i__4, &
1264                             c_b15, &work[ppwo], &i__7, &a[j + (jcol + nnb) * 
1265                             a_dim1], lda, &c_b14, &work[pw], &i__8);
1266                     i__5 = nnb << 1;
1267                     i__4 = nnb << 1;
1268                     slacpy_("All", &i__5, &cola, &work[pw], &i__4, &a[j + (
1269                             jcol + nnb) * a_dim1], lda);
1270                 }
1271                 ppwo += (nnb << 2) * nnb;
1272             }
1273
1274 /*           Apply accumulated orthogonal matrices to Q. */
1275
1276             if (wantq) {
1277                 j = *ihi - nblst + 1;
1278                 if (initq) {
1279 /* Computing MAX */
1280                     i__6 = 2, i__3 = j - jcol + 1;
1281                     topq = f2cmax(i__6,i__3);
1282                     nh = *ihi - topq + 1;
1283                 } else {
1284                     topq = 1;
1285                     nh = *n;
1286                 }
1287                 sgemm_("No Transpose", "No Transpose", &nh, &nblst, &nblst, &
1288                         c_b15, &q[topq + j * q_dim1], ldq, &work[1], &nblst, &
1289                         c_b14, &work[pw], &nh);
1290                 slacpy_("All", &nh, &nblst, &work[pw], &nh, &q[topq + j * 
1291                         q_dim1], ldq);
1292                 ppwo = nblst * nblst + 1;
1293                 j0 = j - nnb;
1294                 i__6 = jcol + 1;
1295                 i__3 = -nnb;
1296                 for (j = j0; i__3 < 0 ? j >= i__6 : j <= i__6; j += i__3) {
1297                     if (initq) {
1298 /* Computing MAX */
1299                         i__5 = 2, i__4 = j - jcol + 1;
1300                         topq = f2cmax(i__5,i__4);
1301                         nh = *ihi - topq + 1;
1302                     }
1303                     if (blk22) {
1304
1305 /*                    Exploit the structure of U. */
1306
1307                         i__5 = nnb << 1;
1308                         i__4 = nnb << 1;
1309                         i__7 = *lwork - pw + 1;
1310                         sorm22_("Right", "No Transpose", &nh, &i__5, &nnb, &
1311                                 nnb, &work[ppwo], &i__4, &q[topq + j * q_dim1]
1312                                 , ldq, &work[pw], &i__7, &ierr);
1313                     } else {
1314
1315 /*                    Ignore the structure of U. */
1316
1317                         i__5 = nnb << 1;
1318                         i__4 = nnb << 1;
1319                         i__7 = nnb << 1;
1320                         sgemm_("No Transpose", "No Transpose", &nh, &i__5, &
1321                                 i__4, &c_b15, &q[topq + j * q_dim1], ldq, &
1322                                 work[ppwo], &i__7, &c_b14, &work[pw], &nh);
1323                         i__5 = nnb << 1;
1324                         slacpy_("All", &nh, &i__5, &work[pw], &nh, &q[topq + 
1325                                 j * q_dim1], ldq);
1326                     }
1327                     ppwo += (nnb << 2) * nnb;
1328                 }
1329             }
1330
1331 /*           Accumulate right Givens rotations if required. */
1332
1333             if (wantz || top > 0) {
1334
1335 /*              Initialize small orthogonal factors that will hold the */
1336 /*              accumulated Givens rotations in workspace. */
1337
1338                 slaset_("All", &nblst, &nblst, &c_b14, &c_b15, &work[1], &
1339                         nblst);
1340                 pw = nblst * nblst + 1;
1341                 i__3 = n2nb;
1342                 for (i__ = 1; i__ <= i__3; ++i__) {
1343                     i__6 = nnb << 1;
1344                     i__5 = nnb << 1;
1345                     i__4 = nnb << 1;
1346                     slaset_("All", &i__6, &i__5, &c_b14, &c_b15, &work[pw], &
1347                             i__4);
1348                     pw += (nnb << 2) * nnb;
1349                 }
1350
1351 /*              Accumulate Givens rotations into workspace array. */
1352
1353                 i__3 = jcol + nnb - 1;
1354                 for (j = jcol; j <= i__3; ++j) {
1355                     ppw = (nblst + 1) * (nblst - 2) - j + jcol + 1;
1356                     len = j + 2 - jcol;
1357                     jrow = j + n2nb * nnb + 2;
1358                     i__6 = jrow;
1359                     for (i__ = *ihi; i__ >= i__6; --i__) {
1360                         c__ = a[i__ + j * a_dim1];
1361                         a[i__ + j * a_dim1] = 0.f;
1362                         s = b[i__ + j * b_dim1];
1363                         b[i__ + j * b_dim1] = 0.f;
1364                         i__5 = ppw + len - 1;
1365                         for (jj = ppw; jj <= i__5; ++jj) {
1366                             temp = work[jj + nblst];
1367                             work[jj + nblst] = c__ * temp - s * work[jj];
1368                             work[jj] = s * temp + c__ * work[jj];
1369                         }
1370                         ++len;
1371                         ppw = ppw - nblst - 1;
1372                     }
1373
1374                     ppwo = nblst * nblst + (nnb + j - jcol - 1 << 1) * nnb + 
1375                             nnb;
1376                     j0 = jrow - nnb;
1377                     i__6 = j + 2;
1378                     i__5 = -nnb;
1379                     for (jrow = j0; i__5 < 0 ? jrow >= i__6 : jrow <= i__6; 
1380                             jrow += i__5) {
1381                         ppw = ppwo;
1382                         len = j + 2 - jcol;
1383                         i__4 = jrow;
1384                         for (i__ = jrow + nnb - 1; i__ >= i__4; --i__) {
1385                             c__ = a[i__ + j * a_dim1];
1386                             a[i__ + j * a_dim1] = 0.f;
1387                             s = b[i__ + j * b_dim1];
1388                             b[i__ + j * b_dim1] = 0.f;
1389                             i__7 = ppw + len - 1;
1390                             for (jj = ppw; jj <= i__7; ++jj) {
1391                                 temp = work[jj + (nnb << 1)];
1392                                 work[jj + (nnb << 1)] = c__ * temp - s * work[
1393                                         jj];
1394                                 work[jj] = s * temp + c__ * work[jj];
1395                             }
1396                             ++len;
1397                             ppw = ppw - (nnb << 1) - 1;
1398                         }
1399                         ppwo += (nnb << 2) * nnb;
1400                     }
1401                 }
1402             } else {
1403
1404                 i__3 = *ihi - jcol - 1;
1405                 slaset_("Lower", &i__3, &nnb, &c_b14, &c_b14, &a[jcol + 2 + 
1406                         jcol * a_dim1], lda);
1407                 i__3 = *ihi - jcol - 1;
1408                 slaset_("Lower", &i__3, &nnb, &c_b14, &c_b14, &b[jcol + 2 + 
1409                         jcol * b_dim1], ldb);
1410             }
1411
1412 /*           Apply accumulated orthogonal matrices to A and B. */
1413
1414             if (top > 0) {
1415                 j = *ihi - nblst + 1;
1416                 sgemm_("No Transpose", "No Transpose", &top, &nblst, &nblst, &
1417                         c_b15, &a[j * a_dim1 + 1], lda, &work[1], &nblst, &
1418                         c_b14, &work[pw], &top);
1419                 slacpy_("All", &top, &nblst, &work[pw], &top, &a[j * a_dim1 + 
1420                         1], lda);
1421                 ppwo = nblst * nblst + 1;
1422                 j0 = j - nnb;
1423                 i__3 = jcol + 1;
1424                 i__5 = -nnb;
1425                 for (j = j0; i__5 < 0 ? j >= i__3 : j <= i__3; j += i__5) {
1426                     if (blk22) {
1427
1428 /*                    Exploit the structure of U. */
1429
1430                         i__6 = nnb << 1;
1431                         i__4 = nnb << 1;
1432                         i__7 = *lwork - pw + 1;
1433                         sorm22_("Right", "No Transpose", &top, &i__6, &nnb, &
1434                                 nnb, &work[ppwo], &i__4, &a[j * a_dim1 + 1], 
1435                                 lda, &work[pw], &i__7, &ierr);
1436                     } else {
1437
1438 /*                    Ignore the structure of U. */
1439
1440                         i__6 = nnb << 1;
1441                         i__4 = nnb << 1;
1442                         i__7 = nnb << 1;
1443                         sgemm_("No Transpose", "No Transpose", &top, &i__6, &
1444                                 i__4, &c_b15, &a[j * a_dim1 + 1], lda, &work[
1445                                 ppwo], &i__7, &c_b14, &work[pw], &top);
1446                         i__6 = nnb << 1;
1447                         slacpy_("All", &top, &i__6, &work[pw], &top, &a[j * 
1448                                 a_dim1 + 1], lda);
1449                     }
1450                     ppwo += (nnb << 2) * nnb;
1451                 }
1452
1453                 j = *ihi - nblst + 1;
1454                 sgemm_("No Transpose", "No Transpose", &top, &nblst, &nblst, &
1455                         c_b15, &b[j * b_dim1 + 1], ldb, &work[1], &nblst, &
1456                         c_b14, &work[pw], &top);
1457                 slacpy_("All", &top, &nblst, &work[pw], &top, &b[j * b_dim1 + 
1458                         1], ldb);
1459                 ppwo = nblst * nblst + 1;
1460                 j0 = j - nnb;
1461                 i__5 = jcol + 1;
1462                 i__3 = -nnb;
1463                 for (j = j0; i__3 < 0 ? j >= i__5 : j <= i__5; j += i__3) {
1464                     if (blk22) {
1465
1466 /*                    Exploit the structure of U. */
1467
1468                         i__6 = nnb << 1;
1469                         i__4 = nnb << 1;
1470                         i__7 = *lwork - pw + 1;
1471                         sorm22_("Right", "No Transpose", &top, &i__6, &nnb, &
1472                                 nnb, &work[ppwo], &i__4, &b[j * b_dim1 + 1], 
1473                                 ldb, &work[pw], &i__7, &ierr);
1474                     } else {
1475
1476 /*                    Ignore the structure of U. */
1477
1478                         i__6 = nnb << 1;
1479                         i__4 = nnb << 1;
1480                         i__7 = nnb << 1;
1481                         sgemm_("No Transpose", "No Transpose", &top, &i__6, &
1482                                 i__4, &c_b15, &b[j * b_dim1 + 1], ldb, &work[
1483                                 ppwo], &i__7, &c_b14, &work[pw], &top);
1484                         i__6 = nnb << 1;
1485                         slacpy_("All", &top, &i__6, &work[pw], &top, &b[j * 
1486                                 b_dim1 + 1], ldb);
1487                     }
1488                     ppwo += (nnb << 2) * nnb;
1489                 }
1490             }
1491
1492 /*           Apply accumulated orthogonal matrices to Z. */
1493
1494             if (wantz) {
1495                 j = *ihi - nblst + 1;
1496                 if (initq) {
1497 /* Computing MAX */
1498                     i__3 = 2, i__5 = j - jcol + 1;
1499                     topq = f2cmax(i__3,i__5);
1500                     nh = *ihi - topq + 1;
1501                 } else {
1502                     topq = 1;
1503                     nh = *n;
1504                 }
1505                 sgemm_("No Transpose", "No Transpose", &nh, &nblst, &nblst, &
1506                         c_b15, &z__[topq + j * z_dim1], ldz, &work[1], &nblst,
1507                          &c_b14, &work[pw], &nh);
1508                 slacpy_("All", &nh, &nblst, &work[pw], &nh, &z__[topq + j * 
1509                         z_dim1], ldz);
1510                 ppwo = nblst * nblst + 1;
1511                 j0 = j - nnb;
1512                 i__3 = jcol + 1;
1513                 i__5 = -nnb;
1514                 for (j = j0; i__5 < 0 ? j >= i__3 : j <= i__3; j += i__5) {
1515                     if (initq) {
1516 /* Computing MAX */
1517                         i__6 = 2, i__4 = j - jcol + 1;
1518                         topq = f2cmax(i__6,i__4);
1519                         nh = *ihi - topq + 1;
1520                     }
1521                     if (blk22) {
1522
1523 /*                    Exploit the structure of U. */
1524
1525                         i__6 = nnb << 1;
1526                         i__4 = nnb << 1;
1527                         i__7 = *lwork - pw + 1;
1528                         sorm22_("Right", "No Transpose", &nh, &i__6, &nnb, &
1529                                 nnb, &work[ppwo], &i__4, &z__[topq + j * 
1530                                 z_dim1], ldz, &work[pw], &i__7, &ierr);
1531                     } else {
1532
1533 /*                    Ignore the structure of U. */
1534
1535                         i__6 = nnb << 1;
1536                         i__4 = nnb << 1;
1537                         i__7 = nnb << 1;
1538                         sgemm_("No Transpose", "No Transpose", &nh, &i__6, &
1539                                 i__4, &c_b15, &z__[topq + j * z_dim1], ldz, &
1540                                 work[ppwo], &i__7, &c_b14, &work[pw], &nh);
1541                         i__6 = nnb << 1;
1542                         slacpy_("All", &nh, &i__6, &work[pw], &nh, &z__[topq 
1543                                 + j * z_dim1], ldz);
1544                     }
1545                     ppwo += (nnb << 2) * nnb;
1546                 }
1547             }
1548         }
1549     }
1550
1551 /*     Use unblocked code to reduce the rest of the matrix */
1552 /*     Avoid re-initialization of modified Q and Z. */
1553
1554     *(unsigned char *)compq2 = *(unsigned char *)compq;
1555     *(unsigned char *)compz2 = *(unsigned char *)compz;
1556     if (jcol != *ilo) {
1557         if (wantq) {
1558             *(unsigned char *)compq2 = 'V';
1559         }
1560         if (wantz) {
1561             *(unsigned char *)compz2 = 'V';
1562         }
1563     }
1564
1565     if (jcol < *ihi) {
1566         sgghrd_(compq2, compz2, n, &jcol, ihi, &a[a_offset], lda, &b[b_offset]
1567                 , ldb, &q[q_offset], ldq, &z__[z_offset], ldz, &ierr);
1568     }
1569     work[1] = (real) lwkopt;
1570
1571     return 0;
1572
1573 /*     End of SGGHD3 */
1574
1575 } /* sgghd3_ */
1576