C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / sgesvdq.c
1 #include <math.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <complex.h>
6 #ifdef complex
7 #undef complex
8 #endif
9 #ifdef I
10 #undef I
11 #endif
12
13 #if defined(_WIN64)
14 typedef long long BLASLONG;
15 typedef unsigned long long BLASULONG;
16 #else
17 typedef long BLASLONG;
18 typedef unsigned long BLASULONG;
19 #endif
20
21 #ifdef LAPACK_ILP64
22 typedef BLASLONG blasint;
23 #if defined(_WIN64)
24 #define blasabs(x) llabs(x)
25 #else
26 #define blasabs(x) labs(x)
27 #endif
28 #else
29 typedef int blasint;
30 #define blasabs(x) abs(x)
31 #endif
32
33 typedef blasint integer;
34
35 typedef unsigned int uinteger;
36 typedef char *address;
37 typedef short int shortint;
38 typedef float real;
39 typedef double doublereal;
40 typedef struct { real r, i; } complex;
41 typedef struct { doublereal r, i; } doublecomplex;
42 #ifdef _MSC_VER
43 static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
44 static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
45 static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
46 static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
47 #else
48 static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
49 static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
50 static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
51 static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
52 #endif
53 #define pCf(z) (*_pCf(z))
54 #define pCd(z) (*_pCd(z))
55 typedef int logical;
56 typedef short int shortlogical;
57 typedef char logical1;
58 typedef char integer1;
59
60 #define TRUE_ (1)
61 #define FALSE_ (0)
62
63 /* Extern is for use with -E */
64 #ifndef Extern
65 #define Extern extern
66 #endif
67
68 /* I/O stuff */
69
70 typedef int flag;
71 typedef int ftnlen;
72 typedef int ftnint;
73
74 /*external read, write*/
75 typedef struct
76 {       flag cierr;
77         ftnint ciunit;
78         flag ciend;
79         char *cifmt;
80         ftnint cirec;
81 } cilist;
82
83 /*internal read, write*/
84 typedef struct
85 {       flag icierr;
86         char *iciunit;
87         flag iciend;
88         char *icifmt;
89         ftnint icirlen;
90         ftnint icirnum;
91 } icilist;
92
93 /*open*/
94 typedef struct
95 {       flag oerr;
96         ftnint ounit;
97         char *ofnm;
98         ftnlen ofnmlen;
99         char *osta;
100         char *oacc;
101         char *ofm;
102         ftnint orl;
103         char *oblnk;
104 } olist;
105
106 /*close*/
107 typedef struct
108 {       flag cerr;
109         ftnint cunit;
110         char *csta;
111 } cllist;
112
113 /*rewind, backspace, endfile*/
114 typedef struct
115 {       flag aerr;
116         ftnint aunit;
117 } alist;
118
119 /* inquire */
120 typedef struct
121 {       flag inerr;
122         ftnint inunit;
123         char *infile;
124         ftnlen infilen;
125         ftnint  *inex;  /*parameters in standard's order*/
126         ftnint  *inopen;
127         ftnint  *innum;
128         ftnint  *innamed;
129         char    *inname;
130         ftnlen  innamlen;
131         char    *inacc;
132         ftnlen  inacclen;
133         char    *inseq;
134         ftnlen  inseqlen;
135         char    *indir;
136         ftnlen  indirlen;
137         char    *infmt;
138         ftnlen  infmtlen;
139         char    *inform;
140         ftnint  informlen;
141         char    *inunf;
142         ftnlen  inunflen;
143         ftnint  *inrecl;
144         ftnint  *innrec;
145         char    *inblank;
146         ftnlen  inblanklen;
147 } inlist;
148
149 #define VOID void
150
151 union Multitype {       /* for multiple entry points */
152         integer1 g;
153         shortint h;
154         integer i;
155         /* longint j; */
156         real r;
157         doublereal d;
158         complex c;
159         doublecomplex z;
160         };
161
162 typedef union Multitype Multitype;
163
164 struct Vardesc {        /* for Namelist */
165         char *name;
166         char *addr;
167         ftnlen *dims;
168         int  type;
169         };
170 typedef struct Vardesc Vardesc;
171
172 struct Namelist {
173         char *name;
174         Vardesc **vars;
175         int nvars;
176         };
177 typedef struct Namelist Namelist;
178
179 #define abs(x) ((x) >= 0 ? (x) : -(x))
180 #define dabs(x) (fabs(x))
181 #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
182 #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
183 #define dmin(a,b) (f2cmin(a,b))
184 #define dmax(a,b) (f2cmax(a,b))
185 #define bit_test(a,b)   ((a) >> (b) & 1)
186 #define bit_clear(a,b)  ((a) & ~((uinteger)1 << (b)))
187 #define bit_set(a,b)    ((a) |  ((uinteger)1 << (b)))
188
189 #define abort_() { sig_die("Fortran abort routine called", 1); }
190 #define c_abs(z) (cabsf(Cf(z)))
191 #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
192 #ifdef _MSC_VER
193 #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
194 #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/df(b)._Val[1]);}
195 #else
196 #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
197 #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
198 #endif
199 #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
200 #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
201 #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
202 //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
203 #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
204 #define d_abs(x) (fabs(*(x)))
205 #define d_acos(x) (acos(*(x)))
206 #define d_asin(x) (asin(*(x)))
207 #define d_atan(x) (atan(*(x)))
208 #define d_atn2(x, y) (atan2(*(x),*(y)))
209 #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
210 #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
211 #define d_cos(x) (cos(*(x)))
212 #define d_cosh(x) (cosh(*(x)))
213 #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
214 #define d_exp(x) (exp(*(x)))
215 #define d_imag(z) (cimag(Cd(z)))
216 #define r_imag(z) (cimagf(Cf(z)))
217 #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
218 #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
219 #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
220 #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
221 #define d_log(x) (log(*(x)))
222 #define d_mod(x, y) (fmod(*(x), *(y)))
223 #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
224 #define d_nint(x) u_nint(*(x))
225 #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
226 #define d_sign(a,b) u_sign(*(a),*(b))
227 #define r_sign(a,b) u_sign(*(a),*(b))
228 #define d_sin(x) (sin(*(x)))
229 #define d_sinh(x) (sinh(*(x)))
230 #define d_sqrt(x) (sqrt(*(x)))
231 #define d_tan(x) (tan(*(x)))
232 #define d_tanh(x) (tanh(*(x)))
233 #define i_abs(x) abs(*(x))
234 #define i_dnnt(x) ((integer)u_nint(*(x)))
235 #define i_len(s, n) (n)
236 #define i_nint(x) ((integer)u_nint(*(x)))
237 #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
238 #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
239 #define pow_si(B,E) spow_ui(*(B),*(E))
240 #define pow_ri(B,E) spow_ui(*(B),*(E))
241 #define pow_di(B,E) dpow_ui(*(B),*(E))
242 #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
243 #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
244 #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
245 #define s_cat(lpp, rpp, rnp, np, llp) {         ftnlen i, nc, ll; char *f__rp, *lp;     ll = (llp); lp = (lpp);         for(i=0; i < (int)*(np); ++i) {                 nc = ll;                if((rnp)[i] < nc) nc = (rnp)[i];                ll -= nc;               f__rp = (rpp)[i];               while(--nc >= 0) *lp++ = *(f__rp)++;         }  while(--ll >= 0) *lp++ = ' '; }
246 #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
247 #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
248 #define sig_die(s, kill) { exit(1); }
249 #define s_stop(s, n) {exit(0);}
250 static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
251 #define z_abs(z) (cabs(Cd(z)))
252 #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
253 #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
254 #define myexit_() break;
255 #define mycycle() continue;
256 #define myceiling(w) {ceil(w)}
257 #define myhuge(w) {HUGE_VAL}
258 //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
259 #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
260
261 /* procedure parameter types for -A and -C++ */
262
263 #define F2C_proc_par_types 1
264 #ifdef __cplusplus
265 typedef logical (*L_fp)(...);
266 #else
267 typedef logical (*L_fp)();
268 #endif
269
270 static float spow_ui(float x, integer n) {
271         float pow=1.0; unsigned long int u;
272         if(n != 0) {
273                 if(n < 0) n = -n, x = 1/x;
274                 for(u = n; ; ) {
275                         if(u & 01) pow *= x;
276                         if(u >>= 1) x *= x;
277                         else break;
278                 }
279         }
280         return pow;
281 }
282 static double dpow_ui(double x, integer n) {
283         double pow=1.0; unsigned long int u;
284         if(n != 0) {
285                 if(n < 0) n = -n, x = 1/x;
286                 for(u = n; ; ) {
287                         if(u & 01) pow *= x;
288                         if(u >>= 1) x *= x;
289                         else break;
290                 }
291         }
292         return pow;
293 }
294 #ifdef _MSC_VER
295 static _Fcomplex cpow_ui(complex x, integer n) {
296         complex pow={1.0,0.0}; unsigned long int u;
297                 if(n != 0) {
298                 if(n < 0) n = -n, x.r = 1/x.r, x.i=1/x.i;
299                 for(u = n; ; ) {
300                         if(u & 01) pow.r *= x.r, pow.i *= x.i;
301                         if(u >>= 1) x.r *= x.r, x.i *= x.i;
302                         else break;
303                 }
304         }
305         _Fcomplex p={pow.r, pow.i};
306         return p;
307 }
308 #else
309 static _Complex float cpow_ui(_Complex float x, integer n) {
310         _Complex float pow=1.0; unsigned long int u;
311         if(n != 0) {
312                 if(n < 0) n = -n, x = 1/x;
313                 for(u = n; ; ) {
314                         if(u & 01) pow *= x;
315                         if(u >>= 1) x *= x;
316                         else break;
317                 }
318         }
319         return pow;
320 }
321 #endif
322 #ifdef _MSC_VER
323 static _Dcomplex zpow_ui(_Dcomplex x, integer n) {
324         _Dcomplex pow={1.0,0.0}; unsigned long int u;
325         if(n != 0) {
326                 if(n < 0) n = -n, x._Val[0] = 1/x._Val[0], x._Val[1] =1/x._Val[1];
327                 for(u = n; ; ) {
328                         if(u & 01) pow._Val[0] *= x._Val[0], pow._Val[1] *= x._Val[1];
329                         if(u >>= 1) x._Val[0] *= x._Val[0], x._Val[1] *= x._Val[1];
330                         else break;
331                 }
332         }
333         _Dcomplex p = {pow._Val[0], pow._Val[1]};
334         return p;
335 }
336 #else
337 static _Complex double zpow_ui(_Complex double x, integer n) {
338         _Complex double pow=1.0; unsigned long int u;
339         if(n != 0) {
340                 if(n < 0) n = -n, x = 1/x;
341                 for(u = n; ; ) {
342                         if(u & 01) pow *= x;
343                         if(u >>= 1) x *= x;
344                         else break;
345                 }
346         }
347         return pow;
348 }
349 #endif
350 static integer pow_ii(integer x, integer n) {
351         integer pow; unsigned long int u;
352         if (n <= 0) {
353                 if (n == 0 || x == 1) pow = 1;
354                 else if (x != -1) pow = x == 0 ? 1/x : 0;
355                 else n = -n;
356         }
357         if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
358                 u = n;
359                 for(pow = 1; ; ) {
360                         if(u & 01) pow *= x;
361                         if(u >>= 1) x *= x;
362                         else break;
363                 }
364         }
365         return pow;
366 }
367 static integer dmaxloc_(double *w, integer s, integer e, integer *n)
368 {
369         double m; integer i, mi;
370         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
371                 if (w[i-1]>m) mi=i ,m=w[i-1];
372         return mi-s+1;
373 }
374 static integer smaxloc_(float *w, integer s, integer e, integer *n)
375 {
376         float m; integer i, mi;
377         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
378                 if (w[i-1]>m) mi=i ,m=w[i-1];
379         return mi-s+1;
380 }
381 static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
382         integer n = *n_, incx = *incx_, incy = *incy_, i;
383 #ifdef _MSC_VER
384         _Fcomplex zdotc = {0.0, 0.0};
385         if (incx == 1 && incy == 1) {
386                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
387                         zdotc._Val[0] += conjf(Cf(&x[i]))._Val[0] * Cf(&y[i])._Val[0];
388                         zdotc._Val[1] += conjf(Cf(&x[i]))._Val[1] * Cf(&y[i])._Val[1];
389                 }
390         } else {
391                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
392                         zdotc._Val[0] += conjf(Cf(&x[i*incx]))._Val[0] * Cf(&y[i*incy])._Val[0];
393                         zdotc._Val[1] += conjf(Cf(&x[i*incx]))._Val[1] * Cf(&y[i*incy])._Val[1];
394                 }
395         }
396         pCf(z) = zdotc;
397 }
398 #else
399         _Complex float zdotc = 0.0;
400         if (incx == 1 && incy == 1) {
401                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
402                         zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
403                 }
404         } else {
405                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
406                         zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
407                 }
408         }
409         pCf(z) = zdotc;
410 }
411 #endif
412 static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
413         integer n = *n_, incx = *incx_, incy = *incy_, i;
414 #ifdef _MSC_VER
415         _Dcomplex zdotc = {0.0, 0.0};
416         if (incx == 1 && incy == 1) {
417                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
418                         zdotc._Val[0] += conj(Cd(&x[i]))._Val[0] * Cd(&y[i])._Val[0];
419                         zdotc._Val[1] += conj(Cd(&x[i]))._Val[1] * Cd(&y[i])._Val[1];
420                 }
421         } else {
422                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
423                         zdotc._Val[0] += conj(Cd(&x[i*incx]))._Val[0] * Cd(&y[i*incy])._Val[0];
424                         zdotc._Val[1] += conj(Cd(&x[i*incx]))._Val[1] * Cd(&y[i*incy])._Val[1];
425                 }
426         }
427         pCd(z) = zdotc;
428 }
429 #else
430         _Complex double zdotc = 0.0;
431         if (incx == 1 && incy == 1) {
432                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
433                         zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
434                 }
435         } else {
436                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
437                         zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
438                 }
439         }
440         pCd(z) = zdotc;
441 }
442 #endif  
443 static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
444         integer n = *n_, incx = *incx_, incy = *incy_, i;
445 #ifdef _MSC_VER
446         _Fcomplex zdotc = {0.0, 0.0};
447         if (incx == 1 && incy == 1) {
448                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
449                         zdotc._Val[0] += Cf(&x[i])._Val[0] * Cf(&y[i])._Val[0];
450                         zdotc._Val[1] += Cf(&x[i])._Val[1] * Cf(&y[i])._Val[1];
451                 }
452         } else {
453                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
454                         zdotc._Val[0] += Cf(&x[i*incx])._Val[0] * Cf(&y[i*incy])._Val[0];
455                         zdotc._Val[1] += Cf(&x[i*incx])._Val[1] * Cf(&y[i*incy])._Val[1];
456                 }
457         }
458         pCf(z) = zdotc;
459 }
460 #else
461         _Complex float zdotc = 0.0;
462         if (incx == 1 && incy == 1) {
463                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
464                         zdotc += Cf(&x[i]) * Cf(&y[i]);
465                 }
466         } else {
467                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
468                         zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
469                 }
470         }
471         pCf(z) = zdotc;
472 }
473 #endif
474 static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
475         integer n = *n_, incx = *incx_, incy = *incy_, i;
476 #ifdef _MSC_VER
477         _Dcomplex zdotc = {0.0, 0.0};
478         if (incx == 1 && incy == 1) {
479                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
480                         zdotc._Val[0] += Cd(&x[i])._Val[0] * Cd(&y[i])._Val[0];
481                         zdotc._Val[1] += Cd(&x[i])._Val[1] * Cd(&y[i])._Val[1];
482                 }
483         } else {
484                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
485                         zdotc._Val[0] += Cd(&x[i*incx])._Val[0] * Cd(&y[i*incy])._Val[0];
486                         zdotc._Val[1] += Cd(&x[i*incx])._Val[1] * Cd(&y[i*incy])._Val[1];
487                 }
488         }
489         pCd(z) = zdotc;
490 }
491 #else
492         _Complex double zdotc = 0.0;
493         if (incx == 1 && incy == 1) {
494                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
495                         zdotc += Cd(&x[i]) * Cd(&y[i]);
496                 }
497         } else {
498                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
499                         zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
500                 }
501         }
502         pCd(z) = zdotc;
503 }
504 #endif
505 /*  -- translated by f2c (version 20000121).
506    You must link the resulting object file with the libraries:
507         -lf2c -lm   (in that order)
508 */
509
510
511
512
513 /* Table of constant values */
514
515 static integer c_n1 = -1;
516 static integer c__1 = 1;
517 static real c_b72 = 0.f;
518 static real c_b76 = 1.f;
519 static integer c__0 = 0;
520 static logical c_false = FALSE_;
521
522 /* > \brief <b> SGESVDQ computes the singular value decomposition (SVD) with a QR-Preconditioned QR SVD Method
523  for GE matrices</b> */
524
525 /*  =========== DOCUMENTATION =========== */
526
527 /* Online html documentation available at */
528 /*            http://www.netlib.org/lapack/explore-html/ */
529
530 /* > \htmlonly */
531 /* > Download SGESVDQ + dependencies */
532 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgesvdq
533 .f"> */
534 /* > [TGZ]</a> */
535 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgesvdq
536 .f"> */
537 /* > [ZIP]</a> */
538 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgesvdq
539 .f"> */
540 /* > [TXT]</a> */
541 /* > \endhtmlonly */
542
543 /*  Definition: */
544 /*  =========== */
545
546 /*      SUBROUTINE SGESVDQ( JOBA, JOBP, JOBR, JOBU, JOBV, M, N, A, LDA, */
547 /*                          S, U, LDU, V, LDV, NUMRANK, IWORK, LIWORK, */
548 /*                          WORK, LWORK, RWORK, LRWORK, INFO ) */
549
550 /*      IMPLICIT    NONE */
551 /*      CHARACTER   JOBA, JOBP, JOBR, JOBU, JOBV */
552 /*      INTEGER     M, N, LDA, LDU, LDV, NUMRANK, LIWORK, LWORK, LRWORK, */
553 /*                  INFO */
554 /*      REAL        A( LDA, * ), U( LDU, * ), V( LDV, * ), WORK( * ) */
555 /*      REAL        S( * ), RWORK( * ) */
556 /*      INTEGER     IWORK( * ) */
557
558
559 /* > \par Purpose: */
560 /*  ============= */
561 /* > */
562 /* > \verbatim */
563 /* > */
564 /* > SGESVDQ computes the singular value decomposition (SVD) of a real */
565 /* > M-by-N matrix A, where M >= N. The SVD of A is written as */
566 /* >                                    [++]   [xx]   [x0]   [xx] */
567 /* >              A = U * SIGMA * V^*,  [++] = [xx] * [ox] * [xx] */
568 /* >                                    [++]   [xx] */
569 /* > where SIGMA is an N-by-N diagonal matrix, U is an M-by-N orthonormal */
570 /* > matrix, and V is an N-by-N orthogonal matrix. The diagonal elements */
571 /* > of SIGMA are the singular values of A. The columns of U and V are the */
572 /* > left and the right singular vectors of A, respectively. */
573 /* > \endverbatim */
574
575 /*  Arguments: */
576 /*  ========== */
577
578 /* > \param[in] JOBA */
579 /* > \verbatim */
580 /* >  JOBA is CHARACTER*1 */
581 /* >  Specifies the level of accuracy in the computed SVD */
582 /* >  = 'A' The requested accuracy corresponds to having the backward */
583 /* >        error bounded by || delta A ||_F <= f(m,n) * EPS * || A ||_F, */
584 /* >        where EPS = SLAMCH('Epsilon'). This authorises CGESVDQ to */
585 /* >        truncate the computed triangular factor in a rank revealing */
586 /* >        QR factorization whenever the truncated part is below the */
587 /* >        threshold of the order of EPS * ||A||_F. This is aggressive */
588 /* >        truncation level. */
589 /* >  = 'M' Similarly as with 'A', but the truncation is more gentle: it */
590 /* >        is allowed only when there is a drop on the diagonal of the */
591 /* >        triangular factor in the QR factorization. This is medium */
592 /* >        truncation level. */
593 /* >  = 'H' High accuracy requested. No numerical rank determination based */
594 /* >        on the rank revealing QR factorization is attempted. */
595 /* >  = 'E' Same as 'H', and in addition the condition number of column */
596 /* >        scaled A is estimated and returned in  RWORK(1). */
597 /* >        N^(-1/4)*RWORK(1) <= ||pinv(A_scaled)||_2 <= N^(1/4)*RWORK(1) */
598 /* > \endverbatim */
599 /* > */
600 /* > \param[in] JOBP */
601 /* > \verbatim */
602 /* >  JOBP is CHARACTER*1 */
603 /* >  = 'P' The rows of A are ordered in decreasing order with respect to */
604 /* >        ||A(i,:)||_\infty. This enhances numerical accuracy at the cost */
605 /* >        of extra data movement. Recommended for numerical robustness. */
606 /* >  = 'N' No row pivoting. */
607 /* > \endverbatim */
608 /* > */
609 /* > \param[in] JOBR */
610 /* > \verbatim */
611 /* >          JOBR is CHARACTER*1 */
612 /* >          = 'T' After the initial pivoted QR factorization, SGESVD is applied to */
613 /* >          the transposed R**T of the computed triangular factor R. This involves */
614 /* >          some extra data movement (matrix transpositions). Useful for */
615 /* >          experiments, research and development. */
616 /* >          = 'N' The triangular factor R is given as input to SGESVD. This may be */
617 /* >          preferred as it involves less data movement. */
618 /* > \endverbatim */
619 /* > */
620 /* > \param[in] JOBU */
621 /* > \verbatim */
622 /* >          JOBU is CHARACTER*1 */
623 /* >          = 'A' All M left singular vectors are computed and returned in the */
624 /* >          matrix U. See the description of U. */
625 /* >          = 'S' or 'U' N = f2cmin(M,N) left singular vectors are computed and returned */
626 /* >          in the matrix U. See the description of U. */
627 /* >          = 'R' Numerical rank NUMRANK is determined and only NUMRANK left singular */
628 /* >          vectors are computed and returned in the matrix U. */
629 /* >          = 'F' The N left singular vectors are returned in factored form as the */
630 /* >          product of the Q factor from the initial QR factorization and the */
631 /* >          N left singular vectors of (R**T , 0)**T. If row pivoting is used, */
632 /* >          then the necessary information on the row pivoting is stored in */
633 /* >          IWORK(N+1:N+M-1). */
634 /* >          = 'N' The left singular vectors are not computed. */
635 /* > \endverbatim */
636 /* > */
637 /* > \param[in] JOBV */
638 /* > \verbatim */
639 /* >          JOBV is CHARACTER*1 */
640 /* >          = 'A', 'V' All N right singular vectors are computed and returned in */
641 /* >          the matrix V. */
642 /* >          = 'R' Numerical rank NUMRANK is determined and only NUMRANK right singular */
643 /* >          vectors are computed and returned in the matrix V. This option is */
644 /* >          allowed only if JOBU = 'R' or JOBU = 'N'; otherwise it is illegal. */
645 /* >          = 'N' The right singular vectors are not computed. */
646 /* > \endverbatim */
647 /* > */
648 /* > \param[in] M */
649 /* > \verbatim */
650 /* >          M is INTEGER */
651 /* >          The number of rows of the input matrix A.  M >= 0. */
652 /* > \endverbatim */
653 /* > */
654 /* > \param[in] N */
655 /* > \verbatim */
656 /* >          N is INTEGER */
657 /* >          The number of columns of the input matrix A.  M >= N >= 0. */
658 /* > \endverbatim */
659 /* > */
660 /* > \param[in,out] A */
661 /* > \verbatim */
662 /* >          A is REAL array of dimensions LDA x N */
663 /* >          On entry, the input matrix A. */
664 /* >          On exit, if JOBU .NE. 'N' or JOBV .NE. 'N', the lower triangle of A contains */
665 /* >          the Householder vectors as stored by SGEQP3. If JOBU = 'F', these Householder */
666 /* >          vectors together with WORK(1:N) can be used to restore the Q factors from */
667 /* >          the initial pivoted QR factorization of A. See the description of U. */
668 /* > \endverbatim */
669 /* > */
670 /* > \param[in] LDA */
671 /* > \verbatim */
672 /* >          LDA is INTEGER. */
673 /* >          The leading dimension of the array A.  LDA >= f2cmax(1,M). */
674 /* > \endverbatim */
675 /* > */
676 /* > \param[out] S */
677 /* > \verbatim */
678 /* >          S is REAL array of dimension N. */
679 /* >          The singular values of A, ordered so that S(i) >= S(i+1). */
680 /* > \endverbatim */
681 /* > */
682 /* > \param[out] U */
683 /* > \verbatim */
684 /* >          U is REAL array, dimension */
685 /* >          LDU x M if JOBU = 'A'; see the description of LDU. In this case, */
686 /* >          on exit, U contains the M left singular vectors. */
687 /* >          LDU x N if JOBU = 'S', 'U', 'R' ; see the description of LDU. In this */
688 /* >          case, U contains the leading N or the leading NUMRANK left singular vectors. */
689 /* >          LDU x N if JOBU = 'F' ; see the description of LDU. In this case U */
690 /* >          contains N x N orthogonal matrix that can be used to form the left */
691 /* >          singular vectors. */
692 /* >          If JOBU = 'N', U is not referenced. */
693 /* > \endverbatim */
694 /* > */
695 /* > \param[in] LDU */
696 /* > \verbatim */
697 /* >          LDU is INTEGER. */
698 /* >          The leading dimension of the array U. */
699 /* >          If JOBU = 'A', 'S', 'U', 'R',  LDU >= f2cmax(1,M). */
700 /* >          If JOBU = 'F',                 LDU >= f2cmax(1,N). */
701 /* >          Otherwise,                     LDU >= 1. */
702 /* > \endverbatim */
703 /* > */
704 /* > \param[out] V */
705 /* > \verbatim */
706 /* >          V is REAL array, dimension */
707 /* >          LDV x N if JOBV = 'A', 'V', 'R' or if JOBA = 'E' . */
708 /* >          If JOBV = 'A', or 'V',  V contains the N-by-N orthogonal matrix  V**T; */
709 /* >          If JOBV = 'R', V contains the first NUMRANK rows of V**T (the right */
710 /* >          singular vectors, stored rowwise, of the NUMRANK largest singular values). */
711 /* >          If JOBV = 'N' and JOBA = 'E', V is used as a workspace. */
712 /* >          If JOBV = 'N', and JOBA.NE.'E', V is not referenced. */
713 /* > \endverbatim */
714 /* > */
715 /* > \param[in] LDV */
716 /* > \verbatim */
717 /* >          LDV is INTEGER */
718 /* >          The leading dimension of the array V. */
719 /* >          If JOBV = 'A', 'V', 'R',  or JOBA = 'E', LDV >= f2cmax(1,N). */
720 /* >          Otherwise,                               LDV >= 1. */
721 /* > \endverbatim */
722 /* > */
723 /* > \param[out] NUMRANK */
724 /* > \verbatim */
725 /* >          NUMRANK is INTEGER */
726 /* >          NUMRANK is the numerical rank first determined after the rank */
727 /* >          revealing QR factorization, following the strategy specified by the */
728 /* >          value of JOBA. If JOBV = 'R' and JOBU = 'R', only NUMRANK */
729 /* >          leading singular values and vectors are then requested in the call */
730 /* >          of SGESVD. The final value of NUMRANK might be further reduced if */
731 /* >          some singular values are computed as zeros. */
732 /* > \endverbatim */
733 /* > */
734 /* > \param[out] IWORK */
735 /* > \verbatim */
736 /* >          IWORK is INTEGER array, dimension (f2cmax(1, LIWORK)). */
737 /* >          On exit, IWORK(1:N) contains column pivoting permutation of the */
738 /* >          rank revealing QR factorization. */
739 /* >          If JOBP = 'P', IWORK(N+1:N+M-1) contains the indices of the sequence */
740 /* >          of row swaps used in row pivoting. These can be used to restore the */
741 /* >          left singular vectors in the case JOBU = 'F'. */
742 /* > */
743 /* >          If LIWORK, LWORK, or LRWORK = -1, then on exit, if INFO = 0, */
744 /* >          LIWORK(1) returns the minimal LIWORK. */
745 /* > \endverbatim */
746 /* > */
747 /* > \param[in] LIWORK */
748 /* > \verbatim */
749 /* >          LIWORK is INTEGER */
750 /* >          The dimension of the array IWORK. */
751 /* >          LIWORK >= N + M - 1,     if JOBP = 'P' and JOBA .NE. 'E'; */
752 /* >          LIWORK >= N              if JOBP = 'N' and JOBA .NE. 'E'; */
753 /* >          LIWORK >= N + M - 1 + N, if JOBP = 'P' and JOBA = 'E'; */
754 /* >          LIWORK >= N + N          if JOBP = 'N' and JOBA = 'E'. */
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 WORK, IWORK, and RWORK arrays, and no error */
759 /* >          message related to LWORK is issued by XERBLA. */
760 /* > \endverbatim */
761 /* > */
762 /* > \param[out] WORK */
763 /* > \verbatim */
764 /* >          WORK is REAL array, dimension (f2cmax(2, LWORK)), used as a workspace. */
765 /* >          On exit, if, on entry, LWORK.NE.-1, WORK(1:N) contains parameters */
766 /* >          needed to recover the Q factor from the QR factorization computed by */
767 /* >          SGEQP3. */
768 /* > */
769 /* >          If LIWORK, LWORK, or LRWORK = -1, then on exit, if INFO = 0, */
770 /* >          WORK(1) returns the optimal LWORK, and */
771 /* >          WORK(2) returns the minimal LWORK. */
772 /* > \endverbatim */
773 /* > */
774 /* > \param[in,out] LWORK */
775 /* > \verbatim */
776 /* >          LWORK is INTEGER */
777 /* >          The dimension of the array WORK. It is determined as follows: */
778 /* >          Let  LWQP3 = 3*N+1,  LWCON = 3*N, and let */
779 /* >          LWORQ = { MAX( N, 1 ),  if JOBU = 'R', 'S', or 'U' */
780 /* >                  { MAX( M, 1 ),  if JOBU = 'A' */
781 /* >          LWSVD = MAX( 5*N, 1 ) */
782 /* >          LWLQF = MAX( N/2, 1 ), LWSVD2 = MAX( 5*(N/2), 1 ), LWORLQ = MAX( N, 1 ), */
783 /* >          LWQRF = MAX( N/2, 1 ), LWORQ2 = MAX( N, 1 ) */
784 /* >          Then the minimal value of LWORK 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, LWORQ ) if the singular values and the left */
790 /* >                                   singular vectors are requested; */
791 /* >          = N + MAX( LWQP3, LWCON, LWSVD, LWORQ ) 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, LWORQ ) if the full SVD is requested with JOBV = 'R'; */
802 /* >                                   independent of JOBR; */
803 /* >          = N + MAX( LWQP3, LWCON, LWSVD, LWORQ ) 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, LWORQ ), */
807 /* >         N + MAX( LWQP3, N/2+LWLQF, N/2+LWSVD2, N/2+LWORLQ, LWORQ) ) if the */
808 /* >                         full SVD is requested with JOBV = 'A' or 'V', and */
809 /* >                         JOBR ='N' */
810 /* >          = MAX( N + MAX( LWQP3, LWCON, LWSVD, LWORQ ), */
811 /* >         N + MAX( LWQP3, LWCON, N/2+LWLQF, N/2+LWSVD2, N/2+LWORLQ, LWORQ ) ) */
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, LWORQ ), */
816 /* >         N + MAX( LWQP3, N/2+LWQRF, N/2+LWSVD2, N/2+LWORQ2, LWORQ ) ) if the */
817 /* >                         full SVD is requested with JOBV = 'A', 'V', and JOBR ='T' */
818 /* >          = MAX( N + MAX( LWQP3, LWCON, LWSVD, LWORQ ), */
819 /* >         N + MAX( LWQP3, LWCON, N/2+LWQRF, N/2+LWSVD2, N/2+LWORQ2, LWORQ ) ) */
820 /* >                         if the full SVD is requested with JOBV = 'A' or 'V', and */
821 /* >                         JOBR ='T', and also a scaled condition number estimate */
822 /* >                         requested. */
823 /* >          Finally, LWORK must be at least two: LWORK = MAX( 2, LWORK ). */
824 /* > */
825 /* >          If LWORK = -1, then a workspace query is assumed; the routine */
826 /* >          only calculates and returns the optimal and minimal sizes */
827 /* >          for the WORK, IWORK, and RWORK arrays, and no error */
828 /* >          message related to LWORK 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 SGESVD applied to the upper triangular or trapeziodal */
842 /* >          R (from the initial QR factorization). In case of early exit (no call to */
843 /* >          SGESVD, such as in the case of zero matrix) RWORK(2) = -1. */
844 /* > */
845 /* >          If LIWORK, LWORK, 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). */
854 /* >          Otherwise, LRWORK >= 2 */
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 WORK, IWORK, and RWORK arrays, and no error */
859 /* >          message related to LWORK 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 SBDSQR did not converge, INFO specifies how many superdiagonals */
868 /* >          of an intermediate bidiagonal form B (computed in SGESVD) 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 realGEsing */
933
934 /*  ===================================================================== */
935 /* Subroutine */ int sgesvdq_(char *joba, char *jobp, char *jobr, char *jobu, 
936         char *jobv, integer *m, integer *n, real *a, integer *lda, real *s, 
937         real *u, integer *ldu, real *v, integer *ldv, integer *numrank, 
938         integer *iwork, integer *liwork, real *work, integer *lwork, real *
939         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     real r__1, r__2, r__3;
944
945     /* Local variables */
946     integer lwrk_sormlq__, lwrk_sormqr__, ierr, lwrk_sgesvd2__;
947     real rtmp;
948     integer lwrk_sormqr2__, optratio;
949     logical lsvc0;
950     extern real snrm2_(integer *, real *, integer *);
951     logical accla;
952     integer lwqp3;
953     logical acclh, acclm;
954     integer p, q;
955     logical conda;
956     extern logical lsame_(char *, char *);
957     extern /* Subroutine */ int sscal_(integer *, real *, real *, integer *);
958     integer iwoff;
959     logical lsvec;
960     real sfmin, epsln;
961     integer lwcon;
962     logical rsvec;
963     integer lwlqf, lwqrf, n1, lwsvd;
964     logical dntwu, dntwv, wntua;
965     integer lworq;
966     logical wntuf, wntva, wntur, wntus, wntvr;
967     extern /* Subroutine */ int sgeqp3_(integer *, integer *, real *, integer 
968             *, integer *, real *, real *, integer *, integer *);
969     integer lwsvd2, lworq2, nr;
970     real sconda;
971     extern real slamch_(char *), slange_(char *, integer *, integer *,
972              real *, integer *, real *);
973     extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen), sgelqf_(
974             integer *, integer *, real *, integer *, real *, real *, integer *
975             , integer *), slascl_(char *, integer *, integer *, real *, real *
976             , integer *, integer *, real *, integer *, integer *);
977     extern integer isamax_(integer *, real *, integer *);
978     extern /* Subroutine */ int sgeqrf_(integer *, integer *, real *, integer 
979             *, real *, real *, integer *, integer *), sgesvd_(char *, char *, 
980             integer *, integer *, real *, integer *, real *, real *, integer *
981             , real *, integer *, real *, integer *, integer *)
982             , slacpy_(char *, integer *, integer *, real *, integer *, real *,
983              integer *), slaset_(char *, integer *, integer *, real *,
984              real *, real *, integer *), slapmt_(logical *, integer *,
985              integer *, real *, integer *, integer *), spocon_(char *, 
986             integer *, real *, integer *, real *, real *, real *, integer *, 
987             integer *);
988     integer minwrk;
989     logical rtrans;
990     extern /* Subroutine */ int slaswp_(integer *, real *, integer *, integer 
991             *, integer *, integer *, integer *);
992     real rdummy[1];
993     extern /* Subroutine */ int sormlq_(char *, char *, integer *, integer *, 
994             integer *, real *, integer *, real *, real *, integer *, real *, 
995             integer *, integer *);
996     logical lquery;
997     integer lwunlq;
998     extern /* Subroutine */ int sormqr_(char *, char *, integer *, integer *, 
999             integer *, real *, integer *, real *, real *, integer *, real *, 
1000             integer *, integer *);
1001     integer optwrk;
1002     logical rowprm;
1003     real big;
1004     integer minwrk2;
1005     logical ascaled;
1006     integer optwrk2, lwrk_sgeqp3__, iminwrk, rminwrk, lwrk_sgelqf__, 
1007             lwrk_sgeqrf__, lwrk_sgesvd__;
1008
1009
1010 /*  ===================================================================== */
1011
1012
1013 /*     Test the input arguments */
1014
1015     /* Parameter adjustments */
1016     a_dim1 = *lda;
1017     a_offset = 1 + a_dim1 * 1;
1018     a -= a_offset;
1019     --s;
1020     u_dim1 = *ldu;
1021     u_offset = 1 + u_dim1 * 1;
1022     u -= u_offset;
1023     v_dim1 = *ldv;
1024     v_offset = 1 + v_dim1 * 1;
1025     v -= v_offset;
1026     --iwork;
1027     --work;
1028     --rwork;
1029
1030     /* Function Body */
1031     wntus = lsame_(jobu, "S") || lsame_(jobu, "U");
1032     wntur = lsame_(jobu, "R");
1033     wntua = lsame_(jobu, "A");
1034     wntuf = lsame_(jobu, "F");
1035     lsvc0 = wntus || wntur || wntua;
1036     lsvec = lsvc0 || wntuf;
1037     dntwu = lsame_(jobu, "N");
1038
1039     wntvr = lsame_(jobv, "R");
1040     wntva = lsame_(jobv, "A") || lsame_(jobv, "V");
1041     rsvec = wntvr || wntva;
1042     dntwv = lsame_(jobv, "N");
1043
1044     accla = lsame_(joba, "A");
1045     acclm = lsame_(joba, "M");
1046     conda = lsame_(joba, "E");
1047     acclh = lsame_(joba, "H") || conda;
1048
1049     rowprm = lsame_(jobp, "P");
1050     rtrans = lsame_(jobr, "T");
1051
1052     if (rowprm) {
1053         if (conda) {
1054 /* Computing MAX */
1055             i__1 = 1, i__2 = *n + *m - 1 + *n;
1056             iminwrk = f2cmax(i__1,i__2);
1057         } else {
1058 /* Computing MAX */
1059             i__1 = 1, i__2 = *n + *m - 1;
1060             iminwrk = f2cmax(i__1,i__2);
1061         }
1062         rminwrk = f2cmax(2,*m);
1063     } else {
1064         if (conda) {
1065 /* Computing MAX */
1066             i__1 = 1, i__2 = *n + *n;
1067             iminwrk = f2cmax(i__1,i__2);
1068         } else {
1069             iminwrk = f2cmax(1,*n);
1070         }
1071         rminwrk = 2;
1072     }
1073     lquery = *liwork == -1 || *lwork == -1 || *lrwork == -1;
1074     *info = 0;
1075     if (! (accla || acclm || acclh)) {
1076         *info = -1;
1077     } else if (! (rowprm || lsame_(jobp, "N"))) {
1078         *info = -2;
1079     } else if (! (rtrans || lsame_(jobr, "N"))) {
1080         *info = -3;
1081     } else if (! (lsvec || dntwu)) {
1082         *info = -4;
1083     } else if (wntur && wntva) {
1084         *info = -5;
1085     } else if (! (rsvec || dntwv)) {
1086         *info = -5;
1087     } else if (*m < 0) {
1088         *info = -6;
1089     } else if (*n < 0 || *n > *m) {
1090         *info = -7;
1091     } else if (*lda < f2cmax(1,*m)) {
1092         *info = -9;
1093     } else if (*ldu < 1 || lsvc0 && *ldu < *m || wntuf && *ldu < *n) {
1094         *info = -12;
1095     } else if (*ldv < 1 || rsvec && *ldv < *n || conda && *ldv < *n) {
1096         *info = -14;
1097     } else if (*liwork < iminwrk && ! lquery) {
1098         *info = -17;
1099     }
1100
1101
1102     if (*info == 0) {
1103 /*        [[The expressions for computing the minimal and the optimal */
1104 /*        values of LWORK are written with a lot of redundancy and */
1105 /*        can be simplified. However, this detailed form is easier for */
1106 /*        maintenance and modifications of the code.]] */
1107
1108         lwqp3 = *n * 3 + 1;
1109         if (wntus || wntur) {
1110             lworq = f2cmax(*n,1);
1111         } else if (wntua) {
1112             lworq = f2cmax(*m,1);
1113         }
1114         lwcon = *n * 3;
1115 /* Computing MAX */
1116         i__1 = *n * 5;
1117         lwsvd = f2cmax(i__1,1);
1118         if (lquery) {
1119             sgeqp3_(m, n, &a[a_offset], lda, &iwork[1], rdummy, rdummy, &c_n1,
1120                      &ierr);
1121             lwrk_sgeqp3__ = (integer) rdummy[0];
1122             if (wntus || wntur) {
1123                 sormqr_("L", "N", m, n, n, &a[a_offset], lda, rdummy, &u[
1124                         u_offset], ldu, rdummy, &c_n1, &ierr);
1125                 lwrk_sormqr__ = (integer) rdummy[0];
1126             } else if (wntua) {
1127                 sormqr_("L", "N", m, m, n, &a[a_offset], lda, rdummy, &u[
1128                         u_offset], ldu, rdummy, &c_n1, &ierr);
1129                 lwrk_sormqr__ = (integer) rdummy[0];
1130             } else {
1131                 lwrk_sormqr__ = 0;
1132             }
1133         }
1134         minwrk = 2;
1135         optwrk = 2;
1136         if (! (lsvec || rsvec)) {
1137 /*            only the singular values are requested */
1138             if (conda) {
1139 /* Computing MAX */
1140                 i__1 = *n + lwqp3, i__1 = f2cmax(i__1,lwcon);
1141                 minwrk = f2cmax(i__1,lwsvd);
1142             } else {
1143 /* Computing MAX */
1144                 i__1 = *n + lwqp3;
1145                 minwrk = f2cmax(i__1,lwsvd);
1146             }
1147             if (lquery) {
1148                 sgesvd_("N", "N", n, n, &a[a_offset], lda, &s[1], &u[u_offset]
1149                         , ldu, &v[v_offset], ldv, rdummy, &c_n1, &ierr);
1150                 lwrk_sgesvd__ = (integer) rdummy[0];
1151                 if (conda) {
1152 /* Computing MAX */
1153                     i__1 = *n + lwrk_sgeqp3__, i__2 = *n + lwcon, i__1 = f2cmax(
1154                             i__1,i__2);
1155                     optwrk = f2cmax(i__1,lwrk_sgesvd__);
1156                 } else {
1157 /* Computing MAX */
1158                     i__1 = *n + lwrk_sgeqp3__;
1159                     optwrk = f2cmax(i__1,lwrk_sgesvd__);
1160                 }
1161             }
1162         } else if (lsvec && ! rsvec) {
1163 /*            singular values and the left singular vectors are requested */
1164             if (conda) {
1165 /* Computing MAX */
1166                 i__1 = f2cmax(lwqp3,lwcon), i__1 = f2cmax(i__1,lwsvd);
1167                 minwrk = *n + f2cmax(i__1,lworq);
1168             } else {
1169 /* Computing MAX */
1170                 i__1 = f2cmax(lwqp3,lwsvd);
1171                 minwrk = *n + f2cmax(i__1,lworq);
1172             }
1173             if (lquery) {
1174                 if (rtrans) {
1175                     sgesvd_("N", "O", n, n, &a[a_offset], lda, &s[1], &u[
1176                             u_offset], ldu, &v[v_offset], ldv, rdummy, &c_n1, 
1177                             &ierr);
1178                 } else {
1179                     sgesvd_("O", "N", n, n, &a[a_offset], lda, &s[1], &u[
1180                             u_offset], ldu, &v[v_offset], ldv, rdummy, &c_n1, 
1181                             &ierr);
1182                 }
1183                 lwrk_sgesvd__ = (integer) rdummy[0];
1184                 if (conda) {
1185 /* Computing MAX */
1186                     i__1 = f2cmax(lwrk_sgeqp3__,lwcon), i__1 = f2cmax(i__1,
1187                             lwrk_sgesvd__);
1188                     optwrk = *n + f2cmax(i__1,lwrk_sormqr__);
1189                 } else {
1190 /* Computing MAX */
1191                     i__1 = f2cmax(lwrk_sgeqp3__,lwrk_sgesvd__);
1192                     optwrk = *n + f2cmax(i__1,lwrk_sormqr__);
1193                 }
1194             }
1195         } else if (rsvec && ! lsvec) {
1196 /*            singular values and the right singular vectors are requested */
1197             if (conda) {
1198 /* Computing MAX */
1199                 i__1 = f2cmax(lwqp3,lwcon);
1200                 minwrk = *n + f2cmax(i__1,lwsvd);
1201             } else {
1202                 minwrk = *n + f2cmax(lwqp3,lwsvd);
1203             }
1204             if (lquery) {
1205                 if (rtrans) {
1206                     sgesvd_("O", "N", n, n, &a[a_offset], lda, &s[1], &u[
1207                             u_offset], ldu, &v[v_offset], ldv, rdummy, &c_n1, 
1208                             &ierr);
1209                 } else {
1210                     sgesvd_("N", "O", n, n, &a[a_offset], lda, &s[1], &u[
1211                             u_offset], ldu, &v[v_offset], ldv, rdummy, &c_n1, 
1212                             &ierr);
1213                 }
1214                 lwrk_sgesvd__ = (integer) rdummy[0];
1215                 if (conda) {
1216 /* Computing MAX */
1217                     i__1 = f2cmax(lwrk_sgeqp3__,lwcon);
1218                     optwrk = *n + f2cmax(i__1,lwrk_sgesvd__);
1219                 } else {
1220                     optwrk = *n + f2cmax(lwrk_sgeqp3__,lwrk_sgesvd__);
1221                 }
1222             }
1223         } else {
1224 /*            full SVD is requested */
1225             if (rtrans) {
1226 /* Computing MAX */
1227                 i__1 = f2cmax(lwqp3,lwsvd);
1228                 minwrk = f2cmax(i__1,lworq);
1229                 if (conda) {
1230                     minwrk = f2cmax(minwrk,lwcon);
1231                 }
1232                 minwrk += *n;
1233                 if (wntva) {
1234 /* Computing MAX */
1235                     i__1 = *n / 2;
1236                     lwqrf = f2cmax(i__1,1);
1237 /* Computing MAX */
1238                     i__1 = *n / 2 * 5;
1239                     lwsvd2 = f2cmax(i__1,1);
1240                     lworq2 = f2cmax(*n,1);
1241 /* Computing MAX */
1242                     i__1 = lwqp3, i__2 = *n / 2 + lwqrf, i__1 = f2cmax(i__1,i__2)
1243                             , i__2 = *n / 2 + lwsvd2, i__1 = f2cmax(i__1,i__2), 
1244                             i__2 = *n / 2 + lworq2, i__1 = f2cmax(i__1,i__2);
1245                     minwrk2 = f2cmax(i__1,lworq);
1246                     if (conda) {
1247                         minwrk2 = f2cmax(minwrk2,lwcon);
1248                     }
1249                     minwrk2 = *n + minwrk2;
1250                     minwrk = f2cmax(minwrk,minwrk2);
1251                 }
1252             } else {
1253 /* Computing MAX */
1254                 i__1 = f2cmax(lwqp3,lwsvd);
1255                 minwrk = f2cmax(i__1,lworq);
1256                 if (conda) {
1257                     minwrk = f2cmax(minwrk,lwcon);
1258                 }
1259                 minwrk += *n;
1260                 if (wntva) {
1261 /* Computing MAX */
1262                     i__1 = *n / 2;
1263                     lwlqf = f2cmax(i__1,1);
1264 /* Computing MAX */
1265                     i__1 = *n / 2 * 5;
1266                     lwsvd2 = f2cmax(i__1,1);
1267                     lwunlq = f2cmax(*n,1);
1268 /* Computing MAX */
1269                     i__1 = lwqp3, i__2 = *n / 2 + lwlqf, i__1 = f2cmax(i__1,i__2)
1270                             , i__2 = *n / 2 + lwsvd2, i__1 = f2cmax(i__1,i__2), 
1271                             i__2 = *n / 2 + lwunlq, i__1 = f2cmax(i__1,i__2);
1272                     minwrk2 = f2cmax(i__1,lworq);
1273                     if (conda) {
1274                         minwrk2 = f2cmax(minwrk2,lwcon);
1275                     }
1276                     minwrk2 = *n + minwrk2;
1277                     minwrk = f2cmax(minwrk,minwrk2);
1278                 }
1279             }
1280             if (lquery) {
1281                 if (rtrans) {
1282                     sgesvd_("O", "A", n, n, &a[a_offset], lda, &s[1], &u[
1283                             u_offset], ldu, &v[v_offset], ldv, rdummy, &c_n1, 
1284                             &ierr);
1285                     lwrk_sgesvd__ = (integer) rdummy[0];
1286 /* Computing MAX */
1287                     i__1 = f2cmax(lwrk_sgeqp3__,lwrk_sgesvd__);
1288                     optwrk = f2cmax(i__1,lwrk_sormqr__);
1289                     if (conda) {
1290                         optwrk = f2cmax(optwrk,lwcon);
1291                     }
1292                     optwrk = *n + optwrk;
1293                     if (wntva) {
1294                         i__1 = *n / 2;
1295                         sgeqrf_(n, &i__1, &u[u_offset], ldu, rdummy, rdummy, &
1296                                 c_n1, &ierr);
1297                         lwrk_sgeqrf__ = (integer) rdummy[0];
1298                         i__1 = *n / 2;
1299                         i__2 = *n / 2;
1300                         sgesvd_("S", "O", &i__1, &i__2, &v[v_offset], ldv, &s[
1301                                 1], &u[u_offset], ldu, &v[v_offset], ldv, 
1302                                 rdummy, &c_n1, &ierr);
1303                         lwrk_sgesvd2__ = (integer) rdummy[0];
1304                         i__1 = *n / 2;
1305                         sormqr_("R", "C", n, n, &i__1, &u[u_offset], ldu, 
1306                                 rdummy, &v[v_offset], ldv, rdummy, &c_n1, &
1307                                 ierr);
1308                         lwrk_sormqr2__ = (integer) rdummy[0];
1309 /* Computing MAX */
1310                         i__1 = lwrk_sgeqp3__, i__2 = *n / 2 + lwrk_sgeqrf__, 
1311                                 i__1 = f2cmax(i__1,i__2), i__2 = *n / 2 + 
1312                                 lwrk_sgesvd2__, i__1 = f2cmax(i__1,i__2), i__2 = 
1313                                 *n / 2 + lwrk_sormqr2__;
1314                         optwrk2 = f2cmax(i__1,i__2);
1315                         if (conda) {
1316                             optwrk2 = f2cmax(optwrk2,lwcon);
1317                         }
1318                         optwrk2 = *n + optwrk2;
1319                         optwrk = f2cmax(optwrk,optwrk2);
1320                     }
1321                 } else {
1322                     sgesvd_("S", "O", n, n, &a[a_offset], lda, &s[1], &u[
1323                             u_offset], ldu, &v[v_offset], ldv, rdummy, &c_n1, 
1324                             &ierr);
1325                     lwrk_sgesvd__ = (integer) rdummy[0];
1326 /* Computing MAX */
1327                     i__1 = f2cmax(lwrk_sgeqp3__,lwrk_sgesvd__);
1328                     optwrk = f2cmax(i__1,lwrk_sormqr__);
1329                     if (conda) {
1330                         optwrk = f2cmax(optwrk,lwcon);
1331                     }
1332                     optwrk = *n + optwrk;
1333                     if (wntva) {
1334                         i__1 = *n / 2;
1335                         sgelqf_(&i__1, n, &u[u_offset], ldu, rdummy, rdummy, &
1336                                 c_n1, &ierr);
1337                         lwrk_sgelqf__ = (integer) rdummy[0];
1338                         i__1 = *n / 2;
1339                         i__2 = *n / 2;
1340                         sgesvd_("S", "O", &i__1, &i__2, &v[v_offset], ldv, &s[
1341                                 1], &u[u_offset], ldu, &v[v_offset], ldv, 
1342                                 rdummy, &c_n1, &ierr);
1343                         lwrk_sgesvd2__ = (integer) rdummy[0];
1344                         i__1 = *n / 2;
1345                         sormlq_("R", "N", n, n, &i__1, &u[u_offset], ldu, 
1346                                 rdummy, &v[v_offset], ldv, rdummy, &c_n1, &
1347                                 ierr);
1348                         lwrk_sormlq__ = (integer) rdummy[0];
1349 /* Computing MAX */
1350                         i__1 = lwrk_sgeqp3__, i__2 = *n / 2 + lwrk_sgelqf__, 
1351                                 i__1 = f2cmax(i__1,i__2), i__2 = *n / 2 + 
1352                                 lwrk_sgesvd2__, i__1 = f2cmax(i__1,i__2), i__2 = 
1353                                 *n / 2 + lwrk_sormlq__;
1354                         optwrk2 = f2cmax(i__1,i__2);
1355                         if (conda) {
1356                             optwrk2 = f2cmax(optwrk2,lwcon);
1357                         }
1358                         optwrk2 = *n + optwrk2;
1359                         optwrk = f2cmax(optwrk,optwrk2);
1360                     }
1361                 }
1362             }
1363         }
1364
1365         minwrk = f2cmax(2,minwrk);
1366         optwrk = f2cmax(2,optwrk);
1367         if (*lwork < minwrk && ! lquery) {
1368             *info = -19;
1369         }
1370
1371     }
1372
1373     if (*info == 0 && *lrwork < rminwrk && ! lquery) {
1374         *info = -21;
1375     }
1376     if (*info != 0) {
1377         i__1 = -(*info);
1378         xerbla_("SGESVDQ", &i__1, (ftnlen)7);
1379         return 0;
1380     } else if (lquery) {
1381
1382 /*     Return optimal workspace */
1383
1384         iwork[1] = iminwrk;
1385         work[1] = (real) optwrk;
1386         work[2] = (real) minwrk;
1387         rwork[1] = (real) rminwrk;
1388         return 0;
1389     }
1390
1391 /*     Quick return if the matrix is void. */
1392
1393     if (*m == 0 || *n == 0) {
1394         return 0;
1395     }
1396
1397     big = slamch_("O");
1398     ascaled = FALSE_;
1399     iwoff = 1;
1400     if (rowprm) {
1401         iwoff = *m;
1402 /*           ell-infinity norm - this enhances numerical robustness in */
1403 /*           the case of differently scaled rows. */
1404         i__1 = *m;
1405         for (p = 1; p <= i__1; ++p) {
1406 /*               RWORK(p) = ABS( A(p,ICAMAX(N,A(p,1),LDA)) ) */
1407 /*               [[SLANGE will return NaN if an entry of the p-th row is Nan]] */
1408             rwork[p] = slange_("M", &c__1, n, &a[p + a_dim1], lda, rdummy);
1409             if (rwork[p] != rwork[p] || rwork[p] * 0.f != 0.f) {
1410                 *info = -8;
1411                 i__2 = -(*info);
1412                 xerbla_("SGESVDQ", &i__2, (ftnlen)7);
1413                 return 0;
1414             }
1415 /* L1904: */
1416         }
1417         i__1 = *m - 1;
1418         for (p = 1; p <= i__1; ++p) {
1419             i__2 = *m - p + 1;
1420             q = isamax_(&i__2, &rwork[p], &c__1) + p - 1;
1421             iwork[*n + p] = q;
1422             if (p != q) {
1423                 rtmp = rwork[p];
1424                 rwork[p] = rwork[q];
1425                 rwork[q] = rtmp;
1426             }
1427 /* L1952: */
1428         }
1429
1430         if (rwork[1] == 0.f) {
1431 /*              Quick return: A is the M x N zero matrix. */
1432             *numrank = 0;
1433             slaset_("G", n, &c__1, &c_b72, &c_b72, &s[1], n);
1434             if (wntus) {
1435                 slaset_("G", m, n, &c_b72, &c_b76, &u[u_offset], ldu);
1436             }
1437             if (wntua) {
1438                 slaset_("G", m, m, &c_b72, &c_b76, &u[u_offset], ldu);
1439             }
1440             if (wntva) {
1441                 slaset_("G", n, n, &c_b72, &c_b76, &v[v_offset], ldv);
1442             }
1443             if (wntuf) {
1444                 slaset_("G", n, &c__1, &c_b72, &c_b72, &work[1], n)
1445                         ;
1446                 slaset_("G", m, n, &c_b72, &c_b76, &u[u_offset], ldu);
1447             }
1448             i__1 = *n;
1449             for (p = 1; p <= i__1; ++p) {
1450                 iwork[p] = p;
1451 /* L5001: */
1452             }
1453             if (rowprm) {
1454                 i__1 = *n + *m - 1;
1455                 for (p = *n + 1; p <= i__1; ++p) {
1456                     iwork[p] = p - *n;
1457 /* L5002: */
1458                 }
1459             }
1460             if (conda) {
1461                 rwork[1] = -1.f;
1462             }
1463             rwork[2] = -1.f;
1464             return 0;
1465         }
1466
1467         if (rwork[1] > big / sqrt((real) (*m))) {
1468 /*               matrix by 1/sqrt(M) if too large entry detected */
1469             r__1 = sqrt((real) (*m));
1470             slascl_("G", &c__0, &c__0, &r__1, &c_b76, m, n, &a[a_offset], lda,
1471                      &ierr);
1472             ascaled = TRUE_;
1473         }
1474         i__1 = *m - 1;
1475         slaswp_(n, &a[a_offset], lda, &c__1, &i__1, &iwork[*n + 1], &c__1);
1476     }
1477
1478 /*    norms overflows during the QR factorization. The SVD procedure should */
1479 /*    have its own scaling to save the singular values from overflows and */
1480 /*    underflows. That depends on the SVD procedure. */
1481
1482     if (! rowprm) {
1483         rtmp = slange_("M", m, n, &a[a_offset], lda, rdummy);
1484         if (rtmp != rtmp || rtmp * 0.f != 0.f) {
1485             *info = -8;
1486             i__1 = -(*info);
1487             xerbla_("SGESVDQ", &i__1, (ftnlen)7);
1488             return 0;
1489         }
1490         if (rtmp > big / sqrt((real) (*m))) {
1491 /*             matrix by 1/sqrt(M) if too large entry detected */
1492             r__1 = sqrt((real) (*m));
1493             slascl_("G", &c__0, &c__0, &r__1, &c_b76, m, n, &a[a_offset], lda,
1494                      &ierr);
1495             ascaled = TRUE_;
1496         }
1497     }
1498
1499
1500 /*     A * P = Q * [ R ] */
1501 /*                 [ 0 ] */
1502
1503     i__1 = *n;
1504     for (p = 1; p <= i__1; ++p) {
1505         iwork[p] = 0;
1506 /* L1963: */
1507     }
1508     i__1 = *lwork - *n;
1509     sgeqp3_(m, n, &a[a_offset], lda, &iwork[1], &work[1], &work[*n + 1], &
1510             i__1, &ierr);
1511
1512 /*    If the user requested accuracy level allows truncation in the */
1513 /*    computed upper triangular factor, the matrix R is examined and, */
1514 /*    if possible, replaced with its leading upper trapezoidal part. */
1515
1516     epsln = slamch_("E");
1517     sfmin = slamch_("S");
1518 /*     SMALL = SFMIN / EPSLN */
1519     nr = *n;
1520
1521     if (accla) {
1522
1523 /*        Standard absolute error bound suffices. All sigma_i with */
1524 /*        sigma_i < N*EPS*||A||_F are flushed to zero. This is an */
1525 /*        aggressive enforcement of lower numerical rank by introducing a */
1526 /*        backward error of the order of N*EPS*||A||_F. */
1527         nr = 1;
1528         rtmp = sqrt((real) (*n)) * epsln;
1529         i__1 = *n;
1530         for (p = 2; p <= i__1; ++p) {
1531             if ((r__2 = a[p + p * a_dim1], abs(r__2)) < rtmp * (r__1 = a[
1532                     a_dim1 + 1], abs(r__1))) {
1533                 goto L3002;
1534             }
1535             ++nr;
1536 /* L3001: */
1537         }
1538 L3002:
1539
1540         ;
1541     } else if (acclm) {
1542 /*        Sudden drop on the diagonal of R is used as the criterion for being */
1543 /*        close-to-rank-deficient. The threshold is set to EPSLN=SLAMCH('E'). */
1544 /*        [[This can be made more flexible by replacing this hard-coded value */
1545 /*        with a user specified threshold.]] Also, the values that underflow */
1546 /*        will be truncated. */
1547         nr = 1;
1548         i__1 = *n;
1549         for (p = 2; p <= i__1; ++p) {
1550             if ((r__2 = a[p + p * a_dim1], abs(r__2)) < epsln * (r__1 = a[p - 
1551                     1 + (p - 1) * a_dim1], abs(r__1)) || (r__3 = a[p + p * 
1552                     a_dim1], abs(r__3)) < sfmin) {
1553                 goto L3402;
1554             }
1555             ++nr;
1556 /* L3401: */
1557         }
1558 L3402:
1559
1560         ;
1561     } else {
1562 /*        obvious case of zero pivots. */
1563 /*        R(i,i)=0 => R(i:N,i:N)=0. */
1564         nr = 1;
1565         i__1 = *n;
1566         for (p = 2; p <= i__1; ++p) {
1567             if ((r__1 = a[p + p * a_dim1], abs(r__1)) == 0.f) {
1568                 goto L3502;
1569             }
1570             ++nr;
1571 /* L3501: */
1572         }
1573 L3502:
1574
1575         if (conda) {
1576 /*           Estimate the scaled condition number of A. Use the fact that it is */
1577 /*           the same as the scaled condition number of R. */
1578             slacpy_("U", n, n, &a[a_offset], lda, &v[v_offset], ldv);
1579 /*              Only the leading NR x NR submatrix of the triangular factor */
1580 /*              is considered. Only if NR=N will this give a reliable error */
1581 /*              bound. However, even for NR < N, this can be used on an */
1582 /*              expert level and obtain useful information in the sense of */
1583 /*              perturbation theory. */
1584             i__1 = nr;
1585             for (p = 1; p <= i__1; ++p) {
1586                 rtmp = snrm2_(&p, &v[p * v_dim1 + 1], &c__1);
1587                 r__1 = 1.f / rtmp;
1588                 sscal_(&p, &r__1, &v[p * v_dim1 + 1], &c__1);
1589 /* L3053: */
1590             }
1591             if (! (lsvec || rsvec)) {
1592                 spocon_("U", &nr, &v[v_offset], ldv, &c_b76, &rtmp, &work[1], 
1593                         &iwork[*n + iwoff], &ierr);
1594             } else {
1595                 spocon_("U", &nr, &v[v_offset], ldv, &c_b76, &rtmp, &work[*n 
1596                         + 1], &iwork[*n + iwoff], &ierr);
1597             }
1598             sconda = 1.f / sqrt(rtmp);
1599 /*           For NR=N, SCONDA is an estimate of SQRT(||(R^* * R)^(-1)||_1), */
1600 /*           N^(-1/4) * SCONDA <= ||R^(-1)||_2 <= N^(1/4) * SCONDA */
1601 /*           See the reference [1] for more details. */
1602         }
1603
1604     }
1605
1606     if (wntur) {
1607         n1 = nr;
1608     } else if (wntus || wntuf) {
1609         n1 = *n;
1610     } else if (wntua) {
1611         n1 = *m;
1612     }
1613
1614     if (! (rsvec || lsvec)) {
1615 /* ....................................................................... */
1616 /* ....................................................................... */
1617         if (rtrans) {
1618
1619 /*           the upper triangle of [A] to zero. */
1620             i__1 = f2cmin(*n,nr);
1621             for (p = 1; p <= i__1; ++p) {
1622                 i__2 = *n;
1623                 for (q = p + 1; q <= i__2; ++q) {
1624                     a[q + p * a_dim1] = a[p + q * a_dim1];
1625                     if (q <= nr) {
1626                         a[p + q * a_dim1] = 0.f;
1627                     }
1628 /* L1147: */
1629                 }
1630 /* L1146: */
1631             }
1632
1633             sgesvd_("N", "N", n, &nr, &a[a_offset], lda, &s[1], &u[u_offset], 
1634                     ldu, &v[v_offset], ldv, &work[1], lwork, info);
1635
1636         } else {
1637
1638
1639             if (nr > 1) {
1640                 i__1 = nr - 1;
1641                 i__2 = nr - 1;
1642                 slaset_("L", &i__1, &i__2, &c_b72, &c_b72, &a[a_dim1 + 2], 
1643                         lda);
1644             }
1645             sgesvd_("N", "N", &nr, n, &a[a_offset], lda, &s[1], &u[u_offset], 
1646                     ldu, &v[v_offset], ldv, &work[1], lwork, info);
1647
1648         }
1649
1650     } else if (lsvec && ! rsvec) {
1651 /* ....................................................................... */
1652 /* ......................................................................."""""""" */
1653         if (rtrans) {
1654 /*            vectors of R */
1655             i__1 = nr;
1656             for (p = 1; p <= i__1; ++p) {
1657                 i__2 = *n;
1658                 for (q = p; q <= i__2; ++q) {
1659                     u[q + p * u_dim1] = a[p + q * a_dim1];
1660 /* L1193: */
1661                 }
1662 /* L1192: */
1663             }
1664             if (nr > 1) {
1665                 i__1 = nr - 1;
1666                 i__2 = nr - 1;
1667                 slaset_("U", &i__1, &i__2, &c_b72, &c_b72, &u[(u_dim1 << 1) + 
1668                         1], ldu);
1669             }
1670 /*           vectors overwrite [U](1:NR,1:NR) as transposed. These */
1671 /*           will be pre-multiplied by Q to build the left singular vectors of A. */
1672             i__1 = *lwork - *n;
1673             sgesvd_("N", "O", n, &nr, &u[u_offset], ldu, &s[1], &u[u_offset], 
1674                     ldu, &u[u_offset], ldu, &work[*n + 1], &i__1, info);
1675
1676             i__1 = nr;
1677             for (p = 1; p <= i__1; ++p) {
1678                 i__2 = nr;
1679                 for (q = p + 1; q <= i__2; ++q) {
1680                     rtmp = u[q + p * u_dim1];
1681                     u[q + p * u_dim1] = u[p + q * u_dim1];
1682                     u[p + q * u_dim1] = rtmp;
1683 /* L1120: */
1684                 }
1685 /* L1119: */
1686             }
1687
1688         } else {
1689             slacpy_("U", &nr, n, &a[a_offset], lda, &u[u_offset], ldu);
1690             if (nr > 1) {
1691                 i__1 = nr - 1;
1692                 i__2 = nr - 1;
1693                 slaset_("L", &i__1, &i__2, &c_b72, &c_b72, &u[u_dim1 + 2], 
1694                         ldu);
1695             }
1696 /*            vectors overwrite [U](1:NR,1:NR) */
1697             i__1 = *lwork - *n;
1698             sgesvd_("O", "N", &nr, n, &u[u_offset], ldu, &s[1], &u[u_offset], 
1699                     ldu, &v[v_offset], ldv, &work[*n + 1], &i__1, info);
1700 /*               R. These will be pre-multiplied by Q to build the left singular */
1701 /*               vectors of A. */
1702         }
1703
1704 /*              (M x NR) or (M x N) or (M x M). */
1705         if (nr < *m && ! wntuf) {
1706             i__1 = *m - nr;
1707             slaset_("A", &i__1, &nr, &c_b72, &c_b72, &u[nr + 1 + u_dim1], ldu);
1708             if (nr < n1) {
1709                 i__1 = n1 - nr;
1710                 slaset_("A", &nr, &i__1, &c_b72, &c_b72, &u[(nr + 1) * u_dim1 
1711                         + 1], ldu);
1712                 i__1 = *m - nr;
1713                 i__2 = n1 - nr;
1714                 slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &u[nr + 1 + (nr + 
1715                         1) * u_dim1], ldu);
1716             }
1717         }
1718
1719 /*           The Q matrix from the first QRF is built into the left singular */
1720 /*           vectors matrix U. */
1721
1722         if (! wntuf) {
1723             i__1 = *lwork - *n;
1724             sormqr_("L", "N", m, &n1, n, &a[a_offset], lda, &work[1], &u[
1725                     u_offset], ldu, &work[*n + 1], &i__1, &ierr);
1726         }
1727         if (rowprm && ! wntuf) {
1728             i__1 = *m - 1;
1729             slaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[*n + 1], &
1730                     c_n1);
1731         }
1732
1733     } else if (rsvec && ! lsvec) {
1734 /* ....................................................................... */
1735 /* ....................................................................... */
1736         if (rtrans) {
1737             i__1 = nr;
1738             for (p = 1; p <= i__1; ++p) {
1739                 i__2 = *n;
1740                 for (q = p; q <= i__2; ++q) {
1741                     v[q + p * v_dim1] = a[p + q * a_dim1];
1742 /* L1166: */
1743                 }
1744 /* L1165: */
1745             }
1746             if (nr > 1) {
1747                 i__1 = nr - 1;
1748                 i__2 = nr - 1;
1749                 slaset_("U", &i__1, &i__2, &c_b72, &c_b72, &v[(v_dim1 << 1) + 
1750                         1], ldv);
1751             }
1752 /*           vectors not computed */
1753             if (wntvr || nr == *n) {
1754                 i__1 = *lwork - *n;
1755                 sgesvd_("O", "N", n, &nr, &v[v_offset], ldv, &s[1], &u[
1756                         u_offset], ldu, &u[u_offset], ldu, &work[*n + 1], &
1757                         i__1, info);
1758
1759                 i__1 = nr;
1760                 for (p = 1; p <= i__1; ++p) {
1761                     i__2 = nr;
1762                     for (q = p + 1; q <= i__2; ++q) {
1763                         rtmp = v[q + p * v_dim1];
1764                         v[q + p * v_dim1] = v[p + q * v_dim1];
1765                         v[p + q * v_dim1] = rtmp;
1766 /* L1122: */
1767                     }
1768 /* L1121: */
1769                 }
1770
1771                 if (nr < *n) {
1772                     i__1 = nr;
1773                     for (p = 1; p <= i__1; ++p) {
1774                         i__2 = *n;
1775                         for (q = nr + 1; q <= i__2; ++q) {
1776                             v[p + q * v_dim1] = v[q + p * v_dim1];
1777 /* L1104: */
1778                         }
1779 /* L1103: */
1780                     }
1781                 }
1782                 slapmt_(&c_false, &nr, n, &v[v_offset], ldv, &iwork[1]);
1783             } else {
1784 /*               [!] This is simple implementation that augments [V](1:N,1:NR) */
1785 /*               by padding a zero block. In the case NR << N, a more efficient */
1786 /*               way is to first use the QR factorization. For more details */
1787 /*               how to implement this, see the " FULL SVD " branch. */
1788                 i__1 = *n - nr;
1789                 slaset_("G", n, &i__1, &c_b72, &c_b72, &v[(nr + 1) * v_dim1 + 
1790                         1], ldv);
1791                 i__1 = *lwork - *n;
1792                 sgesvd_("O", "N", n, n, &v[v_offset], ldv, &s[1], &u[u_offset]
1793                         , ldu, &u[u_offset], ldu, &work[*n + 1], &i__1, info);
1794
1795                 i__1 = *n;
1796                 for (p = 1; p <= i__1; ++p) {
1797                     i__2 = *n;
1798                     for (q = p + 1; q <= i__2; ++q) {
1799                         rtmp = v[q + p * v_dim1];
1800                         v[q + p * v_dim1] = v[p + q * v_dim1];
1801                         v[p + q * v_dim1] = rtmp;
1802 /* L1124: */
1803                     }
1804 /* L1123: */
1805                 }
1806                 slapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
1807             }
1808
1809         } else {
1810             slacpy_("U", &nr, n, &a[a_offset], lda, &v[v_offset], ldv);
1811             if (nr > 1) {
1812                 i__1 = nr - 1;
1813                 i__2 = nr - 1;
1814                 slaset_("L", &i__1, &i__2, &c_b72, &c_b72, &v[v_dim1 + 2], 
1815                         ldv);
1816             }
1817 /*            vectors stored in U(1:NR,1:NR) */
1818             if (wntvr || nr == *n) {
1819                 i__1 = *lwork - *n;
1820                 sgesvd_("N", "O", &nr, n, &v[v_offset], ldv, &s[1], &u[
1821                         u_offset], ldu, &v[v_offset], ldv, &work[*n + 1], &
1822                         i__1, info);
1823                 slapmt_(&c_false, &nr, n, &v[v_offset], ldv, &iwork[1]);
1824             } else {
1825 /*               [!] This is simple implementation that augments [V](1:NR,1:N) */
1826 /*               by padding a zero block. In the case NR << N, a more efficient */
1827 /*               way is to first use the LQ factorization. For more details */
1828 /*               how to implement this, see the " FULL SVD " branch. */
1829                 i__1 = *n - nr;
1830                 slaset_("G", &i__1, n, &c_b72, &c_b72, &v[nr + 1 + v_dim1], 
1831                         ldv);
1832                 i__1 = *lwork - *n;
1833                 sgesvd_("N", "O", n, n, &v[v_offset], ldv, &s[1], &u[u_offset]
1834                         , ldu, &v[v_offset], ldv, &work[*n + 1], &i__1, info);
1835                 slapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
1836             }
1837 /*            vectors of A. */
1838         }
1839
1840     } else {
1841 /* ....................................................................... */
1842 /* ....................................................................... */
1843         if (rtrans) {
1844
1845
1846             if (wntvr || nr == *n) {
1847 /*            vectors of R**T */
1848                 i__1 = nr;
1849                 for (p = 1; p <= i__1; ++p) {
1850                     i__2 = *n;
1851                     for (q = p; q <= i__2; ++q) {
1852                         v[q + p * v_dim1] = a[p + q * a_dim1];
1853 /* L1169: */
1854                     }
1855 /* L1168: */
1856                 }
1857                 if (nr > 1) {
1858                     i__1 = nr - 1;
1859                     i__2 = nr - 1;
1860                     slaset_("U", &i__1, &i__2, &c_b72, &c_b72, &v[(v_dim1 << 
1861                             1) + 1], ldv);
1862                 }
1863
1864 /*           singular vectors of R**T stored in [U](1:NR,1:NR) as transposed */
1865                 i__1 = *lwork - *n;
1866                 sgesvd_("O", "A", n, &nr, &v[v_offset], ldv, &s[1], &v[
1867                         v_offset], ldv, &u[u_offset], ldu, &work[*n + 1], &
1868                         i__1, info);
1869                 i__1 = nr;
1870                 for (p = 1; p <= i__1; ++p) {
1871                     i__2 = nr;
1872                     for (q = p + 1; q <= i__2; ++q) {
1873                         rtmp = v[q + p * v_dim1];
1874                         v[q + p * v_dim1] = v[p + q * v_dim1];
1875                         v[p + q * v_dim1] = rtmp;
1876 /* L1116: */
1877                     }
1878 /* L1115: */
1879                 }
1880                 if (nr < *n) {
1881                     i__1 = nr;
1882                     for (p = 1; p <= i__1; ++p) {
1883                         i__2 = *n;
1884                         for (q = nr + 1; q <= i__2; ++q) {
1885                             v[p + q * v_dim1] = v[q + p * v_dim1];
1886 /* L1102: */
1887                         }
1888 /* L1101: */
1889                     }
1890                 }
1891                 slapmt_(&c_false, &nr, n, &v[v_offset], ldv, &iwork[1]);
1892
1893                 i__1 = nr;
1894                 for (p = 1; p <= i__1; ++p) {
1895                     i__2 = nr;
1896                     for (q = p + 1; q <= i__2; ++q) {
1897                         rtmp = u[q + p * u_dim1];
1898                         u[q + p * u_dim1] = u[p + q * u_dim1];
1899                         u[p + q * u_dim1] = rtmp;
1900 /* L1118: */
1901                     }
1902 /* L1117: */
1903                 }
1904
1905                 if (nr < *m && ! wntuf) {
1906                     i__1 = *m - nr;
1907                     slaset_("A", &i__1, &nr, &c_b72, &c_b72, &u[nr + 1 + 
1908                             u_dim1], ldu);
1909                     if (nr < n1) {
1910                         i__1 = n1 - nr;
1911                         slaset_("A", &nr, &i__1, &c_b72, &c_b72, &u[(nr + 1) *
1912                                  u_dim1 + 1], ldu);
1913                         i__1 = *m - nr;
1914                         i__2 = n1 - nr;
1915                         slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &u[nr + 1 
1916                                 + (nr + 1) * u_dim1], ldu);
1917                     }
1918                 }
1919
1920             } else {
1921 /*            vectors of R**T */
1922 /*               [[The optimal ratio N/NR for using QRF instead of padding */
1923 /*                 with zeros. Here hard coded to 2; it must be at least */
1924 /*                 two due to work space constraints.]] */
1925 /*               OPTRATIO = ILAENV(6, 'SGESVD', 'S' // 'O', NR,N,0,0) */
1926 /*               OPTRATIO = MAX( OPTRATIO, 2 ) */
1927                 optratio = 2;
1928                 if (optratio * nr > *n) {
1929                     i__1 = nr;
1930                     for (p = 1; p <= i__1; ++p) {
1931                         i__2 = *n;
1932                         for (q = p; q <= i__2; ++q) {
1933                             v[q + p * v_dim1] = a[p + q * a_dim1];
1934 /* L1199: */
1935                         }
1936 /* L1198: */
1937                     }
1938                     if (nr > 1) {
1939                         i__1 = nr - 1;
1940                         i__2 = nr - 1;
1941                         slaset_("U", &i__1, &i__2, &c_b72, &c_b72, &v[(v_dim1 
1942                                 << 1) + 1], ldv);
1943                     }
1944
1945                     i__1 = *n - nr;
1946                     slaset_("A", n, &i__1, &c_b72, &c_b72, &v[(nr + 1) * 
1947                             v_dim1 + 1], ldv);
1948                     i__1 = *lwork - *n;
1949                     sgesvd_("O", "A", n, n, &v[v_offset], ldv, &s[1], &v[
1950                             v_offset], ldv, &u[u_offset], ldu, &work[*n + 1], 
1951                             &i__1, info);
1952
1953                     i__1 = *n;
1954                     for (p = 1; p <= i__1; ++p) {
1955                         i__2 = *n;
1956                         for (q = p + 1; q <= i__2; ++q) {
1957                             rtmp = v[q + p * v_dim1];
1958                             v[q + p * v_dim1] = v[p + q * v_dim1];
1959                             v[p + q * v_dim1] = rtmp;
1960 /* L1114: */
1961                         }
1962 /* L1113: */
1963                     }
1964                     slapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
1965 /*              (M x N1), i.e. (M x N) or (M x M). */
1966
1967                     i__1 = *n;
1968                     for (p = 1; p <= i__1; ++p) {
1969                         i__2 = *n;
1970                         for (q = p + 1; q <= i__2; ++q) {
1971                             rtmp = u[q + p * u_dim1];
1972                             u[q + p * u_dim1] = u[p + q * u_dim1];
1973                             u[p + q * u_dim1] = rtmp;
1974 /* L1112: */
1975                         }
1976 /* L1111: */
1977                     }
1978
1979                     if (*n < *m && ! wntuf) {
1980                         i__1 = *m - *n;
1981                         slaset_("A", &i__1, n, &c_b72, &c_b72, &u[*n + 1 + 
1982                                 u_dim1], ldu);
1983                         if (*n < n1) {
1984                             i__1 = n1 - *n;
1985                             slaset_("A", n, &i__1, &c_b72, &c_b72, &u[(*n + 1)
1986                                      * u_dim1 + 1], ldu);
1987                             i__1 = *m - *n;
1988                             i__2 = n1 - *n;
1989                             slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &u[*n 
1990                                     + 1 + (*n + 1) * u_dim1], ldu);
1991                         }
1992                     }
1993                 } else {
1994 /*                  singular vectors of R */
1995                     i__1 = nr;
1996                     for (p = 1; p <= i__1; ++p) {
1997                         i__2 = *n;
1998                         for (q = p; q <= i__2; ++q) {
1999                             u[q + (nr + p) * u_dim1] = a[p + q * a_dim1];
2000 /* L1197: */
2001                         }
2002 /* L1196: */
2003                     }
2004                     if (nr > 1) {
2005                         i__1 = nr - 1;
2006                         i__2 = nr - 1;
2007                         slaset_("U", &i__1, &i__2, &c_b72, &c_b72, &u[(nr + 2)
2008                                  * u_dim1 + 1], ldu);
2009                     }
2010                     i__1 = *lwork - *n - nr;
2011                     sgeqrf_(n, &nr, &u[(nr + 1) * u_dim1 + 1], ldu, &work[*n 
2012                             + 1], &work[*n + nr + 1], &i__1, &ierr);
2013                     i__1 = nr;
2014                     for (p = 1; p <= i__1; ++p) {
2015                         i__2 = *n;
2016                         for (q = 1; q <= i__2; ++q) {
2017                             v[q + p * v_dim1] = u[p + (nr + q) * u_dim1];
2018 /* L1144: */
2019                         }
2020 /* L1143: */
2021                     }
2022                     i__1 = nr - 1;
2023                     i__2 = nr - 1;
2024                     slaset_("U", &i__1, &i__2, &c_b72, &c_b72, &v[(v_dim1 << 
2025                             1) + 1], ldv);
2026                     i__1 = *lwork - *n - nr;
2027                     sgesvd_("S", "O", &nr, &nr, &v[v_offset], ldv, &s[1], &u[
2028                             u_offset], ldu, &v[v_offset], ldv, &work[*n + nr 
2029                             + 1], &i__1, info);
2030                     i__1 = *n - nr;
2031                     slaset_("A", &i__1, &nr, &c_b72, &c_b72, &v[nr + 1 + 
2032                             v_dim1], ldv);
2033                     i__1 = *n - nr;
2034                     slaset_("A", &nr, &i__1, &c_b72, &c_b72, &v[(nr + 1) * 
2035                             v_dim1 + 1], ldv);
2036                     i__1 = *n - nr;
2037                     i__2 = *n - nr;
2038                     slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &v[nr + 1 + (
2039                             nr + 1) * v_dim1], ldv);
2040                     i__1 = *lwork - *n - nr;
2041                     sormqr_("R", "C", n, n, &nr, &u[(nr + 1) * u_dim1 + 1], 
2042                             ldu, &work[*n + 1], &v[v_offset], ldv, &work[*n + 
2043                             nr + 1], &i__1, &ierr);
2044                     slapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
2045 /*                 (M x NR) or (M x N) or (M x M). */
2046                     if (nr < *m && ! wntuf) {
2047                         i__1 = *m - nr;
2048                         slaset_("A", &i__1, &nr, &c_b72, &c_b72, &u[nr + 1 + 
2049                                 u_dim1], ldu);
2050                         if (nr < n1) {
2051                             i__1 = n1 - nr;
2052                             slaset_("A", &nr, &i__1, &c_b72, &c_b72, &u[(nr + 
2053                                     1) * u_dim1 + 1], ldu);
2054                             i__1 = *m - nr;
2055                             i__2 = n1 - nr;
2056                             slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &u[nr 
2057                                     + 1 + (nr + 1) * u_dim1], ldu);
2058                         }
2059                     }
2060                 }
2061             }
2062
2063         } else {
2064
2065
2066             if (wntvr || nr == *n) {
2067                 slacpy_("U", &nr, n, &a[a_offset], lda, &v[v_offset], ldv);
2068                 if (nr > 1) {
2069                     i__1 = nr - 1;
2070                     i__2 = nr - 1;
2071                     slaset_("L", &i__1, &i__2, &c_b72, &c_b72, &v[v_dim1 + 2],
2072                              ldv);
2073                 }
2074 /*               singular vectors of R stored in [U](1:NR,1:NR) */
2075                 i__1 = *lwork - *n;
2076                 sgesvd_("S", "O", &nr, n, &v[v_offset], ldv, &s[1], &u[
2077                         u_offset], ldu, &v[v_offset], ldv, &work[*n + 1], &
2078                         i__1, info);
2079                 slapmt_(&c_false, &nr, n, &v[v_offset], ldv, &iwork[1]);
2080 /*              (M x NR) or (M x N) or (M x M). */
2081                 if (nr < *m && ! wntuf) {
2082                     i__1 = *m - nr;
2083                     slaset_("A", &i__1, &nr, &c_b72, &c_b72, &u[nr + 1 + 
2084                             u_dim1], ldu);
2085                     if (nr < n1) {
2086                         i__1 = n1 - nr;
2087                         slaset_("A", &nr, &i__1, &c_b72, &c_b72, &u[(nr + 1) *
2088                                  u_dim1 + 1], ldu);
2089                         i__1 = *m - nr;
2090                         i__2 = n1 - nr;
2091                         slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &u[nr + 1 
2092                                 + (nr + 1) * u_dim1], ldu);
2093                     }
2094                 }
2095
2096             } else {
2097 /*               is then N1 (N or M) */
2098 /*               [[The optimal ratio N/NR for using LQ instead of padding */
2099 /*                 with zeros. Here hard coded to 2; it must be at least */
2100 /*                 two due to work space constraints.]] */
2101 /*               OPTRATIO = ILAENV(6, 'SGESVD', 'S' // 'O', NR,N,0,0) */
2102 /*               OPTRATIO = MAX( OPTRATIO, 2 ) */
2103                 optratio = 2;
2104                 if (optratio * nr > *n) {
2105                     slacpy_("U", &nr, n, &a[a_offset], lda, &v[v_offset], ldv);
2106                     if (nr > 1) {
2107                         i__1 = nr - 1;
2108                         i__2 = nr - 1;
2109                         slaset_("L", &i__1, &i__2, &c_b72, &c_b72, &v[v_dim1 
2110                                 + 2], ldv);
2111                     }
2112 /*                 singular vectors of R stored in [U](1:NR,1:NR) */
2113                     i__1 = *n - nr;
2114                     slaset_("A", &i__1, n, &c_b72, &c_b72, &v[nr + 1 + v_dim1]
2115                             , ldv);
2116                     i__1 = *lwork - *n;
2117                     sgesvd_("S", "O", n, n, &v[v_offset], ldv, &s[1], &u[
2118                             u_offset], ldu, &v[v_offset], ldv, &work[*n + 1], 
2119                             &i__1, info);
2120                     slapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
2121 /*                 singular vectors of A. The leading N left singular vectors */
2122 /*                 are in [U](1:N,1:N) */
2123 /*                 (M x N1), i.e. (M x N) or (M x M). */
2124                     if (*n < *m && ! wntuf) {
2125                         i__1 = *m - *n;
2126                         slaset_("A", &i__1, n, &c_b72, &c_b72, &u[*n + 1 + 
2127                                 u_dim1], ldu);
2128                         if (*n < n1) {
2129                             i__1 = n1 - *n;
2130                             slaset_("A", n, &i__1, &c_b72, &c_b72, &u[(*n + 1)
2131                                      * u_dim1 + 1], ldu);
2132                             i__1 = *m - *n;
2133                             i__2 = n1 - *n;
2134                             slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &u[*n 
2135                                     + 1 + (*n + 1) * u_dim1], ldu);
2136                         }
2137                     }
2138                 } else {
2139                     slacpy_("U", &nr, n, &a[a_offset], lda, &u[nr + 1 + 
2140                             u_dim1], ldu);
2141                     if (nr > 1) {
2142                         i__1 = nr - 1;
2143                         i__2 = nr - 1;
2144                         slaset_("L", &i__1, &i__2, &c_b72, &c_b72, &u[nr + 2 
2145                                 + u_dim1], ldu);
2146                     }
2147                     i__1 = *lwork - *n - nr;
2148                     sgelqf_(&nr, n, &u[nr + 1 + u_dim1], ldu, &work[*n + 1], &
2149                             work[*n + nr + 1], &i__1, &ierr);
2150                     slacpy_("L", &nr, &nr, &u[nr + 1 + u_dim1], ldu, &v[
2151                             v_offset], ldv);
2152                     if (nr > 1) {
2153                         i__1 = nr - 1;
2154                         i__2 = nr - 1;
2155                         slaset_("U", &i__1, &i__2, &c_b72, &c_b72, &v[(v_dim1 
2156                                 << 1) + 1], ldv);
2157                     }
2158                     i__1 = *lwork - *n - nr;
2159                     sgesvd_("S", "O", &nr, &nr, &v[v_offset], ldv, &s[1], &u[
2160                             u_offset], ldu, &v[v_offset], ldv, &work[*n + nr 
2161                             + 1], &i__1, info);
2162                     i__1 = *n - nr;
2163                     slaset_("A", &i__1, &nr, &c_b72, &c_b72, &v[nr + 1 + 
2164                             v_dim1], ldv);
2165                     i__1 = *n - nr;
2166                     slaset_("A", &nr, &i__1, &c_b72, &c_b72, &v[(nr + 1) * 
2167                             v_dim1 + 1], ldv);
2168                     i__1 = *n - nr;
2169                     i__2 = *n - nr;
2170                     slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &v[nr + 1 + (
2171                             nr + 1) * v_dim1], ldv);
2172                     i__1 = *lwork - *n - nr;
2173                     sormlq_("R", "N", n, n, &nr, &u[nr + 1 + u_dim1], ldu, &
2174                             work[*n + 1], &v[v_offset], ldv, &work[*n + nr + 
2175                             1], &i__1, &ierr);
2176                     slapmt_(&c_false, n, n, &v[v_offset], ldv, &iwork[1]);
2177 /*              (M x NR) or (M x N) or (M x M). */
2178                     if (nr < *m && ! wntuf) {
2179                         i__1 = *m - nr;
2180                         slaset_("A", &i__1, &nr, &c_b72, &c_b72, &u[nr + 1 + 
2181                                 u_dim1], ldu);
2182                         if (nr < n1) {
2183                             i__1 = n1 - nr;
2184                             slaset_("A", &nr, &i__1, &c_b72, &c_b72, &u[(nr + 
2185                                     1) * u_dim1 + 1], ldu);
2186                             i__1 = *m - nr;
2187                             i__2 = n1 - nr;
2188                             slaset_("A", &i__1, &i__2, &c_b72, &c_b76, &u[nr 
2189                                     + 1 + (nr + 1) * u_dim1], ldu);
2190                         }
2191                     }
2192                 }
2193             }
2194         }
2195
2196 /*           The Q matrix from the first QRF is built into the left singular */
2197 /*           vectors matrix U. */
2198
2199         if (! wntuf) {
2200             i__1 = *lwork - *n;
2201             sormqr_("L", "N", m, &n1, n, &a[a_offset], lda, &work[1], &u[
2202                     u_offset], ldu, &work[*n + 1], &i__1, &ierr);
2203         }
2204         if (rowprm && ! wntuf) {
2205             i__1 = *m - 1;
2206             slaswp_(&n1, &u[u_offset], ldu, &c__1, &i__1, &iwork[*n + 1], &
2207                     c_n1);
2208         }
2209
2210 /*     ... end of the "full SVD" branch */
2211     }
2212
2213 /*     Check whether some singular values are returned as zeros, e.g. */
2214 /*     due to underflow, and update the numerical rank. */
2215     p = nr;
2216     for (q = p; q >= 1; --q) {
2217         if (s[q] > 0.f) {
2218             goto L4002;
2219         }
2220         --nr;
2221 /* L4001: */
2222     }
2223 L4002:
2224
2225 /*     singular values are set to zero. */
2226     if (nr < *n) {
2227         i__1 = *n - nr;
2228         slaset_("G", &i__1, &c__1, &c_b72, &c_b72, &s[nr + 1], n);
2229     }
2230 /*     values. */
2231     if (ascaled) {
2232         r__1 = sqrt((real) (*m));
2233         slascl_("G", &c__0, &c__0, &c_b76, &r__1, &nr, &c__1, &s[1], n, &ierr);
2234     }
2235     if (conda) {
2236         rwork[1] = sconda;
2237     }
2238     rwork[2] = (real) (p - nr);
2239 /*     exact zeros in SGESVD() applied to the (possibly truncated) */
2240 /*     full row rank triangular (trapezoidal) factor of A. */
2241     *numrank = nr;
2242
2243     return 0;
2244
2245 /*     End of SGESVDQ */
2246
2247 } /* sgesvdq_ */
2248