C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / zlalsd.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]/Cd(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__1 = 1;
517 static integer c__0 = 0;
518 static doublereal c_b10 = 1.;
519 static doublereal c_b35 = 0.;
520
521 /* > \brief \b ZLALSD uses the singular value decomposition of A to solve the least squares problem. */
522
523 /*  =========== DOCUMENTATION =========== */
524
525 /* Online html documentation available at */
526 /*            http://www.netlib.org/lapack/explore-html/ */
527
528 /* > \htmlonly */
529 /* > Download ZLALSD + dependencies */
530 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/zlalsd.
531 f"> */
532 /* > [TGZ]</a> */
533 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/zlalsd.
534 f"> */
535 /* > [ZIP]</a> */
536 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/zlalsd.
537 f"> */
538 /* > [TXT]</a> */
539 /* > \endhtmlonly */
540
541 /*  Definition: */
542 /*  =========== */
543
544 /*       SUBROUTINE ZLALSD( UPLO, SMLSIZ, N, NRHS, D, E, B, LDB, RCOND, */
545 /*                          RANK, WORK, RWORK, IWORK, INFO ) */
546
547 /*       CHARACTER          UPLO */
548 /*       INTEGER            INFO, LDB, N, NRHS, RANK, SMLSIZ */
549 /*       DOUBLE PRECISION   RCOND */
550 /*       INTEGER            IWORK( * ) */
551 /*       DOUBLE PRECISION   D( * ), E( * ), RWORK( * ) */
552 /*       COMPLEX*16         B( LDB, * ), WORK( * ) */
553
554
555 /* > \par Purpose: */
556 /*  ============= */
557 /* > */
558 /* > \verbatim */
559 /* > */
560 /* > ZLALSD uses the singular value decomposition of A to solve the least */
561 /* > squares problem of finding X to minimize the Euclidean norm of each */
562 /* > column of A*X-B, where A is N-by-N upper bidiagonal, and X and B */
563 /* > are N-by-NRHS. The solution X overwrites B. */
564 /* > */
565 /* > The singular values of A smaller than RCOND times the largest */
566 /* > singular value are treated as zero in solving the least squares */
567 /* > problem; in this case a minimum norm solution is returned. */
568 /* > The actual singular values are returned in D in ascending order. */
569 /* > */
570 /* > This code makes very mild assumptions about floating point */
571 /* > arithmetic. It will work on machines with a guard digit in */
572 /* > add/subtract, or on those binary machines without guard digits */
573 /* > which subtract like the Cray XMP, Cray YMP, Cray C 90, or Cray 2. */
574 /* > It could conceivably fail on hexadecimal or decimal machines */
575 /* > without guard digits, but we know of none. */
576 /* > \endverbatim */
577
578 /*  Arguments: */
579 /*  ========== */
580
581 /* > \param[in] UPLO */
582 /* > \verbatim */
583 /* >          UPLO is CHARACTER*1 */
584 /* >         = 'U': D and E define an upper bidiagonal matrix. */
585 /* >         = 'L': D and E define a  lower bidiagonal matrix. */
586 /* > \endverbatim */
587 /* > */
588 /* > \param[in] SMLSIZ */
589 /* > \verbatim */
590 /* >          SMLSIZ is INTEGER */
591 /* >         The maximum size of the subproblems at the bottom of the */
592 /* >         computation tree. */
593 /* > \endverbatim */
594 /* > */
595 /* > \param[in] N */
596 /* > \verbatim */
597 /* >          N is INTEGER */
598 /* >         The dimension of the  bidiagonal matrix.  N >= 0. */
599 /* > \endverbatim */
600 /* > */
601 /* > \param[in] NRHS */
602 /* > \verbatim */
603 /* >          NRHS is INTEGER */
604 /* >         The number of columns of B. NRHS must be at least 1. */
605 /* > \endverbatim */
606 /* > */
607 /* > \param[in,out] D */
608 /* > \verbatim */
609 /* >          D is DOUBLE PRECISION array, dimension (N) */
610 /* >         On entry D contains the main diagonal of the bidiagonal */
611 /* >         matrix. On exit, if INFO = 0, D contains its singular values. */
612 /* > \endverbatim */
613 /* > */
614 /* > \param[in,out] E */
615 /* > \verbatim */
616 /* >          E is DOUBLE PRECISION array, dimension (N-1) */
617 /* >         Contains the super-diagonal entries of the bidiagonal matrix. */
618 /* >         On exit, E has been destroyed. */
619 /* > \endverbatim */
620 /* > */
621 /* > \param[in,out] B */
622 /* > \verbatim */
623 /* >          B is COMPLEX*16 array, dimension (LDB,NRHS) */
624 /* >         On input, B contains the right hand sides of the least */
625 /* >         squares problem. On output, B contains the solution X. */
626 /* > \endverbatim */
627 /* > */
628 /* > \param[in] LDB */
629 /* > \verbatim */
630 /* >          LDB is INTEGER */
631 /* >         The leading dimension of B in the calling subprogram. */
632 /* >         LDB must be at least f2cmax(1,N). */
633 /* > \endverbatim */
634 /* > */
635 /* > \param[in] RCOND */
636 /* > \verbatim */
637 /* >          RCOND is DOUBLE PRECISION */
638 /* >         The singular values of A less than or equal to RCOND times */
639 /* >         the largest singular value are treated as zero in solving */
640 /* >         the least squares problem. If RCOND is negative, */
641 /* >         machine precision is used instead. */
642 /* >         For example, if diag(S)*X=B were the least squares problem, */
643 /* >         where diag(S) is a diagonal matrix of singular values, the */
644 /* >         solution would be X(i) = B(i) / S(i) if S(i) is greater than */
645 /* >         RCOND*f2cmax(S), and X(i) = 0 if S(i) is less than or equal to */
646 /* >         RCOND*f2cmax(S). */
647 /* > \endverbatim */
648 /* > */
649 /* > \param[out] RANK */
650 /* > \verbatim */
651 /* >          RANK is INTEGER */
652 /* >         The number of singular values of A greater than RCOND times */
653 /* >         the largest singular value. */
654 /* > \endverbatim */
655 /* > */
656 /* > \param[out] WORK */
657 /* > \verbatim */
658 /* >          WORK is COMPLEX*16 array, dimension (N * NRHS) */
659 /* > \endverbatim */
660 /* > */
661 /* > \param[out] RWORK */
662 /* > \verbatim */
663 /* >          RWORK is DOUBLE PRECISION array, dimension at least */
664 /* >         (9*N + 2*N*SMLSIZ + 8*N*NLVL + 3*SMLSIZ*NRHS + */
665 /* >         MAX( (SMLSIZ+1)**2, N*(1+NRHS) + 2*NRHS ), */
666 /* >         where */
667 /* >         NLVL = MAX( 0, INT( LOG_2( MIN( M,N )/(SMLSIZ+1) ) ) + 1 ) */
668 /* > \endverbatim */
669 /* > */
670 /* > \param[out] IWORK */
671 /* > \verbatim */
672 /* >          IWORK is INTEGER array, dimension at least */
673 /* >         (3*N*NLVL + 11*N). */
674 /* > \endverbatim */
675 /* > */
676 /* > \param[out] INFO */
677 /* > \verbatim */
678 /* >          INFO is INTEGER */
679 /* >         = 0:  successful exit. */
680 /* >         < 0:  if INFO = -i, the i-th argument had an illegal value. */
681 /* >         > 0:  The algorithm failed to compute a singular value while */
682 /* >               working on the submatrix lying in rows and columns */
683 /* >               INFO/(N+1) through MOD(INFO,N+1). */
684 /* > \endverbatim */
685
686 /*  Authors: */
687 /*  ======== */
688
689 /* > \author Univ. of Tennessee */
690 /* > \author Univ. of California Berkeley */
691 /* > \author Univ. of Colorado Denver */
692 /* > \author NAG Ltd. */
693
694 /* > \date June 2017 */
695
696 /* > \ingroup complex16OTHERcomputational */
697
698 /* > \par Contributors: */
699 /*  ================== */
700 /* > */
701 /* >     Ming Gu and Ren-Cang Li, Computer Science Division, University of */
702 /* >       California at Berkeley, USA \n */
703 /* >     Osni Marques, LBNL/NERSC, USA \n */
704
705 /*  ===================================================================== */
706 /* Subroutine */ int zlalsd_(char *uplo, integer *smlsiz, integer *n, integer 
707         *nrhs, doublereal *d__, doublereal *e, doublecomplex *b, integer *ldb,
708          doublereal *rcond, integer *rank, doublecomplex *work, doublereal *
709         rwork, integer *iwork, integer *info)
710 {
711     /* System generated locals */
712     integer b_dim1, b_offset, i__1, i__2, i__3, i__4, i__5, i__6;
713     doublereal d__1;
714     doublecomplex z__1;
715
716     /* Local variables */
717     integer difl, difr;
718     doublereal rcnd;
719     integer jcol, irwb, perm, nsub, nlvl, sqre, bxst, jrow, irwu, c__, i__, j,
720              k;
721     doublereal r__;
722     integer s, u, jimag;
723     extern /* Subroutine */ int dgemm_(char *, char *, integer *, integer *, 
724             integer *, doublereal *, doublereal *, integer *, doublereal *, 
725             integer *, doublereal *, doublereal *, integer *);
726     integer z__, jreal, irwib, poles, sizei, irwrb, nsize;
727     extern /* Subroutine */ int zdrot_(integer *, doublecomplex *, integer *, 
728             doublecomplex *, integer *, doublereal *, doublereal *), zcopy_(
729             integer *, doublecomplex *, integer *, doublecomplex *, integer *)
730             ;
731     integer irwvt, icmpq1, icmpq2;
732     doublereal cs;
733     extern doublereal dlamch_(char *);
734     extern /* Subroutine */ int dlasda_(integer *, integer *, integer *, 
735             integer *, doublereal *, doublereal *, doublereal *, integer *, 
736             doublereal *, integer *, doublereal *, doublereal *, doublereal *,
737              doublereal *, integer *, integer *, integer *, integer *, 
738             doublereal *, doublereal *, doublereal *, doublereal *, integer *,
739              integer *);
740     integer bx;
741     doublereal sn;
742     extern /* Subroutine */ int dlascl_(char *, integer *, integer *, 
743             doublereal *, doublereal *, integer *, integer *, doublereal *, 
744             integer *, integer *);
745     extern integer idamax_(integer *, doublereal *, integer *);
746     integer st;
747     extern /* Subroutine */ int dlasdq_(char *, integer *, integer *, integer 
748             *, integer *, integer *, doublereal *, doublereal *, doublereal *,
749              integer *, doublereal *, integer *, doublereal *, integer *, 
750             doublereal *, integer *);
751     integer vt;
752     extern /* Subroutine */ int dlaset_(char *, integer *, integer *, 
753             doublereal *, doublereal *, doublereal *, integer *), 
754             dlartg_(doublereal *, doublereal *, doublereal *, doublereal *, 
755             doublereal *), xerbla_(char *, integer *, ftnlen);
756     integer givcol;
757     extern doublereal dlanst_(char *, integer *, doublereal *, doublereal *);
758     extern /* Subroutine */ int zlalsa_(integer *, integer *, integer *, 
759             integer *, doublecomplex *, integer *, doublecomplex *, integer *,
760              doublereal *, integer *, doublereal *, integer *, doublereal *, 
761             doublereal *, doublereal *, doublereal *, integer *, integer *, 
762             integer *, integer *, doublereal *, doublereal *, doublereal *, 
763             doublereal *, integer *, integer *), zlascl_(char *, integer *, 
764             integer *, doublereal *, doublereal *, integer *, integer *, 
765             doublecomplex *, integer *, integer *), dlasrt_(char *, 
766             integer *, doublereal *, integer *), zlacpy_(char *, 
767             integer *, integer *, doublecomplex *, integer *, doublecomplex *,
768              integer *), zlaset_(char *, integer *, integer *, 
769             doublecomplex *, doublecomplex *, doublecomplex *, integer *);
770     doublereal orgnrm;
771     integer givnum, givptr, nm1, nrwork, irwwrk, smlszp, st1;
772     doublereal eps;
773     integer iwk;
774     doublereal tol;
775
776
777 /*  -- LAPACK computational routine (version 3.7.1) -- */
778 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
779 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
780 /*     June 2017 */
781
782
783 /*  ===================================================================== */
784
785
786 /*     Test the input parameters. */
787
788     /* Parameter adjustments */
789     --d__;
790     --e;
791     b_dim1 = *ldb;
792     b_offset = 1 + b_dim1 * 1;
793     b -= b_offset;
794     --work;
795     --rwork;
796     --iwork;
797
798     /* Function Body */
799     *info = 0;
800
801     if (*n < 0) {
802         *info = -3;
803     } else if (*nrhs < 1) {
804         *info = -4;
805     } else if (*ldb < 1 || *ldb < *n) {
806         *info = -8;
807     }
808     if (*info != 0) {
809         i__1 = -(*info);
810         xerbla_("ZLALSD", &i__1, (ftnlen)6);
811         return 0;
812     }
813
814     eps = dlamch_("Epsilon");
815
816 /*     Set up the tolerance. */
817
818     if (*rcond <= 0. || *rcond >= 1.) {
819         rcnd = eps;
820     } else {
821         rcnd = *rcond;
822     }
823
824     *rank = 0;
825
826 /*     Quick return if possible. */
827
828     if (*n == 0) {
829         return 0;
830     } else if (*n == 1) {
831         if (d__[1] == 0.) {
832             zlaset_("A", &c__1, nrhs, &c_b1, &c_b1, &b[b_offset], ldb);
833         } else {
834             *rank = 1;
835             zlascl_("G", &c__0, &c__0, &d__[1], &c_b10, &c__1, nrhs, &b[
836                     b_offset], ldb, info);
837             d__[1] = abs(d__[1]);
838         }
839         return 0;
840     }
841
842 /*     Rotate the matrix if it is lower bidiagonal. */
843
844     if (*(unsigned char *)uplo == 'L') {
845         i__1 = *n - 1;
846         for (i__ = 1; i__ <= i__1; ++i__) {
847             dlartg_(&d__[i__], &e[i__], &cs, &sn, &r__);
848             d__[i__] = r__;
849             e[i__] = sn * d__[i__ + 1];
850             d__[i__ + 1] = cs * d__[i__ + 1];
851             if (*nrhs == 1) {
852                 zdrot_(&c__1, &b[i__ + b_dim1], &c__1, &b[i__ + 1 + b_dim1], &
853                         c__1, &cs, &sn);
854             } else {
855                 rwork[(i__ << 1) - 1] = cs;
856                 rwork[i__ * 2] = sn;
857             }
858 /* L10: */
859         }
860         if (*nrhs > 1) {
861             i__1 = *nrhs;
862             for (i__ = 1; i__ <= i__1; ++i__) {
863                 i__2 = *n - 1;
864                 for (j = 1; j <= i__2; ++j) {
865                     cs = rwork[(j << 1) - 1];
866                     sn = rwork[j * 2];
867                     zdrot_(&c__1, &b[j + i__ * b_dim1], &c__1, &b[j + 1 + i__ 
868                             * b_dim1], &c__1, &cs, &sn);
869 /* L20: */
870                 }
871 /* L30: */
872             }
873         }
874     }
875
876 /*     Scale. */
877
878     nm1 = *n - 1;
879     orgnrm = dlanst_("M", n, &d__[1], &e[1]);
880     if (orgnrm == 0.) {
881         zlaset_("A", n, nrhs, &c_b1, &c_b1, &b[b_offset], ldb);
882         return 0;
883     }
884
885     dlascl_("G", &c__0, &c__0, &orgnrm, &c_b10, n, &c__1, &d__[1], n, info);
886     dlascl_("G", &c__0, &c__0, &orgnrm, &c_b10, &nm1, &c__1, &e[1], &nm1, 
887             info);
888
889 /*     If N is smaller than the minimum divide size SMLSIZ, then solve */
890 /*     the problem with another solver. */
891
892     if (*n <= *smlsiz) {
893         irwu = 1;
894         irwvt = irwu + *n * *n;
895         irwwrk = irwvt + *n * *n;
896         irwrb = irwwrk;
897         irwib = irwrb + *n * *nrhs;
898         irwb = irwib + *n * *nrhs;
899         dlaset_("A", n, n, &c_b35, &c_b10, &rwork[irwu], n);
900         dlaset_("A", n, n, &c_b35, &c_b10, &rwork[irwvt], n);
901         dlasdq_("U", &c__0, n, n, n, &c__0, &d__[1], &e[1], &rwork[irwvt], n, 
902                 &rwork[irwu], n, &rwork[irwwrk], &c__1, &rwork[irwwrk], info);
903         if (*info != 0) {
904             return 0;
905         }
906
907 /*        In the real version, B is passed to DLASDQ and multiplied */
908 /*        internally by Q**H. Here B is complex and that product is */
909 /*        computed below in two steps (real and imaginary parts). */
910
911         j = irwb - 1;
912         i__1 = *nrhs;
913         for (jcol = 1; jcol <= i__1; ++jcol) {
914             i__2 = *n;
915             for (jrow = 1; jrow <= i__2; ++jrow) {
916                 ++j;
917                 i__3 = jrow + jcol * b_dim1;
918                 rwork[j] = b[i__3].r;
919 /* L40: */
920             }
921 /* L50: */
922         }
923         dgemm_("T", "N", n, nrhs, n, &c_b10, &rwork[irwu], n, &rwork[irwb], n,
924                  &c_b35, &rwork[irwrb], n);
925         j = irwb - 1;
926         i__1 = *nrhs;
927         for (jcol = 1; jcol <= i__1; ++jcol) {
928             i__2 = *n;
929             for (jrow = 1; jrow <= i__2; ++jrow) {
930                 ++j;
931                 rwork[j] = d_imag(&b[jrow + jcol * b_dim1]);
932 /* L60: */
933             }
934 /* L70: */
935         }
936         dgemm_("T", "N", n, nrhs, n, &c_b10, &rwork[irwu], n, &rwork[irwb], n,
937                  &c_b35, &rwork[irwib], n);
938         jreal = irwrb - 1;
939         jimag = irwib - 1;
940         i__1 = *nrhs;
941         for (jcol = 1; jcol <= i__1; ++jcol) {
942             i__2 = *n;
943             for (jrow = 1; jrow <= i__2; ++jrow) {
944                 ++jreal;
945                 ++jimag;
946                 i__3 = jrow + jcol * b_dim1;
947                 i__4 = jreal;
948                 i__5 = jimag;
949                 z__1.r = rwork[i__4], z__1.i = rwork[i__5];
950                 b[i__3].r = z__1.r, b[i__3].i = z__1.i;
951 /* L80: */
952             }
953 /* L90: */
954         }
955
956         tol = rcnd * (d__1 = d__[idamax_(n, &d__[1], &c__1)], abs(d__1));
957         i__1 = *n;
958         for (i__ = 1; i__ <= i__1; ++i__) {
959             if (d__[i__] <= tol) {
960                 zlaset_("A", &c__1, nrhs, &c_b1, &c_b1, &b[i__ + b_dim1], ldb);
961             } else {
962                 zlascl_("G", &c__0, &c__0, &d__[i__], &c_b10, &c__1, nrhs, &b[
963                         i__ + b_dim1], ldb, info);
964                 ++(*rank);
965             }
966 /* L100: */
967         }
968
969 /*        Since B is complex, the following call to DGEMM is performed */
970 /*        in two steps (real and imaginary parts). That is for V * B */
971 /*        (in the real version of the code V**H is stored in WORK). */
972
973 /*        CALL DGEMM( 'T', 'N', N, NRHS, N, ONE, WORK, N, B, LDB, ZERO, */
974 /*    $               WORK( NWORK ), N ) */
975
976         j = irwb - 1;
977         i__1 = *nrhs;
978         for (jcol = 1; jcol <= i__1; ++jcol) {
979             i__2 = *n;
980             for (jrow = 1; jrow <= i__2; ++jrow) {
981                 ++j;
982                 i__3 = jrow + jcol * b_dim1;
983                 rwork[j] = b[i__3].r;
984 /* L110: */
985             }
986 /* L120: */
987         }
988         dgemm_("T", "N", n, nrhs, n, &c_b10, &rwork[irwvt], n, &rwork[irwb], 
989                 n, &c_b35, &rwork[irwrb], n);
990         j = irwb - 1;
991         i__1 = *nrhs;
992         for (jcol = 1; jcol <= i__1; ++jcol) {
993             i__2 = *n;
994             for (jrow = 1; jrow <= i__2; ++jrow) {
995                 ++j;
996                 rwork[j] = d_imag(&b[jrow + jcol * b_dim1]);
997 /* L130: */
998             }
999 /* L140: */
1000         }
1001         dgemm_("T", "N", n, nrhs, n, &c_b10, &rwork[irwvt], n, &rwork[irwb], 
1002                 n, &c_b35, &rwork[irwib], n);
1003         jreal = irwrb - 1;
1004         jimag = irwib - 1;
1005         i__1 = *nrhs;
1006         for (jcol = 1; jcol <= i__1; ++jcol) {
1007             i__2 = *n;
1008             for (jrow = 1; jrow <= i__2; ++jrow) {
1009                 ++jreal;
1010                 ++jimag;
1011                 i__3 = jrow + jcol * b_dim1;
1012                 i__4 = jreal;
1013                 i__5 = jimag;
1014                 z__1.r = rwork[i__4], z__1.i = rwork[i__5];
1015                 b[i__3].r = z__1.r, b[i__3].i = z__1.i;
1016 /* L150: */
1017             }
1018 /* L160: */
1019         }
1020
1021 /*        Unscale. */
1022
1023         dlascl_("G", &c__0, &c__0, &c_b10, &orgnrm, n, &c__1, &d__[1], n, 
1024                 info);
1025         dlasrt_("D", n, &d__[1], info);
1026         zlascl_("G", &c__0, &c__0, &orgnrm, &c_b10, n, nrhs, &b[b_offset], 
1027                 ldb, info);
1028
1029         return 0;
1030     }
1031
1032 /*     Book-keeping and setting up some constants. */
1033
1034     nlvl = (integer) (log((doublereal) (*n) / (doublereal) (*smlsiz + 1)) / 
1035             log(2.)) + 1;
1036
1037     smlszp = *smlsiz + 1;
1038
1039     u = 1;
1040     vt = *smlsiz * *n + 1;
1041     difl = vt + smlszp * *n;
1042     difr = difl + nlvl * *n;
1043     z__ = difr + (nlvl * *n << 1);
1044     c__ = z__ + nlvl * *n;
1045     s = c__ + *n;
1046     poles = s + *n;
1047     givnum = poles + (nlvl << 1) * *n;
1048     nrwork = givnum + (nlvl << 1) * *n;
1049     bx = 1;
1050
1051     irwrb = nrwork;
1052     irwib = irwrb + *smlsiz * *nrhs;
1053     irwb = irwib + *smlsiz * *nrhs;
1054
1055     sizei = *n + 1;
1056     k = sizei + *n;
1057     givptr = k + *n;
1058     perm = givptr + *n;
1059     givcol = perm + nlvl * *n;
1060     iwk = givcol + (nlvl * *n << 1);
1061
1062     st = 1;
1063     sqre = 0;
1064     icmpq1 = 1;
1065     icmpq2 = 0;
1066     nsub = 0;
1067
1068     i__1 = *n;
1069     for (i__ = 1; i__ <= i__1; ++i__) {
1070         if ((d__1 = d__[i__], abs(d__1)) < eps) {
1071             d__[i__] = d_sign(&eps, &d__[i__]);
1072         }
1073 /* L170: */
1074     }
1075
1076     i__1 = nm1;
1077     for (i__ = 1; i__ <= i__1; ++i__) {
1078         if ((d__1 = e[i__], abs(d__1)) < eps || i__ == nm1) {
1079             ++nsub;
1080             iwork[nsub] = st;
1081
1082 /*           Subproblem found. First determine its size and then */
1083 /*           apply divide and conquer on it. */
1084
1085             if (i__ < nm1) {
1086
1087 /*              A subproblem with E(I) small for I < NM1. */
1088
1089                 nsize = i__ - st + 1;
1090                 iwork[sizei + nsub - 1] = nsize;
1091             } else if ((d__1 = e[i__], abs(d__1)) >= eps) {
1092
1093 /*              A subproblem with E(NM1) not too small but I = NM1. */
1094
1095                 nsize = *n - st + 1;
1096                 iwork[sizei + nsub - 1] = nsize;
1097             } else {
1098
1099 /*              A subproblem with E(NM1) small. This implies an */
1100 /*              1-by-1 subproblem at D(N), which is not solved */
1101 /*              explicitly. */
1102
1103                 nsize = i__ - st + 1;
1104                 iwork[sizei + nsub - 1] = nsize;
1105                 ++nsub;
1106                 iwork[nsub] = *n;
1107                 iwork[sizei + nsub - 1] = 1;
1108                 zcopy_(nrhs, &b[*n + b_dim1], ldb, &work[bx + nm1], n);
1109             }
1110             st1 = st - 1;
1111             if (nsize == 1) {
1112
1113 /*              This is a 1-by-1 subproblem and is not solved */
1114 /*              explicitly. */
1115
1116                 zcopy_(nrhs, &b[st + b_dim1], ldb, &work[bx + st1], n);
1117             } else if (nsize <= *smlsiz) {
1118
1119 /*              This is a small subproblem and is solved by DLASDQ. */
1120
1121                 dlaset_("A", &nsize, &nsize, &c_b35, &c_b10, &rwork[vt + st1],
1122                          n);
1123                 dlaset_("A", &nsize, &nsize, &c_b35, &c_b10, &rwork[u + st1], 
1124                         n);
1125                 dlasdq_("U", &c__0, &nsize, &nsize, &nsize, &c__0, &d__[st], &
1126                         e[st], &rwork[vt + st1], n, &rwork[u + st1], n, &
1127                         rwork[nrwork], &c__1, &rwork[nrwork], info)
1128                         ;
1129                 if (*info != 0) {
1130                     return 0;
1131                 }
1132
1133 /*              In the real version, B is passed to DLASDQ and multiplied */
1134 /*              internally by Q**H. Here B is complex and that product is */
1135 /*              computed below in two steps (real and imaginary parts). */
1136
1137                 j = irwb - 1;
1138                 i__2 = *nrhs;
1139                 for (jcol = 1; jcol <= i__2; ++jcol) {
1140                     i__3 = st + nsize - 1;
1141                     for (jrow = st; jrow <= i__3; ++jrow) {
1142                         ++j;
1143                         i__4 = jrow + jcol * b_dim1;
1144                         rwork[j] = b[i__4].r;
1145 /* L180: */
1146                     }
1147 /* L190: */
1148                 }
1149                 dgemm_("T", "N", &nsize, nrhs, &nsize, &c_b10, &rwork[u + st1]
1150                         , n, &rwork[irwb], &nsize, &c_b35, &rwork[irwrb], &
1151                         nsize);
1152                 j = irwb - 1;
1153                 i__2 = *nrhs;
1154                 for (jcol = 1; jcol <= i__2; ++jcol) {
1155                     i__3 = st + nsize - 1;
1156                     for (jrow = st; jrow <= i__3; ++jrow) {
1157                         ++j;
1158                         rwork[j] = d_imag(&b[jrow + jcol * b_dim1]);
1159 /* L200: */
1160                     }
1161 /* L210: */
1162                 }
1163                 dgemm_("T", "N", &nsize, nrhs, &nsize, &c_b10, &rwork[u + st1]
1164                         , n, &rwork[irwb], &nsize, &c_b35, &rwork[irwib], &
1165                         nsize);
1166                 jreal = irwrb - 1;
1167                 jimag = irwib - 1;
1168                 i__2 = *nrhs;
1169                 for (jcol = 1; jcol <= i__2; ++jcol) {
1170                     i__3 = st + nsize - 1;
1171                     for (jrow = st; jrow <= i__3; ++jrow) {
1172                         ++jreal;
1173                         ++jimag;
1174                         i__4 = jrow + jcol * b_dim1;
1175                         i__5 = jreal;
1176                         i__6 = jimag;
1177                         z__1.r = rwork[i__5], z__1.i = rwork[i__6];
1178                         b[i__4].r = z__1.r, b[i__4].i = z__1.i;
1179 /* L220: */
1180                     }
1181 /* L230: */
1182                 }
1183
1184                 zlacpy_("A", &nsize, nrhs, &b[st + b_dim1], ldb, &work[bx + 
1185                         st1], n);
1186             } else {
1187
1188 /*              A large problem. Solve it using divide and conquer. */
1189
1190                 dlasda_(&icmpq1, smlsiz, &nsize, &sqre, &d__[st], &e[st], &
1191                         rwork[u + st1], n, &rwork[vt + st1], &iwork[k + st1], 
1192                         &rwork[difl + st1], &rwork[difr + st1], &rwork[z__ + 
1193                         st1], &rwork[poles + st1], &iwork[givptr + st1], &
1194                         iwork[givcol + st1], n, &iwork[perm + st1], &rwork[
1195                         givnum + st1], &rwork[c__ + st1], &rwork[s + st1], &
1196                         rwork[nrwork], &iwork[iwk], info);
1197                 if (*info != 0) {
1198                     return 0;
1199                 }
1200                 bxst = bx + st1;
1201                 zlalsa_(&icmpq2, smlsiz, &nsize, nrhs, &b[st + b_dim1], ldb, &
1202                         work[bxst], n, &rwork[u + st1], n, &rwork[vt + st1], &
1203                         iwork[k + st1], &rwork[difl + st1], &rwork[difr + st1]
1204                         , &rwork[z__ + st1], &rwork[poles + st1], &iwork[
1205                         givptr + st1], &iwork[givcol + st1], n, &iwork[perm + 
1206                         st1], &rwork[givnum + st1], &rwork[c__ + st1], &rwork[
1207                         s + st1], &rwork[nrwork], &iwork[iwk], info);
1208                 if (*info != 0) {
1209                     return 0;
1210                 }
1211             }
1212             st = i__ + 1;
1213         }
1214 /* L240: */
1215     }
1216
1217 /*     Apply the singular values and treat the tiny ones as zero. */
1218
1219     tol = rcnd * (d__1 = d__[idamax_(n, &d__[1], &c__1)], abs(d__1));
1220
1221     i__1 = *n;
1222     for (i__ = 1; i__ <= i__1; ++i__) {
1223
1224 /*        Some of the elements in D can be negative because 1-by-1 */
1225 /*        subproblems were not solved explicitly. */
1226
1227         if ((d__1 = d__[i__], abs(d__1)) <= tol) {
1228             zlaset_("A", &c__1, nrhs, &c_b1, &c_b1, &work[bx + i__ - 1], n);
1229         } else {
1230             ++(*rank);
1231             zlascl_("G", &c__0, &c__0, &d__[i__], &c_b10, &c__1, nrhs, &work[
1232                     bx + i__ - 1], n, info);
1233         }
1234         d__[i__] = (d__1 = d__[i__], abs(d__1));
1235 /* L250: */
1236     }
1237
1238 /*     Now apply back the right singular vectors. */
1239
1240     icmpq2 = 1;
1241     i__1 = nsub;
1242     for (i__ = 1; i__ <= i__1; ++i__) {
1243         st = iwork[i__];
1244         st1 = st - 1;
1245         nsize = iwork[sizei + i__ - 1];
1246         bxst = bx + st1;
1247         if (nsize == 1) {
1248             zcopy_(nrhs, &work[bxst], n, &b[st + b_dim1], ldb);
1249         } else if (nsize <= *smlsiz) {
1250
1251 /*           Since B and BX are complex, the following call to DGEMM */
1252 /*           is performed in two steps (real and imaginary parts). */
1253
1254 /*           CALL DGEMM( 'T', 'N', NSIZE, NRHS, NSIZE, ONE, */
1255 /*    $                  RWORK( VT+ST1 ), N, RWORK( BXST ), N, ZERO, */
1256 /*    $                  B( ST, 1 ), LDB ) */
1257
1258             j = bxst - *n - 1;
1259             jreal = irwb - 1;
1260             i__2 = *nrhs;
1261             for (jcol = 1; jcol <= i__2; ++jcol) {
1262                 j += *n;
1263                 i__3 = nsize;
1264                 for (jrow = 1; jrow <= i__3; ++jrow) {
1265                     ++jreal;
1266                     i__4 = j + jrow;
1267                     rwork[jreal] = work[i__4].r;
1268 /* L260: */
1269                 }
1270 /* L270: */
1271             }
1272             dgemm_("T", "N", &nsize, nrhs, &nsize, &c_b10, &rwork[vt + st1], 
1273                     n, &rwork[irwb], &nsize, &c_b35, &rwork[irwrb], &nsize);
1274             j = bxst - *n - 1;
1275             jimag = irwb - 1;
1276             i__2 = *nrhs;
1277             for (jcol = 1; jcol <= i__2; ++jcol) {
1278                 j += *n;
1279                 i__3 = nsize;
1280                 for (jrow = 1; jrow <= i__3; ++jrow) {
1281                     ++jimag;
1282                     rwork[jimag] = d_imag(&work[j + jrow]);
1283 /* L280: */
1284                 }
1285 /* L290: */
1286             }
1287             dgemm_("T", "N", &nsize, nrhs, &nsize, &c_b10, &rwork[vt + st1], 
1288                     n, &rwork[irwb], &nsize, &c_b35, &rwork[irwib], &nsize);
1289             jreal = irwrb - 1;
1290             jimag = irwib - 1;
1291             i__2 = *nrhs;
1292             for (jcol = 1; jcol <= i__2; ++jcol) {
1293                 i__3 = st + nsize - 1;
1294                 for (jrow = st; jrow <= i__3; ++jrow) {
1295                     ++jreal;
1296                     ++jimag;
1297                     i__4 = jrow + jcol * b_dim1;
1298                     i__5 = jreal;
1299                     i__6 = jimag;
1300                     z__1.r = rwork[i__5], z__1.i = rwork[i__6];
1301                     b[i__4].r = z__1.r, b[i__4].i = z__1.i;
1302 /* L300: */
1303                 }
1304 /* L310: */
1305             }
1306         } else {
1307             zlalsa_(&icmpq2, smlsiz, &nsize, nrhs, &work[bxst], n, &b[st + 
1308                     b_dim1], ldb, &rwork[u + st1], n, &rwork[vt + st1], &
1309                     iwork[k + st1], &rwork[difl + st1], &rwork[difr + st1], &
1310                     rwork[z__ + st1], &rwork[poles + st1], &iwork[givptr + 
1311                     st1], &iwork[givcol + st1], n, &iwork[perm + st1], &rwork[
1312                     givnum + st1], &rwork[c__ + st1], &rwork[s + st1], &rwork[
1313                     nrwork], &iwork[iwk], info);
1314             if (*info != 0) {
1315                 return 0;
1316             }
1317         }
1318 /* L320: */
1319     }
1320
1321 /*     Unscale and sort the singular values. */
1322
1323     dlascl_("G", &c__0, &c__0, &c_b10, &orgnrm, n, &c__1, &d__[1], n, info);
1324     dlasrt_("D", n, &d__[1], info);
1325     zlascl_("G", &c__0, &c__0, &orgnrm, &c_b10, n, nrhs, &b[b_offset], ldb, 
1326             info);
1327
1328     return 0;
1329
1330 /*     End of ZLALSD */
1331
1332 } /* zlalsd_ */
1333