C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / zbbcsd.c
1 #include <math.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <complex.h>
6 #ifdef complex
7 #undef complex
8 #endif
9 #ifdef I
10 #undef I
11 #endif
12
13 #if defined(_WIN64)
14 typedef long long BLASLONG;
15 typedef unsigned long long BLASULONG;
16 #else
17 typedef long BLASLONG;
18 typedef unsigned long BLASULONG;
19 #endif
20
21 #ifdef LAPACK_ILP64
22 typedef BLASLONG blasint;
23 #if defined(_WIN64)
24 #define blasabs(x) llabs(x)
25 #else
26 #define blasabs(x) labs(x)
27 #endif
28 #else
29 typedef int blasint;
30 #define blasabs(x) abs(x)
31 #endif
32
33 typedef blasint integer;
34
35 typedef unsigned int uinteger;
36 typedef char *address;
37 typedef short int shortint;
38 typedef float real;
39 typedef double doublereal;
40 typedef struct { real r, i; } complex;
41 typedef struct { doublereal r, i; } doublecomplex;
42 #ifdef _MSC_VER
43 static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
44 static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
45 static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
46 static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
47 #else
48 static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
49 static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
50 static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
51 static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
52 #endif
53 #define pCf(z) (*_pCf(z))
54 #define pCd(z) (*_pCd(z))
55 typedef int logical;
56 typedef short int shortlogical;
57 typedef char logical1;
58 typedef char integer1;
59
60 #define TRUE_ (1)
61 #define FALSE_ (0)
62
63 /* Extern is for use with -E */
64 #ifndef Extern
65 #define Extern extern
66 #endif
67
68 /* I/O stuff */
69
70 typedef int flag;
71 typedef int ftnlen;
72 typedef int ftnint;
73
74 /*external read, write*/
75 typedef struct
76 {       flag cierr;
77         ftnint ciunit;
78         flag ciend;
79         char *cifmt;
80         ftnint cirec;
81 } cilist;
82
83 /*internal read, write*/
84 typedef struct
85 {       flag icierr;
86         char *iciunit;
87         flag iciend;
88         char *icifmt;
89         ftnint icirlen;
90         ftnint icirnum;
91 } icilist;
92
93 /*open*/
94 typedef struct
95 {       flag oerr;
96         ftnint ounit;
97         char *ofnm;
98         ftnlen ofnmlen;
99         char *osta;
100         char *oacc;
101         char *ofm;
102         ftnint orl;
103         char *oblnk;
104 } olist;
105
106 /*close*/
107 typedef struct
108 {       flag cerr;
109         ftnint cunit;
110         char *csta;
111 } cllist;
112
113 /*rewind, backspace, endfile*/
114 typedef struct
115 {       flag aerr;
116         ftnint aunit;
117 } alist;
118
119 /* inquire */
120 typedef struct
121 {       flag inerr;
122         ftnint inunit;
123         char *infile;
124         ftnlen infilen;
125         ftnint  *inex;  /*parameters in standard's order*/
126         ftnint  *inopen;
127         ftnint  *innum;
128         ftnint  *innamed;
129         char    *inname;
130         ftnlen  innamlen;
131         char    *inacc;
132         ftnlen  inacclen;
133         char    *inseq;
134         ftnlen  inseqlen;
135         char    *indir;
136         ftnlen  indirlen;
137         char    *infmt;
138         ftnlen  infmtlen;
139         char    *inform;
140         ftnint  informlen;
141         char    *inunf;
142         ftnlen  inunflen;
143         ftnint  *inrecl;
144         ftnint  *innrec;
145         char    *inblank;
146         ftnlen  inblanklen;
147 } inlist;
148
149 #define VOID void
150
151 union Multitype {       /* for multiple entry points */
152         integer1 g;
153         shortint h;
154         integer i;
155         /* longint j; */
156         real r;
157         doublereal d;
158         complex c;
159         doublecomplex z;
160         };
161
162 typedef union Multitype Multitype;
163
164 struct Vardesc {        /* for Namelist */
165         char *name;
166         char *addr;
167         ftnlen *dims;
168         int  type;
169         };
170 typedef struct Vardesc Vardesc;
171
172 struct Namelist {
173         char *name;
174         Vardesc **vars;
175         int nvars;
176         };
177 typedef struct Namelist Namelist;
178
179 #define abs(x) ((x) >= 0 ? (x) : -(x))
180 #define dabs(x) (fabs(x))
181 #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
182 #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
183 #define dmin(a,b) (f2cmin(a,b))
184 #define dmax(a,b) (f2cmax(a,b))
185 #define bit_test(a,b)   ((a) >> (b) & 1)
186 #define bit_clear(a,b)  ((a) & ~((uinteger)1 << (b)))
187 #define bit_set(a,b)    ((a) |  ((uinteger)1 << (b)))
188
189 #define abort_() { sig_die("Fortran abort routine called", 1); }
190 #define c_abs(z) (cabsf(Cf(z)))
191 #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
192 #ifdef _MSC_VER
193 #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
194 #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/df(b)._Val[1]);}
195 #else
196 #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
197 #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
198 #endif
199 #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
200 #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
201 #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
202 //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
203 #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
204 #define d_abs(x) (fabs(*(x)))
205 #define d_acos(x) (acos(*(x)))
206 #define d_asin(x) (asin(*(x)))
207 #define d_atan(x) (atan(*(x)))
208 #define d_atn2(x, y) (atan2(*(x),*(y)))
209 #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
210 #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
211 #define d_cos(x) (cos(*(x)))
212 #define d_cosh(x) (cosh(*(x)))
213 #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
214 #define d_exp(x) (exp(*(x)))
215 #define d_imag(z) (cimag(Cd(z)))
216 #define r_imag(z) (cimagf(Cf(z)))
217 #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
218 #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
219 #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
220 #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
221 #define d_log(x) (log(*(x)))
222 #define d_mod(x, y) (fmod(*(x), *(y)))
223 #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
224 #define d_nint(x) u_nint(*(x))
225 #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
226 #define d_sign(a,b) u_sign(*(a),*(b))
227 #define r_sign(a,b) u_sign(*(a),*(b))
228 #define d_sin(x) (sin(*(x)))
229 #define d_sinh(x) (sinh(*(x)))
230 #define d_sqrt(x) (sqrt(*(x)))
231 #define d_tan(x) (tan(*(x)))
232 #define d_tanh(x) (tanh(*(x)))
233 #define i_abs(x) abs(*(x))
234 #define i_dnnt(x) ((integer)u_nint(*(x)))
235 #define i_len(s, n) (n)
236 #define i_nint(x) ((integer)u_nint(*(x)))
237 #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
238 #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
239 #define pow_si(B,E) spow_ui(*(B),*(E))
240 #define pow_ri(B,E) spow_ui(*(B),*(E))
241 #define pow_di(B,E) dpow_ui(*(B),*(E))
242 #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
243 #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
244 #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
245 #define s_cat(lpp, rpp, rnp, np, llp) {         ftnlen i, nc, ll; char *f__rp, *lp;     ll = (llp); lp = (lpp);         for(i=0; i < (int)*(np); ++i) {                 nc = ll;                if((rnp)[i] < nc) nc = (rnp)[i];                ll -= nc;               f__rp = (rpp)[i];               while(--nc >= 0) *lp++ = *(f__rp)++;         }  while(--ll >= 0) *lp++ = ' '; }
246 #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
247 #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
248 #define sig_die(s, kill) { exit(1); }
249 #define s_stop(s, n) {exit(0);}
250 static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
251 #define z_abs(z) (cabs(Cd(z)))
252 #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
253 #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
254 #define myexit_() break;
255 #define mycycle() continue;
256 #define myceiling(w) {ceil(w)}
257 #define myhuge(w) {HUGE_VAL}
258 //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
259 #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
260
261 /* procedure parameter types for -A and -C++ */
262
263 #define F2C_proc_par_types 1
264 #ifdef __cplusplus
265 typedef logical (*L_fp)(...);
266 #else
267 typedef logical (*L_fp)();
268 #endif
269
270 static float spow_ui(float x, integer n) {
271         float pow=1.0; unsigned long int u;
272         if(n != 0) {
273                 if(n < 0) n = -n, x = 1/x;
274                 for(u = n; ; ) {
275                         if(u & 01) pow *= x;
276                         if(u >>= 1) x *= x;
277                         else break;
278                 }
279         }
280         return pow;
281 }
282 static double dpow_ui(double x, integer n) {
283         double pow=1.0; unsigned long int u;
284         if(n != 0) {
285                 if(n < 0) n = -n, x = 1/x;
286                 for(u = n; ; ) {
287                         if(u & 01) pow *= x;
288                         if(u >>= 1) x *= x;
289                         else break;
290                 }
291         }
292         return pow;
293 }
294 #ifdef _MSC_VER
295 static _Fcomplex cpow_ui(complex x, integer n) {
296         complex pow={1.0,0.0}; unsigned long int u;
297                 if(n != 0) {
298                 if(n < 0) n = -n, x.r = 1/x.r, x.i=1/x.i;
299                 for(u = n; ; ) {
300                         if(u & 01) pow.r *= x.r, pow.i *= x.i;
301                         if(u >>= 1) x.r *= x.r, x.i *= x.i;
302                         else break;
303                 }
304         }
305         _Fcomplex p={pow.r, pow.i};
306         return p;
307 }
308 #else
309 static _Complex float cpow_ui(_Complex float x, integer n) {
310         _Complex float pow=1.0; unsigned long int u;
311         if(n != 0) {
312                 if(n < 0) n = -n, x = 1/x;
313                 for(u = n; ; ) {
314                         if(u & 01) pow *= x;
315                         if(u >>= 1) x *= x;
316                         else break;
317                 }
318         }
319         return pow;
320 }
321 #endif
322 #ifdef _MSC_VER
323 static _Dcomplex zpow_ui(_Dcomplex x, integer n) {
324         _Dcomplex pow={1.0,0.0}; unsigned long int u;
325         if(n != 0) {
326                 if(n < 0) n = -n, x._Val[0] = 1/x._Val[0], x._Val[1] =1/x._Val[1];
327                 for(u = n; ; ) {
328                         if(u & 01) pow._Val[0] *= x._Val[0], pow._Val[1] *= x._Val[1];
329                         if(u >>= 1) x._Val[0] *= x._Val[0], x._Val[1] *= x._Val[1];
330                         else break;
331                 }
332         }
333         _Dcomplex p = {pow._Val[0], pow._Val[1]};
334         return p;
335 }
336 #else
337 static _Complex double zpow_ui(_Complex double x, integer n) {
338         _Complex double pow=1.0; unsigned long int u;
339         if(n != 0) {
340                 if(n < 0) n = -n, x = 1/x;
341                 for(u = n; ; ) {
342                         if(u & 01) pow *= x;
343                         if(u >>= 1) x *= x;
344                         else break;
345                 }
346         }
347         return pow;
348 }
349 #endif
350 static integer pow_ii(integer x, integer n) {
351         integer pow; unsigned long int u;
352         if (n <= 0) {
353                 if (n == 0 || x == 1) pow = 1;
354                 else if (x != -1) pow = x == 0 ? 1/x : 0;
355                 else n = -n;
356         }
357         if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
358                 u = n;
359                 for(pow = 1; ; ) {
360                         if(u & 01) pow *= x;
361                         if(u >>= 1) x *= x;
362                         else break;
363                 }
364         }
365         return pow;
366 }
367 static integer dmaxloc_(double *w, integer s, integer e, integer *n)
368 {
369         double m; integer i, mi;
370         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
371                 if (w[i-1]>m) mi=i ,m=w[i-1];
372         return mi-s+1;
373 }
374 static integer smaxloc_(float *w, integer s, integer e, integer *n)
375 {
376         float m; integer i, mi;
377         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
378                 if (w[i-1]>m) mi=i ,m=w[i-1];
379         return mi-s+1;
380 }
381 static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
382         integer n = *n_, incx = *incx_, incy = *incy_, i;
383 #ifdef _MSC_VER
384         _Fcomplex zdotc = {0.0, 0.0};
385         if (incx == 1 && incy == 1) {
386                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
387                         zdotc._Val[0] += conjf(Cf(&x[i]))._Val[0] * Cf(&y[i])._Val[0];
388                         zdotc._Val[1] += conjf(Cf(&x[i]))._Val[1] * Cf(&y[i])._Val[1];
389                 }
390         } else {
391                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
392                         zdotc._Val[0] += conjf(Cf(&x[i*incx]))._Val[0] * Cf(&y[i*incy])._Val[0];
393                         zdotc._Val[1] += conjf(Cf(&x[i*incx]))._Val[1] * Cf(&y[i*incy])._Val[1];
394                 }
395         }
396         pCf(z) = zdotc;
397 }
398 #else
399         _Complex float zdotc = 0.0;
400         if (incx == 1 && incy == 1) {
401                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
402                         zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
403                 }
404         } else {
405                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
406                         zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
407                 }
408         }
409         pCf(z) = zdotc;
410 }
411 #endif
412 static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
413         integer n = *n_, incx = *incx_, incy = *incy_, i;
414 #ifdef _MSC_VER
415         _Dcomplex zdotc = {0.0, 0.0};
416         if (incx == 1 && incy == 1) {
417                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
418                         zdotc._Val[0] += conj(Cd(&x[i]))._Val[0] * Cd(&y[i])._Val[0];
419                         zdotc._Val[1] += conj(Cd(&x[i]))._Val[1] * Cd(&y[i])._Val[1];
420                 }
421         } else {
422                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
423                         zdotc._Val[0] += conj(Cd(&x[i*incx]))._Val[0] * Cd(&y[i*incy])._Val[0];
424                         zdotc._Val[1] += conj(Cd(&x[i*incx]))._Val[1] * Cd(&y[i*incy])._Val[1];
425                 }
426         }
427         pCd(z) = zdotc;
428 }
429 #else
430         _Complex double zdotc = 0.0;
431         if (incx == 1 && incy == 1) {
432                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
433                         zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
434                 }
435         } else {
436                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
437                         zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
438                 }
439         }
440         pCd(z) = zdotc;
441 }
442 #endif  
443 static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
444         integer n = *n_, incx = *incx_, incy = *incy_, i;
445 #ifdef _MSC_VER
446         _Fcomplex zdotc = {0.0, 0.0};
447         if (incx == 1 && incy == 1) {
448                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
449                         zdotc._Val[0] += Cf(&x[i])._Val[0] * Cf(&y[i])._Val[0];
450                         zdotc._Val[1] += Cf(&x[i])._Val[1] * Cf(&y[i])._Val[1];
451                 }
452         } else {
453                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
454                         zdotc._Val[0] += Cf(&x[i*incx])._Val[0] * Cf(&y[i*incy])._Val[0];
455                         zdotc._Val[1] += Cf(&x[i*incx])._Val[1] * Cf(&y[i*incy])._Val[1];
456                 }
457         }
458         pCf(z) = zdotc;
459 }
460 #else
461         _Complex float zdotc = 0.0;
462         if (incx == 1 && incy == 1) {
463                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
464                         zdotc += Cf(&x[i]) * Cf(&y[i]);
465                 }
466         } else {
467                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
468                         zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
469                 }
470         }
471         pCf(z) = zdotc;
472 }
473 #endif
474 static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
475         integer n = *n_, incx = *incx_, incy = *incy_, i;
476 #ifdef _MSC_VER
477         _Dcomplex zdotc = {0.0, 0.0};
478         if (incx == 1 && incy == 1) {
479                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
480                         zdotc._Val[0] += Cd(&x[i])._Val[0] * Cd(&y[i])._Val[0];
481                         zdotc._Val[1] += Cd(&x[i])._Val[1] * Cd(&y[i])._Val[1];
482                 }
483         } else {
484                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
485                         zdotc._Val[0] += Cd(&x[i*incx])._Val[0] * Cd(&y[i*incy])._Val[0];
486                         zdotc._Val[1] += Cd(&x[i*incx])._Val[1] * Cd(&y[i*incy])._Val[1];
487                 }
488         }
489         pCd(z) = zdotc;
490 }
491 #else
492         _Complex double zdotc = 0.0;
493         if (incx == 1 && incy == 1) {
494                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
495                         zdotc += Cd(&x[i]) * Cd(&y[i]);
496                 }
497         } else {
498                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
499                         zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
500                 }
501         }
502         pCd(z) = zdotc;
503 }
504 #endif
505 /*  -- translated by f2c (version 20000121).
506    You must link the resulting object file with the libraries:
507         -lf2c -lm   (in that order)
508 */
509
510
511
512
513 /* Table of constant values */
514
515 static doublecomplex c_b1 = {-1.,0.};
516 static doublereal c_b11 = -.125;
517 static integer c__1 = 1;
518
519 /* > \brief \b ZBBCSD */
520
521 /*  =========== DOCUMENTATION =========== */
522
523 /* Online html documentation available at */
524 /*            http://www.netlib.org/lapack/explore-html/ */
525
526 /* > \htmlonly */
527 /* > Download ZBBCSD + dependencies */
528 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/zbbcsd.
529 f"> */
530 /* > [TGZ]</a> */
531 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/zbbcsd.
532 f"> */
533 /* > [ZIP]</a> */
534 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/zbbcsd.
535 f"> */
536 /* > [TXT]</a> */
537 /* > \endhtmlonly */
538
539 /*  Definition: */
540 /*  =========== */
541
542 /*       SUBROUTINE ZBBCSD( JOBU1, JOBU2, JOBV1T, JOBV2T, TRANS, M, P, Q, */
543 /*                          THETA, PHI, U1, LDU1, U2, LDU2, V1T, LDV1T, */
544 /*                          V2T, LDV2T, B11D, B11E, B12D, B12E, B21D, B21E, */
545 /*                          B22D, B22E, RWORK, LRWORK, INFO ) */
546
547 /*       CHARACTER          JOBU1, JOBU2, JOBV1T, JOBV2T, TRANS */
548 /*       INTEGER            INFO, LDU1, LDU2, LDV1T, LDV2T, LRWORK, M, P, Q */
549 /*       DOUBLE PRECISION   B11D( * ), B11E( * ), B12D( * ), B12E( * ), */
550 /*      $                   B21D( * ), B21E( * ), B22D( * ), B22E( * ), */
551 /*      $                   PHI( * ), THETA( * ), RWORK( * ) */
552 /*       COMPLEX*16         U1( LDU1, * ), U2( LDU2, * ), V1T( LDV1T, * ), */
553 /*      $                   V2T( LDV2T, * ) */
554
555
556 /* > \par Purpose: */
557 /*  ============= */
558 /* > */
559 /* > \verbatim */
560 /* > */
561 /* > ZBBCSD computes the CS decomposition of a unitary matrix in */
562 /* > bidiagonal-block form, */
563 /* > */
564 /* > */
565 /* >     [ B11 | B12 0  0 ] */
566 /* >     [  0  |  0 -I  0 ] */
567 /* > X = [----------------] */
568 /* >     [ B21 | B22 0  0 ] */
569 /* >     [  0  |  0  0  I ] */
570 /* > */
571 /* >                               [  C | -S  0  0 ] */
572 /* >                   [ U1 |    ] [  0 |  0 -I  0 ] [ V1 |    ]**H */
573 /* >                 = [---------] [---------------] [---------]   . */
574 /* >                   [    | U2 ] [  S |  C  0  0 ] [    | V2 ] */
575 /* >                               [  0 |  0  0  I ] */
576 /* > */
577 /* > X is M-by-M, its top-left block is P-by-Q, and Q must be no larger */
578 /* > than P, M-P, or M-Q. (If Q is not the smallest index, then X must be */
579 /* > transposed and/or permuted. This can be done in constant time using */
580 /* > the TRANS and SIGNS options. See ZUNCSD for details.) */
581 /* > */
582 /* > The bidiagonal matrices B11, B12, B21, and B22 are represented */
583 /* > implicitly by angles THETA(1:Q) and PHI(1:Q-1). */
584 /* > */
585 /* > The unitary matrices U1, U2, V1T, and V2T are input/output. */
586 /* > The input matrices are pre- or post-multiplied by the appropriate */
587 /* > singular vector matrices. */
588 /* > \endverbatim */
589
590 /*  Arguments: */
591 /*  ========== */
592
593 /* > \param[in] JOBU1 */
594 /* > \verbatim */
595 /* >          JOBU1 is CHARACTER */
596 /* >          = 'Y':      U1 is updated; */
597 /* >          otherwise:  U1 is not updated. */
598 /* > \endverbatim */
599 /* > */
600 /* > \param[in] JOBU2 */
601 /* > \verbatim */
602 /* >          JOBU2 is CHARACTER */
603 /* >          = 'Y':      U2 is updated; */
604 /* >          otherwise:  U2 is not updated. */
605 /* > \endverbatim */
606 /* > */
607 /* > \param[in] JOBV1T */
608 /* > \verbatim */
609 /* >          JOBV1T is CHARACTER */
610 /* >          = 'Y':      V1T is updated; */
611 /* >          otherwise:  V1T is not updated. */
612 /* > \endverbatim */
613 /* > */
614 /* > \param[in] JOBV2T */
615 /* > \verbatim */
616 /* >          JOBV2T is CHARACTER */
617 /* >          = 'Y':      V2T is updated; */
618 /* >          otherwise:  V2T is not updated. */
619 /* > \endverbatim */
620 /* > */
621 /* > \param[in] TRANS */
622 /* > \verbatim */
623 /* >          TRANS is CHARACTER */
624 /* >          = 'T':      X, U1, U2, V1T, and V2T are stored in row-major */
625 /* >                      order; */
626 /* >          otherwise:  X, U1, U2, V1T, and V2T are stored in column- */
627 /* >                      major order. */
628 /* > \endverbatim */
629 /* > */
630 /* > \param[in] M */
631 /* > \verbatim */
632 /* >          M is INTEGER */
633 /* >          The number of rows and columns in X, the unitary matrix in */
634 /* >          bidiagonal-block form. */
635 /* > \endverbatim */
636 /* > */
637 /* > \param[in] P */
638 /* > \verbatim */
639 /* >          P is INTEGER */
640 /* >          The number of rows in the top-left block of X. 0 <= P <= M. */
641 /* > \endverbatim */
642 /* > */
643 /* > \param[in] Q */
644 /* > \verbatim */
645 /* >          Q is INTEGER */
646 /* >          The number of columns in the top-left block of X. */
647 /* >          0 <= Q <= MIN(P,M-P,M-Q). */
648 /* > \endverbatim */
649 /* > */
650 /* > \param[in,out] THETA */
651 /* > \verbatim */
652 /* >          THETA is DOUBLE PRECISION array, dimension (Q) */
653 /* >          On entry, the angles THETA(1),...,THETA(Q) that, along with */
654 /* >          PHI(1), ...,PHI(Q-1), define the matrix in bidiagonal-block */
655 /* >          form. On exit, the angles whose cosines and sines define the */
656 /* >          diagonal blocks in the CS decomposition. */
657 /* > \endverbatim */
658 /* > */
659 /* > \param[in,out] PHI */
660 /* > \verbatim */
661 /* >          PHI is DOUBLE PRECISION array, dimension (Q-1) */
662 /* >          The angles PHI(1),...,PHI(Q-1) that, along with THETA(1),..., */
663 /* >          THETA(Q), define the matrix in bidiagonal-block form. */
664 /* > \endverbatim */
665 /* > */
666 /* > \param[in,out] U1 */
667 /* > \verbatim */
668 /* >          U1 is COMPLEX*16 array, dimension (LDU1,P) */
669 /* >          On entry, a P-by-P matrix. On exit, U1 is postmultiplied */
670 /* >          by the left singular vector matrix common to [ B11 ; 0 ] and */
671 /* >          [ B12 0 0 ; 0 -I 0 0 ]. */
672 /* > \endverbatim */
673 /* > */
674 /* > \param[in] LDU1 */
675 /* > \verbatim */
676 /* >          LDU1 is INTEGER */
677 /* >          The leading dimension of the array U1, LDU1 >= MAX(1,P). */
678 /* > \endverbatim */
679 /* > */
680 /* > \param[in,out] U2 */
681 /* > \verbatim */
682 /* >          U2 is COMPLEX*16 array, dimension (LDU2,M-P) */
683 /* >          On entry, an (M-P)-by-(M-P) matrix. On exit, U2 is */
684 /* >          postmultiplied by the left singular vector matrix common to */
685 /* >          [ B21 ; 0 ] and [ B22 0 0 ; 0 0 I ]. */
686 /* > \endverbatim */
687 /* > */
688 /* > \param[in] LDU2 */
689 /* > \verbatim */
690 /* >          LDU2 is INTEGER */
691 /* >          The leading dimension of the array U2, LDU2 >= MAX(1,M-P). */
692 /* > \endverbatim */
693 /* > */
694 /* > \param[in,out] V1T */
695 /* > \verbatim */
696 /* >          V1T is COMPLEX*16 array, dimension (LDV1T,Q) */
697 /* >          On entry, a Q-by-Q matrix. On exit, V1T is premultiplied */
698 /* >          by the conjugate transpose of the right singular vector */
699 /* >          matrix common to [ B11 ; 0 ] and [ B21 ; 0 ]. */
700 /* > \endverbatim */
701 /* > */
702 /* > \param[in] LDV1T */
703 /* > \verbatim */
704 /* >          LDV1T is INTEGER */
705 /* >          The leading dimension of the array V1T, LDV1T >= MAX(1,Q). */
706 /* > \endverbatim */
707 /* > */
708 /* > \param[in,out] V2T */
709 /* > \verbatim */
710 /* >          V2T is COMPLEX*16 array, dimension (LDV2T,M-Q) */
711 /* >          On entry, an (M-Q)-by-(M-Q) matrix. On exit, V2T is */
712 /* >          premultiplied by the conjugate transpose of the right */
713 /* >          singular vector matrix common to [ B12 0 0 ; 0 -I 0 ] and */
714 /* >          [ B22 0 0 ; 0 0 I ]. */
715 /* > \endverbatim */
716 /* > */
717 /* > \param[in] LDV2T */
718 /* > \verbatim */
719 /* >          LDV2T is INTEGER */
720 /* >          The leading dimension of the array V2T, LDV2T >= MAX(1,M-Q). */
721 /* > \endverbatim */
722 /* > */
723 /* > \param[out] B11D */
724 /* > \verbatim */
725 /* >          B11D is DOUBLE PRECISION array, dimension (Q) */
726 /* >          When ZBBCSD converges, B11D contains the cosines of THETA(1), */
727 /* >          ..., THETA(Q). If ZBBCSD fails to converge, then B11D */
728 /* >          contains the diagonal of the partially reduced top-left */
729 /* >          block. */
730 /* > \endverbatim */
731 /* > */
732 /* > \param[out] B11E */
733 /* > \verbatim */
734 /* >          B11E is DOUBLE PRECISION array, dimension (Q-1) */
735 /* >          When ZBBCSD converges, B11E contains zeros. If ZBBCSD fails */
736 /* >          to converge, then B11E contains the superdiagonal of the */
737 /* >          partially reduced top-left block. */
738 /* > \endverbatim */
739 /* > */
740 /* > \param[out] B12D */
741 /* > \verbatim */
742 /* >          B12D is DOUBLE PRECISION array, dimension (Q) */
743 /* >          When ZBBCSD converges, B12D contains the negative sines of */
744 /* >          THETA(1), ..., THETA(Q). If ZBBCSD fails to converge, then */
745 /* >          B12D contains the diagonal of the partially reduced top-right */
746 /* >          block. */
747 /* > \endverbatim */
748 /* > */
749 /* > \param[out] B12E */
750 /* > \verbatim */
751 /* >          B12E is DOUBLE PRECISION array, dimension (Q-1) */
752 /* >          When ZBBCSD converges, B12E contains zeros. If ZBBCSD fails */
753 /* >          to converge, then B12E contains the subdiagonal of the */
754 /* >          partially reduced top-right block. */
755 /* > \endverbatim */
756 /* > */
757 /* > \param[out] B21D */
758 /* > \verbatim */
759 /* >          B21D is DOUBLE PRECISION array, dimension (Q) */
760 /* >          When ZBBCSD converges, B21D contains the negative sines of */
761 /* >          THETA(1), ..., THETA(Q). If ZBBCSD fails to converge, then */
762 /* >          B21D contains the diagonal of the partially reduced bottom-left */
763 /* >          block. */
764 /* > \endverbatim */
765 /* > */
766 /* > \param[out] B21E */
767 /* > \verbatim */
768 /* >          B21E is DOUBLE PRECISION array, dimension (Q-1) */
769 /* >          When ZBBCSD converges, B21E contains zeros. If ZBBCSD fails */
770 /* >          to converge, then B21E contains the subdiagonal of the */
771 /* >          partially reduced bottom-left block. */
772 /* > \endverbatim */
773 /* > */
774 /* > \param[out] B22D */
775 /* > \verbatim */
776 /* >          B22D is DOUBLE PRECISION array, dimension (Q) */
777 /* >          When ZBBCSD converges, B22D contains the negative sines of */
778 /* >          THETA(1), ..., THETA(Q). If ZBBCSD fails to converge, then */
779 /* >          B22D contains the diagonal of the partially reduced bottom-right */
780 /* >          block. */
781 /* > \endverbatim */
782 /* > */
783 /* > \param[out] B22E */
784 /* > \verbatim */
785 /* >          B22E is DOUBLE PRECISION array, dimension (Q-1) */
786 /* >          When ZBBCSD converges, B22E contains zeros. If ZBBCSD fails */
787 /* >          to converge, then B22E contains the subdiagonal of the */
788 /* >          partially reduced bottom-right block. */
789 /* > \endverbatim */
790 /* > */
791 /* > \param[out] RWORK */
792 /* > \verbatim */
793 /* >          RWORK is DOUBLE PRECISION array, dimension (MAX(1,LRWORK)) */
794 /* >          On exit, if INFO = 0, RWORK(1) returns the optimal LRWORK. */
795 /* > \endverbatim */
796 /* > */
797 /* > \param[in] LRWORK */
798 /* > \verbatim */
799 /* >          LRWORK is INTEGER */
800 /* >          The dimension of the array RWORK. LRWORK >= MAX(1,8*Q). */
801 /* > */
802 /* >          If LRWORK = -1, then a workspace query is assumed; the */
803 /* >          routine only calculates the optimal size of the RWORK array, */
804 /* >          returns this value as the first entry of the work array, and */
805 /* >          no error message related to LRWORK is issued by XERBLA. */
806 /* > \endverbatim */
807 /* > */
808 /* > \param[out] INFO */
809 /* > \verbatim */
810 /* >          INFO is INTEGER */
811 /* >          = 0:  successful exit. */
812 /* >          < 0:  if INFO = -i, the i-th argument had an illegal value. */
813 /* >          > 0:  if ZBBCSD did not converge, INFO specifies the number */
814 /* >                of nonzero entries in PHI, and B11D, B11E, etc., */
815 /* >                contain the partially reduced matrix. */
816 /* > \endverbatim */
817
818 /* > \par Internal Parameters: */
819 /*  ========================= */
820 /* > */
821 /* > \verbatim */
822 /* >  TOLMUL  DOUBLE PRECISION, default = MAX(10,MIN(100,EPS**(-1/8))) */
823 /* >          TOLMUL controls the convergence criterion of the QR loop. */
824 /* >          Angles THETA(i), PHI(i) are rounded to 0 or PI/2 when they */
825 /* >          are within TOLMUL*EPS of either bound. */
826 /* > \endverbatim */
827
828 /* > \par References: */
829 /*  ================ */
830 /* > */
831 /* >  [1] Brian D. Sutton. Computing the complete CS decomposition. Numer. */
832 /* >      Algorithms, 50(1):33-65, 2009. */
833
834 /*  Authors: */
835 /*  ======== */
836
837 /* > \author Univ. of Tennessee */
838 /* > \author Univ. of California Berkeley */
839 /* > \author Univ. of Colorado Denver */
840 /* > \author NAG Ltd. */
841
842 /* > \date June 2016 */
843
844 /* > \ingroup complex16OTHERcomputational */
845
846 /*  ===================================================================== */
847 /* Subroutine */ int zbbcsd_(char *jobu1, char *jobu2, char *jobv1t, char *
848         jobv2t, char *trans, integer *m, integer *p, integer *q, doublereal *
849         theta, doublereal *phi, doublecomplex *u1, integer *ldu1, 
850         doublecomplex *u2, integer *ldu2, doublecomplex *v1t, integer *ldv1t, 
851         doublecomplex *v2t, integer *ldv2t, doublereal *b11d, doublereal *
852         b11e, doublereal *b12d, doublereal *b12e, doublereal *b21d, 
853         doublereal *b21e, doublereal *b22d, doublereal *b22e, doublereal *
854         rwork, integer *lrwork, integer *info)
855 {
856     /* System generated locals */
857     integer u1_dim1, u1_offset, u2_dim1, u2_offset, v1t_dim1, v1t_offset, 
858             v2t_dim1, v2t_offset, i__1, i__2;
859     doublereal d__1, d__2, d__3, d__4;
860
861     /* Local variables */
862     integer imin, mini, imax, iter;
863     doublereal unfl, temp;
864     logical colmajor;
865     doublereal thetamin, thetamax;
866     logical restart11, restart12, restart21, restart22;
867     extern /* Subroutine */ int dlas2_(doublereal *, doublereal *, doublereal 
868             *, doublereal *, doublereal *);
869     integer iu1cs, iu2cs, iu1sn, iu2sn, i__, j;
870     doublereal r__;
871     extern logical lsame_(char *, char *);
872     extern /* Subroutine */ int zscal_(integer *, doublecomplex *, 
873             doublecomplex *, integer *);
874     integer maxit;
875     doublereal dummy;
876     extern /* Subroutine */ int zlasr_(char *, char *, char *, integer *, 
877             integer *, doublereal *, doublereal *, doublecomplex *, integer *), zswap_(integer *, doublecomplex *, 
878             integer *, doublecomplex *, integer *);
879     doublereal x1, x2, y1, y2;
880     integer lrworkmin, iv1tcs, iv2tcs;
881     logical wantu1, wantu2;
882     integer lrworkopt, iv1tsn, iv2tsn;
883     extern doublereal dlamch_(char *);
884     doublereal mu, nu, sigma11, sigma21;
885     extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
886     doublereal thresh, tolmul;
887     extern /* Subroutine */ int mecago_();
888     logical lquery;
889     doublereal b11bulge;
890     logical wantv1t, wantv2t;
891     doublereal b12bulge, b21bulge, b22bulge, eps, tol;
892     extern /* Subroutine */ int dlartgp_(doublereal *, doublereal *, 
893             doublereal *, doublereal *, doublereal *), dlartgs_(doublereal *, 
894             doublereal *, doublereal *, doublereal *, doublereal *);
895
896
897 /*  -- LAPACK computational routine (version 3.7.1) -- */
898 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
899 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
900 /*     June 2016 */
901
902
903 /*  =================================================================== */
904
905
906
907 /*     Test input arguments */
908
909     /* Parameter adjustments */
910     --theta;
911     --phi;
912     u1_dim1 = *ldu1;
913     u1_offset = 1 + u1_dim1 * 1;
914     u1 -= u1_offset;
915     u2_dim1 = *ldu2;
916     u2_offset = 1 + u2_dim1 * 1;
917     u2 -= u2_offset;
918     v1t_dim1 = *ldv1t;
919     v1t_offset = 1 + v1t_dim1 * 1;
920     v1t -= v1t_offset;
921     v2t_dim1 = *ldv2t;
922     v2t_offset = 1 + v2t_dim1 * 1;
923     v2t -= v2t_offset;
924     --b11d;
925     --b11e;
926     --b12d;
927     --b12e;
928     --b21d;
929     --b21e;
930     --b22d;
931     --b22e;
932     --rwork;
933
934     /* Function Body */
935     *info = 0;
936     lquery = *lrwork == -1;
937     wantu1 = lsame_(jobu1, "Y");
938     wantu2 = lsame_(jobu2, "Y");
939     wantv1t = lsame_(jobv1t, "Y");
940     wantv2t = lsame_(jobv2t, "Y");
941     colmajor = ! lsame_(trans, "T");
942
943     if (*m < 0) {
944         *info = -6;
945     } else if (*p < 0 || *p > *m) {
946         *info = -7;
947     } else if (*q < 0 || *q > *m) {
948         *info = -8;
949     } else if (*q > *p || *q > *m - *p || *q > *m - *q) {
950         *info = -8;
951     } else if (wantu1 && *ldu1 < *p) {
952         *info = -12;
953     } else if (wantu2 && *ldu2 < *m - *p) {
954         *info = -14;
955     } else if (wantv1t && *ldv1t < *q) {
956         *info = -16;
957     } else if (wantv2t && *ldv2t < *m - *q) {
958         *info = -18;
959     }
960
961 /*     Quick return if Q = 0 */
962
963     if (*info == 0 && *q == 0) {
964         lrworkmin = 1;
965         rwork[1] = (doublereal) lrworkmin;
966         return 0;
967     }
968
969 /*     Compute workspace */
970
971     if (*info == 0) {
972         iu1cs = 1;
973         iu1sn = iu1cs + *q;
974         iu2cs = iu1sn + *q;
975         iu2sn = iu2cs + *q;
976         iv1tcs = iu2sn + *q;
977         iv1tsn = iv1tcs + *q;
978         iv2tcs = iv1tsn + *q;
979         iv2tsn = iv2tcs + *q;
980         lrworkopt = iv2tsn + *q - 1;
981         lrworkmin = lrworkopt;
982         rwork[1] = (doublereal) lrworkopt;
983         if (*lrwork < lrworkmin && ! lquery) {
984             *info = -28;
985         }
986     }
987
988     if (*info != 0) {
989         i__1 = -(*info);
990         xerbla_("ZBBCSD", &i__1, (ftnlen)6);
991         return 0;
992     } else if (lquery) {
993         return 0;
994     }
995
996 /*     Get machine constants */
997
998     eps = dlamch_("Epsilon");
999     unfl = dlamch_("Safe minimum");
1000 /* Computing MAX */
1001 /* Computing MIN */
1002     d__3 = 100., d__4 = pow_dd(&eps, &c_b11);
1003     d__1 = 10., d__2 = f2cmin(d__3,d__4);
1004     tolmul = f2cmax(d__1,d__2);
1005     tol = tolmul * eps;
1006 /* Computing MAX */
1007     d__1 = tol, d__2 = *q * 6 * *q * unfl;
1008     thresh = f2cmax(d__1,d__2);
1009
1010 /*     Test for negligible sines or cosines */
1011
1012     i__1 = *q;
1013     for (i__ = 1; i__ <= i__1; ++i__) {
1014         if (theta[i__] < thresh) {
1015             theta[i__] = 0.;
1016         } else if (theta[i__] > 1.57079632679489662 - thresh) {
1017             theta[i__] = 1.57079632679489662;
1018         }
1019     }
1020     i__1 = *q - 1;
1021     for (i__ = 1; i__ <= i__1; ++i__) {
1022         if (phi[i__] < thresh) {
1023             phi[i__] = 0.;
1024         } else if (phi[i__] > 1.57079632679489662 - thresh) {
1025             phi[i__] = 1.57079632679489662;
1026         }
1027     }
1028
1029 /*     Initial deflation */
1030
1031     imax = *q;
1032     while(imax > 1) {
1033         if (phi[imax - 1] != 0.) {
1034             myexit_();
1035         }
1036         --imax;
1037     }
1038     imin = imax - 1;
1039     if (imin > 1) {
1040         while(phi[imin - 1] != 0.) {
1041             --imin;
1042             if (imin <= 1) {
1043                 myexit_();
1044             }
1045         }
1046     }
1047
1048 /*     Initialize iteration counter */
1049
1050     maxit = *q * 6 * *q;
1051     iter = 0;
1052
1053 /*     Begin main iteration loop */
1054
1055     while(imax > 1) {
1056
1057 /*        Compute the matrix entries */
1058
1059         b11d[imin] = cos(theta[imin]);
1060         b21d[imin] = -sin(theta[imin]);
1061         i__1 = imax - 1;
1062         for (i__ = imin; i__ <= i__1; ++i__) {
1063             b11e[i__] = -sin(theta[i__]) * sin(phi[i__]);
1064             b11d[i__ + 1] = cos(theta[i__ + 1]) * cos(phi[i__]);
1065             b12d[i__] = sin(theta[i__]) * cos(phi[i__]);
1066             b12e[i__] = cos(theta[i__ + 1]) * sin(phi[i__]);
1067             b21e[i__] = -cos(theta[i__]) * sin(phi[i__]);
1068             b21d[i__ + 1] = -sin(theta[i__ + 1]) * cos(phi[i__]);
1069             b22d[i__] = cos(theta[i__]) * cos(phi[i__]);
1070             b22e[i__] = -sin(theta[i__ + 1]) * sin(phi[i__]);
1071         }
1072         b12d[imax] = sin(theta[imax]);
1073         b22d[imax] = cos(theta[imax]);
1074
1075 /*        Abort if not converging; otherwise, increment ITER */
1076
1077         if (iter > maxit) {
1078             *info = 0;
1079             i__1 = *q;
1080             for (i__ = 1; i__ <= i__1; ++i__) {
1081                 if (phi[i__] != 0.) {
1082                     ++(*info);
1083                 }
1084             }
1085             return 0;
1086         }
1087
1088         iter = iter + imax - imin;
1089
1090 /*        Compute shifts */
1091
1092         thetamax = theta[imin];
1093         thetamin = theta[imin];
1094         i__1 = imax;
1095         for (i__ = imin + 1; i__ <= i__1; ++i__) {
1096             if (theta[i__] > thetamax) {
1097                 thetamax = theta[i__];
1098             }
1099             if (theta[i__] < thetamin) {
1100                 thetamin = theta[i__];
1101             }
1102         }
1103
1104         if (thetamax > 1.57079632679489662 - thresh) {
1105
1106 /*           Zero on diagonals of B11 and B22; induce deflation with a */
1107 /*           zero shift */
1108
1109             mu = 0.;
1110             nu = 1.;
1111
1112         } else if (thetamin < thresh) {
1113
1114 /*           Zero on diagonals of B12 and B22; induce deflation with a */
1115 /*           zero shift */
1116
1117             mu = 1.;
1118             nu = 0.;
1119
1120         } else {
1121
1122 /*           Compute shifts for B11 and B21 and use the lesser */
1123
1124             dlas2_(&b11d[imax - 1], &b11e[imax - 1], &b11d[imax], &sigma11, &
1125                     dummy);
1126             dlas2_(&b21d[imax - 1], &b21e[imax - 1], &b21d[imax], &sigma21, &
1127                     dummy);
1128
1129             if (sigma11 <= sigma21) {
1130                 mu = sigma11;
1131 /* Computing 2nd power */
1132                 d__1 = mu;
1133                 nu = sqrt(1. - d__1 * d__1);
1134                 if (mu < thresh) {
1135                     mu = 0.;
1136                     nu = 1.;
1137                 }
1138             } else {
1139                 nu = sigma21;
1140 /* Computing 2nd power */
1141                 d__1 = nu;
1142                 mu = sqrt(1.f - d__1 * d__1);
1143                 if (nu < thresh) {
1144                     mu = 1.;
1145                     nu = 0.;
1146                 }
1147             }
1148         }
1149
1150 /*        Rotate to produce bulges in B11 and B21 */
1151
1152         if (mu <= nu) {
1153             dlartgs_(&b11d[imin], &b11e[imin], &mu, &rwork[iv1tcs + imin - 1],
1154                      &rwork[iv1tsn + imin - 1]);
1155         } else {
1156             dlartgs_(&b21d[imin], &b21e[imin], &nu, &rwork[iv1tcs + imin - 1],
1157                      &rwork[iv1tsn + imin - 1]);
1158         }
1159
1160         temp = rwork[iv1tcs + imin - 1] * b11d[imin] + rwork[iv1tsn + imin - 
1161                 1] * b11e[imin];
1162         b11e[imin] = rwork[iv1tcs + imin - 1] * b11e[imin] - rwork[iv1tsn + 
1163                 imin - 1] * b11d[imin];
1164         b11d[imin] = temp;
1165         b11bulge = rwork[iv1tsn + imin - 1] * b11d[imin + 1];
1166         b11d[imin + 1] = rwork[iv1tcs + imin - 1] * b11d[imin + 1];
1167         temp = rwork[iv1tcs + imin - 1] * b21d[imin] + rwork[iv1tsn + imin - 
1168                 1] * b21e[imin];
1169         b21e[imin] = rwork[iv1tcs + imin - 1] * b21e[imin] - rwork[iv1tsn + 
1170                 imin - 1] * b21d[imin];
1171         b21d[imin] = temp;
1172         b21bulge = rwork[iv1tsn + imin - 1] * b21d[imin + 1];
1173         b21d[imin + 1] = rwork[iv1tcs + imin - 1] * b21d[imin + 1];
1174
1175 /*        Compute THETA(IMIN) */
1176
1177 /* Computing 2nd power */
1178         d__1 = b21d[imin];
1179 /* Computing 2nd power */
1180         d__2 = b21bulge;
1181 /* Computing 2nd power */
1182         d__3 = b11d[imin];
1183 /* Computing 2nd power */
1184         d__4 = b11bulge;
1185         theta[imin] = atan2(sqrt(d__1 * d__1 + d__2 * d__2), sqrt(d__3 * d__3 
1186                 + d__4 * d__4));
1187
1188 /*        Chase the bulges in B11(IMIN+1,IMIN) and B21(IMIN+1,IMIN) */
1189
1190 /* Computing 2nd power */
1191         d__1 = b11d[imin];
1192 /* Computing 2nd power */
1193         d__2 = b11bulge;
1194 /* Computing 2nd power */
1195         d__3 = thresh;
1196         if (d__1 * d__1 + d__2 * d__2 > d__3 * d__3) {
1197             dlartgp_(&b11bulge, &b11d[imin], &rwork[iu1sn + imin - 1], &rwork[
1198                     iu1cs + imin - 1], &r__);
1199         } else if (mu <= nu) {
1200             dlartgs_(&b11e[imin], &b11d[imin + 1], &mu, &rwork[iu1cs + imin - 
1201                     1], &rwork[iu1sn + imin - 1]);
1202         } else {
1203             dlartgs_(&b12d[imin], &b12e[imin], &nu, &rwork[iu1cs + imin - 1], 
1204                     &rwork[iu1sn + imin - 1]);
1205         }
1206 /* Computing 2nd power */
1207         d__1 = b21d[imin];
1208 /* Computing 2nd power */
1209         d__2 = b21bulge;
1210 /* Computing 2nd power */
1211         d__3 = thresh;
1212         if (d__1 * d__1 + d__2 * d__2 > d__3 * d__3) {
1213             dlartgp_(&b21bulge, &b21d[imin], &rwork[iu2sn + imin - 1], &rwork[
1214                     iu2cs + imin - 1], &r__);
1215         } else if (nu < mu) {
1216             dlartgs_(&b21e[imin], &b21d[imin + 1], &nu, &rwork[iu2cs + imin - 
1217                     1], &rwork[iu2sn + imin - 1]);
1218         } else {
1219             dlartgs_(&b22d[imin], &b22e[imin], &mu, &rwork[iu2cs + imin - 1], 
1220                     &rwork[iu2sn + imin - 1]);
1221         }
1222         rwork[iu2cs + imin - 1] = -rwork[iu2cs + imin - 1];
1223         rwork[iu2sn + imin - 1] = -rwork[iu2sn + imin - 1];
1224
1225         temp = rwork[iu1cs + imin - 1] * b11e[imin] + rwork[iu1sn + imin - 1] 
1226                 * b11d[imin + 1];
1227         b11d[imin + 1] = rwork[iu1cs + imin - 1] * b11d[imin + 1] - rwork[
1228                 iu1sn + imin - 1] * b11e[imin];
1229         b11e[imin] = temp;
1230         if (imax > imin + 1) {
1231             b11bulge = rwork[iu1sn + imin - 1] * b11e[imin + 1];
1232             b11e[imin + 1] = rwork[iu1cs + imin - 1] * b11e[imin + 1];
1233         }
1234         temp = rwork[iu1cs + imin - 1] * b12d[imin] + rwork[iu1sn + imin - 1] 
1235                 * b12e[imin];
1236         b12e[imin] = rwork[iu1cs + imin - 1] * b12e[imin] - rwork[iu1sn + 
1237                 imin - 1] * b12d[imin];
1238         b12d[imin] = temp;
1239         b12bulge = rwork[iu1sn + imin - 1] * b12d[imin + 1];
1240         b12d[imin + 1] = rwork[iu1cs + imin - 1] * b12d[imin + 1];
1241         temp = rwork[iu2cs + imin - 1] * b21e[imin] + rwork[iu2sn + imin - 1] 
1242                 * b21d[imin + 1];
1243         b21d[imin + 1] = rwork[iu2cs + imin - 1] * b21d[imin + 1] - rwork[
1244                 iu2sn + imin - 1] * b21e[imin];
1245         b21e[imin] = temp;
1246         if (imax > imin + 1) {
1247             b21bulge = rwork[iu2sn + imin - 1] * b21e[imin + 1];
1248             b21e[imin + 1] = rwork[iu2cs + imin - 1] * b21e[imin + 1];
1249         }
1250         temp = rwork[iu2cs + imin - 1] * b22d[imin] + rwork[iu2sn + imin - 1] 
1251                 * b22e[imin];
1252         b22e[imin] = rwork[iu2cs + imin - 1] * b22e[imin] - rwork[iu2sn + 
1253                 imin - 1] * b22d[imin];
1254         b22d[imin] = temp;
1255         b22bulge = rwork[iu2sn + imin - 1] * b22d[imin + 1];
1256         b22d[imin + 1] = rwork[iu2cs + imin - 1] * b22d[imin + 1];
1257
1258 /*        Inner loop: chase bulges from B11(IMIN,IMIN+2), */
1259 /*        B12(IMIN,IMIN+1), B21(IMIN,IMIN+2), and B22(IMIN,IMIN+1) to */
1260 /*        bottom-right */
1261
1262         i__1 = imax - 1;
1263         for (i__ = imin + 1; i__ <= i__1; ++i__) {
1264
1265 /*           Compute PHI(I-1) */
1266
1267             x1 = sin(theta[i__ - 1]) * b11e[i__ - 1] + cos(theta[i__ - 1]) * 
1268                     b21e[i__ - 1];
1269             x2 = sin(theta[i__ - 1]) * b11bulge + cos(theta[i__ - 1]) * 
1270                     b21bulge;
1271             y1 = sin(theta[i__ - 1]) * b12d[i__ - 1] + cos(theta[i__ - 1]) * 
1272                     b22d[i__ - 1];
1273             y2 = sin(theta[i__ - 1]) * b12bulge + cos(theta[i__ - 1]) * 
1274                     b22bulge;
1275
1276 /* Computing 2nd power */
1277             d__1 = x1;
1278 /* Computing 2nd power */
1279             d__2 = x2;
1280 /* Computing 2nd power */
1281             d__3 = y1;
1282 /* Computing 2nd power */
1283             d__4 = y2;
1284             phi[i__ - 1] = atan2(sqrt(d__1 * d__1 + d__2 * d__2), sqrt(d__3 * 
1285                     d__3 + d__4 * d__4));
1286
1287 /*           Determine if there are bulges to chase or if a new direct */
1288 /*           summand has been reached */
1289
1290 /* Computing 2nd power */
1291             d__1 = b11e[i__ - 1];
1292 /* Computing 2nd power */
1293             d__2 = b11bulge;
1294 /* Computing 2nd power */
1295             d__3 = thresh;
1296             restart11 = d__1 * d__1 + d__2 * d__2 <= d__3 * d__3;
1297 /* Computing 2nd power */
1298             d__1 = b21e[i__ - 1];
1299 /* Computing 2nd power */
1300             d__2 = b21bulge;
1301 /* Computing 2nd power */
1302             d__3 = thresh;
1303             restart21 = d__1 * d__1 + d__2 * d__2 <= d__3 * d__3;
1304 /* Computing 2nd power */
1305             d__1 = b12d[i__ - 1];
1306 /* Computing 2nd power */
1307             d__2 = b12bulge;
1308 /* Computing 2nd power */
1309             d__3 = thresh;
1310             restart12 = d__1 * d__1 + d__2 * d__2 <= d__3 * d__3;
1311 /* Computing 2nd power */
1312             d__1 = b22d[i__ - 1];
1313 /* Computing 2nd power */
1314             d__2 = b22bulge;
1315 /* Computing 2nd power */
1316             d__3 = thresh;
1317             restart22 = d__1 * d__1 + d__2 * d__2 <= d__3 * d__3;
1318
1319 /*           If possible, chase bulges from B11(I-1,I+1), B12(I-1,I), */
1320 /*           B21(I-1,I+1), and B22(I-1,I). If necessary, restart bulge- */
1321 /*           chasing by applying the original shift again. */
1322
1323             if (! restart11 && ! restart21) {
1324                 dlartgp_(&x2, &x1, &rwork[iv1tsn + i__ - 1], &rwork[iv1tcs + 
1325                         i__ - 1], &r__);
1326             } else if (! restart11 && restart21) {
1327                 dlartgp_(&b11bulge, &b11e[i__ - 1], &rwork[iv1tsn + i__ - 1], 
1328                         &rwork[iv1tcs + i__ - 1], &r__);
1329             } else if (restart11 && ! restart21) {
1330                 dlartgp_(&b21bulge, &b21e[i__ - 1], &rwork[iv1tsn + i__ - 1], 
1331                         &rwork[iv1tcs + i__ - 1], &r__);
1332             } else if (mu <= nu) {
1333                 dlartgs_(&b11d[i__], &b11e[i__], &mu, &rwork[iv1tcs + i__ - 1]
1334                         , &rwork[iv1tsn + i__ - 1]);
1335             } else {
1336                 dlartgs_(&b21d[i__], &b21e[i__], &nu, &rwork[iv1tcs + i__ - 1]
1337                         , &rwork[iv1tsn + i__ - 1]);
1338             }
1339             rwork[iv1tcs + i__ - 1] = -rwork[iv1tcs + i__ - 1];
1340             rwork[iv1tsn + i__ - 1] = -rwork[iv1tsn + i__ - 1];
1341             if (! restart12 && ! restart22) {
1342                 dlartgp_(&y2, &y1, &rwork[iv2tsn + i__ - 2], &rwork[iv2tcs + 
1343                         i__ - 2], &r__);
1344             } else if (! restart12 && restart22) {
1345                 dlartgp_(&b12bulge, &b12d[i__ - 1], &rwork[iv2tsn + i__ - 2], 
1346                         &rwork[iv2tcs + i__ - 2], &r__);
1347             } else if (restart12 && ! restart22) {
1348                 dlartgp_(&b22bulge, &b22d[i__ - 1], &rwork[iv2tsn + i__ - 2], 
1349                         &rwork[iv2tcs + i__ - 2], &r__);
1350             } else if (nu < mu) {
1351                 dlartgs_(&b12e[i__ - 1], &b12d[i__], &nu, &rwork[iv2tcs + i__ 
1352                         - 2], &rwork[iv2tsn + i__ - 2]);
1353             } else {
1354                 dlartgs_(&b22e[i__ - 1], &b22d[i__], &mu, &rwork[iv2tcs + i__ 
1355                         - 2], &rwork[iv2tsn + i__ - 2]);
1356             }
1357
1358             temp = rwork[iv1tcs + i__ - 1] * b11d[i__] + rwork[iv1tsn + i__ - 
1359                     1] * b11e[i__];
1360             b11e[i__] = rwork[iv1tcs + i__ - 1] * b11e[i__] - rwork[iv1tsn + 
1361                     i__ - 1] * b11d[i__];
1362             b11d[i__] = temp;
1363             b11bulge = rwork[iv1tsn + i__ - 1] * b11d[i__ + 1];
1364             b11d[i__ + 1] = rwork[iv1tcs + i__ - 1] * b11d[i__ + 1];
1365             temp = rwork[iv1tcs + i__ - 1] * b21d[i__] + rwork[iv1tsn + i__ - 
1366                     1] * b21e[i__];
1367             b21e[i__] = rwork[iv1tcs + i__ - 1] * b21e[i__] - rwork[iv1tsn + 
1368                     i__ - 1] * b21d[i__];
1369             b21d[i__] = temp;
1370             b21bulge = rwork[iv1tsn + i__ - 1] * b21d[i__ + 1];
1371             b21d[i__ + 1] = rwork[iv1tcs + i__ - 1] * b21d[i__ + 1];
1372             temp = rwork[iv2tcs + i__ - 2] * b12e[i__ - 1] + rwork[iv2tsn + 
1373                     i__ - 2] * b12d[i__];
1374             b12d[i__] = rwork[iv2tcs + i__ - 2] * b12d[i__] - rwork[iv2tsn + 
1375                     i__ - 2] * b12e[i__ - 1];
1376             b12e[i__ - 1] = temp;
1377             b12bulge = rwork[iv2tsn + i__ - 2] * b12e[i__];
1378             b12e[i__] = rwork[iv2tcs + i__ - 2] * b12e[i__];
1379             temp = rwork[iv2tcs + i__ - 2] * b22e[i__ - 1] + rwork[iv2tsn + 
1380                     i__ - 2] * b22d[i__];
1381             b22d[i__] = rwork[iv2tcs + i__ - 2] * b22d[i__] - rwork[iv2tsn + 
1382                     i__ - 2] * b22e[i__ - 1];
1383             b22e[i__ - 1] = temp;
1384             b22bulge = rwork[iv2tsn + i__ - 2] * b22e[i__];
1385             b22e[i__] = rwork[iv2tcs + i__ - 2] * b22e[i__];
1386
1387 /*           Compute THETA(I) */
1388
1389             x1 = cos(phi[i__ - 1]) * b11d[i__] + sin(phi[i__ - 1]) * b12e[i__ 
1390                     - 1];
1391             x2 = cos(phi[i__ - 1]) * b11bulge + sin(phi[i__ - 1]) * b12bulge;
1392             y1 = cos(phi[i__ - 1]) * b21d[i__] + sin(phi[i__ - 1]) * b22e[i__ 
1393                     - 1];
1394             y2 = cos(phi[i__ - 1]) * b21bulge + sin(phi[i__ - 1]) * b22bulge;
1395
1396 /* Computing 2nd power */
1397             d__1 = y1;
1398 /* Computing 2nd power */
1399             d__2 = y2;
1400 /* Computing 2nd power */
1401             d__3 = x1;
1402 /* Computing 2nd power */
1403             d__4 = x2;
1404             theta[i__] = atan2(sqrt(d__1 * d__1 + d__2 * d__2), sqrt(d__3 * 
1405                     d__3 + d__4 * d__4));
1406
1407 /*           Determine if there are bulges to chase or if a new direct */
1408 /*           summand has been reached */
1409
1410 /* Computing 2nd power */
1411             d__1 = b11d[i__];
1412 /* Computing 2nd power */
1413             d__2 = b11bulge;
1414 /* Computing 2nd power */
1415             d__3 = thresh;
1416             restart11 = d__1 * d__1 + d__2 * d__2 <= d__3 * d__3;
1417 /* Computing 2nd power */
1418             d__1 = b12e[i__ - 1];
1419 /* Computing 2nd power */
1420             d__2 = b12bulge;
1421 /* Computing 2nd power */
1422             d__3 = thresh;
1423             restart12 = d__1 * d__1 + d__2 * d__2 <= d__3 * d__3;
1424 /* Computing 2nd power */
1425             d__1 = b21d[i__];
1426 /* Computing 2nd power */
1427             d__2 = b21bulge;
1428 /* Computing 2nd power */
1429             d__3 = thresh;
1430             restart21 = d__1 * d__1 + d__2 * d__2 <= d__3 * d__3;
1431 /* Computing 2nd power */
1432             d__1 = b22e[i__ - 1];
1433 /* Computing 2nd power */
1434             d__2 = b22bulge;
1435 /* Computing 2nd power */
1436             d__3 = thresh;
1437             restart22 = d__1 * d__1 + d__2 * d__2 <= d__3 * d__3;
1438
1439 /*           If possible, chase bulges from B11(I+1,I), B12(I+1,I-1), */
1440 /*           B21(I+1,I), and B22(I+1,I-1). If necessary, restart bulge- */
1441 /*           chasing by applying the original shift again. */
1442
1443             if (! restart11 && ! restart12) {
1444                 dlartgp_(&x2, &x1, &rwork[iu1sn + i__ - 1], &rwork[iu1cs + 
1445                         i__ - 1], &r__);
1446             } else if (! restart11 && restart12) {
1447                 dlartgp_(&b11bulge, &b11d[i__], &rwork[iu1sn + i__ - 1], &
1448                         rwork[iu1cs + i__ - 1], &r__);
1449             } else if (restart11 && ! restart12) {
1450                 dlartgp_(&b12bulge, &b12e[i__ - 1], &rwork[iu1sn + i__ - 1], &
1451                         rwork[iu1cs + i__ - 1], &r__);
1452             } else if (mu <= nu) {
1453                 dlartgs_(&b11e[i__], &b11d[i__ + 1], &mu, &rwork[iu1cs + i__ 
1454                         - 1], &rwork[iu1sn + i__ - 1]);
1455             } else {
1456                 dlartgs_(&b12d[i__], &b12e[i__], &nu, &rwork[iu1cs + i__ - 1],
1457                          &rwork[iu1sn + i__ - 1]);
1458             }
1459             if (! restart21 && ! restart22) {
1460                 dlartgp_(&y2, &y1, &rwork[iu2sn + i__ - 1], &rwork[iu2cs + 
1461                         i__ - 1], &r__);
1462             } else if (! restart21 && restart22) {
1463                 dlartgp_(&b21bulge, &b21d[i__], &rwork[iu2sn + i__ - 1], &
1464                         rwork[iu2cs + i__ - 1], &r__);
1465             } else if (restart21 && ! restart22) {
1466                 dlartgp_(&b22bulge, &b22e[i__ - 1], &rwork[iu2sn + i__ - 1], &
1467                         rwork[iu2cs + i__ - 1], &r__);
1468             } else if (nu < mu) {
1469                 dlartgs_(&b21e[i__], &b21e[i__ + 1], &nu, &rwork[iu2cs + i__ 
1470                         - 1], &rwork[iu2sn + i__ - 1]);
1471             } else {
1472                 dlartgs_(&b22d[i__], &b22e[i__], &mu, &rwork[iu2cs + i__ - 1],
1473                          &rwork[iu2sn + i__ - 1]);
1474             }
1475             rwork[iu2cs + i__ - 1] = -rwork[iu2cs + i__ - 1];
1476             rwork[iu2sn + i__ - 1] = -rwork[iu2sn + i__ - 1];
1477
1478             temp = rwork[iu1cs + i__ - 1] * b11e[i__] + rwork[iu1sn + i__ - 1]
1479                      * b11d[i__ + 1];
1480             b11d[i__ + 1] = rwork[iu1cs + i__ - 1] * b11d[i__ + 1] - rwork[
1481                     iu1sn + i__ - 1] * b11e[i__];
1482             b11e[i__] = temp;
1483             if (i__ < imax - 1) {
1484                 b11bulge = rwork[iu1sn + i__ - 1] * b11e[i__ + 1];
1485                 b11e[i__ + 1] = rwork[iu1cs + i__ - 1] * b11e[i__ + 1];
1486             }
1487             temp = rwork[iu2cs + i__ - 1] * b21e[i__] + rwork[iu2sn + i__ - 1]
1488                      * b21d[i__ + 1];
1489             b21d[i__ + 1] = rwork[iu2cs + i__ - 1] * b21d[i__ + 1] - rwork[
1490                     iu2sn + i__ - 1] * b21e[i__];
1491             b21e[i__] = temp;
1492             if (i__ < imax - 1) {
1493                 b21bulge = rwork[iu2sn + i__ - 1] * b21e[i__ + 1];
1494                 b21e[i__ + 1] = rwork[iu2cs + i__ - 1] * b21e[i__ + 1];
1495             }
1496             temp = rwork[iu1cs + i__ - 1] * b12d[i__] + rwork[iu1sn + i__ - 1]
1497                      * b12e[i__];
1498             b12e[i__] = rwork[iu1cs + i__ - 1] * b12e[i__] - rwork[iu1sn + 
1499                     i__ - 1] * b12d[i__];
1500             b12d[i__] = temp;
1501             b12bulge = rwork[iu1sn + i__ - 1] * b12d[i__ + 1];
1502             b12d[i__ + 1] = rwork[iu1cs + i__ - 1] * b12d[i__ + 1];
1503             temp = rwork[iu2cs + i__ - 1] * b22d[i__] + rwork[iu2sn + i__ - 1]
1504                      * b22e[i__];
1505             b22e[i__] = rwork[iu2cs + i__ - 1] * b22e[i__] - rwork[iu2sn + 
1506                     i__ - 1] * b22d[i__];
1507             b22d[i__] = temp;
1508             b22bulge = rwork[iu2sn + i__ - 1] * b22d[i__ + 1];
1509             b22d[i__ + 1] = rwork[iu2cs + i__ - 1] * b22d[i__ + 1];
1510
1511         }
1512
1513 /*        Compute PHI(IMAX-1) */
1514
1515         x1 = sin(theta[imax - 1]) * b11e[imax - 1] + cos(theta[imax - 1]) * 
1516                 b21e[imax - 1];
1517         y1 = sin(theta[imax - 1]) * b12d[imax - 1] + cos(theta[imax - 1]) * 
1518                 b22d[imax - 1];
1519         y2 = sin(theta[imax - 1]) * b12bulge + cos(theta[imax - 1]) * 
1520                 b22bulge;
1521
1522 /* Computing 2nd power */
1523         d__1 = y1;
1524 /* Computing 2nd power */
1525         d__2 = y2;
1526         phi[imax - 1] = atan2((abs(x1)), sqrt(d__1 * d__1 + d__2 * d__2));
1527
1528 /*        Chase bulges from B12(IMAX-1,IMAX) and B22(IMAX-1,IMAX) */
1529
1530 /* Computing 2nd power */
1531         d__1 = b12d[imax - 1];
1532 /* Computing 2nd power */
1533         d__2 = b12bulge;
1534 /* Computing 2nd power */
1535         d__3 = thresh;
1536         restart12 = d__1 * d__1 + d__2 * d__2 <= d__3 * d__3;
1537 /* Computing 2nd power */
1538         d__1 = b22d[imax - 1];
1539 /* Computing 2nd power */
1540         d__2 = b22bulge;
1541 /* Computing 2nd power */
1542         d__3 = thresh;
1543         restart22 = d__1 * d__1 + d__2 * d__2 <= d__3 * d__3;
1544
1545         if (! restart12 && ! restart22) {
1546             dlartgp_(&y2, &y1, &rwork[iv2tsn + imax - 2], &rwork[iv2tcs + 
1547                     imax - 2], &r__);
1548         } else if (! restart12 && restart22) {
1549             dlartgp_(&b12bulge, &b12d[imax - 1], &rwork[iv2tsn + imax - 2], &
1550                     rwork[iv2tcs + imax - 2], &r__);
1551         } else if (restart12 && ! restart22) {
1552             dlartgp_(&b22bulge, &b22d[imax - 1], &rwork[iv2tsn + imax - 2], &
1553                     rwork[iv2tcs + imax - 2], &r__);
1554         } else if (nu < mu) {
1555             dlartgs_(&b12e[imax - 1], &b12d[imax], &nu, &rwork[iv2tcs + imax 
1556                     - 2], &rwork[iv2tsn + imax - 2]);
1557         } else {
1558             dlartgs_(&b22e[imax - 1], &b22d[imax], &mu, &rwork[iv2tcs + imax 
1559                     - 2], &rwork[iv2tsn + imax - 2]);
1560         }
1561
1562         temp = rwork[iv2tcs + imax - 2] * b12e[imax - 1] + rwork[iv2tsn + 
1563                 imax - 2] * b12d[imax];
1564         b12d[imax] = rwork[iv2tcs + imax - 2] * b12d[imax] - rwork[iv2tsn + 
1565                 imax - 2] * b12e[imax - 1];
1566         b12e[imax - 1] = temp;
1567         temp = rwork[iv2tcs + imax - 2] * b22e[imax - 1] + rwork[iv2tsn + 
1568                 imax - 2] * b22d[imax];
1569         b22d[imax] = rwork[iv2tcs + imax - 2] * b22d[imax] - rwork[iv2tsn + 
1570                 imax - 2] * b22e[imax - 1];
1571         b22e[imax - 1] = temp;
1572
1573 /*        Update singular vectors */
1574
1575         if (wantu1) {
1576             if (colmajor) {
1577                 i__1 = imax - imin + 1;
1578                 zlasr_("R", "V", "F", p, &i__1, &rwork[iu1cs + imin - 1], &
1579                         rwork[iu1sn + imin - 1], &u1[imin * u1_dim1 + 1], 
1580                         ldu1);
1581             } else {
1582                 i__1 = imax - imin + 1;
1583                 zlasr_("L", "V", "F", &i__1, p, &rwork[iu1cs + imin - 1], &
1584                         rwork[iu1sn + imin - 1], &u1[imin + u1_dim1], ldu1);
1585             }
1586         }
1587         if (wantu2) {
1588             if (colmajor) {
1589                 i__1 = *m - *p;
1590                 i__2 = imax - imin + 1;
1591                 zlasr_("R", "V", "F", &i__1, &i__2, &rwork[iu2cs + imin - 1], 
1592                         &rwork[iu2sn + imin - 1], &u2[imin * u2_dim1 + 1], 
1593                         ldu2);
1594             } else {
1595                 i__1 = imax - imin + 1;
1596                 i__2 = *m - *p;
1597                 zlasr_("L", "V", "F", &i__1, &i__2, &rwork[iu2cs + imin - 1], 
1598                         &rwork[iu2sn + imin - 1], &u2[imin + u2_dim1], ldu2);
1599             }
1600         }
1601         if (wantv1t) {
1602             if (colmajor) {
1603                 i__1 = imax - imin + 1;
1604                 zlasr_("L", "V", "F", &i__1, q, &rwork[iv1tcs + imin - 1], &
1605                         rwork[iv1tsn + imin - 1], &v1t[imin + v1t_dim1], 
1606                         ldv1t);
1607             } else {
1608                 i__1 = imax - imin + 1;
1609                 zlasr_("R", "V", "F", q, &i__1, &rwork[iv1tcs + imin - 1], &
1610                         rwork[iv1tsn + imin - 1], &v1t[imin * v1t_dim1 + 1], 
1611                         ldv1t);
1612             }
1613         }
1614         if (wantv2t) {
1615             if (colmajor) {
1616                 i__1 = imax - imin + 1;
1617                 i__2 = *m - *q;
1618                 zlasr_("L", "V", "F", &i__1, &i__2, &rwork[iv2tcs + imin - 1],
1619                          &rwork[iv2tsn + imin - 1], &v2t[imin + v2t_dim1], 
1620                         ldv2t);
1621             } else {
1622                 i__1 = *m - *q;
1623                 i__2 = imax - imin + 1;
1624                 zlasr_("R", "V", "F", &i__1, &i__2, &rwork[iv2tcs + imin - 1],
1625                          &rwork[iv2tsn + imin - 1], &v2t[imin * v2t_dim1 + 1],
1626                          ldv2t);
1627             }
1628         }
1629
1630 /*        Fix signs on B11(IMAX-1,IMAX) and B21(IMAX-1,IMAX) */
1631
1632         if (b11e[imax - 1] + b21e[imax - 1] > 0.) {
1633             b11d[imax] = -b11d[imax];
1634             b21d[imax] = -b21d[imax];
1635             if (wantv1t) {
1636                 if (colmajor) {
1637                     zscal_(q, &c_b1, &v1t[imax + v1t_dim1], ldv1t);
1638                 } else {
1639                     zscal_(q, &c_b1, &v1t[imax * v1t_dim1 + 1], &c__1);
1640                 }
1641             }
1642         }
1643
1644 /*        Compute THETA(IMAX) */
1645
1646         x1 = cos(phi[imax - 1]) * b11d[imax] + sin(phi[imax - 1]) * b12e[imax 
1647                 - 1];
1648         y1 = cos(phi[imax - 1]) * b21d[imax] + sin(phi[imax - 1]) * b22e[imax 
1649                 - 1];
1650
1651         theta[imax] = atan2((abs(y1)), (abs(x1)));
1652
1653 /*        Fix signs on B11(IMAX,IMAX), B12(IMAX,IMAX-1), B21(IMAX,IMAX), */
1654 /*        and B22(IMAX,IMAX-1) */
1655
1656         if (b11d[imax] + b12e[imax - 1] < 0.) {
1657             b12d[imax] = -b12d[imax];
1658             if (wantu1) {
1659                 if (colmajor) {
1660                     zscal_(p, &c_b1, &u1[imax * u1_dim1 + 1], &c__1);
1661                 } else {
1662                     zscal_(p, &c_b1, &u1[imax + u1_dim1], ldu1);
1663                 }
1664             }
1665         }
1666         if (b21d[imax] + b22e[imax - 1] > 0.) {
1667             b22d[imax] = -b22d[imax];
1668             if (wantu2) {
1669                 if (colmajor) {
1670                     i__1 = *m - *p;
1671                     zscal_(&i__1, &c_b1, &u2[imax * u2_dim1 + 1], &c__1);
1672                 } else {
1673                     i__1 = *m - *p;
1674                     zscal_(&i__1, &c_b1, &u2[imax + u2_dim1], ldu2);
1675                 }
1676             }
1677         }
1678
1679 /*        Fix signs on B12(IMAX,IMAX) and B22(IMAX,IMAX) */
1680
1681         if (b12d[imax] + b22d[imax] < 0.) {
1682             if (wantv2t) {
1683                 if (colmajor) {
1684                     i__1 = *m - *q;
1685                     zscal_(&i__1, &c_b1, &v2t[imax + v2t_dim1], ldv2t);
1686                 } else {
1687                     i__1 = *m - *q;
1688                     zscal_(&i__1, &c_b1, &v2t[imax * v2t_dim1 + 1], &c__1);
1689                 }
1690             }
1691         }
1692
1693 /*        Test for negligible sines or cosines */
1694
1695         i__1 = imax;
1696         for (i__ = imin; i__ <= i__1; ++i__) {
1697             if (theta[i__] < thresh) {
1698                 theta[i__] = 0.;
1699             } else if (theta[i__] > 1.57079632679489662 - thresh) {
1700                 theta[i__] = 1.57079632679489662;
1701             }
1702         }
1703         i__1 = imax - 1;
1704         for (i__ = imin; i__ <= i__1; ++i__) {
1705             if (phi[i__] < thresh) {
1706                 phi[i__] = 0.;
1707             } else if (phi[i__] > 1.57079632679489662 - thresh) {
1708                 phi[i__] = 1.57079632679489662;
1709             }
1710         }
1711
1712 /*        Deflate */
1713
1714         if (imax > 1) {
1715             while(phi[imax - 1] == 0.) {
1716                 --imax;
1717                 if (imax <= 1) {
1718                     myexit_();
1719                 }
1720             }
1721         }
1722         if (imin > imax - 1) {
1723             imin = imax - 1;
1724         }
1725         if (imin > 1) {
1726             while(phi[imin - 1] != 0.) {
1727                 --imin;
1728                 if (imin <= 1) {
1729                     myexit_();
1730                 }
1731             }
1732         }
1733
1734 /*        Repeat main iteration loop */
1735
1736     }
1737
1738 /*     Postprocessing: order THETA from least to greatest */
1739
1740     i__1 = *q;
1741     for (i__ = 1; i__ <= i__1; ++i__) {
1742
1743         mini = i__;
1744         thetamin = theta[i__];
1745         i__2 = *q;
1746         for (j = i__ + 1; j <= i__2; ++j) {
1747             if (theta[j] < thetamin) {
1748                 mini = j;
1749                 thetamin = theta[j];
1750             }
1751         }
1752
1753         if (mini != i__) {
1754             theta[mini] = theta[i__];
1755             theta[i__] = thetamin;
1756             if (colmajor) {
1757                 if (wantu1) {
1758                     zswap_(p, &u1[i__ * u1_dim1 + 1], &c__1, &u1[mini * 
1759                             u1_dim1 + 1], &c__1);
1760                 }
1761                 if (wantu2) {
1762                     i__2 = *m - *p;
1763                     zswap_(&i__2, &u2[i__ * u2_dim1 + 1], &c__1, &u2[mini * 
1764                             u2_dim1 + 1], &c__1);
1765                 }
1766                 if (wantv1t) {
1767                     zswap_(q, &v1t[i__ + v1t_dim1], ldv1t, &v1t[mini + 
1768                             v1t_dim1], ldv1t);
1769                 }
1770                 if (wantv2t) {
1771                     i__2 = *m - *q;
1772                     zswap_(&i__2, &v2t[i__ + v2t_dim1], ldv2t, &v2t[mini + 
1773                             v2t_dim1], ldv2t);
1774                 }
1775             } else {
1776                 if (wantu1) {
1777                     zswap_(p, &u1[i__ + u1_dim1], ldu1, &u1[mini + u1_dim1], 
1778                             ldu1);
1779                 }
1780                 if (wantu2) {
1781                     i__2 = *m - *p;
1782                     zswap_(&i__2, &u2[i__ + u2_dim1], ldu2, &u2[mini + 
1783                             u2_dim1], ldu2);
1784                 }
1785                 if (wantv1t) {
1786                     zswap_(q, &v1t[i__ * v1t_dim1 + 1], &c__1, &v1t[mini * 
1787                             v1t_dim1 + 1], &c__1);
1788                 }
1789                 if (wantv2t) {
1790                     i__2 = *m - *q;
1791                     zswap_(&i__2, &v2t[i__ * v2t_dim1 + 1], &c__1, &v2t[mini *
1792                              v2t_dim1 + 1], &c__1);
1793                 }
1794             }
1795         }
1796
1797     }
1798
1799     return 0;
1800
1801 /*     End of ZBBCSD */
1802
1803 } /* zbbcsd_ */
1804