C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / cgesvdq.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 = {0.f,0.f};
516 static complex c_b2 = {1.f,0.f};
517 static integer c_n1 = -1;
518 static integer c__1 = 1;
519 static real c_b74 = 0.f;
520 static integer c__0 = 0;
521 static real c_b87 = 1.f;
522 static logical c_false = FALSE_;
523
524 /* > \brief <b> CGESVDQ computes the singular value decomposition (SVD) with a QR-Preconditioned QR SVD Method
525  for GE matrices</b> */
526
527 /*  =========== DOCUMENTATION =========== */
528
529 /* Online html documentation available at */
530 /*            http://www.netlib.org/lapack/explore-html/ */
531
532 /* > \htmlonly */
533 /* > Download CGESVDQ + dependencies */
534 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/cgesvdq
535 .f"> */
536 /* > [TGZ]</a> */
537 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/cgesvdq
538 .f"> */
539 /* > [ZIP]</a> */
540 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/cgesvdq
541 .f"> */
542 /* > [TXT]</a> */
543 /* > \endhtmlonly */
544
545 /*  Definition: */
546 /*  =========== */
547
548 /*      SUBROUTINE CGESVDQ( JOBA, JOBP, JOBR, JOBU, JOBV, M, N, A, LDA, */
549 /*                          S, U, LDU, V, LDV, NUMRANK, IWORK, LIWORK, */
550 /*                          CWORK, LCWORK, RWORK, LRWORK, INFO ) */
551
552 /*      IMPLICIT    NONE */
553 /*      CHARACTER   JOBA, JOBP, JOBR, JOBU, JOBV */
554 /*      INTEGER     M, N, LDA, LDU, LDV, NUMRANK, LIWORK, LCWORK, LRWORK, */
555 /*                  INFO */
556 /*      COMPLEX     A( LDA, * ), U( LDU, * ), V( LDV, * ), CWORK( * ) */
557 /*      REAL        S( * ), RWORK( * ) */
558 /*      INTEGER     IWORK( * ) */
559
560
561 /* > \par Purpose: */
562 /*  ============= */
563 /* > */
564 /* > \verbatim */
565 /* > */
566 /* > CGESVDQ computes the singular value decomposition (SVD) of a complex */
567 /* > M-by-N matrix A, where M >= N. The SVD of A is written as */
568 /* >                                    [++]   [xx]   [x0]   [xx] */
569 /* >              A = U * SIGMA * V^*,  [++] = [xx] * [ox] * [xx] */
570 /* >                                    [++]   [xx] */
571 /* > where SIGMA is an N-by-N diagonal matrix, U is an M-by-N orthonormal */
572 /* > matrix, and V is an N-by-N unitary matrix. The diagonal elements */
573 /* > of SIGMA are the singular values of A. The columns of U and V are the */
574 /* > left and the right singular vectors of A, respectively. */
575 /* > \endverbatim */
576
577 /*  Arguments: */
578 /*  ========== */
579
580 /* > \param[in] JOBA */
581 /* > \verbatim */
582 /* >  JOBA is CHARACTER*1 */
583 /* >  Specifies the level of accuracy in the computed SVD */
584 /* >  = 'A' The requested accuracy corresponds to having the backward */
585 /* >        error bounded by || delta A ||_F <= f(m,n) * EPS * || A ||_F, */
586 /* >        where EPS = SLAMCH('Epsilon'). This authorises CGESVDQ to */
587 /* >        truncate the computed triangular factor in a rank revealing */
588 /* >        QR factorization whenever the truncated part is below the */
589 /* >        threshold of the order of EPS * ||A||_F. This is aggressive */
590 /* >        truncation level. */
591 /* >  = 'M' Similarly as with 'A', but the truncation is more gentle: it */
592 /* >        is allowed only when there is a drop on the diagonal of the */
593 /* >        triangular factor in the QR factorization. This is medium */
594 /* >        truncation level. */
595 /* >  = 'H' High accuracy requested. No numerical rank determination based */
596 /* >        on the rank revealing QR factorization is attempted. */
597 /* >  = 'E' Same as 'H', and in addition the condition number of column */
598 /* >        scaled A is estimated and returned in  RWORK(1). */
599 /* >        N^(-1/4)*RWORK(1) <= ||pinv(A_scaled)||_2 <= N^(1/4)*RWORK(1) */
600 /* > \endverbatim */
601 /* > */
602 /* > \param[in] JOBP */
603 /* > \verbatim */
604 /* >  JOBP is CHARACTER*1 */
605 /* >  = 'P' The rows of A are ordered in decreasing order with respect to */
606 /* >        ||A(i,:)||_\infty. This enhances numerical accuracy at the cost */
607 /* >        of extra data movement. Recommended for numerical robustness. */
608 /* >  = 'N' No row pivoting. */
609 /* > \endverbatim */
610 /* > */
611 /* > \param[in] JOBR */
612 /* > \verbatim */
613 /* >          JOBR is CHARACTER*1 */
614 /* >          = 'T' After the initial pivoted QR factorization, CGESVD is applied to */
615 /* >          the adjoint R**H of the computed triangular factor R. This involves */
616 /* >          some extra data movement (matrix transpositions). Useful for */
617 /* >          experiments, research and development. */
618 /* >          = 'N' The triangular factor R is given as input to CGESVD. This may be */
619 /* >          preferred as it involves less data movement. */
620 /* > \endverbatim */
621 /* > */
622 /* > \param[in] JOBU */
623 /* > \verbatim */
624 /* >          JOBU is CHARACTER*1 */
625 /* >          = 'A' All M left singular vectors are computed and returned in the */
626 /* >          matrix U. See the description of U. */
627 /* >          = 'S' or 'U' N = f2cmin(M,N) left singular vectors are computed and returned */
628 /* >          in the matrix U. See the description of U. */
629 /* >          = 'R' Numerical rank NUMRANK is determined and only NUMRANK left singular */
630 /* >          vectors are computed and returned in the matrix U. */
631 /* >          = 'F' The N left singular vectors are returned in factored form as the */
632 /* >          product of the Q factor from the initial QR factorization and the */
633 /* >          N left singular vectors of (R**H , 0)**H. If row pivoting is used, */
634 /* >          then the necessary information on the row pivoting is stored in */
635 /* >          IWORK(N+1:N+M-1). */
636 /* >          = 'N' The left singular vectors are not computed. */
637 /* > \endverbatim */
638 /* > */
639 /* > \param[in] JOBV */
640 /* > \verbatim */
641 /* >          JOBV is CHARACTER*1 */
642 /* >          = 'A', 'V' All N right singular vectors are computed and returned in */
643 /* >          the matrix V. */
644 /* >          = 'R' Numerical rank NUMRANK is determined and only NUMRANK right singular */
645 /* >          vectors are computed and returned in the matrix V. This option is */
646 /* >          allowed only if JOBU = 'R' or JOBU = 'N'; otherwise it is illegal. */
647 /* >          = 'N' The right singular vectors are not computed. */
648 /* > \endverbatim */
649 /* > */
650 /* > \param[in] M */
651 /* > \verbatim */
652 /* >          M is INTEGER */
653 /* >          The number of rows of the input matrix A.  M >= 0. */
654 /* > \endverbatim */
655 /* > */
656 /* > \param[in] N */
657 /* > \verbatim */
658 /* >          N is INTEGER */
659 /* >          The number of columns of the input matrix A.  M >= N >= 0. */
660 /* > \endverbatim */
661 /* > */
662 /* > \param[in,out] A */
663 /* > \verbatim */
664 /* >          A is COMPLEX array of dimensions LDA x N */
665 /* >          On entry, the input matrix A. */
666 /* >          On exit, if JOBU .NE. 'N' or JOBV .NE. 'N', the lower triangle of A contains */
667 /* >          the Householder vectors as stored by CGEQP3. If JOBU = 'F', these Householder */
668 /* >          vectors together with CWORK(1:N) can be used to restore the Q factors from */
669 /* >          the initial pivoted QR factorization of A. See the description of U. */
670 /* > \endverbatim */
671 /* > */
672 /* > \param[in] LDA */
673 /* > \verbatim */
674 /* >          LDA is INTEGER. */
675 /* >          The leading dimension of the array A.  LDA >= f2cmax(1,M). */
676 /* > \endverbatim */
677 /* > */
678 /* > \param[out] S */
679 /* > \verbatim */
680 /* >          S is REAL array of dimension N. */
681 /* >          The singular values of A, ordered so that S(i) >= S(i+1). */
682 /* > \endverbatim */
683 /* > */
684 /* > \param[out] U */
685 /* > \verbatim */
686 /* >          U is COMPLEX array, dimension */
687 /* >          LDU x M if JOBU = 'A'; see the description of LDU. In this case, */
688 /* >          on exit, U contains the M left singular vectors. */
689 /* >          LDU x N if JOBU = 'S', 'U', 'R' ; see the description of LDU. In this */
690 /* >          case, U contains the leading N or the leading NUMRANK left singular vectors. */
691 /* >          LDU x N if JOBU = 'F' ; see the description of LDU. In this case U */
692 /* >          contains N x N unitary matrix that can be used to form the left */
693 /* >          singular vectors. */
694 /* >          If JOBU = 'N', U is not referenced. */
695 /* > \endverbatim */
696 /* > */
697 /* > \param[in] LDU */
698 /* > \verbatim */
699 /* >          LDU is INTEGER. */
700 /* >          The leading dimension of the array U. */
701 /* >          If JOBU = 'A', 'S', 'U', 'R',  LDU >= f2cmax(1,M). */
702 /* >          If JOBU = 'F',                 LDU >= f2cmax(1,N). */
703 /* >          Otherwise,                     LDU >= 1. */
704 /* > \endverbatim */
705 /* > */
706 /* > \param[out] V */
707 /* > \verbatim */
708 /* >          V is COMPLEX array, dimension */
709 /* >          LDV x N if JOBV = 'A', 'V', 'R' or if JOBA = 'E' . */
710 /* >          If JOBV = 'A', or 'V',  V contains the N-by-N unitary matrix  V**H; */
711 /* >          If JOBV = 'R', V contains the first NUMRANK rows of V**H (the right */
712 /* >          singular vectors, stored rowwise, of the NUMRANK largest singular values). */
713 /* >          If JOBV = 'N' and JOBA = 'E', V is used as a workspace. */
714 /* >          If JOBV = 'N', and JOBA.NE.'E', V is not referenced. */
715 /* > \endverbatim */
716 /* > */
717 /* > \param[in] LDV */
718 /* > \verbatim */
719 /* >          LDV is INTEGER */
720 /* >          The leading dimension of the array V. */
721 /* >          If JOBV = 'A', 'V', 'R',  or JOBA = 'E', LDV >= f2cmax(1,N). */
722 /* >          Otherwise,                               LDV >= 1. */
723 /* > \endverbatim */
724 /* > */
725 /* > \param[out] NUMRANK */
726 /* > \verbatim */
727 /* >          NUMRANK is INTEGER */
728 /* >          NUMRANK is the numerical rank first determined after the rank */
729 /* >          revealing QR factorization, following the strategy specified by the */
730 /* >          value of JOBA. If JOBV = 'R' and JOBU = 'R', only NUMRANK */
731 /* >          leading singular values and vectors are then requested in the call */
732 /* >          of CGESVD. The final value of NUMRANK might be further reduced if */
733 /* >          some singular values are computed as zeros. */
734 /* > \endverbatim */
735 /* > */
736 /* > \param[out] IWORK */
737 /* > \verbatim */
738 /* >          IWORK is INTEGER array, dimension (f2cmax(1, LIWORK)). */
739 /* >          On exit, IWORK(1:N) contains column pivoting permutation of the */
740 /* >          rank revealing QR factorization. */
741 /* >          If JOBP = 'P', IWORK(N+1:N+M-1) contains the indices of the sequence */
742 /* >          of row swaps used in row pivoting. These can be used to restore the */
743 /* >          left singular vectors in the case JOBU = 'F'. */
744 /* > */
745 /* >          If LIWORK, LCWORK, or LRWORK = -1, then on exit, if INFO = 0, */
746 /* >          LIWORK(1) returns the minimal LIWORK. */
747 /* > \endverbatim */
748 /* > */
749 /* > \param[in] LIWORK */
750 /* > \verbatim */
751 /* >          LIWORK is INTEGER */
752 /* >          The dimension of the array IWORK. */
753 /* >          LIWORK >= N + M - 1,  if JOBP = 'P'; */
754 /* >          LIWORK >= N           if JOBP = 'N'. */
755 /* > */
756 /* >          If LIWORK = -1, then a workspace query is assumed; the routine */
757 /* >          only calculates and returns the optimal and minimal sizes */
758 /* >          for the CWORK, IWORK, and RWORK arrays, and no error */
759 /* >          message related to LCWORK is issued by XERBLA. */
760 /* > \endverbatim */
761 /* > */
762 /* > \param[out] CWORK */
763 /* > \verbatim */
764 /* >          CWORK is COMPLEX array, dimension (f2cmax(2, LCWORK)), used as a workspace. */
765 /* >          On exit, if, on entry, LCWORK.NE.-1, CWORK(1:N) contains parameters */
766 /* >          needed to recover the Q factor from the QR factorization computed by */
767 /* >          CGEQP3. */
768 /* > */
769 /* >          If LIWORK, LCWORK, or LRWORK = -1, then on exit, if INFO = 0, */
770 /* >          CWORK(1) returns the optimal LCWORK, and */
771 /* >          CWORK(2) returns the minimal LCWORK. */
772 /* > \endverbatim */
773 /* > */
774 /* > \param[in,out] LCWORK */
775 /* > \verbatim */
776 /* >          LCWORK is INTEGER */
777 /* >          The dimension of the array CWORK. It is determined as follows: */
778 /* >          Let  LWQP3 = N+1,  LWCON = 2*N, and let */
779 /* >          LWUNQ = { MAX( N, 1 ),  if JOBU = 'R', 'S', or 'U' */
780 /* >                  { MAX( M, 1 ),  if JOBU = 'A' */
781 /* >          LWSVD = MAX( 3*N, 1 ) */
782 /* >          LWLQF = MAX( N/2, 1 ), LWSVD2 = MAX( 3*(N/2), 1 ), LWUNLQ = MAX( N, 1 ), */
783 /* >          LWQRF = MAX( N/2, 1 ), LWUNQ2 = MAX( N, 1 ) */
784 /* >          Then the minimal value of LCWORK is: */
785 /* >          = MAX( N + LWQP3, LWSVD )        if only the singular values are needed; */
786 /* >          = MAX( N + LWQP3, LWCON, LWSVD ) if only the singular values are needed, */
787 /* >                                   and a scaled condition estimate requested; */
788 /* > */
789 /* >          = N + MAX( LWQP3, LWSVD, LWUNQ ) if the singular values and the left */
790 /* >                                   singular vectors are requested; */
791 /* >          = N + MAX( LWQP3, LWCON, LWSVD, LWUNQ ) if the singular values and the left */
792 /* >                                   singular vectors are requested, and also */
793 /* >                                   a scaled condition estimate requested; */
794 /* > */
795 /* >          = N + MAX( LWQP3, LWSVD )        if the singular values and the right */
796 /* >                                   singular vectors are requested; */
797 /* >          = N + MAX( LWQP3, LWCON, LWSVD ) if the singular values and the right */
798 /* >                                   singular vectors are requested, and also */
799 /* >                                   a scaled condition etimate requested; */
800 /* > */
801 /* >          = N + MAX( LWQP3, LWSVD, LWUNQ ) if the full SVD is requested with JOBV = 'R'; */
802 /* >                                   independent of JOBR; */
803 /* >          = N + MAX( LWQP3, LWCON, LWSVD, LWUNQ ) if the full SVD is requested, */
804 /* >                                   JOBV = 'R' and, also a scaled condition */
805 /* >                                   estimate requested; independent of JOBR; */
806 /* >          = MAX( N + MAX( LWQP3, LWSVD, LWUNQ ), */
807 /* >         N + MAX( LWQP3, N/2+LWLQF, N/2+LWSVD2, N/2+LWUNLQ, LWUNQ) ) if the */
808 /* >                         full SVD is requested with JOBV = 'A' or 'V', and */
809 /* >                         JOBR ='N' */
810 /* >          = MAX( N + MAX( LWQP3, LWCON, LWSVD, LWUNQ ), */
811 /* >         N + MAX( LWQP3, LWCON, N/2+LWLQF, N/2+LWSVD2, N/2+LWUNLQ, LWUNQ ) ) */
812 /* >                         if the full SVD is requested with JOBV = 'A' or 'V', and */
813 /* >                         JOBR ='N', and also a scaled condition number estimate */
814 /* >                         requested. */
815 /* >          = MAX( N + MAX( LWQP3, LWSVD, LWUNQ ), */
816 /* >         N + MAX( LWQP3, N/2+LWQRF, N/2+LWSVD2, N/2+LWUNQ2, LWUNQ ) ) if the */
817 /* >                         full SVD is requested with JOBV = 'A', 'V', and JOBR ='T' */
818 /* >          = MAX( N + MAX( LWQP3, LWCON, LWSVD, LWUNQ ), */
819 /* >         N + MAX( LWQP3, LWCON, N/2+LWQRF, N/2+LWSVD2, N/2+LWUNQ2, LWUNQ ) ) */
820 /* >                         if the full SVD is requested with JOBV = 'A', 'V' and */
821 /* >                         JOBR ='T', and also a scaled condition number estimate */
822 /* >                         requested. */
823 /* >          Finally, LCWORK must be at least two: LCWORK = MAX( 2, LCWORK ). */
824 /* > */
825 /* >          If LCWORK = -1, then a workspace query is assumed; the routine */
826 /* >          only calculates and returns the optimal and minimal sizes */
827 /* >          for the CWORK, IWORK, and RWORK arrays, and no error */
828 /* >          message related to LCWORK is issued by XERBLA. */
829 /* > \endverbatim */
830 /* > */
831 /* > \param[out] RWORK */
832 /* > \verbatim */
833 /* >          RWORK is REAL array, dimension (f2cmax(1, LRWORK)). */
834 /* >          On exit, */
835 /* >          1. If JOBA = 'E', RWORK(1) contains an estimate of the condition */
836 /* >          number of column scaled A. If A = C * D where D is diagonal and C */
837 /* >          has unit columns in the Euclidean norm, then, assuming full column rank, */
838 /* >          N^(-1/4) * RWORK(1) <= ||pinv(C)||_2 <= N^(1/4) * RWORK(1). */
839 /* >          Otherwise, RWORK(1) = -1. */
840 /* >          2. RWORK(2) contains the number of singular values computed as */
841 /* >          exact zeros in CGESVD applied to the upper triangular or trapeziodal */
842 /* >          R (from the initial QR factorization). In case of early exit (no call to */
843 /* >          CGESVD, such as in the case of zero matrix) RWORK(2) = -1. */
844 /* > */
845 /* >          If LIWORK, LCWORK, or LRWORK = -1, then on exit, if INFO = 0, */
846 /* >          RWORK(1) returns the minimal LRWORK. */
847 /* > \endverbatim */
848 /* > */
849 /* > \param[in] LRWORK */
850 /* > \verbatim */
851 /* >          LRWORK is INTEGER. */
852 /* >          The dimension of the array RWORK. */
853 /* >          If JOBP ='P', then LRWORK >= MAX(2, M, 5*N); */
854 /* >          Otherwise, LRWORK >= MAX(2, 5*N). */
855 /* > */
856 /* >          If LRWORK = -1, then a workspace query is assumed; the routine */
857 /* >          only calculates and returns the optimal and minimal sizes */
858 /* >          for the CWORK, IWORK, and RWORK arrays, and no error */
859 /* >          message related to LCWORK is issued by XERBLA. */
860 /* > \endverbatim */
861 /* > */
862 /* > \param[out] INFO */
863 /* > \verbatim */
864 /* >          INFO is INTEGER */
865 /* >          = 0:  successful exit. */
866 /* >          < 0:  if INFO = -i, the i-th argument had an illegal value. */
867 /* >          > 0:  if CBDSQR did not converge, INFO specifies how many superdiagonals */
868 /* >          of an intermediate bidiagonal form B (computed in CGESVD) did not */
869 /* >          converge to zero. */
870 /* > \endverbatim */
871
872 /* > \par Further Details: */
873 /*  ======================== */
874 /* > */
875 /* > \verbatim */
876 /* > */
877 /* >   1. The data movement (matrix transpose) is coded using simple nested */
878 /* >   DO-loops because BLAS and LAPACK do not provide corresponding subroutines. */
879 /* >   Those DO-loops are easily identified in this source code - by the CONTINUE */
880 /* >   statements labeled with 11**. In an optimized version of this code, the */
881 /* >   nested DO loops should be replaced with calls to an optimized subroutine. */
882 /* >   2. This code scales A by 1/SQRT(M) if the largest ABS(A(i,j)) could cause */
883 /* >   column norm overflow. This is the minial precaution and it is left to the */
884 /* >   SVD routine (CGESVD) to do its own preemptive scaling if potential over- */
885 /* >   or underflows are detected. To avoid repeated scanning of the array A, */
886 /* >   an optimal implementation would do all necessary scaling before calling */
887 /* >   CGESVD and the scaling in CGESVD can be switched off. */
888 /* >   3. Other comments related to code optimization are given in comments in the */
889 /* >   code, enlosed in [[double brackets]]. */
890 /* > \endverbatim */
891
892 /* > \par Bugs, examples and comments */
893 /*  =========================== */
894
895 /* > \verbatim */
896 /* >  Please report all bugs and send interesting examples and/or comments to */
897 /* >  drmac@math.hr. Thank you. */
898 /* > \endverbatim */
899
900 /* > \par References */
901 /*  =============== */
902
903 /* > \verbatim */
904 /* >  [1] Zlatko Drmac, Algorithm 977: A QR-Preconditioned QR SVD Method for */
905 /* >      Computing the SVD with High Accuracy. ACM Trans. Math. Softw. */
906 /* >      44(1): 11:1-11:30 (2017) */
907 /* > */
908 /* >  SIGMA library, xGESVDQ section updated February 2016. */
909 /* >  Developed and coded by Zlatko Drmac, Department of Mathematics */
910 /* >  University of Zagreb, Croatia, drmac@math.hr */
911 /* > \endverbatim */
912
913
914 /* > \par Contributors: */
915 /*  ================== */
916 /* > */
917 /* > \verbatim */
918 /* > Developed and coded by Zlatko Drmac, Department of Mathematics */
919 /* >  University of Zagreb, Croatia, drmac@math.hr */
920 /* > \endverbatim */
921
922 /*  Authors: */
923 /*  ======== */
924
925 /* > \author Univ. of Tennessee */
926 /* > \author Univ. of California Berkeley */
927 /* > \author Univ. of Colorado Denver */
928 /* > \author NAG Ltd. */
929
930 /* > \date November 2018 */
931
932 /* > \ingroup complexGEsing */
933
934 /*  ===================================================================== */
935 /* Subroutine */ int cgesvdq_(char *joba, char *jobp, char *jobr, char *jobu, 
936         char *jobv, integer *m, integer *n, complex *a, integer *lda, real *s,
937          complex *u, integer *ldu, complex *v, integer *ldv, integer *numrank,
938          integer *iwork, integer *liwork, complex *cwork, integer *lcwork, 
939         real *rwork, integer *lrwork, integer *info)
940 {
941     /* System generated locals */
942     integer a_dim1, a_offset, u_dim1, u_offset, v_dim1, v_offset, i__1, i__2, 
943             i__3;
944     real r__1;
945     complex q__1;
946
947     /* Local variables */
948     integer lwrk_cunmqr__, lwrk_cgesvd2__, ierr;
949     complex ctmp;
950     real rtmp;
951     integer lwrk_cunmqr2__, optratio;
952     logical lsvc0, accla;
953     integer lwqp3;
954     logical acclh, acclm;
955     integer p, q;
956     logical conda;
957     extern logical lsame_(char *, char *);
958     logical lsvec;
959     real sfmin, epsln;
960     integer lwcon;
961     logical rsvec;
962     integer lwlqf, lwqrf;
963     logical wntua;
964     integer n1, lwsvd;
965     logical dntwu, dntwv, wntuf, wntva;
966     integer lwunq;
967     logical wntur, wntus, wntvr;
968     extern /* Subroutine */ int cgeqp3_(integer *, integer *, complex *, 
969             integer *, integer *, complex *, complex *, integer *, real *, 
970             integer *);
971     extern real scnrm2_(integer *, complex *, integer *);
972     integer lwsvd2, lwunq2;
973     extern real clange_(char *, integer *, integer *, complex *, integer *, 
974             real *);
975     integer nr;
976     extern /* Subroutine */ int cgelqf_(integer *, integer *, complex *, 
977             integer *, complex *, complex *, integer *, integer *), clascl_(
978             char *, integer *, integer *, real *, real *, integer *, integer *
979             , complex *, integer *, integer *);
980     real sconda;
981     extern /* Subroutine */ int cgeqrf_(integer *, integer *, complex *, 
982             integer *, complex *, complex *, integer *, integer *), csscal_(
983             integer *, real *, complex *, integer *);
984     extern real slamch_(char *);
985     extern /* Subroutine */ int cgesvd_(char *, char *, integer *, integer *, 
986             complex *, integer *, real *, complex *, integer *, complex *, 
987             integer *, complex *, integer *, real *, integer *), clacpy_(char *, integer *, integer *, complex *, integer 
988             *, complex *, integer *), claset_(char *, integer *, 
989             integer *, complex *, complex *, complex *, integer *), 
990             xerbla_(char *, integer *, ftnlen), clapmt_(logical *, integer *, 
991             integer *, complex *, integer *, integer *), slascl_(char *, 
992             integer *, integer *, real *, real *, integer *, integer *, real *
993             , integer *, integer *), cpocon_(char *, integer *, 
994             complex *, integer *, real *, real *, complex *, real *, integer *
995             );
996     extern integer isamax_(integer *, real *, integer *);
997     extern /* Subroutine */ int claswp_(integer *, complex *, integer *, 
998             integer *, integer *, integer *, integer *), slaset_(char *, 
999             integer *, integer *, real *, real *, real *, integer *);
1000     complex cdummy[1];
1001     extern /* Subroutine */ int cunmlq_(char *, char *, integer *, integer *, 
1002             integer *, complex *, integer *, complex *, complex *, integer *, 
1003             complex *, integer *, integer *), cunmqr_(char *, 
1004             char *, integer *, integer *, integer *, complex *, integer *, 
1005             complex *, complex *, integer *, complex *, integer *, integer *);
1006     integer minwrk;
1007     logical rtrans;
1008     real rdummy[1];
1009     logical lquery;
1010     integer lwunlq, optwrk;
1011     logical rowprm;
1012     real big;
1013     integer minwrk2;
1014     logical ascaled;
1015     integer lwrk_cgeqp3__, optwrk2, lwrk_cgelqf__, iminwrk, lwrk_cgeqrf__, 
1016             lwrk_cgesvd__, rminwrk, lwrk_cunmlq__;
1017
1018
1019 /*  ===================================================================== */
1020
1021
1022 /*     Test the input arguments */
1023
1024     /* Parameter adjustments */
1025     a_dim1 = *lda;
1026     a_offset = 1 + a_dim1 * 1;
1027     a -= a_offset;
1028     --s;
1029     u_dim1 = *ldu;
1030     u_offset = 1 + u_dim1 * 1;
1031     u -= u_offset;
1032     v_dim1 = *ldv;
1033     v_offset = 1 + v_dim1 * 1;
1034     v -= v_offset;
1035     --iwork;
1036     --cwork;
1037     --rwork;
1038
1039     /* Function Body */
1040     wntus = lsame_(jobu, "S") || lsame_(jobu, "U");
1041     wntur = lsame_(jobu, "R");
1042     wntua = lsame_(jobu, "A");
1043     wntuf = lsame_(jobu, "F");
1044     lsvc0 = wntus || wntur || wntua;
1045     lsvec = lsvc0 || wntuf;
1046     dntwu = lsame_(jobu, "N");
1047
1048     wntvr = lsame_(jobv, "R");
1049     wntva = lsame_(jobv, "A") || lsame_(jobv, "V");
1050     rsvec = wntvr || wntva;
1051     dntwv = lsame_(jobv, "N");
1052
1053     accla = lsame_(joba, "A");
1054     acclm = lsame_(joba, "M");
1055     conda = lsame_(joba, "E");
1056     acclh = lsame_(joba, "H") || conda;
1057
1058     rowprm = lsame_(jobp, "P");
1059     rtrans = lsame_(jobr, "T");
1060
1061     if (rowprm) {
1062 /* Computing MAX */
1063         i__1 = 1, i__2 = *n + *m - 1;
1064         iminwrk = f2cmax(i__1,i__2);
1065 /* Computing MAX */
1066         i__1 = f2cmax(2,*m), i__2 = *n * 5;
1067         rminwrk = f2cmax(i__1,i__2);
1068     } else {
1069         iminwrk = f2cmax(1,*n);
1070 /* Computing MAX */
1071         i__1 = 2, i__2 = *n * 5;
1072         rminwrk = f2cmax(i__1,i__2);
1073     }
1074     lquery = *liwork == -1 || *lcwork == -1 || *lrwork == -1;
1075     *info = 0;
1076     if (! (accla || acclm || acclh)) {
1077         *info = -1;
1078     } else if (! (rowprm || lsame_(jobp, "N"))) {
1079         *info = -2;
1080     } else if (! (rtrans || lsame_(jobr, "N"))) {
1081         *info = -3;
1082     } else if (! (lsvec || dntwu)) {
1083         *info = -4;
1084     } else if (wntur && wntva) {
1085         *info = -5;
1086     } else if (! (rsvec || dntwv)) {
1087         *info = -5;
1088     } else if (*m < 0) {
1089         *info = -6;
1090     } else if (*n < 0 || *n > *m) {
1091         *info = -7;
1092     } else if (*lda < f2cmax(1,*m)) {
1093         *info = -9;
1094     } else if (*ldu < 1 || lsvc0 && *ldu < *m || wntuf && *ldu < *n) {
1095         *info = -12;
1096     } else if (*ldv < 1 || rsvec && *ldv < *n || conda && *ldv < *n) {
1097         *info = -14;
1098     } else if (*liwork < iminwrk && ! lquery) {
1099         *info = -17;
1100     }
1101
1102
1103     if (*info == 0) {
1104
1105 /*     Compute workspace */
1106 /*        [[The expressions for computing the minimal and the optimal */
1107 /*        values of LCWORK are written with a lot of redundancy and */
1108 /*        can be simplified. However, this detailed form is easier for */
1109 /*        maintenance and modifications of the code.]] */
1110
1111         lwqp3 = *n + 1;
1112         if (wntus || wntur) {
1113             lwunq = f2cmax(*n,1);
1114         } else if (wntua) {
1115             lwunq = f2cmax(*m,1);
1116         }
1117         lwcon = *n << 1;
1118 /* Computing MAX */
1119         i__1 = *n * 3;
1120         lwsvd = f2cmax(i__1,1);
1121         if (lquery) {
1122             cgeqp3_(m, n, &a[a_offset], lda, &iwork[1], cdummy, cdummy, &c_n1,
1123                      rdummy, &ierr);
1124             lwrk_cgeqp3__ = (integer) cdummy[0].r;
1125             if (wntus || wntur) {
1126                 cunmqr_("L", "N", m, n, n, &a[a_offset], lda, cdummy, &u[
1127                         u_offset], ldu, cdummy, &c_n1, &ierr);
1128                 lwrk_cunmqr__ = (integer) cdummy[0].r;
1129             } else if (wntua) {
1130                 cunmqr_("L", "N", m, m, n, &a[a_offset], lda, cdummy, &u[
1131                         u_offset], ldu, cdummy, &c_n1, &ierr);
1132                 lwrk_cunmqr__ = (integer) cdummy[0].r;
1133             } else {
1134                 lwrk_cunmqr__ = 0;
1135             }
1136         }
1137         minwrk = 2;
1138         optwrk = 2;
1139         if (! (lsvec || rsvec)) {
1140 /*            only the singular values are requested */
1141             if (conda) {
1142 /* Computing MAX */
1143                 i__1 = *n + lwqp3, i__1 = f2cmax(i__1,lwcon);
1144                 minwrk = f2cmax(i__1,lwsvd);
1145             } else {
1146 /* Computing MAX */
1147                 i__1 = *n + lwqp3;
1148                 minwrk = f2cmax(i__1,lwsvd);
1149             }
1150             if (lquery) {
1151                 cgesvd_("N", "N", n, n, &a[a_offset], lda, &s[1], &u[u_offset]
1152                         , ldu, &v[v_offset], ldv, cdummy, &c_n1, rdummy, &
1153                         ierr);
1154                 lwrk_cgesvd__ = (integer) cdummy[0].r;
1155                 if (conda) {
1156 /* Computing MAX */
1157                     i__1 = *n + lwrk_cgeqp3__, i__2 = *n + lwcon, i__1 = f2cmax(
1158                             i__1,i__2);
1159                     optwrk = f2cmax(i__1,lwrk_cgesvd__);
1160                 } else {
1161 /* Computing MAX */
1162                     i__1 = *n + lwrk_cgeqp3__;
1163                     optwrk = f2cmax(i__1,lwrk_cgesvd__);
1164                 }
1165             }
1166         } else if (lsvec && ! rsvec) {
1167 /*            singular values and the left singular vectors are requested */
1168             if (conda) {
1169 /* Computing MAX */
1170                 i__1 = f2cmax(lwqp3,lwcon), i__1 = f2cmax(i__1,lwsvd);
1171                 minwrk = *n + f2cmax(i__1,lwunq);
1172             } else {
1173 /* Computing MAX */
1174                 i__1 = f2cmax(lwqp3,lwsvd);
1175                 minwrk = *n + f2cmax(i__1,lwunq);
1176             }
1177             if (lquery) {
1178                 if (rtrans) {
1179                     cgesvd_("N", "O", n, n, &a[a_offset], lda, &s[1], &u[
1180                             u_offset], ldu, &v[v_offset], ldv, cdummy, &c_n1, 
1181                             rdummy, &ierr);
1182                 } else {
1183                     cgesvd_("O", "N", n, n, &a[a_offset], lda, &s[1], &u[
1184                             u_offset], ldu, &v[v_offset], ldv, cdummy, &c_n1, 
1185                             rdummy, &ierr);
1186                 }
1187                 lwrk_cgesvd__ = (integer) cdummy[0].r;
1188                 if (conda) {
1189 /* Computing MAX */
1190                     i__1 = f2cmax(lwrk_cgeqp3__,lwcon), i__1 = f2cmax(i__1,
1191                             lwrk_cgesvd__);
1192                     optwrk = *n + f2cmax(i__1,lwrk_cunmqr__);
1193                 } else {
1194 /* Computing MAX */
1195                     i__1 = f2cmax(lwrk_cgeqp3__,lwrk_cgesvd__);
1196                     optwrk = *n + f2cmax(i__1,lwrk_cunmqr__);
1197                 }
1198             }
1199         } else if (rsvec && ! lsvec) {
1200 /*            singular values and the right singular vectors are requested */
1201             if (conda) {
1202 /* Computing MAX */
1203                 i__1 = f2cmax(lwqp3,lwcon);
1204                 minwrk = *n + f2cmax(i__1,lwsvd);
1205             } else {
1206                 minwrk = *n + f2cmax(lwqp3,lwsvd);
1207             }
1208             if (lquery) {
1209                 if (rtrans) {
1210                     cgesvd_("O", "N", n, n, &a[a_offset], lda, &s[1], &u[
1211                             u_offset], ldu, &v[v_offset], ldv, cdummy, &c_n1, 
1212                             rdummy, &ierr);
1213                 } else {
1214                     cgesvd_("N", "O", n, n, &a[a_offset], lda, &s[1], &u[
1215                             u_offset], ldu, &v[v_offset], ldv, cdummy, &c_n1, 
1216                             rdummy, &ierr);
1217                 }
1218                 lwrk_cgesvd__ = (integer) cdummy[0].r;
1219                 if (conda) {
1220 /* Computing MAX */
1221                     i__1 = f2cmax(lwrk_cgeqp3__,lwcon);
1222                     optwrk = *n + f2cmax(i__1,lwrk_cgesvd__);
1223                 } else {
1224                     optwrk = *n + f2cmax(lwrk_cgeqp3__,lwrk_cgesvd__);
1225                 }
1226             }
1227         } else {
1228 /*            full SVD is requested */
1229             if (rtrans) {
1230 /* Computing MAX */
1231                 i__1 = f2cmax(lwqp3,lwsvd);
1232                 minwrk = f2cmax(i__1,lwunq);
1233                 if (conda) {
1234                     minwrk = f2cmax(minwrk,lwcon);
1235                 }
1236                 minwrk += *n;
1237                 if (wntva) {
1238 /* Computing MAX */
1239                     i__1 = *n / 2;
1240                     lwqrf = f2cmax(i__1,1);
1241 /* Computing MAX */
1242                     i__1 = *n / 2 * 3;
1243                     lwsvd2 = f2cmax(i__1,1);
1244                     lwunq2 = f2cmax(*n,1);
1245 /* Computing MAX */
1246                     i__1 = lwqp3, i__2 = *n / 2 + lwqrf, i__1 = f2cmax(i__1,i__2)
1247                             , i__2 = *n / 2 + lwsvd2, i__1 = f2cmax(i__1,i__2), 
1248                             i__2 = *n / 2 + lwunq2, i__1 = f2cmax(i__1,i__2);
1249                     minwrk2 = f2cmax(i__1,lwunq);
1250                     if (conda) {
1251                         minwrk2 = f2cmax(minwrk2,lwcon);
1252                     }
1253                     minwrk2 = *n + minwrk2;
1254                     minwrk = f2cmax(minwrk,minwrk2);
1255                 }
1256             } else {
1257 /* Computing MAX */
1258                 i__1 = f2cmax(lwqp3,lwsvd);
1259                 minwrk = f2cmax(i__1,lwunq);
1260                 if (conda) {
1261                     minwrk = f2cmax(minwrk,lwcon);
1262                 }
1263                 minwrk += *n;
1264                 if (wntva) {
1265 /* Computing MAX */
1266                     i__1 = *n / 2;
1267                     lwlqf = f2cmax(i__1,1);
1268 /* Computing MAX */
1269                     i__1 = *n / 2 * 3;
1270                     lwsvd2 = f2cmax(i__1,1);
1271                     lwunlq = f2cmax(*n,1);
1272 /* Computing MAX */
1273                     i__1 = lwqp3, i__2 = *n / 2 + lwlqf, i__1 = f2cmax(i__1,i__2)
1274                             , i__2 = *n / 2 + lwsvd2, i__1 = f2cmax(i__1,i__2), 
1275                             i__2 = *n / 2 + lwunlq, i__1 = f2cmax(i__1,i__2);
1276                     minwrk2 = f2cmax(i__1,lwunq);
1277                     if (conda) {
1278                         minwrk2 = f2cmax(minwrk2,lwcon);
1279                     }
1280                     minwrk2 = *n + minwrk2;
1281                     minwrk = f2cmax(minwrk,minwrk2);
1282                 }
1283             }
1284             if (lquery) {
1285                 if (rtrans) {
1286                     cgesvd_("O", "A", n, n, &a[a_offset], lda, &s[1], &u[
1287                             u_offset], ldu, &v[v_offset], ldv, cdummy, &c_n1, 
1288                             rdummy, &ierr);
1289                     lwrk_cgesvd__ = (integer) cdummy[0].r;
1290 /* Computing MAX */
1291                     i__1 = f2cmax(lwrk_cgeqp3__,lwrk_cgesvd__);
1292                     optwrk = f2cmax(i__1,lwrk_cunmqr__);
1293                     if (conda) {
1294                         optwrk = f2cmax(optwrk,lwcon);
1295                     }
1296                     optwrk = *n + optwrk;
1297                     if (wntva) {
1298                         i__1 = *n / 2;
1299                         cgeqrf_(n, &i__1, &u[u_offset], ldu, cdummy, cdummy, &
1300                                 c_n1, &ierr);
1301                         lwrk_cgeqrf__ = (integer) cdummy[0].r;
1302                         i__1 = *n / 2;
1303                         i__2 = *n / 2;
1304                         cgesvd_("S", "O", &i__1, &i__2, &v[v_offset], ldv, &s[
1305                                 1], &u[u_offset], ldu, &v[v_offset], ldv, 
1306                                 cdummy, &c_n1, rdummy, &ierr);
1307                         lwrk_cgesvd2__ = (integer) cdummy[0].r;
1308                         i__1 = *n / 2;
1309                         cunmqr_("R", "C", n, n, &i__1, &u[u_offset], ldu, 
1310                                 cdummy, &v[v_offset], ldv, cdummy, &c_n1, &
1311                                 ierr);
1312                         lwrk_cunmqr2__ = (integer) cdummy[0].r;
1313 /* Computing MAX */
1314                         i__1 = lwrk_cgeqp3__, i__2 = *n / 2 + lwrk_cgeqrf__, 
1315                                 i__1 = f2cmax(i__1,i__2), i__2 = *n / 2 + 
1316                                 lwrk_cgesvd2__, i__1 = f2cmax(i__1,i__2), i__2 = 
1317                                 *n / 2 + lwrk_cunmqr2__;
1318                         optwrk2 = f2cmax(i__1,i__2);
1319                         if (conda) {
1320                             optwrk2 = f2cmax(optwrk2,lwcon);
1321                         }
1322                         optwrk2 = *n + optwrk2;
1323                         optwrk = f2cmax(optwrk,optwrk2);
1324                     }
1325                 } else {
1326                     cgesvd_("S", "O", n, n, &a[a_offset], lda, &s[1], &u[
1327                             u_offset], ldu, &v[v_offset], ldv, cdummy, &c_n1, 
1328                             rdummy, &ierr);
1329                     lwrk_cgesvd__ = (integer) cdummy[0].r;
1330 /* Computing MAX */
1331                     i__1 = f2cmax(lwrk_cgeqp3__,lwrk_cgesvd__);
1332                     optwrk = f2cmax(i__1,lwrk_cunmqr__);
1333                     if (conda) {
1334                         optwrk = f2cmax(optwrk,lwcon);
1335                     }
1336                     optwrk = *n + optwrk;
1337                     if (wntva) {
1338                         i__1 = *n / 2;
1339                         cgelqf_(&i__1, n, &u[u_offset], ldu, cdummy, cdummy, &
1340                                 c_n1, &ierr);
1341                         lwrk_cgelqf__ = (integer) cdummy[0].r;
1342                         i__1 = *n / 2;
1343                         i__2 = *n / 2;
1344                         cgesvd_("S", "O", &i__1, &i__2, &v[v_offset], ldv, &s[
1345                                 1], &u[u_offset], ldu, &v[v_offset], ldv, 
1346                                 cdummy, &c_n1, rdummy, &ierr);
1347                         lwrk_cgesvd2__ = (integer) cdummy[0].r;
1348                         i__1 = *n / 2;
1349                         cunmlq_("R", "N", n, n, &i__1, &u[u_offset], ldu, 
1350                                 cdummy, &v[v_offset], ldv, cdummy, &c_n1, &
1351                                 ierr);
1352                         lwrk_cunmlq__ = (integer) cdummy[0].r;
1353 /* Computing MAX */
1354                         i__1 = lwrk_cgeqp3__, i__2 = *n / 2 + lwrk_cgelqf__, 
1355                                 i__1 = f2cmax(i__1,i__2), i__2 = *n / 2 + 
1356                                 lwrk_cgesvd2__, i__1 = f2cmax(i__1,i__2), i__2 = 
1357                                 *n / 2 + lwrk_cunmlq__;
1358                         optwrk2 = f2cmax(i__1,i__2);
1359                         if (conda) {
1360                             optwrk2 = f2cmax(optwrk2,lwcon);
1361                         }
1362                         optwrk2 = *n + optwrk2;
1363                         optwrk = f2cmax(optwrk,optwrk2);
1364                     }
1365                 }
1366             }
1367         }
1368
1369         minwrk = f2cmax(2,minwrk);
1370         optwrk = f2cmax(2,optwrk);
1371         if (*lcwork < minwrk && ! lquery) {
1372             *info = -19;
1373         }
1374
1375     }
1376
1377     if (*info == 0 && *lrwork < rminwrk && ! lquery) {
1378         *info = -21;
1379     }
1380     if (*info != 0) {
1381         i__1 = -(*info);
1382         xerbla_("CGESVDQ", &i__1, (ftnlen)7);
1383         return 0;
1384     } else if (lquery) {
1385
1386 /*     Return optimal workspace */
1387
1388         iwork[1] = iminwrk;
1389         cwork[1].r = (real) optwrk, cwork[1].i = 0.f;
1390         cwork[2].r = (real) minwrk, cwork[2].i = 0.f;
1391         rwork[1] = (real) rminwrk;
1392         return 0;
1393     }
1394
1395 /*     Quick return if the matrix is void. */
1396
1397     if (*m == 0 || *n == 0) {
1398         return 0;
1399     }
1400
1401     big = slamch_("O");
1402     ascaled = FALSE_;
1403     if (rowprm) {
1404 /*           ell-infinity norm - this enhances numerical robustness in */
1405 /*           the case of differently scaled rows. */
1406         i__1 = *m;
1407         for (p = 1; p <= i__1; ++p) {
1408 /*               RWORK(p) = ABS( A(p,ICAMAX(N,A(p,1),LDA)) ) */
1409 /*               [[CLANGE will return NaN if an entry of the p-th row is Nan]] */
1410             rwork[p] = clange_("M", &c__1, n, &a[p + a_dim1], lda, rdummy);
1411             if (rwork[p] != rwork[p] || rwork[p] * 0.f != 0.f) {
1412                 *info = -8;
1413                 i__2 = -(*info);
1414                 xerbla_("CGESVDQ", &i__2, (ftnlen)7);
1415                 return 0;
1416             }
1417 /* L1904: */
1418         }
1419         i__1 = *m - 1;
1420         for (p = 1; p <= i__1; ++p) {
1421             i__2 = *m - p + 1;
1422             q = isamax_(&i__2, &rwork[p], &c__1) + p - 1;
1423             iwork[*n + p] = q;
1424             if (p != q) {
1425                 rtmp = rwork[p];
1426                 rwork[p] = rwork[q];
1427                 rwork[q] = rtmp;
1428             }
1429 /* L1952: */
1430         }
1431
1432         if (rwork[1] == 0.f) {
1433 /*              Quick return: A is the M x N zero matrix. */
1434             *numrank = 0;
1435             slaset_("G", n, &c__1, &c_b74, &c_b74, &s[1], n);
1436             if (wntus) {
1437                 claset_("G", m, n, &c_b1, &c_b2, &u[u_offset], ldu)
1438                         ;
1439             }
1440             if (wntua) {
1441                 claset_("G", m, m, &c_b1, &c_b2, &u[u_offset], ldu)
1442                         ;
1443             }
1444             if (wntva) {
1445                 claset_("G", n, n, &c_b1, &c_b2, &v[v_offset], ldv)
1446                         ;
1447             }
1448             if (wntuf) {
1449                 claset_("G", n, &c__1, &c_b1, &c_b1, &cwork[1], n);
1450                 claset_("G", m, n, &c_b1, &c_b2, &u[u_offset], ldu)
1451                         ;
1452             }
1453             i__1 = *n;
1454             for (p = 1; p <= i__1; ++p) {
1455                 iwork[p] = p;
1456 /* L5001: */
1457             }
1458             if (rowprm) {
1459                 i__1 = *n + *m - 1;
1460                 for (p = *n + 1; p <= i__1; ++p) {
1461                     iwork[p] = p - *n;
1462 /* L5002: */
1463                 }
1464             }
1465             if (conda) {
1466                 rwork[1] = -1.f;
1467             }
1468             rwork[2] = -1.f;
1469             return 0;
1470         }
1471
1472         if (rwork[1] > big / sqrt((real) (*m))) {
1473 /*               matrix by 1/sqrt(M) if too large entry detected */
1474             r__1 = sqrt((real) (*m));
1475             clascl_("G", &c__0, &c__0, &r__1, &c_b87, m, n, &a[a_offset], lda,
1476                      &ierr);
1477             ascaled = TRUE_;
1478         }
1479         i__1 = *m - 1;
1480         claswp_(n, &a[a_offset], lda, &c__1, &i__1, &iwork[*n + 1], &c__1);
1481     }
1482
1483 /*    norms overflows during the QR factorization. The SVD procedure should */
1484 /*    have its own scaling to save the singular values from overflows and */
1485 /*    underflows. That depends on the SVD procedure. */
1486
1487     if (! rowprm) {
1488         rtmp = clange_("M", m, n, &a[a_offset], lda, &rwork[1]);
1489         if (rtmp != rtmp || rtmp * 0.f != 0.f) {
1490             *info = -8;
1491             i__1 = -(*info);
1492             xerbla_("CGESVDQ", &i__1, (ftnlen)7);
1493             return 0;
1494         }
1495         if (rtmp > big / sqrt((real) (*m))) {
1496 /*             matrix by 1/sqrt(M) if too large entry detected */
1497             r__1 = sqrt((real) (*m));
1498             clascl_("G", &c__0, &c__0, &r__1, &c_b87, m, n, &a[a_offset], lda,
1499                      &ierr);
1500             ascaled = TRUE_;
1501         }
1502     }
1503
1504
1505 /*     A * P = Q * [ R ] */
1506 /*                 [ 0 ] */
1507
1508     i__1 = *n;
1509     for (p = 1; p <= i__1; ++p) {
1510         iwork[p] = 0;
1511 /* L1963: */
1512     }
1513     i__1 = *lcwork - *n;
1514     cgeqp3_(m, n, &a[a_offset], lda, &iwork[1], &cwork[1], &cwork[*n + 1], &
1515             i__1, &rwork[1], &ierr);
1516
1517 /*    If the user requested accuracy level allows truncation in the */
1518 /*    computed upper triangular factor, the matrix R is examined and, */
1519 /*    if possible, replaced with its leading upper trapezoidal part. */
1520
1521     epsln = slamch_("E");
1522     sfmin = slamch_("S");
1523 /*     SMALL = SFMIN / EPSLN */
1524     nr = *n;
1525
1526     if (accla) {
1527
1528 /*        Standard absolute error bound suffices. All sigma_i with */
1529 /*        sigma_i < N*EPS*||A||_F are flushed to zero. This is an */
1530 /*        aggressive enforcement of lower numerical rank by introducing a */
1531 /*        backward error of the order of N*EPS*||A||_F. */
1532         nr = 1;
1533         rtmp = sqrt((real) (*n)) * epsln;
1534         i__1 = *n;
1535         for (p = 2; p <= i__1; ++p) {
1536             if (c_abs(&a[p + p * a_dim1]) < rtmp * c_abs(&a[a_dim1 + 1])) {
1537                 goto L3002;
1538             }
1539             ++nr;
1540 /* L3001: */
1541         }
1542 L3002:
1543
1544         ;
1545     } else if (acclm) {
1546 /*        Sudden drop on the diagonal of R is used as the criterion for being */
1547 /*        close-to-rank-deficient. The threshold is set to EPSLN=SLAMCH('E'). */
1548 /*        [[This can be made more flexible by replacing this hard-coded value */
1549 /*        with a user specified threshold.]] Also, the values that underflow */
1550 /*        will be truncated. */
1551         nr = 1;
1552         i__1 = *n;
1553         for (p = 2; p <= i__1; ++p) {
1554             if (c_abs(&a[p + p * a_dim1]) < epsln * c_abs(&a[p - 1 + (p - 1) *
1555                      a_dim1]) || c_abs(&a[p + p * a_dim1]) < sfmin) {
1556                 goto L3402;
1557             }
1558             ++nr;
1559 /* L3401: */
1560         }
1561 L3402:
1562
1563         ;
1564     } else {
1565 /*        obvious case of zero pivots. */
1566 /*        R(i,i)=0 => R(i:N,i:N)=0. */
1567         nr = 1;
1568         i__1 = *n;
1569         for (p = 2; p <= i__1; ++p) {
1570             if (c_abs(&a[p + p * a_dim1]) == 0.f) {
1571                 goto L3502;
1572             }
1573             ++nr;
1574 /* L3501: */
1575         }
1576 L3502:
1577
1578         if (conda) {
1579 /*           Estimate the scaled condition number of A. Use the fact that it is */
1580 /*           the same as the scaled condition number of R. */
1581             clacpy_("U", n, n, &a[a_offset], lda, &v[v_offset], ldv);
1582 /*              Only the leading NR x NR submatrix of the triangular factor */
1583 /*              is considered. Only if NR=N will this give a reliable error */
1584 /*              bound. However, even for NR < N, this can be used on an */
1585 /*              expert level and obtain useful information in the sense of */
1586 /*              perturbation theory. */
1587             i__1 = nr;
1588             for (p = 1; p <= i__1; ++p) {
1589                 rtmp = scnrm2_(&p, &v[p * v_dim1 + 1], &c__1);
1590                 r__1 = 1.f / rtmp;
1591                 csscal_(&p, &r__1, &v[p * v_dim1 + 1], &c__1);
1592 /* L3053: */
1593             }
1594             if (! (lsvec || rsvec)) {
1595                 cpocon_("U", &nr, &v[v_offset], ldv, &c_b87, &rtmp, &cwork[1],
1596                          &rwork[1], &ierr);
1597             } else {
1598                 cpocon_("U", &nr, &v[v_offset], ldv, &c_b87, &rtmp, &cwork[*n 
1599                         + 1], &rwork[1], &ierr);
1600             }
1601             sconda = 1.f / sqrt(rtmp);
1602 /*           For NR=N, SCONDA is an estimate of SQRT(||(R^* * R)^(-1)||_1), */
1603 /*           N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA */
1604 /*           See the reference [1] for more details. */
1605         }
1606
1607     }
1608
1609     if (wntur) {
1610         n1 = nr;
1611     } else if (wntus || wntuf) {
1612         n1 = *n;
1613     } else if (wntua) {
1614         n1 = *m;
1615     }
1616
1617     if (! (rsvec || lsvec)) {
1618 /* ....................................................................... */
1619 /* ....................................................................... */
1620         if (rtrans) {
1621
1622 /*           the upper triangle of [A] to zero. */
1623             i__1 = f2cmin(*n,nr);
1624             for (p = 1; p <= i__1; ++p) {
1625                 i__2 = p + p * a_dim1;
1626                 r_cnjg(&q__1, &a[p + p * a_dim1]);
1627                 a[i__2].r = q__1.r, a[i__2].i = q__1.i;
1628                 i__2 = *n;
1629                 for (q = p + 1; q <= i__2; ++q) {
1630                     i__3 = q + p * a_dim1;
1631                     r_cnjg(&q__1, &a[p + q * a_dim1]);
1632                     a[i__3].r = q__1.r, a[i__3].i = q__1.i;
1633                     if (q <= nr) {
1634                         i__3 = p + q * a_dim1;
1635                         a[i__3].r = 0.f, a[i__3].i = 0.f;
1636                     }
1637 /* L1147: */
1638                 }
1639 /* L1146: */
1640             }
1641
1642             cgesvd_("N", "N", n, &nr, &a[a_offset], lda, &s[1], &u[u_offset], 
1643                     ldu, &v[v_offset], ldv, &cwork[1], lcwork, &rwork[1], 
1644                     info);
1645
1646         } else {
1647
1648
1649             if (nr > 1) {
1650                 i__1 = nr - 1;
1651                 i__2 = nr - 1;
1652                 claset_("L", &i__1, &i__2, &c_b1, &c_b1, &a[a_dim1 + 2], lda);
1653             }
1654             cgesvd_("N", "N", &nr, n, &a[a_offset], lda, &s[1], &u[u_offset], 
1655                     ldu, &v[v_offset], ldv, &cwork[1], lcwork, &rwork[1], 
1656                     info);
1657
1658         }
1659
1660     } else if (lsvec && ! rsvec) {
1661 /* ....................................................................... */
1662 /* ......................................................................."""""""" */
1663         if (rtrans) {
1664 /*            vectors of R */
1665             i__1 = nr;
1666             for (p = 1; p <= i__1; ++p) {
1667                 i__2 = *n;
1668                 for (q = p; q <= i__2; ++q) {
1669                     i__3 = q + p * u_dim1;
1670                     r_cnjg(&q__1, &a[p + q * a_dim1]);
1671                     u[i__3].r = q__1.r, u[i__3].i = q__1.i;
1672 /* L1193: */
1673                 }
1674 /* L1192: */
1675             }
1676             if (nr > 1) {
1677                 i__1 = nr - 1;
1678                 i__2 = nr - 1;
1679                 claset_("U", &i__1, &i__2, &c_b1, &c_b1, &u[(u_dim1 << 1) + 1]
1680                         , ldu);
1681             }
1682 /*           vectors overwrite [U](1:NR,1:NR) as conjugate transposed. These */
1683 /*           will be pre-multiplied by Q to build the left singular vectors of A. */
1684             i__1 = *lcwork - *n;
1685             cgesvd_("N", "O", n, &nr, &u[u_offset], ldu, &s[1], &u[u_offset], 
1686                     ldu, &u[u_offset], ldu, &cwork[*n + 1], &i__1, &rwork[1], 
1687                     info);
1688
1689             i__1 = nr;
1690             for (p = 1; p <= i__1; ++p) {
1691                 i__2 = p + p * u_dim1;
1692                 r_cnjg(&q__1, &u[p + p * u_dim1]);
1693                 u[i__2].r = q__1.r, u[i__2].i = q__1.i;
1694                 i__2 = nr;
1695                 for (q = p + 1; q <= i__2; ++q) {
1696                     r_cnjg(&q__1, &u[q + p * u_dim1]);
1697                     ctmp.r = q__1.r, ctmp.i = q__1.i;
1698                     i__3 = q + p * u_dim1;
1699                     r_cnjg(&q__1, &u[p + q * u_dim1]);
1700                     u[i__3].r = q__1.r, u[i__3].i = q__1.i;
1701                     i__3 = p + q * u_dim1;
1702                     u[i__3].r = ctmp.r, u[i__3].i = ctmp.i;
1703 /* L1120: */
1704                 }
1705 /* L1119: */
1706             }
1707
1708         } else {
1709             clacpy_("U", &nr, n, &a[a_offset], lda, &u[u_offset], ldu);
1710             if (nr > 1) {
1711                 i__1 = nr - 1;
1712                 i__2 = nr - 1;
1713                 claset_("L", &i__1, &i__2, &c_b1, &c_b1, &u[u_dim1 + 2], ldu);
1714             }
1715 /*            vectors overwrite [U](1:NR,1:NR) */
1716             i__1 = *lcwork - *n;
1717             cgesvd_("O", "N", &nr, n, &u[u_offset], ldu, &s[1], &u[u_offset], 
1718                     ldu, &v[v_offset], ldv, &cwork[*n + 1], &i__1, &rwork[1], 
1719                     info);
1720 /*               R. These will be pre-multiplied by Q to build the left singular */
1721 /*               vectors of A. */
1722         }
1723
1724 /*              (M x NR) or (M x N) or (M x M). */
1725         if (nr < *m && ! wntuf) {
1726             i__1 = *m - nr;
1727             claset_("A", &i__1, &nr, &c_b1, &c_b1, &u[nr + 1 + u_dim1], ldu);
1728             if (nr < n1) {
1729                 i__1 = n1 - nr;
1730                 claset_("A", &nr, &i__1, &c_b1, &c_b1, &u[(nr + 1) * u_dim1 + 
1731                         1], ldu);
1732                 i__1 = *m - nr;
1733                 i__2 = n1 - nr;
1734                 claset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[nr + 1 + (nr + 1) 
1735                         * u_dim1], ldu);
1736             }
1737         }
1738
1739 /*           The Q matrix from the first QRF is built into the left singular */
1740 /*           vectors matrix U. */
1741
1742         if (! wntuf) {
1743             i__1 = *lcwork - *n;
1744             cunmqr_("L", "N", m, &n1, n, &a[a_offset], lda, &cwork[1], &u[
1745                     u_offset], ldu, &cwork[*n + 1], &i__1, &ierr);
1746         }
1747         if (rowprm && ! wntuf) {
1748             i__1 = *m - 1;
1749             claswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[*n + 1], &
1750                     c_n1);
1751         }
1752
1753     } else if (rsvec && ! lsvec) {
1754 /* ....................................................................... */
1755 /* ....................................................................... */
1756         if (rtrans) {
1757             i__1 = nr;
1758             for (p = 1; p <= i__1; ++p) {
1759                 i__2 = *n;
1760                 for (q = p; q <= i__2; ++q) {
1761                     i__3 = q + p * v_dim1;
1762                     r_cnjg(&q__1, &a[p + q * a_dim1]);
1763                     v[i__3].r = q__1.r, v[i__3].i = q__1.i;
1764 /* L1166: */
1765                 }
1766 /* L1165: */
1767             }
1768             if (nr > 1) {
1769                 i__1 = nr - 1;
1770                 i__2 = nr - 1;
1771                 claset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1) + 1]
1772                         , ldv);
1773             }
1774 /*           vectors not computed */
1775             if (wntvr || nr == *n) {
1776                 i__1 = *lcwork - *n;
1777                 cgesvd_("O", "N", n, &nr, &v[v_offset], ldv, &s[1], &u[
1778                         u_offset], ldu, &u[u_offset], ldu, &cwork[*n + 1], &
1779                         i__1, &rwork[1], info);
1780
1781                 i__1 = nr;
1782                 for (p = 1; p <= i__1; ++p) {
1783                     i__2 = p + p * v_dim1;
1784                     r_cnjg(&q__1, &v[p + p * v_dim1]);
1785                     v[i__2].r = q__1.r, v[i__2].i = q__1.i;
1786                     i__2 = nr;
1787                     for (q = p + 1; q <= i__2; ++q) {
1788                         r_cnjg(&q__1, &v[q + p * v_dim1]);
1789                         ctmp.r = q__1.r, ctmp.i = q__1.i;
1790                         i__3 = q + p * v_dim1;
1791                         r_cnjg(&q__1, &v[p + q * v_dim1]);
1792                         v[i__3].r = q__1.r, v[i__3].i = q__1.i;
1793                         i__3 = p + q * v_dim1;
1794                         v[i__3].r = ctmp.r, v[i__3].i = ctmp.i;
1795 /* L1122: */
1796                     }
1797 /* L1121: */
1798                 }
1799
1800                 if (nr < *n) {
1801                     i__1 = nr;
1802                     for (p = 1; p <= i__1; ++p) {
1803                         i__2 = *n;
1804                         for (q = nr + 1; q <= i__2; ++q) {
1805                             i__3 = p + q * v_dim1;
1806                             r_cnjg(&q__1, &v[q + p * v_dim1]);
1807                             v[i__3].r = q__1.r, v[i__3].i = q__1.i;
1808 /* L1104: */
1809                         }
1810 /* L1103: */
1811                     }
1812                 }
1813                 clapmt_(&c_false, &nr, n, &v[v_offset], ldv, &iwork[1]);
1814             } else {
1815 /*               [!] This is simple implementation that augments [V](1:N,1:NR) */
1816 /*               by padding a zero block. In the case NR << N, a more efficient */
1817 /*               way is to first use the QR factorization. For more details */
1818 /*               how to implement this, see the " FULL SVD " branch. */
1819                 i__1 = *n - nr;
1820                 claset_("G", n, &i__1, &c_b1, &c_b1, &v[(nr + 1) * v_dim1 + 1]
1821                         , ldv);
1822                 i__1 = *lcwork - *n;
1823                 cgesvd_("O", "N", n, n, &v[v_offset], ldv, &s[1], &u[u_offset]
1824                         , ldu, &u[u_offset], ldu, &cwork[*n + 1], &i__1, &
1825                         rwork[1], info);
1826
1827                 i__1 = *n;
1828                 for (p = 1; p <= i__1; ++p) {
1829                     i__2 = p + p * v_dim1;
1830                     r_cnjg(&q__1, &v[p + p * v_dim1]);
1831                     v[i__2].r = q__1.r, v[i__2].i = q__1.i;
1832                     i__2 = *n;
1833                     for (q = p + 1; q <= i__2; ++q) {
1834                         r_cnjg(&q__1, &v[q + p * v_dim1]);
1835                         ctmp.r = q__1.r, ctmp.i = q__1.i;
1836                         i__3 = q + p * v_dim1;
1837                         r_cnjg(&q__1, &v[p + q * v_dim1]);
1838                         v[i__3].r = q__1.r, v[i__3].i = q__1.i;
1839                         i__3 = p + q * v_dim1;
1840                         v[i__3].r = ctmp.r, v[i__3].i = ctmp.i;
1841 /* L1124: */
1842                     }
1843 /* L1123: */
1844                 }
1845                 clapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
1846             }
1847
1848         } else {
1849             clacpy_("U", &nr, n, &a[a_offset], lda, &v[v_offset], ldv);
1850             if (nr > 1) {
1851                 i__1 = nr - 1;
1852                 i__2 = nr - 1;
1853                 claset_("L", &i__1, &i__2, &c_b1, &c_b1, &v[v_dim1 + 2], ldv);
1854             }
1855 /*            vectors stored in U(1:NR,1:NR) */
1856             if (wntvr || nr == *n) {
1857                 i__1 = *lcwork - *n;
1858                 cgesvd_("N", "O", &nr, n, &v[v_offset], ldv, &s[1], &u[
1859                         u_offset], ldu, &v[v_offset], ldv, &cwork[*n + 1], &
1860                         i__1, &rwork[1], info);
1861                 clapmt_(&c_false, &nr, n, &v[v_offset], ldv, &iwork[1]);
1862             } else {
1863 /*               [!] This is simple implementation that augments [V](1:NR,1:N) */
1864 /*               by padding a zero block. In the case NR << N, a more efficient */
1865 /*               way is to first use the LQ factorization. For more details */
1866 /*               how to implement this, see the " FULL SVD " branch. */
1867                 i__1 = *n - nr;
1868                 claset_("G", &i__1, n, &c_b1, &c_b1, &v[nr + 1 + v_dim1], ldv);
1869                 i__1 = *lcwork - *n;
1870                 cgesvd_("N", "O", n, n, &v[v_offset], ldv, &s[1], &u[u_offset]
1871                         , ldu, &v[v_offset], ldv, &cwork[*n + 1], &i__1, &
1872                         rwork[1], info);
1873                 clapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
1874             }
1875 /*            vectors of A. */
1876         }
1877
1878     } else {
1879 /* ....................................................................... */
1880 /* ....................................................................... */
1881         if (rtrans) {
1882
1883
1884             if (wntvr || nr == *n) {
1885 /*            vectors of R**H */
1886                 i__1 = nr;
1887                 for (p = 1; p <= i__1; ++p) {
1888                     i__2 = *n;
1889                     for (q = p; q <= i__2; ++q) {
1890                         i__3 = q + p * v_dim1;
1891                         r_cnjg(&q__1, &a[p + q * a_dim1]);
1892                         v[i__3].r = q__1.r, v[i__3].i = q__1.i;
1893 /* L1169: */
1894                     }
1895 /* L1168: */
1896                 }
1897                 if (nr > 1) {
1898                     i__1 = nr - 1;
1899                     i__2 = nr - 1;
1900                     claset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1) 
1901                             + 1], ldv);
1902                 }
1903
1904 /*           singular vectors of R**H stored in [U](1:NR,1:NR) as conjugate */
1905 /*           transposed */
1906                 i__1 = *lcwork - *n;
1907                 cgesvd_("O", "A", n, &nr, &v[v_offset], ldv, &s[1], &v[
1908                         v_offset], ldv, &u[u_offset], ldu, &cwork[*n + 1], &
1909                         i__1, &rwork[1], info);
1910                 i__1 = nr;
1911                 for (p = 1; p <= i__1; ++p) {
1912                     i__2 = p + p * v_dim1;
1913                     r_cnjg(&q__1, &v[p + p * v_dim1]);
1914                     v[i__2].r = q__1.r, v[i__2].i = q__1.i;
1915                     i__2 = nr;
1916                     for (q = p + 1; q <= i__2; ++q) {
1917                         r_cnjg(&q__1, &v[q + p * v_dim1]);
1918                         ctmp.r = q__1.r, ctmp.i = q__1.i;
1919                         i__3 = q + p * v_dim1;
1920                         r_cnjg(&q__1, &v[p + q * v_dim1]);
1921                         v[i__3].r = q__1.r, v[i__3].i = q__1.i;
1922                         i__3 = p + q * v_dim1;
1923                         v[i__3].r = ctmp.r, v[i__3].i = ctmp.i;
1924 /* L1116: */
1925                     }
1926 /* L1115: */
1927                 }
1928                 if (nr < *n) {
1929                     i__1 = nr;
1930                     for (p = 1; p <= i__1; ++p) {
1931                         i__2 = *n;
1932                         for (q = nr + 1; q <= i__2; ++q) {
1933                             i__3 = p + q * v_dim1;
1934                             r_cnjg(&q__1, &v[q + p * v_dim1]);
1935                             v[i__3].r = q__1.r, v[i__3].i = q__1.i;
1936 /* L1102: */
1937                         }
1938 /* L1101: */
1939                     }
1940                 }
1941                 clapmt_(&c_false, &nr, n, &v[v_offset], ldv, &iwork[1]);
1942
1943                 i__1 = nr;
1944                 for (p = 1; p <= i__1; ++p) {
1945                     i__2 = p + p * u_dim1;
1946                     r_cnjg(&q__1, &u[p + p * u_dim1]);
1947                     u[i__2].r = q__1.r, u[i__2].i = q__1.i;
1948                     i__2 = nr;
1949                     for (q = p + 1; q <= i__2; ++q) {
1950                         r_cnjg(&q__1, &u[q + p * u_dim1]);
1951                         ctmp.r = q__1.r, ctmp.i = q__1.i;
1952                         i__3 = q + p * u_dim1;
1953                         r_cnjg(&q__1, &u[p + q * u_dim1]);
1954                         u[i__3].r = q__1.r, u[i__3].i = q__1.i;
1955                         i__3 = p + q * u_dim1;
1956                         u[i__3].r = ctmp.r, u[i__3].i = ctmp.i;
1957 /* L1118: */
1958                     }
1959 /* L1117: */
1960                 }
1961
1962                 if (nr < *m && ! wntuf) {
1963                     i__1 = *m - nr;
1964                     claset_("A", &i__1, &nr, &c_b1, &c_b1, &u[nr + 1 + u_dim1]
1965                             , ldu);
1966                     if (nr < n1) {
1967                         i__1 = n1 - nr;
1968                         claset_("A", &nr, &i__1, &c_b1, &c_b1, &u[(nr + 1) * 
1969                                 u_dim1 + 1], ldu);
1970                         i__1 = *m - nr;
1971                         i__2 = n1 - nr;
1972                         claset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[nr + 1 + (
1973                                 nr + 1) * u_dim1], ldu);
1974                     }
1975                 }
1976
1977             } else {
1978 /*            vectors of R**H */
1979 /*               [[The optimal ratio N/NR for using QRF instead of padding */
1980 /*                 with zeros. Here hard coded to 2; it must be at least */
1981 /*                 two due to work space constraints.]] */
1982 /*               OPTRATIO = ILAENV(6, 'CGESVD', 'S' // 'O', NR,N,0,0) */
1983 /*               OPTRATIO = MAX( OPTRATIO, 2 ) */
1984                 optratio = 2;
1985                 if (optratio * nr > *n) {
1986                     i__1 = nr;
1987                     for (p = 1; p <= i__1; ++p) {
1988                         i__2 = *n;
1989                         for (q = p; q <= i__2; ++q) {
1990                             i__3 = q + p * v_dim1;
1991                             r_cnjg(&q__1, &a[p + q * a_dim1]);
1992                             v[i__3].r = q__1.r, v[i__3].i = q__1.i;
1993 /* L1199: */
1994                         }
1995 /* L1198: */
1996                     }
1997                     if (nr > 1) {
1998                         i__1 = nr - 1;
1999                         i__2 = nr - 1;
2000                         claset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 <<
2001                                  1) + 1], ldv);
2002                     }
2003
2004                     i__1 = *n - nr;
2005                     claset_("A", n, &i__1, &c_b1, &c_b1, &v[(nr + 1) * v_dim1 
2006                             + 1], ldv);
2007                     i__1 = *lcwork - *n;
2008                     cgesvd_("O", "A", n, n, &v[v_offset], ldv, &s[1], &v[
2009                             v_offset], ldv, &u[u_offset], ldu, &cwork[*n + 1],
2010                              &i__1, &rwork[1], info);
2011
2012                     i__1 = *n;
2013                     for (p = 1; p <= i__1; ++p) {
2014                         i__2 = p + p * v_dim1;
2015                         r_cnjg(&q__1, &v[p + p * v_dim1]);
2016                         v[i__2].r = q__1.r, v[i__2].i = q__1.i;
2017                         i__2 = *n;
2018                         for (q = p + 1; q <= i__2; ++q) {
2019                             r_cnjg(&q__1, &v[q + p * v_dim1]);
2020                             ctmp.r = q__1.r, ctmp.i = q__1.i;
2021                             i__3 = q + p * v_dim1;
2022                             r_cnjg(&q__1, &v[p + q * v_dim1]);
2023                             v[i__3].r = q__1.r, v[i__3].i = q__1.i;
2024                             i__3 = p + q * v_dim1;
2025                             v[i__3].r = ctmp.r, v[i__3].i = ctmp.i;
2026 /* L1114: */
2027                         }
2028 /* L1113: */
2029                     }
2030                     clapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
2031 /*              (M x N1), i.e. (M x N) or (M x M). */
2032
2033                     i__1 = *n;
2034                     for (p = 1; p <= i__1; ++p) {
2035                         i__2 = p + p * u_dim1;
2036                         r_cnjg(&q__1, &u[p + p * u_dim1]);
2037                         u[i__2].r = q__1.r, u[i__2].i = q__1.i;
2038                         i__2 = *n;
2039                         for (q = p + 1; q <= i__2; ++q) {
2040                             r_cnjg(&q__1, &u[q + p * u_dim1]);
2041                             ctmp.r = q__1.r, ctmp.i = q__1.i;
2042                             i__3 = q + p * u_dim1;
2043                             r_cnjg(&q__1, &u[p + q * u_dim1]);
2044                             u[i__3].r = q__1.r, u[i__3].i = q__1.i;
2045                             i__3 = p + q * u_dim1;
2046                             u[i__3].r = ctmp.r, u[i__3].i = ctmp.i;
2047 /* L1112: */
2048                         }
2049 /* L1111: */
2050                     }
2051
2052                     if (*n < *m && ! wntuf) {
2053                         i__1 = *m - *n;
2054                         claset_("A", &i__1, n, &c_b1, &c_b1, &u[*n + 1 + 
2055                                 u_dim1], ldu);
2056                         if (*n < n1) {
2057                             i__1 = n1 - *n;
2058                             claset_("A", n, &i__1, &c_b1, &c_b1, &u[(*n + 1) *
2059                                      u_dim1 + 1], ldu);
2060                             i__1 = *m - *n;
2061                             i__2 = n1 - *n;
2062                             claset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[*n + 
2063                                     1 + (*n + 1) * u_dim1], ldu);
2064                         }
2065                     }
2066                 } else {
2067 /*                  singular vectors of R */
2068                     i__1 = nr;
2069                     for (p = 1; p <= i__1; ++p) {
2070                         i__2 = *n;
2071                         for (q = p; q <= i__2; ++q) {
2072                             i__3 = q + (nr + p) * u_dim1;
2073                             r_cnjg(&q__1, &a[p + q * a_dim1]);
2074                             u[i__3].r = q__1.r, u[i__3].i = q__1.i;
2075 /* L1197: */
2076                         }
2077 /* L1196: */
2078                     }
2079                     if (nr > 1) {
2080                         i__1 = nr - 1;
2081                         i__2 = nr - 1;
2082                         claset_("U", &i__1, &i__2, &c_b1, &c_b1, &u[(nr + 2) *
2083                                  u_dim1 + 1], ldu);
2084                     }
2085                     i__1 = *lcwork - *n - nr;
2086                     cgeqrf_(n, &nr, &u[(nr + 1) * u_dim1 + 1], ldu, &cwork[*n 
2087                             + 1], &cwork[*n + nr + 1], &i__1, &ierr);
2088                     i__1 = nr;
2089                     for (p = 1; p <= i__1; ++p) {
2090                         i__2 = *n;
2091                         for (q = 1; q <= i__2; ++q) {
2092                             i__3 = q + p * v_dim1;
2093                             r_cnjg(&q__1, &u[p + (nr + q) * u_dim1]);
2094                             v[i__3].r = q__1.r, v[i__3].i = q__1.i;
2095 /* L1144: */
2096                         }
2097 /* L1143: */
2098                     }
2099                     i__1 = nr - 1;
2100                     i__2 = nr - 1;
2101                     claset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 << 1) 
2102                             + 1], ldv);
2103                     i__1 = *lcwork - *n - nr;
2104                     cgesvd_("S", "O", &nr, &nr, &v[v_offset], ldv, &s[1], &u[
2105                             u_offset], ldu, &v[v_offset], ldv, &cwork[*n + nr 
2106                             + 1], &i__1, &rwork[1], info);
2107                     i__1 = *n - nr;
2108                     claset_("A", &i__1, &nr, &c_b1, &c_b1, &v[nr + 1 + v_dim1]
2109                             , ldv);
2110                     i__1 = *n - nr;
2111                     claset_("A", &nr, &i__1, &c_b1, &c_b1, &v[(nr + 1) * 
2112                             v_dim1 + 1], ldv);
2113                     i__1 = *n - nr;
2114                     i__2 = *n - nr;
2115                     claset_("A", &i__1, &i__2, &c_b1, &c_b2, &v[nr + 1 + (nr 
2116                             + 1) * v_dim1], ldv);
2117                     i__1 = *lcwork - *n - nr;
2118                     cunmqr_("R", "C", n, n, &nr, &u[(nr + 1) * u_dim1 + 1], 
2119                             ldu, &cwork[*n + 1], &v[v_offset], ldv, &cwork[*n 
2120                             + nr + 1], &i__1, &ierr);
2121                     clapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
2122 /*                 (M x NR) or (M x N) or (M x M). */
2123                     if (nr < *m && ! wntuf) {
2124                         i__1 = *m - nr;
2125                         claset_("A", &i__1, &nr, &c_b1, &c_b1, &u[nr + 1 + 
2126                                 u_dim1], ldu);
2127                         if (nr < n1) {
2128                             i__1 = n1 - nr;
2129                             claset_("A", &nr, &i__1, &c_b1, &c_b1, &u[(nr + 1)
2130                                      * u_dim1 + 1], ldu);
2131                             i__1 = *m - nr;
2132                             i__2 = n1 - nr;
2133                             claset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[nr + 
2134                                     1 + (nr + 1) * u_dim1], ldu);
2135                         }
2136                     }
2137                 }
2138             }
2139
2140         } else {
2141
2142
2143             if (wntvr || nr == *n) {
2144                 clacpy_("U", &nr, n, &a[a_offset], lda, &v[v_offset], ldv);
2145                 if (nr > 1) {
2146                     i__1 = nr - 1;
2147                     i__2 = nr - 1;
2148                     claset_("L", &i__1, &i__2, &c_b1, &c_b1, &v[v_dim1 + 2], 
2149                             ldv);
2150                 }
2151 /*               singular vectors of R stored in [U](1:NR,1:NR) */
2152                 i__1 = *lcwork - *n;
2153                 cgesvd_("S", "O", &nr, n, &v[v_offset], ldv, &s[1], &u[
2154                         u_offset], ldu, &v[v_offset], ldv, &cwork[*n + 1], &
2155                         i__1, &rwork[1], info);
2156                 clapmt_(&c_false, &nr, n, &v[v_offset], ldv, &iwork[1]);
2157 /*              (M x NR) or (M x N) or (M x M). */
2158                 if (nr < *m && ! wntuf) {
2159                     i__1 = *m - nr;
2160                     claset_("A", &i__1, &nr, &c_b1, &c_b1, &u[nr + 1 + u_dim1]
2161                             , ldu);
2162                     if (nr < n1) {
2163                         i__1 = n1 - nr;
2164                         claset_("A", &nr, &i__1, &c_b1, &c_b1, &u[(nr + 1) * 
2165                                 u_dim1 + 1], ldu);
2166                         i__1 = *m - nr;
2167                         i__2 = n1 - nr;
2168                         claset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[nr + 1 + (
2169                                 nr + 1) * u_dim1], ldu);
2170                     }
2171                 }
2172
2173             } else {
2174 /*               is then N1 (N or M) */
2175 /*               [[The optimal ratio N/NR for using LQ instead of padding */
2176 /*                 with zeros. Here hard coded to 2; it must be at least */
2177 /*                 two due to work space constraints.]] */
2178 /*               OPTRATIO = ILAENV(6, 'CGESVD', 'S' // 'O', NR,N,0,0) */
2179 /*               OPTRATIO = MAX( OPTRATIO, 2 ) */
2180                 optratio = 2;
2181                 if (optratio * nr > *n) {
2182                     clacpy_("U", &nr, n, &a[a_offset], lda, &v[v_offset], ldv);
2183                     if (nr > 1) {
2184                         i__1 = nr - 1;
2185                         i__2 = nr - 1;
2186                         claset_("L", &i__1, &i__2, &c_b1, &c_b1, &v[v_dim1 + 
2187                                 2], ldv);
2188                     }
2189 /*                 singular vectors of R stored in [U](1:NR,1:NR) */
2190                     i__1 = *n - nr;
2191                     claset_("A", &i__1, n, &c_b1, &c_b1, &v[nr + 1 + v_dim1], 
2192                             ldv);
2193                     i__1 = *lcwork - *n;
2194                     cgesvd_("S", "O", n, n, &v[v_offset], ldv, &s[1], &u[
2195                             u_offset], ldu, &v[v_offset], ldv, &cwork[*n + 1],
2196                              &i__1, &rwork[1], info);
2197                     clapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
2198 /*                 singular vectors of A. The leading N left singular vectors */
2199 /*                 are in [U](1:N,1:N) */
2200 /*                 (M x N1), i.e. (M x N) or (M x M). */
2201                     if (*n < *m && ! wntuf) {
2202                         i__1 = *m - *n;
2203                         claset_("A", &i__1, n, &c_b1, &c_b1, &u[*n + 1 + 
2204                                 u_dim1], ldu);
2205                         if (*n < n1) {
2206                             i__1 = n1 - *n;
2207                             claset_("A", n, &i__1, &c_b1, &c_b1, &u[(*n + 1) *
2208                                      u_dim1 + 1], ldu);
2209                             i__1 = *m - *n;
2210                             i__2 = n1 - *n;
2211                             claset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[*n + 
2212                                     1 + (*n + 1) * u_dim1], ldu);
2213                         }
2214                     }
2215                 } else {
2216                     clacpy_("U", &nr, n, &a[a_offset], lda, &u[nr + 1 + 
2217                             u_dim1], ldu);
2218                     if (nr > 1) {
2219                         i__1 = nr - 1;
2220                         i__2 = nr - 1;
2221                         claset_("L", &i__1, &i__2, &c_b1, &c_b1, &u[nr + 2 + 
2222                                 u_dim1], ldu);
2223                     }
2224                     i__1 = *lcwork - *n - nr;
2225                     cgelqf_(&nr, n, &u[nr + 1 + u_dim1], ldu, &cwork[*n + 1], 
2226                             &cwork[*n + nr + 1], &i__1, &ierr);
2227                     clacpy_("L", &nr, &nr, &u[nr + 1 + u_dim1], ldu, &v[
2228                             v_offset], ldv);
2229                     if (nr > 1) {
2230                         i__1 = nr - 1;
2231                         i__2 = nr - 1;
2232                         claset_("U", &i__1, &i__2, &c_b1, &c_b1, &v[(v_dim1 <<
2233                                  1) + 1], ldv);
2234                     }
2235                     i__1 = *lcwork - *n - nr;
2236                     cgesvd_("S", "O", &nr, &nr, &v[v_offset], ldv, &s[1], &u[
2237                             u_offset], ldu, &v[v_offset], ldv, &cwork[*n + nr 
2238                             + 1], &i__1, &rwork[1], info);
2239                     i__1 = *n - nr;
2240                     claset_("A", &i__1, &nr, &c_b1, &c_b1, &v[nr + 1 + v_dim1]
2241                             , ldv);
2242                     i__1 = *n - nr;
2243                     claset_("A", &nr, &i__1, &c_b1, &c_b1, &v[(nr + 1) * 
2244                             v_dim1 + 1], ldv);
2245                     i__1 = *n - nr;
2246                     i__2 = *n - nr;
2247                     claset_("A", &i__1, &i__2, &c_b1, &c_b2, &v[nr + 1 + (nr 
2248                             + 1) * v_dim1], ldv);
2249                     i__1 = *lcwork - *n - nr;
2250                     cunmlq_("R", "N", n, n, &nr, &u[nr + 1 + u_dim1], ldu, &
2251                             cwork[*n + 1], &v[v_offset], ldv, &cwork[*n + nr 
2252                             + 1], &i__1, &ierr);
2253                     clapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
2254 /*              (M x NR) or (M x N) or (M x M). */
2255                     if (nr < *m && ! wntuf) {
2256                         i__1 = *m - nr;
2257                         claset_("A", &i__1, &nr, &c_b1, &c_b1, &u[nr + 1 + 
2258                                 u_dim1], ldu);
2259                         if (nr < n1) {
2260                             i__1 = n1 - nr;
2261                             claset_("A", &nr, &i__1, &c_b1, &c_b1, &u[(nr + 1)
2262                                      * u_dim1 + 1], ldu);
2263                             i__1 = *m - nr;
2264                             i__2 = n1 - nr;
2265                             claset_("A", &i__1, &i__2, &c_b1, &c_b2, &u[nr + 
2266                                     1 + (nr + 1) * u_dim1], ldu);
2267                         }
2268                     }
2269                 }
2270             }
2271         }
2272
2273 /*           The Q matrix from the first QRF is built into the left singular */
2274 /*           vectors matrix U. */
2275
2276         if (! wntuf) {
2277             i__1 = *lcwork - *n;
2278             cunmqr_("L", "N", m, &n1, n, &a[a_offset], lda, &cwork[1], &u[
2279                     u_offset], ldu, &cwork[*n + 1], &i__1, &ierr);
2280         }
2281         if (rowprm && ! wntuf) {
2282             i__1 = *m - 1;
2283             claswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[*n + 1], &
2284                     c_n1);
2285         }
2286
2287 /*     ... end of the "full SVD" branch */
2288     }
2289
2290 /*     Check whether some singular values are returned as zeros, e.g. */
2291 /*     due to underflow, and update the numerical rank. */
2292     p = nr;
2293     for (q = p; q >= 1; --q) {
2294         if (s[q] > 0.f) {
2295             goto L4002;
2296         }
2297         --nr;
2298 /* L4001: */
2299     }
2300 L4002:
2301
2302 /*     singular values are set to zero. */
2303     if (nr < *n) {
2304         i__1 = *n - nr;
2305         slaset_("G", &i__1, &c__1, &c_b74, &c_b74, &s[nr + 1], n);
2306     }
2307 /*     values. */
2308     if (ascaled) {
2309         r__1 = sqrt((real) (*m));
2310         slascl_("G", &c__0, &c__0, &c_b87, &r__1, &nr, &c__1, &s[1], n, &ierr);
2311     }
2312     if (conda) {
2313         rwork[1] = sconda;
2314     }
2315     rwork[2] = (real) (p - nr);
2316 /*     exact zeros in CGESVD() applied to the (possibly truncated) */
2317 /*     full row rank triangular (trapezoidal) factor of A. */
2318     *numrank = nr;
2319
2320     return 0;
2321
2322 /*     End of CGESVDQ */
2323
2324 } /* cgesvdq_ */
2325