C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / cbdsqr.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 /*  -- translated by f2c (version 20000121).
512    You must link the resulting object file with the libraries:
513         -lf2c -lm   (in that order)
514 */
515
516
517
518 /* Table of constant values */
519
520 static doublereal c_b15 = -.125;
521 static integer c__1 = 1;
522 static real c_b49 = 1.f;
523 static real c_b72 = -1.f;
524
525 /* > \brief \b CBDSQR */
526
527 /*  =========== DOCUMENTATION =========== */
528
529 /* Online html documentation available at */
530 /*            http://www.netlib.org/lapack/explore-html/ */
531
532 /* > \htmlonly */
533 /* > Download CBDSQR + dependencies */
534 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/cbdsqr.
535 f"> */
536 /* > [TGZ]</a> */
537 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/cbdsqr.
538 f"> */
539 /* > [ZIP]</a> */
540 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/cbdsqr.
541 f"> */
542 /* > [TXT]</a> */
543 /* > \endhtmlonly */
544
545 /*  Definition: */
546 /*  =========== */
547
548 /*       SUBROUTINE CBDSQR( UPLO, N, NCVT, NRU, NCC, D, E, VT, LDVT, U, */
549 /*                          LDU, C, LDC, RWORK, INFO ) */
550
551 /*       CHARACTER          UPLO */
552 /*       INTEGER            INFO, LDC, LDU, LDVT, N, NCC, NCVT, NRU */
553 /*       REAL               D( * ), E( * ), RWORK( * ) */
554 /*       COMPLEX            C( LDC, * ), U( LDU, * ), VT( LDVT, * ) */
555
556
557 /* > \par Purpose: */
558 /*  ============= */
559 /* > */
560 /* > \verbatim */
561 /* > */
562 /* > CBDSQR computes the singular values and, optionally, the right and/or */
563 /* > left singular vectors from the singular value decomposition (SVD) of */
564 /* > a real N-by-N (upper or lower) bidiagonal matrix B using the implicit */
565 /* > zero-shift QR algorithm.  The SVD of B has the form */
566 /* > */
567 /* >    B = Q * S * P**H */
568 /* > */
569 /* > where S is the diagonal matrix of singular values, Q is an orthogonal */
570 /* > matrix of left singular vectors, and P is an orthogonal matrix of */
571 /* > right singular vectors.  If left singular vectors are requested, this */
572 /* > subroutine actually returns U*Q instead of Q, and, if right singular */
573 /* > vectors are requested, this subroutine returns P**H*VT instead of */
574 /* > P**H, for given complex input matrices U and VT.  When U and VT are */
575 /* > the unitary matrices that reduce a general matrix A to bidiagonal */
576 /* > form: A = U*B*VT, as computed by CGEBRD, then */
577 /* > */
578 /* >    A = (U*Q) * S * (P**H*VT) */
579 /* > */
580 /* > is the SVD of A.  Optionally, the subroutine may also compute Q**H*C */
581 /* > for a given complex input matrix C. */
582 /* > */
583 /* > See "Computing  Small Singular Values of Bidiagonal Matrices With */
584 /* > Guaranteed High Relative Accuracy," by J. Demmel and W. Kahan, */
585 /* > LAPACK Working Note #3 (or SIAM J. Sci. Statist. Comput. vol. 11, */
586 /* > no. 5, pp. 873-912, Sept 1990) and */
587 /* > "Accurate singular values and differential qd algorithms," by */
588 /* > B. Parlett and V. Fernando, Technical Report CPAM-554, Mathematics */
589 /* > Department, University of California at Berkeley, July 1992 */
590 /* > for a detailed description of the algorithm. */
591 /* > \endverbatim */
592
593 /*  Arguments: */
594 /*  ========== */
595
596 /* > \param[in] UPLO */
597 /* > \verbatim */
598 /* >          UPLO is CHARACTER*1 */
599 /* >          = 'U':  B is upper bidiagonal; */
600 /* >          = 'L':  B is lower bidiagonal. */
601 /* > \endverbatim */
602 /* > */
603 /* > \param[in] N */
604 /* > \verbatim */
605 /* >          N is INTEGER */
606 /* >          The order of the matrix B.  N >= 0. */
607 /* > \endverbatim */
608 /* > */
609 /* > \param[in] NCVT */
610 /* > \verbatim */
611 /* >          NCVT is INTEGER */
612 /* >          The number of columns of the matrix VT. NCVT >= 0. */
613 /* > \endverbatim */
614 /* > */
615 /* > \param[in] NRU */
616 /* > \verbatim */
617 /* >          NRU is INTEGER */
618 /* >          The number of rows of the matrix U. NRU >= 0. */
619 /* > \endverbatim */
620 /* > */
621 /* > \param[in] NCC */
622 /* > \verbatim */
623 /* >          NCC is INTEGER */
624 /* >          The number of columns of the matrix C. NCC >= 0. */
625 /* > \endverbatim */
626 /* > */
627 /* > \param[in,out] D */
628 /* > \verbatim */
629 /* >          D is REAL array, dimension (N) */
630 /* >          On entry, the n diagonal elements of the bidiagonal matrix B. */
631 /* >          On exit, if INFO=0, the singular values of B in decreasing */
632 /* >          order. */
633 /* > \endverbatim */
634 /* > */
635 /* > \param[in,out] E */
636 /* > \verbatim */
637 /* >          E is REAL array, dimension (N-1) */
638 /* >          On entry, the N-1 offdiagonal elements of the bidiagonal */
639 /* >          matrix B. */
640 /* >          On exit, if INFO = 0, E is destroyed; if INFO > 0, D and E */
641 /* >          will contain the diagonal and superdiagonal elements of a */
642 /* >          bidiagonal matrix orthogonally equivalent to the one given */
643 /* >          as input. */
644 /* > \endverbatim */
645 /* > */
646 /* > \param[in,out] VT */
647 /* > \verbatim */
648 /* >          VT is COMPLEX array, dimension (LDVT, NCVT) */
649 /* >          On entry, an N-by-NCVT matrix VT. */
650 /* >          On exit, VT is overwritten by P**H * VT. */
651 /* >          Not referenced if NCVT = 0. */
652 /* > \endverbatim */
653 /* > */
654 /* > \param[in] LDVT */
655 /* > \verbatim */
656 /* >          LDVT is INTEGER */
657 /* >          The leading dimension of the array VT. */
658 /* >          LDVT >= f2cmax(1,N) if NCVT > 0; LDVT >= 1 if NCVT = 0. */
659 /* > \endverbatim */
660 /* > */
661 /* > \param[in,out] U */
662 /* > \verbatim */
663 /* >          U is COMPLEX array, dimension (LDU, N) */
664 /* >          On entry, an NRU-by-N matrix U. */
665 /* >          On exit, U is overwritten by U * Q. */
666 /* >          Not referenced if NRU = 0. */
667 /* > \endverbatim */
668 /* > */
669 /* > \param[in] LDU */
670 /* > \verbatim */
671 /* >          LDU is INTEGER */
672 /* >          The leading dimension of the array U.  LDU >= f2cmax(1,NRU). */
673 /* > \endverbatim */
674 /* > */
675 /* > \param[in,out] C */
676 /* > \verbatim */
677 /* >          C is COMPLEX array, dimension (LDC, NCC) */
678 /* >          On entry, an N-by-NCC matrix C. */
679 /* >          On exit, C is overwritten by Q**H * C. */
680 /* >          Not referenced if NCC = 0. */
681 /* > \endverbatim */
682 /* > */
683 /* > \param[in] LDC */
684 /* > \verbatim */
685 /* >          LDC is INTEGER */
686 /* >          The leading dimension of the array C. */
687 /* >          LDC >= f2cmax(1,N) if NCC > 0; LDC >=1 if NCC = 0. */
688 /* > \endverbatim */
689 /* > */
690 /* > \param[out] RWORK */
691 /* > \verbatim */
692 /* >          RWORK is REAL array, dimension (4*N) */
693 /* > \endverbatim */
694 /* > */
695 /* > \param[out] INFO */
696 /* > \verbatim */
697 /* >          INFO is INTEGER */
698 /* >          = 0:  successful exit */
699 /* >          < 0:  If INFO = -i, the i-th argument had an illegal value */
700 /* >          > 0:  the algorithm did not converge; D and E contain the */
701 /* >                elements of a bidiagonal matrix which is orthogonally */
702 /* >                similar to the input matrix B;  if INFO = i, i */
703 /* >                elements of E have not converged to zero. */
704 /* > \endverbatim */
705
706 /* > \par Internal Parameters: */
707 /*  ========================= */
708 /* > */
709 /* > \verbatim */
710 /* >  TOLMUL  REAL, default = f2cmax(10,f2cmin(100,EPS**(-1/8))) */
711 /* >          TOLMUL controls the convergence criterion of the QR loop. */
712 /* >          If it is positive, TOLMUL*EPS is the desired relative */
713 /* >             precision in the computed singular values. */
714 /* >          If it is negative, abs(TOLMUL*EPS*sigma_max) is the */
715 /* >             desired absolute accuracy in the computed singular */
716 /* >             values (corresponds to relative accuracy */
717 /* >             abs(TOLMUL*EPS) in the largest singular value. */
718 /* >          abs(TOLMUL) should be between 1 and 1/EPS, and preferably */
719 /* >             between 10 (for fast convergence) and .1/EPS */
720 /* >             (for there to be some accuracy in the results). */
721 /* >          Default is to lose at either one eighth or 2 of the */
722 /* >             available decimal digits in each computed singular value */
723 /* >             (whichever is smaller). */
724 /* > */
725 /* >  MAXITR  INTEGER, default = 6 */
726 /* >          MAXITR controls the maximum number of passes of the */
727 /* >          algorithm through its inner loop. The algorithms stops */
728 /* >          (and so fails to converge) if the number of passes */
729 /* >          through the inner loop exceeds MAXITR*N**2. */
730 /* > \endverbatim */
731
732 /*  Authors: */
733 /*  ======== */
734
735 /* > \author Univ. of Tennessee */
736 /* > \author Univ. of California Berkeley */
737 /* > \author Univ. of Colorado Denver */
738 /* > \author NAG Ltd. */
739
740 /* > \date December 2016 */
741
742 /* > \ingroup complexOTHERcomputational */
743
744 /*  ===================================================================== */
745 /* Subroutine */ int cbdsqr_(char *uplo, integer *n, integer *ncvt, integer *
746         nru, integer *ncc, real *d__, real *e, complex *vt, integer *ldvt, 
747         complex *u, integer *ldu, complex *c__, integer *ldc, real *rwork, 
748         integer *info)
749 {
750     /* System generated locals */
751     integer c_dim1, c_offset, u_dim1, u_offset, vt_dim1, vt_offset, i__1, 
752             i__2;
753     real r__1, r__2, r__3, r__4;
754     doublereal d__1;
755
756     /* Local variables */
757     real abse;
758     integer idir;
759     real abss;
760     integer oldm;
761     real cosl;
762     integer isub, iter;
763     real unfl, sinl, cosr, smin, smax, sinr;
764     extern /* Subroutine */ int slas2_(real *, real *, real *, real *, real *)
765             ;
766     real f, g, h__;
767     integer i__, j, m;
768     real r__;
769     extern logical lsame_(char *, char *);
770     real oldcs;
771     extern /* Subroutine */ int clasr_(char *, char *, char *, integer *, 
772             integer *, real *, real *, complex *, integer *);
773     integer oldll;
774     real shift, sigmn, oldsn;
775     extern /* Subroutine */ int cswap_(integer *, complex *, integer *, 
776             complex *, integer *);
777     integer maxit;
778     real sminl, sigmx;
779     logical lower;
780     extern /* Subroutine */ int csrot_(integer *, complex *, integer *, 
781             complex *, integer *, real *, real *), slasq1_(integer *, real *, 
782             real *, real *, integer *), slasv2_(real *, real *, real *, real *
783             , real *, real *, real *, real *, real *);
784     real cs;
785     integer ll;
786     real sn, mu;
787     extern real slamch_(char *);
788     extern /* Subroutine */ int csscal_(integer *, real *, complex *, integer 
789             *), xerbla_(char *, integer *, ftnlen);
790     real sminoa;
791     extern /* Subroutine */ int slartg_(real *, real *, real *, real *, real *
792             );
793     real thresh;
794     logical rotate;
795     integer nm1;
796     real tolmul;
797     integer nm12, nm13, lll;
798     real eps, sll, tol;
799
800
801 /*  -- LAPACK computational routine (version 3.7.0) -- */
802 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
803 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
804 /*     December 2016 */
805
806
807 /*  ===================================================================== */
808
809
810 /*     Test the input parameters. */
811
812     /* Parameter adjustments */
813     --d__;
814     --e;
815     vt_dim1 = *ldvt;
816     vt_offset = 1 + vt_dim1 * 1;
817     vt -= vt_offset;
818     u_dim1 = *ldu;
819     u_offset = 1 + u_dim1 * 1;
820     u -= u_offset;
821     c_dim1 = *ldc;
822     c_offset = 1 + c_dim1 * 1;
823     c__ -= c_offset;
824     --rwork;
825
826     /* Function Body */
827     *info = 0;
828     lower = lsame_(uplo, "L");
829     if (! lsame_(uplo, "U") && ! lower) {
830         *info = -1;
831     } else if (*n < 0) {
832         *info = -2;
833     } else if (*ncvt < 0) {
834         *info = -3;
835     } else if (*nru < 0) {
836         *info = -4;
837     } else if (*ncc < 0) {
838         *info = -5;
839     } else if (*ncvt == 0 && *ldvt < 1 || *ncvt > 0 && *ldvt < f2cmax(1,*n)) {
840         *info = -9;
841     } else if (*ldu < f2cmax(1,*nru)) {
842         *info = -11;
843     } else if (*ncc == 0 && *ldc < 1 || *ncc > 0 && *ldc < f2cmax(1,*n)) {
844         *info = -13;
845     }
846     if (*info != 0) {
847         i__1 = -(*info);
848         xerbla_("CBDSQR", &i__1, (ftnlen)6);
849         return 0;
850     }
851     if (*n == 0) {
852         return 0;
853     }
854     if (*n == 1) {
855         goto L160;
856     }
857
858 /*     ROTATE is true if any singular vectors desired, false otherwise */
859
860     rotate = *ncvt > 0 || *nru > 0 || *ncc > 0;
861
862 /*     If no singular vectors desired, use qd algorithm */
863
864     if (! rotate) {
865         slasq1_(n, &d__[1], &e[1], &rwork[1], info);
866
867 /*     If INFO equals 2, dqds didn't finish, try to finish */
868
869         if (*info != 2) {
870             return 0;
871         }
872         *info = 0;
873     }
874
875     nm1 = *n - 1;
876     nm12 = nm1 + nm1;
877     nm13 = nm12 + nm1;
878     idir = 0;
879
880 /*     Get machine constants */
881
882     eps = slamch_("Epsilon");
883     unfl = slamch_("Safe minimum");
884
885 /*     If matrix lower bidiagonal, rotate to be upper bidiagonal */
886 /*     by applying Givens rotations on the left */
887
888     if (lower) {
889         i__1 = *n - 1;
890         for (i__ = 1; i__ <= i__1; ++i__) {
891             slartg_(&d__[i__], &e[i__], &cs, &sn, &r__);
892             d__[i__] = r__;
893             e[i__] = sn * d__[i__ + 1];
894             d__[i__ + 1] = cs * d__[i__ + 1];
895             rwork[i__] = cs;
896             rwork[nm1 + i__] = sn;
897 /* L10: */
898         }
899
900 /*        Update singular vectors if desired */
901
902         if (*nru > 0) {
903             clasr_("R", "V", "F", nru, n, &rwork[1], &rwork[*n], &u[u_offset],
904                      ldu);
905         }
906         if (*ncc > 0) {
907             clasr_("L", "V", "F", n, ncc, &rwork[1], &rwork[*n], &c__[
908                     c_offset], ldc);
909         }
910     }
911
912 /*     Compute singular values to relative accuracy TOL */
913 /*     (By setting TOL to be negative, algorithm will compute */
914 /*     singular values to absolute accuracy ABS(TOL)*norm(input matrix)) */
915
916 /* Computing MAX */
917 /* Computing MIN */
918     d__1 = (doublereal) eps;
919     r__3 = 100.f, r__4 = pow_dd(&d__1, &c_b15);
920     r__1 = 10.f, r__2 = f2cmin(r__3,r__4);
921     tolmul = f2cmax(r__1,r__2);
922     tol = tolmul * eps;
923
924 /*     Compute approximate maximum, minimum singular values */
925
926     smax = 0.f;
927     i__1 = *n;
928     for (i__ = 1; i__ <= i__1; ++i__) {
929 /* Computing MAX */
930         r__2 = smax, r__3 = (r__1 = d__[i__], abs(r__1));
931         smax = f2cmax(r__2,r__3);
932 /* L20: */
933     }
934     i__1 = *n - 1;
935     for (i__ = 1; i__ <= i__1; ++i__) {
936 /* Computing MAX */
937         r__2 = smax, r__3 = (r__1 = e[i__], abs(r__1));
938         smax = f2cmax(r__2,r__3);
939 /* L30: */
940     }
941     sminl = 0.f;
942     if (tol >= 0.f) {
943
944 /*        Relative accuracy desired */
945
946         sminoa = abs(d__[1]);
947         if (sminoa == 0.f) {
948             goto L50;
949         }
950         mu = sminoa;
951         i__1 = *n;
952         for (i__ = 2; i__ <= i__1; ++i__) {
953             mu = (r__2 = d__[i__], abs(r__2)) * (mu / (mu + (r__1 = e[i__ - 1]
954                     , abs(r__1))));
955             sminoa = f2cmin(sminoa,mu);
956             if (sminoa == 0.f) {
957                 goto L50;
958             }
959 /* L40: */
960         }
961 L50:
962         sminoa /= sqrt((real) (*n));
963 /* Computing MAX */
964         r__1 = tol * sminoa, r__2 = *n * 6 * *n * unfl;
965         thresh = f2cmax(r__1,r__2);
966     } else {
967
968 /*        Absolute accuracy desired */
969
970 /* Computing MAX */
971         r__1 = abs(tol) * smax, r__2 = *n * 6 * *n * unfl;
972         thresh = f2cmax(r__1,r__2);
973     }
974
975 /*     Prepare for main iteration loop for the singular values */
976 /*     (MAXIT is the maximum number of passes through the inner */
977 /*     loop permitted before nonconvergence signalled.) */
978
979     maxit = *n * 6 * *n;
980     iter = 0;
981     oldll = -1;
982     oldm = -1;
983
984 /*     M points to last element of unconverged part of matrix */
985
986     m = *n;
987
988 /*     Begin main iteration loop */
989
990 L60:
991
992 /*     Check for convergence or exceeding iteration count */
993
994     if (m <= 1) {
995         goto L160;
996     }
997     if (iter > maxit) {
998         goto L200;
999     }
1000
1001 /*     Find diagonal block of matrix to work on */
1002
1003     if (tol < 0.f && (r__1 = d__[m], abs(r__1)) <= thresh) {
1004         d__[m] = 0.f;
1005     }
1006     smax = (r__1 = d__[m], abs(r__1));
1007     smin = smax;
1008     i__1 = m - 1;
1009     for (lll = 1; lll <= i__1; ++lll) {
1010         ll = m - lll;
1011         abss = (r__1 = d__[ll], abs(r__1));
1012         abse = (r__1 = e[ll], abs(r__1));
1013         if (tol < 0.f && abss <= thresh) {
1014             d__[ll] = 0.f;
1015         }
1016         if (abse <= thresh) {
1017             goto L80;
1018         }
1019         smin = f2cmin(smin,abss);
1020 /* Computing MAX */
1021         r__1 = f2cmax(smax,abss);
1022         smax = f2cmax(r__1,abse);
1023 /* L70: */
1024     }
1025     ll = 0;
1026     goto L90;
1027 L80:
1028     e[ll] = 0.f;
1029
1030 /*     Matrix splits since E(LL) = 0 */
1031
1032     if (ll == m - 1) {
1033
1034 /*        Convergence of bottom singular value, return to top of loop */
1035
1036         --m;
1037         goto L60;
1038     }
1039 L90:
1040     ++ll;
1041
1042 /*     E(LL) through E(M-1) are nonzero, E(LL-1) is zero */
1043
1044     if (ll == m - 1) {
1045
1046 /*        2 by 2 block, handle separately */
1047
1048         slasv2_(&d__[m - 1], &e[m - 1], &d__[m], &sigmn, &sigmx, &sinr, &cosr,
1049                  &sinl, &cosl);
1050         d__[m - 1] = sigmx;
1051         e[m - 1] = 0.f;
1052         d__[m] = sigmn;
1053
1054 /*        Compute singular vectors, if desired */
1055
1056         if (*ncvt > 0) {
1057             csrot_(ncvt, &vt[m - 1 + vt_dim1], ldvt, &vt[m + vt_dim1], ldvt, &
1058                     cosr, &sinr);
1059         }
1060         if (*nru > 0) {
1061             csrot_(nru, &u[(m - 1) * u_dim1 + 1], &c__1, &u[m * u_dim1 + 1], &
1062                     c__1, &cosl, &sinl);
1063         }
1064         if (*ncc > 0) {
1065             csrot_(ncc, &c__[m - 1 + c_dim1], ldc, &c__[m + c_dim1], ldc, &
1066                     cosl, &sinl);
1067         }
1068         m += -2;
1069         goto L60;
1070     }
1071
1072 /*     If working on new submatrix, choose shift direction */
1073 /*     (from larger end diagonal element towards smaller) */
1074
1075     if (ll > oldm || m < oldll) {
1076         if ((r__1 = d__[ll], abs(r__1)) >= (r__2 = d__[m], abs(r__2))) {
1077
1078 /*           Chase bulge from top (big end) to bottom (small end) */
1079
1080             idir = 1;
1081         } else {
1082
1083 /*           Chase bulge from bottom (big end) to top (small end) */
1084
1085             idir = 2;
1086         }
1087     }
1088
1089 /*     Apply convergence tests */
1090
1091     if (idir == 1) {
1092
1093 /*        Run convergence test in forward direction */
1094 /*        First apply standard test to bottom of matrix */
1095
1096         if ((r__2 = e[m - 1], abs(r__2)) <= abs(tol) * (r__1 = d__[m], abs(
1097                 r__1)) || tol < 0.f && (r__3 = e[m - 1], abs(r__3)) <= thresh)
1098                  {
1099             e[m - 1] = 0.f;
1100             goto L60;
1101         }
1102
1103         if (tol >= 0.f) {
1104
1105 /*           If relative accuracy desired, */
1106 /*           apply convergence criterion forward */
1107
1108             mu = (r__1 = d__[ll], abs(r__1));
1109             sminl = mu;
1110             i__1 = m - 1;
1111             for (lll = ll; lll <= i__1; ++lll) {
1112                 if ((r__1 = e[lll], abs(r__1)) <= tol * mu) {
1113                     e[lll] = 0.f;
1114                     goto L60;
1115                 }
1116                 mu = (r__2 = d__[lll + 1], abs(r__2)) * (mu / (mu + (r__1 = e[
1117                         lll], abs(r__1))));
1118                 sminl = f2cmin(sminl,mu);
1119 /* L100: */
1120             }
1121         }
1122
1123     } else {
1124
1125 /*        Run convergence test in backward direction */
1126 /*        First apply standard test to top of matrix */
1127
1128         if ((r__2 = e[ll], abs(r__2)) <= abs(tol) * (r__1 = d__[ll], abs(r__1)
1129                 ) || tol < 0.f && (r__3 = e[ll], abs(r__3)) <= thresh) {
1130             e[ll] = 0.f;
1131             goto L60;
1132         }
1133
1134         if (tol >= 0.f) {
1135
1136 /*           If relative accuracy desired, */
1137 /*           apply convergence criterion backward */
1138
1139             mu = (r__1 = d__[m], abs(r__1));
1140             sminl = mu;
1141             i__1 = ll;
1142             for (lll = m - 1; lll >= i__1; --lll) {
1143                 if ((r__1 = e[lll], abs(r__1)) <= tol * mu) {
1144                     e[lll] = 0.f;
1145                     goto L60;
1146                 }
1147                 mu = (r__2 = d__[lll], abs(r__2)) * (mu / (mu + (r__1 = e[lll]
1148                         , abs(r__1))));
1149                 sminl = f2cmin(sminl,mu);
1150 /* L110: */
1151             }
1152         }
1153     }
1154     oldll = ll;
1155     oldm = m;
1156
1157 /*     Compute shift.  First, test if shifting would ruin relative */
1158 /*     accuracy, and if so set the shift to zero. */
1159
1160 /* Computing MAX */
1161     r__1 = eps, r__2 = tol * .01f;
1162     if (tol >= 0.f && *n * tol * (sminl / smax) <= f2cmax(r__1,r__2)) {
1163
1164 /*        Use a zero shift to avoid loss of relative accuracy */
1165
1166         shift = 0.f;
1167     } else {
1168
1169 /*        Compute the shift from 2-by-2 block at end of matrix */
1170
1171         if (idir == 1) {
1172             sll = (r__1 = d__[ll], abs(r__1));
1173             slas2_(&d__[m - 1], &e[m - 1], &d__[m], &shift, &r__);
1174         } else {
1175             sll = (r__1 = d__[m], abs(r__1));
1176             slas2_(&d__[ll], &e[ll], &d__[ll + 1], &shift, &r__);
1177         }
1178
1179 /*        Test if shift negligible, and if so set to zero */
1180
1181         if (sll > 0.f) {
1182 /* Computing 2nd power */
1183             r__1 = shift / sll;
1184             if (r__1 * r__1 < eps) {
1185                 shift = 0.f;
1186             }
1187         }
1188     }
1189
1190 /*     Increment iteration count */
1191
1192     iter = iter + m - ll;
1193
1194 /*     If SHIFT = 0, do simplified QR iteration */
1195
1196     if (shift == 0.f) {
1197         if (idir == 1) {
1198
1199 /*           Chase bulge from top to bottom */
1200 /*           Save cosines and sines for later singular vector updates */
1201
1202             cs = 1.f;
1203             oldcs = 1.f;
1204             i__1 = m - 1;
1205             for (i__ = ll; i__ <= i__1; ++i__) {
1206                 r__1 = d__[i__] * cs;
1207                 slartg_(&r__1, &e[i__], &cs, &sn, &r__);
1208                 if (i__ > ll) {
1209                     e[i__ - 1] = oldsn * r__;
1210                 }
1211                 r__1 = oldcs * r__;
1212                 r__2 = d__[i__ + 1] * sn;
1213                 slartg_(&r__1, &r__2, &oldcs, &oldsn, &d__[i__]);
1214                 rwork[i__ - ll + 1] = cs;
1215                 rwork[i__ - ll + 1 + nm1] = sn;
1216                 rwork[i__ - ll + 1 + nm12] = oldcs;
1217                 rwork[i__ - ll + 1 + nm13] = oldsn;
1218 /* L120: */
1219             }
1220             h__ = d__[m] * cs;
1221             d__[m] = h__ * oldcs;
1222             e[m - 1] = h__ * oldsn;
1223
1224 /*           Update singular vectors */
1225
1226             if (*ncvt > 0) {
1227                 i__1 = m - ll + 1;
1228                 clasr_("L", "V", "F", &i__1, ncvt, &rwork[1], &rwork[*n], &vt[
1229                         ll + vt_dim1], ldvt);
1230             }
1231             if (*nru > 0) {
1232                 i__1 = m - ll + 1;
1233                 clasr_("R", "V", "F", nru, &i__1, &rwork[nm12 + 1], &rwork[
1234                         nm13 + 1], &u[ll * u_dim1 + 1], ldu);
1235             }
1236             if (*ncc > 0) {
1237                 i__1 = m - ll + 1;
1238                 clasr_("L", "V", "F", &i__1, ncc, &rwork[nm12 + 1], &rwork[
1239                         nm13 + 1], &c__[ll + c_dim1], ldc);
1240             }
1241
1242 /*           Test convergence */
1243
1244             if ((r__1 = e[m - 1], abs(r__1)) <= thresh) {
1245                 e[m - 1] = 0.f;
1246             }
1247
1248         } else {
1249
1250 /*           Chase bulge from bottom to top */
1251 /*           Save cosines and sines for later singular vector updates */
1252
1253             cs = 1.f;
1254             oldcs = 1.f;
1255             i__1 = ll + 1;
1256             for (i__ = m; i__ >= i__1; --i__) {
1257                 r__1 = d__[i__] * cs;
1258                 slartg_(&r__1, &e[i__ - 1], &cs, &sn, &r__);
1259                 if (i__ < m) {
1260                     e[i__] = oldsn * r__;
1261                 }
1262                 r__1 = oldcs * r__;
1263                 r__2 = d__[i__ - 1] * sn;
1264                 slartg_(&r__1, &r__2, &oldcs, &oldsn, &d__[i__]);
1265                 rwork[i__ - ll] = cs;
1266                 rwork[i__ - ll + nm1] = -sn;
1267                 rwork[i__ - ll + nm12] = oldcs;
1268                 rwork[i__ - ll + nm13] = -oldsn;
1269 /* L130: */
1270             }
1271             h__ = d__[ll] * cs;
1272             d__[ll] = h__ * oldcs;
1273             e[ll] = h__ * oldsn;
1274
1275 /*           Update singular vectors */
1276
1277             if (*ncvt > 0) {
1278                 i__1 = m - ll + 1;
1279                 clasr_("L", "V", "B", &i__1, ncvt, &rwork[nm12 + 1], &rwork[
1280                         nm13 + 1], &vt[ll + vt_dim1], ldvt);
1281             }
1282             if (*nru > 0) {
1283                 i__1 = m - ll + 1;
1284                 clasr_("R", "V", "B", nru, &i__1, &rwork[1], &rwork[*n], &u[
1285                         ll * u_dim1 + 1], ldu);
1286             }
1287             if (*ncc > 0) {
1288                 i__1 = m - ll + 1;
1289                 clasr_("L", "V", "B", &i__1, ncc, &rwork[1], &rwork[*n], &c__[
1290                         ll + c_dim1], ldc);
1291             }
1292
1293 /*           Test convergence */
1294
1295             if ((r__1 = e[ll], abs(r__1)) <= thresh) {
1296                 e[ll] = 0.f;
1297             }
1298         }
1299     } else {
1300
1301 /*        Use nonzero shift */
1302
1303         if (idir == 1) {
1304
1305 /*           Chase bulge from top to bottom */
1306 /*           Save cosines and sines for later singular vector updates */
1307
1308             f = ((r__1 = d__[ll], abs(r__1)) - shift) * (r_sign(&c_b49, &d__[
1309                     ll]) + shift / d__[ll]);
1310             g = e[ll];
1311             i__1 = m - 1;
1312             for (i__ = ll; i__ <= i__1; ++i__) {
1313                 slartg_(&f, &g, &cosr, &sinr, &r__);
1314                 if (i__ > ll) {
1315                     e[i__ - 1] = r__;
1316                 }
1317                 f = cosr * d__[i__] + sinr * e[i__];
1318                 e[i__] = cosr * e[i__] - sinr * d__[i__];
1319                 g = sinr * d__[i__ + 1];
1320                 d__[i__ + 1] = cosr * d__[i__ + 1];
1321                 slartg_(&f, &g, &cosl, &sinl, &r__);
1322                 d__[i__] = r__;
1323                 f = cosl * e[i__] + sinl * d__[i__ + 1];
1324                 d__[i__ + 1] = cosl * d__[i__ + 1] - sinl * e[i__];
1325                 if (i__ < m - 1) {
1326                     g = sinl * e[i__ + 1];
1327                     e[i__ + 1] = cosl * e[i__ + 1];
1328                 }
1329                 rwork[i__ - ll + 1] = cosr;
1330                 rwork[i__ - ll + 1 + nm1] = sinr;
1331                 rwork[i__ - ll + 1 + nm12] = cosl;
1332                 rwork[i__ - ll + 1 + nm13] = sinl;
1333 /* L140: */
1334             }
1335             e[m - 1] = f;
1336
1337 /*           Update singular vectors */
1338
1339             if (*ncvt > 0) {
1340                 i__1 = m - ll + 1;
1341                 clasr_("L", "V", "F", &i__1, ncvt, &rwork[1], &rwork[*n], &vt[
1342                         ll + vt_dim1], ldvt);
1343             }
1344             if (*nru > 0) {
1345                 i__1 = m - ll + 1;
1346                 clasr_("R", "V", "F", nru, &i__1, &rwork[nm12 + 1], &rwork[
1347                         nm13 + 1], &u[ll * u_dim1 + 1], ldu);
1348             }
1349             if (*ncc > 0) {
1350                 i__1 = m - ll + 1;
1351                 clasr_("L", "V", "F", &i__1, ncc, &rwork[nm12 + 1], &rwork[
1352                         nm13 + 1], &c__[ll + c_dim1], ldc);
1353             }
1354
1355 /*           Test convergence */
1356
1357             if ((r__1 = e[m - 1], abs(r__1)) <= thresh) {
1358                 e[m - 1] = 0.f;
1359             }
1360
1361         } else {
1362
1363 /*           Chase bulge from bottom to top */
1364 /*           Save cosines and sines for later singular vector updates */
1365
1366             f = ((r__1 = d__[m], abs(r__1)) - shift) * (r_sign(&c_b49, &d__[m]
1367                     ) + shift / d__[m]);
1368             g = e[m - 1];
1369             i__1 = ll + 1;
1370             for (i__ = m; i__ >= i__1; --i__) {
1371                 slartg_(&f, &g, &cosr, &sinr, &r__);
1372                 if (i__ < m) {
1373                     e[i__] = r__;
1374                 }
1375                 f = cosr * d__[i__] + sinr * e[i__ - 1];
1376                 e[i__ - 1] = cosr * e[i__ - 1] - sinr * d__[i__];
1377                 g = sinr * d__[i__ - 1];
1378                 d__[i__ - 1] = cosr * d__[i__ - 1];
1379                 slartg_(&f, &g, &cosl, &sinl, &r__);
1380                 d__[i__] = r__;
1381                 f = cosl * e[i__ - 1] + sinl * d__[i__ - 1];
1382                 d__[i__ - 1] = cosl * d__[i__ - 1] - sinl * e[i__ - 1];
1383                 if (i__ > ll + 1) {
1384                     g = sinl * e[i__ - 2];
1385                     e[i__ - 2] = cosl * e[i__ - 2];
1386                 }
1387                 rwork[i__ - ll] = cosr;
1388                 rwork[i__ - ll + nm1] = -sinr;
1389                 rwork[i__ - ll + nm12] = cosl;
1390                 rwork[i__ - ll + nm13] = -sinl;
1391 /* L150: */
1392             }
1393             e[ll] = f;
1394
1395 /*           Test convergence */
1396
1397             if ((r__1 = e[ll], abs(r__1)) <= thresh) {
1398                 e[ll] = 0.f;
1399             }
1400
1401 /*           Update singular vectors if desired */
1402
1403             if (*ncvt > 0) {
1404                 i__1 = m - ll + 1;
1405                 clasr_("L", "V", "B", &i__1, ncvt, &rwork[nm12 + 1], &rwork[
1406                         nm13 + 1], &vt[ll + vt_dim1], ldvt);
1407             }
1408             if (*nru > 0) {
1409                 i__1 = m - ll + 1;
1410                 clasr_("R", "V", "B", nru, &i__1, &rwork[1], &rwork[*n], &u[
1411                         ll * u_dim1 + 1], ldu);
1412             }
1413             if (*ncc > 0) {
1414                 i__1 = m - ll + 1;
1415                 clasr_("L", "V", "B", &i__1, ncc, &rwork[1], &rwork[*n], &c__[
1416                         ll + c_dim1], ldc);
1417             }
1418         }
1419     }
1420
1421 /*     QR iteration finished, go back and check convergence */
1422
1423     goto L60;
1424
1425 /*     All singular values converged, so make them positive */
1426
1427 L160:
1428     i__1 = *n;
1429     for (i__ = 1; i__ <= i__1; ++i__) {
1430         if (d__[i__] < 0.f) {
1431             d__[i__] = -d__[i__];
1432
1433 /*           Change sign of singular vectors, if desired */
1434
1435             if (*ncvt > 0) {
1436                 csscal_(ncvt, &c_b72, &vt[i__ + vt_dim1], ldvt);
1437             }
1438         }
1439 /* L170: */
1440     }
1441
1442 /*     Sort the singular values into decreasing order (insertion sort on */
1443 /*     singular values, but only one transposition per singular vector) */
1444
1445     i__1 = *n - 1;
1446     for (i__ = 1; i__ <= i__1; ++i__) {
1447
1448 /*        Scan for smallest D(I) */
1449
1450         isub = 1;
1451         smin = d__[1];
1452         i__2 = *n + 1 - i__;
1453         for (j = 2; j <= i__2; ++j) {
1454             if (d__[j] <= smin) {
1455                 isub = j;
1456                 smin = d__[j];
1457             }
1458 /* L180: */
1459         }
1460         if (isub != *n + 1 - i__) {
1461
1462 /*           Swap singular values and vectors */
1463
1464             d__[isub] = d__[*n + 1 - i__];
1465             d__[*n + 1 - i__] = smin;
1466             if (*ncvt > 0) {
1467                 cswap_(ncvt, &vt[isub + vt_dim1], ldvt, &vt[*n + 1 - i__ + 
1468                         vt_dim1], ldvt);
1469             }
1470             if (*nru > 0) {
1471                 cswap_(nru, &u[isub * u_dim1 + 1], &c__1, &u[(*n + 1 - i__) * 
1472                         u_dim1 + 1], &c__1);
1473             }
1474             if (*ncc > 0) {
1475                 cswap_(ncc, &c__[isub + c_dim1], ldc, &c__[*n + 1 - i__ + 
1476                         c_dim1], ldc);
1477             }
1478         }
1479 /* L190: */
1480     }
1481     goto L220;
1482
1483 /*     Maximum number of iterations exceeded, failure to converge */
1484
1485 L200:
1486     *info = 0;
1487     i__1 = *n - 1;
1488     for (i__ = 1; i__ <= i__1; ++i__) {
1489         if (e[i__] != 0.f) {
1490             ++(*info);
1491         }
1492 /* L210: */
1493     }
1494 L220:
1495     return 0;
1496
1497 /*     End of CBDSQR */
1498
1499 } /* cbdsqr_ */
1500