C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / zuncsd2by1.c
1 #include <math.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <complex.h>
6 #ifdef complex
7 #undef complex
8 #endif
9 #ifdef I
10 #undef I
11 #endif
12
13 #if defined(_WIN64)
14 typedef long long BLASLONG;
15 typedef unsigned long long BLASULONG;
16 #else
17 typedef long BLASLONG;
18 typedef unsigned long BLASULONG;
19 #endif
20
21 #ifdef LAPACK_ILP64
22 typedef BLASLONG blasint;
23 #if defined(_WIN64)
24 #define blasabs(x) llabs(x)
25 #else
26 #define blasabs(x) labs(x)
27 #endif
28 #else
29 typedef int blasint;
30 #define blasabs(x) abs(x)
31 #endif
32
33 typedef blasint integer;
34
35 typedef unsigned int uinteger;
36 typedef char *address;
37 typedef short int shortint;
38 typedef float real;
39 typedef double doublereal;
40 typedef struct { real r, i; } complex;
41 typedef struct { doublereal r, i; } doublecomplex;
42 #ifdef _MSC_VER
43 static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
44 static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
45 static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
46 static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
47 #else
48 static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
49 static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
50 static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
51 static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
52 #endif
53 #define pCf(z) (*_pCf(z))
54 #define pCd(z) (*_pCd(z))
55 typedef int logical;
56 typedef short int shortlogical;
57 typedef char logical1;
58 typedef char integer1;
59
60 #define TRUE_ (1)
61 #define FALSE_ (0)
62
63 /* Extern is for use with -E */
64 #ifndef Extern
65 #define Extern extern
66 #endif
67
68 /* I/O stuff */
69
70 typedef int flag;
71 typedef int ftnlen;
72 typedef int ftnint;
73
74 /*external read, write*/
75 typedef struct
76 {       flag cierr;
77         ftnint ciunit;
78         flag ciend;
79         char *cifmt;
80         ftnint cirec;
81 } cilist;
82
83 /*internal read, write*/
84 typedef struct
85 {       flag icierr;
86         char *iciunit;
87         flag iciend;
88         char *icifmt;
89         ftnint icirlen;
90         ftnint icirnum;
91 } icilist;
92
93 /*open*/
94 typedef struct
95 {       flag oerr;
96         ftnint ounit;
97         char *ofnm;
98         ftnlen ofnmlen;
99         char *osta;
100         char *oacc;
101         char *ofm;
102         ftnint orl;
103         char *oblnk;
104 } olist;
105
106 /*close*/
107 typedef struct
108 {       flag cerr;
109         ftnint cunit;
110         char *csta;
111 } cllist;
112
113 /*rewind, backspace, endfile*/
114 typedef struct
115 {       flag aerr;
116         ftnint aunit;
117 } alist;
118
119 /* inquire */
120 typedef struct
121 {       flag inerr;
122         ftnint inunit;
123         char *infile;
124         ftnlen infilen;
125         ftnint  *inex;  /*parameters in standard's order*/
126         ftnint  *inopen;
127         ftnint  *innum;
128         ftnint  *innamed;
129         char    *inname;
130         ftnlen  innamlen;
131         char    *inacc;
132         ftnlen  inacclen;
133         char    *inseq;
134         ftnlen  inseqlen;
135         char    *indir;
136         ftnlen  indirlen;
137         char    *infmt;
138         ftnlen  infmtlen;
139         char    *inform;
140         ftnint  informlen;
141         char    *inunf;
142         ftnlen  inunflen;
143         ftnint  *inrecl;
144         ftnint  *innrec;
145         char    *inblank;
146         ftnlen  inblanklen;
147 } inlist;
148
149 #define VOID void
150
151 union Multitype {       /* for multiple entry points */
152         integer1 g;
153         shortint h;
154         integer i;
155         /* longint j; */
156         real r;
157         doublereal d;
158         complex c;
159         doublecomplex z;
160         };
161
162 typedef union Multitype Multitype;
163
164 struct Vardesc {        /* for Namelist */
165         char *name;
166         char *addr;
167         ftnlen *dims;
168         int  type;
169         };
170 typedef struct Vardesc Vardesc;
171
172 struct Namelist {
173         char *name;
174         Vardesc **vars;
175         int nvars;
176         };
177 typedef struct Namelist Namelist;
178
179 #define abs(x) ((x) >= 0 ? (x) : -(x))
180 #define dabs(x) (fabs(x))
181 #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
182 #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
183 #define dmin(a,b) (f2cmin(a,b))
184 #define dmax(a,b) (f2cmax(a,b))
185 #define bit_test(a,b)   ((a) >> (b) & 1)
186 #define bit_clear(a,b)  ((a) & ~((uinteger)1 << (b)))
187 #define bit_set(a,b)    ((a) |  ((uinteger)1 << (b)))
188
189 #define abort_() { sig_die("Fortran abort routine called", 1); }
190 #define c_abs(z) (cabsf(Cf(z)))
191 #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
192 #ifdef _MSC_VER
193 #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
194 #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/Cd(b)._Val[1]);}
195 #else
196 #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
197 #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
198 #endif
199 #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
200 #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
201 #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
202 //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
203 #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
204 #define d_abs(x) (fabs(*(x)))
205 #define d_acos(x) (acos(*(x)))
206 #define d_asin(x) (asin(*(x)))
207 #define d_atan(x) (atan(*(x)))
208 #define d_atn2(x, y) (atan2(*(x),*(y)))
209 #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
210 #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
211 #define d_cos(x) (cos(*(x)))
212 #define d_cosh(x) (cosh(*(x)))
213 #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
214 #define d_exp(x) (exp(*(x)))
215 #define d_imag(z) (cimag(Cd(z)))
216 #define r_imag(z) (cimagf(Cf(z)))
217 #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
218 #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
219 #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
220 #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
221 #define d_log(x) (log(*(x)))
222 #define d_mod(x, y) (fmod(*(x), *(y)))
223 #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
224 #define d_nint(x) u_nint(*(x))
225 #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
226 #define d_sign(a,b) u_sign(*(a),*(b))
227 #define r_sign(a,b) u_sign(*(a),*(b))
228 #define d_sin(x) (sin(*(x)))
229 #define d_sinh(x) (sinh(*(x)))
230 #define d_sqrt(x) (sqrt(*(x)))
231 #define d_tan(x) (tan(*(x)))
232 #define d_tanh(x) (tanh(*(x)))
233 #define i_abs(x) abs(*(x))
234 #define i_dnnt(x) ((integer)u_nint(*(x)))
235 #define i_len(s, n) (n)
236 #define i_nint(x) ((integer)u_nint(*(x)))
237 #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
238 #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
239 #define pow_si(B,E) spow_ui(*(B),*(E))
240 #define pow_ri(B,E) spow_ui(*(B),*(E))
241 #define pow_di(B,E) dpow_ui(*(B),*(E))
242 #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
243 #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
244 #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
245 #define s_cat(lpp, rpp, rnp, np, llp) {         ftnlen i, nc, ll; char *f__rp, *lp;     ll = (llp); lp = (lpp);         for(i=0; i < (int)*(np); ++i) {                 nc = ll;                if((rnp)[i] < nc) nc = (rnp)[i];                ll -= nc;               f__rp = (rpp)[i];               while(--nc >= 0) *lp++ = *(f__rp)++;         }  while(--ll >= 0) *lp++ = ' '; }
246 #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
247 #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
248 #define sig_die(s, kill) { exit(1); }
249 #define s_stop(s, n) {exit(0);}
250 static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
251 #define z_abs(z) (cabs(Cd(z)))
252 #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
253 #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
254 #define myexit_() break;
255 #define mycycle_() continue;
256 #define myceiling_(w) {ceil(w)}
257 #define myhuge_(w) {HUGE_VAL}
258 //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
259 #define mymaxloc_(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
260
261 /* procedure parameter types for -A and -C++ */
262
263 #define F2C_proc_par_types 1
264 #ifdef __cplusplus
265 typedef logical (*L_fp)(...);
266 #else
267 typedef logical (*L_fp)();
268 #endif
269
270 static float spow_ui(float x, integer n) {
271         float pow=1.0; unsigned long int u;
272         if(n != 0) {
273                 if(n < 0) n = -n, x = 1/x;
274                 for(u = n; ; ) {
275                         if(u & 01) pow *= x;
276                         if(u >>= 1) x *= x;
277                         else break;
278                 }
279         }
280         return pow;
281 }
282 static double dpow_ui(double x, integer n) {
283         double pow=1.0; unsigned long int u;
284         if(n != 0) {
285                 if(n < 0) n = -n, x = 1/x;
286                 for(u = n; ; ) {
287                         if(u & 01) pow *= x;
288                         if(u >>= 1) x *= x;
289                         else break;
290                 }
291         }
292         return pow;
293 }
294 #ifdef _MSC_VER
295 static _Fcomplex cpow_ui(complex x, integer n) {
296         complex pow={1.0,0.0}; unsigned long int u;
297                 if(n != 0) {
298                 if(n < 0) n = -n, x.r = 1/x.r, x.i=1/x.i;
299                 for(u = n; ; ) {
300                         if(u & 01) pow.r *= x.r, pow.i *= x.i;
301                         if(u >>= 1) x.r *= x.r, x.i *= x.i;
302                         else break;
303                 }
304         }
305         _Fcomplex p={pow.r, pow.i};
306         return p;
307 }
308 #else
309 static _Complex float cpow_ui(_Complex float x, integer n) {
310         _Complex float pow=1.0; unsigned long int u;
311         if(n != 0) {
312                 if(n < 0) n = -n, x = 1/x;
313                 for(u = n; ; ) {
314                         if(u & 01) pow *= x;
315                         if(u >>= 1) x *= x;
316                         else break;
317                 }
318         }
319         return pow;
320 }
321 #endif
322 #ifdef _MSC_VER
323 static _Dcomplex zpow_ui(_Dcomplex x, integer n) {
324         _Dcomplex pow={1.0,0.0}; unsigned long int u;
325         if(n != 0) {
326                 if(n < 0) n = -n, x._Val[0] = 1/x._Val[0], x._Val[1] =1/x._Val[1];
327                 for(u = n; ; ) {
328                         if(u & 01) pow._Val[0] *= x._Val[0], pow._Val[1] *= x._Val[1];
329                         if(u >>= 1) x._Val[0] *= x._Val[0], x._Val[1] *= x._Val[1];
330                         else break;
331                 }
332         }
333         _Dcomplex p = {pow._Val[0], pow._Val[1]};
334         return p;
335 }
336 #else
337 static _Complex double zpow_ui(_Complex double x, integer n) {
338         _Complex double pow=1.0; unsigned long int u;
339         if(n != 0) {
340                 if(n < 0) n = -n, x = 1/x;
341                 for(u = n; ; ) {
342                         if(u & 01) pow *= x;
343                         if(u >>= 1) x *= x;
344                         else break;
345                 }
346         }
347         return pow;
348 }
349 #endif
350 static integer pow_ii(integer x, integer n) {
351         integer pow; unsigned long int u;
352         if (n <= 0) {
353                 if (n == 0 || x == 1) pow = 1;
354                 else if (x != -1) pow = x == 0 ? 1/x : 0;
355                 else n = -n;
356         }
357         if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
358                 u = n;
359                 for(pow = 1; ; ) {
360                         if(u & 01) pow *= x;
361                         if(u >>= 1) x *= x;
362                         else break;
363                 }
364         }
365         return pow;
366 }
367 static integer dmaxloc_(double *w, integer s, integer e, integer *n)
368 {
369         double m; integer i, mi;
370         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
371                 if (w[i-1]>m) mi=i ,m=w[i-1];
372         return mi-s+1;
373 }
374 static integer smaxloc_(float *w, integer s, integer e, integer *n)
375 {
376         float m; integer i, mi;
377         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
378                 if (w[i-1]>m) mi=i ,m=w[i-1];
379         return mi-s+1;
380 }
381 static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
382         integer n = *n_, incx = *incx_, incy = *incy_, i;
383 #ifdef _MSC_VER
384         _Fcomplex zdotc = {0.0, 0.0};
385         if (incx == 1 && incy == 1) {
386                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
387                         zdotc._Val[0] += conjf(Cf(&x[i]))._Val[0] * Cf(&y[i])._Val[0];
388                         zdotc._Val[1] += conjf(Cf(&x[i]))._Val[1] * Cf(&y[i])._Val[1];
389                 }
390         } else {
391                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
392                         zdotc._Val[0] += conjf(Cf(&x[i*incx]))._Val[0] * Cf(&y[i*incy])._Val[0];
393                         zdotc._Val[1] += conjf(Cf(&x[i*incx]))._Val[1] * Cf(&y[i*incy])._Val[1];
394                 }
395         }
396         pCf(z) = zdotc;
397 }
398 #else
399         _Complex float zdotc = 0.0;
400         if (incx == 1 && incy == 1) {
401                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
402                         zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
403                 }
404         } else {
405                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
406                         zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
407                 }
408         }
409         pCf(z) = zdotc;
410 }
411 #endif
412 static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
413         integer n = *n_, incx = *incx_, incy = *incy_, i;
414 #ifdef _MSC_VER
415         _Dcomplex zdotc = {0.0, 0.0};
416         if (incx == 1 && incy == 1) {
417                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
418                         zdotc._Val[0] += conj(Cd(&x[i]))._Val[0] * Cd(&y[i])._Val[0];
419                         zdotc._Val[1] += conj(Cd(&x[i]))._Val[1] * Cd(&y[i])._Val[1];
420                 }
421         } else {
422                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
423                         zdotc._Val[0] += conj(Cd(&x[i*incx]))._Val[0] * Cd(&y[i*incy])._Val[0];
424                         zdotc._Val[1] += conj(Cd(&x[i*incx]))._Val[1] * Cd(&y[i*incy])._Val[1];
425                 }
426         }
427         pCd(z) = zdotc;
428 }
429 #else
430         _Complex double zdotc = 0.0;
431         if (incx == 1 && incy == 1) {
432                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
433                         zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
434                 }
435         } else {
436                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
437                         zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
438                 }
439         }
440         pCd(z) = zdotc;
441 }
442 #endif  
443 static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
444         integer n = *n_, incx = *incx_, incy = *incy_, i;
445 #ifdef _MSC_VER
446         _Fcomplex zdotc = {0.0, 0.0};
447         if (incx == 1 && incy == 1) {
448                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
449                         zdotc._Val[0] += Cf(&x[i])._Val[0] * Cf(&y[i])._Val[0];
450                         zdotc._Val[1] += Cf(&x[i])._Val[1] * Cf(&y[i])._Val[1];
451                 }
452         } else {
453                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
454                         zdotc._Val[0] += Cf(&x[i*incx])._Val[0] * Cf(&y[i*incy])._Val[0];
455                         zdotc._Val[1] += Cf(&x[i*incx])._Val[1] * Cf(&y[i*incy])._Val[1];
456                 }
457         }
458         pCf(z) = zdotc;
459 }
460 #else
461         _Complex float zdotc = 0.0;
462         if (incx == 1 && incy == 1) {
463                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
464                         zdotc += Cf(&x[i]) * Cf(&y[i]);
465                 }
466         } else {
467                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
468                         zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
469                 }
470         }
471         pCf(z) = zdotc;
472 }
473 #endif
474 static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
475         integer n = *n_, incx = *incx_, incy = *incy_, i;
476 #ifdef _MSC_VER
477         _Dcomplex zdotc = {0.0, 0.0};
478         if (incx == 1 && incy == 1) {
479                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
480                         zdotc._Val[0] += Cd(&x[i])._Val[0] * Cd(&y[i])._Val[0];
481                         zdotc._Val[1] += Cd(&x[i])._Val[1] * Cd(&y[i])._Val[1];
482                 }
483         } else {
484                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
485                         zdotc._Val[0] += Cd(&x[i*incx])._Val[0] * Cd(&y[i*incy])._Val[0];
486                         zdotc._Val[1] += Cd(&x[i*incx])._Val[1] * Cd(&y[i*incy])._Val[1];
487                 }
488         }
489         pCd(z) = zdotc;
490 }
491 #else
492         _Complex double zdotc = 0.0;
493         if (incx == 1 && incy == 1) {
494                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
495                         zdotc += Cd(&x[i]) * Cd(&y[i]);
496                 }
497         } else {
498                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
499                         zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
500                 }
501         }
502         pCd(z) = zdotc;
503 }
504 #endif
505 /*  -- translated by f2c (version 20000121).
506    You must link the resulting object file with the libraries:
507         -lf2c -lm   (in that order)
508 */
509
510
511
512
513 /* Table of constant values */
514
515 static integer c_n1 = -1;
516 static integer c__1 = 1;
517 static logical c_false = FALSE_;
518
519 /* > \brief \b ZUNCSD2BY1 */
520
521 /*  =========== DOCUMENTATION =========== */
522
523 /* Online html documentation available at */
524 /*            http://www.netlib.org/lapack/explore-html/ */
525
526 /* > \htmlonly */
527 /* > Download ZUNCSD2BY1 + dependencies */
528 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/zuncsd2
529 by1.f"> */
530 /* > [TGZ]</a> */
531 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/zuncsd2
532 by1.f"> */
533 /* > [ZIP]</a> */
534 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/zuncsd2
535 by1.f"> */
536 /* > [TXT]</a> */
537 /* > \endhtmlonly */
538
539 /*  Definition: */
540 /*  =========== */
541
542 /*       SUBROUTINE ZUNCSD2BY1( JOBU1, JOBU2, JOBV1T, M, P, Q, X11, LDX11, */
543 /*                              X21, LDX21, THETA, U1, LDU1, U2, LDU2, V1T, */
544 /*                              LDV1T, WORK, LWORK, RWORK, LRWORK, IWORK, */
545 /*                              INFO ) */
546
547 /*       CHARACTER          JOBU1, JOBU2, JOBV1T */
548 /*       INTEGER            INFO, LDU1, LDU2, LDV1T, LWORK, LDX11, LDX21, */
549 /*      $                   M, P, Q */
550 /*       INTEGER            LRWORK, LRWORKMIN, LRWORKOPT */
551 /*       DOUBLE PRECISION   RWORK(*) */
552 /*       DOUBLE PRECISION   THETA(*) */
553 /*       COMPLEX*16         U1(LDU1,*), U2(LDU2,*), V1T(LDV1T,*), WORK(*), */
554 /*      $                   X11(LDX11,*), X21(LDX21,*) */
555 /*       INTEGER            IWORK(*) */
556
557
558 /* > \par Purpose: */
559 /*  ============= */
560 /* > */
561 /* >\verbatim */
562 /* > */
563 /* > ZUNCSD2BY1 computes the CS decomposition of an M-by-Q matrix X with */
564 /* > orthonormal columns that has been partitioned into a 2-by-1 block */
565 /* > structure: */
566 /* > */
567 /* >                                [  I1 0  0 ] */
568 /* >                                [  0  C  0 ] */
569 /* >          [ X11 ]   [ U1 |    ] [  0  0  0 ] */
570 /* >      X = [-----] = [---------] [----------] V1**T . */
571 /* >          [ X21 ]   [    | U2 ] [  0  0  0 ] */
572 /* >                                [  0  S  0 ] */
573 /* >                                [  0  0  I2] */
574 /* > */
575 /* > X11 is P-by-Q. The unitary matrices U1, U2, and V1 are P-by-P, */
576 /* > (M-P)-by-(M-P), and Q-by-Q, respectively. C and S are R-by-R */
577 /* > nonnegative diagonal matrices satisfying C^2 + S^2 = I, in which */
578 /* > R = MIN(P,M-P,Q,M-Q). I1 is a K1-by-K1 identity matrix and I2 is a */
579 /* > K2-by-K2 identity matrix, where K1 = MAX(Q+P-M,0), K2 = MAX(Q-P,0). */
580 /* > \endverbatim */
581
582 /*  Arguments: */
583 /*  ========== */
584
585 /* > \param[in] JOBU1 */
586 /* > \verbatim */
587 /* >          JOBU1 is CHARACTER */
588 /* >          = 'Y':      U1 is computed; */
589 /* >          otherwise:  U1 is not computed. */
590 /* > \endverbatim */
591 /* > */
592 /* > \param[in] JOBU2 */
593 /* > \verbatim */
594 /* >          JOBU2 is CHARACTER */
595 /* >          = 'Y':      U2 is computed; */
596 /* >          otherwise:  U2 is not computed. */
597 /* > \endverbatim */
598 /* > */
599 /* > \param[in] JOBV1T */
600 /* > \verbatim */
601 /* >          JOBV1T is CHARACTER */
602 /* >          = 'Y':      V1T is computed; */
603 /* >          otherwise:  V1T is not computed. */
604 /* > \endverbatim */
605 /* > */
606 /* > \param[in] M */
607 /* > \verbatim */
608 /* >          M is INTEGER */
609 /* >          The number of rows in X. */
610 /* > \endverbatim */
611 /* > */
612 /* > \param[in] P */
613 /* > \verbatim */
614 /* >          P is INTEGER */
615 /* >          The number of rows in X11. 0 <= P <= M. */
616 /* > \endverbatim */
617 /* > */
618 /* > \param[in] Q */
619 /* > \verbatim */
620 /* >          Q is INTEGER */
621 /* >          The number of columns in X11 and X21. 0 <= Q <= M. */
622 /* > \endverbatim */
623 /* > */
624 /* > \param[in,out] X11 */
625 /* > \verbatim */
626 /* >          X11 is COMPLEX*16 array, dimension (LDX11,Q) */
627 /* >          On entry, part of the unitary matrix whose CSD is desired. */
628 /* > \endverbatim */
629 /* > */
630 /* > \param[in] LDX11 */
631 /* > \verbatim */
632 /* >          LDX11 is INTEGER */
633 /* >          The leading dimension of X11. LDX11 >= MAX(1,P). */
634 /* > \endverbatim */
635 /* > */
636 /* > \param[in,out] X21 */
637 /* > \verbatim */
638 /* >          X21 is COMPLEX*16 array, dimension (LDX21,Q) */
639 /* >          On entry, part of the unitary matrix whose CSD is desired. */
640 /* > \endverbatim */
641 /* > */
642 /* > \param[in] LDX21 */
643 /* > \verbatim */
644 /* >          LDX21 is INTEGER */
645 /* >          The leading dimension of X21. LDX21 >= MAX(1,M-P). */
646 /* > \endverbatim */
647 /* > */
648 /* > \param[out] THETA */
649 /* > \verbatim */
650 /* >          THETA is DOUBLE PRECISION array, dimension (R), in which R = */
651 /* >          MIN(P,M-P,Q,M-Q). */
652 /* >          C = DIAG( COS(THETA(1)), ... , COS(THETA(R)) ) and */
653 /* >          S = DIAG( SIN(THETA(1)), ... , SIN(THETA(R)) ). */
654 /* > \endverbatim */
655 /* > */
656 /* > \param[out] U1 */
657 /* > \verbatim */
658 /* >          U1 is COMPLEX*16 array, dimension (P) */
659 /* >          If JOBU1 = 'Y', U1 contains the P-by-P unitary matrix U1. */
660 /* > \endverbatim */
661 /* > */
662 /* > \param[in] LDU1 */
663 /* > \verbatim */
664 /* >          LDU1 is INTEGER */
665 /* >          The leading dimension of U1. If JOBU1 = 'Y', LDU1 >= */
666 /* >          MAX(1,P). */
667 /* > \endverbatim */
668 /* > */
669 /* > \param[out] U2 */
670 /* > \verbatim */
671 /* >          U2 is COMPLEX*16 array, dimension (M-P) */
672 /* >          If JOBU2 = 'Y', U2 contains the (M-P)-by-(M-P) unitary */
673 /* >          matrix U2. */
674 /* > \endverbatim */
675 /* > */
676 /* > \param[in] LDU2 */
677 /* > \verbatim */
678 /* >          LDU2 is INTEGER */
679 /* >          The leading dimension of U2. If JOBU2 = 'Y', LDU2 >= */
680 /* >          MAX(1,M-P). */
681 /* > \endverbatim */
682 /* > */
683 /* > \param[out] V1T */
684 /* > \verbatim */
685 /* >          V1T is COMPLEX*16 array, dimension (Q) */
686 /* >          If JOBV1T = 'Y', V1T contains the Q-by-Q matrix unitary */
687 /* >          matrix V1**T. */
688 /* > \endverbatim */
689 /* > */
690 /* > \param[in] LDV1T */
691 /* > \verbatim */
692 /* >          LDV1T is INTEGER */
693 /* >          The leading dimension of V1T. If JOBV1T = 'Y', LDV1T >= */
694 /* >          MAX(1,Q). */
695 /* > \endverbatim */
696 /* > */
697 /* > \param[out] WORK */
698 /* > \verbatim */
699 /* >          WORK is COMPLEX*16 array, dimension (MAX(1,LWORK)) */
700 /* >          On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
701 /* > \endverbatim */
702 /* > */
703 /* > \param[in] LWORK */
704 /* > \verbatim */
705 /* >          LWORK is INTEGER */
706 /* >          The dimension of the array WORK. */
707 /* > */
708 /* >          If LWORK = -1, then a workspace query is assumed; the routine */
709 /* >          only calculates the optimal size of the WORK array, returns */
710 /* >          this value as the first entry of the work array, and no error */
711 /* >          message related to LWORK is issued by XERBLA. */
712 /* > \endverbatim */
713 /* > */
714 /* > \param[out] RWORK */
715 /* > \verbatim */
716 /* >          RWORK is DOUBLE PRECISION array, dimension (MAX(1,LRWORK)) */
717 /* >          On exit, if INFO = 0, RWORK(1) returns the optimal LRWORK. */
718 /* >          If INFO > 0 on exit, RWORK(2:R) contains the values PHI(1), */
719 /* >          ..., PHI(R-1) that, together with THETA(1), ..., THETA(R), */
720 /* >          define the matrix in intermediate bidiagonal-block form */
721 /* >          remaining after nonconvergence. INFO specifies the number */
722 /* >          of nonzero PHI's. */
723 /* > \endverbatim */
724 /* > */
725 /* > \param[in] LRWORK */
726 /* > \verbatim */
727 /* >          LRWORK is INTEGER */
728 /* >          The dimension of the array RWORK. */
729 /* > */
730 /* >          If LRWORK = -1, then a workspace query is assumed; the routine */
731 /* >          only calculates the optimal size of the RWORK array, returns */
732 /* >          this value as the first entry of the work array, and no error */
733 /* >          message related to LRWORK is issued by XERBLA. */
734 /* > \endverbatim */
735
736 /* > \param[out] IWORK */
737 /* > \verbatim */
738 /* >          IWORK is INTEGER array, dimension (M-MIN(P,M-P,Q,M-Q)) */
739 /* > \endverbatim */
740 /* > */
741 /* > \param[out] INFO */
742 /* > \verbatim */
743 /* >          INFO is INTEGER */
744 /* >          = 0:  successful exit. */
745 /* >          < 0:  if INFO = -i, the i-th argument had an illegal value. */
746 /* >          > 0:  ZBBCSD did not converge. See the description of WORK */
747 /* >                above for details. */
748 /* > \endverbatim */
749
750 /* > \par References: */
751 /*  ================ */
752 /* > */
753 /* >  [1] Brian D. Sutton. Computing the complete CS decomposition. Numer. */
754 /* >      Algorithms, 50(1):33-65, 2009. */
755
756 /*  Authors: */
757 /*  ======== */
758
759 /* > \author Univ. of Tennessee */
760 /* > \author Univ. of California Berkeley */
761 /* > \author Univ. of Colorado Denver */
762 /* > \author NAG Ltd. */
763
764 /* > \date July 2012 */
765
766 /* > \ingroup complex16OTHERcomputational */
767
768 /*  ===================================================================== */
769 /* Subroutine */ int zuncsd2by1_(char *jobu1, char *jobu2, char *jobv1t, 
770         integer *m, integer *p, integer *q, doublecomplex *x11, integer *
771         ldx11, doublecomplex *x21, integer *ldx21, doublereal *theta, 
772         doublecomplex *u1, integer *ldu1, doublecomplex *u2, integer *ldu2, 
773         doublecomplex *v1t, integer *ldv1t, doublecomplex *work, integer *
774         lwork, doublereal *rwork, integer *lrwork, integer *iwork, integer *
775         info)
776 {
777     /* System generated locals */
778     integer u1_dim1, u1_offset, u2_dim1, u2_offset, v1t_dim1, v1t_offset, 
779             x11_dim1, x11_offset, x21_dim1, x21_offset, i__1, i__2, i__3;
780
781     /* Local variables */
782     integer ib11d, ib11e, ib12d, ib12e, ib21d, ib21e, ib22d, ib22e;
783     doublecomplex cdum[1]       /* was [1][1] */;
784     integer iphi, lworkmin, lworkopt, i__, j, r__;
785     extern logical lsame_(char *, char *);
786     integer childinfo;
787     extern /* Subroutine */ int zcopy_(integer *, doublecomplex *, integer *, 
788             doublecomplex *, integer *);
789     integer lorglqmin, lorgqrmin, lorglqopt, lrworkmin, itaup1, itaup2, 
790             itauq1, lorgqropt;
791     logical wantu1, wantu2;
792     integer lrworkopt, ibbcsd, lbbcsd, iorbdb, lorbdb;
793     extern /* Subroutine */ int zbbcsd_(char *, char *, char *, char *, char *
794             , integer *, integer *, integer *, doublereal *, doublereal *, 
795             doublecomplex *, integer *, doublecomplex *, integer *, 
796             doublecomplex *, integer *, doublecomplex *, integer *, 
797             doublereal *, doublereal *, doublereal *, doublereal *, 
798             doublereal *, doublereal *, doublereal *, doublereal *, 
799             doublereal *, integer *, integer *), xerbla_(char *, integer *, ftnlen);
800     integer iorglq, lorglq;
801     extern /* Subroutine */ int zlacpy_(char *, integer *, integer *, 
802             doublecomplex *, integer *, doublecomplex *, integer *);
803     integer iorgqr;
804     extern /* Subroutine */ int zlapmr_(logical *, integer *, integer *, 
805             doublecomplex *, integer *, integer *);
806     integer lorgqr;
807     extern /* Subroutine */ int zlapmt_(logical *, integer *, integer *, 
808             doublecomplex *, integer *, integer *);
809     logical lquery;
810     extern /* Subroutine */ int zunglq_(integer *, integer *, integer *, 
811             doublecomplex *, integer *, doublecomplex *, doublecomplex *, 
812             integer *, integer *), zungqr_(integer *, integer *, integer *, 
813             doublecomplex *, integer *, doublecomplex *, doublecomplex *, 
814             integer *, integer *), zunbdb1_(integer *, integer *, integer *, 
815             doublecomplex *, integer *, doublecomplex *, integer *, 
816             doublereal *, doublereal *, doublecomplex *, doublecomplex *, 
817             doublecomplex *, doublecomplex *, integer *, integer *), zunbdb2_(
818             integer *, integer *, integer *, doublecomplex *, integer *, 
819             doublecomplex *, integer *, doublereal *, doublereal *, 
820             doublecomplex *, doublecomplex *, doublecomplex *, doublecomplex *
821             , integer *, integer *), zunbdb3_(integer *, integer *, integer *,
822              doublecomplex *, integer *, doublecomplex *, integer *, 
823             doublereal *, doublereal *, doublecomplex *, doublecomplex *, 
824             doublecomplex *, doublecomplex *, integer *, integer *), zunbdb4_(
825             integer *, integer *, integer *, doublecomplex *, integer *, 
826             doublecomplex *, integer *, doublereal *, doublereal *, 
827             doublecomplex *, doublecomplex *, doublecomplex *, doublecomplex *
828             , doublecomplex *, integer *, integer *);
829     logical wantv1t;
830     doublereal dum[1];
831
832
833 /*  -- LAPACK computational routine (version 3.7.1) -- */
834 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
835 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
836 /*     July 2012 */
837
838
839 /*  ===================================================================== */
840
841
842 /*     Test input arguments */
843
844     /* Parameter adjustments */
845     x11_dim1 = *ldx11;
846     x11_offset = 1 + x11_dim1 * 1;
847     x11 -= x11_offset;
848     x21_dim1 = *ldx21;
849     x21_offset = 1 + x21_dim1 * 1;
850     x21 -= x21_offset;
851     --theta;
852     u1_dim1 = *ldu1;
853     u1_offset = 1 + u1_dim1 * 1;
854     u1 -= u1_offset;
855     u2_dim1 = *ldu2;
856     u2_offset = 1 + u2_dim1 * 1;
857     u2 -= u2_offset;
858     v1t_dim1 = *ldv1t;
859     v1t_offset = 1 + v1t_dim1 * 1;
860     v1t -= v1t_offset;
861     --work;
862     --rwork;
863     --iwork;
864
865     /* Function Body */
866     *info = 0;
867     wantu1 = lsame_(jobu1, "Y");
868     wantu2 = lsame_(jobu2, "Y");
869     wantv1t = lsame_(jobv1t, "Y");
870     lquery = *lwork == -1;
871
872     if (*m < 0) {
873         *info = -4;
874     } else if (*p < 0 || *p > *m) {
875         *info = -5;
876     } else if (*q < 0 || *q > *m) {
877         *info = -6;
878     } else if (*ldx11 < f2cmax(1,*p)) {
879         *info = -8;
880     } else /* if(complicated condition) */ {
881 /* Computing MAX */
882         i__1 = 1, i__2 = *m - *p;
883         if (*ldx21 < f2cmax(i__1,i__2)) {
884             *info = -10;
885         } else if (wantu1 && *ldu1 < f2cmax(1,*p)) {
886             *info = -13;
887         } else /* if(complicated condition) */ {
888 /* Computing MAX */
889             i__1 = 1, i__2 = *m - *p;
890             if (wantu2 && *ldu2 < f2cmax(i__1,i__2)) {
891                 *info = -15;
892             } else if (wantv1t && *ldv1t < f2cmax(1,*q)) {
893                 *info = -17;
894             }
895         }
896     }
897
898 /* Computing MIN */
899     i__1 = *p, i__2 = *m - *p, i__1 = f2cmin(i__1,i__2), i__1 = f2cmin(i__1,*q), 
900             i__2 = *m - *q;
901     r__ = f2cmin(i__1,i__2);
902
903 /*     Compute workspace */
904
905 /*       WORK layout: */
906 /*     |-----------------------------------------| */
907 /*     | LWORKOPT (1)                            | */
908 /*     |-----------------------------------------| */
909 /*     | TAUP1 (MAX(1,P))                        | */
910 /*     | TAUP2 (MAX(1,M-P))                      | */
911 /*     | TAUQ1 (MAX(1,Q))                        | */
912 /*     |-----------------------------------------| */
913 /*     | ZUNBDB WORK | ZUNGQR WORK | ZUNGLQ WORK | */
914 /*     |             |             |             | */
915 /*     |             |             |             | */
916 /*     |             |             |             | */
917 /*     |             |             |             | */
918 /*     |-----------------------------------------| */
919 /*       RWORK layout: */
920 /*     |------------------| */
921 /*     | LRWORKOPT (1)    | */
922 /*     |------------------| */
923 /*     | PHI (MAX(1,R-1)) | */
924 /*     |------------------| */
925 /*     | B11D (R)         | */
926 /*     | B11E (R-1)       | */
927 /*     | B12D (R)         | */
928 /*     | B12E (R-1)       | */
929 /*     | B21D (R)         | */
930 /*     | B21E (R-1)       | */
931 /*     | B22D (R)         | */
932 /*     | B22E (R-1)       | */
933 /*     | ZBBCSD RWORK     | */
934 /*     |------------------| */
935
936     if (*info == 0) {
937         iphi = 2;
938 /* Computing MAX */
939         i__1 = 1, i__2 = r__ - 1;
940         ib11d = iphi + f2cmax(i__1,i__2);
941         ib11e = ib11d + f2cmax(1,r__);
942 /* Computing MAX */
943         i__1 = 1, i__2 = r__ - 1;
944         ib12d = ib11e + f2cmax(i__1,i__2);
945         ib12e = ib12d + f2cmax(1,r__);
946 /* Computing MAX */
947         i__1 = 1, i__2 = r__ - 1;
948         ib21d = ib12e + f2cmax(i__1,i__2);
949         ib21e = ib21d + f2cmax(1,r__);
950 /* Computing MAX */
951         i__1 = 1, i__2 = r__ - 1;
952         ib22d = ib21e + f2cmax(i__1,i__2);
953         ib22e = ib22d + f2cmax(1,r__);
954 /* Computing MAX */
955         i__1 = 1, i__2 = r__ - 1;
956         ibbcsd = ib22e + f2cmax(i__1,i__2);
957         itaup1 = 2;
958         itaup2 = itaup1 + f2cmax(1,*p);
959 /* Computing MAX */
960         i__1 = 1, i__2 = *m - *p;
961         itauq1 = itaup2 + f2cmax(i__1,i__2);
962         iorbdb = itauq1 + f2cmax(1,*q);
963         iorgqr = itauq1 + f2cmax(1,*q);
964         iorglq = itauq1 + f2cmax(1,*q);
965         lorgqrmin = 1;
966         lorgqropt = 1;
967         lorglqmin = 1;
968         lorglqopt = 1;
969         if (r__ == *q) {
970             zunbdb1_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], 
971                     ldx21, &theta[1], dum, cdum, cdum, cdum, &work[1], &c_n1, 
972                     &childinfo);
973             lorbdb = (integer) work[1].r;
974             if (wantu1 && *p > 0) {
975                 zungqr_(p, p, q, &u1[u1_offset], ldu1, cdum, &work[1], &c_n1, 
976                         &childinfo);
977                 lorgqrmin = f2cmax(lorgqrmin,*p);
978 /* Computing MAX */
979                 i__1 = lorgqropt, i__2 = (integer) work[1].r;
980                 lorgqropt = f2cmax(i__1,i__2);
981             }
982             if (wantu2 && *m - *p > 0) {
983                 i__1 = *m - *p;
984                 i__2 = *m - *p;
985                 zungqr_(&i__1, &i__2, q, &u2[u2_offset], ldu2, cdum, &work[1],
986                          &c_n1, &childinfo);
987 /* Computing MAX */
988                 i__1 = lorgqrmin, i__2 = *m - *p;
989                 lorgqrmin = f2cmax(i__1,i__2);
990 /* Computing MAX */
991                 i__1 = lorgqropt, i__2 = (integer) work[1].r;
992                 lorgqropt = f2cmax(i__1,i__2);
993             }
994             if (wantv1t && *q > 0) {
995                 i__1 = *q - 1;
996                 i__2 = *q - 1;
997                 i__3 = *q - 1;
998                 zunglq_(&i__1, &i__2, &i__3, &v1t[v1t_offset], ldv1t, cdum, &
999                         work[1], &c_n1, &childinfo);
1000 /* Computing MAX */
1001                 i__1 = lorglqmin, i__2 = *q - 1;
1002                 lorglqmin = f2cmax(i__1,i__2);
1003 /* Computing MAX */
1004                 i__1 = lorglqopt, i__2 = (integer) work[1].r;
1005                 lorglqopt = f2cmax(i__1,i__2);
1006             }
1007             zbbcsd_(jobu1, jobu2, jobv1t, "N", "N", m, p, q, &theta[1], dum, &
1008                     u1[u1_offset], ldu1, &u2[u2_offset], ldu2, &v1t[
1009                     v1t_offset], ldv1t, cdum, &c__1, dum, dum, dum, dum, dum, 
1010                     dum, dum, dum, &rwork[1], &c_n1, &childinfo);
1011             lbbcsd = (integer) rwork[1];
1012         } else if (r__ == *p) {
1013             zunbdb2_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], 
1014                     ldx21, &theta[1], dum, cdum, cdum, cdum, &work[1], &c_n1, 
1015                     &childinfo);
1016             lorbdb = (integer) work[1].r;
1017             if (wantu1 && *p > 0) {
1018                 i__1 = *p - 1;
1019                 i__2 = *p - 1;
1020                 i__3 = *p - 1;
1021                 zungqr_(&i__1, &i__2, &i__3, &u1[(u1_dim1 << 1) + 2], ldu1, 
1022                         cdum, &work[1], &c_n1, &childinfo);
1023 /* Computing MAX */
1024                 i__1 = lorgqrmin, i__2 = *p - 1;
1025                 lorgqrmin = f2cmax(i__1,i__2);
1026 /* Computing MAX */
1027                 i__1 = lorgqropt, i__2 = (integer) work[1].r;
1028                 lorgqropt = f2cmax(i__1,i__2);
1029             }
1030             if (wantu2 && *m - *p > 0) {
1031                 i__1 = *m - *p;
1032                 i__2 = *m - *p;
1033                 zungqr_(&i__1, &i__2, q, &u2[u2_offset], ldu2, cdum, &work[1],
1034                          &c_n1, &childinfo);
1035 /* Computing MAX */
1036                 i__1 = lorgqrmin, i__2 = *m - *p;
1037                 lorgqrmin = f2cmax(i__1,i__2);
1038 /* Computing MAX */
1039                 i__1 = lorgqropt, i__2 = (integer) work[1].r;
1040                 lorgqropt = f2cmax(i__1,i__2);
1041             }
1042             if (wantv1t && *q > 0) {
1043                 zunglq_(q, q, &r__, &v1t[v1t_offset], ldv1t, cdum, &work[1], &
1044                         c_n1, &childinfo);
1045                 lorglqmin = f2cmax(lorglqmin,*q);
1046 /* Computing MAX */
1047                 i__1 = lorglqopt, i__2 = (integer) work[1].r;
1048                 lorglqopt = f2cmax(i__1,i__2);
1049             }
1050             zbbcsd_(jobv1t, "N", jobu1, jobu2, "T", m, q, p, &theta[1], dum, &
1051                     v1t[v1t_offset], ldv1t, cdum, &c__1, &u1[u1_offset], ldu1,
1052                      &u2[u2_offset], ldu2, dum, dum, dum, dum, dum, dum, dum, 
1053                     dum, &rwork[1], &c_n1, &childinfo);
1054             lbbcsd = (integer) rwork[1];
1055         } else if (r__ == *m - *p) {
1056             zunbdb3_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], 
1057                     ldx21, &theta[1], dum, cdum, cdum, cdum, &work[1], &c_n1, 
1058                     &childinfo);
1059             lorbdb = (integer) work[1].r;
1060             if (wantu1 && *p > 0) {
1061                 zungqr_(p, p, q, &u1[u1_offset], ldu1, cdum, &work[1], &c_n1, 
1062                         &childinfo);
1063                 lorgqrmin = f2cmax(lorgqrmin,*p);
1064 /* Computing MAX */
1065                 i__1 = lorgqropt, i__2 = (integer) work[1].r;
1066                 lorgqropt = f2cmax(i__1,i__2);
1067             }
1068             if (wantu2 && *m - *p > 0) {
1069                 i__1 = *m - *p - 1;
1070                 i__2 = *m - *p - 1;
1071                 i__3 = *m - *p - 1;
1072                 zungqr_(&i__1, &i__2, &i__3, &u2[(u2_dim1 << 1) + 2], ldu2, 
1073                         cdum, &work[1], &c_n1, &childinfo);
1074 /* Computing MAX */
1075                 i__1 = lorgqrmin, i__2 = *m - *p - 1;
1076                 lorgqrmin = f2cmax(i__1,i__2);
1077 /* Computing MAX */
1078                 i__1 = lorgqropt, i__2 = (integer) work[1].r;
1079                 lorgqropt = f2cmax(i__1,i__2);
1080             }
1081             if (wantv1t && *q > 0) {
1082                 zunglq_(q, q, &r__, &v1t[v1t_offset], ldv1t, cdum, &work[1], &
1083                         c_n1, &childinfo);
1084                 lorglqmin = f2cmax(lorglqmin,*q);
1085 /* Computing MAX */
1086                 i__1 = lorglqopt, i__2 = (integer) work[1].r;
1087                 lorglqopt = f2cmax(i__1,i__2);
1088             }
1089             i__1 = *m - *q;
1090             i__2 = *m - *p;
1091             zbbcsd_("N", jobv1t, jobu2, jobu1, "T", m, &i__1, &i__2, &theta[1]
1092                     , dum, cdum, &c__1, &v1t[v1t_offset], ldv1t, &u2[
1093                     u2_offset], ldu2, &u1[u1_offset], ldu1, dum, dum, dum, 
1094                     dum, dum, dum, dum, dum, &rwork[1], &c_n1, &childinfo);
1095             lbbcsd = (integer) rwork[1];
1096         } else {
1097             zunbdb4_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], 
1098                     ldx21, &theta[1], dum, cdum, cdum, cdum, cdum, &work[1], &
1099                     c_n1, &childinfo);
1100             lorbdb = *m + (integer) work[1].r;
1101             if (wantu1 && *p > 0) {
1102                 i__1 = *m - *q;
1103                 zungqr_(p, p, &i__1, &u1[u1_offset], ldu1, cdum, &work[1], &
1104                         c_n1, &childinfo);
1105                 lorgqrmin = f2cmax(lorgqrmin,*p);
1106 /* Computing MAX */
1107                 i__1 = lorgqropt, i__2 = (integer) work[1].r;
1108                 lorgqropt = f2cmax(i__1,i__2);
1109             }
1110             if (wantu2 && *m - *p > 0) {
1111                 i__1 = *m - *p;
1112                 i__2 = *m - *p;
1113                 i__3 = *m - *q;
1114                 zungqr_(&i__1, &i__2, &i__3, &u2[u2_offset], ldu2, cdum, &
1115                         work[1], &c_n1, &childinfo);
1116 /* Computing MAX */
1117                 i__1 = lorgqrmin, i__2 = *m - *p;
1118                 lorgqrmin = f2cmax(i__1,i__2);
1119 /* Computing MAX */
1120                 i__1 = lorgqropt, i__2 = (integer) work[1].r;
1121                 lorgqropt = f2cmax(i__1,i__2);
1122             }
1123             if (wantv1t && *q > 0) {
1124                 zunglq_(q, q, q, &v1t[v1t_offset], ldv1t, cdum, &work[1], &
1125                         c_n1, &childinfo);
1126                 lorglqmin = f2cmax(lorglqmin,*q);
1127 /* Computing MAX */
1128                 i__1 = lorglqopt, i__2 = (integer) work[1].r;
1129                 lorglqopt = f2cmax(i__1,i__2);
1130             }
1131             i__1 = *m - *p;
1132             i__2 = *m - *q;
1133             zbbcsd_(jobu2, jobu1, "N", jobv1t, "N", m, &i__1, &i__2, &theta[1]
1134                     , dum, &u2[u2_offset], ldu2, &u1[u1_offset], ldu1, cdum, &
1135                     c__1, &v1t[v1t_offset], ldv1t, dum, dum, dum, dum, dum, 
1136                     dum, dum, dum, &rwork[1], &c_n1, &childinfo);
1137             lbbcsd = (integer) rwork[1];
1138         }
1139         lrworkmin = ibbcsd + lbbcsd - 1;
1140         lrworkopt = lrworkmin;
1141         rwork[1] = (doublereal) lrworkopt;
1142 /* Computing MAX */
1143         i__1 = iorbdb + lorbdb - 1, i__2 = iorgqr + lorgqrmin - 1, i__1 = f2cmax(
1144                 i__1,i__2), i__2 = iorglq + lorglqmin - 1;
1145         lworkmin = f2cmax(i__1,i__2);
1146 /* Computing MAX */
1147         i__1 = iorbdb + lorbdb - 1, i__2 = iorgqr + lorgqropt - 1, i__1 = f2cmax(
1148                 i__1,i__2), i__2 = iorglq + lorglqopt - 1;
1149         lworkopt = f2cmax(i__1,i__2);
1150         work[1].r = (doublereal) lworkopt, work[1].i = 0.;
1151         if (*lwork < lworkmin && ! lquery) {
1152             *info = -19;
1153         }
1154     }
1155     if (*info != 0) {
1156         i__1 = -(*info);
1157         xerbla_("ZUNCSD2BY1", &i__1, (ftnlen)10);
1158         return 0;
1159     } else if (lquery) {
1160         return 0;
1161     }
1162     lorgqr = *lwork - iorgqr + 1;
1163     lorglq = *lwork - iorglq + 1;
1164
1165 /*     Handle four cases separately: R = Q, R = P, R = M-P, and R = M-Q, */
1166 /*     in which R = MIN(P,M-P,Q,M-Q) */
1167
1168     if (r__ == *q) {
1169
1170 /*        Case 1: R = Q */
1171
1172 /*        Simultaneously bidiagonalize X11 and X21 */
1173
1174         zunbdb1_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], ldx21, &
1175                 theta[1], &rwork[iphi], &work[itaup1], &work[itaup2], &work[
1176                 itauq1], &work[iorbdb], &lorbdb, &childinfo);
1177
1178 /*        Accumulate Householder reflectors */
1179
1180         if (wantu1 && *p > 0) {
1181             zlacpy_("L", p, q, &x11[x11_offset], ldx11, &u1[u1_offset], ldu1);
1182             zungqr_(p, p, q, &u1[u1_offset], ldu1, &work[itaup1], &work[
1183                     iorgqr], &lorgqr, &childinfo);
1184         }
1185         if (wantu2 && *m - *p > 0) {
1186             i__1 = *m - *p;
1187             zlacpy_("L", &i__1, q, &x21[x21_offset], ldx21, &u2[u2_offset], 
1188                     ldu2);
1189             i__1 = *m - *p;
1190             i__2 = *m - *p;
1191             zungqr_(&i__1, &i__2, q, &u2[u2_offset], ldu2, &work[itaup2], &
1192                     work[iorgqr], &lorgqr, &childinfo);
1193         }
1194         if (wantv1t && *q > 0) {
1195             i__1 = v1t_dim1 + 1;
1196             v1t[i__1].r = 1., v1t[i__1].i = 0.;
1197             i__1 = *q;
1198             for (j = 2; j <= i__1; ++j) {
1199                 i__2 = j * v1t_dim1 + 1;
1200                 v1t[i__2].r = 0., v1t[i__2].i = 0.;
1201                 i__2 = j + v1t_dim1;
1202                 v1t[i__2].r = 0., v1t[i__2].i = 0.;
1203             }
1204             i__1 = *q - 1;
1205             i__2 = *q - 1;
1206             zlacpy_("U", &i__1, &i__2, &x21[(x21_dim1 << 1) + 1], ldx21, &v1t[
1207                     (v1t_dim1 << 1) + 2], ldv1t);
1208             i__1 = *q - 1;
1209             i__2 = *q - 1;
1210             i__3 = *q - 1;
1211             zunglq_(&i__1, &i__2, &i__3, &v1t[(v1t_dim1 << 1) + 2], ldv1t, &
1212                     work[itauq1], &work[iorglq], &lorglq, &childinfo);
1213         }
1214
1215 /*        Simultaneously diagonalize X11 and X21. */
1216
1217         zbbcsd_(jobu1, jobu2, jobv1t, "N", "N", m, p, q, &theta[1], &rwork[
1218                 iphi], &u1[u1_offset], ldu1, &u2[u2_offset], ldu2, &v1t[
1219                 v1t_offset], ldv1t, cdum, &c__1, &rwork[ib11d], &rwork[ib11e],
1220                  &rwork[ib12d], &rwork[ib12e], &rwork[ib21d], &rwork[ib21e], &
1221                 rwork[ib22d], &rwork[ib22e], &rwork[ibbcsd], &lbbcsd, &
1222                 childinfo);
1223
1224 /*        Permute rows and columns to place zero submatrices in */
1225 /*        preferred positions */
1226
1227         if (*q > 0 && wantu2) {
1228             i__1 = *q;
1229             for (i__ = 1; i__ <= i__1; ++i__) {
1230                 iwork[i__] = *m - *p - *q + i__;
1231             }
1232             i__1 = *m - *p;
1233             for (i__ = *q + 1; i__ <= i__1; ++i__) {
1234                 iwork[i__] = i__ - *q;
1235             }
1236             i__1 = *m - *p;
1237             i__2 = *m - *p;
1238             zlapmt_(&c_false, &i__1, &i__2, &u2[u2_offset], ldu2, &iwork[1]);
1239         }
1240     } else if (r__ == *p) {
1241
1242 /*        Case 2: R = P */
1243
1244 /*        Simultaneously bidiagonalize X11 and X21 */
1245
1246         zunbdb2_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], ldx21, &
1247                 theta[1], &rwork[iphi], &work[itaup1], &work[itaup2], &work[
1248                 itauq1], &work[iorbdb], &lorbdb, &childinfo);
1249
1250 /*        Accumulate Householder reflectors */
1251
1252         if (wantu1 && *p > 0) {
1253             i__1 = u1_dim1 + 1;
1254             u1[i__1].r = 1., u1[i__1].i = 0.;
1255             i__1 = *p;
1256             for (j = 2; j <= i__1; ++j) {
1257                 i__2 = j * u1_dim1 + 1;
1258                 u1[i__2].r = 0., u1[i__2].i = 0.;
1259                 i__2 = j + u1_dim1;
1260                 u1[i__2].r = 0., u1[i__2].i = 0.;
1261             }
1262             i__1 = *p - 1;
1263             i__2 = *p - 1;
1264             zlacpy_("L", &i__1, &i__2, &x11[x11_dim1 + 2], ldx11, &u1[(
1265                     u1_dim1 << 1) + 2], ldu1);
1266             i__1 = *p - 1;
1267             i__2 = *p - 1;
1268             i__3 = *p - 1;
1269             zungqr_(&i__1, &i__2, &i__3, &u1[(u1_dim1 << 1) + 2], ldu1, &work[
1270                     itaup1], &work[iorgqr], &lorgqr, &childinfo);
1271         }
1272         if (wantu2 && *m - *p > 0) {
1273             i__1 = *m - *p;
1274             zlacpy_("L", &i__1, q, &x21[x21_offset], ldx21, &u2[u2_offset], 
1275                     ldu2);
1276             i__1 = *m - *p;
1277             i__2 = *m - *p;
1278             zungqr_(&i__1, &i__2, q, &u2[u2_offset], ldu2, &work[itaup2], &
1279                     work[iorgqr], &lorgqr, &childinfo);
1280         }
1281         if (wantv1t && *q > 0) {
1282             zlacpy_("U", p, q, &x11[x11_offset], ldx11, &v1t[v1t_offset], 
1283                     ldv1t);
1284             zunglq_(q, q, &r__, &v1t[v1t_offset], ldv1t, &work[itauq1], &work[
1285                     iorglq], &lorglq, &childinfo);
1286         }
1287
1288 /*        Simultaneously diagonalize X11 and X21. */
1289
1290         zbbcsd_(jobv1t, "N", jobu1, jobu2, "T", m, q, p, &theta[1], &rwork[
1291                 iphi], &v1t[v1t_offset], ldv1t, cdum, &c__1, &u1[u1_offset], 
1292                 ldu1, &u2[u2_offset], ldu2, &rwork[ib11d], &rwork[ib11e], &
1293                 rwork[ib12d], &rwork[ib12e], &rwork[ib21d], &rwork[ib21e], &
1294                 rwork[ib22d], &rwork[ib22e], &rwork[ibbcsd], &lbbcsd, &
1295                 childinfo);
1296
1297 /*        Permute rows and columns to place identity submatrices in */
1298 /*        preferred positions */
1299
1300         if (*q > 0 && wantu2) {
1301             i__1 = *q;
1302             for (i__ = 1; i__ <= i__1; ++i__) {
1303                 iwork[i__] = *m - *p - *q + i__;
1304             }
1305             i__1 = *m - *p;
1306             for (i__ = *q + 1; i__ <= i__1; ++i__) {
1307                 iwork[i__] = i__ - *q;
1308             }
1309             i__1 = *m - *p;
1310             i__2 = *m - *p;
1311             zlapmt_(&c_false, &i__1, &i__2, &u2[u2_offset], ldu2, &iwork[1]);
1312         }
1313     } else if (r__ == *m - *p) {
1314
1315 /*        Case 3: R = M-P */
1316
1317 /*        Simultaneously bidiagonalize X11 and X21 */
1318
1319         zunbdb3_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], ldx21, &
1320                 theta[1], &rwork[iphi], &work[itaup1], &work[itaup2], &work[
1321                 itauq1], &work[iorbdb], &lorbdb, &childinfo);
1322
1323 /*        Accumulate Householder reflectors */
1324
1325         if (wantu1 && *p > 0) {
1326             zlacpy_("L", p, q, &x11[x11_offset], ldx11, &u1[u1_offset], ldu1);
1327             zungqr_(p, p, q, &u1[u1_offset], ldu1, &work[itaup1], &work[
1328                     iorgqr], &lorgqr, &childinfo);
1329         }
1330         if (wantu2 && *m - *p > 0) {
1331             i__1 = u2_dim1 + 1;
1332             u2[i__1].r = 1., u2[i__1].i = 0.;
1333             i__1 = *m - *p;
1334             for (j = 2; j <= i__1; ++j) {
1335                 i__2 = j * u2_dim1 + 1;
1336                 u2[i__2].r = 0., u2[i__2].i = 0.;
1337                 i__2 = j + u2_dim1;
1338                 u2[i__2].r = 0., u2[i__2].i = 0.;
1339             }
1340             i__1 = *m - *p - 1;
1341             i__2 = *m - *p - 1;
1342             zlacpy_("L", &i__1, &i__2, &x21[x21_dim1 + 2], ldx21, &u2[(
1343                     u2_dim1 << 1) + 2], ldu2);
1344             i__1 = *m - *p - 1;
1345             i__2 = *m - *p - 1;
1346             i__3 = *m - *p - 1;
1347             zungqr_(&i__1, &i__2, &i__3, &u2[(u2_dim1 << 1) + 2], ldu2, &work[
1348                     itaup2], &work[iorgqr], &lorgqr, &childinfo);
1349         }
1350         if (wantv1t && *q > 0) {
1351             i__1 = *m - *p;
1352             zlacpy_("U", &i__1, q, &x21[x21_offset], ldx21, &v1t[v1t_offset], 
1353                     ldv1t);
1354             zunglq_(q, q, &r__, &v1t[v1t_offset], ldv1t, &work[itauq1], &work[
1355                     iorglq], &lorglq, &childinfo);
1356         }
1357
1358 /*        Simultaneously diagonalize X11 and X21. */
1359
1360         i__1 = *m - *q;
1361         i__2 = *m - *p;
1362         zbbcsd_("N", jobv1t, jobu2, jobu1, "T", m, &i__1, &i__2, &theta[1], &
1363                 rwork[iphi], cdum, &c__1, &v1t[v1t_offset], ldv1t, &u2[
1364                 u2_offset], ldu2, &u1[u1_offset], ldu1, &rwork[ib11d], &rwork[
1365                 ib11e], &rwork[ib12d], &rwork[ib12e], &rwork[ib21d], &rwork[
1366                 ib21e], &rwork[ib22d], &rwork[ib22e], &rwork[ibbcsd], &lbbcsd,
1367                  &childinfo);
1368
1369 /*        Permute rows and columns to place identity submatrices in */
1370 /*        preferred positions */
1371
1372         if (*q > r__) {
1373             i__1 = r__;
1374             for (i__ = 1; i__ <= i__1; ++i__) {
1375                 iwork[i__] = *q - r__ + i__;
1376             }
1377             i__1 = *q;
1378             for (i__ = r__ + 1; i__ <= i__1; ++i__) {
1379                 iwork[i__] = i__ - r__;
1380             }
1381             if (wantu1) {
1382                 zlapmt_(&c_false, p, q, &u1[u1_offset], ldu1, &iwork[1]);
1383             }
1384             if (wantv1t) {
1385                 zlapmr_(&c_false, q, q, &v1t[v1t_offset], ldv1t, &iwork[1]);
1386             }
1387         }
1388     } else {
1389
1390 /*        Case 4: R = M-Q */
1391
1392 /*        Simultaneously bidiagonalize X11 and X21 */
1393
1394         i__1 = lorbdb - *m;
1395         zunbdb4_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], ldx21, &
1396                 theta[1], &rwork[iphi], &work[itaup1], &work[itaup2], &work[
1397                 itauq1], &work[iorbdb], &work[iorbdb + *m], &i__1, &childinfo)
1398                 ;
1399
1400 /*        Accumulate Householder reflectors */
1401
1402         if (wantu1 && *p > 0) {
1403             zcopy_(p, &work[iorbdb], &c__1, &u1[u1_offset], &c__1);
1404             i__1 = *p;
1405             for (j = 2; j <= i__1; ++j) {
1406                 i__2 = j * u1_dim1 + 1;
1407                 u1[i__2].r = 0., u1[i__2].i = 0.;
1408             }
1409             i__1 = *p - 1;
1410             i__2 = *m - *q - 1;
1411             zlacpy_("L", &i__1, &i__2, &x11[x11_dim1 + 2], ldx11, &u1[(
1412                     u1_dim1 << 1) + 2], ldu1);
1413             i__1 = *m - *q;
1414             zungqr_(p, p, &i__1, &u1[u1_offset], ldu1, &work[itaup1], &work[
1415                     iorgqr], &lorgqr, &childinfo);
1416         }
1417         if (wantu2 && *m - *p > 0) {
1418             i__1 = *m - *p;
1419             zcopy_(&i__1, &work[iorbdb + *p], &c__1, &u2[u2_offset], &c__1);
1420             i__1 = *m - *p;
1421             for (j = 2; j <= i__1; ++j) {
1422                 i__2 = j * u2_dim1 + 1;
1423                 u2[i__2].r = 0., u2[i__2].i = 0.;
1424             }
1425             i__1 = *m - *p - 1;
1426             i__2 = *m - *q - 1;
1427             zlacpy_("L", &i__1, &i__2, &x21[x21_dim1 + 2], ldx21, &u2[(
1428                     u2_dim1 << 1) + 2], ldu2);
1429             i__1 = *m - *p;
1430             i__2 = *m - *p;
1431             i__3 = *m - *q;
1432             zungqr_(&i__1, &i__2, &i__3, &u2[u2_offset], ldu2, &work[itaup2], 
1433                     &work[iorgqr], &lorgqr, &childinfo);
1434         }
1435         if (wantv1t && *q > 0) {
1436             i__1 = *m - *q;
1437             zlacpy_("U", &i__1, q, &x21[x21_offset], ldx21, &v1t[v1t_offset], 
1438                     ldv1t);
1439             i__1 = *p - (*m - *q);
1440             i__2 = *q - (*m - *q);
1441             zlacpy_("U", &i__1, &i__2, &x11[*m - *q + 1 + (*m - *q + 1) * 
1442                     x11_dim1], ldx11, &v1t[*m - *q + 1 + (*m - *q + 1) * 
1443                     v1t_dim1], ldv1t);
1444             i__1 = -(*p) + *q;
1445             i__2 = *q - *p;
1446             zlacpy_("U", &i__1, &i__2, &x21[*m - *q + 1 + (*p + 1) * x21_dim1]
1447                     , ldx21, &v1t[*p + 1 + (*p + 1) * v1t_dim1], ldv1t);
1448             zunglq_(q, q, q, &v1t[v1t_offset], ldv1t, &work[itauq1], &work[
1449                     iorglq], &lorglq, &childinfo);
1450         }
1451
1452 /*        Simultaneously diagonalize X11 and X21. */
1453
1454         i__1 = *m - *p;
1455         i__2 = *m - *q;
1456         zbbcsd_(jobu2, jobu1, "N", jobv1t, "N", m, &i__1, &i__2, &theta[1], &
1457                 rwork[iphi], &u2[u2_offset], ldu2, &u1[u1_offset], ldu1, cdum,
1458                  &c__1, &v1t[v1t_offset], ldv1t, &rwork[ib11d], &rwork[ib11e],
1459                  &rwork[ib12d], &rwork[ib12e], &rwork[ib21d], &rwork[ib21e], &
1460                 rwork[ib22d], &rwork[ib22e], &rwork[ibbcsd], &lbbcsd, &
1461                 childinfo);
1462
1463 /*        Permute rows and columns to place identity submatrices in */
1464 /*        preferred positions */
1465
1466         if (*p > r__) {
1467             i__1 = r__;
1468             for (i__ = 1; i__ <= i__1; ++i__) {
1469                 iwork[i__] = *p - r__ + i__;
1470             }
1471             i__1 = *p;
1472             for (i__ = r__ + 1; i__ <= i__1; ++i__) {
1473                 iwork[i__] = i__ - r__;
1474             }
1475             if (wantu1) {
1476                 zlapmt_(&c_false, p, p, &u1[u1_offset], ldu1, &iwork[1]);
1477             }
1478             if (wantv1t) {
1479                 zlapmr_(&c_false, p, q, &v1t[v1t_offset], ldv1t, &iwork[1]);
1480             }
1481         }
1482     }
1483
1484     return 0;
1485
1486 /*     End of ZUNCSD2BY1 */
1487
1488 } /* zuncsd2by1_ */
1489