C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / sgesvj.c
1 #include <math.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <complex.h>
6 #ifdef complex
7 #undef complex
8 #endif
9 #ifdef I
10 #undef I
11 #endif
12
13 #if defined(_WIN64)
14 typedef long long BLASLONG;
15 typedef unsigned long long BLASULONG;
16 #else
17 typedef long BLASLONG;
18 typedef unsigned long BLASULONG;
19 #endif
20
21 #ifdef LAPACK_ILP64
22 typedef BLASLONG blasint;
23 #if defined(_WIN64)
24 #define blasabs(x) llabs(x)
25 #else
26 #define blasabs(x) labs(x)
27 #endif
28 #else
29 typedef int blasint;
30 #define blasabs(x) abs(x)
31 #endif
32
33 typedef blasint integer;
34
35 typedef unsigned int uinteger;
36 typedef char *address;
37 typedef short int shortint;
38 typedef float real;
39 typedef double doublereal;
40 typedef struct { real r, i; } complex;
41 typedef struct { doublereal r, i; } doublecomplex;
42 #ifdef _MSC_VER
43 static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
44 static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
45 static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
46 static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
47 #else
48 static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
49 static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
50 static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
51 static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
52 #endif
53 #define pCf(z) (*_pCf(z))
54 #define pCd(z) (*_pCd(z))
55 typedef int logical;
56 typedef short int shortlogical;
57 typedef char logical1;
58 typedef char integer1;
59
60 #define TRUE_ (1)
61 #define FALSE_ (0)
62
63 /* Extern is for use with -E */
64 #ifndef Extern
65 #define Extern extern
66 #endif
67
68 /* I/O stuff */
69
70 typedef int flag;
71 typedef int ftnlen;
72 typedef int ftnint;
73
74 /*external read, write*/
75 typedef struct
76 {       flag cierr;
77         ftnint ciunit;
78         flag ciend;
79         char *cifmt;
80         ftnint cirec;
81 } cilist;
82
83 /*internal read, write*/
84 typedef struct
85 {       flag icierr;
86         char *iciunit;
87         flag iciend;
88         char *icifmt;
89         ftnint icirlen;
90         ftnint icirnum;
91 } icilist;
92
93 /*open*/
94 typedef struct
95 {       flag oerr;
96         ftnint ounit;
97         char *ofnm;
98         ftnlen ofnmlen;
99         char *osta;
100         char *oacc;
101         char *ofm;
102         ftnint orl;
103         char *oblnk;
104 } olist;
105
106 /*close*/
107 typedef struct
108 {       flag cerr;
109         ftnint cunit;
110         char *csta;
111 } cllist;
112
113 /*rewind, backspace, endfile*/
114 typedef struct
115 {       flag aerr;
116         ftnint aunit;
117 } alist;
118
119 /* inquire */
120 typedef struct
121 {       flag inerr;
122         ftnint inunit;
123         char *infile;
124         ftnlen infilen;
125         ftnint  *inex;  /*parameters in standard's order*/
126         ftnint  *inopen;
127         ftnint  *innum;
128         ftnint  *innamed;
129         char    *inname;
130         ftnlen  innamlen;
131         char    *inacc;
132         ftnlen  inacclen;
133         char    *inseq;
134         ftnlen  inseqlen;
135         char    *indir;
136         ftnlen  indirlen;
137         char    *infmt;
138         ftnlen  infmtlen;
139         char    *inform;
140         ftnint  informlen;
141         char    *inunf;
142         ftnlen  inunflen;
143         ftnint  *inrecl;
144         ftnint  *innrec;
145         char    *inblank;
146         ftnlen  inblanklen;
147 } inlist;
148
149 #define VOID void
150
151 union Multitype {       /* for multiple entry points */
152         integer1 g;
153         shortint h;
154         integer i;
155         /* longint j; */
156         real r;
157         doublereal d;
158         complex c;
159         doublecomplex z;
160         };
161
162 typedef union Multitype Multitype;
163
164 struct Vardesc {        /* for Namelist */
165         char *name;
166         char *addr;
167         ftnlen *dims;
168         int  type;
169         };
170 typedef struct Vardesc Vardesc;
171
172 struct Namelist {
173         char *name;
174         Vardesc **vars;
175         int nvars;
176         };
177 typedef struct Namelist Namelist;
178
179 #define abs(x) ((x) >= 0 ? (x) : -(x))
180 #define dabs(x) (fabs(x))
181 #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
182 #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
183 #define dmin(a,b) (f2cmin(a,b))
184 #define dmax(a,b) (f2cmax(a,b))
185 #define bit_test(a,b)   ((a) >> (b) & 1)
186 #define bit_clear(a,b)  ((a) & ~((uinteger)1 << (b)))
187 #define bit_set(a,b)    ((a) |  ((uinteger)1 << (b)))
188
189 #define abort_() { sig_die("Fortran abort routine called", 1); }
190 #define c_abs(z) (cabsf(Cf(z)))
191 #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
192 #ifdef _MSC_VER
193 #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
194 #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/df(b)._Val[1]);}
195 #else
196 #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
197 #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
198 #endif
199 #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
200 #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
201 #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
202 //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
203 #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
204 #define d_abs(x) (fabs(*(x)))
205 #define d_acos(x) (acos(*(x)))
206 #define d_asin(x) (asin(*(x)))
207 #define d_atan(x) (atan(*(x)))
208 #define d_atn2(x, y) (atan2(*(x),*(y)))
209 #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
210 #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
211 #define d_cos(x) (cos(*(x)))
212 #define d_cosh(x) (cosh(*(x)))
213 #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
214 #define d_exp(x) (exp(*(x)))
215 #define d_imag(z) (cimag(Cd(z)))
216 #define r_imag(z) (cimagf(Cf(z)))
217 #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
218 #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
219 #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
220 #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
221 #define d_log(x) (log(*(x)))
222 #define d_mod(x, y) (fmod(*(x), *(y)))
223 #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
224 #define d_nint(x) u_nint(*(x))
225 #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
226 #define d_sign(a,b) u_sign(*(a),*(b))
227 #define r_sign(a,b) u_sign(*(a),*(b))
228 #define d_sin(x) (sin(*(x)))
229 #define d_sinh(x) (sinh(*(x)))
230 #define d_sqrt(x) (sqrt(*(x)))
231 #define d_tan(x) (tan(*(x)))
232 #define d_tanh(x) (tanh(*(x)))
233 #define i_abs(x) abs(*(x))
234 #define i_dnnt(x) ((integer)u_nint(*(x)))
235 #define i_len(s, n) (n)
236 #define i_nint(x) ((integer)u_nint(*(x)))
237 #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
238 #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
239 #define pow_si(B,E) spow_ui(*(B),*(E))
240 #define pow_ri(B,E) spow_ui(*(B),*(E))
241 #define pow_di(B,E) dpow_ui(*(B),*(E))
242 #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
243 #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
244 #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
245 #define s_cat(lpp, rpp, rnp, np, llp) {         ftnlen i, nc, ll; char *f__rp, *lp;     ll = (llp); lp = (lpp);         for(i=0; i < (int)*(np); ++i) {                 nc = ll;                if((rnp)[i] < nc) nc = (rnp)[i];                ll -= nc;               f__rp = (rpp)[i];               while(--nc >= 0) *lp++ = *(f__rp)++;         }  while(--ll >= 0) *lp++ = ' '; }
246 #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
247 #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
248 #define sig_die(s, kill) { exit(1); }
249 #define s_stop(s, n) {exit(0);}
250 static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
251 #define z_abs(z) (cabs(Cd(z)))
252 #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
253 #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
254 #define myexit_() break;
255 #define mycycle() continue;
256 #define myceiling(w) {ceil(w)}
257 #define myhuge(w) {HUGE_VAL}
258 //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
259 #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
260
261 /* procedure parameter types for -A and -C++ */
262
263 #define F2C_proc_par_types 1
264 #ifdef __cplusplus
265 typedef logical (*L_fp)(...);
266 #else
267 typedef logical (*L_fp)();
268 #endif
269
270 static float spow_ui(float x, integer n) {
271         float pow=1.0; unsigned long int u;
272         if(n != 0) {
273                 if(n < 0) n = -n, x = 1/x;
274                 for(u = n; ; ) {
275                         if(u & 01) pow *= x;
276                         if(u >>= 1) x *= x;
277                         else break;
278                 }
279         }
280         return pow;
281 }
282 static double dpow_ui(double x, integer n) {
283         double pow=1.0; unsigned long int u;
284         if(n != 0) {
285                 if(n < 0) n = -n, x = 1/x;
286                 for(u = n; ; ) {
287                         if(u & 01) pow *= x;
288                         if(u >>= 1) x *= x;
289                         else break;
290                 }
291         }
292         return pow;
293 }
294 #ifdef _MSC_VER
295 static _Fcomplex cpow_ui(complex x, integer n) {
296         complex pow={1.0,0.0}; unsigned long int u;
297                 if(n != 0) {
298                 if(n < 0) n = -n, x.r = 1/x.r, x.i=1/x.i;
299                 for(u = n; ; ) {
300                         if(u & 01) pow.r *= x.r, pow.i *= x.i;
301                         if(u >>= 1) x.r *= x.r, x.i *= x.i;
302                         else break;
303                 }
304         }
305         _Fcomplex p={pow.r, pow.i};
306         return p;
307 }
308 #else
309 static _Complex float cpow_ui(_Complex float x, integer n) {
310         _Complex float pow=1.0; unsigned long int u;
311         if(n != 0) {
312                 if(n < 0) n = -n, x = 1/x;
313                 for(u = n; ; ) {
314                         if(u & 01) pow *= x;
315                         if(u >>= 1) x *= x;
316                         else break;
317                 }
318         }
319         return pow;
320 }
321 #endif
322 #ifdef _MSC_VER
323 static _Dcomplex zpow_ui(_Dcomplex x, integer n) {
324         _Dcomplex pow={1.0,0.0}; unsigned long int u;
325         if(n != 0) {
326                 if(n < 0) n = -n, x._Val[0] = 1/x._Val[0], x._Val[1] =1/x._Val[1];
327                 for(u = n; ; ) {
328                         if(u & 01) pow._Val[0] *= x._Val[0], pow._Val[1] *= x._Val[1];
329                         if(u >>= 1) x._Val[0] *= x._Val[0], x._Val[1] *= x._Val[1];
330                         else break;
331                 }
332         }
333         _Dcomplex p = {pow._Val[0], pow._Val[1]};
334         return p;
335 }
336 #else
337 static _Complex double zpow_ui(_Complex double x, integer n) {
338         _Complex double pow=1.0; unsigned long int u;
339         if(n != 0) {
340                 if(n < 0) n = -n, x = 1/x;
341                 for(u = n; ; ) {
342                         if(u & 01) pow *= x;
343                         if(u >>= 1) x *= x;
344                         else break;
345                 }
346         }
347         return pow;
348 }
349 #endif
350 static integer pow_ii(integer x, integer n) {
351         integer pow; unsigned long int u;
352         if (n <= 0) {
353                 if (n == 0 || x == 1) pow = 1;
354                 else if (x != -1) pow = x == 0 ? 1/x : 0;
355                 else n = -n;
356         }
357         if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
358                 u = n;
359                 for(pow = 1; ; ) {
360                         if(u & 01) pow *= x;
361                         if(u >>= 1) x *= x;
362                         else break;
363                 }
364         }
365         return pow;
366 }
367 static integer dmaxloc_(double *w, integer s, integer e, integer *n)
368 {
369         double m; integer i, mi;
370         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
371                 if (w[i-1]>m) mi=i ,m=w[i-1];
372         return mi-s+1;
373 }
374 static integer smaxloc_(float *w, integer s, integer e, integer *n)
375 {
376         float m; integer i, mi;
377         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
378                 if (w[i-1]>m) mi=i ,m=w[i-1];
379         return mi-s+1;
380 }
381 static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
382         integer n = *n_, incx = *incx_, incy = *incy_, i;
383 #ifdef _MSC_VER
384         _Fcomplex zdotc = {0.0, 0.0};
385         if (incx == 1 && incy == 1) {
386                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
387                         zdotc._Val[0] += conjf(Cf(&x[i]))._Val[0] * Cf(&y[i])._Val[0];
388                         zdotc._Val[1] += conjf(Cf(&x[i]))._Val[1] * Cf(&y[i])._Val[1];
389                 }
390         } else {
391                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
392                         zdotc._Val[0] += conjf(Cf(&x[i*incx]))._Val[0] * Cf(&y[i*incy])._Val[0];
393                         zdotc._Val[1] += conjf(Cf(&x[i*incx]))._Val[1] * Cf(&y[i*incy])._Val[1];
394                 }
395         }
396         pCf(z) = zdotc;
397 }
398 #else
399         _Complex float zdotc = 0.0;
400         if (incx == 1 && incy == 1) {
401                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
402                         zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
403                 }
404         } else {
405                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
406                         zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
407                 }
408         }
409         pCf(z) = zdotc;
410 }
411 #endif
412 static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
413         integer n = *n_, incx = *incx_, incy = *incy_, i;
414 #ifdef _MSC_VER
415         _Dcomplex zdotc = {0.0, 0.0};
416         if (incx == 1 && incy == 1) {
417                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
418                         zdotc._Val[0] += conj(Cd(&x[i]))._Val[0] * Cd(&y[i])._Val[0];
419                         zdotc._Val[1] += conj(Cd(&x[i]))._Val[1] * Cd(&y[i])._Val[1];
420                 }
421         } else {
422                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
423                         zdotc._Val[0] += conj(Cd(&x[i*incx]))._Val[0] * Cd(&y[i*incy])._Val[0];
424                         zdotc._Val[1] += conj(Cd(&x[i*incx]))._Val[1] * Cd(&y[i*incy])._Val[1];
425                 }
426         }
427         pCd(z) = zdotc;
428 }
429 #else
430         _Complex double zdotc = 0.0;
431         if (incx == 1 && incy == 1) {
432                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
433                         zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
434                 }
435         } else {
436                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
437                         zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
438                 }
439         }
440         pCd(z) = zdotc;
441 }
442 #endif  
443 static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
444         integer n = *n_, incx = *incx_, incy = *incy_, i;
445 #ifdef _MSC_VER
446         _Fcomplex zdotc = {0.0, 0.0};
447         if (incx == 1 && incy == 1) {
448                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
449                         zdotc._Val[0] += Cf(&x[i])._Val[0] * Cf(&y[i])._Val[0];
450                         zdotc._Val[1] += Cf(&x[i])._Val[1] * Cf(&y[i])._Val[1];
451                 }
452         } else {
453                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
454                         zdotc._Val[0] += Cf(&x[i*incx])._Val[0] * Cf(&y[i*incy])._Val[0];
455                         zdotc._Val[1] += Cf(&x[i*incx])._Val[1] * Cf(&y[i*incy])._Val[1];
456                 }
457         }
458         pCf(z) = zdotc;
459 }
460 #else
461         _Complex float zdotc = 0.0;
462         if (incx == 1 && incy == 1) {
463                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
464                         zdotc += Cf(&x[i]) * Cf(&y[i]);
465                 }
466         } else {
467                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
468                         zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
469                 }
470         }
471         pCf(z) = zdotc;
472 }
473 #endif
474 static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
475         integer n = *n_, incx = *incx_, incy = *incy_, i;
476 #ifdef _MSC_VER
477         _Dcomplex zdotc = {0.0, 0.0};
478         if (incx == 1 && incy == 1) {
479                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
480                         zdotc._Val[0] += Cd(&x[i])._Val[0] * Cd(&y[i])._Val[0];
481                         zdotc._Val[1] += Cd(&x[i])._Val[1] * Cd(&y[i])._Val[1];
482                 }
483         } else {
484                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
485                         zdotc._Val[0] += Cd(&x[i*incx])._Val[0] * Cd(&y[i*incy])._Val[0];
486                         zdotc._Val[1] += Cd(&x[i*incx])._Val[1] * Cd(&y[i*incy])._Val[1];
487                 }
488         }
489         pCd(z) = zdotc;
490 }
491 #else
492         _Complex double zdotc = 0.0;
493         if (incx == 1 && incy == 1) {
494                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
495                         zdotc += Cd(&x[i]) * Cd(&y[i]);
496                 }
497         } else {
498                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
499                         zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
500                 }
501         }
502         pCd(z) = zdotc;
503 }
504 #endif
505 /*  -- translated by f2c (version 20000121).
506    You must link the resulting object file with the libraries:
507         -lf2c -lm   (in that order)
508 */
509
510
511
512
513 /* Table of constant values */
514
515 static real c_b17 = 0.f;
516 static real c_b18 = 1.f;
517 static integer c__1 = 1;
518 static integer c__0 = 0;
519 static integer c__2 = 2;
520
521 /* > \brief \b SGESVJ */
522
523 /*  =========== DOCUMENTATION =========== */
524
525 /* Online html documentation available at */
526 /*            http://www.netlib.org/lapack/explore-html/ */
527
528 /* > \htmlonly */
529 /* > Download SGESVJ + dependencies */
530 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/sgesvj.
531 f"> */
532 /* > [TGZ]</a> */
533 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/sgesvj.
534 f"> */
535 /* > [ZIP]</a> */
536 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/sgesvj.
537 f"> */
538 /* > [TXT]</a> */
539 /* > \endhtmlonly */
540
541 /*  Definition: */
542 /*  =========== */
543
544 /*       SUBROUTINE SGESVJ( 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 /*       REAL               A( LDA, * ), SVA( N ), V( LDV, * ), */
550 /*      $                   WORK( LWORK ) */
551
552
553 /* > \par Purpose: */
554 /*  ============= */
555 /* > */
556 /* > \verbatim */
557 /* > */
558 /* > SGESVJ 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 /* > SGESVJ 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=SQRT(M), EPS=SLAMCH('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/SLAMCH('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 REAL 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 SLAMCH('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=SQRT(M)*EPS (default); or TOL=CTOL*EPS (JOBU = 'C'), */
652 /* >                 see the description of JOBU. */
653 /* >                 If INFO > 0, */
654 /* >                 the procedure SGESVJ 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 /* >          If JOBU = 'N': */
662 /* >                 If INFO = 0: */
663 /* >                 Note that the left singular vectors are 'for free' in the */
664 /* >                 one-sided Jacobi SVD algorithm. However, if only the */
665 /* >                 singular values are needed, the level of numerical */
666 /* >                 orthogonality of U is not an issue and iterations are */
667 /* >                 stopped when the columns of the iterated matrix are */
668 /* >                 numerically orthogonal up to approximately M*EPS. Thus, */
669 /* >                 on exit, A contains the columns of U scaled with the */
670 /* >                 corresponding singular values. */
671 /* >                 If INFO > 0: */
672 /* >                 the procedure SGESVJ did not converge in the given number */
673 /* >                 of iterations (sweeps). */
674 /* > \endverbatim */
675 /* > */
676 /* > \param[in] LDA */
677 /* > \verbatim */
678 /* >          LDA is INTEGER */
679 /* >          The leading dimension of the array A.  LDA >= f2cmax(1,M). */
680 /* > \endverbatim */
681 /* > */
682 /* > \param[out] SVA */
683 /* > \verbatim */
684 /* >          SVA is REAL array, dimension (N) */
685 /* >          On exit, */
686 /* >          If INFO = 0 : */
687 /* >          depending on the value SCALE = WORK(1), we have: */
688 /* >                 If SCALE = ONE: */
689 /* >                 SVA(1:N) contains the computed singular values of A. */
690 /* >                 During the computation SVA contains the Euclidean column */
691 /* >                 norms of the iterated matrices in the array A. */
692 /* >                 If SCALE .NE. ONE: */
693 /* >                 The singular values of A are SCALE*SVA(1:N), and this */
694 /* >                 factored representation is due to the fact that some of the */
695 /* >                 singular values of A might underflow or overflow. */
696 /* > */
697 /* >          If INFO > 0 : */
698 /* >          the procedure SGESVJ 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 SGESVJ */
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 REAL 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 REAL 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=SLAMCH('E'). */
736 /* >                    It is required that CTOL >= ONE, i.e. it is not */
737 /* >                    allowed to force the routine to obtain orthogonality */
738 /* >                    below EPSILON. */
739 /* >          On exit, */
740 /* >          WORK(1) = SCALE is the scaling factor such that SCALE*SVA(1:N) */
741 /* >                    are the computed singular vcalues 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 SGESVJ 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:  SGESVJ 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 realGEcomputational */
785
786 /* > \par Further Details: */
787 /*  ===================== */
788 /* > */
789 /* > The orthogonal N-by-N matrix V is obtained as a product of Jacobi plane */
790 /* > rotations. The rotations are implemented as fast scaled rotations of */
791 /* > Anda and Park [1]. In the case of underflow of the Jacobi angle, a */
792 /* > modified Jacobi transformation of Drmac [4] is used. Pivot strategy uses */
793 /* > column interchanges of de Rijk [2]. The relative accuracy of the computed */
794 /* > singular values and the accuracy of the computed singular vectors (in */
795 /* > angle metric) is as guaranteed by the theory of Demmel and Veselic [3]. */
796 /* > The condition number that determines the accuracy in the full rank case */
797 /* > is essentially min_{D=diag} kappa(A*D), where kappa(.) is the */
798 /* > spectral condition number. The best performance of this Jacobi SVD */
799 /* > procedure is achieved if used in an  accelerated version of Drmac and */
800 /* > Veselic [5,6], and it is the kernel routine in the SIGMA library [7]. */
801 /* > Some tunning parameters (marked with [TP]) are available for the */
802 /* > implementer. \n */
803 /* > The computational range for the nonzero singular values is the  machine */
804 /* > number interval ( UNDERFLOW , OVERFLOW ). In extreme cases, even */
805 /* > denormalized singular values can be computed with the corresponding */
806 /* > gradual loss of accurate digits. */
807 /* > */
808 /* > \par Contributors: */
809 /*  ================== */
810 /* > */
811 /* > Zlatko Drmac (Zagreb, Croatia) and Kresimir Veselic (Hagen, Germany) */
812 /* > */
813 /* > \par References: */
814 /*  ================ */
815 /* > */
816 /* > [1] A. A. Anda and H. Park: Fast plane rotations with dynamic scaling. \n */
817 /* >    SIAM J. matrix Anal. Appl., Vol. 15 (1994), pp. 162-174. \n\n */
818 /* > [2] P. P. M. De Rijk: A one-sided Jacobi algorithm for computing the */
819 /* >    singular value decomposition on a vector computer. \n */
820 /* >    SIAM J. Sci. Stat. Comp., Vol. 10 (1998), pp. 359-371. \n\n */
821 /* > [3] J. Demmel and K. Veselic: Jacobi method is more accurate than QR. \n */
822 /* > [4] Z. Drmac: Implementation of Jacobi rotations for accurate singular */
823 /* >    value computation in floating point arithmetic. \n */
824 /* >    SIAM J. Sci. Comp., Vol. 18 (1997), pp. 1200-1222. \n\n */
825 /* > [5] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm I. \n */
826 /* >    SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1322-1342. \n */
827 /* >    LAPACK Working note 169. \n\n */
828 /* > [6] Z. Drmac and K. Veselic: New fast and accurate Jacobi SVD algorithm II. \n */
829 /* >    SIAM J. Matrix Anal. Appl. Vol. 35, No. 2 (2008), pp. 1343-1362. \n */
830 /* >    LAPACK Working note 170. \n\n */
831 /* > [7] Z. Drmac: SIGMA - mathematical software library for accurate SVD, PSV, */
832 /* >    QSVD, (H,K)-SVD computations.\n */
833 /* >    Department of Mathematics, University of Zagreb, 2008. */
834 /* > */
835 /* > \par Bugs, Examples and Comments: */
836 /*  ================================= */
837 /* > */
838 /* > Please report all bugs and send interesting test examples and comments to */
839 /* > drmac@math.hr. Thank you. */
840
841 /*  ===================================================================== */
842 /* Subroutine */ int sgesvj_(char *joba, char *jobu, char *jobv, integer *m, 
843         integer *n, real *a, integer *lda, real *sva, integer *mv, real *v, 
844         integer *ldv, real *work, integer *lwork, integer *info)
845 {
846     /* System generated locals */
847     integer a_dim1, a_offset, v_dim1, v_offset, i__1, i__2, i__3, i__4, i__5;
848     real r__1, r__2;
849
850     /* Local variables */
851     real aapp, aapq, aaqq, ctol;
852     integer ierr;
853     real bigtheta;
854     extern real sdot_(integer *, real *, integer *, real *, integer *);
855     integer pskipped;
856     real aapp0, temp1;
857     extern real snrm2_(integer *, real *, integer *);
858     integer i__, p, q;
859     real t, large, apoaq, aqoap;
860     extern logical lsame_(char *, char *);
861     real theta;
862     extern /* Subroutine */ int sscal_(integer *, real *, real *, integer *);
863     real small, sfmin;
864     logical lsvec;
865     real fastr[5], epsln;
866     logical applv, rsvec, uctol, lower, upper;
867     extern /* Subroutine */ int scopy_(integer *, real *, integer *, real *, 
868             integer *);
869     logical rotok;
870     integer n2;
871     extern /* Subroutine */ int sswap_(integer *, real *, integer *, real *, 
872             integer *);
873     integer n4;
874     extern /* Subroutine */ int saxpy_(integer *, real *, real *, integer *, 
875             real *, integer *), srotm_(integer *, real *, integer *, real *, 
876             integer *, real *);
877     real rootsfmin;
878     extern /* Subroutine */ int sgsvj0_(char *, integer *, integer *, real *, 
879             integer *, real *, real *, integer *, real *, integer *, real *, 
880             real *, real *, integer *, real *, integer *, integer *), 
881             sgsvj1_(char *, integer *, integer *, integer *, real *, integer *
882             , real *, real *, integer *, real *, integer *, real *, real *, 
883             real *, integer *, real *, integer *, integer *);
884     integer n34;
885     real cs, sn;
886     extern real slamch_(char *);
887     extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
888     integer ijblsk, swband;
889     extern /* Subroutine */ int slascl_(char *, integer *, integer *, real *, 
890             real *, integer *, integer *, real *, integer *, integer *);
891     extern integer isamax_(integer *, real *, integer *);
892     integer blskip;
893     real mxaapq;
894     extern /* Subroutine */ int slaset_(char *, integer *, integer *, real *, 
895             real *, real *, integer *);
896     real thsign;
897     extern /* Subroutine */ int slassq_(integer *, real *, integer *, real *, 
898             real *);
899     real mxsinj;
900     integer ir1, emptsw, notrot, iswrot, jbc;
901     real big;
902     integer kbl, lkahead, igl, ibr, jgl, nbl;
903     real skl;
904     logical goscale;
905     real tol;
906     integer mvl;
907     logical noscale;
908     real rootbig, rooteps;
909     integer rowskip;
910     real roottol;
911
912
913 /*  -- LAPACK computational routine (version 3.7.1) -- */
914 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
915 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
916 /*     June 2017 */
917
918
919 /*  ===================================================================== */
920
921 /*     from BLAS */
922 /*     from LAPACK */
923 /*     from BLAS */
924 /*     from LAPACK */
925
926
927 /*     Test the input arguments */
928
929     /* Parameter adjustments */
930     --sva;
931     a_dim1 = *lda;
932     a_offset = 1 + a_dim1 * 1;
933     a -= a_offset;
934     v_dim1 = *ldv;
935     v_offset = 1 + v_dim1 * 1;
936     v -= v_offset;
937     --work;
938
939     /* Function Body */
940     lsvec = lsame_(jobu, "U");
941     uctol = lsame_(jobu, "C");
942     rsvec = lsame_(jobv, "V");
943     applv = lsame_(jobv, "A");
944     upper = lsame_(joba, "U");
945     lower = lsame_(joba, "L");
946
947     if (! (upper || lower || lsame_(joba, "G"))) {
948         *info = -1;
949     } else if (! (lsvec || uctol || lsame_(jobu, "N"))) 
950             {
951         *info = -2;
952     } else if (! (rsvec || applv || lsame_(jobv, "N"))) 
953             {
954         *info = -3;
955     } else if (*m < 0) {
956         *info = -4;
957     } else if (*n < 0 || *n > *m) {
958         *info = -5;
959     } else if (*lda < *m) {
960         *info = -7;
961     } else if (*mv < 0) {
962         *info = -9;
963     } else if (rsvec && *ldv < *n || applv && *ldv < *mv) {
964         *info = -11;
965     } else if (uctol && work[1] <= 1.f) {
966         *info = -12;
967     } else /* if(complicated condition) */ {
968 /* Computing MAX */
969         i__1 = *m + *n;
970         if (*lwork < f2cmax(i__1,6)) {
971             *info = -13;
972         } else {
973             *info = 0;
974         }
975     }
976
977 /*     #:( */
978     if (*info != 0) {
979         i__1 = -(*info);
980         xerbla_("SGESVJ", &i__1, (ftnlen)6);
981         return 0;
982     }
983
984 /* #:) Quick return for void matrix */
985
986     if (*m == 0 || *n == 0) {
987         return 0;
988     }
989
990 /*     Set numerical parameters */
991 /*     The stopping criterion for Jacobi rotations is */
992
993 /*     max_{i<>j}|A(:,i)^T * A(:,j)|/(||A(:,i)||*||A(:,j)||) < CTOL*EPS */
994
995 /*     where EPS is the round-off and CTOL is defined as follows: */
996
997     if (uctol) {
998 /*        ... user controlled */
999         ctol = work[1];
1000     } else {
1001 /*        ... default */
1002         if (lsvec || rsvec || applv) {
1003             ctol = sqrt((real) (*m));
1004         } else {
1005             ctol = (real) (*m);
1006         }
1007     }
1008 /*     ... and the machine dependent parameters are */
1009 /* [!]  (Make sure that SLAMCH() works properly on the target machine.) */
1010
1011     epsln = slamch_("Epsilon");
1012     rooteps = sqrt(epsln);
1013     sfmin = slamch_("SafeMinimum");
1014     rootsfmin = sqrt(sfmin);
1015     small = sfmin / epsln;
1016     big = slamch_("Overflow");
1017 /*     BIG         = ONE    / SFMIN */
1018     rootbig = 1.f / rootsfmin;
1019     large = big / sqrt((real) (*m * *n));
1020     bigtheta = 1.f / rooteps;
1021
1022     tol = ctol * epsln;
1023     roottol = sqrt(tol);
1024
1025     if ((real) (*m) * epsln >= 1.f) {
1026         *info = -4;
1027         i__1 = -(*info);
1028         xerbla_("SGESVJ", &i__1, (ftnlen)6);
1029         return 0;
1030     }
1031
1032 /*     Initialize the right singular vector matrix. */
1033
1034     if (rsvec) {
1035         mvl = *n;
1036         slaset_("A", &mvl, n, &c_b17, &c_b18, &v[v_offset], ldv);
1037     } else if (applv) {
1038         mvl = *mv;
1039     }
1040     rsvec = rsvec || applv;
1041
1042 /*     Initialize SVA( 1:N ) = ( ||A e_i||_2, i = 1:N ) */
1043 /* (!)  If necessary, scale A to protect the largest singular value */
1044 /*     from overflow. It is possible that saving the largest singular */
1045 /*     value destroys the information about the small ones. */
1046 /*     This initial scaling is almost minimal in the sense that the */
1047 /*     goal is to make sure that no column norm overflows, and that */
1048 /*     SQRT(N)*max_i SVA(i) does not overflow. If INFinite entries */
1049 /*     in A are detected, the procedure returns with INFO=-6. */
1050
1051     skl = 1.f / sqrt((real) (*m) * (real) (*n));
1052     noscale = TRUE_;
1053     goscale = TRUE_;
1054
1055     if (lower) {
1056 /*        the input matrix is M-by-N lower triangular (trapezoidal) */
1057         i__1 = *n;
1058         for (p = 1; p <= i__1; ++p) {
1059             aapp = 0.f;
1060             aaqq = 1.f;
1061             i__2 = *m - p + 1;
1062             slassq_(&i__2, &a[p + p * a_dim1], &c__1, &aapp, &aaqq);
1063             if (aapp > big) {
1064                 *info = -6;
1065                 i__2 = -(*info);
1066                 xerbla_("SGESVJ", &i__2, (ftnlen)6);
1067                 return 0;
1068             }
1069             aaqq = sqrt(aaqq);
1070             if (aapp < big / aaqq && noscale) {
1071                 sva[p] = aapp * aaqq;
1072             } else {
1073                 noscale = FALSE_;
1074                 sva[p] = aapp * (aaqq * skl);
1075                 if (goscale) {
1076                     goscale = FALSE_;
1077                     i__2 = p - 1;
1078                     for (q = 1; q <= i__2; ++q) {
1079                         sva[q] *= skl;
1080 /* L1873: */
1081                     }
1082                 }
1083             }
1084 /* L1874: */
1085         }
1086     } else if (upper) {
1087 /*        the input matrix is M-by-N upper triangular (trapezoidal) */
1088         i__1 = *n;
1089         for (p = 1; p <= i__1; ++p) {
1090             aapp = 0.f;
1091             aaqq = 1.f;
1092             slassq_(&p, &a[p * a_dim1 + 1], &c__1, &aapp, &aaqq);
1093             if (aapp > big) {
1094                 *info = -6;
1095                 i__2 = -(*info);
1096                 xerbla_("SGESVJ", &i__2, (ftnlen)6);
1097                 return 0;
1098             }
1099             aaqq = sqrt(aaqq);
1100             if (aapp < big / aaqq && noscale) {
1101                 sva[p] = aapp * aaqq;
1102             } else {
1103                 noscale = FALSE_;
1104                 sva[p] = aapp * (aaqq * skl);
1105                 if (goscale) {
1106                     goscale = FALSE_;
1107                     i__2 = p - 1;
1108                     for (q = 1; q <= i__2; ++q) {
1109                         sva[q] *= skl;
1110 /* L2873: */
1111                     }
1112                 }
1113             }
1114 /* L2874: */
1115         }
1116     } else {
1117 /*        the input matrix is M-by-N general dense */
1118         i__1 = *n;
1119         for (p = 1; p <= i__1; ++p) {
1120             aapp = 0.f;
1121             aaqq = 1.f;
1122             slassq_(m, &a[p * a_dim1 + 1], &c__1, &aapp, &aaqq);
1123             if (aapp > big) {
1124                 *info = -6;
1125                 i__2 = -(*info);
1126                 xerbla_("SGESVJ", &i__2, (ftnlen)6);
1127                 return 0;
1128             }
1129             aaqq = sqrt(aaqq);
1130             if (aapp < big / aaqq && noscale) {
1131                 sva[p] = aapp * aaqq;
1132             } else {
1133                 noscale = FALSE_;
1134                 sva[p] = aapp * (aaqq * skl);
1135                 if (goscale) {
1136                     goscale = FALSE_;
1137                     i__2 = p - 1;
1138                     for (q = 1; q <= i__2; ++q) {
1139                         sva[q] *= skl;
1140 /* L3873: */
1141                     }
1142                 }
1143             }
1144 /* L3874: */
1145         }
1146     }
1147
1148     if (noscale) {
1149         skl = 1.f;
1150     }
1151
1152 /*     Move the smaller part of the spectrum from the underflow threshold */
1153 /* (!)  Start by determining the position of the nonzero entries of the */
1154 /*     array SVA() relative to ( SFMIN, BIG ). */
1155
1156     aapp = 0.f;
1157     aaqq = big;
1158     i__1 = *n;
1159     for (p = 1; p <= i__1; ++p) {
1160         if (sva[p] != 0.f) {
1161 /* Computing MIN */
1162             r__1 = aaqq, r__2 = sva[p];
1163             aaqq = f2cmin(r__1,r__2);
1164         }
1165 /* Computing MAX */
1166         r__1 = aapp, r__2 = sva[p];
1167         aapp = f2cmax(r__1,r__2);
1168 /* L4781: */
1169     }
1170
1171 /* #:) Quick return for zero matrix */
1172
1173     if (aapp == 0.f) {
1174         if (lsvec) {
1175             slaset_("G", m, n, &c_b17, &c_b18, &a[a_offset], lda);
1176         }
1177         work[1] = 1.f;
1178         work[2] = 0.f;
1179         work[3] = 0.f;
1180         work[4] = 0.f;
1181         work[5] = 0.f;
1182         work[6] = 0.f;
1183         return 0;
1184     }
1185
1186 /* #:) Quick return for one-column matrix */
1187
1188     if (*n == 1) {
1189         if (lsvec) {
1190             slascl_("G", &c__0, &c__0, &sva[1], &skl, m, &c__1, &a[a_dim1 + 1]
1191                     , lda, &ierr);
1192         }
1193         work[1] = 1.f / skl;
1194         if (sva[1] >= sfmin) {
1195             work[2] = 1.f;
1196         } else {
1197             work[2] = 0.f;
1198         }
1199         work[3] = 0.f;
1200         work[4] = 0.f;
1201         work[5] = 0.f;
1202         work[6] = 0.f;
1203         return 0;
1204     }
1205
1206 /*     Protect small singular values from underflow, and try to */
1207 /*     avoid underflows/overflows in computing Jacobi rotations. */
1208
1209     sn = sqrt(sfmin / epsln);
1210     temp1 = sqrt(big / (real) (*n));
1211     if (aapp <= sn || aaqq >= temp1 || sn <= aaqq && aapp <= temp1) {
1212 /* Computing MIN */
1213         r__1 = big, r__2 = temp1 / aapp;
1214         temp1 = f2cmin(r__1,r__2);
1215 /*         AAQQ  = AAQQ*TEMP1 */
1216 /*         AAPP  = AAPP*TEMP1 */
1217     } else if (aaqq <= sn && aapp <= temp1) {
1218 /* Computing MIN */
1219         r__1 = sn / aaqq, r__2 = big / (aapp * sqrt((real) (*n)));
1220         temp1 = f2cmin(r__1,r__2);
1221 /*         AAQQ  = AAQQ*TEMP1 */
1222 /*         AAPP  = AAPP*TEMP1 */
1223     } else if (aaqq >= sn && aapp >= temp1) {
1224 /* Computing MAX */
1225         r__1 = sn / aaqq, r__2 = temp1 / aapp;
1226         temp1 = f2cmax(r__1,r__2);
1227 /*         AAQQ  = AAQQ*TEMP1 */
1228 /*         AAPP  = AAPP*TEMP1 */
1229     } else if (aaqq <= sn && aapp >= temp1) {
1230 /* Computing MIN */
1231         r__1 = sn / aaqq, r__2 = big / (sqrt((real) (*n)) * aapp);
1232         temp1 = f2cmin(r__1,r__2);
1233 /*         AAQQ  = AAQQ*TEMP1 */
1234 /*         AAPP  = AAPP*TEMP1 */
1235     } else {
1236         temp1 = 1.f;
1237     }
1238
1239 /*     Scale, if necessary */
1240
1241     if (temp1 != 1.f) {
1242         slascl_("G", &c__0, &c__0, &c_b18, &temp1, n, &c__1, &sva[1], n, &
1243                 ierr);
1244     }
1245     skl = temp1 * skl;
1246     if (skl != 1.f) {
1247         slascl_(joba, &c__0, &c__0, &c_b18, &skl, m, n, &a[a_offset], lda, &
1248                 ierr);
1249         skl = 1.f / skl;
1250     }
1251
1252 /*     Row-cyclic Jacobi SVD algorithm with column pivoting */
1253
1254     emptsw = *n * (*n - 1) / 2;
1255     notrot = 0;
1256     fastr[0] = 0.f;
1257
1258 /*     A is represented in factored form A = A * diag(WORK), where diag(WORK) */
1259 /*     is initialized to identity. WORK is updated during fast scaled */
1260 /*     rotations. */
1261
1262     i__1 = *n;
1263     for (q = 1; q <= i__1; ++q) {
1264         work[q] = 1.f;
1265 /* L1868: */
1266     }
1267
1268
1269     swband = 3;
1270 /* [TP] SWBAND is a tuning parameter [TP]. It is meaningful and effective */
1271 /*     if SGESVJ is used as a computational routine in the preconditioned */
1272 /*     Jacobi SVD algorithm SGESVJ. For sweeps i=1:SWBAND the procedure */
1273 /*     works on pivots inside a band-like region around the diagonal. */
1274 /*     The boundaries are determined dynamically, based on the number of */
1275 /*     pivots above a threshold. */
1276
1277     kbl = f2cmin(8,*n);
1278 /* [TP] KBL is a tuning parameter that defines the tile size in the */
1279 /*     tiling of the p-q loops of pivot pairs. In general, an optimal */
1280 /*     value of KBL depends on the matrix dimensions and on the */
1281 /*     parameters of the computer's memory. */
1282
1283     nbl = *n / kbl;
1284     if (nbl * kbl != *n) {
1285         ++nbl;
1286     }
1287
1288 /* Computing 2nd power */
1289     i__1 = kbl;
1290     blskip = i__1 * i__1;
1291 /* [TP] BLKSKIP is a tuning parameter that depends on SWBAND and KBL. */
1292
1293     rowskip = f2cmin(5,kbl);
1294 /* [TP] ROWSKIP is a tuning parameter. */
1295
1296     lkahead = 1;
1297 /* [TP] LKAHEAD is a tuning parameter. */
1298
1299 /*     Quasi block transformations, using the lower (upper) triangular */
1300 /*     structure of the input matrix. The quasi-block-cycling usually */
1301 /*     invokes cubic convergence. Big part of this cycle is done inside */
1302 /*     canonical subspaces of dimensions less than M. */
1303
1304 /* Computing MAX */
1305     i__1 = 64, i__2 = kbl << 2;
1306     if ((lower || upper) && *n > f2cmax(i__1,i__2)) {
1307 /* [TP] The number of partition levels and the actual partition are */
1308 /*     tuning parameters. */
1309         n4 = *n / 4;
1310         n2 = *n / 2;
1311         n34 = n4 * 3;
1312         if (applv) {
1313             q = 0;
1314         } else {
1315             q = 1;
1316         }
1317
1318         if (lower) {
1319
1320 /*     This works very well on lower triangular matrices, in particular */
1321 /*     in the framework of the preconditioned Jacobi SVD (xGEJSV). */
1322 /*     The idea is simple: */
1323 /*     [+ 0 0 0]   Note that Jacobi transformations of [0 0] */
1324 /*     [+ + 0 0]                                       [0 0] */
1325 /*     [+ + x 0]   actually work on [x 0]              [x 0] */
1326 /*     [+ + x x]                    [x x].             [x x] */
1327
1328             i__1 = *m - n34;
1329             i__2 = *n - n34;
1330             i__3 = *lwork - *n;
1331             sgsvj0_(jobv, &i__1, &i__2, &a[n34 + 1 + (n34 + 1) * a_dim1], lda,
1332                      &work[n34 + 1], &sva[n34 + 1], &mvl, &v[n34 * q + 1 + (
1333                     n34 + 1) * v_dim1], ldv, &epsln, &sfmin, &tol, &c__2, &
1334                     work[*n + 1], &i__3, &ierr);
1335
1336             i__1 = *m - n2;
1337             i__2 = n34 - n2;
1338             i__3 = *lwork - *n;
1339             sgsvj0_(jobv, &i__1, &i__2, &a[n2 + 1 + (n2 + 1) * a_dim1], lda, &
1340                     work[n2 + 1], &sva[n2 + 1], &mvl, &v[n2 * q + 1 + (n2 + 1)
1341                      * v_dim1], ldv, &epsln, &sfmin, &tol, &c__2, &work[*n + 
1342                     1], &i__3, &ierr);
1343
1344             i__1 = *m - n2;
1345             i__2 = *n - n2;
1346             i__3 = *lwork - *n;
1347             sgsvj1_(jobv, &i__1, &i__2, &n4, &a[n2 + 1 + (n2 + 1) * a_dim1], 
1348                     lda, &work[n2 + 1], &sva[n2 + 1], &mvl, &v[n2 * q + 1 + (
1349                     n2 + 1) * v_dim1], ldv, &epsln, &sfmin, &tol, &c__1, &
1350                     work[*n + 1], &i__3, &ierr);
1351
1352             i__1 = *m - n4;
1353             i__2 = n2 - n4;
1354             i__3 = *lwork - *n;
1355             sgsvj0_(jobv, &i__1, &i__2, &a[n4 + 1 + (n4 + 1) * a_dim1], lda, &
1356                     work[n4 + 1], &sva[n4 + 1], &mvl, &v[n4 * q + 1 + (n4 + 1)
1357                      * v_dim1], ldv, &epsln, &sfmin, &tol, &c__1, &work[*n + 
1358                     1], &i__3, &ierr);
1359
1360             i__1 = *lwork - *n;
1361             sgsvj0_(jobv, m, &n4, &a[a_offset], lda, &work[1], &sva[1], &mvl, 
1362                     &v[v_offset], ldv, &epsln, &sfmin, &tol, &c__1, &work[*n 
1363                     + 1], &i__1, &ierr);
1364
1365             i__1 = *lwork - *n;
1366             sgsvj1_(jobv, m, &n2, &n4, &a[a_offset], lda, &work[1], &sva[1], &
1367                     mvl, &v[v_offset], ldv, &epsln, &sfmin, &tol, &c__1, &
1368                     work[*n + 1], &i__1, &ierr);
1369
1370
1371         } else if (upper) {
1372
1373
1374             i__1 = *lwork - *n;
1375             sgsvj0_(jobv, &n4, &n4, &a[a_offset], lda, &work[1], &sva[1], &
1376                     mvl, &v[v_offset], ldv, &epsln, &sfmin, &tol, &c__2, &
1377                     work[*n + 1], &i__1, &ierr);
1378
1379             i__1 = *lwork - *n;
1380             sgsvj0_(jobv, &n2, &n4, &a[(n4 + 1) * a_dim1 + 1], lda, &work[n4 
1381                     + 1], &sva[n4 + 1], &mvl, &v[n4 * q + 1 + (n4 + 1) * 
1382                     v_dim1], ldv, &epsln, &sfmin, &tol, &c__1, &work[*n + 1], 
1383                     &i__1, &ierr);
1384
1385             i__1 = *lwork - *n;
1386             sgsvj1_(jobv, &n2, &n2, &n4, &a[a_offset], lda, &work[1], &sva[1],
1387                      &mvl, &v[v_offset], ldv, &epsln, &sfmin, &tol, &c__1, &
1388                     work[*n + 1], &i__1, &ierr);
1389
1390             i__1 = n2 + n4;
1391             i__2 = *lwork - *n;
1392             sgsvj0_(jobv, &i__1, &n4, &a[(n2 + 1) * a_dim1 + 1], lda, &work[
1393                     n2 + 1], &sva[n2 + 1], &mvl, &v[n2 * q + 1 + (n2 + 1) * 
1394                     v_dim1], ldv, &epsln, &sfmin, &tol, &c__1, &work[*n + 1], 
1395                     &i__2, &ierr);
1396         }
1397
1398     }
1399
1400
1401     for (i__ = 1; i__ <= 30; ++i__) {
1402
1403
1404         mxaapq = 0.f;
1405         mxsinj = 0.f;
1406         iswrot = 0;
1407
1408         notrot = 0;
1409         pskipped = 0;
1410
1411 /*     Each sweep is unrolled using KBL-by-KBL tiles over the pivot pairs */
1412 /*     1 <= p < q <= N. This is the first step toward a blocked implementation */
1413 /*     of the rotations. New implementation, based on block transformations, */
1414 /*     is under development. */
1415
1416         i__1 = nbl;
1417         for (ibr = 1; ibr <= i__1; ++ibr) {
1418
1419             igl = (ibr - 1) * kbl + 1;
1420
1421 /* Computing MIN */
1422             i__3 = lkahead, i__4 = nbl - ibr;
1423             i__2 = f2cmin(i__3,i__4);
1424             for (ir1 = 0; ir1 <= i__2; ++ir1) {
1425
1426                 igl += ir1 * kbl;
1427
1428 /* Computing MIN */
1429                 i__4 = igl + kbl - 1, i__5 = *n - 1;
1430                 i__3 = f2cmin(i__4,i__5);
1431                 for (p = igl; p <= i__3; ++p) {
1432
1433
1434                     i__4 = *n - p + 1;
1435                     q = isamax_(&i__4, &sva[p], &c__1) + p - 1;
1436                     if (p != q) {
1437                         sswap_(m, &a[p * a_dim1 + 1], &c__1, &a[q * a_dim1 + 
1438                                 1], &c__1);
1439                         if (rsvec) {
1440                             sswap_(&mvl, &v[p * v_dim1 + 1], &c__1, &v[q * 
1441                                     v_dim1 + 1], &c__1);
1442                         }
1443                         temp1 = sva[p];
1444                         sva[p] = sva[q];
1445                         sva[q] = temp1;
1446                         temp1 = work[p];
1447                         work[p] = work[q];
1448                         work[q] = temp1;
1449                     }
1450
1451                     if (ir1 == 0) {
1452
1453 /*        Column norms are periodically updated by explicit */
1454 /*        norm computation. */
1455 /*        Caveat: */
1456 /*        Unfortunately, some BLAS implementations compute SNRM2(M,A(1,p),1) */
1457 /*        as SQRT(SDOT(M,A(1,p),1,A(1,p),1)), which may cause the result to */
1458 /*        overflow for ||A(:,p)||_2 > SQRT(overflow_threshold), and to */
1459 /*        underflow for ||A(:,p)||_2 < SQRT(underflow_threshold). */
1460 /*        Hence, SNRM2 cannot be trusted, not even in the case when */
1461 /*        the true norm is far from the under(over)flow boundaries. */
1462 /*        If properly implemented SNRM2 is available, the IF-THEN-ELSE */
1463 /*        below should read "AAPP = SNRM2( M, A(1,p), 1 ) * WORK(p)". */
1464
1465                         if (sva[p] < rootbig && sva[p] > rootsfmin) {
1466                             sva[p] = snrm2_(m, &a[p * a_dim1 + 1], &c__1) * 
1467                                     work[p];
1468                         } else {
1469                             temp1 = 0.f;
1470                             aapp = 1.f;
1471                             slassq_(m, &a[p * a_dim1 + 1], &c__1, &temp1, &
1472                                     aapp);
1473                             sva[p] = temp1 * sqrt(aapp) * work[p];
1474                         }
1475                         aapp = sva[p];
1476                     } else {
1477                         aapp = sva[p];
1478                     }
1479
1480                     if (aapp > 0.f) {
1481
1482                         pskipped = 0;
1483
1484 /* Computing MIN */
1485                         i__5 = igl + kbl - 1;
1486                         i__4 = f2cmin(i__5,*n);
1487                         for (q = p + 1; q <= i__4; ++q) {
1488
1489                             aaqq = sva[q];
1490
1491                             if (aaqq > 0.f) {
1492
1493                                 aapp0 = aapp;
1494                                 if (aaqq >= 1.f) {
1495                                     rotok = small * aapp <= aaqq;
1496                                     if (aapp < big / aaqq) {
1497                                         aapq = sdot_(m, &a[p * a_dim1 + 1], &
1498                                                 c__1, &a[q * a_dim1 + 1], &
1499                                                 c__1) * work[p] * work[q] / 
1500                                                 aaqq / aapp;
1501                                     } else {
1502                                         scopy_(m, &a[p * a_dim1 + 1], &c__1, &
1503                                                 work[*n + 1], &c__1);
1504                                         slascl_("G", &c__0, &c__0, &aapp, &
1505                                                 work[p], m, &c__1, &work[*n + 
1506                                                 1], lda, &ierr);
1507                                         aapq = sdot_(m, &work[*n + 1], &c__1, 
1508                                                 &a[q * a_dim1 + 1], &c__1) * 
1509                                                 work[q] / aaqq;
1510                                     }
1511                                 } else {
1512                                     rotok = aapp <= aaqq / small;
1513                                     if (aapp > small / aaqq) {
1514                                         aapq = sdot_(m, &a[p * a_dim1 + 1], &
1515                                                 c__1, &a[q * a_dim1 + 1], &
1516                                                 c__1) * work[p] * work[q] / 
1517                                                 aaqq / aapp;
1518                                     } else {
1519                                         scopy_(m, &a[q * a_dim1 + 1], &c__1, &
1520                                                 work[*n + 1], &c__1);
1521                                         slascl_("G", &c__0, &c__0, &aaqq, &
1522                                                 work[q], m, &c__1, &work[*n + 
1523                                                 1], lda, &ierr);
1524                                         aapq = sdot_(m, &work[*n + 1], &c__1, 
1525                                                 &a[p * a_dim1 + 1], &c__1) * 
1526                                                 work[p] / aapp;
1527                                     }
1528                                 }
1529
1530 /* Computing MAX */
1531                                 r__1 = mxaapq, r__2 = abs(aapq);
1532                                 mxaapq = f2cmax(r__1,r__2);
1533
1534 /*        TO rotate or NOT to rotate, THAT is the question ... */
1535
1536                                 if (abs(aapq) > tol) {
1537
1538 /* [RTD]      ROTATED = ROTATED + ONE */
1539
1540                                     if (ir1 == 0) {
1541                                         notrot = 0;
1542                                         pskipped = 0;
1543                                         ++iswrot;
1544                                     }
1545
1546                                     if (rotok) {
1547
1548                                         aqoap = aaqq / aapp;
1549                                         apoaq = aapp / aaqq;
1550                                         theta = (r__1 = aqoap - apoaq, abs(
1551                                                 r__1)) * -.5f / aapq;
1552
1553                                         if (abs(theta) > bigtheta) {
1554
1555                                             t = .5f / theta;
1556                                             fastr[2] = t * work[p] / work[q];
1557                                             fastr[3] = -t * work[q] / work[p];
1558                                             srotm_(m, &a[p * a_dim1 + 1], &
1559                                                     c__1, &a[q * a_dim1 + 1], 
1560                                                     &c__1, fastr);
1561                                             if (rsvec) {
1562                           srotm_(&mvl, &v[p * v_dim1 + 1], &c__1, &v[q * 
1563                                   v_dim1 + 1], &c__1, fastr);
1564                                             }
1565 /* Computing MAX */
1566                                             r__1 = 0.f, r__2 = t * apoaq * 
1567                                                     aapq + 1.f;
1568                                             sva[q] = aaqq * sqrt((f2cmax(r__1,
1569                                                     r__2)));
1570 /* Computing MAX */
1571                                             r__1 = 0.f, r__2 = 1.f - t * 
1572                                                     aqoap * aapq;
1573                                             aapp *= sqrt((f2cmax(r__1,r__2)));
1574 /* Computing MAX */
1575                                             r__1 = mxsinj, r__2 = abs(t);
1576                                             mxsinj = f2cmax(r__1,r__2);
1577
1578                                         } else {
1579
1580
1581                                             thsign = -r_sign(&c_b18, &aapq);
1582                                             t = 1.f / (theta + thsign * sqrt(
1583                                                     theta * theta + 1.f));
1584                                             cs = sqrt(1.f / (t * t + 1.f));
1585                                             sn = t * cs;
1586
1587 /* Computing MAX */
1588                                             r__1 = mxsinj, r__2 = abs(sn);
1589                                             mxsinj = f2cmax(r__1,r__2);
1590 /* Computing MAX */
1591                                             r__1 = 0.f, r__2 = t * apoaq * 
1592                                                     aapq + 1.f;
1593                                             sva[q] = aaqq * sqrt((f2cmax(r__1,
1594                                                     r__2)));
1595 /* Computing MAX */
1596                                             r__1 = 0.f, r__2 = 1.f - t * 
1597                                                     aqoap * aapq;
1598                                             aapp *= sqrt((f2cmax(r__1,r__2)));
1599
1600                                             apoaq = work[p] / work[q];
1601                                             aqoap = work[q] / work[p];
1602                                             if (work[p] >= 1.f) {
1603                           if (work[q] >= 1.f) {
1604                               fastr[2] = t * apoaq;
1605                               fastr[3] = -t * aqoap;
1606                               work[p] *= cs;
1607                               work[q] *= cs;
1608                               srotm_(m, &a[p * a_dim1 + 1], &c__1, &a[q * 
1609                                       a_dim1 + 1], &c__1, fastr);
1610                               if (rsvec) {
1611                                   srotm_(&mvl, &v[p * v_dim1 + 1], &c__1, &v[
1612                                           q * v_dim1 + 1], &c__1, fastr);
1613                               }
1614                           } else {
1615                               r__1 = -t * aqoap;
1616                               saxpy_(m, &r__1, &a[q * a_dim1 + 1], &c__1, &a[
1617                                       p * a_dim1 + 1], &c__1);
1618                               r__1 = cs * sn * apoaq;
1619                               saxpy_(m, &r__1, &a[p * a_dim1 + 1], &c__1, &a[
1620                                       q * a_dim1 + 1], &c__1);
1621                               work[p] *= cs;
1622                               work[q] /= cs;
1623                               if (rsvec) {
1624                                   r__1 = -t * aqoap;
1625                                   saxpy_(&mvl, &r__1, &v[q * v_dim1 + 1], &
1626                                           c__1, &v[p * v_dim1 + 1], &c__1);
1627                                   r__1 = cs * sn * apoaq;
1628                                   saxpy_(&mvl, &r__1, &v[p * v_dim1 + 1], &
1629                                           c__1, &v[q * v_dim1 + 1], &c__1);
1630                               }
1631                           }
1632                                             } else {
1633                           if (work[q] >= 1.f) {
1634                               r__1 = t * apoaq;
1635                               saxpy_(m, &r__1, &a[p * a_dim1 + 1], &c__1, &a[
1636                                       q * a_dim1 + 1], &c__1);
1637                               r__1 = -cs * sn * aqoap;
1638                               saxpy_(m, &r__1, &a[q * a_dim1 + 1], &c__1, &a[
1639                                       p * a_dim1 + 1], &c__1);
1640                               work[p] /= cs;
1641                               work[q] *= cs;
1642                               if (rsvec) {
1643                                   r__1 = t * apoaq;
1644                                   saxpy_(&mvl, &r__1, &v[p * v_dim1 + 1], &
1645                                           c__1, &v[q * v_dim1 + 1], &c__1);
1646                                   r__1 = -cs * sn * aqoap;
1647                                   saxpy_(&mvl, &r__1, &v[q * v_dim1 + 1], &
1648                                           c__1, &v[p * v_dim1 + 1], &c__1);
1649                               }
1650                           } else {
1651                               if (work[p] >= work[q]) {
1652                                   r__1 = -t * aqoap;
1653                                   saxpy_(m, &r__1, &a[q * a_dim1 + 1], &c__1, 
1654                                           &a[p * a_dim1 + 1], &c__1);
1655                                   r__1 = cs * sn * apoaq;
1656                                   saxpy_(m, &r__1, &a[p * a_dim1 + 1], &c__1, 
1657                                           &a[q * a_dim1 + 1], &c__1);
1658                                   work[p] *= cs;
1659                                   work[q] /= cs;
1660                                   if (rsvec) {
1661                                       r__1 = -t * aqoap;
1662                                       saxpy_(&mvl, &r__1, &v[q * v_dim1 + 1], 
1663                                               &c__1, &v[p * v_dim1 + 1], &
1664                                               c__1);
1665                                       r__1 = cs * sn * apoaq;
1666                                       saxpy_(&mvl, &r__1, &v[p * v_dim1 + 1], 
1667                                               &c__1, &v[q * v_dim1 + 1], &
1668                                               c__1);
1669                                   }
1670                               } else {
1671                                   r__1 = t * apoaq;
1672                                   saxpy_(m, &r__1, &a[p * a_dim1 + 1], &c__1, 
1673                                           &a[q * a_dim1 + 1], &c__1);
1674                                   r__1 = -cs * sn * aqoap;
1675                                   saxpy_(m, &r__1, &a[q * a_dim1 + 1], &c__1, 
1676                                           &a[p * a_dim1 + 1], &c__1);
1677                                   work[p] /= cs;
1678                                   work[q] *= cs;
1679                                   if (rsvec) {
1680                                       r__1 = t * apoaq;
1681                                       saxpy_(&mvl, &r__1, &v[p * v_dim1 + 1], 
1682                                               &c__1, &v[q * v_dim1 + 1], &
1683                                               c__1);
1684                                       r__1 = -cs * sn * aqoap;
1685                                       saxpy_(&mvl, &r__1, &v[q * v_dim1 + 1], 
1686                                               &c__1, &v[p * v_dim1 + 1], &
1687                                               c__1);
1688                                   }
1689                               }
1690                           }
1691                                             }
1692                                         }
1693
1694                                     } else {
1695                                         scopy_(m, &a[p * a_dim1 + 1], &c__1, &
1696                                                 work[*n + 1], &c__1);
1697                                         slascl_("G", &c__0, &c__0, &aapp, &
1698                                                 c_b18, m, &c__1, &work[*n + 1]
1699                                                 , lda, &ierr);
1700                                         slascl_("G", &c__0, &c__0, &aaqq, &
1701                                                 c_b18, m, &c__1, &a[q * 
1702                                                 a_dim1 + 1], lda, &ierr);
1703                                         temp1 = -aapq * work[p] / work[q];
1704                                         saxpy_(m, &temp1, &work[*n + 1], &
1705                                                 c__1, &a[q * a_dim1 + 1], &
1706                                                 c__1);
1707                                         slascl_("G", &c__0, &c__0, &c_b18, &
1708                                                 aaqq, m, &c__1, &a[q * a_dim1 
1709                                                 + 1], lda, &ierr);
1710 /* Computing MAX */
1711                                         r__1 = 0.f, r__2 = 1.f - aapq * aapq;
1712                                         sva[q] = aaqq * sqrt((f2cmax(r__1,r__2)))
1713                                                 ;
1714                                         mxsinj = f2cmax(mxsinj,sfmin);
1715                                     }
1716 /*           END IF ROTOK THEN ... ELSE */
1717
1718 /*           In the case of cancellation in updating SVA(q), SVA(p) */
1719 /*           recompute SVA(q), SVA(p). */
1720
1721 /* Computing 2nd power */
1722                                     r__1 = sva[q] / aaqq;
1723                                     if (r__1 * r__1 <= rooteps) {
1724                                         if (aaqq < rootbig && aaqq > 
1725                                                 rootsfmin) {
1726                                             sva[q] = snrm2_(m, &a[q * a_dim1 
1727                                                     + 1], &c__1) * work[q];
1728                                         } else {
1729                                             t = 0.f;
1730                                             aaqq = 1.f;
1731                                             slassq_(m, &a[q * a_dim1 + 1], &
1732                                                     c__1, &t, &aaqq);
1733                                             sva[q] = t * sqrt(aaqq) * work[q];
1734                                         }
1735                                     }
1736                                     if (aapp / aapp0 <= rooteps) {
1737                                         if (aapp < rootbig && aapp > 
1738                                                 rootsfmin) {
1739                                             aapp = snrm2_(m, &a[p * a_dim1 + 
1740                                                     1], &c__1) * work[p];
1741                                         } else {
1742                                             t = 0.f;
1743                                             aapp = 1.f;
1744                                             slassq_(m, &a[p * a_dim1 + 1], &
1745                                                     c__1, &t, &aapp);
1746                                             aapp = t * sqrt(aapp) * work[p];
1747                                         }
1748                                         sva[p] = aapp;
1749                                     }
1750
1751                                 } else {
1752 /*        A(:,p) and A(:,q) already numerically orthogonal */
1753                                     if (ir1 == 0) {
1754                                         ++notrot;
1755                                     }
1756 /* [RTD]      SKIPPED  = SKIPPED  + 1 */
1757                                     ++pskipped;
1758                                 }
1759                             } else {
1760 /*        A(:,q) is zero column */
1761                                 if (ir1 == 0) {
1762                                     ++notrot;
1763                                 }
1764                                 ++pskipped;
1765                             }
1766
1767                             if (i__ <= swband && pskipped > rowskip) {
1768                                 if (ir1 == 0) {
1769                                     aapp = -aapp;
1770                                 }
1771                                 notrot = 0;
1772                                 goto L2103;
1773                             }
1774
1775 /* L2002: */
1776                         }
1777 /*     END q-LOOP */
1778
1779 L2103:
1780 /*     bailed out of q-loop */
1781
1782                         sva[p] = aapp;
1783
1784                     } else {
1785                         sva[p] = aapp;
1786                         if (ir1 == 0 && aapp == 0.f) {
1787 /* Computing MIN */
1788                             i__4 = igl + kbl - 1;
1789                             notrot = notrot + f2cmin(i__4,*n) - p;
1790                         }
1791                     }
1792
1793 /* L2001: */
1794                 }
1795 /*     end of the p-loop */
1796 /*     end of doing the block ( ibr, ibr ) */
1797 /* L1002: */
1798             }
1799 /*     end of ir1-loop */
1800
1801 /* ... go to the off diagonal blocks */
1802
1803             igl = (ibr - 1) * kbl + 1;
1804
1805             i__2 = nbl;
1806             for (jbc = ibr + 1; jbc <= i__2; ++jbc) {
1807
1808                 jgl = (jbc - 1) * kbl + 1;
1809
1810 /*        doing the block at ( ibr, jbc ) */
1811
1812                 ijblsk = 0;
1813 /* Computing MIN */
1814                 i__4 = igl + kbl - 1;
1815                 i__3 = f2cmin(i__4,*n);
1816                 for (p = igl; p <= i__3; ++p) {
1817
1818                     aapp = sva[p];
1819                     if (aapp > 0.f) {
1820
1821                         pskipped = 0;
1822
1823 /* Computing MIN */
1824                         i__5 = jgl + kbl - 1;
1825                         i__4 = f2cmin(i__5,*n);
1826                         for (q = jgl; q <= i__4; ++q) {
1827
1828                             aaqq = sva[q];
1829                             if (aaqq > 0.f) {
1830                                 aapp0 = aapp;
1831
1832
1833 /*        Safe Gram matrix computation */
1834
1835                                 if (aaqq >= 1.f) {
1836                                     if (aapp >= aaqq) {
1837                                         rotok = small * aapp <= aaqq;
1838                                     } else {
1839                                         rotok = small * aaqq <= aapp;
1840                                     }
1841                                     if (aapp < big / aaqq) {
1842                                         aapq = sdot_(m, &a[p * a_dim1 + 1], &
1843                                                 c__1, &a[q * a_dim1 + 1], &
1844                                                 c__1) * work[p] * work[q] / 
1845                                                 aaqq / aapp;
1846                                     } else {
1847                                         scopy_(m, &a[p * a_dim1 + 1], &c__1, &
1848                                                 work[*n + 1], &c__1);
1849                                         slascl_("G", &c__0, &c__0, &aapp, &
1850                                                 work[p], m, &c__1, &work[*n + 
1851                                                 1], lda, &ierr);
1852                                         aapq = sdot_(m, &work[*n + 1], &c__1, 
1853                                                 &a[q * a_dim1 + 1], &c__1) * 
1854                                                 work[q] / aaqq;
1855                                     }
1856                                 } else {
1857                                     if (aapp >= aaqq) {
1858                                         rotok = aapp <= aaqq / small;
1859                                     } else {
1860                                         rotok = aaqq <= aapp / small;
1861                                     }
1862                                     if (aapp > small / aaqq) {
1863                                         aapq = sdot_(m, &a[p * a_dim1 + 1], &
1864                                                 c__1, &a[q * a_dim1 + 1], &
1865                                                 c__1) * work[p] * work[q] / 
1866                                                 aaqq / aapp;
1867                                     } else {
1868                                         scopy_(m, &a[q * a_dim1 + 1], &c__1, &
1869                                                 work[*n + 1], &c__1);
1870                                         slascl_("G", &c__0, &c__0, &aaqq, &
1871                                                 work[q], m, &c__1, &work[*n + 
1872                                                 1], lda, &ierr);
1873                                         aapq = sdot_(m, &work[*n + 1], &c__1, 
1874                                                 &a[p * a_dim1 + 1], &c__1) * 
1875                                                 work[p] / aapp;
1876                                     }
1877                                 }
1878
1879 /* Computing MAX */
1880                                 r__1 = mxaapq, r__2 = abs(aapq);
1881                                 mxaapq = f2cmax(r__1,r__2);
1882
1883 /*        TO rotate or NOT to rotate, THAT is the question ... */
1884
1885                                 if (abs(aapq) > tol) {
1886                                     notrot = 0;
1887 /* [RTD]      ROTATED  = ROTATED + 1 */
1888                                     pskipped = 0;
1889                                     ++iswrot;
1890
1891                                     if (rotok) {
1892
1893                                         aqoap = aaqq / aapp;
1894                                         apoaq = aapp / aaqq;
1895                                         theta = (r__1 = aqoap - apoaq, abs(
1896                                                 r__1)) * -.5f / aapq;
1897                                         if (aaqq > aapp0) {
1898                                             theta = -theta;
1899                                         }
1900
1901                                         if (abs(theta) > bigtheta) {
1902                                             t = .5f / theta;
1903                                             fastr[2] = t * work[p] / work[q];
1904                                             fastr[3] = -t * work[q] / work[p];
1905                                             srotm_(m, &a[p * a_dim1 + 1], &
1906                                                     c__1, &a[q * a_dim1 + 1], 
1907                                                     &c__1, fastr);
1908                                             if (rsvec) {
1909                           srotm_(&mvl, &v[p * v_dim1 + 1], &c__1, &v[q * 
1910                                   v_dim1 + 1], &c__1, fastr);
1911                                             }
1912 /* Computing MAX */
1913                                             r__1 = 0.f, r__2 = t * apoaq * 
1914                                                     aapq + 1.f;
1915                                             sva[q] = aaqq * sqrt((f2cmax(r__1,
1916                                                     r__2)));
1917 /* Computing MAX */
1918                                             r__1 = 0.f, r__2 = 1.f - t * 
1919                                                     aqoap * aapq;
1920                                             aapp *= sqrt((f2cmax(r__1,r__2)));
1921 /* Computing MAX */
1922                                             r__1 = mxsinj, r__2 = abs(t);
1923                                             mxsinj = f2cmax(r__1,r__2);
1924                                         } else {
1925
1926
1927                                             thsign = -r_sign(&c_b18, &aapq);
1928                                             if (aaqq > aapp0) {
1929                           thsign = -thsign;
1930                                             }
1931                                             t = 1.f / (theta + thsign * sqrt(
1932                                                     theta * theta + 1.f));
1933                                             cs = sqrt(1.f / (t * t + 1.f));
1934                                             sn = t * cs;
1935 /* Computing MAX */
1936                                             r__1 = mxsinj, r__2 = abs(sn);
1937                                             mxsinj = f2cmax(r__1,r__2);
1938 /* Computing MAX */
1939                                             r__1 = 0.f, r__2 = t * apoaq * 
1940                                                     aapq + 1.f;
1941                                             sva[q] = aaqq * sqrt((f2cmax(r__1,
1942                                                     r__2)));
1943 /* Computing MAX */
1944                                             r__1 = 0.f, r__2 = 1.f - t * 
1945                                                     aqoap * aapq;
1946                                             aapp *= sqrt((f2cmax(r__1,r__2)));
1947
1948                                             apoaq = work[p] / work[q];
1949                                             aqoap = work[q] / work[p];
1950                                             if (work[p] >= 1.f) {
1951
1952                           if (work[q] >= 1.f) {
1953                               fastr[2] = t * apoaq;
1954                               fastr[3] = -t * aqoap;
1955                               work[p] *= cs;
1956                               work[q] *= cs;
1957                               srotm_(m, &a[p * a_dim1 + 1], &c__1, &a[q * 
1958                                       a_dim1 + 1], &c__1, fastr);
1959                               if (rsvec) {
1960                                   srotm_(&mvl, &v[p * v_dim1 + 1], &c__1, &v[
1961                                           q * v_dim1 + 1], &c__1, fastr);
1962                               }
1963                           } else {
1964                               r__1 = -t * aqoap;
1965                               saxpy_(m, &r__1, &a[q * a_dim1 + 1], &c__1, &a[
1966                                       p * a_dim1 + 1], &c__1);
1967                               r__1 = cs * sn * apoaq;
1968                               saxpy_(m, &r__1, &a[p * a_dim1 + 1], &c__1, &a[
1969                                       q * a_dim1 + 1], &c__1);
1970                               if (rsvec) {
1971                                   r__1 = -t * aqoap;
1972                                   saxpy_(&mvl, &r__1, &v[q * v_dim1 + 1], &
1973                                           c__1, &v[p * v_dim1 + 1], &c__1);
1974                                   r__1 = cs * sn * apoaq;
1975                                   saxpy_(&mvl, &r__1, &v[p * v_dim1 + 1], &
1976                                           c__1, &v[q * v_dim1 + 1], &c__1);
1977                               }
1978                               work[p] *= cs;
1979                               work[q] /= cs;
1980                           }
1981                                             } else {
1982                           if (work[q] >= 1.f) {
1983                               r__1 = t * apoaq;
1984                               saxpy_(m, &r__1, &a[p * a_dim1 + 1], &c__1, &a[
1985                                       q * a_dim1 + 1], &c__1);
1986                               r__1 = -cs * sn * aqoap;
1987                               saxpy_(m, &r__1, &a[q * a_dim1 + 1], &c__1, &a[
1988                                       p * a_dim1 + 1], &c__1);
1989                               if (rsvec) {
1990                                   r__1 = t * apoaq;
1991                                   saxpy_(&mvl, &r__1, &v[p * v_dim1 + 1], &
1992                                           c__1, &v[q * v_dim1 + 1], &c__1);
1993                                   r__1 = -cs * sn * aqoap;
1994                                   saxpy_(&mvl, &r__1, &v[q * v_dim1 + 1], &
1995                                           c__1, &v[p * v_dim1 + 1], &c__1);
1996                               }
1997                               work[p] /= cs;
1998                               work[q] *= cs;
1999                           } else {
2000                               if (work[p] >= work[q]) {
2001                                   r__1 = -t * aqoap;
2002                                   saxpy_(m, &r__1, &a[q * a_dim1 + 1], &c__1, 
2003                                           &a[p * a_dim1 + 1], &c__1);
2004                                   r__1 = cs * sn * apoaq;
2005                                   saxpy_(m, &r__1, &a[p * a_dim1 + 1], &c__1, 
2006                                           &a[q * a_dim1 + 1], &c__1);
2007                                   work[p] *= cs;
2008                                   work[q] /= cs;
2009                                   if (rsvec) {
2010                                       r__1 = -t * aqoap;
2011                                       saxpy_(&mvl, &r__1, &v[q * v_dim1 + 1], 
2012                                               &c__1, &v[p * v_dim1 + 1], &
2013                                               c__1);
2014                                       r__1 = cs * sn * apoaq;
2015                                       saxpy_(&mvl, &r__1, &v[p * v_dim1 + 1], 
2016                                               &c__1, &v[q * v_dim1 + 1], &
2017                                               c__1);
2018                                   }
2019                               } else {
2020                                   r__1 = t * apoaq;
2021                                   saxpy_(m, &r__1, &a[p * a_dim1 + 1], &c__1, 
2022                                           &a[q * a_dim1 + 1], &c__1);
2023                                   r__1 = -cs * sn * aqoap;
2024                                   saxpy_(m, &r__1, &a[q * a_dim1 + 1], &c__1, 
2025                                           &a[p * a_dim1 + 1], &c__1);
2026                                   work[p] /= cs;
2027                                   work[q] *= cs;
2028                                   if (rsvec) {
2029                                       r__1 = t * apoaq;
2030                                       saxpy_(&mvl, &r__1, &v[p * v_dim1 + 1], 
2031                                               &c__1, &v[q * v_dim1 + 1], &
2032                                               c__1);
2033                                       r__1 = -cs * sn * aqoap;
2034                                       saxpy_(&mvl, &r__1, &v[q * v_dim1 + 1], 
2035                                               &c__1, &v[p * v_dim1 + 1], &
2036                                               c__1);
2037                                   }
2038                               }
2039                           }
2040                                             }
2041                                         }
2042
2043                                     } else {
2044                                         if (aapp > aaqq) {
2045                                             scopy_(m, &a[p * a_dim1 + 1], &
2046                                                     c__1, &work[*n + 1], &
2047                                                     c__1);
2048                                             slascl_("G", &c__0, &c__0, &aapp, 
2049                                                     &c_b18, m, &c__1, &work[*
2050                                                     n + 1], lda, &ierr);
2051                                             slascl_("G", &c__0, &c__0, &aaqq, 
2052                                                     &c_b18, m, &c__1, &a[q * 
2053                                                     a_dim1 + 1], lda, &ierr);
2054                                             temp1 = -aapq * work[p] / work[q];
2055                                             saxpy_(m, &temp1, &work[*n + 1], &
2056                                                     c__1, &a[q * a_dim1 + 1], 
2057                                                     &c__1);
2058                                             slascl_("G", &c__0, &c__0, &c_b18,
2059                                                      &aaqq, m, &c__1, &a[q * 
2060                                                     a_dim1 + 1], lda, &ierr);
2061 /* Computing MAX */
2062                                             r__1 = 0.f, r__2 = 1.f - aapq * 
2063                                                     aapq;
2064                                             sva[q] = aaqq * sqrt((f2cmax(r__1,
2065                                                     r__2)));
2066                                             mxsinj = f2cmax(mxsinj,sfmin);
2067                                         } else {
2068                                             scopy_(m, &a[q * a_dim1 + 1], &
2069                                                     c__1, &work[*n + 1], &
2070                                                     c__1);
2071                                             slascl_("G", &c__0, &c__0, &aaqq, 
2072                                                     &c_b18, m, &c__1, &work[*
2073                                                     n + 1], lda, &ierr);
2074                                             slascl_("G", &c__0, &c__0, &aapp, 
2075                                                     &c_b18, m, &c__1, &a[p * 
2076                                                     a_dim1 + 1], lda, &ierr);
2077                                             temp1 = -aapq * work[q] / work[p];
2078                                             saxpy_(m, &temp1, &work[*n + 1], &
2079                                                     c__1, &a[p * a_dim1 + 1], 
2080                                                     &c__1);
2081                                             slascl_("G", &c__0, &c__0, &c_b18,
2082                                                      &aapp, m, &c__1, &a[p * 
2083                                                     a_dim1 + 1], lda, &ierr);
2084 /* Computing MAX */
2085                                             r__1 = 0.f, r__2 = 1.f - aapq * 
2086                                                     aapq;
2087                                             sva[p] = aapp * sqrt((f2cmax(r__1,
2088                                                     r__2)));
2089                                             mxsinj = f2cmax(mxsinj,sfmin);
2090                                         }
2091                                     }
2092 /*           END IF ROTOK THEN ... ELSE */
2093
2094 /*           In the case of cancellation in updating SVA(q) */
2095 /* Computing 2nd power */
2096                                     r__1 = sva[q] / aaqq;
2097                                     if (r__1 * r__1 <= rooteps) {
2098                                         if (aaqq < rootbig && aaqq > 
2099                                                 rootsfmin) {
2100                                             sva[q] = snrm2_(m, &a[q * a_dim1 
2101                                                     + 1], &c__1) * work[q];
2102                                         } else {
2103                                             t = 0.f;
2104                                             aaqq = 1.f;
2105                                             slassq_(m, &a[q * a_dim1 + 1], &
2106                                                     c__1, &t, &aaqq);
2107                                             sva[q] = t * sqrt(aaqq) * work[q];
2108                                         }
2109                                     }
2110 /* Computing 2nd power */
2111                                     r__1 = aapp / aapp0;
2112                                     if (r__1 * r__1 <= rooteps) {
2113                                         if (aapp < rootbig && aapp > 
2114                                                 rootsfmin) {
2115                                             aapp = snrm2_(m, &a[p * a_dim1 + 
2116                                                     1], &c__1) * work[p];
2117                                         } else {
2118                                             t = 0.f;
2119                                             aapp = 1.f;
2120                                             slassq_(m, &a[p * a_dim1 + 1], &
2121                                                     c__1, &t, &aapp);
2122                                             aapp = t * sqrt(aapp) * work[p];
2123                                         }
2124                                         sva[p] = aapp;
2125                                     }
2126 /*              end of OK rotation */
2127                                 } else {
2128                                     ++notrot;
2129 /* [RTD]      SKIPPED  = SKIPPED  + 1 */
2130                                     ++pskipped;
2131                                     ++ijblsk;
2132                                 }
2133                             } else {
2134                                 ++notrot;
2135                                 ++pskipped;
2136                                 ++ijblsk;
2137                             }
2138
2139                             if (i__ <= swband && ijblsk >= blskip) {
2140                                 sva[p] = aapp;
2141                                 notrot = 0;
2142                                 goto L2011;
2143                             }
2144                             if (i__ <= swband && pskipped > rowskip) {
2145                                 aapp = -aapp;
2146                                 notrot = 0;
2147                                 goto L2203;
2148                             }
2149
2150 /* L2200: */
2151                         }
2152 /*        end of the q-loop */
2153 L2203:
2154
2155                         sva[p] = aapp;
2156
2157                     } else {
2158
2159                         if (aapp == 0.f) {
2160 /* Computing MIN */
2161                             i__4 = jgl + kbl - 1;
2162                             notrot = notrot + f2cmin(i__4,*n) - jgl + 1;
2163                         }
2164                         if (aapp < 0.f) {
2165                             notrot = 0;
2166                         }
2167
2168                     }
2169
2170 /* L2100: */
2171                 }
2172 /*     end of the p-loop */
2173 /* L2010: */
2174             }
2175 /*     end of the jbc-loop */
2176 L2011:
2177 /* 2011 bailed out of the jbc-loop */
2178 /* Computing MIN */
2179             i__3 = igl + kbl - 1;
2180             i__2 = f2cmin(i__3,*n);
2181             for (p = igl; p <= i__2; ++p) {
2182                 sva[p] = (r__1 = sva[p], abs(r__1));
2183 /* L2012: */
2184             }
2185 /* ** */
2186 /* L2000: */
2187         }
2188 /* 2000 :: end of the ibr-loop */
2189
2190         if (sva[*n] < rootbig && sva[*n] > rootsfmin) {
2191             sva[*n] = snrm2_(m, &a[*n * a_dim1 + 1], &c__1) * work[*n];
2192         } else {
2193             t = 0.f;
2194             aapp = 1.f;
2195             slassq_(m, &a[*n * a_dim1 + 1], &c__1, &t, &aapp);
2196             sva[*n] = t * sqrt(aapp) * work[*n];
2197         }
2198
2199 /*     Additional steering devices */
2200
2201         if (i__ < swband && (mxaapq <= roottol || iswrot <= *n)) {
2202             swband = i__;
2203         }
2204
2205         if (i__ > swband + 1 && mxaapq < sqrt((real) (*n)) * tol && (real) (*
2206                 n) * mxaapq * mxsinj < tol) {
2207             goto L1994;
2208         }
2209
2210         if (notrot >= emptsw) {
2211             goto L1994;
2212         }
2213
2214 /* L1993: */
2215     }
2216 /*     end i=1:NSWEEP loop */
2217
2218 /* #:( Reaching this point means that the procedure has not converged. */
2219     *info = 29;
2220     goto L1995;
2221
2222 L1994:
2223 /* #:) Reaching this point means numerical convergence after the i-th */
2224 /*     sweep. */
2225
2226     *info = 0;
2227 /* #:) INFO = 0 confirms successful iterations. */
2228 L1995:
2229
2230 /*     Sort the singular values and find how many are above */
2231 /*     the underflow threshold. */
2232
2233     n2 = 0;
2234     n4 = 0;
2235     i__1 = *n - 1;
2236     for (p = 1; p <= i__1; ++p) {
2237         i__2 = *n - p + 1;
2238         q = isamax_(&i__2, &sva[p], &c__1) + p - 1;
2239         if (p != q) {
2240             temp1 = sva[p];
2241             sva[p] = sva[q];
2242             sva[q] = temp1;
2243             temp1 = work[p];
2244             work[p] = work[q];
2245             work[q] = temp1;
2246             sswap_(m, &a[p * a_dim1 + 1], &c__1, &a[q * a_dim1 + 1], &c__1);
2247             if (rsvec) {
2248                 sswap_(&mvl, &v[p * v_dim1 + 1], &c__1, &v[q * v_dim1 + 1], &
2249                         c__1);
2250             }
2251         }
2252         if (sva[p] != 0.f) {
2253             ++n4;
2254             if (sva[p] * skl > sfmin) {
2255                 ++n2;
2256             }
2257         }
2258 /* L5991: */
2259     }
2260     if (sva[*n] != 0.f) {
2261         ++n4;
2262         if (sva[*n] * skl > sfmin) {
2263             ++n2;
2264         }
2265     }
2266
2267 /*     Normalize the left singular vectors. */
2268
2269     if (lsvec || uctol) {
2270         i__1 = n2;
2271         for (p = 1; p <= i__1; ++p) {
2272             r__1 = work[p] / sva[p];
2273             sscal_(m, &r__1, &a[p * a_dim1 + 1], &c__1);
2274 /* L1998: */
2275         }
2276     }
2277
2278 /*     Scale the product of Jacobi rotations (assemble the fast rotations). */
2279
2280     if (rsvec) {
2281         if (applv) {
2282             i__1 = *n;
2283             for (p = 1; p <= i__1; ++p) {
2284                 sscal_(&mvl, &work[p], &v[p * v_dim1 + 1], &c__1);
2285 /* L2398: */
2286             }
2287         } else {
2288             i__1 = *n;
2289             for (p = 1; p <= i__1; ++p) {
2290                 temp1 = 1.f / snrm2_(&mvl, &v[p * v_dim1 + 1], &c__1);
2291                 sscal_(&mvl, &temp1, &v[p * v_dim1 + 1], &c__1);
2292 /* L2399: */
2293             }
2294         }
2295     }
2296
2297 /*     Undo scaling, if necessary (and possible). */
2298     if (skl > 1.f && sva[1] < big / skl || skl < 1.f && sva[f2cmax(n2,1)] > 
2299             sfmin / skl) {
2300         i__1 = *n;
2301         for (p = 1; p <= i__1; ++p) {
2302             sva[p] = skl * sva[p];
2303 /* L2400: */
2304         }
2305         skl = 1.f;
2306     }
2307
2308     work[1] = skl;
2309 /*     The singular values of A are SKL*SVA(1:N). If SKL.NE.ONE */
2310 /*     then some of the singular values may overflow or underflow and */
2311 /*     the spectrum is given in this factored representation. */
2312
2313     work[2] = (real) n4;
2314 /*     N4 is the number of computed nonzero singular values of A. */
2315
2316     work[3] = (real) n2;
2317 /*     N2 is the number of singular values of A greater than SFMIN. */
2318 /*     If N2<N, SVA(N2:N) contains ZEROS and/or denormalized numbers */
2319 /*     that may carry some information. */
2320
2321     work[4] = (real) i__;
2322 /*     i is the index of the last sweep before declaring convergence. */
2323
2324     work[5] = mxaapq;
2325 /*     MXAAPQ is the largest absolute value of scaled pivots in the */
2326 /*     last sweep */
2327
2328     work[6] = mxsinj;
2329 /*     MXSINJ is the largest absolute value of the sines of Jacobi angles */
2330 /*     in the last sweep */
2331
2332     return 0;
2333 } /* sgesvj_ */
2334