C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / sorcsd2by1.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 integer c_n1 = -1;
516 static integer c__1 = 1;
517 static logical c_false = FALSE_;
518
519 /* > \brief \b SORCSD2BY1 */
520
521 /*  =========== DOCUMENTATION =========== */
522
523 /* Online html documentation available at */
524 /*            http://www.netlib.org/lapack/explore-html/ */
525
526 /* > \htmlonly */
527 /* > Download SORCSD2BY1 + dependencies */
528 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sorcsd2
529 by1.f"> */
530 /* > [TGZ]</a> */
531 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sorcsd2
532 by1.f"> */
533 /* > [ZIP]</a> */
534 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sorcsd2
535 by1.f"> */
536 /* > [TXT]</a> */
537 /* > \endhtmlonly */
538
539 /*  Definition: */
540 /*  =========== */
541
542 /*       SUBROUTINE SORCSD2BY1( JOBU1, JOBU2, JOBV1T, M, P, Q, X11, LDX11, */
543 /*                              X21, LDX21, THETA, U1, LDU1, U2, LDU2, V1T, */
544 /*                              LDV1T, WORK, LWORK, IWORK, INFO ) */
545
546 /*       CHARACTER          JOBU1, JOBU2, JOBV1T */
547 /*       INTEGER            INFO, LDU1, LDU2, LDV1T, LWORK, LDX11, LDX21, */
548 /*      $                   M, P, Q */
549 /*       REAL               THETA(*) */
550 /*       REAL               U1(LDU1,*), U2(LDU2,*), V1T(LDV1T,*), WORK(*), */
551 /*      $                   X11(LDX11,*), X21(LDX21,*) */
552 /*       INTEGER            IWORK(*) */
553
554
555 /* > \par Purpose: */
556 /*  ============= */
557 /* > */
558 /* >\verbatim */
559 /* > */
560 /* > SORCSD2BY1 computes the CS decomposition of an M-by-Q matrix X with */
561 /* > orthonormal columns that has been partitioned into a 2-by-1 block */
562 /* > structure: */
563 /* > */
564 /* >                                [  I1 0  0 ] */
565 /* >                                [  0  C  0 ] */
566 /* >          [ X11 ]   [ U1 |    ] [  0  0  0 ] */
567 /* >      X = [-----] = [---------] [----------] V1**T . */
568 /* >          [ X21 ]   [    | U2 ] [  0  0  0 ] */
569 /* >                                [  0  S  0 ] */
570 /* >                                [  0  0  I2] */
571 /* > */
572 /* > X11 is P-by-Q. The orthogonal matrices U1, U2, and V1 are P-by-P, */
573 /* > (M-P)-by-(M-P), and Q-by-Q, respectively. C and S are R-by-R */
574 /* > nonnegative diagonal matrices satisfying C^2 + S^2 = I, in which */
575 /* > R = MIN(P,M-P,Q,M-Q). I1 is a K1-by-K1 identity matrix and I2 is a */
576 /* > K2-by-K2 identity matrix, where K1 = MAX(Q+P-M,0), K2 = MAX(Q-P,0). */
577 /* > \endverbatim */
578
579 /*  Arguments: */
580 /*  ========== */
581
582 /* > \param[in] JOBU1 */
583 /* > \verbatim */
584 /* >          JOBU1 is CHARACTER */
585 /* >          = 'Y':      U1 is computed; */
586 /* >          otherwise:  U1 is not computed. */
587 /* > \endverbatim */
588 /* > */
589 /* > \param[in] JOBU2 */
590 /* > \verbatim */
591 /* >          JOBU2 is CHARACTER */
592 /* >          = 'Y':      U2 is computed; */
593 /* >          otherwise:  U2 is not computed. */
594 /* > \endverbatim */
595 /* > */
596 /* > \param[in] JOBV1T */
597 /* > \verbatim */
598 /* >          JOBV1T is CHARACTER */
599 /* >          = 'Y':      V1T is computed; */
600 /* >          otherwise:  V1T is not computed. */
601 /* > \endverbatim */
602 /* > */
603 /* > \param[in] M */
604 /* > \verbatim */
605 /* >          M is INTEGER */
606 /* >          The number of rows in X. */
607 /* > \endverbatim */
608 /* > */
609 /* > \param[in] P */
610 /* > \verbatim */
611 /* >          P is INTEGER */
612 /* >          The number of rows in X11. 0 <= P <= M. */
613 /* > \endverbatim */
614 /* > */
615 /* > \param[in] Q */
616 /* > \verbatim */
617 /* >          Q is INTEGER */
618 /* >          The number of columns in X11 and X21. 0 <= Q <= M. */
619 /* > \endverbatim */
620 /* > */
621 /* > \param[in,out] X11 */
622 /* > \verbatim */
623 /* >          X11 is REAL array, dimension (LDX11,Q) */
624 /* >          On entry, part of the orthogonal matrix whose CSD is desired. */
625 /* > \endverbatim */
626 /* > */
627 /* > \param[in] LDX11 */
628 /* > \verbatim */
629 /* >          LDX11 is INTEGER */
630 /* >          The leading dimension of X11. LDX11 >= MAX(1,P). */
631 /* > \endverbatim */
632 /* > */
633 /* > \param[in,out] X21 */
634 /* > \verbatim */
635 /* >          X21 is REAL array, dimension (LDX21,Q) */
636 /* >          On entry, part of the orthogonal matrix whose CSD is desired. */
637 /* > \endverbatim */
638 /* > */
639 /* > \param[in] LDX21 */
640 /* > \verbatim */
641 /* >          LDX21 is INTEGER */
642 /* >           The leading dimension of X21. LDX21 >= MAX(1,M-P). */
643 /* > \endverbatim */
644 /* > */
645 /* > \param[out] THETA */
646 /* > \verbatim */
647 /* >          THETA is REAL array, dimension (R), in which R = */
648 /* >          MIN(P,M-P,Q,M-Q). */
649 /* >          C = DIAG( COS(THETA(1)), ... , COS(THETA(R)) ) and */
650 /* >          S = DIAG( SIN(THETA(1)), ... , SIN(THETA(R)) ). */
651 /* > \endverbatim */
652 /* > */
653 /* > \param[out] U1 */
654 /* > \verbatim */
655 /* >          U1 is REAL array, dimension (P) */
656 /* >          If JOBU1 = 'Y', U1 contains the P-by-P orthogonal matrix U1. */
657 /* > \endverbatim */
658 /* > */
659 /* > \param[in] LDU1 */
660 /* > \verbatim */
661 /* >          LDU1 is INTEGER */
662 /* >          The leading dimension of U1. If JOBU1 = 'Y', LDU1 >= */
663 /* >          MAX(1,P). */
664 /* > \endverbatim */
665 /* > */
666 /* > \param[out] U2 */
667 /* > \verbatim */
668 /* >          U2 is REAL array, dimension (M-P) */
669 /* >          If JOBU2 = 'Y', U2 contains the (M-P)-by-(M-P) orthogonal */
670 /* >          matrix U2. */
671 /* > \endverbatim */
672 /* > */
673 /* > \param[in] LDU2 */
674 /* > \verbatim */
675 /* >          LDU2 is INTEGER */
676 /* >          The leading dimension of U2. If JOBU2 = 'Y', LDU2 >= */
677 /* >          MAX(1,M-P). */
678 /* > \endverbatim */
679 /* > */
680 /* > \param[out] V1T */
681 /* > \verbatim */
682 /* >          V1T is REAL array, dimension (Q) */
683 /* >          If JOBV1T = 'Y', V1T contains the Q-by-Q matrix orthogonal */
684 /* >          matrix V1**T. */
685 /* > \endverbatim */
686 /* > */
687 /* > \param[in] LDV1T */
688 /* > \verbatim */
689 /* >          LDV1T is INTEGER */
690 /* >          The leading dimension of V1T. If JOBV1T = 'Y', LDV1T >= */
691 /* >          MAX(1,Q). */
692 /* > \endverbatim */
693 /* > */
694 /* > \param[out] WORK */
695 /* > \verbatim */
696 /* >          WORK is REAL array, dimension (MAX(1,LWORK)) */
697 /* >          On exit, if INFO = 0, WORK(1) returns the optimal LWORK. */
698 /* >          If INFO > 0 on exit, WORK(2:R) contains the values PHI(1), */
699 /* >          ..., PHI(R-1) that, together with THETA(1), ..., THETA(R), */
700 /* >          define the matrix in intermediate bidiagonal-block form */
701 /* >          remaining after nonconvergence. INFO specifies the number */
702 /* >          of nonzero PHI's. */
703 /* > \endverbatim */
704 /* > */
705 /* > \param[in] LWORK */
706 /* > \verbatim */
707 /* >          LWORK is INTEGER */
708 /* >          The dimension of the array WORK. */
709 /* > */
710 /* >          If LWORK = -1, then a workspace query is assumed; the routine */
711 /* >          only calculates the optimal size of the WORK array, returns */
712 /* >          this value as the first entry of the work array, and no error */
713 /* >          message related to LWORK is issued by XERBLA. */
714 /* > \endverbatim */
715 /* > */
716 /* > \param[out] IWORK */
717 /* > \verbatim */
718 /* >          IWORK is INTEGER array, dimension (M-MIN(P,M-P,Q,M-Q)) */
719 /* > \endverbatim */
720 /* > */
721 /* > \param[out] INFO */
722 /* > \verbatim */
723 /* >          INFO is INTEGER */
724 /* >          = 0:  successful exit. */
725 /* >          < 0:  if INFO = -i, the i-th argument had an illegal value. */
726 /* >          > 0:  SBBCSD did not converge. See the description of WORK */
727 /* >                above for details. */
728 /* > \endverbatim */
729
730 /* > \par References: */
731 /*  ================ */
732 /* > */
733 /* >  [1] Brian D. Sutton. Computing the complete CS decomposition. Numer. */
734 /* >      Algorithms, 50(1):33-65, 2009. */
735
736 /*  Authors: */
737 /*  ======== */
738
739 /* > \author Univ. of Tennessee */
740 /* > \author Univ. of California Berkeley */
741 /* > \author Univ. of Colorado Denver */
742 /* > \author NAG Ltd. */
743
744 /* > \date July 2012 */
745
746 /* > \ingroup realOTHERcomputational */
747
748 /*  ===================================================================== */
749 /* Subroutine */ int sorcsd2by1_(char *jobu1, char *jobu2, char *jobv1t, 
750         integer *m, integer *p, integer *q, real *x11, integer *ldx11, real *
751         x21, integer *ldx21, real *theta, real *u1, integer *ldu1, real *u2, 
752         integer *ldu2, real *v1t, integer *ldv1t, real *work, integer *lwork, 
753         integer *iwork, integer *info)
754 {
755     /* System generated locals */
756     integer u1_dim1, u1_offset, u2_dim1, u2_offset, v1t_dim1, v1t_offset, 
757             x11_dim1, x11_offset, x21_dim1, x21_offset, i__1, i__2, i__3;
758
759     /* Local variables */
760     integer ib11d, ib11e, ib12d, ib12e, ib21d, ib21e, ib22d, ib22e, iphi, 
761             lworkmin, lworkopt, i__, j, r__;
762     extern logical lsame_(char *, char *);
763     integer childinfo;
764     extern /* Subroutine */ int scopy_(integer *, real *, integer *, real *, 
765             integer *);
766     integer lorglqmin, lorgqrmin, lorglqopt, itaup1, itaup2, itauq1, 
767             lorgqropt;
768     logical wantu1, wantu2;
769     integer ibbcsd, lbbcsd;
770     extern /* Subroutine */ int sbbcsd_(char *, char *, char *, char *, char *
771             , integer *, integer *, integer *, real *, real *, real *, 
772             integer *, real *, integer *, real *, integer *, real *, integer *
773             , real *, real *, real *, real *, real *, real *, real *, real *, 
774             real *, integer *, integer *);
775     integer iorbdb, lorbdb;
776     extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen), slacpy_(
777             char *, integer *, integer *, real *, integer *, real *, integer *
778             );
779     integer iorglq;
780     extern /* Subroutine */ int slapmr_(logical *, integer *, integer *, real 
781             *, integer *, integer *);
782     integer lorglq;
783     extern /* Subroutine */ int slapmt_(logical *, integer *, integer *, real 
784             *, integer *, integer *);
785     integer iorgqr, lorgqr;
786     extern /* Subroutine */ int sorglq_(integer *, integer *, integer *, real 
787             *, integer *, real *, real *, integer *, integer *), sorgqr_(
788             integer *, integer *, integer *, real *, integer *, real *, real *
789             , integer *, integer *);
790     logical lquery;
791     extern /* Subroutine */ int sorbdb1_(integer *, integer *, integer *, 
792             real *, integer *, real *, integer *, real *, real *, real *, 
793             real *, real *, real *, integer *, integer *), sorbdb2_(integer *,
794              integer *, integer *, real *, integer *, real *, integer *, real 
795             *, real *, real *, real *, real *, real *, integer *, integer *), 
796             sorbdb3_(integer *, integer *, integer *, real *, integer *, real 
797             *, integer *, real *, real *, real *, real *, real *, real *, 
798             integer *, integer *), sorbdb4_(integer *, integer *, integer *, 
799             real *, integer *, real *, integer *, real *, real *, real *, 
800             real *, real *, real *, real *, integer *, integer *);
801     logical wantv1t;
802     real dum1[1], dum2[1]       /* was [1][1] */;
803
804
805 /*  -- LAPACK computational routine (version 3.7.1) -- */
806 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
807 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
808 /*     July 2012 */
809
810
811 /*  ===================================================================== */
812
813
814 /*     Test input arguments */
815
816     /* Parameter adjustments */
817     x11_dim1 = *ldx11;
818     x11_offset = 1 + x11_dim1 * 1;
819     x11 -= x11_offset;
820     x21_dim1 = *ldx21;
821     x21_offset = 1 + x21_dim1 * 1;
822     x21 -= x21_offset;
823     --theta;
824     u1_dim1 = *ldu1;
825     u1_offset = 1 + u1_dim1 * 1;
826     u1 -= u1_offset;
827     u2_dim1 = *ldu2;
828     u2_offset = 1 + u2_dim1 * 1;
829     u2 -= u2_offset;
830     v1t_dim1 = *ldv1t;
831     v1t_offset = 1 + v1t_dim1 * 1;
832     v1t -= v1t_offset;
833     --work;
834     --iwork;
835
836     /* Function Body */
837     *info = 0;
838     wantu1 = lsame_(jobu1, "Y");
839     wantu2 = lsame_(jobu2, "Y");
840     wantv1t = lsame_(jobv1t, "Y");
841     lquery = *lwork == -1;
842
843     if (*m < 0) {
844         *info = -4;
845     } else if (*p < 0 || *p > *m) {
846         *info = -5;
847     } else if (*q < 0 || *q > *m) {
848         *info = -6;
849     } else if (*ldx11 < f2cmax(1,*p)) {
850         *info = -8;
851     } else /* if(complicated condition) */ {
852 /* Computing MAX */
853         i__1 = 1, i__2 = *m - *p;
854         if (*ldx21 < f2cmax(i__1,i__2)) {
855             *info = -10;
856         } else if (wantu1 && *ldu1 < f2cmax(1,*p)) {
857             *info = -13;
858         } else /* if(complicated condition) */ {
859 /* Computing MAX */
860             i__1 = 1, i__2 = *m - *p;
861             if (wantu2 && *ldu2 < f2cmax(i__1,i__2)) {
862                 *info = -15;
863             } else if (wantv1t && *ldv1t < f2cmax(1,*q)) {
864                 *info = -17;
865             }
866         }
867     }
868
869 /* Computing MIN */
870     i__1 = *p, i__2 = *m - *p, i__1 = f2cmin(i__1,i__2), i__1 = f2cmin(i__1,*q), 
871             i__2 = *m - *q;
872     r__ = f2cmin(i__1,i__2);
873
874 /*     Compute workspace */
875
876 /*       WORK layout: */
877 /*     |-------------------------------------------------------| */
878 /*     | LWORKOPT (1)                                          | */
879 /*     |-------------------------------------------------------| */
880 /*     | PHI (MAX(1,R-1))                                      | */
881 /*     |-------------------------------------------------------| */
882 /*     | TAUP1 (MAX(1,P))                        | B11D (R)    | */
883 /*     | TAUP2 (MAX(1,M-P))                      | B11E (R-1)  | */
884 /*     | TAUQ1 (MAX(1,Q))                        | B12D (R)    | */
885 /*     |-----------------------------------------| B12E (R-1)  | */
886 /*     | SORBDB WORK | SORGQR WORK | SORGLQ WORK | B21D (R)    | */
887 /*     |             |             |             | B21E (R-1)  | */
888 /*     |             |             |             | B22D (R)    | */
889 /*     |             |             |             | B22E (R-1)  | */
890 /*     |             |             |             | SBBCSD WORK | */
891 /*     |-------------------------------------------------------| */
892
893     if (*info == 0) {
894         iphi = 2;
895 /* Computing MAX */
896         i__1 = 1, i__2 = r__ - 1;
897         ib11d = iphi + f2cmax(i__1,i__2);
898         ib11e = ib11d + f2cmax(1,r__);
899 /* Computing MAX */
900         i__1 = 1, i__2 = r__ - 1;
901         ib12d = ib11e + f2cmax(i__1,i__2);
902         ib12e = ib12d + f2cmax(1,r__);
903 /* Computing MAX */
904         i__1 = 1, i__2 = r__ - 1;
905         ib21d = ib12e + f2cmax(i__1,i__2);
906         ib21e = ib21d + f2cmax(1,r__);
907 /* Computing MAX */
908         i__1 = 1, i__2 = r__ - 1;
909         ib22d = ib21e + f2cmax(i__1,i__2);
910         ib22e = ib22d + f2cmax(1,r__);
911 /* Computing MAX */
912         i__1 = 1, i__2 = r__ - 1;
913         ibbcsd = ib22e + f2cmax(i__1,i__2);
914 /* Computing MAX */
915         i__1 = 1, i__2 = r__ - 1;
916         itaup1 = iphi + f2cmax(i__1,i__2);
917         itaup2 = itaup1 + f2cmax(1,*p);
918 /* Computing MAX */
919         i__1 = 1, i__2 = *m - *p;
920         itauq1 = itaup2 + f2cmax(i__1,i__2);
921         iorbdb = itauq1 + f2cmax(1,*q);
922         iorgqr = itauq1 + f2cmax(1,*q);
923         iorglq = itauq1 + f2cmax(1,*q);
924         lorgqrmin = 1;
925         lorgqropt = 1;
926         lorglqmin = 1;
927         lorglqopt = 1;
928         if (r__ == *q) {
929             sorbdb1_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], 
930                     ldx21, &theta[1], dum1, dum1, dum1, dum1, &work[1], &c_n1,
931                      &childinfo);
932             lorbdb = (integer) work[1];
933             if (wantu1 && *p > 0) {
934                 sorgqr_(p, p, q, &u1[u1_offset], ldu1, dum1, &work[1], &c_n1, 
935                         &childinfo);
936                 lorgqrmin = f2cmax(lorgqrmin,*p);
937 /* Computing MAX */
938                 i__1 = lorgqropt, i__2 = (integer) work[1];
939                 lorgqropt = f2cmax(i__1,i__2);
940             }
941             if (wantu2 && *m - *p > 0) {
942                 i__1 = *m - *p;
943                 i__2 = *m - *p;
944                 sorgqr_(&i__1, &i__2, q, &u2[u2_offset], ldu2, dum1, &work[1],
945                          &c_n1, &childinfo);
946 /* Computing MAX */
947                 i__1 = lorgqrmin, i__2 = *m - *p;
948                 lorgqrmin = f2cmax(i__1,i__2);
949 /* Computing MAX */
950                 i__1 = lorgqropt, i__2 = (integer) work[1];
951                 lorgqropt = f2cmax(i__1,i__2);
952             }
953             if (wantv1t && *q > 0) {
954                 i__1 = *q - 1;
955                 i__2 = *q - 1;
956                 i__3 = *q - 1;
957                 sorglq_(&i__1, &i__2, &i__3, &v1t[v1t_offset], ldv1t, dum1, &
958                         work[1], &c_n1, &childinfo);
959 /* Computing MAX */
960                 i__1 = lorglqmin, i__2 = *q - 1;
961                 lorglqmin = f2cmax(i__1,i__2);
962 /* Computing MAX */
963                 i__1 = lorglqopt, i__2 = (integer) work[1];
964                 lorglqopt = f2cmax(i__1,i__2);
965             }
966             sbbcsd_(jobu1, jobu2, jobv1t, "N", "N", m, p, q, &theta[1], dum1, 
967                     &u1[u1_offset], ldu1, &u2[u2_offset], ldu2, &v1t[
968                     v1t_offset], ldv1t, dum2, &c__1, dum1, dum1, dum1, dum1, 
969                     dum1, dum1, dum1, dum1, &work[1], &c_n1, &childinfo);
970             lbbcsd = (integer) work[1];
971         } else if (r__ == *p) {
972             sorbdb2_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], 
973                     ldx21, &theta[1], dum1, dum1, dum1, dum1, &work[1], &c_n1,
974                      &childinfo);
975             lorbdb = (integer) work[1];
976             if (wantu1 && *p > 0) {
977                 i__1 = *p - 1;
978                 i__2 = *p - 1;
979                 i__3 = *p - 1;
980                 sorgqr_(&i__1, &i__2, &i__3, &u1[(u1_dim1 << 1) + 2], ldu1, 
981                         dum1, &work[1], &c_n1, &childinfo);
982 /* Computing MAX */
983                 i__1 = lorgqrmin, i__2 = *p - 1;
984                 lorgqrmin = f2cmax(i__1,i__2);
985 /* Computing MAX */
986                 i__1 = lorgqropt, i__2 = (integer) work[1];
987                 lorgqropt = f2cmax(i__1,i__2);
988             }
989             if (wantu2 && *m - *p > 0) {
990                 i__1 = *m - *p;
991                 i__2 = *m - *p;
992                 sorgqr_(&i__1, &i__2, q, &u2[u2_offset], ldu2, dum1, &work[1],
993                          &c_n1, &childinfo);
994 /* Computing MAX */
995                 i__1 = lorgqrmin, i__2 = *m - *p;
996                 lorgqrmin = f2cmax(i__1,i__2);
997 /* Computing MAX */
998                 i__1 = lorgqropt, i__2 = (integer) work[1];
999                 lorgqropt = f2cmax(i__1,i__2);
1000             }
1001             if (wantv1t && *q > 0) {
1002                 sorglq_(q, q, &r__, &v1t[v1t_offset], ldv1t, dum1, &work[1], &
1003                         c_n1, &childinfo);
1004                 lorglqmin = f2cmax(lorglqmin,*q);
1005 /* Computing MAX */
1006                 i__1 = lorglqopt, i__2 = (integer) work[1];
1007                 lorglqopt = f2cmax(i__1,i__2);
1008             }
1009             sbbcsd_(jobv1t, "N", jobu1, jobu2, "T", m, q, p, &theta[1], dum1, 
1010                     &v1t[v1t_offset], ldv1t, dum2, &c__1, &u1[u1_offset], 
1011                     ldu1, &u2[u2_offset], ldu2, dum1, dum1, dum1, dum1, dum1, 
1012                     dum1, dum1, dum1, &work[1], &c_n1, &childinfo);
1013             lbbcsd = (integer) work[1];
1014         } else if (r__ == *m - *p) {
1015             sorbdb3_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], 
1016                     ldx21, &theta[1], dum1, dum1, dum1, dum1, &work[1], &c_n1,
1017                      &childinfo);
1018             lorbdb = (integer) work[1];
1019             if (wantu1 && *p > 0) {
1020                 sorgqr_(p, p, q, &u1[u1_offset], ldu1, dum1, &work[1], &c_n1, 
1021                         &childinfo);
1022                 lorgqrmin = f2cmax(lorgqrmin,*p);
1023 /* Computing MAX */
1024                 i__1 = lorgqropt, i__2 = (integer) work[1];
1025                 lorgqropt = f2cmax(i__1,i__2);
1026             }
1027             if (wantu2 && *m - *p > 0) {
1028                 i__1 = *m - *p - 1;
1029                 i__2 = *m - *p - 1;
1030                 i__3 = *m - *p - 1;
1031                 sorgqr_(&i__1, &i__2, &i__3, &u2[(u2_dim1 << 1) + 2], ldu2, 
1032                         dum1, &work[1], &c_n1, &childinfo);
1033 /* Computing MAX */
1034                 i__1 = lorgqrmin, i__2 = *m - *p - 1;
1035                 lorgqrmin = f2cmax(i__1,i__2);
1036 /* Computing MAX */
1037                 i__1 = lorgqropt, i__2 = (integer) work[1];
1038                 lorgqropt = f2cmax(i__1,i__2);
1039             }
1040             if (wantv1t && *q > 0) {
1041                 sorglq_(q, q, &r__, &v1t[v1t_offset], ldv1t, dum1, &work[1], &
1042                         c_n1, &childinfo);
1043                 lorglqmin = f2cmax(lorglqmin,*q);
1044 /* Computing MAX */
1045                 i__1 = lorglqopt, i__2 = (integer) work[1];
1046                 lorglqopt = f2cmax(i__1,i__2);
1047             }
1048             i__1 = *m - *q;
1049             i__2 = *m - *p;
1050             sbbcsd_("N", jobv1t, jobu2, jobu1, "T", m, &i__1, &i__2, &theta[1]
1051                     , dum1, dum2, &c__1, &v1t[v1t_offset], ldv1t, &u2[
1052                     u2_offset], ldu2, &u1[u1_offset], ldu1, dum1, dum1, dum1, 
1053                     dum1, dum1, dum1, dum1, dum1, &work[1], &c_n1, &childinfo);
1054             lbbcsd = (integer) work[1];
1055         } else {
1056             sorbdb4_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], 
1057                     ldx21, &theta[1], dum1, dum1, dum1, dum1, dum1, &work[1], 
1058                     &c_n1, &childinfo);
1059             lorbdb = *m + (integer) work[1];
1060             if (wantu1 && *p > 0) {
1061                 i__1 = *m - *q;
1062                 sorgqr_(p, p, &i__1, &u1[u1_offset], ldu1, dum1, &work[1], &
1063                         c_n1, &childinfo);
1064                 lorgqrmin = f2cmax(lorgqrmin,*p);
1065 /* Computing MAX */
1066                 i__1 = lorgqropt, i__2 = (integer) work[1];
1067                 lorgqropt = f2cmax(i__1,i__2);
1068             }
1069             if (wantu2 && *m - *p > 0) {
1070                 i__1 = *m - *p;
1071                 i__2 = *m - *p;
1072                 i__3 = *m - *q;
1073                 sorgqr_(&i__1, &i__2, &i__3, &u2[u2_offset], ldu2, dum1, &
1074                         work[1], &c_n1, &childinfo);
1075 /* Computing MAX */
1076                 i__1 = lorgqrmin, i__2 = *m - *p;
1077                 lorgqrmin = f2cmax(i__1,i__2);
1078 /* Computing MAX */
1079                 i__1 = lorgqropt, i__2 = (integer) work[1];
1080                 lorgqropt = f2cmax(i__1,i__2);
1081             }
1082             if (wantv1t && *q > 0) {
1083                 sorglq_(q, q, q, &v1t[v1t_offset], ldv1t, dum1, &work[1], &
1084                         c_n1, &childinfo);
1085                 lorglqmin = f2cmax(lorglqmin,*q);
1086 /* Computing MAX */
1087                 i__1 = lorglqopt, i__2 = (integer) work[1];
1088                 lorglqopt = f2cmax(i__1,i__2);
1089             }
1090             i__1 = *m - *p;
1091             i__2 = *m - *q;
1092             sbbcsd_(jobu2, jobu1, "N", jobv1t, "N", m, &i__1, &i__2, &theta[1]
1093                     , dum1, &u2[u2_offset], ldu2, &u1[u1_offset], ldu1, dum2, 
1094                     &c__1, &v1t[v1t_offset], ldv1t, dum1, dum1, dum1, dum1, 
1095                     dum1, dum1, dum1, dum1, &work[1], &c_n1, &childinfo);
1096             lbbcsd = (integer) work[1];
1097         }
1098 /* Computing MAX */
1099         i__1 = iorbdb + lorbdb - 1, i__2 = iorgqr + lorgqrmin - 1, i__1 = f2cmax(
1100                 i__1,i__2), i__2 = iorglq + lorglqmin - 1, i__1 = f2cmax(i__1,
1101                 i__2), i__2 = ibbcsd + lbbcsd - 1;
1102         lworkmin = f2cmax(i__1,i__2);
1103 /* Computing MAX */
1104         i__1 = iorbdb + lorbdb - 1, i__2 = iorgqr + lorgqropt - 1, i__1 = f2cmax(
1105                 i__1,i__2), i__2 = iorglq + lorglqopt - 1, i__1 = f2cmax(i__1,
1106                 i__2), i__2 = ibbcsd + lbbcsd - 1;
1107         lworkopt = f2cmax(i__1,i__2);
1108         work[1] = (real) lworkopt;
1109         if (*lwork < lworkmin && ! lquery) {
1110             *info = -19;
1111         }
1112     }
1113     if (*info != 0) {
1114         i__1 = -(*info);
1115         xerbla_("SORCSD2BY1", &i__1, (ftnlen)10);
1116         return 0;
1117     } else if (lquery) {
1118         return 0;
1119     }
1120     lorgqr = *lwork - iorgqr + 1;
1121     lorglq = *lwork - iorglq + 1;
1122
1123 /*     Handle four cases separately: R = Q, R = P, R = M-P, and R = M-Q, */
1124 /*     in which R = MIN(P,M-P,Q,M-Q) */
1125
1126     if (r__ == *q) {
1127
1128 /*        Case 1: R = Q */
1129
1130 /*        Simultaneously bidiagonalize X11 and X21 */
1131
1132         sorbdb1_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], ldx21, &
1133                 theta[1], &work[iphi], &work[itaup1], &work[itaup2], &work[
1134                 itauq1], &work[iorbdb], &lorbdb, &childinfo);
1135
1136 /*        Accumulate Householder reflectors */
1137
1138         if (wantu1 && *p > 0) {
1139             slacpy_("L", p, q, &x11[x11_offset], ldx11, &u1[u1_offset], ldu1);
1140             sorgqr_(p, p, q, &u1[u1_offset], ldu1, &work[itaup1], &work[
1141                     iorgqr], &lorgqr, &childinfo);
1142         }
1143         if (wantu2 && *m - *p > 0) {
1144             i__1 = *m - *p;
1145             slacpy_("L", &i__1, q, &x21[x21_offset], ldx21, &u2[u2_offset], 
1146                     ldu2);
1147             i__1 = *m - *p;
1148             i__2 = *m - *p;
1149             sorgqr_(&i__1, &i__2, q, &u2[u2_offset], ldu2, &work[itaup2], &
1150                     work[iorgqr], &lorgqr, &childinfo);
1151         }
1152         if (wantv1t && *q > 0) {
1153             v1t[v1t_dim1 + 1] = 1.f;
1154             i__1 = *q;
1155             for (j = 2; j <= i__1; ++j) {
1156                 v1t[j * v1t_dim1 + 1] = 0.f;
1157                 v1t[j + v1t_dim1] = 0.f;
1158             }
1159             i__1 = *q - 1;
1160             i__2 = *q - 1;
1161             slacpy_("U", &i__1, &i__2, &x21[(x21_dim1 << 1) + 1], ldx21, &v1t[
1162                     (v1t_dim1 << 1) + 2], ldv1t);
1163             i__1 = *q - 1;
1164             i__2 = *q - 1;
1165             i__3 = *q - 1;
1166             sorglq_(&i__1, &i__2, &i__3, &v1t[(v1t_dim1 << 1) + 2], ldv1t, &
1167                     work[itauq1], &work[iorglq], &lorglq, &childinfo);
1168         }
1169
1170 /*        Simultaneously diagonalize X11 and X21. */
1171
1172         sbbcsd_(jobu1, jobu2, jobv1t, "N", "N", m, p, q, &theta[1], &work[
1173                 iphi], &u1[u1_offset], ldu1, &u2[u2_offset], ldu2, &v1t[
1174                 v1t_offset], ldv1t, dum2, &c__1, &work[ib11d], &work[ib11e], &
1175                 work[ib12d], &work[ib12e], &work[ib21d], &work[ib21e], &work[
1176                 ib22d], &work[ib22e], &work[ibbcsd], &lbbcsd, &childinfo);
1177
1178 /*        Permute rows and columns to place zero submatrices in */
1179 /*        preferred positions */
1180
1181         if (*q > 0 && wantu2) {
1182             i__1 = *q;
1183             for (i__ = 1; i__ <= i__1; ++i__) {
1184                 iwork[i__] = *m - *p - *q + i__;
1185             }
1186             i__1 = *m - *p;
1187             for (i__ = *q + 1; i__ <= i__1; ++i__) {
1188                 iwork[i__] = i__ - *q;
1189             }
1190             i__1 = *m - *p;
1191             i__2 = *m - *p;
1192             slapmt_(&c_false, &i__1, &i__2, &u2[u2_offset], ldu2, &iwork[1]);
1193         }
1194     } else if (r__ == *p) {
1195
1196 /*        Case 2: R = P */
1197
1198 /*        Simultaneously bidiagonalize X11 and X21 */
1199
1200         sorbdb2_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], ldx21, &
1201                 theta[1], &work[iphi], &work[itaup1], &work[itaup2], &work[
1202                 itauq1], &work[iorbdb], &lorbdb, &childinfo);
1203
1204 /*        Accumulate Householder reflectors */
1205
1206         if (wantu1 && *p > 0) {
1207             u1[u1_dim1 + 1] = 1.f;
1208             i__1 = *p;
1209             for (j = 2; j <= i__1; ++j) {
1210                 u1[j * u1_dim1 + 1] = 0.f;
1211                 u1[j + u1_dim1] = 0.f;
1212             }
1213             i__1 = *p - 1;
1214             i__2 = *p - 1;
1215             slacpy_("L", &i__1, &i__2, &x11[x11_dim1 + 2], ldx11, &u1[(
1216                     u1_dim1 << 1) + 2], ldu1);
1217             i__1 = *p - 1;
1218             i__2 = *p - 1;
1219             i__3 = *p - 1;
1220             sorgqr_(&i__1, &i__2, &i__3, &u1[(u1_dim1 << 1) + 2], ldu1, &work[
1221                     itaup1], &work[iorgqr], &lorgqr, &childinfo);
1222         }
1223         if (wantu2 && *m - *p > 0) {
1224             i__1 = *m - *p;
1225             slacpy_("L", &i__1, q, &x21[x21_offset], ldx21, &u2[u2_offset], 
1226                     ldu2);
1227             i__1 = *m - *p;
1228             i__2 = *m - *p;
1229             sorgqr_(&i__1, &i__2, q, &u2[u2_offset], ldu2, &work[itaup2], &
1230                     work[iorgqr], &lorgqr, &childinfo);
1231         }
1232         if (wantv1t && *q > 0) {
1233             slacpy_("U", p, q, &x11[x11_offset], ldx11, &v1t[v1t_offset], 
1234                     ldv1t);
1235             sorglq_(q, q, &r__, &v1t[v1t_offset], ldv1t, &work[itauq1], &work[
1236                     iorglq], &lorglq, &childinfo);
1237         }
1238
1239 /*        Simultaneously diagonalize X11 and X21. */
1240
1241         sbbcsd_(jobv1t, "N", jobu1, jobu2, "T", m, q, p, &theta[1], &work[
1242                 iphi], &v1t[v1t_offset], ldv1t, dum1, &c__1, &u1[u1_offset], 
1243                 ldu1, &u2[u2_offset], ldu2, &work[ib11d], &work[ib11e], &work[
1244                 ib12d], &work[ib12e], &work[ib21d], &work[ib21e], &work[ib22d]
1245                 , &work[ib22e], &work[ibbcsd], &lbbcsd, &childinfo);
1246
1247 /*        Permute rows and columns to place identity submatrices in */
1248 /*        preferred positions */
1249
1250         if (*q > 0 && wantu2) {
1251             i__1 = *q;
1252             for (i__ = 1; i__ <= i__1; ++i__) {
1253                 iwork[i__] = *m - *p - *q + i__;
1254             }
1255             i__1 = *m - *p;
1256             for (i__ = *q + 1; i__ <= i__1; ++i__) {
1257                 iwork[i__] = i__ - *q;
1258             }
1259             i__1 = *m - *p;
1260             i__2 = *m - *p;
1261             slapmt_(&c_false, &i__1, &i__2, &u2[u2_offset], ldu2, &iwork[1]);
1262         }
1263     } else if (r__ == *m - *p) {
1264
1265 /*        Case 3: R = M-P */
1266
1267 /*        Simultaneously bidiagonalize X11 and X21 */
1268
1269         sorbdb3_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], ldx21, &
1270                 theta[1], &work[iphi], &work[itaup1], &work[itaup2], &work[
1271                 itauq1], &work[iorbdb], &lorbdb, &childinfo);
1272
1273 /*        Accumulate Householder reflectors */
1274
1275         if (wantu1 && *p > 0) {
1276             slacpy_("L", p, q, &x11[x11_offset], ldx11, &u1[u1_offset], ldu1);
1277             sorgqr_(p, p, q, &u1[u1_offset], ldu1, &work[itaup1], &work[
1278                     iorgqr], &lorgqr, &childinfo);
1279         }
1280         if (wantu2 && *m - *p > 0) {
1281             u2[u2_dim1 + 1] = 1.f;
1282             i__1 = *m - *p;
1283             for (j = 2; j <= i__1; ++j) {
1284                 u2[j * u2_dim1 + 1] = 0.f;
1285                 u2[j + u2_dim1] = 0.f;
1286             }
1287             i__1 = *m - *p - 1;
1288             i__2 = *m - *p - 1;
1289             slacpy_("L", &i__1, &i__2, &x21[x21_dim1 + 2], ldx21, &u2[(
1290                     u2_dim1 << 1) + 2], ldu2);
1291             i__1 = *m - *p - 1;
1292             i__2 = *m - *p - 1;
1293             i__3 = *m - *p - 1;
1294             sorgqr_(&i__1, &i__2, &i__3, &u2[(u2_dim1 << 1) + 2], ldu2, &work[
1295                     itaup2], &work[iorgqr], &lorgqr, &childinfo);
1296         }
1297         if (wantv1t && *q > 0) {
1298             i__1 = *m - *p;
1299             slacpy_("U", &i__1, q, &x21[x21_offset], ldx21, &v1t[v1t_offset], 
1300                     ldv1t);
1301             sorglq_(q, q, &r__, &v1t[v1t_offset], ldv1t, &work[itauq1], &work[
1302                     iorglq], &lorglq, &childinfo);
1303         }
1304
1305 /*        Simultaneously diagonalize X11 and X21. */
1306
1307         i__1 = *m - *q;
1308         i__2 = *m - *p;
1309         sbbcsd_("N", jobv1t, jobu2, jobu1, "T", m, &i__1, &i__2, &theta[1], &
1310                 work[iphi], dum1, &c__1, &v1t[v1t_offset], ldv1t, &u2[
1311                 u2_offset], ldu2, &u1[u1_offset], ldu1, &work[ib11d], &work[
1312                 ib11e], &work[ib12d], &work[ib12e], &work[ib21d], &work[ib21e]
1313                 , &work[ib22d], &work[ib22e], &work[ibbcsd], &lbbcsd, &
1314                 childinfo);
1315
1316 /*        Permute rows and columns to place identity submatrices in */
1317 /*        preferred positions */
1318
1319         if (*q > r__) {
1320             i__1 = r__;
1321             for (i__ = 1; i__ <= i__1; ++i__) {
1322                 iwork[i__] = *q - r__ + i__;
1323             }
1324             i__1 = *q;
1325             for (i__ = r__ + 1; i__ <= i__1; ++i__) {
1326                 iwork[i__] = i__ - r__;
1327             }
1328             if (wantu1) {
1329                 slapmt_(&c_false, p, q, &u1[u1_offset], ldu1, &iwork[1]);
1330             }
1331             if (wantv1t) {
1332                 slapmr_(&c_false, q, q, &v1t[v1t_offset], ldv1t, &iwork[1]);
1333             }
1334         }
1335     } else {
1336
1337 /*        Case 4: R = M-Q */
1338
1339 /*        Simultaneously bidiagonalize X11 and X21 */
1340
1341         i__1 = lorbdb - *m;
1342         sorbdb4_(m, p, q, &x11[x11_offset], ldx11, &x21[x21_offset], ldx21, &
1343                 theta[1], &work[iphi], &work[itaup1], &work[itaup2], &work[
1344                 itauq1], &work[iorbdb], &work[iorbdb + *m], &i__1, &childinfo)
1345                 ;
1346
1347 /*        Accumulate Householder reflectors */
1348
1349         if (wantu1 && *p > 0) {
1350             scopy_(p, &work[iorbdb], &c__1, &u1[u1_offset], &c__1);
1351             i__1 = *p;
1352             for (j = 2; j <= i__1; ++j) {
1353                 u1[j * u1_dim1 + 1] = 0.f;
1354             }
1355             i__1 = *p - 1;
1356             i__2 = *m - *q - 1;
1357             slacpy_("L", &i__1, &i__2, &x11[x11_dim1 + 2], ldx11, &u1[(
1358                     u1_dim1 << 1) + 2], ldu1);
1359             i__1 = *m - *q;
1360             sorgqr_(p, p, &i__1, &u1[u1_offset], ldu1, &work[itaup1], &work[
1361                     iorgqr], &lorgqr, &childinfo);
1362         }
1363         if (wantu2 && *m - *p > 0) {
1364             i__1 = *m - *p;
1365             scopy_(&i__1, &work[iorbdb + *p], &c__1, &u2[u2_offset], &c__1);
1366             i__1 = *m - *p;
1367             for (j = 2; j <= i__1; ++j) {
1368                 u2[j * u2_dim1 + 1] = 0.f;
1369             }
1370             i__1 = *m - *p - 1;
1371             i__2 = *m - *q - 1;
1372             slacpy_("L", &i__1, &i__2, &x21[x21_dim1 + 2], ldx21, &u2[(
1373                     u2_dim1 << 1) + 2], ldu2);
1374             i__1 = *m - *p;
1375             i__2 = *m - *p;
1376             i__3 = *m - *q;
1377             sorgqr_(&i__1, &i__2, &i__3, &u2[u2_offset], ldu2, &work[itaup2], 
1378                     &work[iorgqr], &lorgqr, &childinfo);
1379         }
1380         if (wantv1t && *q > 0) {
1381             i__1 = *m - *q;
1382             slacpy_("U", &i__1, q, &x21[x21_offset], ldx21, &v1t[v1t_offset], 
1383                     ldv1t);
1384             i__1 = *p - (*m - *q);
1385             i__2 = *q - (*m - *q);
1386             slacpy_("U", &i__1, &i__2, &x11[*m - *q + 1 + (*m - *q + 1) * 
1387                     x11_dim1], ldx11, &v1t[*m - *q + 1 + (*m - *q + 1) * 
1388                     v1t_dim1], ldv1t);
1389             i__1 = -(*p) + *q;
1390             i__2 = *q - *p;
1391             slacpy_("U", &i__1, &i__2, &x21[*m - *q + 1 + (*p + 1) * x21_dim1]
1392                     , ldx21, &v1t[*p + 1 + (*p + 1) * v1t_dim1], ldv1t);
1393             sorglq_(q, q, q, &v1t[v1t_offset], ldv1t, &work[itauq1], &work[
1394                     iorglq], &lorglq, &childinfo);
1395         }
1396
1397 /*        Simultaneously diagonalize X11 and X21. */
1398
1399         i__1 = *m - *p;
1400         i__2 = *m - *q;
1401         sbbcsd_(jobu2, jobu1, "N", jobv1t, "N", m, &i__1, &i__2, &theta[1], &
1402                 work[iphi], &u2[u2_offset], ldu2, &u1[u1_offset], ldu1, dum1, 
1403                 &c__1, &v1t[v1t_offset], ldv1t, &work[ib11d], &work[ib11e], &
1404                 work[ib12d], &work[ib12e], &work[ib21d], &work[ib21e], &work[
1405                 ib22d], &work[ib22e], &work[ibbcsd], &lbbcsd, &childinfo);
1406
1407 /*        Permute rows and columns to place identity submatrices in */
1408 /*        preferred positions */
1409
1410         if (*p > r__) {
1411             i__1 = r__;
1412             for (i__ = 1; i__ <= i__1; ++i__) {
1413                 iwork[i__] = *p - r__ + i__;
1414             }
1415             i__1 = *p;
1416             for (i__ = r__ + 1; i__ <= i__1; ++i__) {
1417                 iwork[i__] = i__ - r__;
1418             }
1419             if (wantu1) {
1420                 slapmt_(&c_false, p, p, &u1[u1_offset], ldu1, &iwork[1]);
1421             }
1422             if (wantv1t) {
1423                 slapmr_(&c_false, p, q, &v1t[v1t_offset], ldv1t, &iwork[1]);
1424             }
1425         }
1426     }
1427
1428     return 0;
1429
1430 /*     End of SORCSD2BY1 */
1431
1432 } /* sorcsd2by1_ */
1433