C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / zgesvdx.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 doublecomplex c_b1 = {0.,0.};
516 static integer c__6 = 6;
517 static integer c__0 = 0;
518 static integer c__2 = 2;
519 static integer c__1 = 1;
520 static integer c_n1 = -1;
521
522 /* > \brief <b> ZGESVDX computes the singular value decomposition (SVD) for GE matrices</b> */
523
524 /*  =========== DOCUMENTATION =========== */
525
526 /* Online html documentation available at */
527 /*            http://www.netlib.org/lapack/explore-html/ */
528
529 /* > \htmlonly */
530 /* > Download ZGESVDX + dependencies */
531 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/zgesvdx
532 .f"> */
533 /* > [TGZ]</a> */
534 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/zgesvdx
535 .f"> */
536 /* > [ZIP]</a> */
537 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/zgesvdx
538 .f"> */
539 /* > [TXT]</a> */
540 /* > \endhtmlonly */
541
542 /*  Definition: */
543 /*  =========== */
544
545 /*     SUBROUTINE ZGESVDX( JOBU, JOBVT, RANGE, M, N, A, LDA, VL, VU, */
546 /*    $                    IL, IU, NS, S, U, LDU, VT, LDVT, WORK, */
547 /*    $                    LWORK, RWORK, IWORK, INFO ) */
548
549
550 /*      CHARACTER          JOBU, JOBVT, RANGE */
551 /*      INTEGER            IL, INFO, IU, LDA, LDU, LDVT, LWORK, M, N, NS */
552 /*      DOUBLE PRECISION   VL, VU */
553 /*      INTEGER            IWORK( * ) */
554 /*      DOUBLE PRECISION   S( * ), RWORK( * ) */
555 /*      COMPLEX*16         A( LDA, * ), U( LDU, * ), VT( LDVT, * ), */
556 /*     $                   WORK( * ) */
557
558
559 /* > \par Purpose: */
560 /*  ============= */
561 /* > */
562 /* > \verbatim */
563 /* > */
564 /* >  ZGESVDX computes the singular value decomposition (SVD) of a complex */
565 /* >  M-by-N matrix A, optionally computing the left and/or right singular */
566 /* >  vectors. The SVD is written */
567 /* > */
568 /* >      A = U * SIGMA * transpose(V) */
569 /* > */
570 /* >  where SIGMA is an M-by-N matrix which is zero except for its */
571 /* >  f2cmin(m,n) diagonal elements, U is an M-by-M unitary matrix, and */
572 /* >  V is an N-by-N unitary matrix.  The diagonal elements of SIGMA */
573 /* >  are the singular values of A; they are real and non-negative, and */
574 /* >  are returned in descending order.  The first f2cmin(m,n) columns of */
575 /* >  U and V are the left and right singular vectors of A. */
576 /* > */
577 /* >  ZGESVDX uses an eigenvalue problem for obtaining the SVD, which */
578 /* >  allows for the computation of a subset of singular values and */
579 /* >  vectors. See DBDSVDX for details. */
580 /* > */
581 /* >  Note that the routine returns V**T, not V. */
582 /* > \endverbatim */
583
584 /*  Arguments: */
585 /*  ========== */
586
587 /* > \param[in] JOBU */
588 /* > \verbatim */
589 /* >          JOBU is CHARACTER*1 */
590 /* >          Specifies options for computing all or part of the matrix U: */
591 /* >          = 'V':  the first f2cmin(m,n) columns of U (the left singular */
592 /* >                  vectors) or as specified by RANGE are returned in */
593 /* >                  the array U; */
594 /* >          = 'N':  no columns of U (no left singular vectors) are */
595 /* >                  computed. */
596 /* > \endverbatim */
597 /* > */
598 /* > \param[in] JOBVT */
599 /* > \verbatim */
600 /* >          JOBVT is CHARACTER*1 */
601 /* >           Specifies options for computing all or part of the matrix */
602 /* >           V**T: */
603 /* >           = 'V':  the first f2cmin(m,n) rows of V**T (the right singular */
604 /* >                   vectors) or as specified by RANGE are returned in */
605 /* >                   the array VT; */
606 /* >           = 'N':  no rows of V**T (no right singular vectors) are */
607 /* >                   computed. */
608 /* > \endverbatim */
609 /* > */
610 /* > \param[in] RANGE */
611 /* > \verbatim */
612 /* >          RANGE is CHARACTER*1 */
613 /* >          = 'A': all singular values will be found. */
614 /* >          = 'V': all singular values in the half-open interval (VL,VU] */
615 /* >                 will be found. */
616 /* >          = 'I': the IL-th through IU-th singular values will be found. */
617 /* > \endverbatim */
618 /* > */
619 /* > \param[in] M */
620 /* > \verbatim */
621 /* >          M is INTEGER */
622 /* >          The number of rows of the input matrix A.  M >= 0. */
623 /* > \endverbatim */
624 /* > */
625 /* > \param[in] N */
626 /* > \verbatim */
627 /* >          N is INTEGER */
628 /* >          The number of columns of the input matrix A.  N >= 0. */
629 /* > \endverbatim */
630 /* > */
631 /* > \param[in,out] A */
632 /* > \verbatim */
633 /* >          A is COMPLEX*16 array, dimension (LDA,N) */
634 /* >          On entry, the M-by-N matrix A. */
635 /* >          On exit, the contents of A are destroyed. */
636 /* > \endverbatim */
637 /* > */
638 /* > \param[in] LDA */
639 /* > \verbatim */
640 /* >          LDA is INTEGER */
641 /* >          The leading dimension of the array A.  LDA >= f2cmax(1,M). */
642 /* > \endverbatim */
643 /* > */
644 /* > \param[in] VL */
645 /* > \verbatim */
646 /* >          VL is DOUBLE PRECISION */
647 /* >          If RANGE='V', the lower bound of the interval to */
648 /* >          be searched for singular values. VU > VL. */
649 /* >          Not referenced if RANGE = 'A' or 'I'. */
650 /* > \endverbatim */
651 /* > */
652 /* > \param[in] VU */
653 /* > \verbatim */
654 /* >          VU is DOUBLE PRECISION */
655 /* >          If RANGE='V', the upper bound of the interval to */
656 /* >          be searched for singular values. VU > VL. */
657 /* >          Not referenced if RANGE = 'A' or 'I'. */
658 /* > \endverbatim */
659 /* > */
660 /* > \param[in] IL */
661 /* > \verbatim */
662 /* >          IL is INTEGER */
663 /* >          If RANGE='I', the index of the */
664 /* >          smallest singular value to be returned. */
665 /* >          1 <= IL <= IU <= f2cmin(M,N), if f2cmin(M,N) > 0. */
666 /* >          Not referenced if RANGE = 'A' or 'V'. */
667 /* > \endverbatim */
668 /* > */
669 /* > \param[in] IU */
670 /* > \verbatim */
671 /* >          IU is INTEGER */
672 /* >          If RANGE='I', the index of the */
673 /* >          largest singular value to be returned. */
674 /* >          1 <= IL <= IU <= f2cmin(M,N), if f2cmin(M,N) > 0. */
675 /* >          Not referenced if RANGE = 'A' or 'V'. */
676 /* > \endverbatim */
677 /* > */
678 /* > \param[out] NS */
679 /* > \verbatim */
680 /* >          NS is INTEGER */
681 /* >          The total number of singular values found, */
682 /* >          0 <= NS <= f2cmin(M,N). */
683 /* >          If RANGE = 'A', NS = f2cmin(M,N); if RANGE = 'I', NS = IU-IL+1. */
684 /* > \endverbatim */
685 /* > */
686 /* > \param[out] S */
687 /* > \verbatim */
688 /* >          S is DOUBLE PRECISION array, dimension (f2cmin(M,N)) */
689 /* >          The singular values of A, sorted so that S(i) >= S(i+1). */
690 /* > \endverbatim */
691 /* > */
692 /* > \param[out] U */
693 /* > \verbatim */
694 /* >          U is COMPLEX*16 array, dimension (LDU,UCOL) */
695 /* >          If JOBU = 'V', U contains columns of U (the left singular */
696 /* >          vectors, stored columnwise) as specified by RANGE; if */
697 /* >          JOBU = 'N', U is not referenced. */
698 /* >          Note: The user must ensure that UCOL >= NS; if RANGE = 'V', */
699 /* >          the exact value of NS is not known in advance and an upper */
700 /* >          bound must be used. */
701 /* > \endverbatim */
702 /* > */
703 /* > \param[in] LDU */
704 /* > \verbatim */
705 /* >          LDU is INTEGER */
706 /* >          The leading dimension of the array U.  LDU >= 1; if */
707 /* >          JOBU = 'V', LDU >= M. */
708 /* > \endverbatim */
709 /* > */
710 /* > \param[out] VT */
711 /* > \verbatim */
712 /* >          VT is COMPLEX*16 array, dimension (LDVT,N) */
713 /* >          If JOBVT = 'V', VT contains the rows of V**T (the right singular */
714 /* >          vectors, stored rowwise) as specified by RANGE; if JOBVT = 'N', */
715 /* >          VT is not referenced. */
716 /* >          Note: The user must ensure that LDVT >= NS; if RANGE = 'V', */
717 /* >          the exact value of NS is not known in advance and an upper */
718 /* >          bound must be used. */
719 /* > \endverbatim */
720 /* > */
721 /* > \param[in] LDVT */
722 /* > \verbatim */
723 /* >          LDVT is INTEGER */
724 /* >          The leading dimension of the array VT.  LDVT >= 1; if */
725 /* >          JOBVT = 'V', LDVT >= NS (see above). */
726 /* > \endverbatim */
727 /* > */
728 /* > \param[out] WORK */
729 /* > \verbatim */
730 /* >          WORK is COMPLEX*16 array, dimension (MAX(1,LWORK)) */
731 /* >          On exit, if INFO = 0, WORK(1) returns the optimal LWORK; */
732 /* > \endverbatim */
733 /* > */
734 /* > \param[in] LWORK */
735 /* > \verbatim */
736 /* >          LWORK is INTEGER */
737 /* >          The dimension of the array WORK. */
738 /* >          LWORK >= MAX(1,MIN(M,N)*(MIN(M,N)+4)) for the paths (see */
739 /* >          comments inside the code): */
740 /* >             - PATH 1  (M much larger than N) */
741 /* >             - PATH 1t (N much larger than M) */
742 /* >          LWORK >= MAX(1,MIN(M,N)*2+MAX(M,N)) for the other paths. */
743 /* >          For good performance, LWORK should generally be larger. */
744 /* > */
745 /* >          If LWORK = -1, then a workspace query is assumed; the routine */
746 /* >          only calculates the optimal size of the WORK array, returns */
747 /* >          this value as the first entry of the WORK array, and no error */
748 /* >          message related to LWORK is issued by XERBLA. */
749 /* > \endverbatim */
750 /* > */
751 /* > \param[out] RWORK */
752 /* > \verbatim */
753 /* >          RWORK is DOUBLE PRECISION array, dimension (MAX(1,LRWORK)) */
754 /* >          LRWORK >= MIN(M,N)*(MIN(M,N)*2+15*MIN(M,N)). */
755 /* > \endverbatim */
756 /* > */
757 /* > \param[out] IWORK */
758 /* > \verbatim */
759 /* >          IWORK is INTEGER array, dimension (12*MIN(M,N)) */
760 /* >          If INFO = 0, the first NS elements of IWORK are zero. If INFO > 0, */
761 /* >          then IWORK contains the indices of the eigenvectors that failed */
762 /* >          to converge in DBDSVDX/DSTEVX. */
763 /* > \endverbatim */
764 /* > */
765 /* > \param[out] INFO */
766 /* > \verbatim */
767 /* >     INFO is INTEGER */
768 /* >           = 0:  successful exit */
769 /* >           < 0:  if INFO = -i, the i-th argument had an illegal value */
770 /* >           > 0:  if INFO = i, then i eigenvectors failed to converge */
771 /* >                 in DBDSVDX/DSTEVX. */
772 /* >                 if INFO = N*2 + 1, an internal error occurred in */
773 /* >                 DBDSVDX */
774 /* > \endverbatim */
775
776 /*  Authors: */
777 /*  ======== */
778
779 /* > \author Univ. of Tennessee */
780 /* > \author Univ. of California Berkeley */
781 /* > \author Univ. of Colorado Denver */
782 /* > \author NAG Ltd. */
783
784 /* > \date June 2016 */
785
786 /* > \ingroup complex16GEsing */
787
788 /*  ===================================================================== */
789 /* Subroutine */ int zgesvdx_(char *jobu, char *jobvt, char *range, integer *
790         m, integer *n, doublecomplex *a, integer *lda, doublereal *vl, 
791         doublereal *vu, integer *il, integer *iu, integer *ns, doublereal *s, 
792         doublecomplex *u, integer *ldu, doublecomplex *vt, integer *ldvt, 
793         doublecomplex *work, integer *lwork, doublereal *rwork, integer *
794         iwork, integer *info)
795 {
796     /* System generated locals */
797     address a__1[2];
798     integer a_dim1, a_offset, u_dim1, u_offset, vt_dim1, vt_offset, i__1[2], 
799             i__2, i__3, i__4, i__5;
800     doublereal d__1;
801     doublecomplex z__1;
802     char ch__1[2];
803
804     /* Local variables */
805     integer iscl;
806     logical alls, inds;
807     integer ilqf;
808     doublereal anrm;
809     integer ierr, iqrf, itau;
810     char jobz[1];
811     logical vals;
812     integer i__, j, k;
813     extern logical lsame_(char *, char *);
814     integer iltgk, itemp, minmn, itaup, itauq, iutgk, itgkz, mnthr;
815     logical wantu;
816     integer id, ie;
817     extern doublereal dlamch_(char *);
818     extern /* Subroutine */ int dlascl_(char *, integer *, integer *, 
819             doublereal *, doublereal *, integer *, integer *, doublereal *, 
820             integer *, integer *), zgebrd_(integer *, integer *, 
821             doublecomplex *, integer *, doublereal *, doublereal *, 
822             doublecomplex *, doublecomplex *, doublecomplex *, integer *, 
823             integer *), xerbla_(char *, integer *, ftnlen);
824     extern integer ilaenv_(integer *, char *, char *, integer *, integer *, 
825             integer *, integer *, ftnlen, ftnlen);
826     extern doublereal zlange_(char *, integer *, integer *, doublecomplex *, 
827             integer *, doublereal *);
828     doublereal bignum, abstol;
829     extern /* Subroutine */ int zgelqf_(integer *, integer *, doublecomplex *,
830              integer *, doublecomplex *, doublecomplex *, integer *, integer *
831             ), zlascl_(char *, integer *, integer *, doublereal *, doublereal 
832             *, integer *, integer *, doublecomplex *, integer *, integer *);
833     char rngtgk[1];
834     extern /* Subroutine */ int zgeqrf_(integer *, integer *, doublecomplex *,
835              integer *, doublecomplex *, doublecomplex *, integer *, integer *
836             );
837     integer itempr;
838     extern /* Subroutine */ int zlaset_(char *, integer *, integer *, 
839             doublecomplex *, doublecomplex *, doublecomplex *, integer *), zlacpy_(char *, integer *, integer *, doublecomplex *, 
840             integer *, doublecomplex *, integer *);
841     integer minwrk, maxwrk;
842     doublereal smlnum;
843     extern /* Subroutine */ int zunmbr_(char *, char *, char *, integer *, 
844             integer *, integer *, doublecomplex *, integer *, doublecomplex *,
845              doublecomplex *, integer *, doublecomplex *, integer *, integer *
846             );
847     logical lquery, wantvt;
848     extern /* Subroutine */ int zunmlq_(char *, char *, integer *, integer *, 
849             integer *, doublecomplex *, integer *, doublecomplex *, 
850             doublecomplex *, integer *, doublecomplex *, integer *, integer *), zunmqr_(char *, char *, integer *, integer *, 
851             integer *, doublecomplex *, integer *, doublecomplex *, 
852             doublecomplex *, integer *, doublecomplex *, integer *, integer *);
853     doublereal dum[1], eps;
854     extern /* Subroutine */ int dbdsvdx_(char *, char *, char *, integer *, 
855             doublereal *, doublereal *, doublereal *, doublereal *, integer *,
856              integer *, integer *, doublereal *, doublereal *, integer *, 
857             doublereal *, integer *, integer *);
858
859
860 /*  -- LAPACK driver routine (version 3.8.0) -- */
861 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
862 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
863 /*     June 2016 */
864
865
866 /*  ===================================================================== */
867
868
869 /*     Test the input arguments. */
870
871     /* Parameter adjustments */
872     a_dim1 = *lda;
873     a_offset = 1 + a_dim1 * 1;
874     a -= a_offset;
875     --s;
876     u_dim1 = *ldu;
877     u_offset = 1 + u_dim1 * 1;
878     u -= u_offset;
879     vt_dim1 = *ldvt;
880     vt_offset = 1 + vt_dim1 * 1;
881     vt -= vt_offset;
882     --work;
883     --rwork;
884     --iwork;
885
886     /* Function Body */
887     *ns = 0;
888     *info = 0;
889     abstol = dlamch_("S") * 2;
890     lquery = *lwork == -1;
891     minmn = f2cmin(*m,*n);
892     wantu = lsame_(jobu, "V");
893     wantvt = lsame_(jobvt, "V");
894     if (wantu || wantvt) {
895         *(unsigned char *)jobz = 'V';
896     } else {
897         *(unsigned char *)jobz = 'N';
898     }
899     alls = lsame_(range, "A");
900     vals = lsame_(range, "V");
901     inds = lsame_(range, "I");
902
903     *info = 0;
904     if (! lsame_(jobu, "V") && ! lsame_(jobu, "N")) {
905         *info = -1;
906     } else if (! lsame_(jobvt, "V") && ! lsame_(jobvt, 
907             "N")) {
908         *info = -2;
909     } else if (! (alls || vals || inds)) {
910         *info = -3;
911     } else if (*m < 0) {
912         *info = -4;
913     } else if (*n < 0) {
914         *info = -5;
915     } else if (*m > *lda) {
916         *info = -7;
917     } else if (minmn > 0) {
918         if (vals) {
919             if (*vl < 0.) {
920                 *info = -8;
921             } else if (*vu <= *vl) {
922                 *info = -9;
923             }
924         } else if (inds) {
925             if (*il < 1 || *il > f2cmax(1,minmn)) {
926                 *info = -10;
927             } else if (*iu < f2cmin(minmn,*il) || *iu > minmn) {
928                 *info = -11;
929             }
930         }
931         if (*info == 0) {
932             if (wantu && *ldu < *m) {
933                 *info = -15;
934             } else if (wantvt) {
935                 if (inds) {
936                     if (*ldvt < *iu - *il + 1) {
937                         *info = -17;
938                     }
939                 } else if (*ldvt < minmn) {
940                     *info = -17;
941                 }
942             }
943         }
944     }
945
946 /*     Compute workspace */
947 /*     (Note: Comments in the code beginning "Workspace:" describe the */
948 /*     minimal amount of workspace needed at that point in the code, */
949 /*     as well as the preferred amount for good performance. */
950 /*     NB refers to the optimal block size for the immediately */
951 /*     following subroutine, as returned by ILAENV.) */
952
953     if (*info == 0) {
954         minwrk = 1;
955         maxwrk = 1;
956         if (minmn > 0) {
957             if (*m >= *n) {
958 /* Writing concatenation */
959                 i__1[0] = 1, a__1[0] = jobu;
960                 i__1[1] = 1, a__1[1] = jobvt;
961                 s_cat(ch__1, a__1, i__1, &c__2, (ftnlen)2);
962                 mnthr = ilaenv_(&c__6, "ZGESVD", ch__1, m, n, &c__0, &c__0, (
963                         ftnlen)6, (ftnlen)2);
964                 if (*m >= mnthr) {
965
966 /*                 Path 1 (M much larger than N) */
967
968                     minwrk = *n * (*n + 5);
969                     maxwrk = *n + *n * ilaenv_(&c__1, "ZGEQRF", " ", m, n, &
970                             c_n1, &c_n1, (ftnlen)6, (ftnlen)1);
971 /* Computing MAX */
972                     i__2 = maxwrk, i__3 = *n * *n + (*n << 1) + (*n << 1) * 
973                             ilaenv_(&c__1, "ZGEBRD", " ", n, n, &c_n1, &c_n1, 
974                             (ftnlen)6, (ftnlen)1);
975                     maxwrk = f2cmax(i__2,i__3);
976                     if (wantu || wantvt) {
977 /* Computing MAX */
978                         i__2 = maxwrk, i__3 = *n * *n + (*n << 1) + *n * 
979                                 ilaenv_(&c__1, "ZUNMQR", "LN", n, n, n, &c_n1,
980                                  (ftnlen)6, (ftnlen)2);
981                         maxwrk = f2cmax(i__2,i__3);
982                     }
983                 } else {
984
985 /*                 Path 2 (M at least N, but not much larger) */
986
987                     minwrk = *n * 3 + *m;
988                     maxwrk = (*n << 1) + (*m + *n) * ilaenv_(&c__1, "ZGEBRD", 
989                             " ", m, n, &c_n1, &c_n1, (ftnlen)6, (ftnlen)1);
990                     if (wantu || wantvt) {
991 /* Computing MAX */
992                         i__2 = maxwrk, i__3 = (*n << 1) + *n * ilaenv_(&c__1, 
993                                 "ZUNMQR", "LN", n, n, n, &c_n1, (ftnlen)6, (
994                                 ftnlen)2);
995                         maxwrk = f2cmax(i__2,i__3);
996                     }
997                 }
998             } else {
999 /* Writing concatenation */
1000                 i__1[0] = 1, a__1[0] = jobu;
1001                 i__1[1] = 1, a__1[1] = jobvt;
1002                 s_cat(ch__1, a__1, i__1, &c__2, (ftnlen)2);
1003                 mnthr = ilaenv_(&c__6, "ZGESVD", ch__1, m, n, &c__0, &c__0, (
1004                         ftnlen)6, (ftnlen)2);
1005                 if (*n >= mnthr) {
1006
1007 /*                 Path 1t (N much larger than M) */
1008
1009                     minwrk = *m * (*m + 5);
1010                     maxwrk = *m + *m * ilaenv_(&c__1, "ZGELQF", " ", m, n, &
1011                             c_n1, &c_n1, (ftnlen)6, (ftnlen)1);
1012 /* Computing MAX */
1013                     i__2 = maxwrk, i__3 = *m * *m + (*m << 1) + (*m << 1) * 
1014                             ilaenv_(&c__1, "ZGEBRD", " ", m, m, &c_n1, &c_n1, 
1015                             (ftnlen)6, (ftnlen)1);
1016                     maxwrk = f2cmax(i__2,i__3);
1017                     if (wantu || wantvt) {
1018 /* Computing MAX */
1019                         i__2 = maxwrk, i__3 = *m * *m + (*m << 1) + *m * 
1020                                 ilaenv_(&c__1, "ZUNMQR", "LN", m, m, m, &c_n1,
1021                                  (ftnlen)6, (ftnlen)2);
1022                         maxwrk = f2cmax(i__2,i__3);
1023                     }
1024                 } else {
1025
1026 /*                 Path 2t (N greater than M, but not much larger) */
1027
1028
1029                     minwrk = *m * 3 + *n;
1030                     maxwrk = (*m << 1) + (*m + *n) * ilaenv_(&c__1, "ZGEBRD", 
1031                             " ", m, n, &c_n1, &c_n1, (ftnlen)6, (ftnlen)1);
1032                     if (wantu || wantvt) {
1033 /* Computing MAX */
1034                         i__2 = maxwrk, i__3 = (*m << 1) + *m * ilaenv_(&c__1, 
1035                                 "ZUNMQR", "LN", m, m, m, &c_n1, (ftnlen)6, (
1036                                 ftnlen)2);
1037                         maxwrk = f2cmax(i__2,i__3);
1038                     }
1039                 }
1040             }
1041         }
1042         maxwrk = f2cmax(maxwrk,minwrk);
1043         d__1 = (doublereal) maxwrk;
1044         z__1.r = d__1, z__1.i = 0.;
1045         work[1].r = z__1.r, work[1].i = z__1.i;
1046
1047         if (*lwork < minwrk && ! lquery) {
1048             *info = -19;
1049         }
1050     }
1051
1052     if (*info != 0) {
1053         i__2 = -(*info);
1054         xerbla_("ZGESVDX", &i__2, (ftnlen)7);
1055         return 0;
1056     } else if (lquery) {
1057         return 0;
1058     }
1059
1060 /*     Quick return if possible */
1061
1062     if (*m == 0 || *n == 0) {
1063         return 0;
1064     }
1065
1066 /*     Set singular values indices accord to RANGE='A'. */
1067
1068     if (alls) {
1069         *(unsigned char *)rngtgk = 'I';
1070         iltgk = 1;
1071         iutgk = f2cmin(*m,*n);
1072     } else if (inds) {
1073         *(unsigned char *)rngtgk = 'I';
1074         iltgk = *il;
1075         iutgk = *iu;
1076     } else {
1077         *(unsigned char *)rngtgk = 'V';
1078         iltgk = 0;
1079         iutgk = 0;
1080     }
1081
1082 /*     Get machine constants */
1083
1084     eps = dlamch_("P");
1085     smlnum = sqrt(dlamch_("S")) / eps;
1086     bignum = 1. / smlnum;
1087
1088 /*     Scale A if f2cmax element outside range [SMLNUM,BIGNUM] */
1089
1090     anrm = zlange_("M", m, n, &a[a_offset], lda, dum);
1091     iscl = 0;
1092     if (anrm > 0. && anrm < smlnum) {
1093         iscl = 1;
1094         zlascl_("G", &c__0, &c__0, &anrm, &smlnum, m, n, &a[a_offset], lda, 
1095                 info);
1096     } else if (anrm > bignum) {
1097         iscl = 1;
1098         zlascl_("G", &c__0, &c__0, &anrm, &bignum, m, n, &a[a_offset], lda, 
1099                 info);
1100     }
1101
1102     if (*m >= *n) {
1103
1104 /*        A has at least as many rows as columns. If A has sufficiently */
1105 /*        more rows than columns, first reduce A using the QR */
1106 /*        decomposition. */
1107
1108         if (*m >= mnthr) {
1109
1110 /*           Path 1 (M much larger than N): */
1111 /*           A = Q * R = Q * ( QB * B * PB**T ) */
1112 /*                     = Q * ( QB * ( UB * S * VB**T ) * PB**T ) */
1113 /*           U = Q * QB * UB; V**T = VB**T * PB**T */
1114
1115 /*           Compute A=Q*R */
1116 /*           (Workspace: need 2*N, prefer N+N*NB) */
1117
1118             itau = 1;
1119             itemp = itau + *n;
1120             i__2 = *lwork - itemp + 1;
1121             zgeqrf_(m, n, &a[a_offset], lda, &work[itau], &work[itemp], &i__2,
1122                      info);
1123
1124 /*           Copy R into WORK and bidiagonalize it: */
1125 /*           (Workspace: need N*N+3*N, prefer N*N+N+2*N*NB) */
1126
1127             iqrf = itemp;
1128             itauq = itemp + *n * *n;
1129             itaup = itauq + *n;
1130             itemp = itaup + *n;
1131             id = 1;
1132             ie = id + *n;
1133             itgkz = ie + *n;
1134             zlacpy_("U", n, n, &a[a_offset], lda, &work[iqrf], n);
1135             i__2 = *n - 1;
1136             i__3 = *n - 1;
1137             zlaset_("L", &i__2, &i__3, &c_b1, &c_b1, &work[iqrf + 1], n);
1138             i__2 = *lwork - itemp + 1;
1139             zgebrd_(n, n, &work[iqrf], n, &rwork[id], &rwork[ie], &work[itauq]
1140                     , &work[itaup], &work[itemp], &i__2, info);
1141             itempr = itgkz + *n * ((*n << 1) + 1);
1142
1143 /*           Solve eigenvalue problem TGK*Z=Z*S. */
1144 /*           (Workspace: need 2*N*N+14*N) */
1145
1146             i__2 = *n << 1;
1147             dbdsvdx_("U", jobz, rngtgk, n, &rwork[id], &rwork[ie], vl, vu, &
1148                     iltgk, &iutgk, ns, &s[1], &rwork[itgkz], &i__2, &rwork[
1149                     itempr], &iwork[1], info)
1150                     ;
1151
1152 /*           If needed, compute left singular vectors. */
1153
1154             if (wantu) {
1155                 k = itgkz;
1156                 i__2 = *ns;
1157                 for (i__ = 1; i__ <= i__2; ++i__) {
1158                     i__3 = *n;
1159                     for (j = 1; j <= i__3; ++j) {
1160                         i__4 = j + i__ * u_dim1;
1161                         i__5 = k;
1162                         z__1.r = rwork[i__5], z__1.i = 0.;
1163                         u[i__4].r = z__1.r, u[i__4].i = z__1.i;
1164                         ++k;
1165                     }
1166                     k += *n;
1167                 }
1168                 i__2 = *m - *n;
1169                 zlaset_("A", &i__2, ns, &c_b1, &c_b1, &u[*n + 1 + u_dim1], 
1170                         ldu);
1171
1172 /*              Call ZUNMBR to compute QB*UB. */
1173 /*              (Workspace in WORK( ITEMP ): need N, prefer N*NB) */
1174
1175                 i__2 = *lwork - itemp + 1;
1176                 zunmbr_("Q", "L", "N", n, ns, n, &work[iqrf], n, &work[itauq],
1177                          &u[u_offset], ldu, &work[itemp], &i__2, info);
1178
1179 /*              Call ZUNMQR to compute Q*(QB*UB). */
1180 /*              (Workspace in WORK( ITEMP ): need N, prefer N*NB) */
1181
1182                 i__2 = *lwork - itemp + 1;
1183                 zunmqr_("L", "N", m, ns, n, &a[a_offset], lda, &work[itau], &
1184                         u[u_offset], ldu, &work[itemp], &i__2, info);
1185             }
1186
1187 /*           If needed, compute right singular vectors. */
1188
1189             if (wantvt) {
1190                 k = itgkz + *n;
1191                 i__2 = *ns;
1192                 for (i__ = 1; i__ <= i__2; ++i__) {
1193                     i__3 = *n;
1194                     for (j = 1; j <= i__3; ++j) {
1195                         i__4 = i__ + j * vt_dim1;
1196                         i__5 = k;
1197                         z__1.r = rwork[i__5], z__1.i = 0.;
1198                         vt[i__4].r = z__1.r, vt[i__4].i = z__1.i;
1199                         ++k;
1200                     }
1201                     k += *n;
1202                 }
1203
1204 /*              Call ZUNMBR to compute VB**T * PB**T */
1205 /*              (Workspace in WORK( ITEMP ): need N, prefer N*NB) */
1206
1207                 i__2 = *lwork - itemp + 1;
1208                 zunmbr_("P", "R", "C", ns, n, n, &work[iqrf], n, &work[itaup],
1209                          &vt[vt_offset], ldvt, &work[itemp], &i__2, info);
1210             }
1211         } else {
1212
1213 /*           Path 2 (M at least N, but not much larger) */
1214 /*           Reduce A to bidiagonal form without QR decomposition */
1215 /*           A = QB * B * PB**T = QB * ( UB * S * VB**T ) * PB**T */
1216 /*           U = QB * UB; V**T = VB**T * PB**T */
1217
1218 /*           Bidiagonalize A */
1219 /*           (Workspace: need 2*N+M, prefer 2*N+(M+N)*NB) */
1220
1221             itauq = 1;
1222             itaup = itauq + *n;
1223             itemp = itaup + *n;
1224             id = 1;
1225             ie = id + *n;
1226             itgkz = ie + *n;
1227             i__2 = *lwork - itemp + 1;
1228             zgebrd_(m, n, &a[a_offset], lda, &rwork[id], &rwork[ie], &work[
1229                     itauq], &work[itaup], &work[itemp], &i__2, info);
1230             itempr = itgkz + *n * ((*n << 1) + 1);
1231
1232 /*           Solve eigenvalue problem TGK*Z=Z*S. */
1233 /*           (Workspace: need 2*N*N+14*N) */
1234
1235             i__2 = *n << 1;
1236             dbdsvdx_("U", jobz, rngtgk, n, &rwork[id], &rwork[ie], vl, vu, &
1237                     iltgk, &iutgk, ns, &s[1], &rwork[itgkz], &i__2, &rwork[
1238                     itempr], &iwork[1], info)
1239                     ;
1240
1241 /*           If needed, compute left singular vectors. */
1242
1243             if (wantu) {
1244                 k = itgkz;
1245                 i__2 = *ns;
1246                 for (i__ = 1; i__ <= i__2; ++i__) {
1247                     i__3 = *n;
1248                     for (j = 1; j <= i__3; ++j) {
1249                         i__4 = j + i__ * u_dim1;
1250                         i__5 = k;
1251                         z__1.r = rwork[i__5], z__1.i = 0.;
1252                         u[i__4].r = z__1.r, u[i__4].i = z__1.i;
1253                         ++k;
1254                     }
1255                     k += *n;
1256                 }
1257                 i__2 = *m - *n;
1258                 zlaset_("A", &i__2, ns, &c_b1, &c_b1, &u[*n + 1 + u_dim1], 
1259                         ldu);
1260
1261 /*              Call ZUNMBR to compute QB*UB. */
1262 /*              (Workspace in WORK( ITEMP ): need N, prefer N*NB) */
1263
1264                 i__2 = *lwork - itemp + 1;
1265                 zunmbr_("Q", "L", "N", m, ns, n, &a[a_offset], lda, &work[
1266                         itauq], &u[u_offset], ldu, &work[itemp], &i__2, &ierr);
1267             }
1268
1269 /*           If needed, compute right singular vectors. */
1270
1271             if (wantvt) {
1272                 k = itgkz + *n;
1273                 i__2 = *ns;
1274                 for (i__ = 1; i__ <= i__2; ++i__) {
1275                     i__3 = *n;
1276                     for (j = 1; j <= i__3; ++j) {
1277                         i__4 = i__ + j * vt_dim1;
1278                         i__5 = k;
1279                         z__1.r = rwork[i__5], z__1.i = 0.;
1280                         vt[i__4].r = z__1.r, vt[i__4].i = z__1.i;
1281                         ++k;
1282                     }
1283                     k += *n;
1284                 }
1285
1286 /*              Call ZUNMBR to compute VB**T * PB**T */
1287 /*              (Workspace in WORK( ITEMP ): need N, prefer N*NB) */
1288
1289                 i__2 = *lwork - itemp + 1;
1290                 zunmbr_("P", "R", "C", ns, n, n, &a[a_offset], lda, &work[
1291                         itaup], &vt[vt_offset], ldvt, &work[itemp], &i__2, &
1292                         ierr);
1293             }
1294         }
1295     } else {
1296
1297 /*        A has more columns than rows. If A has sufficiently more */
1298 /*        columns than rows, first reduce A using the LQ decomposition. */
1299
1300         if (*n >= mnthr) {
1301
1302 /*           Path 1t (N much larger than M): */
1303 /*           A = L * Q = ( QB * B * PB**T ) * Q */
1304 /*                     = ( QB * ( UB * S * VB**T ) * PB**T ) * Q */
1305 /*           U = QB * UB ; V**T = VB**T * PB**T * Q */
1306
1307 /*           Compute A=L*Q */
1308 /*           (Workspace: need 2*M, prefer M+M*NB) */
1309
1310             itau = 1;
1311             itemp = itau + *m;
1312             i__2 = *lwork - itemp + 1;
1313             zgelqf_(m, n, &a[a_offset], lda, &work[itau], &work[itemp], &i__2,
1314                      info);
1315 /*           Copy L into WORK and bidiagonalize it: */
1316 /*           (Workspace in WORK( ITEMP ): need M*M+3*M, prefer M*M+M+2*M*NB) */
1317
1318             ilqf = itemp;
1319             itauq = ilqf + *m * *m;
1320             itaup = itauq + *m;
1321             itemp = itaup + *m;
1322             id = 1;
1323             ie = id + *m;
1324             itgkz = ie + *m;
1325             zlacpy_("L", m, m, &a[a_offset], lda, &work[ilqf], m);
1326             i__2 = *m - 1;
1327             i__3 = *m - 1;
1328             zlaset_("U", &i__2, &i__3, &c_b1, &c_b1, &work[ilqf + *m], m);
1329             i__2 = *lwork - itemp + 1;
1330             zgebrd_(m, m, &work[ilqf], m, &rwork[id], &rwork[ie], &work[itauq]
1331                     , &work[itaup], &work[itemp], &i__2, info);
1332             itempr = itgkz + *m * ((*m << 1) + 1);
1333
1334 /*           Solve eigenvalue problem TGK*Z=Z*S. */
1335 /*           (Workspace: need 2*M*M+14*M) */
1336
1337             i__2 = *m << 1;
1338             dbdsvdx_("U", jobz, rngtgk, m, &rwork[id], &rwork[ie], vl, vu, &
1339                     iltgk, &iutgk, ns, &s[1], &rwork[itgkz], &i__2, &rwork[
1340                     itempr], &iwork[1], info)
1341                     ;
1342
1343 /*           If needed, compute left singular vectors. */
1344
1345             if (wantu) {
1346                 k = itgkz;
1347                 i__2 = *ns;
1348                 for (i__ = 1; i__ <= i__2; ++i__) {
1349                     i__3 = *m;
1350                     for (j = 1; j <= i__3; ++j) {
1351                         i__4 = j + i__ * u_dim1;
1352                         i__5 = k;
1353                         z__1.r = rwork[i__5], z__1.i = 0.;
1354                         u[i__4].r = z__1.r, u[i__4].i = z__1.i;
1355                         ++k;
1356                     }
1357                     k += *m;
1358                 }
1359
1360 /*              Call ZUNMBR to compute QB*UB. */
1361 /*              (Workspace in WORK( ITEMP ): need M, prefer M*NB) */
1362
1363                 i__2 = *lwork - itemp + 1;
1364                 zunmbr_("Q", "L", "N", m, ns, m, &work[ilqf], m, &work[itauq],
1365                          &u[u_offset], ldu, &work[itemp], &i__2, info);
1366             }
1367
1368 /*           If needed, compute right singular vectors. */
1369
1370             if (wantvt) {
1371                 k = itgkz + *m;
1372                 i__2 = *ns;
1373                 for (i__ = 1; i__ <= i__2; ++i__) {
1374                     i__3 = *m;
1375                     for (j = 1; j <= i__3; ++j) {
1376                         i__4 = i__ + j * vt_dim1;
1377                         i__5 = k;
1378                         z__1.r = rwork[i__5], z__1.i = 0.;
1379                         vt[i__4].r = z__1.r, vt[i__4].i = z__1.i;
1380                         ++k;
1381                     }
1382                     k += *m;
1383                 }
1384                 i__2 = *n - *m;
1385                 zlaset_("A", ns, &i__2, &c_b1, &c_b1, &vt[(*m + 1) * vt_dim1 
1386                         + 1], ldvt);
1387
1388 /*              Call ZUNMBR to compute (VB**T)*(PB**T) */
1389 /*              (Workspace in WORK( ITEMP ): need M, prefer M*NB) */
1390
1391                 i__2 = *lwork - itemp + 1;
1392                 zunmbr_("P", "R", "C", ns, m, m, &work[ilqf], m, &work[itaup],
1393                          &vt[vt_offset], ldvt, &work[itemp], &i__2, info);
1394
1395 /*              Call ZUNMLQ to compute ((VB**T)*(PB**T))*Q. */
1396 /*              (Workspace in WORK( ITEMP ): need M, prefer M*NB) */
1397
1398                 i__2 = *lwork - itemp + 1;
1399                 zunmlq_("R", "N", ns, n, m, &a[a_offset], lda, &work[itau], &
1400                         vt[vt_offset], ldvt, &work[itemp], &i__2, info);
1401             }
1402         } else {
1403
1404 /*           Path 2t (N greater than M, but not much larger) */
1405 /*           Reduce to bidiagonal form without LQ decomposition */
1406 /*           A = QB * B * PB**T = QB * ( UB * S * VB**T ) * PB**T */
1407 /*           U = QB * UB; V**T = VB**T * PB**T */
1408
1409 /*           Bidiagonalize A */
1410 /*           (Workspace: need 2*M+N, prefer 2*M+(M+N)*NB) */
1411
1412             itauq = 1;
1413             itaup = itauq + *m;
1414             itemp = itaup + *m;
1415             id = 1;
1416             ie = id + *m;
1417             itgkz = ie + *m;
1418             i__2 = *lwork - itemp + 1;
1419             zgebrd_(m, n, &a[a_offset], lda, &rwork[id], &rwork[ie], &work[
1420                     itauq], &work[itaup], &work[itemp], &i__2, info);
1421             itempr = itgkz + *m * ((*m << 1) + 1);
1422
1423 /*           Solve eigenvalue problem TGK*Z=Z*S. */
1424 /*           (Workspace: need 2*M*M+14*M) */
1425
1426             i__2 = *m << 1;
1427             dbdsvdx_("L", jobz, rngtgk, m, &rwork[id], &rwork[ie], vl, vu, &
1428                     iltgk, &iutgk, ns, &s[1], &rwork[itgkz], &i__2, &rwork[
1429                     itempr], &iwork[1], info)
1430                     ;
1431
1432 /*           If needed, compute left singular vectors. */
1433
1434             if (wantu) {
1435                 k = itgkz;
1436                 i__2 = *ns;
1437                 for (i__ = 1; i__ <= i__2; ++i__) {
1438                     i__3 = *m;
1439                     for (j = 1; j <= i__3; ++j) {
1440                         i__4 = j + i__ * u_dim1;
1441                         i__5 = k;
1442                         z__1.r = rwork[i__5], z__1.i = 0.;
1443                         u[i__4].r = z__1.r, u[i__4].i = z__1.i;
1444                         ++k;
1445                     }
1446                     k += *m;
1447                 }
1448
1449 /*              Call ZUNMBR to compute QB*UB. */
1450 /*              (Workspace in WORK( ITEMP ): need M, prefer M*NB) */
1451
1452                 i__2 = *lwork - itemp + 1;
1453                 zunmbr_("Q", "L", "N", m, ns, n, &a[a_offset], lda, &work[
1454                         itauq], &u[u_offset], ldu, &work[itemp], &i__2, info);
1455             }
1456
1457 /*           If needed, compute right singular vectors. */
1458
1459             if (wantvt) {
1460                 k = itgkz + *m;
1461                 i__2 = *ns;
1462                 for (i__ = 1; i__ <= i__2; ++i__) {
1463                     i__3 = *m;
1464                     for (j = 1; j <= i__3; ++j) {
1465                         i__4 = i__ + j * vt_dim1;
1466                         i__5 = k;
1467                         z__1.r = rwork[i__5], z__1.i = 0.;
1468                         vt[i__4].r = z__1.r, vt[i__4].i = z__1.i;
1469                         ++k;
1470                     }
1471                     k += *m;
1472                 }
1473                 i__2 = *n - *m;
1474                 zlaset_("A", ns, &i__2, &c_b1, &c_b1, &vt[(*m + 1) * vt_dim1 
1475                         + 1], ldvt);
1476
1477 /*              Call ZUNMBR to compute VB**T * PB**T */
1478 /*              (Workspace in WORK( ITEMP ): need M, prefer M*NB) */
1479
1480                 i__2 = *lwork - itemp + 1;
1481                 zunmbr_("P", "R", "C", ns, n, m, &a[a_offset], lda, &work[
1482                         itaup], &vt[vt_offset], ldvt, &work[itemp], &i__2, 
1483                         info);
1484             }
1485         }
1486     }
1487
1488 /*     Undo scaling if necessary */
1489
1490     if (iscl == 1) {
1491         if (anrm > bignum) {
1492             dlascl_("G", &c__0, &c__0, &bignum, &anrm, &minmn, &c__1, &s[1], &
1493                     minmn, info);
1494         }
1495         if (anrm < smlnum) {
1496             dlascl_("G", &c__0, &c__0, &smlnum, &anrm, &minmn, &c__1, &s[1], &
1497                     minmn, info);
1498         }
1499     }
1500
1501 /*     Return optimal workspace in WORK(1) */
1502
1503     d__1 = (doublereal) maxwrk;
1504     z__1.r = d__1, z__1.i = 0.;
1505     work[1].r = z__1.r, work[1].i = z__1.i;
1506
1507     return 0;
1508
1509 /*     End of ZGESVDX */
1510
1511 } /* zgesvdx_ */
1512