C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / dbdsvdx.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 doublereal c_b10 = 1.;
516 static doublereal c_b14 = -.125;
517 static integer c__1 = 1;
518 static doublereal c_b19 = 0.;
519 static integer c__2 = 2;
520
521 /* > \brief \b DBDSVDX */
522
523 /*  =========== DOCUMENTATION =========== */
524
525 /* Online html documentation available at */
526 /*            http://www.netlib.org/lapack/explore-html/ */
527
528 /* > \htmlonly */
529 /* > Download DBDSVDX + dependencies */
530 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dbdsvdx
531 .f"> */
532 /* > [TGZ]</a> */
533 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dbdsvdx
534 .f"> */
535 /* > [ZIP]</a> */
536 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dbdsvdx
537 .f"> */
538 /* > [TXT]</a> */
539 /* > \endhtmlonly */
540
541 /*  Definition: */
542 /*  =========== */
543
544 /*     SUBROUTINE DBDSVDX( 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 /*      DOUBLE PRECISION   VL, VU */
550 /*      INTEGER            IWORK( * ) */
551 /*      DOUBLE PRECISION   D( * ), E( * ), S( * ), WORK( * ), */
552 /*                         Z( LDZ, * ) */
553
554 /* > \par Purpose: */
555 /*  ============= */
556 /* > */
557 /* > \verbatim */
558 /* > */
559 /* >  DBDSVDX 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 ], DBDSVDX 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 DGESVD/DGESDD) or b) compute s,v and reorder */
584 /* >  the values (and corresponding vectors). DBDSVDX implements a) by */
585 /* >  calling DSTEVX (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 DOUBLE PRECISION 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 DOUBLE PRECISION 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 DOUBLE PRECISION */
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 DOUBLE PRECISION */
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 DOUBLE PRECISION 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 DOUBLE PRECISION 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 DOUBLE PRECISION 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 DSTEVX. The indices of the eigenvectors */
727 /* >                   (as returned by DSTEVX) 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 doubleOTHEReigen */
743
744 /*  ===================================================================== */
745 /* Subroutine */ int dbdsvdx_(char *uplo, char *jobz, char *range, integer *n,
746          doublereal *d__, doublereal *e, doublereal *vl, doublereal *vu, 
747         integer *il, integer *iu, integer *ns, doublereal *s, doublereal *z__,
748          integer *ldz, doublereal *work, integer *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     doublereal d__1, d__2, d__3, d__4;
753
754     /* Local variables */
755     doublereal emin;
756     extern doublereal ddot_(integer *, doublereal *, integer *, doublereal *, 
757             integer *);
758     integer ntgk;
759     doublereal smin, smax, nrmu, nrmv;
760     extern doublereal dnrm2_(integer *, doublereal *, integer *);
761     logical sveq0;
762     integer i__, idbeg, j, k;
763     doublereal sqrt2;
764     integer idend;
765     extern /* Subroutine */ int dscal_(integer *, doublereal *, doublereal *, 
766             integer *);
767     integer isbeg;
768     extern logical lsame_(char *, char *);
769     integer idtgk, ietgk, iltgk, itemp;
770     extern /* Subroutine */ int dcopy_(integer *, doublereal *, integer *, 
771             doublereal *, integer *);
772     integer icolz;
773     logical allsv;
774     integer idptr;
775     logical indsv;
776     integer ieptr, iutgk;
777     extern /* Subroutine */ int daxpy_(integer *, doublereal *, doublereal *, 
778             integer *, doublereal *, integer *);
779     doublereal vltgk;
780     logical lower;
781     extern /* Subroutine */ int dswap_(integer *, doublereal *, integer *, 
782             doublereal *, integer *);
783     doublereal zjtji;
784     logical split, valsv;
785     integer isplt;
786     doublereal ortol, vutgk;
787     logical wantz;
788     char rngvx[1];
789     integer irowu, irowv, irowz;
790     extern doublereal dlamch_(char *);
791     integer iifail;
792     doublereal mu;
793     extern integer idamax_(integer *, doublereal *, integer *);
794     extern /* Subroutine */ int dlaset_(char *, integer *, integer *, 
795             doublereal *, doublereal *, doublereal *, integer *), 
796             xerbla_(char *, integer *, ftnlen);
797     doublereal abstol, thresh;
798     integer iiwork;
799     extern /* Subroutine */ int dstevx_(char *, char *, integer *, doublereal 
800             *, doublereal *, doublereal *, doublereal *, integer *, integer *,
801              doublereal *, integer *, doublereal *, doublereal *, integer *, 
802             doublereal *, integer *, integer *, integer *), 
803             mecago_();
804     doublereal eps;
805     integer nsl;
806     doublereal tol, ulp;
807     integer nru, nrv;
808
809
810 /*  -- LAPACK driver routine (version 3.8.0) -- */
811 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
812 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
813 /*     November 2017 */
814
815
816 /*  ===================================================================== */
817
818
819 /*     Test the input parameters. */
820
821     /* Parameter adjustments */
822     --d__;
823     --e;
824     --s;
825     z_dim1 = *ldz;
826     z_offset = 1 + z_dim1 * 1;
827     z__ -= z_offset;
828     --work;
829     --iwork;
830
831     /* Function Body */
832     allsv = lsame_(range, "A");
833     valsv = lsame_(range, "V");
834     indsv = lsame_(range, "I");
835     wantz = lsame_(jobz, "V");
836     lower = lsame_(uplo, "L");
837
838     *info = 0;
839     if (! lsame_(uplo, "U") && ! lower) {
840         *info = -1;
841     } else if (! (wantz || lsame_(jobz, "N"))) {
842         *info = -2;
843     } else if (! (allsv || valsv || indsv)) {
844         *info = -3;
845     } else if (*n < 0) {
846         *info = -4;
847     } else if (*n > 0) {
848         if (valsv) {
849             if (*vl < 0.) {
850                 *info = -7;
851             } else if (*vu <= *vl) {
852                 *info = -8;
853             }
854         } else if (indsv) {
855             if (*il < 1 || *il > f2cmax(1,*n)) {
856                 *info = -9;
857             } else if (*iu < f2cmin(*n,*il) || *iu > *n) {
858                 *info = -10;
859             }
860         }
861     }
862     if (*info == 0) {
863         if (*ldz < 1 || wantz && *ldz < *n << 1) {
864             *info = -14;
865         }
866     }
867
868     if (*info != 0) {
869         i__1 = -(*info);
870         xerbla_("DBDSVDX", &i__1, (ftnlen)7);
871         return 0;
872     }
873
874 /*     Quick return if possible (N.LE.1) */
875
876     *ns = 0;
877     if (*n == 0) {
878         return 0;
879     }
880
881     if (*n == 1) {
882         if (allsv || indsv) {
883             *ns = 1;
884             s[1] = abs(d__[1]);
885         } else {
886             if (*vl < abs(d__[1]) && *vu >= abs(d__[1])) {
887                 *ns = 1;
888                 s[1] = abs(d__[1]);
889             }
890         }
891         if (wantz) {
892             z__[z_dim1 + 1] = d_sign(&c_b10, &d__[1]);
893             z__[z_dim1 + 2] = 1.;
894         }
895         return 0;
896     }
897
898     abstol = dlamch_("Safe Minimum") * 2;
899     ulp = dlamch_("Precision");
900     eps = dlamch_("Epsilon");
901     sqrt2 = sqrt(2.);
902     ortol = sqrt(ulp);
903
904 /*     Criterion for splitting is taken from DBDSQR when singular */
905 /*     values are computed to relative accuracy TOL. (See J. Demmel and */
906 /*     W. Kahan, Accurate singular values of bidiagonal matrices, SIAM */
907 /*     J. Sci. and Stat. Comput., 11:873–912, 1990.) */
908
909 /* Computing MAX */
910 /* Computing MIN */
911     d__3 = 100., d__4 = pow_dd(&eps, &c_b14);
912     d__1 = 10., d__2 = f2cmin(d__3,d__4);
913     tol = f2cmax(d__1,d__2) * eps;
914
915 /*     Compute approximate maximum, minimum singular values. */
916
917     i__ = idamax_(n, &d__[1], &c__1);
918     smax = (d__1 = d__[i__], abs(d__1));
919     i__1 = *n - 1;
920     i__ = idamax_(&i__1, &e[1], &c__1);
921 /* Computing MAX */
922     d__2 = smax, d__3 = (d__1 = e[i__], abs(d__1));
923     smax = f2cmax(d__2,d__3);
924
925 /*     Compute threshold for neglecting D's and E's. */
926
927     smin = abs(d__[1]);
928     if (smin != 0.) {
929         mu = smin;
930         i__1 = *n;
931         for (i__ = 2; i__ <= i__1; ++i__) {
932             mu = (d__2 = d__[i__], abs(d__2)) * (mu / (mu + (d__1 = e[i__ - 1]
933                     , abs(d__1))));
934             smin = f2cmin(smin,mu);
935             if (smin == 0.) {
936                 myexit_();
937             }
938         }
939     }
940     smin /= sqrt((doublereal) (*n));
941     thresh = tol * smin;
942
943 /*     Check for zeros in D and E (splits), i.e. submatrices. */
944
945     i__1 = *n - 1;
946     for (i__ = 1; i__ <= i__1; ++i__) {
947         if ((d__1 = d__[i__], abs(d__1)) <= thresh) {
948             d__[i__] = 0.;
949         }
950         if ((d__1 = e[i__], abs(d__1)) <= thresh) {
951             e[i__] = 0.;
952         }
953     }
954     if ((d__1 = d__[*n], abs(d__1)) <= thresh) {
955         d__[*n] = 0.;
956     }
957
958 /*     Pointers for arrays used by DSTEVX. */
959
960     idtgk = 1;
961     ietgk = idtgk + (*n << 1);
962     itemp = ietgk + (*n << 1);
963     iifail = 1;
964     iiwork = iifail + (*n << 1);
965
966 /*     Set RNGVX, which corresponds to RANGE for DSTEVX in TGK mode. */
967 /*     VL,VU or IL,IU are redefined to conform to implementation a) */
968 /*     described in the leading comments. */
969
970     iltgk = 0;
971     iutgk = 0;
972     vltgk = 0.;
973     vutgk = 0.;
974
975     if (allsv) {
976
977 /*        All singular values will be found. We aim at -s (see */
978 /*        leading comments) with RNGVX = 'I'. IL and IU are set */
979 /*        later (as ILTGK and IUTGK) according to the dimension */
980 /*        of the active submatrix. */
981
982         *(unsigned char *)rngvx = 'I';
983         if (wantz) {
984             i__1 = *n << 1;
985             i__2 = *n + 1;
986             dlaset_("F", &i__1, &i__2, &c_b19, &c_b19, &z__[z_offset], ldz);
987         }
988     } else if (valsv) {
989
990 /*        Find singular values in a half-open interval. We aim */
991 /*        at -s (see leading comments) and we swap VL and VU */
992 /*        (as VUTGK and VLTGK), changing their signs. */
993
994         *(unsigned char *)rngvx = 'V';
995         vltgk = -(*vu);
996         vutgk = -(*vl);
997         i__1 = idtgk + (*n << 1) - 1;
998         for (i__ = idtgk; i__ <= i__1; ++i__) {
999             work[i__] = 0.;
1000         }
1001 /*         WORK( IDTGK:IDTGK+2*N-1 ) = ZERO */
1002         dcopy_(n, &d__[1], &c__1, &work[ietgk], &c__2);
1003         i__1 = *n - 1;
1004         dcopy_(&i__1, &e[1], &c__1, &work[ietgk + 1], &c__2);
1005         i__1 = *n << 1;
1006         dstevx_("N", "V", &i__1, &work[idtgk], &work[ietgk], &vltgk, &vutgk, &
1007                 iltgk, &iltgk, &abstol, ns, &s[1], &z__[z_offset], ldz, &work[
1008                 itemp], &iwork[iiwork], &iwork[iifail], info);
1009         if (*ns == 0) {
1010             return 0;
1011         } else {
1012             if (wantz) {
1013                 i__1 = *n << 1;
1014                 dlaset_("F", &i__1, ns, &c_b19, &c_b19, &z__[z_offset], ldz);
1015             }
1016         }
1017     } else if (indsv) {
1018
1019 /*        Find the IL-th through the IU-th singular values. We aim */
1020 /*        at -s (see leading comments) and indices are mapped into */
1021 /*        values, therefore mimicking DSTEBZ, where */
1022
1023 /*        GL = GL - FUDGE*TNORM*ULP*N - FUDGE*TWO*PIVMIN */
1024 /*        GU = GU + FUDGE*TNORM*ULP*N + FUDGE*PIVMIN */
1025
1026         iltgk = *il;
1027         iutgk = *iu;
1028         *(unsigned char *)rngvx = 'V';
1029         i__1 = idtgk + (*n << 1) - 1;
1030         for (i__ = idtgk; i__ <= i__1; ++i__) {
1031             work[i__] = 0.;
1032         }
1033 /*         WORK( IDTGK:IDTGK+2*N-1 ) = ZERO */
1034         dcopy_(n, &d__[1], &c__1, &work[ietgk], &c__2);
1035         i__1 = *n - 1;
1036         dcopy_(&i__1, &e[1], &c__1, &work[ietgk + 1], &c__2);
1037         i__1 = *n << 1;
1038         dstevx_("N", "I", &i__1, &work[idtgk], &work[ietgk], &vltgk, &vltgk, &
1039                 iltgk, &iltgk, &abstol, ns, &s[1], &z__[z_offset], ldz, &work[
1040                 itemp], &iwork[iiwork], &iwork[iifail], info);
1041         vltgk = s[1] - smax * 2. * ulp * *n;
1042         i__1 = idtgk + (*n << 1) - 1;
1043         for (i__ = idtgk; i__ <= i__1; ++i__) {
1044             work[i__] = 0.;
1045         }
1046 /*         WORK( IDTGK:IDTGK+2*N-1 ) = ZERO */
1047         dcopy_(n, &d__[1], &c__1, &work[ietgk], &c__2);
1048         i__1 = *n - 1;
1049         dcopy_(&i__1, &e[1], &c__1, &work[ietgk + 1], &c__2);
1050         i__1 = *n << 1;
1051         dstevx_("N", "I", &i__1, &work[idtgk], &work[ietgk], &vutgk, &vutgk, &
1052                 iutgk, &iutgk, &abstol, ns, &s[1], &z__[z_offset], ldz, &work[
1053                 itemp], &iwork[iiwork], &iwork[iifail], info);
1054         vutgk = s[1] + smax * 2. * ulp * *n;
1055         vutgk = f2cmin(vutgk,0.);
1056
1057 /*        If VLTGK=VUTGK, DSTEVX returns an error message, */
1058 /*        so if needed we change VUTGK slightly. */
1059
1060         if (vltgk == vutgk) {
1061             vltgk -= tol;
1062         }
1063
1064         if (wantz) {
1065             i__1 = *n << 1;
1066             i__2 = *iu - *il + 1;
1067             dlaset_("F", &i__1, &i__2, &c_b19, &c_b19, &z__[z_offset], ldz);
1068         }
1069     }
1070
1071 /*     Initialize variables and pointers for S, Z, and WORK. */
1072
1073 /*     NRU, NRV: number of rows in U and V for the active submatrix */
1074 /*     IDBEG, ISBEG: offsets for the entries of D and S */
1075 /*     IROWZ, ICOLZ: offsets for the rows and columns of Z */
1076 /*     IROWU, IROWV: offsets for the rows of U and V */
1077
1078     *ns = 0;
1079     nru = 0;
1080     nrv = 0;
1081     idbeg = 1;
1082     isbeg = 1;
1083     irowz = 1;
1084     icolz = 1;
1085     irowu = 2;
1086     irowv = 1;
1087     split = FALSE_;
1088     sveq0 = FALSE_;
1089
1090 /*     Form the tridiagonal TGK matrix. */
1091
1092     i__1 = *n;
1093     for (i__ = 1; i__ <= i__1; ++i__) {
1094         s[i__] = 0.;
1095     }
1096 /*      S( 1:N ) = ZERO */
1097     work[ietgk + (*n << 1) - 1] = 0.;
1098     i__1 = idtgk + (*n << 1) - 1;
1099     for (i__ = idtgk; i__ <= i__1; ++i__) {
1100         work[i__] = 0.;
1101     }
1102 /*      WORK( IDTGK:IDTGK+2*N-1 ) = ZERO */
1103     dcopy_(n, &d__[1], &c__1, &work[ietgk], &c__2);
1104     i__1 = *n - 1;
1105     dcopy_(&i__1, &e[1], &c__1, &work[ietgk + 1], &c__2);
1106
1107
1108 /*     Check for splits in two levels, outer level */
1109 /*     in E and inner level in D. */
1110
1111     i__1 = *n << 1;
1112     for (ieptr = 2; ieptr <= i__1; ieptr += 2) {
1113         if (work[ietgk + ieptr - 1] == 0.) {
1114
1115 /*           Split in E (this piece of B is square) or bottom */
1116 /*           of the (input bidiagonal) matrix. */
1117
1118             isplt = idbeg;
1119             idend = ieptr - 1;
1120             i__2 = idend;
1121             for (idptr = idbeg; idptr <= i__2; idptr += 2) {
1122                 if (work[ietgk + idptr - 1] == 0.) {
1123
1124 /*                 Split in D (rectangular submatrix). Set the number */
1125 /*                 of rows in U and V (NRU and NRV) accordingly. */
1126
1127                     if (idptr == idbeg) {
1128
1129 /*                    D=0 at the top. */
1130
1131                         sveq0 = TRUE_;
1132                         if (idbeg == idend) {
1133                             nru = 1;
1134                             nrv = 1;
1135                         }
1136                     } else if (idptr == idend) {
1137
1138 /*                    D=0 at the bottom. */
1139
1140                         sveq0 = TRUE_;
1141                         nru = (idend - isplt) / 2 + 1;
1142                         nrv = nru;
1143                         if (isplt != idbeg) {
1144                             ++nru;
1145                         }
1146                     } else {
1147                         if (isplt == idbeg) {
1148
1149 /*                       Split: top rectangular submatrix. */
1150
1151                             nru = (idptr - idbeg) / 2;
1152                             nrv = nru + 1;
1153                         } else {
1154
1155 /*                       Split: middle square submatrix. */
1156
1157                             nru = (idptr - isplt) / 2 + 1;
1158                             nrv = nru;
1159                         }
1160                     }
1161                 } else if (idptr == idend) {
1162
1163 /*                 Last entry of D in the active submatrix. */
1164
1165                     if (isplt == idbeg) {
1166
1167 /*                    No split (trivial case). */
1168
1169                         nru = (idend - idbeg) / 2 + 1;
1170                         nrv = nru;
1171                     } else {
1172
1173 /*                    Split: bottom rectangular submatrix. */
1174
1175                         nrv = (idend - isplt) / 2 + 1;
1176                         nru = nrv + 1;
1177                     }
1178                 }
1179
1180                 ntgk = nru + nrv;
1181
1182                 if (ntgk > 0) {
1183
1184 /*                 Compute eigenvalues/vectors of the active */
1185 /*                 submatrix according to RANGE: */
1186 /*                 if RANGE='A' (ALLSV) then RNGVX = 'I' */
1187 /*                 if RANGE='V' (VALSV) then RNGVX = 'V' */
1188 /*                 if RANGE='I' (INDSV) then RNGVX = 'V' */
1189
1190                     iltgk = 1;
1191                     iutgk = ntgk / 2;
1192                     if (allsv || vutgk == 0.) {
1193                         if (sveq0 || smin < eps || ntgk % 2 > 0) {
1194 /*                        Special case: eigenvalue equal to zero or very */
1195 /*                        small, additional eigenvector is needed. */
1196                             ++iutgk;
1197                         }
1198                     }
1199
1200 /*                 Workspace needed by DSTEVX: */
1201 /*                 WORK( ITEMP: ): 2*5*NTGK */
1202 /*                 IWORK( 1: ): 2*6*NTGK */
1203
1204                     dstevx_(jobz, rngvx, &ntgk, &work[idtgk + isplt - 1], &
1205                             work[ietgk + isplt - 1], &vltgk, &vutgk, &iltgk, &
1206                             iutgk, &abstol, &nsl, &s[isbeg], &z__[irowz + 
1207                             icolz * z_dim1], ldz, &work[itemp], &iwork[iiwork]
1208                             , &iwork[iifail], info);
1209                     if (*info != 0) {
1210 /*                    Exit with the error code from DSTEVX. */
1211                         return 0;
1212                     }
1213                     emin = (d__1 = s[isbeg], abs(d__1));
1214                     i__3 = isbeg + nsl - 1;
1215                     for (i__ = isbeg; i__ <= i__3; ++i__) {
1216                         if ((d__1 = s[i__], abs(d__1)) > emin) {
1217                             emin = s[i__];
1218                         }
1219                     }
1220 /*                  EMIN = ABS( MAXVAL( S( ISBEG:ISBEG+NSL-1 ) ) ) */
1221
1222                     if (nsl > 0 && wantz) {
1223
1224 /*                    Normalize u=Z([2,4,...],:) and v=Z([1,3,...],:), */
1225 /*                    changing the sign of v as discussed in the leading */
1226 /*                    comments. The norms of u and v may be (slightly) */
1227 /*                    different from 1/sqrt(2) if the corresponding */
1228 /*                    eigenvalues are very small or too close. We check */
1229 /*                    those norms and, if needed, reorthogonalize the */
1230 /*                    vectors. */
1231
1232                         if (nsl > 1 && vutgk == 0. && ntgk % 2 == 0 && emin ==
1233                                  0. && ! split) {
1234
1235 /*                       D=0 at the top or bottom of the active submatrix: */
1236 /*                       one eigenvalue is equal to zero; concatenate the */
1237 /*                       eigenvectors corresponding to the two smallest */
1238 /*                       eigenvalues. */
1239
1240                             i__3 = irowz + ntgk - 1;
1241                             for (i__ = irowz; i__ <= i__3; ++i__) {
1242                                 z__[i__ + (icolz + nsl - 2) * z_dim1] += z__[
1243                                         i__ + (icolz + nsl - 1) * z_dim1];
1244                                 z__[i__ + (icolz + nsl - 1) * z_dim1] = 0.;
1245                             }
1246 /*                        Z( IROWZ:IROWZ+NTGK-1,ICOLZ+NSL-2 ) = */
1247 /*     $                  Z( IROWZ:IROWZ+NTGK-1,ICOLZ+NSL-2 ) + */
1248 /*     $                  Z( IROWZ:IROWZ+NTGK-1,ICOLZ+NSL-1 ) */
1249 /*                        Z( IROWZ:IROWZ+NTGK-1,ICOLZ+NSL-1 ) = */
1250 /*     $                  ZERO */
1251 /*                       IF( IUTGK*2.GT.NTGK ) THEN */
1252 /*                          Eigenvalue equal to zero or very small. */
1253 /*                          NSL = NSL - 1 */
1254 /*                       END IF */
1255                         }
1256
1257 /* Computing MIN */
1258                         i__4 = nsl - 1, i__5 = nru - 1;
1259                         i__3 = f2cmin(i__4,i__5);
1260                         for (i__ = 0; i__ <= i__3; ++i__) {
1261                             nrmu = dnrm2_(&nru, &z__[irowu + (icolz + i__) * 
1262                                     z_dim1], &c__2);
1263                             if (nrmu == 0.) {
1264                                 *info = (*n << 1) + 1;
1265                                 return 0;
1266                             }
1267                             d__1 = 1. / nrmu;
1268                             dscal_(&nru, &d__1, &z__[irowu + (icolz + i__) * 
1269                                     z_dim1], &c__2);
1270                             if (nrmu != 1. && (d__1 = nrmu - ortol, abs(d__1))
1271                                      * sqrt2 > 1.) {
1272                                 i__4 = i__ - 1;
1273                                 for (j = 0; j <= i__4; ++j) {
1274                                     zjtji = -ddot_(&nru, &z__[irowu + (icolz 
1275                                             + j) * z_dim1], &c__2, &z__[irowu 
1276                                             + (icolz + i__) * z_dim1], &c__2);
1277                                     daxpy_(&nru, &zjtji, &z__[irowu + (icolz 
1278                                             + j) * z_dim1], &c__2, &z__[irowu 
1279                                             + (icolz + i__) * z_dim1], &c__2);
1280                                 }
1281                                 nrmu = dnrm2_(&nru, &z__[irowu + (icolz + i__)
1282                                          * z_dim1], &c__2);
1283                                 d__1 = 1. / nrmu;
1284                                 dscal_(&nru, &d__1, &z__[irowu + (icolz + i__)
1285                                          * z_dim1], &c__2);
1286                             }
1287                         }
1288 /* Computing MIN */
1289                         i__4 = nsl - 1, i__5 = nrv - 1;
1290                         i__3 = f2cmin(i__4,i__5);
1291                         for (i__ = 0; i__ <= i__3; ++i__) {
1292                             nrmv = dnrm2_(&nrv, &z__[irowv + (icolz + i__) * 
1293                                     z_dim1], &c__2);
1294                             if (nrmv == 0.) {
1295                                 *info = (*n << 1) + 1;
1296                                 return 0;
1297                             }
1298                             d__1 = -1. / nrmv;
1299                             dscal_(&nrv, &d__1, &z__[irowv + (icolz + i__) * 
1300                                     z_dim1], &c__2);
1301                             if (nrmv != 1. && (d__1 = nrmv - ortol, abs(d__1))
1302                                      * sqrt2 > 1.) {
1303                                 i__4 = i__ - 1;
1304                                 for (j = 0; j <= i__4; ++j) {
1305                                     zjtji = -ddot_(&nrv, &z__[irowv + (icolz 
1306                                             + j) * z_dim1], &c__2, &z__[irowv 
1307                                             + (icolz + i__) * z_dim1], &c__2);
1308                                     daxpy_(&nru, &zjtji, &z__[irowv + (icolz 
1309                                             + j) * z_dim1], &c__2, &z__[irowv 
1310                                             + (icolz + i__) * z_dim1], &c__2);
1311                                 }
1312                                 nrmv = dnrm2_(&nrv, &z__[irowv + (icolz + i__)
1313                                          * z_dim1], &c__2);
1314                                 d__1 = 1. / nrmv;
1315                                 dscal_(&nrv, &d__1, &z__[irowv + (icolz + i__)
1316                                          * z_dim1], &c__2);
1317                             }
1318                         }
1319                         if (vutgk == 0. && idptr < idend && ntgk % 2 > 0) {
1320
1321 /*                       D=0 in the middle of the active submatrix (one */
1322 /*                       eigenvalue is equal to zero): save the corresponding */
1323 /*                       eigenvector for later use (when bottom of the */
1324 /*                       active submatrix is reached). */
1325
1326                             split = TRUE_;
1327                             i__3 = irowz + ntgk - 1;
1328                             for (i__ = irowz; i__ <= i__3; ++i__) {
1329                                 z__[i__ + (*n + 1) * z_dim1] = z__[i__ + (*ns 
1330                                         + nsl) * z_dim1];
1331                                 z__[i__ + (*ns + nsl) * z_dim1] = 0.;
1332                             }
1333 /*                        Z( IROWZ:IROWZ+NTGK-1,N+1 ) = */
1334 /*     $                     Z( IROWZ:IROWZ+NTGK-1,NS+NSL ) */
1335 /*                        Z( IROWZ:IROWZ+NTGK-1,NS+NSL ) = */
1336 /*     $                     ZERO */
1337                         }
1338                     }
1339
1340 /* ** WANTZ **! */
1341                     nsl = f2cmin(nsl,nru);
1342                     sveq0 = FALSE_;
1343
1344 /*                 Absolute values of the eigenvalues of TGK. */
1345
1346                     i__3 = nsl - 1;
1347                     for (i__ = 0; i__ <= i__3; ++i__) {
1348                         s[isbeg + i__] = (d__1 = s[isbeg + i__], abs(d__1));
1349                     }
1350
1351 /*                 Update pointers for TGK, S and Z. */
1352
1353                     isbeg += nsl;
1354                     irowz += ntgk;
1355                     icolz += nsl;
1356                     irowu = irowz;
1357                     irowv = irowz + 1;
1358                     isplt = idptr + 1;
1359                     *ns += nsl;
1360                     nru = 0;
1361                     nrv = 0;
1362                 }
1363 /* ** NTGK.GT.0 **! */
1364                 if (irowz < *n << 1 && wantz) {
1365                     i__3 = irowz - 1;
1366                     for (i__ = 1; i__ <= i__3; ++i__) {
1367                         z__[i__ + icolz * z_dim1] = 0.;
1368                     }
1369 /*                       Z( 1:IROWZ-1, ICOLZ ) = ZERO */
1370                 }
1371             }
1372 /* ** IDPTR loop **! */
1373             if (split && wantz) {
1374
1375 /*              Bring back eigenvector corresponding */
1376 /*              to eigenvalue equal to zero. */
1377
1378                 i__2 = idend - ntgk + 1;
1379                 for (i__ = idbeg; i__ <= i__2; ++i__) {
1380                     z__[i__ + (isbeg - 1) * z_dim1] += z__[i__ + (*n + 1) * 
1381                             z_dim1];
1382                     z__[i__ + (*n + 1) * z_dim1] = 0.;
1383                 }
1384 /*               Z( IDBEG:IDEND-NTGK+1,ISBEG-1 ) = */
1385 /*     $         Z( IDBEG:IDEND-NTGK+1,ISBEG-1 ) + */
1386 /*     $         Z( IDBEG:IDEND-NTGK+1,N+1 ) */
1387 /*               Z( IDBEG:IDEND-NTGK+1,N+1 ) = 0 */
1388             }
1389             --irowv;
1390             ++irowu;
1391             idbeg = ieptr + 1;
1392             sveq0 = FALSE_;
1393             split = FALSE_;
1394         }
1395 /* ** Check for split in E **! */
1396     }
1397
1398 /*     Sort the singular values into decreasing order (insertion sort on */
1399 /*     singular values, but only one transposition per singular vector) */
1400
1401 /* ** IEPTR loop **! */
1402     i__1 = *ns - 1;
1403     for (i__ = 1; i__ <= i__1; ++i__) {
1404         k = 1;
1405         smin = s[1];
1406         i__2 = *ns + 1 - i__;
1407         for (j = 2; j <= i__2; ++j) {
1408             if (s[j] <= smin) {
1409                 k = j;
1410                 smin = s[j];
1411             }
1412         }
1413         if (k != *ns + 1 - i__) {
1414             s[k] = s[*ns + 1 - i__];
1415             s[*ns + 1 - i__] = smin;
1416             if (wantz) {
1417                 i__2 = *n << 1;
1418                 dswap_(&i__2, &z__[k * z_dim1 + 1], &c__1, &z__[(*ns + 1 - 
1419                         i__) * z_dim1 + 1], &c__1);
1420             }
1421         }
1422     }
1423
1424 /*     If RANGE=I, check for singular values/vectors to be discarded. */
1425
1426     if (indsv) {
1427         k = *iu - *il + 1;
1428         if (k < *ns) {
1429             i__1 = *ns;
1430             for (i__ = k + 1; i__ <= i__1; ++i__) {
1431                 s[i__] = 0.;
1432             }
1433 /*            S( K+1:NS ) = ZERO */
1434             if (wantz) {
1435                 i__1 = *n << 1;
1436                 for (i__ = 1; i__ <= i__1; ++i__) {
1437                     i__2 = *ns;
1438                     for (j = k + 1; j <= i__2; ++j) {
1439                         z__[i__ + j * z_dim1] = 0.;
1440                     }
1441                 }
1442 /*           Z( 1:N*2,K+1:NS ) = ZERO */
1443             }
1444             *ns = k;
1445         }
1446     }
1447
1448 /*     Reorder Z: U = Z( 1:N,1:NS ), V = Z( N+1:N*2,1:NS ). */
1449 /*     If B is a lower diagonal, swap U and V. */
1450
1451     if (wantz) {
1452         i__1 = *ns;
1453         for (i__ = 1; i__ <= i__1; ++i__) {
1454             i__2 = *n << 1;
1455             dcopy_(&i__2, &z__[i__ * z_dim1 + 1], &c__1, &work[1], &c__1);
1456             if (lower) {
1457                 dcopy_(n, &work[2], &c__2, &z__[*n + 1 + i__ * z_dim1], &c__1)
1458                         ;
1459                 dcopy_(n, &work[1], &c__2, &z__[i__ * z_dim1 + 1], &c__1);
1460             } else {
1461                 dcopy_(n, &work[2], &c__2, &z__[i__ * z_dim1 + 1], &c__1);
1462                 dcopy_(n, &work[1], &c__2, &z__[*n + 1 + i__ * z_dim1], &c__1)
1463                         ;
1464             }
1465         }
1466     }
1467
1468     return 0;
1469
1470 /*     End of DBDSVDX */
1471
1472 } /* dbdsvdx_ */
1473