C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / clahef_rk.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 complex c_b1 = {1.f,0.f};
516 static integer c__1 = 1;
517
518 /* > \brief \b CLAHEF_RK computes a partial factorization of a complex Hermitian indefinite matrix using bound
519 ed Bunch-Kaufman (rook) diagonal pivoting method. */
520
521 /*  =========== DOCUMENTATION =========== */
522
523 /* Online html documentation available at */
524 /*            http://www.netlib.org/lapack/explore-html/ */
525
526 /* > \htmlonly */
527 /* > Download CLAHEF_RK + dependencies */
528 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/clahef_
529 rk.f"> */
530 /* > [TGZ]</a> */
531 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/clahef_
532 rk.f"> */
533 /* > [ZIP]</a> */
534 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/clahef_
535 rk.f"> */
536 /* > [TXT]</a> */
537 /* > \endhtmlonly */
538
539 /*  Definition: */
540 /*  =========== */
541
542 /*       SUBROUTINE CLAHEF_RK( UPLO, N, NB, KB, A, LDA, E, IPIV, W, LDW, */
543 /*                             INFO ) */
544
545 /*       CHARACTER          UPLO */
546 /*       INTEGER            INFO, KB, LDA, LDW, N, NB */
547 /*       INTEGER            IPIV( * ) */
548 /*       COMPLEX            A( LDA, * ), E( * ), W( LDW, * ) */
549
550
551 /* > \par Purpose: */
552 /*  ============= */
553 /* > */
554 /* > \verbatim */
555 /* > CLAHEF_RK computes a partial factorization of a complex Hermitian */
556 /* > matrix A using the bounded Bunch-Kaufman (rook) diagonal */
557 /* > pivoting method. The partial factorization has the form: */
558 /* > */
559 /* > A  =  ( I  U12 ) ( A11  0  ) (  I       0    )  if UPLO = 'U', or: */
560 /* >       ( 0  U22 ) (  0   D  ) ( U12**H U22**H ) */
561 /* > */
562 /* > A  =  ( L11  0 ) (  D   0  ) ( L11**H L21**H )  if UPLO = 'L', */
563 /* >       ( L21  I ) (  0  A22 ) (  0       I    ) */
564 /* > */
565 /* > where the order of D is at most NB. The actual order is returned in */
566 /* > the argument KB, and is either NB or NB-1, or N if N <= NB. */
567 /* > */
568 /* > CLAHEF_RK is an auxiliary routine called by CHETRF_RK. It uses */
569 /* > blocked code (calling Level 3 BLAS) to update the submatrix */
570 /* > A11 (if UPLO = 'U') or A22 (if UPLO = 'L'). */
571 /* > \endverbatim */
572
573 /*  Arguments: */
574 /*  ========== */
575
576 /* > \param[in] UPLO */
577 /* > \verbatim */
578 /* >          UPLO is CHARACTER*1 */
579 /* >          Specifies whether the upper or lower triangular part of the */
580 /* >          Hermitian matrix A is stored: */
581 /* >          = 'U':  Upper triangular */
582 /* >          = 'L':  Lower triangular */
583 /* > \endverbatim */
584 /* > */
585 /* > \param[in] N */
586 /* > \verbatim */
587 /* >          N is INTEGER */
588 /* >          The order of the matrix A.  N >= 0. */
589 /* > \endverbatim */
590 /* > */
591 /* > \param[in] NB */
592 /* > \verbatim */
593 /* >          NB is INTEGER */
594 /* >          The maximum number of columns of the matrix A that should be */
595 /* >          factored.  NB should be at least 2 to allow for 2-by-2 pivot */
596 /* >          blocks. */
597 /* > \endverbatim */
598 /* > */
599 /* > \param[out] KB */
600 /* > \verbatim */
601 /* >          KB is INTEGER */
602 /* >          The number of columns of A that were actually factored. */
603 /* >          KB is either NB-1 or NB, or N if N <= NB. */
604 /* > \endverbatim */
605 /* > */
606 /* > \param[in,out] A */
607 /* > \verbatim */
608 /* >          A is COMPLEX array, dimension (LDA,N) */
609 /* >          On entry, the Hermitian matrix A. */
610 /* >            If UPLO = 'U': the leading N-by-N upper triangular part */
611 /* >            of A contains the upper triangular part of the matrix A, */
612 /* >            and the strictly lower triangular part of A is not */
613 /* >            referenced. */
614 /* > */
615 /* >            If UPLO = 'L': the leading N-by-N lower triangular part */
616 /* >            of A contains the lower triangular part of the matrix A, */
617 /* >            and the strictly upper triangular part of A is not */
618 /* >            referenced. */
619 /* > */
620 /* >          On exit, contains: */
621 /* >            a) ONLY diagonal elements of the Hermitian block diagonal */
622 /* >               matrix D on the diagonal of A, i.e. D(k,k) = A(k,k); */
623 /* >               (superdiagonal (or subdiagonal) elements of D */
624 /* >                are stored on exit in array E), and */
625 /* >            b) If UPLO = 'U': factor U in the superdiagonal part of A. */
626 /* >               If UPLO = 'L': factor L in the subdiagonal part of A. */
627 /* > \endverbatim */
628 /* > */
629 /* > \param[in] LDA */
630 /* > \verbatim */
631 /* >          LDA is INTEGER */
632 /* >          The leading dimension of the array A.  LDA >= f2cmax(1,N). */
633 /* > \endverbatim */
634 /* > */
635 /* > \param[out] E */
636 /* > \verbatim */
637 /* >          E is COMPLEX array, dimension (N) */
638 /* >          On exit, contains the superdiagonal (or subdiagonal) */
639 /* >          elements of the Hermitian block diagonal matrix D */
640 /* >          with 1-by-1 or 2-by-2 diagonal blocks, where */
641 /* >          If UPLO = 'U': E(i) = D(i-1,i), i=2:N, E(1) is set to 0; */
642 /* >          If UPLO = 'L': E(i) = D(i+1,i), i=1:N-1, E(N) is set to 0. */
643 /* > */
644 /* >          NOTE: For 1-by-1 diagonal block D(k), where */
645 /* >          1 <= k <= N, the element E(k) is set to 0 in both */
646 /* >          UPLO = 'U' or UPLO = 'L' cases. */
647 /* > \endverbatim */
648 /* > */
649 /* > \param[out] IPIV */
650 /* > \verbatim */
651 /* >          IPIV is INTEGER array, dimension (N) */
652 /* >          IPIV describes the permutation matrix P in the factorization */
653 /* >          of matrix A as follows. The absolute value of IPIV(k) */
654 /* >          represents the index of row and column that were */
655 /* >          interchanged with the k-th row and column. The value of UPLO */
656 /* >          describes the order in which the interchanges were applied. */
657 /* >          Also, the sign of IPIV represents the block structure of */
658 /* >          the Hermitian block diagonal matrix D with 1-by-1 or 2-by-2 */
659 /* >          diagonal blocks which correspond to 1 or 2 interchanges */
660 /* >          at each factorization step. */
661 /* > */
662 /* >          If UPLO = 'U', */
663 /* >          ( in factorization order, k decreases from N to 1 ): */
664 /* >            a) A single positive entry IPIV(k) > 0 means: */
665 /* >               D(k,k) is a 1-by-1 diagonal block. */
666 /* >               If IPIV(k) != k, rows and columns k and IPIV(k) were */
667 /* >               interchanged in the submatrix A(1:N,N-KB+1:N); */
668 /* >               If IPIV(k) = k, no interchange occurred. */
669 /* > */
670 /* > */
671 /* >            b) A pair of consecutive negative entries */
672 /* >               IPIV(k) < 0 and IPIV(k-1) < 0 means: */
673 /* >               D(k-1:k,k-1:k) is a 2-by-2 diagonal block. */
674 /* >               (NOTE: negative entries in IPIV appear ONLY in pairs). */
675 /* >               1) If -IPIV(k) != k, rows and columns */
676 /* >                  k and -IPIV(k) were interchanged */
677 /* >                  in the matrix A(1:N,N-KB+1:N). */
678 /* >                  If -IPIV(k) = k, no interchange occurred. */
679 /* >               2) If -IPIV(k-1) != k-1, rows and columns */
680 /* >                  k-1 and -IPIV(k-1) were interchanged */
681 /* >                  in the submatrix A(1:N,N-KB+1:N). */
682 /* >                  If -IPIV(k-1) = k-1, no interchange occurred. */
683 /* > */
684 /* >            c) In both cases a) and b) is always ABS( IPIV(k) ) <= k. */
685 /* > */
686 /* >            d) NOTE: Any entry IPIV(k) is always NONZERO on output. */
687 /* > */
688 /* >          If UPLO = 'L', */
689 /* >          ( in factorization order, k increases from 1 to N ): */
690 /* >            a) A single positive entry IPIV(k) > 0 means: */
691 /* >               D(k,k) is a 1-by-1 diagonal block. */
692 /* >               If IPIV(k) != k, rows and columns k and IPIV(k) were */
693 /* >               interchanged in the submatrix A(1:N,1:KB). */
694 /* >               If IPIV(k) = k, no interchange occurred. */
695 /* > */
696 /* >            b) A pair of consecutive negative entries */
697 /* >               IPIV(k) < 0 and IPIV(k+1) < 0 means: */
698 /* >               D(k:k+1,k:k+1) is a 2-by-2 diagonal block. */
699 /* >               (NOTE: negative entries in IPIV appear ONLY in pairs). */
700 /* >               1) If -IPIV(k) != k, rows and columns */
701 /* >                  k and -IPIV(k) were interchanged */
702 /* >                  in the submatrix A(1:N,1:KB). */
703 /* >                  If -IPIV(k) = k, no interchange occurred. */
704 /* >               2) If -IPIV(k+1) != k+1, rows and columns */
705 /* >                  k-1 and -IPIV(k-1) were interchanged */
706 /* >                  in the submatrix A(1:N,1:KB). */
707 /* >                  If -IPIV(k+1) = k+1, no interchange occurred. */
708 /* > */
709 /* >            c) In both cases a) and b) is always ABS( IPIV(k) ) >= k. */
710 /* > */
711 /* >            d) NOTE: Any entry IPIV(k) is always NONZERO on output. */
712 /* > \endverbatim */
713 /* > */
714 /* > \param[out] W */
715 /* > \verbatim */
716 /* >          W is COMPLEX array, dimension (LDW,NB) */
717 /* > \endverbatim */
718 /* > */
719 /* > \param[in] LDW */
720 /* > \verbatim */
721 /* >          LDW is INTEGER */
722 /* >          The leading dimension of the array W.  LDW >= f2cmax(1,N). */
723 /* > \endverbatim */
724 /* > */
725 /* > \param[out] INFO */
726 /* > \verbatim */
727 /* >          INFO is INTEGER */
728 /* >          = 0: successful exit */
729 /* > */
730 /* >          < 0: If INFO = -k, the k-th argument had an illegal value */
731 /* > */
732 /* >          > 0: If INFO = k, the matrix A is singular, because: */
733 /* >                 If UPLO = 'U': column k in the upper */
734 /* >                 triangular part of A contains all zeros. */
735 /* >                 If UPLO = 'L': column k in the lower */
736 /* >                 triangular part of A contains all zeros. */
737 /* > */
738 /* >               Therefore D(k,k) is exactly zero, and superdiagonal */
739 /* >               elements of column k of U (or subdiagonal elements of */
740 /* >               column k of L ) are all zeros. The factorization has */
741 /* >               been completed, but the block diagonal matrix D is */
742 /* >               exactly singular, and division by zero will occur if */
743 /* >               it is used to solve a system of equations. */
744 /* > */
745 /* >               NOTE: INFO only stores the first occurrence of */
746 /* >               a singularity, any subsequent occurrence of singularity */
747 /* >               is not stored in INFO even though the factorization */
748 /* >               always completes. */
749 /* > \endverbatim */
750
751 /*  Authors: */
752 /*  ======== */
753
754 /* > \author Univ. of Tennessee */
755 /* > \author Univ. of California Berkeley */
756 /* > \author Univ. of Colorado Denver */
757 /* > \author NAG Ltd. */
758
759 /* > \date December 2016 */
760
761 /* > \ingroup complexHEcomputational */
762
763 /* > \par Contributors: */
764 /*  ================== */
765 /* > */
766 /* > \verbatim */
767 /* > */
768 /* >  December 2016,  Igor Kozachenko, */
769 /* >                  Computer Science Division, */
770 /* >                  University of California, Berkeley */
771 /* > */
772 /* >  September 2007, Sven Hammarling, Nicholas J. Higham, Craig Lucas, */
773 /* >                  School of Mathematics, */
774 /* >                  University of Manchester */
775 /* > */
776 /* > \endverbatim */
777
778 /*  ===================================================================== */
779 /* Subroutine */ int clahef_rk_(char *uplo, integer *n, integer *nb, integer 
780         *kb, complex *a, integer *lda, complex *e, integer *ipiv, complex *w, 
781         integer *ldw, integer *info)
782 {
783     /* System generated locals */
784     integer a_dim1, a_offset, w_dim1, w_offset, i__1, i__2, i__3, i__4, i__5;
785     real r__1, r__2;
786     complex q__1, q__2, q__3, q__4, q__5;
787
788     /* Local variables */
789     logical done;
790     integer imax, jmax, j, k, p;
791     real t, alpha;
792     extern /* Subroutine */ int cgemm_(char *, char *, integer *, integer *, 
793             integer *, complex *, complex *, integer *, complex *, integer *, 
794             complex *, complex *, integer *);
795     extern logical lsame_(char *, char *);
796     extern /* Subroutine */ int cgemv_(char *, integer *, integer *, complex *
797             , complex *, integer *, complex *, integer *, complex *, complex *
798             , integer *);
799     real sfmin;
800     extern /* Subroutine */ int ccopy_(integer *, complex *, integer *, 
801             complex *, integer *);
802     integer itemp;
803     extern /* Subroutine */ int cswap_(integer *, complex *, integer *, 
804             complex *, integer *);
805     integer kstep;
806     real stemp, r1;
807     complex d11, d21, d22;
808     integer jb, ii, jj, kk, kp;
809     real absakk;
810     extern /* Subroutine */ int clacgv_(integer *, complex *, integer *);
811     integer kw;
812     extern integer icamax_(integer *, complex *, integer *);
813     extern real slamch_(char *);
814     extern /* Subroutine */ int csscal_(integer *, real *, complex *, integer 
815             *);
816     real colmax, rowmax;
817     integer kkw;
818
819
820 /*  -- LAPACK computational routine (version 3.7.0) -- */
821 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
822 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
823 /*     December 2016 */
824
825
826 /*  ===================================================================== */
827
828
829     /* Parameter adjustments */
830     a_dim1 = *lda;
831     a_offset = 1 + a_dim1 * 1;
832     a -= a_offset;
833     --e;
834     --ipiv;
835     w_dim1 = *ldw;
836     w_offset = 1 + w_dim1 * 1;
837     w -= w_offset;
838
839     /* Function Body */
840     *info = 0;
841
842 /*     Initialize ALPHA for use in choosing pivot block size. */
843
844     alpha = (sqrt(17.f) + 1.f) / 8.f;
845
846 /*     Compute machine safe minimum */
847
848     sfmin = slamch_("S");
849
850     if (lsame_(uplo, "U")) {
851
852 /*        Factorize the trailing columns of A using the upper triangle */
853 /*        of A and working backwards, and compute the matrix W = U12*D */
854 /*        for use in updating A11 (note that conjg(W) is actually stored) */
855
856 /*        Initialize the first entry of array E, where superdiagonal */
857 /*        elements of D are stored */
858
859         e[1].r = 0.f, e[1].i = 0.f;
860
861 /*        K is the main loop index, decreasing from N in steps of 1 or 2 */
862
863         k = *n;
864 L10:
865
866 /*        KW is the column of W which corresponds to column K of A */
867
868         kw = *nb + k - *n;
869
870 /*        Exit from loop */
871
872         if (k <= *n - *nb + 1 && *nb < *n || k < 1) {
873             goto L30;
874         }
875
876         kstep = 1;
877         p = k;
878
879 /*        Copy column K of A to column KW of W and update it */
880
881         if (k > 1) {
882             i__1 = k - 1;
883             ccopy_(&i__1, &a[k * a_dim1 + 1], &c__1, &w[kw * w_dim1 + 1], &
884                     c__1);
885         }
886         i__1 = k + kw * w_dim1;
887         i__2 = k + k * a_dim1;
888         r__1 = a[i__2].r;
889         w[i__1].r = r__1, w[i__1].i = 0.f;
890         if (k < *n) {
891             i__1 = *n - k;
892             q__1.r = -1.f, q__1.i = 0.f;
893             cgemv_("No transpose", &k, &i__1, &q__1, &a[(k + 1) * a_dim1 + 1],
894                      lda, &w[k + (kw + 1) * w_dim1], ldw, &c_b1, &w[kw * 
895                     w_dim1 + 1], &c__1);
896             i__1 = k + kw * w_dim1;
897             i__2 = k + kw * w_dim1;
898             r__1 = w[i__2].r;
899             w[i__1].r = r__1, w[i__1].i = 0.f;
900         }
901
902 /*        Determine rows and columns to be interchanged and whether */
903 /*        a 1-by-1 or 2-by-2 pivot block will be used */
904
905         i__1 = k + kw * w_dim1;
906         absakk = (r__1 = w[i__1].r, abs(r__1));
907
908 /*        IMAX is the row-index of the largest off-diagonal element in */
909 /*        column K, and COLMAX is its absolute value. */
910 /*        Determine both COLMAX and IMAX. */
911
912         if (k > 1) {
913             i__1 = k - 1;
914             imax = icamax_(&i__1, &w[kw * w_dim1 + 1], &c__1);
915             i__1 = imax + kw * w_dim1;
916             colmax = (r__1 = w[i__1].r, abs(r__1)) + (r__2 = r_imag(&w[imax + 
917                     kw * w_dim1]), abs(r__2));
918         } else {
919             colmax = 0.f;
920         }
921
922         if (f2cmax(absakk,colmax) == 0.f) {
923
924 /*           Column K is zero or underflow: set INFO and continue */
925
926             if (*info == 0) {
927                 *info = k;
928             }
929             kp = k;
930             i__1 = k + k * a_dim1;
931             i__2 = k + kw * w_dim1;
932             r__1 = w[i__2].r;
933             a[i__1].r = r__1, a[i__1].i = 0.f;
934             if (k > 1) {
935                 i__1 = k - 1;
936                 ccopy_(&i__1, &w[kw * w_dim1 + 1], &c__1, &a[k * a_dim1 + 1], 
937                         &c__1);
938             }
939
940 /*           Set E( K ) to zero */
941
942             if (k > 1) {
943                 i__1 = k;
944                 e[i__1].r = 0.f, e[i__1].i = 0.f;
945             }
946
947         } else {
948
949 /*           ============================================================ */
950
951 /*           BEGIN pivot search */
952
953 /*           Case(1) */
954 /*           Equivalent to testing for ABSAKK.GE.ALPHA*COLMAX */
955 /*           (used to handle NaN and Inf) */
956             if (! (absakk < alpha * colmax)) {
957
958 /*              no interchange, use 1-by-1 pivot block */
959
960                 kp = k;
961
962             } else {
963
964 /*              Lop until pivot found */
965
966                 done = FALSE_;
967
968 L12:
969
970 /*                 BEGIN pivot search loop body */
971
972
973 /*                 Copy column IMAX to column KW-1 of W and update it */
974
975                 if (imax > 1) {
976                     i__1 = imax - 1;
977                     ccopy_(&i__1, &a[imax * a_dim1 + 1], &c__1, &w[(kw - 1) * 
978                             w_dim1 + 1], &c__1);
979                 }
980                 i__1 = imax + (kw - 1) * w_dim1;
981                 i__2 = imax + imax * a_dim1;
982                 r__1 = a[i__2].r;
983                 w[i__1].r = r__1, w[i__1].i = 0.f;
984
985                 i__1 = k - imax;
986                 ccopy_(&i__1, &a[imax + (imax + 1) * a_dim1], lda, &w[imax + 
987                         1 + (kw - 1) * w_dim1], &c__1);
988                 i__1 = k - imax;
989                 clacgv_(&i__1, &w[imax + 1 + (kw - 1) * w_dim1], &c__1);
990
991                 if (k < *n) {
992                     i__1 = *n - k;
993                     q__1.r = -1.f, q__1.i = 0.f;
994                     cgemv_("No transpose", &k, &i__1, &q__1, &a[(k + 1) * 
995                             a_dim1 + 1], lda, &w[imax + (kw + 1) * w_dim1], 
996                             ldw, &c_b1, &w[(kw - 1) * w_dim1 + 1], &c__1);
997                     i__1 = imax + (kw - 1) * w_dim1;
998                     i__2 = imax + (kw - 1) * w_dim1;
999                     r__1 = w[i__2].r;
1000                     w[i__1].r = r__1, w[i__1].i = 0.f;
1001                 }
1002
1003 /*                 JMAX is the column-index of the largest off-diagonal */
1004 /*                 element in row IMAX, and ROWMAX is its absolute value. */
1005 /*                 Determine both ROWMAX and JMAX. */
1006
1007                 if (imax != k) {
1008                     i__1 = k - imax;
1009                     jmax = imax + icamax_(&i__1, &w[imax + 1 + (kw - 1) * 
1010                             w_dim1], &c__1);
1011                     i__1 = jmax + (kw - 1) * w_dim1;
1012                     rowmax = (r__1 = w[i__1].r, abs(r__1)) + (r__2 = r_imag(&
1013                             w[jmax + (kw - 1) * w_dim1]), abs(r__2));
1014                 } else {
1015                     rowmax = 0.f;
1016                 }
1017
1018                 if (imax > 1) {
1019                     i__1 = imax - 1;
1020                     itemp = icamax_(&i__1, &w[(kw - 1) * w_dim1 + 1], &c__1);
1021                     i__1 = itemp + (kw - 1) * w_dim1;
1022                     stemp = (r__1 = w[i__1].r, abs(r__1)) + (r__2 = r_imag(&w[
1023                             itemp + (kw - 1) * w_dim1]), abs(r__2));
1024                     if (stemp > rowmax) {
1025                         rowmax = stemp;
1026                         jmax = itemp;
1027                     }
1028                 }
1029
1030 /*                 Case(2) */
1031 /*                 Equivalent to testing for */
1032 /*                 ABS( REAL( W( IMAX,KW-1 ) ) ).GE.ALPHA*ROWMAX */
1033 /*                 (used to handle NaN and Inf) */
1034
1035                 i__1 = imax + (kw - 1) * w_dim1;
1036                 if (! ((r__1 = w[i__1].r, abs(r__1)) < alpha * rowmax)) {
1037
1038 /*                    interchange rows and columns K and IMAX, */
1039 /*                    use 1-by-1 pivot block */
1040
1041                     kp = imax;
1042
1043 /*                    copy column KW-1 of W to column KW of W */
1044
1045                     ccopy_(&k, &w[(kw - 1) * w_dim1 + 1], &c__1, &w[kw * 
1046                             w_dim1 + 1], &c__1);
1047
1048                     done = TRUE_;
1049
1050 /*                 Case(3) */
1051 /*                 Equivalent to testing for ROWMAX.EQ.COLMAX, */
1052 /*                 (used to handle NaN and Inf) */
1053
1054                 } else if (p == jmax || rowmax <= colmax) {
1055
1056 /*                    interchange rows and columns K-1 and IMAX, */
1057 /*                    use 2-by-2 pivot block */
1058
1059                     kp = imax;
1060                     kstep = 2;
1061                     done = TRUE_;
1062
1063 /*                 Case(4) */
1064                 } else {
1065
1066 /*                    Pivot not found: set params and repeat */
1067
1068                     p = imax;
1069                     colmax = rowmax;
1070                     imax = jmax;
1071
1072 /*                    Copy updated JMAXth (next IMAXth) column to Kth of W */
1073
1074                     ccopy_(&k, &w[(kw - 1) * w_dim1 + 1], &c__1, &w[kw * 
1075                             w_dim1 + 1], &c__1);
1076
1077                 }
1078
1079
1080 /*                 END pivot search loop body */
1081
1082                 if (! done) {
1083                     goto L12;
1084                 }
1085
1086             }
1087
1088 /*           END pivot search */
1089
1090 /*           ============================================================ */
1091
1092 /*           KK is the column of A where pivoting step stopped */
1093
1094             kk = k - kstep + 1;
1095
1096 /*           KKW is the column of W which corresponds to column KK of A */
1097
1098             kkw = *nb + kk - *n;
1099
1100 /*           Interchange rows and columns P and K. */
1101 /*           Updated column P is already stored in column KW of W. */
1102
1103             if (kstep == 2 && p != k) {
1104
1105 /*              Copy non-updated column K to column P of submatrix A */
1106 /*              at step K. No need to copy element into columns */
1107 /*              K and K-1 of A for 2-by-2 pivot, since these columns */
1108 /*              will be later overwritten. */
1109
1110                 i__1 = p + p * a_dim1;
1111                 i__2 = k + k * a_dim1;
1112                 r__1 = a[i__2].r;
1113                 a[i__1].r = r__1, a[i__1].i = 0.f;
1114                 i__1 = k - 1 - p;
1115                 ccopy_(&i__1, &a[p + 1 + k * a_dim1], &c__1, &a[p + (p + 1) * 
1116                         a_dim1], lda);
1117                 i__1 = k - 1 - p;
1118                 clacgv_(&i__1, &a[p + (p + 1) * a_dim1], lda);
1119                 if (p > 1) {
1120                     i__1 = p - 1;
1121                     ccopy_(&i__1, &a[k * a_dim1 + 1], &c__1, &a[p * a_dim1 + 
1122                             1], &c__1);
1123                 }
1124
1125 /*              Interchange rows K and P in the last K+1 to N columns of A */
1126 /*              (columns K and K-1 of A for 2-by-2 pivot will be */
1127 /*              later overwritten). Interchange rows K and P */
1128 /*              in last KKW to NB columns of W. */
1129
1130                 if (k < *n) {
1131                     i__1 = *n - k;
1132                     cswap_(&i__1, &a[k + (k + 1) * a_dim1], lda, &a[p + (k + 
1133                             1) * a_dim1], lda);
1134                 }
1135                 i__1 = *n - kk + 1;
1136                 cswap_(&i__1, &w[k + kkw * w_dim1], ldw, &w[p + kkw * w_dim1],
1137                          ldw);
1138             }
1139
1140 /*           Interchange rows and columns KP and KK. */
1141 /*           Updated column KP is already stored in column KKW of W. */
1142
1143             if (kp != kk) {
1144
1145 /*              Copy non-updated column KK to column KP of submatrix A */
1146 /*              at step K. No need to copy element into column K */
1147 /*              (or K and K-1 for 2-by-2 pivot) of A, since these columns */
1148 /*              will be later overwritten. */
1149
1150                 i__1 = kp + kp * a_dim1;
1151                 i__2 = kk + kk * a_dim1;
1152                 r__1 = a[i__2].r;
1153                 a[i__1].r = r__1, a[i__1].i = 0.f;
1154                 i__1 = kk - 1 - kp;
1155                 ccopy_(&i__1, &a[kp + 1 + kk * a_dim1], &c__1, &a[kp + (kp + 
1156                         1) * a_dim1], lda);
1157                 i__1 = kk - 1 - kp;
1158                 clacgv_(&i__1, &a[kp + (kp + 1) * a_dim1], lda);
1159                 if (kp > 1) {
1160                     i__1 = kp - 1;
1161                     ccopy_(&i__1, &a[kk * a_dim1 + 1], &c__1, &a[kp * a_dim1 
1162                             + 1], &c__1);
1163                 }
1164
1165 /*              Interchange rows KK and KP in last K+1 to N columns of A */
1166 /*              (columns K (or K and K-1 for 2-by-2 pivot) of A will be */
1167 /*              later overwritten). Interchange rows KK and KP */
1168 /*              in last KKW to NB columns of W. */
1169
1170                 if (k < *n) {
1171                     i__1 = *n - k;
1172                     cswap_(&i__1, &a[kk + (k + 1) * a_dim1], lda, &a[kp + (k 
1173                             + 1) * a_dim1], lda);
1174                 }
1175                 i__1 = *n - kk + 1;
1176                 cswap_(&i__1, &w[kk + kkw * w_dim1], ldw, &w[kp + kkw * 
1177                         w_dim1], ldw);
1178             }
1179
1180             if (kstep == 1) {
1181
1182 /*              1-by-1 pivot block D(k): column kw of W now holds */
1183
1184 /*              W(kw) = U(k)*D(k), */
1185
1186 /*              where U(k) is the k-th column of U */
1187
1188 /*              (1) Store subdiag. elements of column U(k) */
1189 /*              and 1-by-1 block D(k) in column k of A. */
1190 /*              (NOTE: Diagonal element U(k,k) is a UNIT element */
1191 /*              and not stored) */
1192 /*                 A(k,k) := D(k,k) = W(k,kw) */
1193 /*                 A(1:k-1,k) := U(1:k-1,k) = W(1:k-1,kw)/D(k,k) */
1194
1195 /*              (NOTE: No need to use for Hermitian matrix */
1196 /*              A( K, K ) = REAL( W( K, K) ) to separately copy diagonal */
1197 /*              element D(k,k) from W (potentially saves only one load)) */
1198                 ccopy_(&k, &w[kw * w_dim1 + 1], &c__1, &a[k * a_dim1 + 1], &
1199                         c__1);
1200                 if (k > 1) {
1201
1202 /*                 (NOTE: No need to check if A(k,k) is NOT ZERO, */
1203 /*                  since that was ensured earlier in pivot search: */
1204 /*                  case A(k,k) = 0 falls into 2x2 pivot case(3)) */
1205
1206 /*                 Handle division by a small number */
1207
1208                     i__1 = k + k * a_dim1;
1209                     t = a[i__1].r;
1210                     if (abs(t) >= sfmin) {
1211                         r1 = 1.f / t;
1212                         i__1 = k - 1;
1213                         csscal_(&i__1, &r1, &a[k * a_dim1 + 1], &c__1);
1214                     } else {
1215                         i__1 = k - 1;
1216                         for (ii = 1; ii <= i__1; ++ii) {
1217                             i__2 = ii + k * a_dim1;
1218                             i__3 = ii + k * a_dim1;
1219                             q__1.r = a[i__3].r / t, q__1.i = a[i__3].i / t;
1220                             a[i__2].r = q__1.r, a[i__2].i = q__1.i;
1221 /* L14: */
1222                         }
1223                     }
1224
1225 /*                 (2) Conjugate column W(kw) */
1226
1227                     i__1 = k - 1;
1228                     clacgv_(&i__1, &w[kw * w_dim1 + 1], &c__1);
1229
1230 /*                 Store the superdiagonal element of D in array E */
1231
1232                     i__1 = k;
1233                     e[i__1].r = 0.f, e[i__1].i = 0.f;
1234
1235                 }
1236
1237             } else {
1238
1239 /*              2-by-2 pivot block D(k): columns kw and kw-1 of W now hold */
1240
1241 /*              ( W(kw-1) W(kw) ) = ( U(k-1) U(k) )*D(k) */
1242
1243 /*              where U(k) and U(k-1) are the k-th and (k-1)-th columns */
1244 /*              of U */
1245
1246 /*              (1) Store U(1:k-2,k-1) and U(1:k-2,k) and 2-by-2 */
1247 /*              block D(k-1:k,k-1:k) in columns k-1 and k of A. */
1248 /*              (NOTE: 2-by-2 diagonal block U(k-1:k,k-1:k) is a UNIT */
1249 /*              block and not stored) */
1250 /*                 A(k-1:k,k-1:k) := D(k-1:k,k-1:k) = W(k-1:k,kw-1:kw) */
1251 /*                 A(1:k-2,k-1:k) := U(1:k-2,k:k-1:k) = */
1252 /*                 = W(1:k-2,kw-1:kw) * ( D(k-1:k,k-1:k)**(-1) ) */
1253
1254                 if (k > 2) {
1255
1256 /*                 Factor out the columns of the inverse of 2-by-2 pivot */
1257 /*                 block D, so that each column contains 1, to reduce the */
1258 /*                 number of FLOPS when we multiply panel */
1259 /*                 ( W(kw-1) W(kw) ) by this inverse, i.e. by D**(-1). */
1260
1261 /*                 D**(-1) = ( d11 cj(d21) )**(-1) = */
1262 /*                           ( d21    d22 ) */
1263
1264 /*                 = 1/(d11*d22-|d21|**2) * ( ( d22) (-cj(d21) ) ) = */
1265 /*                                          ( (-d21) (     d11 ) ) */
1266
1267 /*                 = 1/(|d21|**2) * 1/((d11/cj(d21))*(d22/d21)-1) * */
1268
1269 /*                   * ( d21*( d22/d21 ) conj(d21)*(           - 1 ) ) = */
1270 /*                     (     (      -1 )           ( d11/conj(d21) ) ) */
1271
1272 /*                 = 1/(|d21|**2) * 1/(D22*D11-1) * */
1273
1274 /*                   * ( d21*( D11 ) conj(d21)*(  -1 ) ) = */
1275 /*                     (     (  -1 )           ( D22 ) ) */
1276
1277 /*                 = (1/|d21|**2) * T * ( d21*( D11 ) conj(d21)*(  -1 ) ) = */
1278 /*                                      (     (  -1 )           ( D22 ) ) */
1279
1280 /*                 = ( (T/conj(d21))*( D11 ) (T/d21)*(  -1 ) ) = */
1281 /*                   (               (  -1 )         ( D22 ) ) */
1282
1283 /*                 Handle division by a small number. (NOTE: order of */
1284 /*                 operations is important) */
1285
1286 /*                 = ( T*(( D11 )/conj(D21)) T*((  -1 )/D21 ) ) */
1287 /*                   (   ((  -1 )          )   (( D22 )     ) ), */
1288
1289 /*                 where D11 = d22/d21, */
1290 /*                       D22 = d11/conj(d21), */
1291 /*                       D21 = d21, */
1292 /*                       T = 1/(D22*D11-1). */
1293
1294 /*                 (NOTE: No need to check for division by ZERO, */
1295 /*                  since that was ensured earlier in pivot search: */
1296 /*                  (a) d21 != 0 in 2x2 pivot case(4), */
1297 /*                      since |d21| should be larger than |d11| and |d22|; */
1298 /*                  (b) (D22*D11 - 1) != 0, since from (a), */
1299 /*                      both |D11| < 1, |D22| < 1, hence |D22*D11| << 1.) */
1300
1301                     i__1 = k - 1 + kw * w_dim1;
1302                     d21.r = w[i__1].r, d21.i = w[i__1].i;
1303                     r_cnjg(&q__2, &d21);
1304                     c_div(&q__1, &w[k + kw * w_dim1], &q__2);
1305                     d11.r = q__1.r, d11.i = q__1.i;
1306                     c_div(&q__1, &w[k - 1 + (kw - 1) * w_dim1], &d21);
1307                     d22.r = q__1.r, d22.i = q__1.i;
1308                     q__1.r = d11.r * d22.r - d11.i * d22.i, q__1.i = d11.r * 
1309                             d22.i + d11.i * d22.r;
1310                     t = 1.f / (q__1.r - 1.f);
1311
1312 /*                 Update elements in columns A(k-1) and A(k) as */
1313 /*                 dot products of rows of ( W(kw-1) W(kw) ) and columns */
1314 /*                 of D**(-1) */
1315
1316                     i__1 = k - 2;
1317                     for (j = 1; j <= i__1; ++j) {
1318                         i__2 = j + (k - 1) * a_dim1;
1319                         i__3 = j + (kw - 1) * w_dim1;
1320                         q__4.r = d11.r * w[i__3].r - d11.i * w[i__3].i, 
1321                                 q__4.i = d11.r * w[i__3].i + d11.i * w[i__3]
1322                                 .r;
1323                         i__4 = j + kw * w_dim1;
1324                         q__3.r = q__4.r - w[i__4].r, q__3.i = q__4.i - w[i__4]
1325                                 .i;
1326                         c_div(&q__2, &q__3, &d21);
1327                         q__1.r = t * q__2.r, q__1.i = t * q__2.i;
1328                         a[i__2].r = q__1.r, a[i__2].i = q__1.i;
1329                         i__2 = j + k * a_dim1;
1330                         i__3 = j + kw * w_dim1;
1331                         q__4.r = d22.r * w[i__3].r - d22.i * w[i__3].i, 
1332                                 q__4.i = d22.r * w[i__3].i + d22.i * w[i__3]
1333                                 .r;
1334                         i__4 = j + (kw - 1) * w_dim1;
1335                         q__3.r = q__4.r - w[i__4].r, q__3.i = q__4.i - w[i__4]
1336                                 .i;
1337                         r_cnjg(&q__5, &d21);
1338                         c_div(&q__2, &q__3, &q__5);
1339                         q__1.r = t * q__2.r, q__1.i = t * q__2.i;
1340                         a[i__2].r = q__1.r, a[i__2].i = q__1.i;
1341 /* L20: */
1342                     }
1343                 }
1344
1345 /*              Copy diagonal elements of D(K) to A, */
1346 /*              copy superdiagonal element of D(K) to E(K) and */
1347 /*              ZERO out superdiagonal entry of A */
1348
1349                 i__1 = k - 1 + (k - 1) * a_dim1;
1350                 i__2 = k - 1 + (kw - 1) * w_dim1;
1351                 a[i__1].r = w[i__2].r, a[i__1].i = w[i__2].i;
1352                 i__1 = k - 1 + k * a_dim1;
1353                 a[i__1].r = 0.f, a[i__1].i = 0.f;
1354                 i__1 = k + k * a_dim1;
1355                 i__2 = k + kw * w_dim1;
1356                 a[i__1].r = w[i__2].r, a[i__1].i = w[i__2].i;
1357                 i__1 = k;
1358                 i__2 = k - 1 + kw * w_dim1;
1359                 e[i__1].r = w[i__2].r, e[i__1].i = w[i__2].i;
1360                 i__1 = k - 1;
1361                 e[i__1].r = 0.f, e[i__1].i = 0.f;
1362
1363 /*              (2) Conjugate columns W(kw) and W(kw-1) */
1364
1365                 i__1 = k - 1;
1366                 clacgv_(&i__1, &w[kw * w_dim1 + 1], &c__1);
1367                 i__1 = k - 2;
1368                 clacgv_(&i__1, &w[(kw - 1) * w_dim1 + 1], &c__1);
1369
1370             }
1371
1372 /*           End column K is nonsingular */
1373
1374         }
1375
1376 /*        Store details of the interchanges in IPIV */
1377
1378         if (kstep == 1) {
1379             ipiv[k] = kp;
1380         } else {
1381             ipiv[k] = -p;
1382             ipiv[k - 1] = -kp;
1383         }
1384
1385 /*        Decrease K and return to the start of the main loop */
1386
1387         k -= kstep;
1388         goto L10;
1389
1390 L30:
1391
1392 /*        Update the upper triangle of A11 (= A(1:k,1:k)) as */
1393
1394 /*        A11 := A11 - U12*D*U12**H = A11 - U12*W**H */
1395
1396 /*        computing blocks of NB columns at a time (note that conjg(W) is */
1397 /*        actually stored) */
1398
1399         i__1 = -(*nb);
1400         for (j = (k - 1) / *nb * *nb + 1; i__1 < 0 ? j >= 1 : j <= 1; j += 
1401                 i__1) {
1402 /* Computing MIN */
1403             i__2 = *nb, i__3 = k - j + 1;
1404             jb = f2cmin(i__2,i__3);
1405
1406 /*           Update the upper triangle of the diagonal block */
1407
1408             i__2 = j + jb - 1;
1409             for (jj = j; jj <= i__2; ++jj) {
1410                 i__3 = jj + jj * a_dim1;
1411                 i__4 = jj + jj * a_dim1;
1412                 r__1 = a[i__4].r;
1413                 a[i__3].r = r__1, a[i__3].i = 0.f;
1414                 i__3 = jj - j + 1;
1415                 i__4 = *n - k;
1416                 q__1.r = -1.f, q__1.i = 0.f;
1417                 cgemv_("No transpose", &i__3, &i__4, &q__1, &a[j + (k + 1) * 
1418                         a_dim1], lda, &w[jj + (kw + 1) * w_dim1], ldw, &c_b1, 
1419                         &a[j + jj * a_dim1], &c__1);
1420                 i__3 = jj + jj * a_dim1;
1421                 i__4 = jj + jj * a_dim1;
1422                 r__1 = a[i__4].r;
1423                 a[i__3].r = r__1, a[i__3].i = 0.f;
1424 /* L40: */
1425             }
1426
1427 /*           Update the rectangular superdiagonal block */
1428
1429             if (j >= 2) {
1430                 i__2 = j - 1;
1431                 i__3 = *n - k;
1432                 q__1.r = -1.f, q__1.i = 0.f;
1433                 cgemm_("No transpose", "Transpose", &i__2, &jb, &i__3, &q__1, 
1434                         &a[(k + 1) * a_dim1 + 1], lda, &w[j + (kw + 1) * 
1435                         w_dim1], ldw, &c_b1, &a[j * a_dim1 + 1], lda);
1436             }
1437 /* L50: */
1438         }
1439
1440 /*        Set KB to the number of columns factorized */
1441
1442         *kb = *n - k;
1443
1444     } else {
1445
1446 /*        Factorize the leading columns of A using the lower triangle */
1447 /*        of A and working forwards, and compute the matrix W = L21*D */
1448 /*        for use in updating A22 (note that conjg(W) is actually stored) */
1449
1450 /*        Initialize the unused last entry of the subdiagonal array E. */
1451
1452         i__1 = *n;
1453         e[i__1].r = 0.f, e[i__1].i = 0.f;
1454
1455 /*        K is the main loop index, increasing from 1 in steps of 1 or 2 */
1456
1457         k = 1;
1458 L70:
1459
1460 /*        Exit from loop */
1461
1462         if (k >= *nb && *nb < *n || k > *n) {
1463             goto L90;
1464         }
1465
1466         kstep = 1;
1467         p = k;
1468
1469 /*        Copy column K of A to column K of W and update column K of W */
1470
1471         i__1 = k + k * w_dim1;
1472         i__2 = k + k * a_dim1;
1473         r__1 = a[i__2].r;
1474         w[i__1].r = r__1, w[i__1].i = 0.f;
1475         if (k < *n) {
1476             i__1 = *n - k;
1477             ccopy_(&i__1, &a[k + 1 + k * a_dim1], &c__1, &w[k + 1 + k * 
1478                     w_dim1], &c__1);
1479         }
1480         if (k > 1) {
1481             i__1 = *n - k + 1;
1482             i__2 = k - 1;
1483             q__1.r = -1.f, q__1.i = 0.f;
1484             cgemv_("No transpose", &i__1, &i__2, &q__1, &a[k + a_dim1], lda, &
1485                     w[k + w_dim1], ldw, &c_b1, &w[k + k * w_dim1], &c__1);
1486             i__1 = k + k * w_dim1;
1487             i__2 = k + k * w_dim1;
1488             r__1 = w[i__2].r;
1489             w[i__1].r = r__1, w[i__1].i = 0.f;
1490         }
1491
1492 /*        Determine rows and columns to be interchanged and whether */
1493 /*        a 1-by-1 or 2-by-2 pivot block will be used */
1494
1495         i__1 = k + k * w_dim1;
1496         absakk = (r__1 = w[i__1].r, abs(r__1));
1497
1498 /*        IMAX is the row-index of the largest off-diagonal element in */
1499 /*        column K, and COLMAX is its absolute value. */
1500 /*        Determine both COLMAX and IMAX. */
1501
1502         if (k < *n) {
1503             i__1 = *n - k;
1504             imax = k + icamax_(&i__1, &w[k + 1 + k * w_dim1], &c__1);
1505             i__1 = imax + k * w_dim1;
1506             colmax = (r__1 = w[i__1].r, abs(r__1)) + (r__2 = r_imag(&w[imax + 
1507                     k * w_dim1]), abs(r__2));
1508         } else {
1509             colmax = 0.f;
1510         }
1511
1512         if (f2cmax(absakk,colmax) == 0.f) {
1513
1514 /*           Column K is zero or underflow: set INFO and continue */
1515
1516             if (*info == 0) {
1517                 *info = k;
1518             }
1519             kp = k;
1520             i__1 = k + k * a_dim1;
1521             i__2 = k + k * w_dim1;
1522             r__1 = w[i__2].r;
1523             a[i__1].r = r__1, a[i__1].i = 0.f;
1524             if (k < *n) {
1525                 i__1 = *n - k;
1526                 ccopy_(&i__1, &w[k + 1 + k * w_dim1], &c__1, &a[k + 1 + k * 
1527                         a_dim1], &c__1);
1528             }
1529
1530 /*           Set E( K ) to zero */
1531
1532             if (k < *n) {
1533                 i__1 = k;
1534                 e[i__1].r = 0.f, e[i__1].i = 0.f;
1535             }
1536
1537         } else {
1538
1539 /*           ============================================================ */
1540
1541 /*           BEGIN pivot search */
1542
1543 /*           Case(1) */
1544 /*           Equivalent to testing for ABSAKK.GE.ALPHA*COLMAX */
1545 /*           (used to handle NaN and Inf) */
1546
1547             if (! (absakk < alpha * colmax)) {
1548
1549 /*              no interchange, use 1-by-1 pivot block */
1550
1551                 kp = k;
1552
1553             } else {
1554
1555                 done = FALSE_;
1556
1557 /*              Loop until pivot found */
1558
1559 L72:
1560
1561 /*                 BEGIN pivot search loop body */
1562
1563
1564 /*                 Copy column IMAX to column k+1 of W and update it */
1565
1566                 i__1 = imax - k;
1567                 ccopy_(&i__1, &a[imax + k * a_dim1], lda, &w[k + (k + 1) * 
1568                         w_dim1], &c__1);
1569                 i__1 = imax - k;
1570                 clacgv_(&i__1, &w[k + (k + 1) * w_dim1], &c__1);
1571                 i__1 = imax + (k + 1) * w_dim1;
1572                 i__2 = imax + imax * a_dim1;
1573                 r__1 = a[i__2].r;
1574                 w[i__1].r = r__1, w[i__1].i = 0.f;
1575
1576                 if (imax < *n) {
1577                     i__1 = *n - imax;
1578                     ccopy_(&i__1, &a[imax + 1 + imax * a_dim1], &c__1, &w[
1579                             imax + 1 + (k + 1) * w_dim1], &c__1);
1580                 }
1581
1582                 if (k > 1) {
1583                     i__1 = *n - k + 1;
1584                     i__2 = k - 1;
1585                     q__1.r = -1.f, q__1.i = 0.f;
1586                     cgemv_("No transpose", &i__1, &i__2, &q__1, &a[k + a_dim1]
1587                             , lda, &w[imax + w_dim1], ldw, &c_b1, &w[k + (k + 
1588                             1) * w_dim1], &c__1);
1589                     i__1 = imax + (k + 1) * w_dim1;
1590                     i__2 = imax + (k + 1) * w_dim1;
1591                     r__1 = w[i__2].r;
1592                     w[i__1].r = r__1, w[i__1].i = 0.f;
1593                 }
1594
1595 /*                 JMAX is the column-index of the largest off-diagonal */
1596 /*                 element in row IMAX, and ROWMAX is its absolute value. */
1597 /*                 Determine both ROWMAX and JMAX. */
1598
1599                 if (imax != k) {
1600                     i__1 = imax - k;
1601                     jmax = k - 1 + icamax_(&i__1, &w[k + (k + 1) * w_dim1], &
1602                             c__1);
1603                     i__1 = jmax + (k + 1) * w_dim1;
1604                     rowmax = (r__1 = w[i__1].r, abs(r__1)) + (r__2 = r_imag(&
1605                             w[jmax + (k + 1) * w_dim1]), abs(r__2));
1606                 } else {
1607                     rowmax = 0.f;
1608                 }
1609
1610                 if (imax < *n) {
1611                     i__1 = *n - imax;
1612                     itemp = imax + icamax_(&i__1, &w[imax + 1 + (k + 1) * 
1613                             w_dim1], &c__1);
1614                     i__1 = itemp + (k + 1) * w_dim1;
1615                     stemp = (r__1 = w[i__1].r, abs(r__1)) + (r__2 = r_imag(&w[
1616                             itemp + (k + 1) * w_dim1]), abs(r__2));
1617                     if (stemp > rowmax) {
1618                         rowmax = stemp;
1619                         jmax = itemp;
1620                     }
1621                 }
1622
1623 /*                 Case(2) */
1624 /*                 Equivalent to testing for */
1625 /*                 ABS( REAL( W( IMAX,K+1 ) ) ).GE.ALPHA*ROWMAX */
1626 /*                 (used to handle NaN and Inf) */
1627
1628                 i__1 = imax + (k + 1) * w_dim1;
1629                 if (! ((r__1 = w[i__1].r, abs(r__1)) < alpha * rowmax)) {
1630
1631 /*                    interchange rows and columns K and IMAX, */
1632 /*                    use 1-by-1 pivot block */
1633
1634                     kp = imax;
1635
1636 /*                    copy column K+1 of W to column K of W */
1637
1638                     i__1 = *n - k + 1;
1639                     ccopy_(&i__1, &w[k + (k + 1) * w_dim1], &c__1, &w[k + k * 
1640                             w_dim1], &c__1);
1641
1642                     done = TRUE_;
1643
1644 /*                 Case(3) */
1645 /*                 Equivalent to testing for ROWMAX.EQ.COLMAX, */
1646 /*                 (used to handle NaN and Inf) */
1647
1648                 } else if (p == jmax || rowmax <= colmax) {
1649
1650 /*                    interchange rows and columns K+1 and IMAX, */
1651 /*                    use 2-by-2 pivot block */
1652
1653                     kp = imax;
1654                     kstep = 2;
1655                     done = TRUE_;
1656
1657 /*                 Case(4) */
1658                 } else {
1659
1660 /*                    Pivot not found: set params and repeat */
1661
1662                     p = imax;
1663                     colmax = rowmax;
1664                     imax = jmax;
1665
1666 /*                    Copy updated JMAXth (next IMAXth) column to Kth of W */
1667
1668                     i__1 = *n - k + 1;
1669                     ccopy_(&i__1, &w[k + (k + 1) * w_dim1], &c__1, &w[k + k * 
1670                             w_dim1], &c__1);
1671
1672                 }
1673
1674
1675 /*                 End pivot search loop body */
1676
1677                 if (! done) {
1678                     goto L72;
1679                 }
1680
1681             }
1682
1683 /*           END pivot search */
1684
1685 /*           ============================================================ */
1686
1687 /*           KK is the column of A where pivoting step stopped */
1688
1689             kk = k + kstep - 1;
1690
1691 /*           Interchange rows and columns P and K (only for 2-by-2 pivot). */
1692 /*           Updated column P is already stored in column K of W. */
1693
1694             if (kstep == 2 && p != k) {
1695
1696 /*              Copy non-updated column KK-1 to column P of submatrix A */
1697 /*              at step K. No need to copy element into columns */
1698 /*              K and K+1 of A for 2-by-2 pivot, since these columns */
1699 /*              will be later overwritten. */
1700
1701                 i__1 = p + p * a_dim1;
1702                 i__2 = k + k * a_dim1;
1703                 r__1 = a[i__2].r;
1704                 a[i__1].r = r__1, a[i__1].i = 0.f;
1705                 i__1 = p - k - 1;
1706                 ccopy_(&i__1, &a[k + 1 + k * a_dim1], &c__1, &a[p + (k + 1) * 
1707                         a_dim1], lda);
1708                 i__1 = p - k - 1;
1709                 clacgv_(&i__1, &a[p + (k + 1) * a_dim1], lda);
1710                 if (p < *n) {
1711                     i__1 = *n - p;
1712                     ccopy_(&i__1, &a[p + 1 + k * a_dim1], &c__1, &a[p + 1 + p 
1713                             * a_dim1], &c__1);
1714                 }
1715
1716 /*              Interchange rows K and P in first K-1 columns of A */
1717 /*              (columns K and K+1 of A for 2-by-2 pivot will be */
1718 /*              later overwritten). Interchange rows K and P */
1719 /*              in first KK columns of W. */
1720
1721                 if (k > 1) {
1722                     i__1 = k - 1;
1723                     cswap_(&i__1, &a[k + a_dim1], lda, &a[p + a_dim1], lda);
1724                 }
1725                 cswap_(&kk, &w[k + w_dim1], ldw, &w[p + w_dim1], ldw);
1726             }
1727
1728 /*           Interchange rows and columns KP and KK. */
1729 /*           Updated column KP is already stored in column KK of W. */
1730
1731             if (kp != kk) {
1732
1733 /*              Copy non-updated column KK to column KP of submatrix A */
1734 /*              at step K. No need to copy element into column K */
1735 /*              (or K and K+1 for 2-by-2 pivot) of A, since these columns */
1736 /*              will be later overwritten. */
1737
1738                 i__1 = kp + kp * a_dim1;
1739                 i__2 = kk + kk * a_dim1;
1740                 r__1 = a[i__2].r;
1741                 a[i__1].r = r__1, a[i__1].i = 0.f;
1742                 i__1 = kp - kk - 1;
1743                 ccopy_(&i__1, &a[kk + 1 + kk * a_dim1], &c__1, &a[kp + (kk + 
1744                         1) * a_dim1], lda);
1745                 i__1 = kp - kk - 1;
1746                 clacgv_(&i__1, &a[kp + (kk + 1) * a_dim1], lda);
1747                 if (kp < *n) {
1748                     i__1 = *n - kp;
1749                     ccopy_(&i__1, &a[kp + 1 + kk * a_dim1], &c__1, &a[kp + 1 
1750                             + kp * a_dim1], &c__1);
1751                 }
1752
1753 /*              Interchange rows KK and KP in first K-1 columns of A */
1754 /*              (column K (or K and K+1 for 2-by-2 pivot) of A will be */
1755 /*              later overwritten). Interchange rows KK and KP */
1756 /*              in first KK columns of W. */
1757
1758                 if (k > 1) {
1759                     i__1 = k - 1;
1760                     cswap_(&i__1, &a[kk + a_dim1], lda, &a[kp + a_dim1], lda);
1761                 }
1762                 cswap_(&kk, &w[kk + w_dim1], ldw, &w[kp + w_dim1], ldw);
1763             }
1764
1765             if (kstep == 1) {
1766
1767 /*              1-by-1 pivot block D(k): column k of W now holds */
1768
1769 /*              W(k) = L(k)*D(k), */
1770
1771 /*              where L(k) is the k-th column of L */
1772
1773 /*              (1) Store subdiag. elements of column L(k) */
1774 /*              and 1-by-1 block D(k) in column k of A. */
1775 /*              (NOTE: Diagonal element L(k,k) is a UNIT element */
1776 /*              and not stored) */
1777 /*                 A(k,k) := D(k,k) = W(k,k) */
1778 /*                 A(k+1:N,k) := L(k+1:N,k) = W(k+1:N,k)/D(k,k) */
1779
1780 /*              (NOTE: No need to use for Hermitian matrix */
1781 /*              A( K, K ) = REAL( W( K, K) ) to separately copy diagonal */
1782 /*              element D(k,k) from W (potentially saves only one load)) */
1783                 i__1 = *n - k + 1;
1784                 ccopy_(&i__1, &w[k + k * w_dim1], &c__1, &a[k + k * a_dim1], &
1785                         c__1);
1786                 if (k < *n) {
1787
1788 /*                 (NOTE: No need to check if A(k,k) is NOT ZERO, */
1789 /*                  since that was ensured earlier in pivot search: */
1790 /*                  case A(k,k) = 0 falls into 2x2 pivot case(3)) */
1791
1792 /*                 Handle division by a small number */
1793
1794                     i__1 = k + k * a_dim1;
1795                     t = a[i__1].r;
1796                     if (abs(t) >= sfmin) {
1797                         r1 = 1.f / t;
1798                         i__1 = *n - k;
1799                         csscal_(&i__1, &r1, &a[k + 1 + k * a_dim1], &c__1);
1800                     } else {
1801                         i__1 = *n;
1802                         for (ii = k + 1; ii <= i__1; ++ii) {
1803                             i__2 = ii + k * a_dim1;
1804                             i__3 = ii + k * a_dim1;
1805                             q__1.r = a[i__3].r / t, q__1.i = a[i__3].i / t;
1806                             a[i__2].r = q__1.r, a[i__2].i = q__1.i;
1807 /* L74: */
1808                         }
1809                     }
1810
1811 /*                 (2) Conjugate column W(k) */
1812
1813                     i__1 = *n - k;
1814                     clacgv_(&i__1, &w[k + 1 + k * w_dim1], &c__1);
1815
1816 /*                 Store the subdiagonal element of D in array E */
1817
1818                     i__1 = k;
1819                     e[i__1].r = 0.f, e[i__1].i = 0.f;
1820
1821                 }
1822
1823             } else {
1824
1825 /*              2-by-2 pivot block D(k): columns k and k+1 of W now hold */
1826
1827 /*              ( W(k) W(k+1) ) = ( L(k) L(k+1) )*D(k) */
1828
1829 /*              where L(k) and L(k+1) are the k-th and (k+1)-th columns */
1830 /*              of L */
1831
1832 /*              (1) Store L(k+2:N,k) and L(k+2:N,k+1) and 2-by-2 */
1833 /*              block D(k:k+1,k:k+1) in columns k and k+1 of A. */
1834 /*              NOTE: 2-by-2 diagonal block L(k:k+1,k:k+1) is a UNIT */
1835 /*              block and not stored. */
1836 /*                 A(k:k+1,k:k+1) := D(k:k+1,k:k+1) = W(k:k+1,k:k+1) */
1837 /*                 A(k+2:N,k:k+1) := L(k+2:N,k:k+1) = */
1838 /*                 = W(k+2:N,k:k+1) * ( D(k:k+1,k:k+1)**(-1) ) */
1839
1840                 if (k < *n - 1) {
1841
1842 /*                 Factor out the columns of the inverse of 2-by-2 pivot */
1843 /*                 block D, so that each column contains 1, to reduce the */
1844 /*                 number of FLOPS when we multiply panel */
1845 /*                 ( W(kw-1) W(kw) ) by this inverse, i.e. by D**(-1). */
1846
1847 /*                 D**(-1) = ( d11 cj(d21) )**(-1) = */
1848 /*                           ( d21    d22 ) */
1849
1850 /*                 = 1/(d11*d22-|d21|**2) * ( ( d22) (-cj(d21) ) ) = */
1851 /*                                          ( (-d21) (     d11 ) ) */
1852
1853 /*                 = 1/(|d21|**2) * 1/((d11/cj(d21))*(d22/d21)-1) * */
1854
1855 /*                   * ( d21*( d22/d21 ) conj(d21)*(           - 1 ) ) = */
1856 /*                     (     (      -1 )           ( d11/conj(d21) ) ) */
1857
1858 /*                 = 1/(|d21|**2) * 1/(D22*D11-1) * */
1859
1860 /*                   * ( d21*( D11 ) conj(d21)*(  -1 ) ) = */
1861 /*                     (     (  -1 )           ( D22 ) ) */
1862
1863 /*                 = (1/|d21|**2) * T * ( d21*( D11 ) conj(d21)*(  -1 ) ) = */
1864 /*                                      (     (  -1 )           ( D22 ) ) */
1865
1866 /*                 = ( (T/conj(d21))*( D11 ) (T/d21)*(  -1 ) ) = */
1867 /*                   (               (  -1 )         ( D22 ) ) */
1868
1869 /*                 Handle division by a small number. (NOTE: order of */
1870 /*                 operations is important) */
1871
1872 /*                 = ( T*(( D11 )/conj(D21)) T*((  -1 )/D21 ) ) */
1873 /*                   (   ((  -1 )          )   (( D22 )     ) ), */
1874
1875 /*                 where D11 = d22/d21, */
1876 /*                       D22 = d11/conj(d21), */
1877 /*                       D21 = d21, */
1878 /*                       T = 1/(D22*D11-1). */
1879
1880 /*                 (NOTE: No need to check for division by ZERO, */
1881 /*                  since that was ensured earlier in pivot search: */
1882 /*                  (a) d21 != 0 in 2x2 pivot case(4), */
1883 /*                      since |d21| should be larger than |d11| and |d22|; */
1884 /*                  (b) (D22*D11 - 1) != 0, since from (a), */
1885 /*                      both |D11| < 1, |D22| < 1, hence |D22*D11| << 1.) */
1886
1887                     i__1 = k + 1 + k * w_dim1;
1888                     d21.r = w[i__1].r, d21.i = w[i__1].i;
1889                     c_div(&q__1, &w[k + 1 + (k + 1) * w_dim1], &d21);
1890                     d11.r = q__1.r, d11.i = q__1.i;
1891                     r_cnjg(&q__2, &d21);
1892                     c_div(&q__1, &w[k + k * w_dim1], &q__2);
1893                     d22.r = q__1.r, d22.i = q__1.i;
1894                     q__1.r = d11.r * d22.r - d11.i * d22.i, q__1.i = d11.r * 
1895                             d22.i + d11.i * d22.r;
1896                     t = 1.f / (q__1.r - 1.f);
1897
1898 /*                 Update elements in columns A(k) and A(k+1) as */
1899 /*                 dot products of rows of ( W(k) W(k+1) ) and columns */
1900 /*                 of D**(-1) */
1901
1902                     i__1 = *n;
1903                     for (j = k + 2; j <= i__1; ++j) {
1904                         i__2 = j + k * a_dim1;
1905                         i__3 = j + k * w_dim1;
1906                         q__4.r = d11.r * w[i__3].r - d11.i * w[i__3].i, 
1907                                 q__4.i = d11.r * w[i__3].i + d11.i * w[i__3]
1908                                 .r;
1909                         i__4 = j + (k + 1) * w_dim1;
1910                         q__3.r = q__4.r - w[i__4].r, q__3.i = q__4.i - w[i__4]
1911                                 .i;
1912                         r_cnjg(&q__5, &d21);
1913                         c_div(&q__2, &q__3, &q__5);
1914                         q__1.r = t * q__2.r, q__1.i = t * q__2.i;
1915                         a[i__2].r = q__1.r, a[i__2].i = q__1.i;
1916                         i__2 = j + (k + 1) * a_dim1;
1917                         i__3 = j + (k + 1) * w_dim1;
1918                         q__4.r = d22.r * w[i__3].r - d22.i * w[i__3].i, 
1919                                 q__4.i = d22.r * w[i__3].i + d22.i * w[i__3]
1920                                 .r;
1921                         i__4 = j + k * w_dim1;
1922                         q__3.r = q__4.r - w[i__4].r, q__3.i = q__4.i - w[i__4]
1923                                 .i;
1924                         c_div(&q__2, &q__3, &d21);
1925                         q__1.r = t * q__2.r, q__1.i = t * q__2.i;
1926                         a[i__2].r = q__1.r, a[i__2].i = q__1.i;
1927 /* L80: */
1928                     }
1929                 }
1930
1931 /*              Copy diagonal elements of D(K) to A, */
1932 /*              copy subdiagonal element of D(K) to E(K) and */
1933 /*              ZERO out subdiagonal entry of A */
1934
1935                 i__1 = k + k * a_dim1;
1936                 i__2 = k + k * w_dim1;
1937                 a[i__1].r = w[i__2].r, a[i__1].i = w[i__2].i;
1938                 i__1 = k + 1 + k * a_dim1;
1939                 a[i__1].r = 0.f, a[i__1].i = 0.f;
1940                 i__1 = k + 1 + (k + 1) * a_dim1;
1941                 i__2 = k + 1 + (k + 1) * w_dim1;
1942                 a[i__1].r = w[i__2].r, a[i__1].i = w[i__2].i;
1943                 i__1 = k;
1944                 i__2 = k + 1 + k * w_dim1;
1945                 e[i__1].r = w[i__2].r, e[i__1].i = w[i__2].i;
1946                 i__1 = k + 1;
1947                 e[i__1].r = 0.f, e[i__1].i = 0.f;
1948
1949 /*              (2) Conjugate columns W(k) and W(k+1) */
1950
1951                 i__1 = *n - k;
1952                 clacgv_(&i__1, &w[k + 1 + k * w_dim1], &c__1);
1953                 i__1 = *n - k - 1;
1954                 clacgv_(&i__1, &w[k + 2 + (k + 1) * w_dim1], &c__1);
1955
1956             }
1957
1958 /*           End column K is nonsingular */
1959
1960         }
1961
1962 /*        Store details of the interchanges in IPIV */
1963
1964         if (kstep == 1) {
1965             ipiv[k] = kp;
1966         } else {
1967             ipiv[k] = -p;
1968             ipiv[k + 1] = -kp;
1969         }
1970
1971 /*        Increase K and return to the start of the main loop */
1972
1973         k += kstep;
1974         goto L70;
1975
1976 L90:
1977
1978 /*        Update the lower triangle of A22 (= A(k:n,k:n)) as */
1979
1980 /*        A22 := A22 - L21*D*L21**H = A22 - L21*W**H */
1981
1982 /*        computing blocks of NB columns at a time (note that conjg(W) is */
1983 /*        actually stored) */
1984
1985         i__1 = *n;
1986         i__2 = *nb;
1987         for (j = k; i__2 < 0 ? j >= i__1 : j <= i__1; j += i__2) {
1988 /* Computing MIN */
1989             i__3 = *nb, i__4 = *n - j + 1;
1990             jb = f2cmin(i__3,i__4);
1991
1992 /*           Update the lower triangle of the diagonal block */
1993
1994             i__3 = j + jb - 1;
1995             for (jj = j; jj <= i__3; ++jj) {
1996                 i__4 = jj + jj * a_dim1;
1997                 i__5 = jj + jj * a_dim1;
1998                 r__1 = a[i__5].r;
1999                 a[i__4].r = r__1, a[i__4].i = 0.f;
2000                 i__4 = j + jb - jj;
2001                 i__5 = k - 1;
2002                 q__1.r = -1.f, q__1.i = 0.f;
2003                 cgemv_("No transpose", &i__4, &i__5, &q__1, &a[jj + a_dim1], 
2004                         lda, &w[jj + w_dim1], ldw, &c_b1, &a[jj + jj * a_dim1]
2005                         , &c__1);
2006                 i__4 = jj + jj * a_dim1;
2007                 i__5 = jj + jj * a_dim1;
2008                 r__1 = a[i__5].r;
2009                 a[i__4].r = r__1, a[i__4].i = 0.f;
2010 /* L100: */
2011             }
2012
2013 /*           Update the rectangular subdiagonal block */
2014
2015             if (j + jb <= *n) {
2016                 i__3 = *n - j - jb + 1;
2017                 i__4 = k - 1;
2018                 q__1.r = -1.f, q__1.i = 0.f;
2019                 cgemm_("No transpose", "Transpose", &i__3, &jb, &i__4, &q__1, 
2020                         &a[j + jb + a_dim1], lda, &w[j + w_dim1], ldw, &c_b1, 
2021                         &a[j + jb + j * a_dim1], lda);
2022             }
2023 /* L110: */
2024         }
2025
2026 /*        Set KB to the number of columns factorized */
2027
2028         *kb = k - 1;
2029
2030     }
2031     return 0;
2032
2033 /*     End of CLAHEF_RK */
2034
2035 } /* clahef_rk__ */
2036