C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / sbdsvdx.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 real c_b10 = 1.f;
516 static doublereal c_b14 = -.125;
517 static integer c__1 = 1;
518 static real c_b19 = 0.f;
519 static integer c__2 = 2;
520
521 /* > \brief \b SBDSVDX */
522
523 /*  =========== DOCUMENTATION =========== */
524
525 /* Online html documentation available at */
526 /*            http://www.netlib.org/lapack/explore-html/ */
527
528 /* > \htmlonly */
529 /* > Download SBDSVDX + dependencies */
530 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sbdsvdx
531 .f"> */
532 /* > [TGZ]</a> */
533 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sbdsvdx
534 .f"> */
535 /* > [ZIP]</a> */
536 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sbdsvdx
537 .f"> */
538 /* > [TXT]</a> */
539 /* > \endhtmlonly */
540
541 /*  Definition: */
542 /*  =========== */
543
544 /*     SUBROUTINE SBDSVDX( UPLO, JOBZ, RANGE, N, D, E, VL, VU, IL, IU, */
545 /*    $                    NS, S, Z, LDZ, WORK, IWORK, INFO ) */
546
547 /*      CHARACTER          JOBZ, RANGE, UPLO */
548 /*      INTEGER            IL, INFO, IU, LDZ, N, NS */
549 /*      REAL               VL, VU */
550 /*      INTEGER            IWORK( * ) */
551 /*      REAL               D( * ), E( * ), S( * ), WORK( * ), */
552 /*                         Z( LDZ, * ) */
553
554 /* > \par Purpose: */
555 /*  ============= */
556 /* > */
557 /* > \verbatim */
558 /* > */
559 /* >  SBDSVDX computes the singular value decomposition (SVD) of a real */
560 /* >  N-by-N (upper or lower) bidiagonal matrix B, B = U * S * VT, */
561 /* >  where S is a diagonal matrix with non-negative diagonal elements */
562 /* >  (the singular values of B), and U and VT are orthogonal matrices */
563 /* >  of left and right singular vectors, respectively. */
564 /* > */
565 /* >  Given an upper bidiagonal B with diagonal D = [ d_1 d_2 ... d_N ] */
566 /* >  and superdiagonal E = [ e_1 e_2 ... e_N-1 ], SBDSVDX computes the */
567 /* >  singular value decompositon of B through the eigenvalues and */
568 /* >  eigenvectors of the N*2-by-N*2 tridiagonal matrix */
569 /* > */
570 /* >        |  0  d_1                | */
571 /* >        | d_1  0  e_1            | */
572 /* >  TGK = |     e_1  0  d_2        | */
573 /* >        |         d_2  .   .     | */
574 /* >        |              .   .   . | */
575 /* > */
576 /* >  If (s,u,v) is a singular triplet of B with ||u|| = ||v|| = 1, then */
577 /* >  (+/-s,q), ||q|| = 1, are eigenpairs of TGK, with q = P * ( u' +/-v' ) / */
578 /* >  sqrt(2) = ( v_1 u_1 v_2 u_2 ... v_n u_n ) / sqrt(2), and */
579 /* >  P = [ e_{n+1} e_{1} e_{n+2} e_{2} ... ]. */
580 /* > */
581 /* >  Given a TGK matrix, one can either a) compute -s,-v and change signs */
582 /* >  so that the singular values (and corresponding vectors) are already in */
583 /* >  descending order (as in SGESVD/SGESDD) or b) compute s,v and reorder */
584 /* >  the values (and corresponding vectors). SBDSVDX implements a) by */
585 /* >  calling SSTEVX (bisection plus inverse iteration, to be replaced */
586 /* >  with a version of the Multiple Relative Robust Representation */
587 /* >  algorithm. (See P. Willems and B. Lang, A framework for the MR^3 */
588 /* >  algorithm: theory and implementation, SIAM J. Sci. Comput., */
589 /* >  35:740-766, 2013.) */
590 /* > \endverbatim */
591
592 /*  Arguments: */
593 /*  ========== */
594
595 /* > \param[in] UPLO */
596 /* > \verbatim */
597 /* >          UPLO is CHARACTER*1 */
598 /* >          = 'U':  B is upper bidiagonal; */
599 /* >          = 'L':  B is lower bidiagonal. */
600 /* > \endverbatim */
601 /* > */
602 /* > \param[in] JOBZ */
603 /* > \verbatim */
604 /* >          JOBZ is CHARACTER*1 */
605 /* >          = 'N':  Compute singular values only; */
606 /* >          = 'V':  Compute singular values and singular vectors. */
607 /* > \endverbatim */
608 /* > */
609 /* > \param[in] RANGE */
610 /* > \verbatim */
611 /* >          RANGE is CHARACTER*1 */
612 /* >          = 'A': all singular values will be found. */
613 /* >          = 'V': all singular values in the half-open interval [VL,VU) */
614 /* >                 will be found. */
615 /* >          = 'I': the IL-th through IU-th singular values will be found. */
616 /* > \endverbatim */
617 /* > */
618 /* > \param[in] N */
619 /* > \verbatim */
620 /* >          N is INTEGER */
621 /* >          The order of the bidiagonal matrix.  N >= 0. */
622 /* > \endverbatim */
623 /* > */
624 /* > \param[in] D */
625 /* > \verbatim */
626 /* >          D is REAL array, dimension (N) */
627 /* >          The n diagonal elements of the bidiagonal matrix B. */
628 /* > \endverbatim */
629 /* > */
630 /* > \param[in] E */
631 /* > \verbatim */
632 /* >          E is REAL array, dimension (f2cmax(1,N-1)) */
633 /* >          The (n-1) superdiagonal elements of the bidiagonal matrix */
634 /* >          B in elements 1 to N-1. */
635 /* > \endverbatim */
636 /* > */
637 /* > \param[in] VL */
638 /* > \verbatim */
639 /* >         VL is REAL */
640 /* >          If RANGE='V', the lower bound of the interval to */
641 /* >          be searched for singular values. VU > VL. */
642 /* >          Not referenced if RANGE = 'A' or 'I'. */
643 /* > \endverbatim */
644 /* > */
645 /* > \param[in] VU */
646 /* > \verbatim */
647 /* >         VU is REAL */
648 /* >          If RANGE='V', the upper bound of the interval to */
649 /* >          be searched for singular values. VU > VL. */
650 /* >          Not referenced if RANGE = 'A' or 'I'. */
651 /* > \endverbatim */
652 /* > */
653 /* > \param[in] IL */
654 /* > \verbatim */
655 /* >          IL is INTEGER */
656 /* >          If RANGE='I', the index of the */
657 /* >          smallest singular value to be returned. */
658 /* >          1 <= IL <= IU <= f2cmin(M,N), if f2cmin(M,N) > 0. */
659 /* >          Not referenced if RANGE = 'A' or 'V'. */
660 /* > \endverbatim */
661 /* > */
662 /* > \param[in] IU */
663 /* > \verbatim */
664 /* >          IU is INTEGER */
665 /* >          If RANGE='I', the index of the */
666 /* >          largest singular value to be returned. */
667 /* >          1 <= IL <= IU <= f2cmin(M,N), if f2cmin(M,N) > 0. */
668 /* >          Not referenced if RANGE = 'A' or 'V'. */
669 /* > \endverbatim */
670 /* > */
671 /* > \param[out] NS */
672 /* > \verbatim */
673 /* >          NS is INTEGER */
674 /* >          The total number of singular values found.  0 <= NS <= N. */
675 /* >          If RANGE = 'A', NS = N, and if RANGE = 'I', NS = IU-IL+1. */
676 /* > \endverbatim */
677 /* > */
678 /* > \param[out] S */
679 /* > \verbatim */
680 /* >          S is REAL array, dimension (N) */
681 /* >          The first NS elements contain the selected singular values in */
682 /* >          ascending order. */
683 /* > \endverbatim */
684 /* > */
685 /* > \param[out] Z */
686 /* > \verbatim */
687 /* >          Z is REAL array, dimension (2*N,K) */
688 /* >          If JOBZ = 'V', then if INFO = 0 the first NS columns of Z */
689 /* >          contain the singular vectors of the matrix B corresponding to */
690 /* >          the selected singular values, with U in rows 1 to N and V */
691 /* >          in rows N+1 to N*2, i.e. */
692 /* >          Z = [ U ] */
693 /* >              [ V ] */
694 /* >          If JOBZ = 'N', then Z is not referenced. */
695 /* >          Note: The user must ensure that at least K = NS+1 columns are */
696 /* >          supplied in the array Z; if RANGE = 'V', the exact value of */
697 /* >          NS is not known in advance and an upper bound must be used. */
698 /* > \endverbatim */
699 /* > */
700 /* > \param[in] LDZ */
701 /* > \verbatim */
702 /* >          LDZ is INTEGER */
703 /* >          The leading dimension of the array Z. LDZ >= 1, and if */
704 /* >          JOBZ = 'V', LDZ >= f2cmax(2,N*2). */
705 /* > \endverbatim */
706 /* > */
707 /* > \param[out] WORK */
708 /* > \verbatim */
709 /* >          WORK is REAL array, dimension (14*N) */
710 /* > \endverbatim */
711 /* > */
712 /* > \param[out] IWORK */
713 /* > \verbatim */
714 /* >          IWORK is INTEGER array, dimension (12*N) */
715 /* >          If JOBZ = 'V', then if INFO = 0, the first NS elements of */
716 /* >          IWORK are zero. If INFO > 0, then IWORK contains the indices */
717 /* >          of the eigenvectors that failed to converge in DSTEVX. */
718 /* > \endverbatim */
719 /* > */
720 /* > \param[out] INFO */
721 /* > \verbatim */
722 /* >          INFO is INTEGER */
723 /* >          = 0:  successful exit */
724 /* >          < 0:  if INFO = -i, the i-th argument had an illegal value */
725 /* >          > 0:  if INFO = i, then i eigenvectors failed to converge */
726 /* >                   in SSTEVX. The indices of the eigenvectors */
727 /* >                   (as returned by SSTEVX) are stored in the */
728 /* >                   array IWORK. */
729 /* >                if INFO = N*2 + 1, an internal error occurred. */
730 /* > \endverbatim */
731
732 /*  Authors: */
733 /*  ======== */
734
735 /* > \author Univ. of Tennessee */
736 /* > \author Univ. of California Berkeley */
737 /* > \author Univ. of Colorado Denver */
738 /* > \author NAG Ltd. */
739
740 /* > \date June 2016 */
741
742 /* > \ingroup realOTHEReigen */
743
744 /*  ===================================================================== */
745 /* Subroutine */ int sbdsvdx_(char *uplo, char *jobz, char *range, integer *n,
746          real *d__, real *e, real *vl, real *vu, integer *il, integer *iu, 
747         integer *ns, real *s, real *z__, integer *ldz, real *work, integer *
748         iwork, integer *info)
749 {
750     /* System generated locals */
751     integer z_dim1, z_offset, i__1, i__2, i__3, i__4, i__5;
752     real r__1, r__2, r__3, r__4;
753     doublereal d__1;
754
755     /* Local variables */
756     real emin;
757     integer ntgk;
758     real smin, smax;
759     extern real sdot_(integer *, real *, integer *, real *, integer *);
760     real nrmu, nrmv;
761     logical sveq0;
762     extern real snrm2_(integer *, real *, integer *);
763     integer i__, idbeg, j, k;
764     real sqrt2;
765     integer idend, isbeg;
766     extern logical lsame_(char *, char *);
767     integer idtgk, ietgk;
768     extern /* Subroutine */ int sscal_(integer *, real *, real *, integer *);
769     integer iltgk, itemp, icolz;
770     logical allsv;
771     integer idptr;
772     logical indsv;
773     integer ieptr, iutgk;
774     real vltgk;
775     logical lower;
776     real zjtji;
777     logical split, valsv;
778     integer isplt;
779     real ortol, vutgk;
780     extern /* Subroutine */ int scopy_(integer *, real *, integer *, real *, 
781             integer *), sswap_(integer *, real *, integer *, real *, integer *
782             );
783     logical wantz;
784     char rngvx[1];
785     integer irowu, irowv;
786     extern /* Subroutine */ int saxpy_(integer *, real *, real *, integer *, 
787             real *, integer *);
788     integer irowz, iifail;
789     real mu;
790     extern real slamch_(char *);
791     extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
792     extern integer isamax_(integer *, real *, integer *);
793     real abstol;
794     extern /* Subroutine */ int slaset_(char *, integer *, integer *, real *, 
795             real *, real *, integer *);
796     real thresh;
797     integer iiwork;
798     extern /* Subroutine */ int mecago_(), sstevx_(char *, char *, 
799             integer *, real *, real *, real *, real *, integer *, integer *, 
800             real *, integer *, real *, real *, integer *, real *, integer *, 
801             integer *, integer *);
802     real eps;
803     integer nsl;
804     real tol, ulp;
805     integer nru, nrv;
806
807
808 /*  -- LAPACK driver routine (version 3.8.0) -- */
809 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
810 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
811 /*     November 2017 */
812
813
814 /*  ===================================================================== */
815
816
817 /*     Test the input parameters. */
818
819     /* Parameter adjustments */
820     --d__;
821     --e;
822     --s;
823     z_dim1 = *ldz;
824     z_offset = 1 + z_dim1 * 1;
825     z__ -= z_offset;
826     --work;
827     --iwork;
828
829     /* Function Body */
830     allsv = lsame_(range, "A");
831     valsv = lsame_(range, "V");
832     indsv = lsame_(range, "I");
833     wantz = lsame_(jobz, "V");
834     lower = lsame_(uplo, "L");
835
836     *info = 0;
837     if (! lsame_(uplo, "U") && ! lower) {
838         *info = -1;
839     } else if (! (wantz || lsame_(jobz, "N"))) {
840         *info = -2;
841     } else if (! (allsv || valsv || indsv)) {
842         *info = -3;
843     } else if (*n < 0) {
844         *info = -4;
845     } else if (*n > 0) {
846         if (valsv) {
847             if (*vl < 0.f) {
848                 *info = -7;
849             } else if (*vu <= *vl) {
850                 *info = -8;
851             }
852         } else if (indsv) {
853             if (*il < 1 || *il > f2cmax(1,*n)) {
854                 *info = -9;
855             } else if (*iu < f2cmin(*n,*il) || *iu > *n) {
856                 *info = -10;
857             }
858         }
859     }
860     if (*info == 0) {
861         if (*ldz < 1 || wantz && *ldz < *n << 1) {
862             *info = -14;
863         }
864     }
865
866     if (*info != 0) {
867         i__1 = -(*info);
868         xerbla_("SBDSVDX", &i__1, (ftnlen)7);
869         return 0;
870     }
871
872 /*     Quick return if possible (N.LE.1) */
873
874     *ns = 0;
875     if (*n == 0) {
876         return 0;
877     }
878
879     if (*n == 1) {
880         if (allsv || indsv) {
881             *ns = 1;
882             s[1] = abs(d__[1]);
883         } else {
884             if (*vl < abs(d__[1]) && *vu >= abs(d__[1])) {
885                 *ns = 1;
886                 s[1] = abs(d__[1]);
887             }
888         }
889         if (wantz) {
890             z__[z_dim1 + 1] = r_sign(&c_b10, &d__[1]);
891             z__[z_dim1 + 2] = 1.f;
892         }
893         return 0;
894     }
895
896     abstol = slamch_("Safe Minimum") * 2;
897     ulp = slamch_("Precision");
898     eps = slamch_("Epsilon");
899     sqrt2 = sqrt(2.f);
900     ortol = sqrt(ulp);
901
902 /*     Criterion for splitting is taken from SBDSQR when singular */
903 /*     values are computed to relative accuracy TOL. (See J. Demmel and */
904 /*     W. Kahan, Accurate singular values of bidiagonal matrices, SIAM */
905 /*     J. Sci. and Stat. Comput., 11:873–912, 1990.) */
906
907 /* Computing MAX */
908 /* Computing MIN */
909     d__1 = (doublereal) eps;
910     r__3 = 100.f, r__4 = pow_dd(&d__1, &c_b14);
911     r__1 = 10.f, r__2 = f2cmin(r__3,r__4);
912     tol = f2cmax(r__1,r__2) * eps;
913
914 /*     Compute approximate maximum, minimum singular values. */
915
916     i__ = isamax_(n, &d__[1], &c__1);
917     smax = (r__1 = d__[i__], abs(r__1));
918     i__1 = *n - 1;
919     i__ = isamax_(&i__1, &e[1], &c__1);
920 /* Computing MAX */
921     r__2 = smax, r__3 = (r__1 = e[i__], abs(r__1));
922     smax = f2cmax(r__2,r__3);
923
924 /*     Compute threshold for neglecting D's and E's. */
925
926     smin = abs(d__[1]);
927     if (smin != 0.f) {
928         mu = smin;
929         i__1 = *n;
930         for (i__ = 2; i__ <= i__1; ++i__) {
931             mu = (r__2 = d__[i__], abs(r__2)) * (mu / (mu + (r__1 = e[i__ - 1]
932                     , abs(r__1))));
933             smin = f2cmin(smin,mu);
934             if (smin == 0.f) {
935                 myexit_();
936             }
937         }
938     }
939     smin /= sqrt((real) (*n));
940     thresh = tol * smin;
941
942 /*     Check for zeros in D and E (splits), i.e. submatrices. */
943
944     i__1 = *n - 1;
945     for (i__ = 1; i__ <= i__1; ++i__) {
946         if ((r__1 = d__[i__], abs(r__1)) <= thresh) {
947             d__[i__] = 0.f;
948         }
949         if ((r__1 = e[i__], abs(r__1)) <= thresh) {
950             e[i__] = 0.f;
951         }
952     }
953     if ((r__1 = d__[*n], abs(r__1)) <= thresh) {
954         d__[*n] = 0.f;
955     }
956
957 /*     Pointers for arrays used by SSTEVX. */
958
959     idtgk = 1;
960     ietgk = idtgk + (*n << 1);
961     itemp = ietgk + (*n << 1);
962     iifail = 1;
963     iiwork = iifail + (*n << 1);
964
965 /*     Set RNGVX, which corresponds to RANGE for SSTEVX in TGK mode. */
966 /*     VL,VU or IL,IU are redefined to conform to implementation a) */
967 /*     described in the leading comments. */
968
969     iltgk = 0;
970     iutgk = 0;
971     vltgk = 0.f;
972     vutgk = 0.f;
973
974     if (allsv) {
975
976 /*        All singular values will be found. We aim at -s (see */
977 /*        leading comments) with RNGVX = 'I'. IL and IU are set */
978 /*        later (as ILTGK and IUTGK) according to the dimension */
979 /*        of the active submatrix. */
980
981         *(unsigned char *)rngvx = 'I';
982         if (wantz) {
983             i__1 = *n << 1;
984             i__2 = *n + 1;
985             slaset_("F", &i__1, &i__2, &c_b19, &c_b19, &z__[z_offset], ldz);
986         }
987     } else if (valsv) {
988
989 /*        Find singular values in a half-open interval. We aim */
990 /*        at -s (see leading comments) and we swap VL and VU */
991 /*        (as VUTGK and VLTGK), changing their signs. */
992
993         *(unsigned char *)rngvx = 'V';
994         vltgk = -(*vu);
995         vutgk = -(*vl);
996         i__1 = idtgk + (*n << 1) - 1;
997         for (i__ = idtgk; i__ <= i__1; ++i__) {
998             work[i__] = 0.f;
999         }
1000 /*         WORK( IDTGK:IDTGK+2*N-1 ) = ZERO */
1001         scopy_(n, &d__[1], &c__1, &work[ietgk], &c__2);
1002         i__1 = *n - 1;
1003         scopy_(&i__1, &e[1], &c__1, &work[ietgk + 1], &c__2);
1004         i__1 = *n << 1;
1005         sstevx_("N", "V", &i__1, &work[idtgk], &work[ietgk], &vltgk, &vutgk, &
1006                 iltgk, &iltgk, &abstol, ns, &s[1], &z__[z_offset], ldz, &work[
1007                 itemp], &iwork[iiwork], &iwork[iifail], info);
1008         if (*ns == 0) {
1009             return 0;
1010         } else {
1011             if (wantz) {
1012                 i__1 = *n << 1;
1013                 slaset_("F", &i__1, ns, &c_b19, &c_b19, &z__[z_offset], ldz);
1014             }
1015         }
1016     } else if (indsv) {
1017
1018 /*        Find the IL-th through the IU-th singular values. We aim */
1019 /*        at -s (see leading comments) and indices are mapped into */
1020 /*        values, therefore mimicking SSTEBZ, where */
1021
1022 /*        GL = GL - FUDGE*TNORM*ULP*N - FUDGE*TWO*PIVMIN */
1023 /*        GU = GU + FUDGE*TNORM*ULP*N + FUDGE*PIVMIN */
1024
1025         iltgk = *il;
1026         iutgk = *iu;
1027         *(unsigned char *)rngvx = 'V';
1028         i__1 = idtgk + (*n << 1) - 1;
1029         for (i__ = idtgk; i__ <= i__1; ++i__) {
1030             work[i__] = 0.f;
1031         }
1032 /*         WORK( IDTGK:IDTGK+2*N-1 ) = ZERO */
1033         scopy_(n, &d__[1], &c__1, &work[ietgk], &c__2);
1034         i__1 = *n - 1;
1035         scopy_(&i__1, &e[1], &c__1, &work[ietgk + 1], &c__2);
1036         i__1 = *n << 1;
1037         sstevx_("N", "I", &i__1, &work[idtgk], &work[ietgk], &vltgk, &vltgk, &
1038                 iltgk, &iltgk, &abstol, ns, &s[1], &z__[z_offset], ldz, &work[
1039                 itemp], &iwork[iiwork], &iwork[iifail], info);
1040         vltgk = s[1] - smax * 2.f * ulp * *n;
1041         i__1 = idtgk + (*n << 1) - 1;
1042         for (i__ = idtgk; i__ <= i__1; ++i__) {
1043             work[i__] = 0.f;
1044         }
1045 /*         WORK( IDTGK:IDTGK+2*N-1 ) = ZERO */
1046         scopy_(n, &d__[1], &c__1, &work[ietgk], &c__2);
1047         i__1 = *n - 1;
1048         scopy_(&i__1, &e[1], &c__1, &work[ietgk + 1], &c__2);
1049         i__1 = *n << 1;
1050         sstevx_("N", "I", &i__1, &work[idtgk], &work[ietgk], &vutgk, &vutgk, &
1051                 iutgk, &iutgk, &abstol, ns, &s[1], &z__[z_offset], ldz, &work[
1052                 itemp], &iwork[iiwork], &iwork[iifail], info);
1053         vutgk = s[1] + smax * 2.f * ulp * *n;
1054         vutgk = f2cmin(vutgk,0.f);
1055
1056 /*        If VLTGK=VUTGK, SSTEVX returns an error message, */
1057 /*        so if needed we change VUTGK slightly. */
1058
1059         if (vltgk == vutgk) {
1060             vltgk -= tol;
1061         }
1062
1063         if (wantz) {
1064             i__1 = *n << 1;
1065             i__2 = *iu - *il + 1;
1066             slaset_("F", &i__1, &i__2, &c_b19, &c_b19, &z__[z_offset], ldz);
1067         }
1068     }
1069
1070 /*     Initialize variables and pointers for S, Z, and WORK. */
1071
1072 /*     NRU, NRV: number of rows in U and V for the active submatrix */
1073 /*     IDBEG, ISBEG: offsets for the entries of D and S */
1074 /*     IROWZ, ICOLZ: offsets for the rows and columns of Z */
1075 /*     IROWU, IROWV: offsets for the rows of U and V */
1076
1077     *ns = 0;
1078     nru = 0;
1079     nrv = 0;
1080     idbeg = 1;
1081     isbeg = 1;
1082     irowz = 1;
1083     icolz = 1;
1084     irowu = 2;
1085     irowv = 1;
1086     split = FALSE_;
1087     sveq0 = FALSE_;
1088
1089 /*     Form the tridiagonal TGK matrix. */
1090
1091     i__1 = *n;
1092     for (i__ = 1; i__ <= i__1; ++i__) {
1093         s[i__] = 0.f;
1094     }
1095 /*      S( 1:N ) = ZERO */
1096     work[ietgk + (*n << 1) - 1] = 0.f;
1097     i__1 = idtgk + (*n << 1) - 1;
1098     for (i__ = idtgk; i__ <= i__1; ++i__) {
1099         work[i__] = 0.f;
1100     }
1101 /*      WORK( IDTGK:IDTGK+2*N-1 ) = ZERO */
1102     scopy_(n, &d__[1], &c__1, &work[ietgk], &c__2);
1103     i__1 = *n - 1;
1104     scopy_(&i__1, &e[1], &c__1, &work[ietgk + 1], &c__2);
1105
1106
1107 /*     Check for splits in two levels, outer level */
1108 /*     in E and inner level in D. */
1109
1110     i__1 = *n << 1;
1111     for (ieptr = 2; ieptr <= i__1; ieptr += 2) {
1112         if (work[ietgk + ieptr - 1] == 0.f) {
1113
1114 /*           Split in E (this piece of B is square) or bottom */
1115 /*           of the (input bidiagonal) matrix. */
1116
1117             isplt = idbeg;
1118             idend = ieptr - 1;
1119             i__2 = idend;
1120             for (idptr = idbeg; idptr <= i__2; idptr += 2) {
1121                 if (work[ietgk + idptr - 1] == 0.f) {
1122
1123 /*                 Split in D (rectangular submatrix). Set the number */
1124 /*                 of rows in U and V (NRU and NRV) accordingly. */
1125
1126                     if (idptr == idbeg) {
1127
1128 /*                    D=0 at the top. */
1129
1130                         sveq0 = TRUE_;
1131                         if (idbeg == idend) {
1132                             nru = 1;
1133                             nrv = 1;
1134                         }
1135                     } else if (idptr == idend) {
1136
1137 /*                    D=0 at the bottom. */
1138
1139                         sveq0 = TRUE_;
1140                         nru = (idend - isplt) / 2 + 1;
1141                         nrv = nru;
1142                         if (isplt != idbeg) {
1143                             ++nru;
1144                         }
1145                     } else {
1146                         if (isplt == idbeg) {
1147
1148 /*                       Split: top rectangular submatrix. */
1149
1150                             nru = (idptr - idbeg) / 2;
1151                             nrv = nru + 1;
1152                         } else {
1153
1154 /*                       Split: middle square submatrix. */
1155
1156                             nru = (idptr - isplt) / 2 + 1;
1157                             nrv = nru;
1158                         }
1159                     }
1160                 } else if (idptr == idend) {
1161
1162 /*                 Last entry of D in the active submatrix. */
1163
1164                     if (isplt == idbeg) {
1165
1166 /*                    No split (trivial case). */
1167
1168                         nru = (idend - idbeg) / 2 + 1;
1169                         nrv = nru;
1170                     } else {
1171
1172 /*                    Split: bottom rectangular submatrix. */
1173
1174                         nrv = (idend - isplt) / 2 + 1;
1175                         nru = nrv + 1;
1176                     }
1177                 }
1178
1179                 ntgk = nru + nrv;
1180
1181                 if (ntgk > 0) {
1182
1183 /*                 Compute eigenvalues/vectors of the active */
1184 /*                 submatrix according to RANGE: */
1185 /*                 if RANGE='A' (ALLSV) then RNGVX = 'I' */
1186 /*                 if RANGE='V' (VALSV) then RNGVX = 'V' */
1187 /*                 if RANGE='I' (INDSV) then RNGVX = 'V' */
1188
1189                     iltgk = 1;
1190                     iutgk = ntgk / 2;
1191                     if (allsv || vutgk == 0.f) {
1192                         if (sveq0 || smin < eps || ntgk % 2 > 0) {
1193 /*                        Special case: eigenvalue equal to zero or very */
1194 /*                        small, additional eigenvector is needed. */
1195                             ++iutgk;
1196                         }
1197                     }
1198
1199 /*                 Workspace needed by SSTEVX: */
1200 /*                 WORK( ITEMP: ): 2*5*NTGK */
1201 /*                 IWORK( 1: ): 2*6*NTGK */
1202
1203                     sstevx_(jobz, rngvx, &ntgk, &work[idtgk + isplt - 1], &
1204                             work[ietgk + isplt - 1], &vltgk, &vutgk, &iltgk, &
1205                             iutgk, &abstol, &nsl, &s[isbeg], &z__[irowz + 
1206                             icolz * z_dim1], ldz, &work[itemp], &iwork[iiwork]
1207                             , &iwork[iifail], info);
1208                     if (*info != 0) {
1209 /*                    Exit with the error code from SSTEVX. */
1210                         return 0;
1211                     }
1212                     emin = (r__1 = s[isbeg], abs(r__1));
1213                     i__3 = isbeg + nsl - 1;
1214                     for (i__ = isbeg; i__ <= i__3; ++i__) {
1215                         if ((r__1 = s[i__], abs(r__1)) > emin) {
1216                             emin = s[i__];
1217                         }
1218                     }
1219 /*                  EMIN = ABS( MAXVAL( S( ISBEG:ISBEG+NSL-1 ) ) ) */
1220
1221                     if (nsl > 0 && wantz) {
1222
1223 /*                    Normalize u=Z([2,4,...],:) and v=Z([1,3,...],:), */
1224 /*                    changing the sign of v as discussed in the leading */
1225 /*                    comments. The norms of u and v may be (slightly) */
1226 /*                    different from 1/sqrt(2) if the corresponding */
1227 /*                    eigenvalues are very small or too close. We check */
1228 /*                    those norms and, if needed, reorthogonalize the */
1229 /*                    vectors. */
1230
1231                         if (nsl > 1 && vutgk == 0.f && ntgk % 2 == 0 && emin 
1232                                 == 0.f && ! split) {
1233
1234 /*                       D=0 at the top or bottom of the active submatrix: */
1235 /*                       one eigenvalue is equal to zero; concatenate the */
1236 /*                       eigenvectors corresponding to the two smallest */
1237 /*                       eigenvalues. */
1238
1239                             i__3 = irowz + ntgk - 1;
1240                             for (i__ = irowz; i__ <= i__3; ++i__) {
1241                                 z__[i__ + (icolz + nsl - 2) * z_dim1] += z__[
1242                                         i__ + (icolz + nsl - 1) * z_dim1];
1243                                 z__[i__ + (icolz + nsl - 1) * z_dim1] = 0.f;
1244                             }
1245 /*                        Z( IROWZ:IROWZ+NTGK-1,ICOLZ+NSL-2 ) = */
1246 /*     $                  Z( IROWZ:IROWZ+NTGK-1,ICOLZ+NSL-2 ) + */
1247 /*     $                  Z( IROWZ:IROWZ+NTGK-1,ICOLZ+NSL-1 ) */
1248 /*                        Z( IROWZ:IROWZ+NTGK-1,ICOLZ+NSL-1 ) = */
1249 /*     $                  ZERO */
1250 /*                       IF( IUTGK*2.GT.NTGK ) THEN */
1251 /*                          Eigenvalue equal to zero or very small. */
1252 /*                          NSL = NSL - 1 */
1253 /*                       END IF */
1254                         }
1255
1256 /* Computing MIN */
1257                         i__4 = nsl - 1, i__5 = nru - 1;
1258                         i__3 = f2cmin(i__4,i__5);
1259                         for (i__ = 0; i__ <= i__3; ++i__) {
1260                             nrmu = snrm2_(&nru, &z__[irowu + (icolz + i__) * 
1261                                     z_dim1], &c__2);
1262                             if (nrmu == 0.f) {
1263                                 *info = (*n << 1) + 1;
1264                                 return 0;
1265                             }
1266                             r__1 = 1.f / nrmu;
1267                             sscal_(&nru, &r__1, &z__[irowu + (icolz + i__) * 
1268                                     z_dim1], &c__2);
1269                             if (nrmu != 1.f && (r__1 = nrmu - ortol, abs(r__1)
1270                                     ) * sqrt2 > 1.f) {
1271                                 i__4 = i__ - 1;
1272                                 for (j = 0; j <= i__4; ++j) {
1273                                     zjtji = -sdot_(&nru, &z__[irowu + (icolz 
1274                                             + j) * z_dim1], &c__2, &z__[irowu 
1275                                             + (icolz + i__) * z_dim1], &c__2);
1276                                     saxpy_(&nru, &zjtji, &z__[irowu + (icolz 
1277                                             + j) * z_dim1], &c__2, &z__[irowu 
1278                                             + (icolz + i__) * z_dim1], &c__2);
1279                                 }
1280                                 nrmu = snrm2_(&nru, &z__[irowu + (icolz + i__)
1281                                          * z_dim1], &c__2);
1282                                 r__1 = 1.f / nrmu;
1283                                 sscal_(&nru, &r__1, &z__[irowu + (icolz + i__)
1284                                          * z_dim1], &c__2);
1285                             }
1286                         }
1287 /* Computing MIN */
1288                         i__4 = nsl - 1, i__5 = nrv - 1;
1289                         i__3 = f2cmin(i__4,i__5);
1290                         for (i__ = 0; i__ <= i__3; ++i__) {
1291                             nrmv = snrm2_(&nrv, &z__[irowv + (icolz + i__) * 
1292                                     z_dim1], &c__2);
1293                             if (nrmv == 0.f) {
1294                                 *info = (*n << 1) + 1;
1295                                 return 0;
1296                             }
1297                             r__1 = -1.f / nrmv;
1298                             sscal_(&nrv, &r__1, &z__[irowv + (icolz + i__) * 
1299                                     z_dim1], &c__2);
1300                             if (nrmv != 1.f && (r__1 = nrmv - ortol, abs(r__1)
1301                                     ) * sqrt2 > 1.f) {
1302                                 i__4 = i__ - 1;
1303                                 for (j = 0; j <= i__4; ++j) {
1304                                     zjtji = -sdot_(&nrv, &z__[irowv + (icolz 
1305                                             + j) * z_dim1], &c__2, &z__[irowv 
1306                                             + (icolz + i__) * z_dim1], &c__2);
1307                                     saxpy_(&nru, &zjtji, &z__[irowv + (icolz 
1308                                             + j) * z_dim1], &c__2, &z__[irowv 
1309                                             + (icolz + i__) * z_dim1], &c__2);
1310                                 }
1311                                 nrmv = snrm2_(&nrv, &z__[irowv + (icolz + i__)
1312                                          * z_dim1], &c__2);
1313                                 r__1 = 1.f / nrmv;
1314                                 sscal_(&nrv, &r__1, &z__[irowv + (icolz + i__)
1315                                          * z_dim1], &c__2);
1316                             }
1317                         }
1318                         if (vutgk == 0.f && idptr < idend && ntgk % 2 > 0) {
1319
1320 /*                       D=0 in the middle of the active submatrix (one */
1321 /*                       eigenvalue is equal to zero): save the corresponding */
1322 /*                       eigenvector for later use (when bottom of the */
1323 /*                       active submatrix is reached). */
1324
1325                             split = TRUE_;
1326                             i__3 = irowz + ntgk - 1;
1327                             for (i__ = irowz; i__ <= i__3; ++i__) {
1328                                 z__[i__ + (*n + 1) * z_dim1] = z__[i__ + (*ns 
1329                                         + nsl) * z_dim1];
1330                                 z__[i__ + (*ns + nsl) * z_dim1] = 0.f;
1331                             }
1332 /*                        Z( IROWZ:IROWZ+NTGK-1,N+1 ) = */
1333 /*     $                     Z( IROWZ:IROWZ+NTGK-1,NS+NSL ) */
1334 /*                        Z( IROWZ:IROWZ+NTGK-1,NS+NSL ) = */
1335 /*     $                     ZERO */
1336                         }
1337                     }
1338
1339 /* ** WANTZ **! */
1340                     nsl = f2cmin(nsl,nru);
1341                     sveq0 = FALSE_;
1342
1343 /*                 Absolute values of the eigenvalues of TGK. */
1344
1345                     i__3 = nsl - 1;
1346                     for (i__ = 0; i__ <= i__3; ++i__) {
1347                         s[isbeg + i__] = (r__1 = s[isbeg + i__], abs(r__1));
1348                     }
1349
1350 /*                 Update pointers for TGK, S and Z. */
1351
1352                     isbeg += nsl;
1353                     irowz += ntgk;
1354                     icolz += nsl;
1355                     irowu = irowz;
1356                     irowv = irowz + 1;
1357                     isplt = idptr + 1;
1358                     *ns += nsl;
1359                     nru = 0;
1360                     nrv = 0;
1361                 }
1362 /* ** NTGK.GT.0 **! */
1363                 if (irowz < *n << 1 && wantz) {
1364                     i__3 = irowz - 1;
1365                     for (i__ = 1; i__ <= i__3; ++i__) {
1366                         z__[i__ + icolz * z_dim1] = 0.f;
1367                     }
1368 /*                       Z( 1:IROWZ-1, ICOLZ ) = ZERO */
1369                 }
1370             }
1371 /* ** IDPTR loop **! */
1372             if (split && wantz) {
1373
1374 /*              Bring back eigenvector corresponding */
1375 /*              to eigenvalue equal to zero. */
1376
1377                 i__2 = idend - ntgk + 1;
1378                 for (i__ = idbeg; i__ <= i__2; ++i__) {
1379                     z__[i__ + (isbeg - 1) * z_dim1] += z__[i__ + (*n + 1) * 
1380                             z_dim1];
1381                     z__[i__ + (*n + 1) * z_dim1] = 0.f;
1382                 }
1383 /*               Z( IDBEG:IDEND-NTGK+1,ISBEG-1 ) = */
1384 /*     $         Z( IDBEG:IDEND-NTGK+1,ISBEG-1 ) + */
1385 /*     $         Z( IDBEG:IDEND-NTGK+1,N+1 ) */
1386 /*               Z( IDBEG:IDEND-NTGK+1,N+1 ) = 0 */
1387             }
1388             --irowv;
1389             ++irowu;
1390             idbeg = ieptr + 1;
1391             sveq0 = FALSE_;
1392             split = FALSE_;
1393         }
1394 /* ** Check for split in E **! */
1395     }
1396
1397 /*     Sort the singular values into decreasing order (insertion sort on */
1398 /*     singular values, but only one transposition per singular vector) */
1399
1400 /* ** IEPTR loop **! */
1401     i__1 = *ns - 1;
1402     for (i__ = 1; i__ <= i__1; ++i__) {
1403         k = 1;
1404         smin = s[1];
1405         i__2 = *ns + 1 - i__;
1406         for (j = 2; j <= i__2; ++j) {
1407             if (s[j] <= smin) {
1408                 k = j;
1409                 smin = s[j];
1410             }
1411         }
1412         if (k != *ns + 1 - i__) {
1413             s[k] = s[*ns + 1 - i__];
1414             s[*ns + 1 - i__] = smin;
1415             if (wantz) {
1416                 i__2 = *n << 1;
1417                 sswap_(&i__2, &z__[k * z_dim1 + 1], &c__1, &z__[(*ns + 1 - 
1418                         i__) * z_dim1 + 1], &c__1);
1419             }
1420         }
1421     }
1422
1423 /*     If RANGE=I, check for singular values/vectors to be discarded. */
1424
1425     if (indsv) {
1426         k = *iu - *il + 1;
1427         if (k < *ns) {
1428             i__1 = *ns;
1429             for (i__ = k + 1; i__ <= i__1; ++i__) {
1430                 s[i__] = 0.f;
1431             }
1432 /*            S( K+1:NS ) = ZERO */
1433             if (wantz) {
1434                 i__1 = *n << 1;
1435                 for (i__ = 1; i__ <= i__1; ++i__) {
1436                     i__2 = *ns;
1437                     for (j = k + 1; j <= i__2; ++j) {
1438                         z__[i__ + j * z_dim1] = 0.f;
1439                     }
1440                 }
1441 /*           Z( 1:N*2,K+1:NS ) = ZERO */
1442             }
1443             *ns = k;
1444         }
1445     }
1446
1447 /*     Reorder Z: U = Z( 1:N,1:NS ), V = Z( N+1:N*2,1:NS ). */
1448 /*     If B is a lower diagonal, swap U and V. */
1449
1450     if (wantz) {
1451         i__1 = *ns;
1452         for (i__ = 1; i__ <= i__1; ++i__) {
1453             i__2 = *n << 1;
1454             scopy_(&i__2, &z__[i__ * z_dim1 + 1], &c__1, &work[1], &c__1);
1455             if (lower) {
1456                 scopy_(n, &work[2], &c__2, &z__[*n + 1 + i__ * z_dim1], &c__1)
1457                         ;
1458                 scopy_(n, &work[1], &c__2, &z__[i__ * z_dim1 + 1], &c__1);
1459             } else {
1460                 scopy_(n, &work[2], &c__2, &z__[i__ * z_dim1 + 1], &c__1);
1461                 scopy_(n, &work[1], &c__2, &z__[*n + 1 + i__ * z_dim1], &c__1)
1462                         ;
1463             }
1464         }
1465     }
1466
1467     return 0;
1468
1469 /*     End of SBDSVDX */
1470
1471 } /* sbdsvdx_ */
1472