C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / dgesvj.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_b17 = 0.;
516 static doublereal c_b18 = 1.;
517 static integer c__1 = 1;
518 static integer c__0 = 0;
519 static integer c__2 = 2;
520
521 /* > \brief \b DGESVJ */
522
523 /*  =========== DOCUMENTATION =========== */
524
525 /* Online html documentation available at */
526 /*            http://www.netlib.org/lapack/explore-html/ */
527
528 /* > \htmlonly */
529 /* > Download DGESVJ + dependencies */
530 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/dgesvj.
531 f"> */
532 /* > [TGZ]</a> */
533 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/dgesvj.
534 f"> */
535 /* > [ZIP]</a> */
536 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/dgesvj.
537 f"> */
538 /* > [TXT]</a> */
539 /* > \endhtmlonly */
540
541 /*  Definition: */
542 /*  =========== */
543
544 /*       SUBROUTINE DGESVJ( JOBA, JOBU, JOBV, M, N, A, LDA, SVA, MV, V, */
545 /*                          LDV, WORK, LWORK, INFO ) */
546
547 /*       INTEGER            INFO, LDA, LDV, LWORK, M, MV, N */
548 /*       CHARACTER*1        JOBA, JOBU, JOBV */
549 /*       DOUBLE PRECISION   A( LDA, * ), SVA( N ), V( LDV, * ), */
550 /*      $                   WORK( LWORK ) */
551
552
553 /* > \par Purpose: */
554 /*  ============= */
555 /* > */
556 /* > \verbatim */
557 /* > */
558 /* > DGESVJ computes the singular value decomposition (SVD) of a real */
559 /* > M-by-N matrix A, where M >= N. The SVD of A is written as */
560 /* >                                    [++]   [xx]   [x0]   [xx] */
561 /* >              A = U * SIGMA * V^t,  [++] = [xx] * [ox] * [xx] */
562 /* >                                    [++]   [xx] */
563 /* > where SIGMA is an N-by-N diagonal matrix, U is an M-by-N orthonormal */
564 /* > matrix, and V is an N-by-N orthogonal matrix. The diagonal elements */
565 /* > of SIGMA are the singular values of A. The columns of U and V are the */
566 /* > left and the right singular vectors of A, respectively. */
567 /* > DGESVJ can sometimes compute tiny singular values and their singular vectors much */
568 /* > more accurately than other SVD routines, see below under Further Details. */
569 /* > \endverbatim */
570
571 /*  Arguments: */
572 /*  ========== */
573
574 /* > \param[in] JOBA */
575 /* > \verbatim */
576 /* >          JOBA is CHARACTER*1 */
577 /* >          Specifies the structure of A. */
578 /* >          = 'L': The input matrix A is lower triangular; */
579 /* >          = 'U': The input matrix A is upper triangular; */
580 /* >          = 'G': The input matrix A is general M-by-N matrix, M >= N. */
581 /* > \endverbatim */
582 /* > */
583 /* > \param[in] JOBU */
584 /* > \verbatim */
585 /* >          JOBU is CHARACTER*1 */
586 /* >          Specifies whether to compute the left singular vectors */
587 /* >          (columns of U): */
588 /* >          = 'U': The left singular vectors corresponding to the nonzero */
589 /* >                 singular values are computed and returned in the leading */
590 /* >                 columns of A. See more details in the description of A. */
591 /* >                 The default numerical orthogonality threshold is set to */
592 /* >                 approximately TOL=CTOL*EPS, CTOL=DSQRT(M), EPS=DLAMCH('E'). */
593 /* >          = 'C': Analogous to JOBU='U', except that user can control the */
594 /* >                 level of numerical orthogonality of the computed left */
595 /* >                 singular vectors. TOL can be set to TOL = CTOL*EPS, where */
596 /* >                 CTOL is given on input in the array WORK. */
597 /* >                 No CTOL smaller than ONE is allowed. CTOL greater */
598 /* >                 than 1 / EPS is meaningless. The option 'C' */
599 /* >                 can be used if M*EPS is satisfactory orthogonality */
600 /* >                 of the computed left singular vectors, so CTOL=M could */
601 /* >                 save few sweeps of Jacobi rotations. */
602 /* >                 See the descriptions of A and WORK(1). */
603 /* >          = 'N': The matrix U is not computed. However, see the */
604 /* >                 description of A. */
605 /* > \endverbatim */
606 /* > */
607 /* > \param[in] JOBV */
608 /* > \verbatim */
609 /* >          JOBV is CHARACTER*1 */
610 /* >          Specifies whether to compute the right singular vectors, that */
611 /* >          is, the matrix V: */
612 /* >          = 'V':  the matrix V is computed and returned in the array V */
613 /* >          = 'A':  the Jacobi rotations are applied to the MV-by-N */
614 /* >                  array V. In other words, the right singular vector */
615 /* >                  matrix V is not computed explicitly, instead it is */
616 /* >                  applied to an MV-by-N matrix initially stored in the */
617 /* >                  first MV rows of V. */
618 /* >          = 'N':  the matrix V is not computed and the array V is not */
619 /* >                  referenced */
620 /* > \endverbatim */
621 /* > */
622 /* > \param[in] M */
623 /* > \verbatim */
624 /* >          M is INTEGER */
625 /* >          The number of rows of the input matrix A. 1/DLAMCH('E') > M >= 0. */
626 /* > \endverbatim */
627 /* > */
628 /* > \param[in] N */
629 /* > \verbatim */
630 /* >          N is INTEGER */
631 /* >          The number of columns of the input matrix A. */
632 /* >          M >= N >= 0. */
633 /* > \endverbatim */
634 /* > */
635 /* > \param[in,out] A */
636 /* > \verbatim */
637 /* >          A is DOUBLE PRECISION array, dimension (LDA,N) */
638 /* >          On entry, the M-by-N matrix A. */
639 /* >          On exit : */
640 /* >          If JOBU = 'U' .OR. JOBU = 'C' : */
641 /* >                 If INFO = 0 : */
642 /* >                 RANKA orthonormal columns of U are returned in the */
643 /* >                 leading RANKA columns of the array A. Here RANKA <= N */
644 /* >                 is the number of computed singular values of A that are */
645 /* >                 above the underflow threshold DLAMCH('S'). The singular */
646 /* >                 vectors corresponding to underflowed or zero singular */
647 /* >                 values are not computed. The value of RANKA is returned */
648 /* >                 in the array WORK as RANKA=NINT(WORK(2)). Also see the */
649 /* >                 descriptions of SVA and WORK. The computed columns of U */
650 /* >                 are mutually numerically orthogonal up to approximately */
651 /* >                 TOL=DSQRT(M)*EPS (default); or TOL=CTOL*EPS (JOBU = 'C'), */
652 /* >                 see the description of JOBU. */
653 /* >                 If INFO > 0 : */
654 /* >                 the procedure DGESVJ did not converge in the given number */
655 /* >                 of iterations (sweeps). In that case, the computed */
656 /* >                 columns of U may not be orthogonal up to TOL. The output */
657 /* >                 U (stored in A), SIGMA (given by the computed singular */
658 /* >                 values in SVA(1:N)) and V is still a decomposition of the */
659 /* >                 input matrix A in the sense that the residual */
660 /* >                 ||A-SCALE*U*SIGMA*V^T||_2 / ||A||_2 is small. */
661 /* > */
662 /* >          If JOBU = 'N' : */
663 /* >                 If INFO = 0 : */
664 /* >                 Note that the left singular vectors are 'for free' in the */
665 /* >                 one-sided Jacobi SVD algorithm. However, if only the */
666 /* >                 singular values are needed, the level of numerical */
667 /* >                 orthogonality of U is not an issue and iterations are */
668 /* >                 stopped when the columns of the iterated matrix are */
669 /* >                 numerically orthogonal up to approximately M*EPS. Thus, */
670 /* >                 on exit, A contains the columns of U scaled with the */
671 /* >                 corresponding singular values. */
672 /* >                 If INFO > 0 : */
673 /* >                 the procedure DGESVJ did not converge in the given number */
674 /* >                 of iterations (sweeps). */
675 /* > \endverbatim */
676 /* > */
677 /* > \param[in] LDA */
678 /* > \verbatim */
679 /* >          LDA is INTEGER */
680 /* >          The leading dimension of the array A.  LDA >= f2cmax(1,M). */
681 /* > \endverbatim */
682 /* > */
683 /* > \param[out] SVA */
684 /* > \verbatim */
685 /* >          SVA is DOUBLE PRECISION array, dimension (N) */
686 /* >          On exit : */
687 /* >          If INFO = 0 : */
688 /* >          depending on the value SCALE = WORK(1), we have: */
689 /* >                 If SCALE = ONE : */
690 /* >                 SVA(1:N) contains the computed singular values of A. */
691 /* >                 During the computation SVA contains the Euclidean column */
692 /* >                 norms of the iterated matrices in the array A. */
693 /* >                 If SCALE .NE. ONE : */
694 /* >                 The singular values of A are SCALE*SVA(1:N), and this */
695 /* >                 factored representation is due to the fact that some of the */
696 /* >                 singular values of A might underflow or overflow. */
697 /* >          If INFO > 0 : */
698 /* >          the procedure DGESVJ did not converge in the given number of */
699 /* >          iterations (sweeps) and SCALE*SVA(1:N) may not be accurate. */
700 /* > \endverbatim */
701 /* > */
702 /* > \param[in] MV */
703 /* > \verbatim */
704 /* >          MV is INTEGER */
705 /* >          If JOBV = 'A', then the product of Jacobi rotations in DGESVJ */
706 /* >          is applied to the first MV rows of V. See the description of JOBV. */
707 /* > \endverbatim */
708 /* > */
709 /* > \param[in,out] V */
710 /* > \verbatim */
711 /* >          V is DOUBLE PRECISION array, dimension (LDV,N) */
712 /* >          If JOBV = 'V', then V contains on exit the N-by-N matrix of */
713 /* >                         the right singular vectors; */
714 /* >          If JOBV = 'A', then V contains the product of the computed right */
715 /* >                         singular vector matrix and the initial matrix in */
716 /* >                         the array V. */
717 /* >          If JOBV = 'N', then V is not referenced. */
718 /* > \endverbatim */
719 /* > */
720 /* > \param[in] LDV */
721 /* > \verbatim */
722 /* >          LDV is INTEGER */
723 /* >          The leading dimension of the array V, LDV >= 1. */
724 /* >          If JOBV = 'V', then LDV >= f2cmax(1,N). */
725 /* >          If JOBV = 'A', then LDV >= f2cmax(1,MV) . */
726 /* > \endverbatim */
727 /* > */
728 /* > \param[in,out] WORK */
729 /* > \verbatim */
730 /* >          WORK is DOUBLE PRECISION array, dimension (LWORK) */
731 /* >          On entry : */
732 /* >          If JOBU = 'C' : */
733 /* >          WORK(1) = CTOL, where CTOL defines the threshold for convergence. */
734 /* >                    The process stops if all columns of A are mutually */
735 /* >                    orthogonal up to CTOL*EPS, EPS=DLAMCH('E'). */
736 /* >                    It is required that CTOL >= ONE, i.e. it is not */
737 /* >                    allowed to force the routine to obtain orthogonality */
738 /* >                    below EPS. */
739 /* >          On exit : */
740 /* >          WORK(1) = SCALE is the scaling factor such that SCALE*SVA(1:N) */
741 /* >                    are the computed singular values of A. */
742 /* >                    (See description of SVA().) */
743 /* >          WORK(2) = NINT(WORK(2)) is the number of the computed nonzero */
744 /* >                    singular values. */
745 /* >          WORK(3) = NINT(WORK(3)) is the number of the computed singular */
746 /* >                    values that are larger than the underflow threshold. */
747 /* >          WORK(4) = NINT(WORK(4)) is the number of sweeps of Jacobi */
748 /* >                    rotations needed for numerical convergence. */
749 /* >          WORK(5) = max_{i.NE.j} |COS(A(:,i),A(:,j))| in the last sweep. */
750 /* >                    This is useful information in cases when DGESVJ did */
751 /* >                    not converge, as it can be used to estimate whether */
752 /* >                    the output is still useful and for post festum analysis. */
753 /* >          WORK(6) = the largest absolute value over all sines of the */
754 /* >                    Jacobi rotation angles in the last sweep. It can be */
755 /* >                    useful for a post festum analysis. */
756 /* > \endverbatim */
757 /* > */
758 /* > \param[in] LWORK */
759 /* > \verbatim */
760 /* >          LWORK is INTEGER */
761 /* >          length of WORK, WORK >= MAX(6,M+N) */
762 /* > \endverbatim */
763 /* > */
764 /* > \param[out] INFO */
765 /* > \verbatim */
766 /* >          INFO is INTEGER */
767 /* >          = 0:  successful exit. */
768 /* >          < 0:  if INFO = -i, then the i-th argument had an illegal value */
769 /* >          > 0:  DGESVJ did not converge in the maximal allowed number (30) */
770 /* >                of sweeps. The output may still be useful. See the */
771 /* >                description of WORK. */
772 /* > \endverbatim */
773
774 /*  Authors: */
775 /*  ======== */
776
777 /* > \author Univ. of Tennessee */
778 /* > \author Univ. of California Berkeley */
779 /* > \author Univ. of Colorado Denver */
780 /* > \author NAG Ltd. */
781
782 /* > \date June 2017 */
783
784 /* > \ingroup doubleGEcomputational */
785
786 /* > \par Further Details: */
787 /*  ===================== */
788 /* > */
789 /* > \verbatim */
790 /* > */
791 /* >  The orthogonal N-by-N matrix V is obtained as a product of Jacobi plane */
792 /* >  rotations. The rotations are implemented as fast scaled rotations of */
793 /* >  Anda and Park [1]. In the case of underflow of the Jacobi angle, a */
794 /* >  modified Jacobi transformation of Drmac [4] is used. Pivot strategy uses */
795 /* >  column interchanges of de Rijk [2]. The relative accuracy of the computed */
796 /* >  singular values and the accuracy of the computed singular vectors (in */
797 /* >  angle metric) is as guaranteed by the theory of Demmel and Veselic [3]. */
798 /* >  The condition number that determines the accuracy in the full rank case */
799 /* >  is essentially min_{D=diag} kappa(A*D), where kappa(.) is the */
800 /* >  spectral condition number. The best performance of this Jacobi SVD */
801 /* >  procedure is achieved if used in an  accelerated version of Drmac and */
802 /* >  Veselic [5,6], and it is the kernel routine in the SIGMA library [7]. */
803 /* >  Some tunning parameters (marked with [TP]) are available for the */
804 /* >  implementer. */
805 /* >  The computational range for the nonzero singular values is the  machine */
806 /* >  number interval ( UNDERFLOW , OVERFLOW ). In extreme cases, even */
807 /* >  denormalized singular values can be computed with the corresponding */
808 /* >  gradual loss of accurate digits. */
809 /* > \endverbatim */
810
811 /* > \par Contributors: */
812 /*  ================== */
813 /* > */
814 /* > \verbatim */
815 /* > */
816 /* >  ============ */
817 /* > */
818 /* >  Zlatko Drmac (Zagreb, Croatia) and Kresimir Veselic (Hagen, Germany) */
819 /* > \endverbatim */
820
821 /* > \par References: */
822 /*  ================ */
823 /* > */
824 /* > \verbatim */
825 /* > */
826 /* > [1] A. A. Anda and H. Park: Fast plane rotations with dynamic scaling. */
827 /* >     SIAM J. matrix Anal. Appl., Vol. 15 (1994), pp. 162-174. */
828 /* > [2] P. P. M. De Rijk: A one-sided Jacobi algorithm for computing the */
829 /* >     singular value decomposition on a vector computer. */
830 /* >     SIAM J. Sci. Stat. Comp., Vol. 10 (1998), pp. 359-371. */
831 /* > [3] J. Demmel and K. Veselic: Jacobi method is more accurate than QR. */
832 /* > [4] Z. Drmac: Implementation of Jacobi rotations for accurate singular */
833 /* >     value computation in floating point arithmetic. */
834 /* >     SIAM J. Sci. Comp., Vol. 18 (1997), pp. 1200-1222. */
835 /* > [5] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm I. */
836 /* >     SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1322-1342. */
837 /* >     LAPACK Working note 169. */
838 /* > [6] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm II. */
839 /* >     SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1343-1362. */
840 /* >     LAPACK Working note 170. */
841 /* > [7] Z. Drmac: SIGMA - mathematical software library for accurate SVD, PSV, */
842 /* >     QSVD, (H,K)-SVD computations. */
843 /* >     Department of Mathematics, University of Zagreb, 2008. */
844 /* > \endverbatim */
845
846 /* >  \par Bugs, examples and comments: */
847 /*   ================================= */
848 /* > */
849 /* > \verbatim */
850 /* >  =========================== */
851 /* >  Please report all bugs and send interesting test examples and comments to */
852 /* >  drmac@math.hr. Thank you. */
853 /* > \endverbatim */
854 /* > */
855 /*  ===================================================================== */
856 /* Subroutine */ int dgesvj_(char *joba, char *jobu, char *jobv, integer *m, 
857         integer *n, doublereal *a, integer *lda, doublereal *sva, integer *mv,
858          doublereal *v, integer *ldv, doublereal *work, integer *lwork, 
859         integer *info)
860 {
861     /* System generated locals */
862     integer a_dim1, a_offset, v_dim1, v_offset, i__1, i__2, i__3, i__4, i__5;
863     doublereal d__1, d__2;
864
865     /* Local variables */
866     doublereal aapp, aapq, aaqq;
867     extern doublereal ddot_(integer *, doublereal *, integer *, doublereal *, 
868             integer *);
869     doublereal ctol;
870     integer ierr;
871     doublereal bigtheta;
872     integer pskipped;
873     doublereal aapp0;
874     extern doublereal dnrm2_(integer *, doublereal *, integer *);
875     doublereal temp1;
876     integer i__, p, q;
877     doublereal t;
878     extern /* Subroutine */ int dscal_(integer *, doublereal *, doublereal *, 
879             integer *);
880     doublereal large, apoaq, aqoap;
881     extern logical lsame_(char *, char *);
882     doublereal theta, small, sfmin;
883     logical lsvec;
884     extern /* Subroutine */ int dcopy_(integer *, doublereal *, integer *, 
885             doublereal *, integer *);
886     doublereal fastr[5];
887     extern /* Subroutine */ int dswap_(integer *, doublereal *, integer *, 
888             doublereal *, integer *);
889     doublereal epsln;
890     logical applv, rsvec;
891     extern /* Subroutine */ int daxpy_(integer *, doublereal *, doublereal *, 
892             integer *, doublereal *, integer *);
893     logical uctol;
894     extern /* Subroutine */ int drotm_(integer *, doublereal *, integer *, 
895             doublereal *, integer *, doublereal *);
896     logical lower, upper, rotok;
897     integer n2, n4;
898     extern /* Subroutine */ int dgsvj0_(char *, integer *, integer *, 
899             doublereal *, integer *, doublereal *, doublereal *, integer *, 
900             doublereal *, integer *, doublereal *, doublereal *, doublereal *,
901              integer *, doublereal *, integer *, integer *), dgsvj1_(
902             char *, integer *, integer *, integer *, doublereal *, integer *, 
903             doublereal *, doublereal *, integer *, doublereal *, integer *, 
904             doublereal *, doublereal *, doublereal *, integer *, doublereal *,
905              integer *, integer *);
906     doublereal rootsfmin;
907     integer n34;
908     doublereal cs;
909     extern doublereal dlamch_(char *);
910     doublereal sn;
911     extern /* Subroutine */ int dlascl_(char *, integer *, integer *, 
912             doublereal *, doublereal *, integer *, integer *, doublereal *, 
913             integer *, integer *);
914     extern integer idamax_(integer *, doublereal *, integer *);
915     extern /* Subroutine */ int dlaset_(char *, integer *, integer *, 
916             doublereal *, doublereal *, doublereal *, integer *), 
917             xerbla_(char *, integer *, ftnlen);
918     integer ijblsk, swband, blskip;
919     doublereal mxaapq;
920     extern /* Subroutine */ int dlassq_(integer *, doublereal *, integer *, 
921             doublereal *, doublereal *);
922     doublereal thsign, mxsinj;
923     integer ir1, emptsw, notrot, iswrot, jbc;
924     doublereal big;
925     integer kbl, lkahead, igl, ibr, jgl, nbl;
926     doublereal skl;
927     logical goscale;
928     doublereal tol;
929     integer mvl;
930     logical noscale;
931     doublereal rootbig, rooteps;
932     integer rowskip;
933     doublereal roottol;
934
935
936 /*  -- LAPACK computational routine (version 3.7.1) -- */
937 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
938 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
939 /*     June 2017 */
940
941
942 /*  ===================================================================== */
943
944 /*     from BLAS */
945 /*     from LAPACK */
946 /*     from BLAS */
947 /*     from LAPACK */
948
949
950 /*     Test the input arguments */
951
952     /* Parameter adjustments */
953     --sva;
954     a_dim1 = *lda;
955     a_offset = 1 + a_dim1 * 1;
956     a -= a_offset;
957     v_dim1 = *ldv;
958     v_offset = 1 + v_dim1 * 1;
959     v -= v_offset;
960     --work;
961
962     /* Function Body */
963     lsvec = lsame_(jobu, "U");
964     uctol = lsame_(jobu, "C");
965     rsvec = lsame_(jobv, "V");
966     applv = lsame_(jobv, "A");
967     upper = lsame_(joba, "U");
968     lower = lsame_(joba, "L");
969
970     if (! (upper || lower || lsame_(joba, "G"))) {
971         *info = -1;
972     } else if (! (lsvec || uctol || lsame_(jobu, "N"))) 
973             {
974         *info = -2;
975     } else if (! (rsvec || applv || lsame_(jobv, "N"))) 
976             {
977         *info = -3;
978     } else if (*m < 0) {
979         *info = -4;
980     } else if (*n < 0 || *n > *m) {
981         *info = -5;
982     } else if (*lda < *m) {
983         *info = -7;
984     } else if (*mv < 0) {
985         *info = -9;
986     } else if (rsvec && *ldv < *n || applv && *ldv < *mv) {
987         *info = -11;
988     } else if (uctol && work[1] <= 1.) {
989         *info = -12;
990     } else /* if(complicated condition) */ {
991 /* Computing MAX */
992         i__1 = *m + *n;
993         if (*lwork < f2cmax(i__1,6)) {
994             *info = -13;
995         } else {
996             *info = 0;
997         }
998     }
999
1000 /*     #:( */
1001     if (*info != 0) {
1002         i__1 = -(*info);
1003         xerbla_("DGESVJ", &i__1, (ftnlen)6);
1004         return 0;
1005     }
1006
1007 /* #:) Quick return for void matrix */
1008
1009     if (*m == 0 || *n == 0) {
1010         return 0;
1011     }
1012
1013 /*     Set numerical parameters */
1014 /*     The stopping criterion for Jacobi rotations is */
1015
1016 /*     max_{i<>j}|A(:,i)^T * A(:,j)|/(||A(:,i)||*||A(:,j)||) < CTOL*EPS */
1017
1018 /*     where EPS is the round-off and CTOL is defined as follows: */
1019
1020     if (uctol) {
1021 /*        ... user controlled */
1022         ctol = work[1];
1023     } else {
1024 /*        ... default */
1025         if (lsvec || rsvec || applv) {
1026             ctol = sqrt((doublereal) (*m));
1027         } else {
1028             ctol = (doublereal) (*m);
1029         }
1030     }
1031 /*     ... and the machine dependent parameters are */
1032 /* [!]  (Make sure that DLAMCH() works properly on the target machine.) */
1033
1034     epsln = dlamch_("Epsilon");
1035     rooteps = sqrt(epsln);
1036     sfmin = dlamch_("SafeMinimum");
1037     rootsfmin = sqrt(sfmin);
1038     small = sfmin / epsln;
1039     big = dlamch_("Overflow");
1040 /*     BIG         = ONE    / SFMIN */
1041     rootbig = 1. / rootsfmin;
1042     large = big / sqrt((doublereal) (*m * *n));
1043     bigtheta = 1. / rooteps;
1044
1045     tol = ctol * epsln;
1046     roottol = sqrt(tol);
1047
1048     if ((doublereal) (*m) * epsln >= 1.) {
1049         *info = -4;
1050         i__1 = -(*info);
1051         xerbla_("DGESVJ", &i__1, (ftnlen)6);
1052         return 0;
1053     }
1054
1055 /*     Initialize the right singular vector matrix. */
1056
1057     if (rsvec) {
1058         mvl = *n;
1059         dlaset_("A", &mvl, n, &c_b17, &c_b18, &v[v_offset], ldv);
1060     } else if (applv) {
1061         mvl = *mv;
1062     }
1063     rsvec = rsvec || applv;
1064
1065 /*     Initialize SVA( 1:N ) = ( ||A e_i||_2, i = 1:N ) */
1066 /* (!)  If necessary, scale A to protect the largest singular value */
1067 /*     from overflow. It is possible that saving the largest singular */
1068 /*     value destroys the information about the small ones. */
1069 /*     This initial scaling is almost minimal in the sense that the */
1070 /*     goal is to make sure that no column norm overflows, and that */
1071 /*     DSQRT(N)*max_i SVA(i) does not overflow. If INFinite entries */
1072 /*     in A are detected, the procedure returns with INFO=-6. */
1073
1074     skl = 1. / sqrt((doublereal) (*m) * (doublereal) (*n));
1075     noscale = TRUE_;
1076     goscale = TRUE_;
1077
1078     if (lower) {
1079 /*        the input matrix is M-by-N lower triangular (trapezoidal) */
1080         i__1 = *n;
1081         for (p = 1; p <= i__1; ++p) {
1082             aapp = 0.;
1083             aaqq = 1.;
1084             i__2 = *m - p + 1;
1085             dlassq_(&i__2, &a[p + p * a_dim1], &c__1, &aapp, &aaqq);
1086             if (aapp > big) {
1087                 *info = -6;
1088                 i__2 = -(*info);
1089                 xerbla_("DGESVJ", &i__2, (ftnlen)6);
1090                 return 0;
1091             }
1092             aaqq = sqrt(aaqq);
1093             if (aapp < big / aaqq && noscale) {
1094                 sva[p] = aapp * aaqq;
1095             } else {
1096                 noscale = FALSE_;
1097                 sva[p] = aapp * (aaqq * skl);
1098                 if (goscale) {
1099                     goscale = FALSE_;
1100                     i__2 = p - 1;
1101                     for (q = 1; q <= i__2; ++q) {
1102                         sva[q] *= skl;
1103 /* L1873: */
1104                     }
1105                 }
1106             }
1107 /* L1874: */
1108         }
1109     } else if (upper) {
1110 /*        the input matrix is M-by-N upper triangular (trapezoidal) */
1111         i__1 = *n;
1112         for (p = 1; p <= i__1; ++p) {
1113             aapp = 0.;
1114             aaqq = 1.;
1115             dlassq_(&p, &a[p * a_dim1 + 1], &c__1, &aapp, &aaqq);
1116             if (aapp > big) {
1117                 *info = -6;
1118                 i__2 = -(*info);
1119                 xerbla_("DGESVJ", &i__2, (ftnlen)6);
1120                 return 0;
1121             }
1122             aaqq = sqrt(aaqq);
1123             if (aapp < big / aaqq && noscale) {
1124                 sva[p] = aapp * aaqq;
1125             } else {
1126                 noscale = FALSE_;
1127                 sva[p] = aapp * (aaqq * skl);
1128                 if (goscale) {
1129                     goscale = FALSE_;
1130                     i__2 = p - 1;
1131                     for (q = 1; q <= i__2; ++q) {
1132                         sva[q] *= skl;
1133 /* L2873: */
1134                     }
1135                 }
1136             }
1137 /* L2874: */
1138         }
1139     } else {
1140 /*        the input matrix is M-by-N general dense */
1141         i__1 = *n;
1142         for (p = 1; p <= i__1; ++p) {
1143             aapp = 0.;
1144             aaqq = 1.;
1145             dlassq_(m, &a[p * a_dim1 + 1], &c__1, &aapp, &aaqq);
1146             if (aapp > big) {
1147                 *info = -6;
1148                 i__2 = -(*info);
1149                 xerbla_("DGESVJ", &i__2, (ftnlen)6);
1150                 return 0;
1151             }
1152             aaqq = sqrt(aaqq);
1153             if (aapp < big / aaqq && noscale) {
1154                 sva[p] = aapp * aaqq;
1155             } else {
1156                 noscale = FALSE_;
1157                 sva[p] = aapp * (aaqq * skl);
1158                 if (goscale) {
1159                     goscale = FALSE_;
1160                     i__2 = p - 1;
1161                     for (q = 1; q <= i__2; ++q) {
1162                         sva[q] *= skl;
1163 /* L3873: */
1164                     }
1165                 }
1166             }
1167 /* L3874: */
1168         }
1169     }
1170
1171     if (noscale) {
1172         skl = 1.;
1173     }
1174
1175 /*     Move the smaller part of the spectrum from the underflow threshold */
1176 /* (!)  Start by determining the position of the nonzero entries of the */
1177 /*     array SVA() relative to ( SFMIN, BIG ). */
1178
1179     aapp = 0.;
1180     aaqq = big;
1181     i__1 = *n;
1182     for (p = 1; p <= i__1; ++p) {
1183         if (sva[p] != 0.) {
1184 /* Computing MIN */
1185             d__1 = aaqq, d__2 = sva[p];
1186             aaqq = f2cmin(d__1,d__2);
1187         }
1188 /* Computing MAX */
1189         d__1 = aapp, d__2 = sva[p];
1190         aapp = f2cmax(d__1,d__2);
1191 /* L4781: */
1192     }
1193
1194 /* #:) Quick return for zero matrix */
1195
1196     if (aapp == 0.) {
1197         if (lsvec) {
1198             dlaset_("G", m, n, &c_b17, &c_b18, &a[a_offset], lda);
1199         }
1200         work[1] = 1.;
1201         work[2] = 0.;
1202         work[3] = 0.;
1203         work[4] = 0.;
1204         work[5] = 0.;
1205         work[6] = 0.;
1206         return 0;
1207     }
1208
1209 /* #:) Quick return for one-column matrix */
1210
1211     if (*n == 1) {
1212         if (lsvec) {
1213             dlascl_("G", &c__0, &c__0, &sva[1], &skl, m, &c__1, &a[a_dim1 + 1]
1214                     , lda, &ierr);
1215         }
1216         work[1] = 1. / skl;
1217         if (sva[1] >= sfmin) {
1218             work[2] = 1.;
1219         } else {
1220             work[2] = 0.;
1221         }
1222         work[3] = 0.;
1223         work[4] = 0.;
1224         work[5] = 0.;
1225         work[6] = 0.;
1226         return 0;
1227     }
1228
1229 /*     Protect small singular values from underflow, and try to */
1230 /*     avoid underflows/overflows in computing Jacobi rotations. */
1231
1232     sn = sqrt(sfmin / epsln);
1233     temp1 = sqrt(big / (doublereal) (*n));
1234     if (aapp <= sn || aaqq >= temp1 || sn <= aaqq && aapp <= temp1) {
1235 /* Computing MIN */
1236         d__1 = big, d__2 = temp1 / aapp;
1237         temp1 = f2cmin(d__1,d__2);
1238 /*         AAQQ  = AAQQ*TEMP1 */
1239 /*         AAPP  = AAPP*TEMP1 */
1240     } else if (aaqq <= sn && aapp <= temp1) {
1241 /* Computing MIN */
1242         d__1 = sn / aaqq, d__2 = big / (aapp * sqrt((doublereal) (*n)));
1243         temp1 = f2cmin(d__1,d__2);
1244 /*         AAQQ  = AAQQ*TEMP1 */
1245 /*         AAPP  = AAPP*TEMP1 */
1246     } else if (aaqq >= sn && aapp >= temp1) {
1247 /* Computing MAX */
1248         d__1 = sn / aaqq, d__2 = temp1 / aapp;
1249         temp1 = f2cmax(d__1,d__2);
1250 /*         AAQQ  = AAQQ*TEMP1 */
1251 /*         AAPP  = AAPP*TEMP1 */
1252     } else if (aaqq <= sn && aapp >= temp1) {
1253 /* Computing MIN */
1254         d__1 = sn / aaqq, d__2 = big / (sqrt((doublereal) (*n)) * aapp);
1255         temp1 = f2cmin(d__1,d__2);
1256 /*         AAQQ  = AAQQ*TEMP1 */
1257 /*         AAPP  = AAPP*TEMP1 */
1258     } else {
1259         temp1 = 1.;
1260     }
1261
1262 /*     Scale, if necessary */
1263
1264     if (temp1 != 1.) {
1265         dlascl_("G", &c__0, &c__0, &c_b18, &temp1, n, &c__1, &sva[1], n, &
1266                 ierr);
1267     }
1268     skl = temp1 * skl;
1269     if (skl != 1.) {
1270         dlascl_(joba, &c__0, &c__0, &c_b18, &skl, m, n, &a[a_offset], lda, &
1271                 ierr);
1272         skl = 1. / skl;
1273     }
1274
1275 /*     Row-cyclic Jacobi SVD algorithm with column pivoting */
1276
1277     emptsw = *n * (*n - 1) / 2;
1278     notrot = 0;
1279     fastr[0] = 0.;
1280
1281 /*     A is represented in factored form A = A * diag(WORK), where diag(WORK) */
1282 /*     is initialized to identity. WORK is updated during fast scaled */
1283 /*     rotations. */
1284
1285     i__1 = *n;
1286     for (q = 1; q <= i__1; ++q) {
1287         work[q] = 1.;
1288 /* L1868: */
1289     }
1290
1291
1292     swband = 3;
1293 /* [TP] SWBAND is a tuning parameter [TP]. It is meaningful and effective */
1294 /*     if DGESVJ is used as a computational routine in the preconditioned */
1295 /*     Jacobi SVD algorithm DGESVJ. For sweeps i=1:SWBAND the procedure */
1296 /*     works on pivots inside a band-like region around the diagonal. */
1297 /*     The boundaries are determined dynamically, based on the number of */
1298 /*     pivots above a threshold. */
1299
1300     kbl = f2cmin(8,*n);
1301 /* [TP] KBL is a tuning parameter that defines the tile size in the */
1302 /*     tiling of the p-q loops of pivot pairs. In general, an optimal */
1303 /*     value of KBL depends on the matrix dimensions and on the */
1304 /*     parameters of the computer's memory. */
1305
1306     nbl = *n / kbl;
1307     if (nbl * kbl != *n) {
1308         ++nbl;
1309     }
1310
1311 /* Computing 2nd power */
1312     i__1 = kbl;
1313     blskip = i__1 * i__1;
1314 /* [TP] BLKSKIP is a tuning parameter that depends on SWBAND and KBL. */
1315
1316     rowskip = f2cmin(5,kbl);
1317 /* [TP] ROWSKIP is a tuning parameter. */
1318
1319     lkahead = 1;
1320 /* [TP] LKAHEAD is a tuning parameter. */
1321
1322 /*     Quasi block transformations, using the lower (upper) triangular */
1323 /*     structure of the input matrix. The quasi-block-cycling usually */
1324 /*     invokes cubic convergence. Big part of this cycle is done inside */
1325 /*     canonical subspaces of dimensions less than M. */
1326
1327 /* Computing MAX */
1328     i__1 = 64, i__2 = kbl << 2;
1329     if ((lower || upper) && *n > f2cmax(i__1,i__2)) {
1330 /* [TP] The number of partition levels and the actual partition are */
1331 /*     tuning parameters. */
1332         n4 = *n / 4;
1333         n2 = *n / 2;
1334         n34 = n4 * 3;
1335         if (applv) {
1336             q = 0;
1337         } else {
1338             q = 1;
1339         }
1340
1341         if (lower) {
1342
1343 /*     This works very well on lower triangular matrices, in particular */
1344 /*     in the framework of the preconditioned Jacobi SVD (xGEJSV). */
1345 /*     The idea is simple: */
1346 /*     [+ 0 0 0]   Note that Jacobi transformations of [0 0] */
1347 /*     [+ + 0 0]                                       [0 0] */
1348 /*     [+ + x 0]   actually work on [x 0]              [x 0] */
1349 /*     [+ + x x]                    [x x].             [x x] */
1350
1351             i__1 = *m - n34;
1352             i__2 = *n - n34;
1353             i__3 = *lwork - *n;
1354             dgsvj0_(jobv, &i__1, &i__2, &a[n34 + 1 + (n34 + 1) * a_dim1], lda,
1355                      &work[n34 + 1], &sva[n34 + 1], &mvl, &v[n34 * q + 1 + (
1356                     n34 + 1) * v_dim1], ldv, &epsln, &sfmin, &tol, &c__2, &
1357                     work[*n + 1], &i__3, &ierr);
1358
1359             i__1 = *m - n2;
1360             i__2 = n34 - n2;
1361             i__3 = *lwork - *n;
1362             dgsvj0_(jobv, &i__1, &i__2, &a[n2 + 1 + (n2 + 1) * a_dim1], lda, &
1363                     work[n2 + 1], &sva[n2 + 1], &mvl, &v[n2 * q + 1 + (n2 + 1)
1364                      * v_dim1], ldv, &epsln, &sfmin, &tol, &c__2, &work[*n + 
1365                     1], &i__3, &ierr);
1366
1367             i__1 = *m - n2;
1368             i__2 = *n - n2;
1369             i__3 = *lwork - *n;
1370             dgsvj1_(jobv, &i__1, &i__2, &n4, &a[n2 + 1 + (n2 + 1) * a_dim1], 
1371                     lda, &work[n2 + 1], &sva[n2 + 1], &mvl, &v[n2 * q + 1 + (
1372                     n2 + 1) * v_dim1], ldv, &epsln, &sfmin, &tol, &c__1, &
1373                     work[*n + 1], &i__3, &ierr);
1374
1375             i__1 = *m - n4;
1376             i__2 = n2 - n4;
1377             i__3 = *lwork - *n;
1378             dgsvj0_(jobv, &i__1, &i__2, &a[n4 + 1 + (n4 + 1) * a_dim1], lda, &
1379                     work[n4 + 1], &sva[n4 + 1], &mvl, &v[n4 * q + 1 + (n4 + 1)
1380                      * v_dim1], ldv, &epsln, &sfmin, &tol, &c__1, &work[*n + 
1381                     1], &i__3, &ierr);
1382
1383             i__1 = *lwork - *n;
1384             dgsvj0_(jobv, m, &n4, &a[a_offset], lda, &work[1], &sva[1], &mvl, 
1385                     &v[v_offset], ldv, &epsln, &sfmin, &tol, &c__1, &work[*n 
1386                     + 1], &i__1, &ierr);
1387
1388             i__1 = *lwork - *n;
1389             dgsvj1_(jobv, m, &n2, &n4, &a[a_offset], lda, &work[1], &sva[1], &
1390                     mvl, &v[v_offset], ldv, &epsln, &sfmin, &tol, &c__1, &
1391                     work[*n + 1], &i__1, &ierr);
1392
1393
1394         } else if (upper) {
1395
1396
1397             i__1 = *lwork - *n;
1398             dgsvj0_(jobv, &n4, &n4, &a[a_offset], lda, &work[1], &sva[1], &
1399                     mvl, &v[v_offset], ldv, &epsln, &sfmin, &tol, &c__2, &
1400                     work[*n + 1], &i__1, &ierr);
1401
1402             i__1 = *lwork - *n;
1403             dgsvj0_(jobv, &n2, &n4, &a[(n4 + 1) * a_dim1 + 1], lda, &work[n4 
1404                     + 1], &sva[n4 + 1], &mvl, &v[n4 * q + 1 + (n4 + 1) * 
1405                     v_dim1], ldv, &epsln, &sfmin, &tol, &c__1, &work[*n + 1], 
1406                     &i__1, &ierr);
1407
1408             i__1 = *lwork - *n;
1409             dgsvj1_(jobv, &n2, &n2, &n4, &a[a_offset], lda, &work[1], &sva[1],
1410                      &mvl, &v[v_offset], ldv, &epsln, &sfmin, &tol, &c__1, &
1411                     work[*n + 1], &i__1, &ierr);
1412
1413             i__1 = n2 + n4;
1414             i__2 = *lwork - *n;
1415             dgsvj0_(jobv, &i__1, &n4, &a[(n2 + 1) * a_dim1 + 1], lda, &work[
1416                     n2 + 1], &sva[n2 + 1], &mvl, &v[n2 * q + 1 + (n2 + 1) * 
1417                     v_dim1], ldv, &epsln, &sfmin, &tol, &c__1, &work[*n + 1], 
1418                     &i__2, &ierr);
1419         }
1420
1421     }
1422
1423
1424     for (i__ = 1; i__ <= 30; ++i__) {
1425
1426
1427         mxaapq = 0.;
1428         mxsinj = 0.;
1429         iswrot = 0;
1430
1431         notrot = 0;
1432         pskipped = 0;
1433
1434 /*     Each sweep is unrolled using KBL-by-KBL tiles over the pivot pairs */
1435 /*     1 <= p < q <= N. This is the first step toward a blocked implementation */
1436 /*     of the rotations. New implementation, based on block transformations, */
1437 /*     is under development. */
1438
1439         i__1 = nbl;
1440         for (ibr = 1; ibr <= i__1; ++ibr) {
1441
1442             igl = (ibr - 1) * kbl + 1;
1443
1444 /* Computing MIN */
1445             i__3 = lkahead, i__4 = nbl - ibr;
1446             i__2 = f2cmin(i__3,i__4);
1447             for (ir1 = 0; ir1 <= i__2; ++ir1) {
1448
1449                 igl += ir1 * kbl;
1450
1451 /* Computing MIN */
1452                 i__4 = igl + kbl - 1, i__5 = *n - 1;
1453                 i__3 = f2cmin(i__4,i__5);
1454                 for (p = igl; p <= i__3; ++p) {
1455
1456
1457                     i__4 = *n - p + 1;
1458                     q = idamax_(&i__4, &sva[p], &c__1) + p - 1;
1459                     if (p != q) {
1460                         dswap_(m, &a[p * a_dim1 + 1], &c__1, &a[q * a_dim1 + 
1461                                 1], &c__1);
1462                         if (rsvec) {
1463                             dswap_(&mvl, &v[p * v_dim1 + 1], &c__1, &v[q * 
1464                                     v_dim1 + 1], &c__1);
1465                         }
1466                         temp1 = sva[p];
1467                         sva[p] = sva[q];
1468                         sva[q] = temp1;
1469                         temp1 = work[p];
1470                         work[p] = work[q];
1471                         work[q] = temp1;
1472                     }
1473
1474                     if (ir1 == 0) {
1475
1476 /*        Column norms are periodically updated by explicit */
1477 /*        norm computation. */
1478 /*        Caveat: */
1479 /*        Unfortunately, some BLAS implementations compute DNRM2(M,A(1,p),1) */
1480 /*        as DSQRT(DDOT(M,A(1,p),1,A(1,p),1)), which may cause the result to */
1481 /*        overflow for ||A(:,p)||_2 > DSQRT(overflow_threshold), and to */
1482 /*        underflow for ||A(:,p)||_2 < DSQRT(underflow_threshold). */
1483 /*        Hence, DNRM2 cannot be trusted, not even in the case when */
1484 /*        the true norm is far from the under(over)flow boundaries. */
1485 /*        If properly implemented DNRM2 is available, the IF-THEN-ELSE */
1486 /*        below should read "AAPP = DNRM2( M, A(1,p), 1 ) * WORK(p)". */
1487
1488                         if (sva[p] < rootbig && sva[p] > rootsfmin) {
1489                             sva[p] = dnrm2_(m, &a[p * a_dim1 + 1], &c__1) * 
1490                                     work[p];
1491                         } else {
1492                             temp1 = 0.;
1493                             aapp = 1.;
1494                             dlassq_(m, &a[p * a_dim1 + 1], &c__1, &temp1, &
1495                                     aapp);
1496                             sva[p] = temp1 * sqrt(aapp) * work[p];
1497                         }
1498                         aapp = sva[p];
1499                     } else {
1500                         aapp = sva[p];
1501                     }
1502
1503                     if (aapp > 0.) {
1504
1505                         pskipped = 0;
1506
1507 /* Computing MIN */
1508                         i__5 = igl + kbl - 1;
1509                         i__4 = f2cmin(i__5,*n);
1510                         for (q = p + 1; q <= i__4; ++q) {
1511
1512                             aaqq = sva[q];
1513
1514                             if (aaqq > 0.) {
1515
1516                                 aapp0 = aapp;
1517                                 if (aaqq >= 1.) {
1518                                     rotok = small * aapp <= aaqq;
1519                                     if (aapp < big / aaqq) {
1520                                         aapq = ddot_(m, &a[p * a_dim1 + 1], &
1521                                                 c__1, &a[q * a_dim1 + 1], &
1522                                                 c__1) * work[p] * work[q] / 
1523                                                 aaqq / aapp;
1524                                     } else {
1525                                         dcopy_(m, &a[p * a_dim1 + 1], &c__1, &
1526                                                 work[*n + 1], &c__1);
1527                                         dlascl_("G", &c__0, &c__0, &aapp, &
1528                                                 work[p], m, &c__1, &work[*n + 
1529                                                 1], lda, &ierr);
1530                                         aapq = ddot_(m, &work[*n + 1], &c__1, 
1531                                                 &a[q * a_dim1 + 1], &c__1) * 
1532                                                 work[q] / aaqq;
1533                                     }
1534                                 } else {
1535                                     rotok = aapp <= aaqq / small;
1536                                     if (aapp > small / aaqq) {
1537                                         aapq = ddot_(m, &a[p * a_dim1 + 1], &
1538                                                 c__1, &a[q * a_dim1 + 1], &
1539                                                 c__1) * work[p] * work[q] / 
1540                                                 aaqq / aapp;
1541                                     } else {
1542                                         dcopy_(m, &a[q * a_dim1 + 1], &c__1, &
1543                                                 work[*n + 1], &c__1);
1544                                         dlascl_("G", &c__0, &c__0, &aaqq, &
1545                                                 work[q], m, &c__1, &work[*n + 
1546                                                 1], lda, &ierr);
1547                                         aapq = ddot_(m, &work[*n + 1], &c__1, 
1548                                                 &a[p * a_dim1 + 1], &c__1) * 
1549                                                 work[p] / aapp;
1550                                     }
1551                                 }
1552
1553 /* Computing MAX */
1554                                 d__1 = mxaapq, d__2 = abs(aapq);
1555                                 mxaapq = f2cmax(d__1,d__2);
1556
1557 /*        TO rotate or NOT to rotate, THAT is the question ... */
1558
1559                                 if (abs(aapq) > tol) {
1560
1561 /* [RTD]      ROTATED = ROTATED + ONE */
1562
1563                                     if (ir1 == 0) {
1564                                         notrot = 0;
1565                                         pskipped = 0;
1566                                         ++iswrot;
1567                                     }
1568
1569                                     if (rotok) {
1570
1571                                         aqoap = aaqq / aapp;
1572                                         apoaq = aapp / aaqq;
1573                                         theta = (d__1 = aqoap - apoaq, abs(
1574                                                 d__1)) * -.5 / aapq;
1575
1576                                         if (abs(theta) > bigtheta) {
1577
1578                                             t = .5 / theta;
1579                                             fastr[2] = t * work[p] / work[q];
1580                                             fastr[3] = -t * work[q] / work[p];
1581                                             drotm_(m, &a[p * a_dim1 + 1], &
1582                                                     c__1, &a[q * a_dim1 + 1], 
1583                                                     &c__1, fastr);
1584                                             if (rsvec) {
1585                           drotm_(&mvl, &v[p * v_dim1 + 1], &c__1, &v[q * 
1586                                   v_dim1 + 1], &c__1, fastr);
1587                                             }
1588 /* Computing MAX */
1589                                             d__1 = 0., d__2 = t * apoaq * 
1590                                                     aapq + 1.;
1591                                             sva[q] = aaqq * sqrt((f2cmax(d__1,
1592                                                     d__2)));
1593 /* Computing MAX */
1594                                             d__1 = 0., d__2 = 1. - t * aqoap *
1595                                                      aapq;
1596                                             aapp *= sqrt((f2cmax(d__1,d__2)));
1597 /* Computing MAX */
1598                                             d__1 = mxsinj, d__2 = abs(t);
1599                                             mxsinj = f2cmax(d__1,d__2);
1600
1601                                         } else {
1602
1603
1604                                             thsign = -d_sign(&c_b18, &aapq);
1605                                             t = 1. / (theta + thsign * sqrt(
1606                                                     theta * theta + 1.));
1607                                             cs = sqrt(1. / (t * t + 1.));
1608                                             sn = t * cs;
1609
1610 /* Computing MAX */
1611                                             d__1 = mxsinj, d__2 = abs(sn);
1612                                             mxsinj = f2cmax(d__1,d__2);
1613 /* Computing MAX */
1614                                             d__1 = 0., d__2 = t * apoaq * 
1615                                                     aapq + 1.;
1616                                             sva[q] = aaqq * sqrt((f2cmax(d__1,
1617                                                     d__2)));
1618 /* Computing MAX */
1619                                             d__1 = 0., d__2 = 1. - t * aqoap *
1620                                                      aapq;
1621                                             aapp *= sqrt((f2cmax(d__1,d__2)));
1622
1623                                             apoaq = work[p] / work[q];
1624                                             aqoap = work[q] / work[p];
1625                                             if (work[p] >= 1.) {
1626                           if (work[q] >= 1.) {
1627                               fastr[2] = t * apoaq;
1628                               fastr[3] = -t * aqoap;
1629                               work[p] *= cs;
1630                               work[q] *= cs;
1631                               drotm_(m, &a[p * a_dim1 + 1], &c__1, &a[q * 
1632                                       a_dim1 + 1], &c__1, fastr);
1633                               if (rsvec) {
1634                                   drotm_(&mvl, &v[p * v_dim1 + 1], &c__1, &v[
1635                                           q * v_dim1 + 1], &c__1, fastr);
1636                               }
1637                           } else {
1638                               d__1 = -t * aqoap;
1639                               daxpy_(m, &d__1, &a[q * a_dim1 + 1], &c__1, &a[
1640                                       p * a_dim1 + 1], &c__1);
1641                               d__1 = cs * sn * apoaq;
1642                               daxpy_(m, &d__1, &a[p * a_dim1 + 1], &c__1, &a[
1643                                       q * a_dim1 + 1], &c__1);
1644                               work[p] *= cs;
1645                               work[q] /= cs;
1646                               if (rsvec) {
1647                                   d__1 = -t * aqoap;
1648                                   daxpy_(&mvl, &d__1, &v[q * v_dim1 + 1], &
1649                                           c__1, &v[p * v_dim1 + 1], &c__1);
1650                                   d__1 = cs * sn * apoaq;
1651                                   daxpy_(&mvl, &d__1, &v[p * v_dim1 + 1], &
1652                                           c__1, &v[q * v_dim1 + 1], &c__1);
1653                               }
1654                           }
1655                                             } else {
1656                           if (work[q] >= 1.) {
1657                               d__1 = t * apoaq;
1658                               daxpy_(m, &d__1, &a[p * a_dim1 + 1], &c__1, &a[
1659                                       q * a_dim1 + 1], &c__1);
1660                               d__1 = -cs * sn * aqoap;
1661                               daxpy_(m, &d__1, &a[q * a_dim1 + 1], &c__1, &a[
1662                                       p * a_dim1 + 1], &c__1);
1663                               work[p] /= cs;
1664                               work[q] *= cs;
1665                               if (rsvec) {
1666                                   d__1 = t * apoaq;
1667                                   daxpy_(&mvl, &d__1, &v[p * v_dim1 + 1], &
1668                                           c__1, &v[q * v_dim1 + 1], &c__1);
1669                                   d__1 = -cs * sn * aqoap;
1670                                   daxpy_(&mvl, &d__1, &v[q * v_dim1 + 1], &
1671                                           c__1, &v[p * v_dim1 + 1], &c__1);
1672                               }
1673                           } else {
1674                               if (work[p] >= work[q]) {
1675                                   d__1 = -t * aqoap;
1676                                   daxpy_(m, &d__1, &a[q * a_dim1 + 1], &c__1, 
1677                                           &a[p * a_dim1 + 1], &c__1);
1678                                   d__1 = cs * sn * apoaq;
1679                                   daxpy_(m, &d__1, &a[p * a_dim1 + 1], &c__1, 
1680                                           &a[q * a_dim1 + 1], &c__1);
1681                                   work[p] *= cs;
1682                                   work[q] /= cs;
1683                                   if (rsvec) {
1684                                       d__1 = -t * aqoap;
1685                                       daxpy_(&mvl, &d__1, &v[q * v_dim1 + 1], 
1686                                               &c__1, &v[p * v_dim1 + 1], &
1687                                               c__1);
1688                                       d__1 = cs * sn * apoaq;
1689                                       daxpy_(&mvl, &d__1, &v[p * v_dim1 + 1], 
1690                                               &c__1, &v[q * v_dim1 + 1], &
1691                                               c__1);
1692                                   }
1693                               } else {
1694                                   d__1 = t * apoaq;
1695                                   daxpy_(m, &d__1, &a[p * a_dim1 + 1], &c__1, 
1696                                           &a[q * a_dim1 + 1], &c__1);
1697                                   d__1 = -cs * sn * aqoap;
1698                                   daxpy_(m, &d__1, &a[q * a_dim1 + 1], &c__1, 
1699                                           &a[p * a_dim1 + 1], &c__1);
1700                                   work[p] /= cs;
1701                                   work[q] *= cs;
1702                                   if (rsvec) {
1703                                       d__1 = t * apoaq;
1704                                       daxpy_(&mvl, &d__1, &v[p * v_dim1 + 1], 
1705                                               &c__1, &v[q * v_dim1 + 1], &
1706                                               c__1);
1707                                       d__1 = -cs * sn * aqoap;
1708                                       daxpy_(&mvl, &d__1, &v[q * v_dim1 + 1], 
1709                                               &c__1, &v[p * v_dim1 + 1], &
1710                                               c__1);
1711                                   }
1712                               }
1713                           }
1714                                             }
1715                                         }
1716
1717                                     } else {
1718                                         dcopy_(m, &a[p * a_dim1 + 1], &c__1, &
1719                                                 work[*n + 1], &c__1);
1720                                         dlascl_("G", &c__0, &c__0, &aapp, &
1721                                                 c_b18, m, &c__1, &work[*n + 1]
1722                                                 , lda, &ierr);
1723                                         dlascl_("G", &c__0, &c__0, &aaqq, &
1724                                                 c_b18, m, &c__1, &a[q * 
1725                                                 a_dim1 + 1], lda, &ierr);
1726                                         temp1 = -aapq * work[p] / work[q];
1727                                         daxpy_(m, &temp1, &work[*n + 1], &
1728                                                 c__1, &a[q * a_dim1 + 1], &
1729                                                 c__1);
1730                                         dlascl_("G", &c__0, &c__0, &c_b18, &
1731                                                 aaqq, m, &c__1, &a[q * a_dim1 
1732                                                 + 1], lda, &ierr);
1733 /* Computing MAX */
1734                                         d__1 = 0., d__2 = 1. - aapq * aapq;
1735                                         sva[q] = aaqq * sqrt((f2cmax(d__1,d__2)))
1736                                                 ;
1737                                         mxsinj = f2cmax(mxsinj,sfmin);
1738                                     }
1739 /*           END IF ROTOK THEN ... ELSE */
1740
1741 /*           In the case of cancellation in updating SVA(q), SVA(p) */
1742 /*           recompute SVA(q), SVA(p). */
1743
1744 /* Computing 2nd power */
1745                                     d__1 = sva[q] / aaqq;
1746                                     if (d__1 * d__1 <= rooteps) {
1747                                         if (aaqq < rootbig && aaqq > 
1748                                                 rootsfmin) {
1749                                             sva[q] = dnrm2_(m, &a[q * a_dim1 
1750                                                     + 1], &c__1) * work[q];
1751                                         } else {
1752                                             t = 0.;
1753                                             aaqq = 1.;
1754                                             dlassq_(m, &a[q * a_dim1 + 1], &
1755                                                     c__1, &t, &aaqq);
1756                                             sva[q] = t * sqrt(aaqq) * work[q];
1757                                         }
1758                                     }
1759                                     if (aapp / aapp0 <= rooteps) {
1760                                         if (aapp < rootbig && aapp > 
1761                                                 rootsfmin) {
1762                                             aapp = dnrm2_(m, &a[p * a_dim1 + 
1763                                                     1], &c__1) * work[p];
1764                                         } else {
1765                                             t = 0.;
1766                                             aapp = 1.;
1767                                             dlassq_(m, &a[p * a_dim1 + 1], &
1768                                                     c__1, &t, &aapp);
1769                                             aapp = t * sqrt(aapp) * work[p];
1770                                         }
1771                                         sva[p] = aapp;
1772                                     }
1773
1774                                 } else {
1775 /*        A(:,p) and A(:,q) already numerically orthogonal */
1776                                     if (ir1 == 0) {
1777                                         ++notrot;
1778                                     }
1779 /* [RTD]      SKIPPED  = SKIPPED  + 1 */
1780                                     ++pskipped;
1781                                 }
1782                             } else {
1783 /*        A(:,q) is zero column */
1784                                 if (ir1 == 0) {
1785                                     ++notrot;
1786                                 }
1787                                 ++pskipped;
1788                             }
1789
1790                             if (i__ <= swband && pskipped > rowskip) {
1791                                 if (ir1 == 0) {
1792                                     aapp = -aapp;
1793                                 }
1794                                 notrot = 0;
1795                                 goto L2103;
1796                             }
1797
1798 /* L2002: */
1799                         }
1800 /*     END q-LOOP */
1801
1802 L2103:
1803 /*     bailed out of q-loop */
1804
1805                         sva[p] = aapp;
1806
1807                     } else {
1808                         sva[p] = aapp;
1809                         if (ir1 == 0 && aapp == 0.) {
1810 /* Computing MIN */
1811                             i__4 = igl + kbl - 1;
1812                             notrot = notrot + f2cmin(i__4,*n) - p;
1813                         }
1814                     }
1815
1816 /* L2001: */
1817                 }
1818 /*     end of the p-loop */
1819 /*     end of doing the block ( ibr, ibr ) */
1820 /* L1002: */
1821             }
1822 /*     end of ir1-loop */
1823
1824 /* ... go to the off diagonal blocks */
1825
1826             igl = (ibr - 1) * kbl + 1;
1827
1828             i__2 = nbl;
1829             for (jbc = ibr + 1; jbc <= i__2; ++jbc) {
1830
1831                 jgl = (jbc - 1) * kbl + 1;
1832
1833 /*        doing the block at ( ibr, jbc ) */
1834
1835                 ijblsk = 0;
1836 /* Computing MIN */
1837                 i__4 = igl + kbl - 1;
1838                 i__3 = f2cmin(i__4,*n);
1839                 for (p = igl; p <= i__3; ++p) {
1840
1841                     aapp = sva[p];
1842                     if (aapp > 0.) {
1843
1844                         pskipped = 0;
1845
1846 /* Computing MIN */
1847                         i__5 = jgl + kbl - 1;
1848                         i__4 = f2cmin(i__5,*n);
1849                         for (q = jgl; q <= i__4; ++q) {
1850
1851                             aaqq = sva[q];
1852                             if (aaqq > 0.) {
1853                                 aapp0 = aapp;
1854
1855
1856 /*        Safe Gram matrix computation */
1857
1858                                 if (aaqq >= 1.) {
1859                                     if (aapp >= aaqq) {
1860                                         rotok = small * aapp <= aaqq;
1861                                     } else {
1862                                         rotok = small * aaqq <= aapp;
1863                                     }
1864                                     if (aapp < big / aaqq) {
1865                                         aapq = ddot_(m, &a[p * a_dim1 + 1], &
1866                                                 c__1, &a[q * a_dim1 + 1], &
1867                                                 c__1) * work[p] * work[q] / 
1868                                                 aaqq / aapp;
1869                                     } else {
1870                                         dcopy_(m, &a[p * a_dim1 + 1], &c__1, &
1871                                                 work[*n + 1], &c__1);
1872                                         dlascl_("G", &c__0, &c__0, &aapp, &
1873                                                 work[p], m, &c__1, &work[*n + 
1874                                                 1], lda, &ierr);
1875                                         aapq = ddot_(m, &work[*n + 1], &c__1, 
1876                                                 &a[q * a_dim1 + 1], &c__1) * 
1877                                                 work[q] / aaqq;
1878                                     }
1879                                 } else {
1880                                     if (aapp >= aaqq) {
1881                                         rotok = aapp <= aaqq / small;
1882                                     } else {
1883                                         rotok = aaqq <= aapp / small;
1884                                     }
1885                                     if (aapp > small / aaqq) {
1886                                         aapq = ddot_(m, &a[p * a_dim1 + 1], &
1887                                                 c__1, &a[q * a_dim1 + 1], &
1888                                                 c__1) * work[p] * work[q] / 
1889                                                 aaqq / aapp;
1890                                     } else {
1891                                         dcopy_(m, &a[q * a_dim1 + 1], &c__1, &
1892                                                 work[*n + 1], &c__1);
1893                                         dlascl_("G", &c__0, &c__0, &aaqq, &
1894                                                 work[q], m, &c__1, &work[*n + 
1895                                                 1], lda, &ierr);
1896                                         aapq = ddot_(m, &work[*n + 1], &c__1, 
1897                                                 &a[p * a_dim1 + 1], &c__1) * 
1898                                                 work[p] / aapp;
1899                                     }
1900                                 }
1901
1902 /* Computing MAX */
1903                                 d__1 = mxaapq, d__2 = abs(aapq);
1904                                 mxaapq = f2cmax(d__1,d__2);
1905
1906 /*        TO rotate or NOT to rotate, THAT is the question ... */
1907
1908                                 if (abs(aapq) > tol) {
1909                                     notrot = 0;
1910 /* [RTD]      ROTATED  = ROTATED + 1 */
1911                                     pskipped = 0;
1912                                     ++iswrot;
1913
1914                                     if (rotok) {
1915
1916                                         aqoap = aaqq / aapp;
1917                                         apoaq = aapp / aaqq;
1918                                         theta = (d__1 = aqoap - apoaq, abs(
1919                                                 d__1)) * -.5 / aapq;
1920                                         if (aaqq > aapp0) {
1921                                             theta = -theta;
1922                                         }
1923
1924                                         if (abs(theta) > bigtheta) {
1925                                             t = .5 / theta;
1926                                             fastr[2] = t * work[p] / work[q];
1927                                             fastr[3] = -t * work[q] / work[p];
1928                                             drotm_(m, &a[p * a_dim1 + 1], &
1929                                                     c__1, &a[q * a_dim1 + 1], 
1930                                                     &c__1, fastr);
1931                                             if (rsvec) {
1932                           drotm_(&mvl, &v[p * v_dim1 + 1], &c__1, &v[q * 
1933                                   v_dim1 + 1], &c__1, fastr);
1934                                             }
1935 /* Computing MAX */
1936                                             d__1 = 0., d__2 = t * apoaq * 
1937                                                     aapq + 1.;
1938                                             sva[q] = aaqq * sqrt((f2cmax(d__1,
1939                                                     d__2)));
1940 /* Computing MAX */
1941                                             d__1 = 0., d__2 = 1. - t * aqoap *
1942                                                      aapq;
1943                                             aapp *= sqrt((f2cmax(d__1,d__2)));
1944 /* Computing MAX */
1945                                             d__1 = mxsinj, d__2 = abs(t);
1946                                             mxsinj = f2cmax(d__1,d__2);
1947                                         } else {
1948
1949
1950                                             thsign = -d_sign(&c_b18, &aapq);
1951                                             if (aaqq > aapp0) {
1952                           thsign = -thsign;
1953                                             }
1954                                             t = 1. / (theta + thsign * sqrt(
1955                                                     theta * theta + 1.));
1956                                             cs = sqrt(1. / (t * t + 1.));
1957                                             sn = t * cs;
1958 /* Computing MAX */
1959                                             d__1 = mxsinj, d__2 = abs(sn);
1960                                             mxsinj = f2cmax(d__1,d__2);
1961 /* Computing MAX */
1962                                             d__1 = 0., d__2 = t * apoaq * 
1963                                                     aapq + 1.;
1964                                             sva[q] = aaqq * sqrt((f2cmax(d__1,
1965                                                     d__2)));
1966 /* Computing MAX */
1967                                             d__1 = 0., d__2 = 1. - t * aqoap *
1968                                                      aapq;
1969                                             aapp *= sqrt((f2cmax(d__1,d__2)));
1970
1971                                             apoaq = work[p] / work[q];
1972                                             aqoap = work[q] / work[p];
1973                                             if (work[p] >= 1.) {
1974
1975                           if (work[q] >= 1.) {
1976                               fastr[2] = t * apoaq;
1977                               fastr[3] = -t * aqoap;
1978                               work[p] *= cs;
1979                               work[q] *= cs;
1980                               drotm_(m, &a[p * a_dim1 + 1], &c__1, &a[q * 
1981                                       a_dim1 + 1], &c__1, fastr);
1982                               if (rsvec) {
1983                                   drotm_(&mvl, &v[p * v_dim1 + 1], &c__1, &v[
1984                                           q * v_dim1 + 1], &c__1, fastr);
1985                               }
1986                           } else {
1987                               d__1 = -t * aqoap;
1988                               daxpy_(m, &d__1, &a[q * a_dim1 + 1], &c__1, &a[
1989                                       p * a_dim1 + 1], &c__1);
1990                               d__1 = cs * sn * apoaq;
1991                               daxpy_(m, &d__1, &a[p * a_dim1 + 1], &c__1, &a[
1992                                       q * a_dim1 + 1], &c__1);
1993                               if (rsvec) {
1994                                   d__1 = -t * aqoap;
1995                                   daxpy_(&mvl, &d__1, &v[q * v_dim1 + 1], &
1996                                           c__1, &v[p * v_dim1 + 1], &c__1);
1997                                   d__1 = cs * sn * apoaq;
1998                                   daxpy_(&mvl, &d__1, &v[p * v_dim1 + 1], &
1999                                           c__1, &v[q * v_dim1 + 1], &c__1);
2000                               }
2001                               work[p] *= cs;
2002                               work[q] /= cs;
2003                           }
2004                                             } else {
2005                           if (work[q] >= 1.) {
2006                               d__1 = t * apoaq;
2007                               daxpy_(m, &d__1, &a[p * a_dim1 + 1], &c__1, &a[
2008                                       q * a_dim1 + 1], &c__1);
2009                               d__1 = -cs * sn * aqoap;
2010                               daxpy_(m, &d__1, &a[q * a_dim1 + 1], &c__1, &a[
2011                                       p * a_dim1 + 1], &c__1);
2012                               if (rsvec) {
2013                                   d__1 = t * apoaq;
2014                                   daxpy_(&mvl, &d__1, &v[p * v_dim1 + 1], &
2015                                           c__1, &v[q * v_dim1 + 1], &c__1);
2016                                   d__1 = -cs * sn * aqoap;
2017                                   daxpy_(&mvl, &d__1, &v[q * v_dim1 + 1], &
2018                                           c__1, &v[p * v_dim1 + 1], &c__1);
2019                               }
2020                               work[p] /= cs;
2021                               work[q] *= cs;
2022                           } else {
2023                               if (work[p] >= work[q]) {
2024                                   d__1 = -t * aqoap;
2025                                   daxpy_(m, &d__1, &a[q * a_dim1 + 1], &c__1, 
2026                                           &a[p * a_dim1 + 1], &c__1);
2027                                   d__1 = cs * sn * apoaq;
2028                                   daxpy_(m, &d__1, &a[p * a_dim1 + 1], &c__1, 
2029                                           &a[q * a_dim1 + 1], &c__1);
2030                                   work[p] *= cs;
2031                                   work[q] /= cs;
2032                                   if (rsvec) {
2033                                       d__1 = -t * aqoap;
2034                                       daxpy_(&mvl, &d__1, &v[q * v_dim1 + 1], 
2035                                               &c__1, &v[p * v_dim1 + 1], &
2036                                               c__1);
2037                                       d__1 = cs * sn * apoaq;
2038                                       daxpy_(&mvl, &d__1, &v[p * v_dim1 + 1], 
2039                                               &c__1, &v[q * v_dim1 + 1], &
2040                                               c__1);
2041                                   }
2042                               } else {
2043                                   d__1 = t * apoaq;
2044                                   daxpy_(m, &d__1, &a[p * a_dim1 + 1], &c__1, 
2045                                           &a[q * a_dim1 + 1], &c__1);
2046                                   d__1 = -cs * sn * aqoap;
2047                                   daxpy_(m, &d__1, &a[q * a_dim1 + 1], &c__1, 
2048                                           &a[p * a_dim1 + 1], &c__1);
2049                                   work[p] /= cs;
2050                                   work[q] *= cs;
2051                                   if (rsvec) {
2052                                       d__1 = t * apoaq;
2053                                       daxpy_(&mvl, &d__1, &v[p * v_dim1 + 1], 
2054                                               &c__1, &v[q * v_dim1 + 1], &
2055                                               c__1);
2056                                       d__1 = -cs * sn * aqoap;
2057                                       daxpy_(&mvl, &d__1, &v[q * v_dim1 + 1], 
2058                                               &c__1, &v[p * v_dim1 + 1], &
2059                                               c__1);
2060                                   }
2061                               }
2062                           }
2063                                             }
2064                                         }
2065
2066                                     } else {
2067                                         if (aapp > aaqq) {
2068                                             dcopy_(m, &a[p * a_dim1 + 1], &
2069                                                     c__1, &work[*n + 1], &
2070                                                     c__1);
2071                                             dlascl_("G", &c__0, &c__0, &aapp, 
2072                                                     &c_b18, m, &c__1, &work[*
2073                                                     n + 1], lda, &ierr);
2074                                             dlascl_("G", &c__0, &c__0, &aaqq, 
2075                                                     &c_b18, m, &c__1, &a[q * 
2076                                                     a_dim1 + 1], lda, &ierr);
2077                                             temp1 = -aapq * work[p] / work[q];
2078                                             daxpy_(m, &temp1, &work[*n + 1], &
2079                                                     c__1, &a[q * a_dim1 + 1], 
2080                                                     &c__1);
2081                                             dlascl_("G", &c__0, &c__0, &c_b18,
2082                                                      &aaqq, m, &c__1, &a[q * 
2083                                                     a_dim1 + 1], lda, &ierr);
2084 /* Computing MAX */
2085                                             d__1 = 0., d__2 = 1. - aapq * 
2086                                                     aapq;
2087                                             sva[q] = aaqq * sqrt((f2cmax(d__1,
2088                                                     d__2)));
2089                                             mxsinj = f2cmax(mxsinj,sfmin);
2090                                         } else {
2091                                             dcopy_(m, &a[q * a_dim1 + 1], &
2092                                                     c__1, &work[*n + 1], &
2093                                                     c__1);
2094                                             dlascl_("G", &c__0, &c__0, &aaqq, 
2095                                                     &c_b18, m, &c__1, &work[*
2096                                                     n + 1], lda, &ierr);
2097                                             dlascl_("G", &c__0, &c__0, &aapp, 
2098                                                     &c_b18, m, &c__1, &a[p * 
2099                                                     a_dim1 + 1], lda, &ierr);
2100                                             temp1 = -aapq * work[q] / work[p];
2101                                             daxpy_(m, &temp1, &work[*n + 1], &
2102                                                     c__1, &a[p * a_dim1 + 1], 
2103                                                     &c__1);
2104                                             dlascl_("G", &c__0, &c__0, &c_b18,
2105                                                      &aapp, m, &c__1, &a[p * 
2106                                                     a_dim1 + 1], lda, &ierr);
2107 /* Computing MAX */
2108                                             d__1 = 0., d__2 = 1. - aapq * 
2109                                                     aapq;
2110                                             sva[p] = aapp * sqrt((f2cmax(d__1,
2111                                                     d__2)));
2112                                             mxsinj = f2cmax(mxsinj,sfmin);
2113                                         }
2114                                     }
2115 /*           END IF ROTOK THEN ... ELSE */
2116
2117 /*           In the case of cancellation in updating SVA(q) */
2118 /* Computing 2nd power */
2119                                     d__1 = sva[q] / aaqq;
2120                                     if (d__1 * d__1 <= rooteps) {
2121                                         if (aaqq < rootbig && aaqq > 
2122                                                 rootsfmin) {
2123                                             sva[q] = dnrm2_(m, &a[q * a_dim1 
2124                                                     + 1], &c__1) * work[q];
2125                                         } else {
2126                                             t = 0.;
2127                                             aaqq = 1.;
2128                                             dlassq_(m, &a[q * a_dim1 + 1], &
2129                                                     c__1, &t, &aaqq);
2130                                             sva[q] = t * sqrt(aaqq) * work[q];
2131                                         }
2132                                     }
2133 /* Computing 2nd power */
2134                                     d__1 = aapp / aapp0;
2135                                     if (d__1 * d__1 <= rooteps) {
2136                                         if (aapp < rootbig && aapp > 
2137                                                 rootsfmin) {
2138                                             aapp = dnrm2_(m, &a[p * a_dim1 + 
2139                                                     1], &c__1) * work[p];
2140                                         } else {
2141                                             t = 0.;
2142                                             aapp = 1.;
2143                                             dlassq_(m, &a[p * a_dim1 + 1], &
2144                                                     c__1, &t, &aapp);
2145                                             aapp = t * sqrt(aapp) * work[p];
2146                                         }
2147                                         sva[p] = aapp;
2148                                     }
2149 /*              end of OK rotation */
2150                                 } else {
2151                                     ++notrot;
2152 /* [RTD]      SKIPPED  = SKIPPED  + 1 */
2153                                     ++pskipped;
2154                                     ++ijblsk;
2155                                 }
2156                             } else {
2157                                 ++notrot;
2158                                 ++pskipped;
2159                                 ++ijblsk;
2160                             }
2161
2162                             if (i__ <= swband && ijblsk >= blskip) {
2163                                 sva[p] = aapp;
2164                                 notrot = 0;
2165                                 goto L2011;
2166                             }
2167                             if (i__ <= swband && pskipped > rowskip) {
2168                                 aapp = -aapp;
2169                                 notrot = 0;
2170                                 goto L2203;
2171                             }
2172
2173 /* L2200: */
2174                         }
2175 /*        end of the q-loop */
2176 L2203:
2177
2178                         sva[p] = aapp;
2179
2180                     } else {
2181
2182                         if (aapp == 0.) {
2183 /* Computing MIN */
2184                             i__4 = jgl + kbl - 1;
2185                             notrot = notrot + f2cmin(i__4,*n) - jgl + 1;
2186                         }
2187                         if (aapp < 0.) {
2188                             notrot = 0;
2189                         }
2190
2191                     }
2192
2193 /* L2100: */
2194                 }
2195 /*     end of the p-loop */
2196 /* L2010: */
2197             }
2198 /*     end of the jbc-loop */
2199 L2011:
2200 /* 2011 bailed out of the jbc-loop */
2201 /* Computing MIN */
2202             i__3 = igl + kbl - 1;
2203             i__2 = f2cmin(i__3,*n);
2204             for (p = igl; p <= i__2; ++p) {
2205                 sva[p] = (d__1 = sva[p], abs(d__1));
2206 /* L2012: */
2207             }
2208 /* ** */
2209 /* L2000: */
2210         }
2211 /* 2000 :: end of the ibr-loop */
2212
2213         if (sva[*n] < rootbig && sva[*n] > rootsfmin) {
2214             sva[*n] = dnrm2_(m, &a[*n * a_dim1 + 1], &c__1) * work[*n];
2215         } else {
2216             t = 0.;
2217             aapp = 1.;
2218             dlassq_(m, &a[*n * a_dim1 + 1], &c__1, &t, &aapp);
2219             sva[*n] = t * sqrt(aapp) * work[*n];
2220         }
2221
2222 /*     Additional steering devices */
2223
2224         if (i__ < swband && (mxaapq <= roottol || iswrot <= *n)) {
2225             swband = i__;
2226         }
2227
2228         if (i__ > swband + 1 && mxaapq < sqrt((doublereal) (*n)) * tol && (
2229                 doublereal) (*n) * mxaapq * mxsinj < tol) {
2230             goto L1994;
2231         }
2232
2233         if (notrot >= emptsw) {
2234             goto L1994;
2235         }
2236
2237 /* L1993: */
2238     }
2239 /*     end i=1:NSWEEP loop */
2240
2241 /* #:( Reaching this point means that the procedure has not converged. */
2242     *info = 29;
2243     goto L1995;
2244
2245 L1994:
2246 /* #:) Reaching this point means numerical convergence after the i-th */
2247 /*     sweep. */
2248
2249     *info = 0;
2250 /* #:) INFO = 0 confirms successful iterations. */
2251 L1995:
2252
2253 /*     Sort the singular values and find how many are above */
2254 /*     the underflow threshold. */
2255
2256     n2 = 0;
2257     n4 = 0;
2258     i__1 = *n - 1;
2259     for (p = 1; p <= i__1; ++p) {
2260         i__2 = *n - p + 1;
2261         q = idamax_(&i__2, &sva[p], &c__1) + p - 1;
2262         if (p != q) {
2263             temp1 = sva[p];
2264             sva[p] = sva[q];
2265             sva[q] = temp1;
2266             temp1 = work[p];
2267             work[p] = work[q];
2268             work[q] = temp1;
2269             dswap_(m, &a[p * a_dim1 + 1], &c__1, &a[q * a_dim1 + 1], &c__1);
2270             if (rsvec) {
2271                 dswap_(&mvl, &v[p * v_dim1 + 1], &c__1, &v[q * v_dim1 + 1], &
2272                         c__1);
2273             }
2274         }
2275         if (sva[p] != 0.) {
2276             ++n4;
2277             if (sva[p] * skl > sfmin) {
2278                 ++n2;
2279             }
2280         }
2281 /* L5991: */
2282     }
2283     if (sva[*n] != 0.) {
2284         ++n4;
2285         if (sva[*n] * skl > sfmin) {
2286             ++n2;
2287         }
2288     }
2289
2290 /*     Normalize the left singular vectors. */
2291
2292     if (lsvec || uctol) {
2293         i__1 = n2;
2294         for (p = 1; p <= i__1; ++p) {
2295             d__1 = work[p] / sva[p];
2296             dscal_(m, &d__1, &a[p * a_dim1 + 1], &c__1);
2297 /* L1998: */
2298         }
2299     }
2300
2301 /*     Scale the product of Jacobi rotations (assemble the fast rotations). */
2302
2303     if (rsvec) {
2304         if (applv) {
2305             i__1 = *n;
2306             for (p = 1; p <= i__1; ++p) {
2307                 dscal_(&mvl, &work[p], &v[p * v_dim1 + 1], &c__1);
2308 /* L2398: */
2309             }
2310         } else {
2311             i__1 = *n;
2312             for (p = 1; p <= i__1; ++p) {
2313                 temp1 = 1. / dnrm2_(&mvl, &v[p * v_dim1 + 1], &c__1);
2314                 dscal_(&mvl, &temp1, &v[p * v_dim1 + 1], &c__1);
2315 /* L2399: */
2316             }
2317         }
2318     }
2319
2320 /*     Undo scaling, if necessary (and possible). */
2321     if (skl > 1. && sva[1] < big / skl || skl < 1. && sva[f2cmax(n2,1)] > sfmin /
2322              skl) {
2323         i__1 = *n;
2324         for (p = 1; p <= i__1; ++p) {
2325             sva[p] = skl * sva[p];
2326 /* L2400: */
2327         }
2328         skl = 1.;
2329     }
2330
2331     work[1] = skl;
2332 /*     The singular values of A are SKL*SVA(1:N). If SKL.NE.ONE */
2333 /*     then some of the singular values may overflow or underflow and */
2334 /*     the spectrum is given in this factored representation. */
2335
2336     work[2] = (doublereal) n4;
2337 /*     N4 is the number of computed nonzero singular values of A. */
2338
2339     work[3] = (doublereal) n2;
2340 /*     N2 is the number of singular values of A greater than SFMIN. */
2341 /*     If N2<N, SVA(N2:N) contains ZEROS and/or denormalized numbers */
2342 /*     that may carry some information. */
2343
2344     work[4] = (doublereal) i__;
2345 /*     i is the index of the last sweep before declaring convergence. */
2346
2347     work[5] = mxaapq;
2348 /*     MXAAPQ is the largest absolute value of scaled pivots in the */
2349 /*     last sweep */
2350
2351     work[6] = mxsinj;
2352 /*     MXSINJ is the largest absolute value of the sines of Jacobi angles */
2353 /*     in the last sweep */
2354
2355     return 0;
2356 } /* dgesvj_ */
2357