C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / slatrs.c
1 #include <math.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <complex.h>
6 #ifdef complex
7 #undef complex
8 #endif
9 #ifdef I
10 #undef I
11 #endif
12
13 #if defined(_WIN64)
14 typedef long long BLASLONG;
15 typedef unsigned long long BLASULONG;
16 #else
17 typedef long BLASLONG;
18 typedef unsigned long BLASULONG;
19 #endif
20
21 #ifdef LAPACK_ILP64
22 typedef BLASLONG blasint;
23 #if defined(_WIN64)
24 #define blasabs(x) llabs(x)
25 #else
26 #define blasabs(x) labs(x)
27 #endif
28 #else
29 typedef int blasint;
30 #define blasabs(x) abs(x)
31 #endif
32
33 typedef blasint integer;
34
35 typedef unsigned int uinteger;
36 typedef char *address;
37 typedef short int shortint;
38 typedef float real;
39 typedef double doublereal;
40 typedef struct { real r, i; } complex;
41 typedef struct { doublereal r, i; } doublecomplex;
42 #ifdef _MSC_VER
43 static inline _Fcomplex Cf(complex *z) {_Fcomplex zz={z->r , z->i}; return zz;}
44 static inline _Dcomplex Cd(doublecomplex *z) {_Dcomplex zz={z->r , z->i};return zz;}
45 static inline _Fcomplex * _pCf(complex *z) {return (_Fcomplex*)z;}
46 static inline _Dcomplex * _pCd(doublecomplex *z) {return (_Dcomplex*)z;}
47 #else
48 static inline _Complex float Cf(complex *z) {return z->r + z->i*_Complex_I;}
49 static inline _Complex double Cd(doublecomplex *z) {return z->r + z->i*_Complex_I;}
50 static inline _Complex float * _pCf(complex *z) {return (_Complex float*)z;}
51 static inline _Complex double * _pCd(doublecomplex *z) {return (_Complex double*)z;}
52 #endif
53 #define pCf(z) (*_pCf(z))
54 #define pCd(z) (*_pCd(z))
55 typedef int logical;
56 typedef short int shortlogical;
57 typedef char logical1;
58 typedef char integer1;
59
60 #define TRUE_ (1)
61 #define FALSE_ (0)
62
63 /* Extern is for use with -E */
64 #ifndef Extern
65 #define Extern extern
66 #endif
67
68 /* I/O stuff */
69
70 typedef int flag;
71 typedef int ftnlen;
72 typedef int ftnint;
73
74 /*external read, write*/
75 typedef struct
76 {       flag cierr;
77         ftnint ciunit;
78         flag ciend;
79         char *cifmt;
80         ftnint cirec;
81 } cilist;
82
83 /*internal read, write*/
84 typedef struct
85 {       flag icierr;
86         char *iciunit;
87         flag iciend;
88         char *icifmt;
89         ftnint icirlen;
90         ftnint icirnum;
91 } icilist;
92
93 /*open*/
94 typedef struct
95 {       flag oerr;
96         ftnint ounit;
97         char *ofnm;
98         ftnlen ofnmlen;
99         char *osta;
100         char *oacc;
101         char *ofm;
102         ftnint orl;
103         char *oblnk;
104 } olist;
105
106 /*close*/
107 typedef struct
108 {       flag cerr;
109         ftnint cunit;
110         char *csta;
111 } cllist;
112
113 /*rewind, backspace, endfile*/
114 typedef struct
115 {       flag aerr;
116         ftnint aunit;
117 } alist;
118
119 /* inquire */
120 typedef struct
121 {       flag inerr;
122         ftnint inunit;
123         char *infile;
124         ftnlen infilen;
125         ftnint  *inex;  /*parameters in standard's order*/
126         ftnint  *inopen;
127         ftnint  *innum;
128         ftnint  *innamed;
129         char    *inname;
130         ftnlen  innamlen;
131         char    *inacc;
132         ftnlen  inacclen;
133         char    *inseq;
134         ftnlen  inseqlen;
135         char    *indir;
136         ftnlen  indirlen;
137         char    *infmt;
138         ftnlen  infmtlen;
139         char    *inform;
140         ftnint  informlen;
141         char    *inunf;
142         ftnlen  inunflen;
143         ftnint  *inrecl;
144         ftnint  *innrec;
145         char    *inblank;
146         ftnlen  inblanklen;
147 } inlist;
148
149 #define VOID void
150
151 union Multitype {       /* for multiple entry points */
152         integer1 g;
153         shortint h;
154         integer i;
155         /* longint j; */
156         real r;
157         doublereal d;
158         complex c;
159         doublecomplex z;
160         };
161
162 typedef union Multitype Multitype;
163
164 struct Vardesc {        /* for Namelist */
165         char *name;
166         char *addr;
167         ftnlen *dims;
168         int  type;
169         };
170 typedef struct Vardesc Vardesc;
171
172 struct Namelist {
173         char *name;
174         Vardesc **vars;
175         int nvars;
176         };
177 typedef struct Namelist Namelist;
178
179 #define abs(x) ((x) >= 0 ? (x) : -(x))
180 #define dabs(x) (fabs(x))
181 #define f2cmin(a,b) ((a) <= (b) ? (a) : (b))
182 #define f2cmax(a,b) ((a) >= (b) ? (a) : (b))
183 #define dmin(a,b) (f2cmin(a,b))
184 #define dmax(a,b) (f2cmax(a,b))
185 #define bit_test(a,b)   ((a) >> (b) & 1)
186 #define bit_clear(a,b)  ((a) & ~((uinteger)1 << (b)))
187 #define bit_set(a,b)    ((a) |  ((uinteger)1 << (b)))
188
189 #define abort_() { sig_die("Fortran abort routine called", 1); }
190 #define c_abs(z) (cabsf(Cf(z)))
191 #define c_cos(R,Z) { pCf(R)=ccos(Cf(Z)); }
192 #ifdef _MSC_VER
193 #define c_div(c, a, b) {Cf(c)._Val[0] = (Cf(a)._Val[0]/Cf(b)._Val[0]); Cf(c)._Val[1]=(Cf(a)._Val[1]/Cf(b)._Val[1]);}
194 #define z_div(c, a, b) {Cd(c)._Val[0] = (Cd(a)._Val[0]/Cd(b)._Val[0]); Cd(c)._Val[1]=(Cd(a)._Val[1]/df(b)._Val[1]);}
195 #else
196 #define c_div(c, a, b) {pCf(c) = Cf(a)/Cf(b);}
197 #define z_div(c, a, b) {pCd(c) = Cd(a)/Cd(b);}
198 #endif
199 #define c_exp(R, Z) {pCf(R) = cexpf(Cf(Z));}
200 #define c_log(R, Z) {pCf(R) = clogf(Cf(Z));}
201 #define c_sin(R, Z) {pCf(R) = csinf(Cf(Z));}
202 //#define c_sqrt(R, Z) {*(R) = csqrtf(Cf(Z));}
203 #define c_sqrt(R, Z) {pCf(R) = csqrtf(Cf(Z));}
204 #define d_abs(x) (fabs(*(x)))
205 #define d_acos(x) (acos(*(x)))
206 #define d_asin(x) (asin(*(x)))
207 #define d_atan(x) (atan(*(x)))
208 #define d_atn2(x, y) (atan2(*(x),*(y)))
209 #define d_cnjg(R, Z) { pCd(R) = conj(Cd(Z)); }
210 #define r_cnjg(R, Z) { pCf(R) = conjf(Cf(Z)); }
211 #define d_cos(x) (cos(*(x)))
212 #define d_cosh(x) (cosh(*(x)))
213 #define d_dim(__a, __b) ( *(__a) > *(__b) ? *(__a) - *(__b) : 0.0 )
214 #define d_exp(x) (exp(*(x)))
215 #define d_imag(z) (cimag(Cd(z)))
216 #define r_imag(z) (cimagf(Cf(z)))
217 #define d_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
218 #define r_int(__x) (*(__x)>0 ? floor(*(__x)) : -floor(- *(__x)))
219 #define d_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
220 #define r_lg10(x) ( 0.43429448190325182765 * log(*(x)) )
221 #define d_log(x) (log(*(x)))
222 #define d_mod(x, y) (fmod(*(x), *(y)))
223 #define u_nint(__x) ((__x)>=0 ? floor((__x) + .5) : -floor(.5 - (__x)))
224 #define d_nint(x) u_nint(*(x))
225 #define u_sign(__a,__b) ((__b) >= 0 ? ((__a) >= 0 ? (__a) : -(__a)) : -((__a) >= 0 ? (__a) : -(__a)))
226 #define d_sign(a,b) u_sign(*(a),*(b))
227 #define r_sign(a,b) u_sign(*(a),*(b))
228 #define d_sin(x) (sin(*(x)))
229 #define d_sinh(x) (sinh(*(x)))
230 #define d_sqrt(x) (sqrt(*(x)))
231 #define d_tan(x) (tan(*(x)))
232 #define d_tanh(x) (tanh(*(x)))
233 #define i_abs(x) abs(*(x))
234 #define i_dnnt(x) ((integer)u_nint(*(x)))
235 #define i_len(s, n) (n)
236 #define i_nint(x) ((integer)u_nint(*(x)))
237 #define i_sign(a,b) ((integer)u_sign((integer)*(a),(integer)*(b)))
238 #define pow_dd(ap, bp) ( pow(*(ap), *(bp)))
239 #define pow_si(B,E) spow_ui(*(B),*(E))
240 #define pow_ri(B,E) spow_ui(*(B),*(E))
241 #define pow_di(B,E) dpow_ui(*(B),*(E))
242 #define pow_zi(p, a, b) {pCd(p) = zpow_ui(Cd(a), *(b));}
243 #define pow_ci(p, a, b) {pCf(p) = cpow_ui(Cf(a), *(b));}
244 #define pow_zz(R,A,B) {pCd(R) = cpow(Cd(A),*(B));}
245 #define s_cat(lpp, rpp, rnp, np, llp) {         ftnlen i, nc, ll; char *f__rp, *lp;     ll = (llp); lp = (lpp);         for(i=0; i < (int)*(np); ++i) {                 nc = ll;                if((rnp)[i] < nc) nc = (rnp)[i];                ll -= nc;               f__rp = (rpp)[i];               while(--nc >= 0) *lp++ = *(f__rp)++;         }  while(--ll >= 0) *lp++ = ' '; }
246 #define s_cmp(a,b,c,d) ((integer)strncmp((a),(b),f2cmin((c),(d))))
247 #define s_copy(A,B,C,D) { int __i,__m; for (__i=0, __m=f2cmin((C),(D)); __i<__m && (B)[__i] != 0; ++__i) (A)[__i] = (B)[__i]; }
248 #define sig_die(s, kill) { exit(1); }
249 #define s_stop(s, n) {exit(0);}
250 static char junk[] = "\n@(#)LIBF77 VERSION 19990503\n";
251 #define z_abs(z) (cabs(Cd(z)))
252 #define z_exp(R, Z) {pCd(R) = cexp(Cd(Z));}
253 #define z_sqrt(R, Z) {pCd(R) = csqrt(Cd(Z));}
254 #define myexit_() break;
255 #define mycycle() continue;
256 #define myceiling(w) {ceil(w)}
257 #define myhuge(w) {HUGE_VAL}
258 //#define mymaxloc_(w,s,e,n) {if (sizeof(*(w)) == sizeof(double)) dmaxloc_((w),*(s),*(e),n); else dmaxloc_((w),*(s),*(e),n);}
259 #define mymaxloc(w,s,e,n) {dmaxloc_(w,*(s),*(e),n)}
260
261 /* procedure parameter types for -A and -C++ */
262
263 #define F2C_proc_par_types 1
264 #ifdef __cplusplus
265 typedef logical (*L_fp)(...);
266 #else
267 typedef logical (*L_fp)();
268 #endif
269
270 static float spow_ui(float x, integer n) {
271         float pow=1.0; unsigned long int u;
272         if(n != 0) {
273                 if(n < 0) n = -n, x = 1/x;
274                 for(u = n; ; ) {
275                         if(u & 01) pow *= x;
276                         if(u >>= 1) x *= x;
277                         else break;
278                 }
279         }
280         return pow;
281 }
282 static double dpow_ui(double x, integer n) {
283         double pow=1.0; unsigned long int u;
284         if(n != 0) {
285                 if(n < 0) n = -n, x = 1/x;
286                 for(u = n; ; ) {
287                         if(u & 01) pow *= x;
288                         if(u >>= 1) x *= x;
289                         else break;
290                 }
291         }
292         return pow;
293 }
294 #ifdef _MSC_VER
295 static _Fcomplex cpow_ui(complex x, integer n) {
296         complex pow={1.0,0.0}; unsigned long int u;
297                 if(n != 0) {
298                 if(n < 0) n = -n, x.r = 1/x.r, x.i=1/x.i;
299                 for(u = n; ; ) {
300                         if(u & 01) pow.r *= x.r, pow.i *= x.i;
301                         if(u >>= 1) x.r *= x.r, x.i *= x.i;
302                         else break;
303                 }
304         }
305         _Fcomplex p={pow.r, pow.i};
306         return p;
307 }
308 #else
309 static _Complex float cpow_ui(_Complex float x, integer n) {
310         _Complex float pow=1.0; unsigned long int u;
311         if(n != 0) {
312                 if(n < 0) n = -n, x = 1/x;
313                 for(u = n; ; ) {
314                         if(u & 01) pow *= x;
315                         if(u >>= 1) x *= x;
316                         else break;
317                 }
318         }
319         return pow;
320 }
321 #endif
322 #ifdef _MSC_VER
323 static _Dcomplex zpow_ui(_Dcomplex x, integer n) {
324         _Dcomplex pow={1.0,0.0}; unsigned long int u;
325         if(n != 0) {
326                 if(n < 0) n = -n, x._Val[0] = 1/x._Val[0], x._Val[1] =1/x._Val[1];
327                 for(u = n; ; ) {
328                         if(u & 01) pow._Val[0] *= x._Val[0], pow._Val[1] *= x._Val[1];
329                         if(u >>= 1) x._Val[0] *= x._Val[0], x._Val[1] *= x._Val[1];
330                         else break;
331                 }
332         }
333         _Dcomplex p = {pow._Val[0], pow._Val[1]};
334         return p;
335 }
336 #else
337 static _Complex double zpow_ui(_Complex double x, integer n) {
338         _Complex double pow=1.0; unsigned long int u;
339         if(n != 0) {
340                 if(n < 0) n = -n, x = 1/x;
341                 for(u = n; ; ) {
342                         if(u & 01) pow *= x;
343                         if(u >>= 1) x *= x;
344                         else break;
345                 }
346         }
347         return pow;
348 }
349 #endif
350 static integer pow_ii(integer x, integer n) {
351         integer pow; unsigned long int u;
352         if (n <= 0) {
353                 if (n == 0 || x == 1) pow = 1;
354                 else if (x != -1) pow = x == 0 ? 1/x : 0;
355                 else n = -n;
356         }
357         if ((n > 0) || !(n == 0 || x == 1 || x != -1)) {
358                 u = n;
359                 for(pow = 1; ; ) {
360                         if(u & 01) pow *= x;
361                         if(u >>= 1) x *= x;
362                         else break;
363                 }
364         }
365         return pow;
366 }
367 static integer dmaxloc_(double *w, integer s, integer e, integer *n)
368 {
369         double m; integer i, mi;
370         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
371                 if (w[i-1]>m) mi=i ,m=w[i-1];
372         return mi-s+1;
373 }
374 static integer smaxloc_(float *w, integer s, integer e, integer *n)
375 {
376         float m; integer i, mi;
377         for(m=w[s-1], mi=s, i=s+1; i<=e; i++)
378                 if (w[i-1]>m) mi=i ,m=w[i-1];
379         return mi-s+1;
380 }
381 static inline void cdotc_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
382         integer n = *n_, incx = *incx_, incy = *incy_, i;
383 #ifdef _MSC_VER
384         _Fcomplex zdotc = {0.0, 0.0};
385         if (incx == 1 && incy == 1) {
386                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
387                         zdotc._Val[0] += conjf(Cf(&x[i]))._Val[0] * Cf(&y[i])._Val[0];
388                         zdotc._Val[1] += conjf(Cf(&x[i]))._Val[1] * Cf(&y[i])._Val[1];
389                 }
390         } else {
391                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
392                         zdotc._Val[0] += conjf(Cf(&x[i*incx]))._Val[0] * Cf(&y[i*incy])._Val[0];
393                         zdotc._Val[1] += conjf(Cf(&x[i*incx]))._Val[1] * Cf(&y[i*incy])._Val[1];
394                 }
395         }
396         pCf(z) = zdotc;
397 }
398 #else
399         _Complex float zdotc = 0.0;
400         if (incx == 1 && incy == 1) {
401                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
402                         zdotc += conjf(Cf(&x[i])) * Cf(&y[i]);
403                 }
404         } else {
405                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
406                         zdotc += conjf(Cf(&x[i*incx])) * Cf(&y[i*incy]);
407                 }
408         }
409         pCf(z) = zdotc;
410 }
411 #endif
412 static inline void zdotc_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
413         integer n = *n_, incx = *incx_, incy = *incy_, i;
414 #ifdef _MSC_VER
415         _Dcomplex zdotc = {0.0, 0.0};
416         if (incx == 1 && incy == 1) {
417                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
418                         zdotc._Val[0] += conj(Cd(&x[i]))._Val[0] * Cd(&y[i])._Val[0];
419                         zdotc._Val[1] += conj(Cd(&x[i]))._Val[1] * Cd(&y[i])._Val[1];
420                 }
421         } else {
422                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
423                         zdotc._Val[0] += conj(Cd(&x[i*incx]))._Val[0] * Cd(&y[i*incy])._Val[0];
424                         zdotc._Val[1] += conj(Cd(&x[i*incx]))._Val[1] * Cd(&y[i*incy])._Val[1];
425                 }
426         }
427         pCd(z) = zdotc;
428 }
429 #else
430         _Complex double zdotc = 0.0;
431         if (incx == 1 && incy == 1) {
432                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
433                         zdotc += conj(Cd(&x[i])) * Cd(&y[i]);
434                 }
435         } else {
436                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
437                         zdotc += conj(Cd(&x[i*incx])) * Cd(&y[i*incy]);
438                 }
439         }
440         pCd(z) = zdotc;
441 }
442 #endif  
443 static inline void cdotu_(complex *z, integer *n_, complex *x, integer *incx_, complex *y, integer *incy_) {
444         integer n = *n_, incx = *incx_, incy = *incy_, i;
445 #ifdef _MSC_VER
446         _Fcomplex zdotc = {0.0, 0.0};
447         if (incx == 1 && incy == 1) {
448                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
449                         zdotc._Val[0] += Cf(&x[i])._Val[0] * Cf(&y[i])._Val[0];
450                         zdotc._Val[1] += Cf(&x[i])._Val[1] * Cf(&y[i])._Val[1];
451                 }
452         } else {
453                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
454                         zdotc._Val[0] += Cf(&x[i*incx])._Val[0] * Cf(&y[i*incy])._Val[0];
455                         zdotc._Val[1] += Cf(&x[i*incx])._Val[1] * Cf(&y[i*incy])._Val[1];
456                 }
457         }
458         pCf(z) = zdotc;
459 }
460 #else
461         _Complex float zdotc = 0.0;
462         if (incx == 1 && incy == 1) {
463                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
464                         zdotc += Cf(&x[i]) * Cf(&y[i]);
465                 }
466         } else {
467                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
468                         zdotc += Cf(&x[i*incx]) * Cf(&y[i*incy]);
469                 }
470         }
471         pCf(z) = zdotc;
472 }
473 #endif
474 static inline void zdotu_(doublecomplex *z, integer *n_, doublecomplex *x, integer *incx_, doublecomplex *y, integer *incy_) {
475         integer n = *n_, incx = *incx_, incy = *incy_, i;
476 #ifdef _MSC_VER
477         _Dcomplex zdotc = {0.0, 0.0};
478         if (incx == 1 && incy == 1) {
479                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
480                         zdotc._Val[0] += Cd(&x[i])._Val[0] * Cd(&y[i])._Val[0];
481                         zdotc._Val[1] += Cd(&x[i])._Val[1] * Cd(&y[i])._Val[1];
482                 }
483         } else {
484                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
485                         zdotc._Val[0] += Cd(&x[i*incx])._Val[0] * Cd(&y[i*incy])._Val[0];
486                         zdotc._Val[1] += Cd(&x[i*incx])._Val[1] * Cd(&y[i*incy])._Val[1];
487                 }
488         }
489         pCd(z) = zdotc;
490 }
491 #else
492         _Complex double zdotc = 0.0;
493         if (incx == 1 && incy == 1) {
494                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
495                         zdotc += Cd(&x[i]) * Cd(&y[i]);
496                 }
497         } else {
498                 for (i=0;i<n;i++) { /* zdotc = zdotc + dconjg(x(i))* y(i) */
499                         zdotc += Cd(&x[i*incx]) * Cd(&y[i*incy]);
500                 }
501         }
502         pCd(z) = zdotc;
503 }
504 #endif
505 /*  -- translated by f2c (version 20000121).
506    You must link the resulting object file with the libraries:
507         -lf2c -lm   (in that order)
508 */
509
510
511
512
513 /* Table of constant values */
514
515 static integer c__1 = 1;
516 static real c_b36 = .5f;
517
518 /* > \brief \b SLATRS solves a triangular system of equations with the scale factor set to prevent overflow. 
519 */
520
521 /*  =========== DOCUMENTATION =========== */
522
523 /* Online html documentation available at */
524 /*            http://www.netlib.org/lapack/explore-html/ */
525
526 /* > \htmlonly */
527 /* > Download SLATRS + dependencies */
528 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/slatrs.
529 f"> */
530 /* > [TGZ]</a> */
531 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/slatrs.
532 f"> */
533 /* > [ZIP]</a> */
534 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/slatrs.
535 f"> */
536 /* > [TXT]</a> */
537 /* > \endhtmlonly */
538
539 /*  Definition: */
540 /*  =========== */
541
542 /*       SUBROUTINE SLATRS( UPLO, TRANS, DIAG, NORMIN, N, A, LDA, X, SCALE, */
543 /*                          CNORM, INFO ) */
544
545 /*       CHARACTER          DIAG, NORMIN, TRANS, UPLO */
546 /*       INTEGER            INFO, LDA, N */
547 /*       REAL               SCALE */
548 /*       REAL               A( LDA, * ), CNORM( * ), X( * ) */
549
550
551 /* > \par Purpose: */
552 /*  ============= */
553 /* > */
554 /* > \verbatim */
555 /* > */
556 /* > SLATRS solves one of the triangular systems */
557 /* > */
558 /* >    A *x = s*b  or  A**T*x = s*b */
559 /* > */
560 /* > with scaling to prevent overflow.  Here A is an upper or lower */
561 /* > triangular matrix, A**T denotes the transpose of A, x and b are */
562 /* > n-element vectors, and s is a scaling factor, usually less than */
563 /* > or equal to 1, chosen so that the components of x will be less than */
564 /* > the overflow threshold.  If the unscaled problem will not cause */
565 /* > overflow, the Level 2 BLAS routine STRSV is called.  If the matrix A */
566 /* > is singular (A(j,j) = 0 for some j), then s is set to 0 and a */
567 /* > non-trivial solution to A*x = 0 is returned. */
568 /* > \endverbatim */
569
570 /*  Arguments: */
571 /*  ========== */
572
573 /* > \param[in] UPLO */
574 /* > \verbatim */
575 /* >          UPLO is CHARACTER*1 */
576 /* >          Specifies whether the matrix A is upper or lower triangular. */
577 /* >          = 'U':  Upper triangular */
578 /* >          = 'L':  Lower triangular */
579 /* > \endverbatim */
580 /* > */
581 /* > \param[in] TRANS */
582 /* > \verbatim */
583 /* >          TRANS is CHARACTER*1 */
584 /* >          Specifies the operation applied to A. */
585 /* >          = 'N':  Solve A * x = s*b  (No transpose) */
586 /* >          = 'T':  Solve A**T* x = s*b  (Transpose) */
587 /* >          = 'C':  Solve A**T* x = s*b  (Conjugate transpose = Transpose) */
588 /* > \endverbatim */
589 /* > */
590 /* > \param[in] DIAG */
591 /* > \verbatim */
592 /* >          DIAG is CHARACTER*1 */
593 /* >          Specifies whether or not the matrix A is unit triangular. */
594 /* >          = 'N':  Non-unit triangular */
595 /* >          = 'U':  Unit triangular */
596 /* > \endverbatim */
597 /* > */
598 /* > \param[in] NORMIN */
599 /* > \verbatim */
600 /* >          NORMIN is CHARACTER*1 */
601 /* >          Specifies whether CNORM has been set or not. */
602 /* >          = 'Y':  CNORM contains the column norms on entry */
603 /* >          = 'N':  CNORM is not set on entry.  On exit, the norms will */
604 /* >                  be computed and stored in CNORM. */
605 /* > \endverbatim */
606 /* > */
607 /* > \param[in] N */
608 /* > \verbatim */
609 /* >          N is INTEGER */
610 /* >          The order of the matrix A.  N >= 0. */
611 /* > \endverbatim */
612 /* > */
613 /* > \param[in] A */
614 /* > \verbatim */
615 /* >          A is REAL array, dimension (LDA,N) */
616 /* >          The triangular matrix A.  If UPLO = 'U', the leading n by n */
617 /* >          upper triangular part of the array A contains the upper */
618 /* >          triangular matrix, and the strictly lower triangular part of */
619 /* >          A is not referenced.  If UPLO = 'L', the leading n by n lower */
620 /* >          triangular part of the array A contains the lower triangular */
621 /* >          matrix, and the strictly upper triangular part of A is not */
622 /* >          referenced.  If DIAG = 'U', the diagonal elements of A are */
623 /* >          also not referenced and are assumed to be 1. */
624 /* > \endverbatim */
625 /* > */
626 /* > \param[in] LDA */
627 /* > \verbatim */
628 /* >          LDA is INTEGER */
629 /* >          The leading dimension of the array A.  LDA >= f2cmax (1,N). */
630 /* > \endverbatim */
631 /* > */
632 /* > \param[in,out] X */
633 /* > \verbatim */
634 /* >          X is REAL array, dimension (N) */
635 /* >          On entry, the right hand side b of the triangular system. */
636 /* >          On exit, X is overwritten by the solution vector x. */
637 /* > \endverbatim */
638 /* > */
639 /* > \param[out] SCALE */
640 /* > \verbatim */
641 /* >          SCALE is REAL */
642 /* >          The scaling factor s for the triangular system */
643 /* >             A * x = s*b  or  A**T* x = s*b. */
644 /* >          If SCALE = 0, the matrix A is singular or badly scaled, and */
645 /* >          the vector x is an exact or approximate solution to A*x = 0. */
646 /* > \endverbatim */
647 /* > */
648 /* > \param[in,out] CNORM */
649 /* > \verbatim */
650 /* >          CNORM is REAL array, dimension (N) */
651 /* > */
652 /* >          If NORMIN = 'Y', CNORM is an input argument and CNORM(j) */
653 /* >          contains the norm of the off-diagonal part of the j-th column */
654 /* >          of A.  If TRANS = 'N', CNORM(j) must be greater than or equal */
655 /* >          to the infinity-norm, and if TRANS = 'T' or 'C', CNORM(j) */
656 /* >          must be greater than or equal to the 1-norm. */
657 /* > */
658 /* >          If NORMIN = 'N', CNORM is an output argument and CNORM(j) */
659 /* >          returns the 1-norm of the offdiagonal part of the j-th column */
660 /* >          of A. */
661 /* > \endverbatim */
662 /* > */
663 /* > \param[out] INFO */
664 /* > \verbatim */
665 /* >          INFO is INTEGER */
666 /* >          = 0:  successful exit */
667 /* >          < 0:  if INFO = -k, the k-th argument had an illegal value */
668 /* > \endverbatim */
669
670 /*  Authors: */
671 /*  ======== */
672
673 /* > \author Univ. of Tennessee */
674 /* > \author Univ. of California Berkeley */
675 /* > \author Univ. of Colorado Denver */
676 /* > \author NAG Ltd. */
677
678 /* > \date December 2016 */
679
680 /* > \ingroup realOTHERauxiliary */
681
682 /* > \par Further Details: */
683 /*  ===================== */
684 /* > */
685 /* > \verbatim */
686 /* > */
687 /* >  A rough bound on x is computed; if that is less than overflow, STRSV */
688 /* >  is called, otherwise, specific code is used which checks for possible */
689 /* >  overflow or divide-by-zero at every operation. */
690 /* > */
691 /* >  A columnwise scheme is used for solving A*x = b.  The basic algorithm */
692 /* >  if A is lower triangular is */
693 /* > */
694 /* >       x[1:n] := b[1:n] */
695 /* >       for j = 1, ..., n */
696 /* >            x(j) := x(j) / A(j,j) */
697 /* >            x[j+1:n] := x[j+1:n] - x(j) * A[j+1:n,j] */
698 /* >       end */
699 /* > */
700 /* >  Define bounds on the components of x after j iterations of the loop: */
701 /* >     M(j) = bound on x[1:j] */
702 /* >     G(j) = bound on x[j+1:n] */
703 /* >  Initially, let M(0) = 0 and G(0) = f2cmax{x(i), i=1,...,n}. */
704 /* > */
705 /* >  Then for iteration j+1 we have */
706 /* >     M(j+1) <= G(j) / | A(j+1,j+1) | */
707 /* >     G(j+1) <= G(j) + M(j+1) * | A[j+2:n,j+1] | */
708 /* >            <= G(j) ( 1 + CNORM(j+1) / | A(j+1,j+1) | ) */
709 /* > */
710 /* >  where CNORM(j+1) is greater than or equal to the infinity-norm of */
711 /* >  column j+1 of A, not counting the diagonal.  Hence */
712 /* > */
713 /* >     G(j) <= G(0) product ( 1 + CNORM(i) / | A(i,i) | ) */
714 /* >                  1<=i<=j */
715 /* >  and */
716 /* > */
717 /* >     |x(j)| <= ( G(0) / |A(j,j)| ) product ( 1 + CNORM(i) / |A(i,i)| ) */
718 /* >                                   1<=i< j */
719 /* > */
720 /* >  Since |x(j)| <= M(j), we use the Level 2 BLAS routine STRSV if the */
721 /* >  reciprocal of the largest M(j), j=1,..,n, is larger than */
722 /* >  f2cmax(underflow, 1/overflow). */
723 /* > */
724 /* >  The bound on x(j) is also used to determine when a step in the */
725 /* >  columnwise method can be performed without fear of overflow.  If */
726 /* >  the computed bound is greater than a large constant, x is scaled to */
727 /* >  prevent overflow, but if the bound overflows, x is set to 0, x(j) to */
728 /* >  1, and scale to 0, and a non-trivial solution to A*x = 0 is found. */
729 /* > */
730 /* >  Similarly, a row-wise scheme is used to solve A**T*x = b.  The basic */
731 /* >  algorithm for A upper triangular is */
732 /* > */
733 /* >       for j = 1, ..., n */
734 /* >            x(j) := ( b(j) - A[1:j-1,j]**T * x[1:j-1] ) / A(j,j) */
735 /* >       end */
736 /* > */
737 /* >  We simultaneously compute two bounds */
738 /* >       G(j) = bound on ( b(i) - A[1:i-1,i]**T * x[1:i-1] ), 1<=i<=j */
739 /* >       M(j) = bound on x(i), 1<=i<=j */
740 /* > */
741 /* >  The initial values are G(0) = 0, M(0) = f2cmax{b(i), i=1,..,n}, and we */
742 /* >  add the constraint G(j) >= G(j-1) and M(j) >= M(j-1) for j >= 1. */
743 /* >  Then the bound on x(j) is */
744 /* > */
745 /* >       M(j) <= M(j-1) * ( 1 + CNORM(j) ) / | A(j,j) | */
746 /* > */
747 /* >            <= M(0) * product ( ( 1 + CNORM(i) ) / |A(i,i)| ) */
748 /* >                      1<=i<=j */
749 /* > */
750 /* >  and we can safely call STRSV if 1/M(n) and 1/G(n) are both greater */
751 /* >  than f2cmax(underflow, 1/overflow). */
752 /* > \endverbatim */
753 /* > */
754 /*  ===================================================================== */
755 /* Subroutine */ int slatrs_(char *uplo, char *trans, char *diag, char *
756         normin, integer *n, real *a, integer *lda, real *x, real *scale, real 
757         *cnorm, integer *info)
758 {
759     /* System generated locals */
760     integer a_dim1, a_offset, i__1, i__2, i__3;
761     real r__1, r__2, r__3;
762
763     /* Local variables */
764     integer jinc;
765     real xbnd;
766     integer imax;
767     real tmax, tjjs;
768     extern real sdot_(integer *, real *, integer *, real *, integer *);
769     real xmax, grow, sumj;
770     integer i__, j;
771     extern logical lsame_(char *, char *);
772     extern /* Subroutine */ int sscal_(integer *, real *, real *, integer *);
773     real tscal, uscal;
774     integer jlast;
775     extern real sasum_(integer *, real *, integer *);
776     logical upper;
777     extern /* Subroutine */ int saxpy_(integer *, real *, real *, integer *, 
778             real *, integer *), strsv_(char *, char *, char *, integer *, 
779             real *, integer *, real *, integer *);
780     real xj;
781     extern real slamch_(char *);
782     extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
783     real bignum;
784     extern integer isamax_(integer *, real *, integer *);
785     logical notran;
786     integer jfirst;
787     real smlnum;
788     logical nounit;
789     real rec, tjj;
790
791
792 /*  -- LAPACK auxiliary routine (version 3.7.0) -- */
793 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
794 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
795 /*     December 2016 */
796
797
798 /*  ===================================================================== */
799
800
801     /* Parameter adjustments */
802     a_dim1 = *lda;
803     a_offset = 1 + a_dim1 * 1;
804     a -= a_offset;
805     --x;
806     --cnorm;
807
808     /* Function Body */
809     *info = 0;
810     upper = lsame_(uplo, "U");
811     notran = lsame_(trans, "N");
812     nounit = lsame_(diag, "N");
813
814 /*     Test the input parameters. */
815
816     if (! upper && ! lsame_(uplo, "L")) {
817         *info = -1;
818     } else if (! notran && ! lsame_(trans, "T") && ! 
819             lsame_(trans, "C")) {
820         *info = -2;
821     } else if (! nounit && ! lsame_(diag, "U")) {
822         *info = -3;
823     } else if (! lsame_(normin, "Y") && ! lsame_(normin,
824              "N")) {
825         *info = -4;
826     } else if (*n < 0) {
827         *info = -5;
828     } else if (*lda < f2cmax(1,*n)) {
829         *info = -7;
830     }
831     if (*info != 0) {
832         i__1 = -(*info);
833         xerbla_("SLATRS", &i__1, (ftnlen)6);
834         return 0;
835     }
836
837 /*     Quick return if possible */
838
839     if (*n == 0) {
840         return 0;
841     }
842
843 /*     Determine machine dependent parameters to control overflow. */
844
845     smlnum = slamch_("Safe minimum") / slamch_("Precision");
846     bignum = 1.f / smlnum;
847     *scale = 1.f;
848
849     if (lsame_(normin, "N")) {
850
851 /*        Compute the 1-norm of each column, not including the diagonal. */
852
853         if (upper) {
854
855 /*           A is upper triangular. */
856
857             i__1 = *n;
858             for (j = 1; j <= i__1; ++j) {
859                 i__2 = j - 1;
860                 cnorm[j] = sasum_(&i__2, &a[j * a_dim1 + 1], &c__1);
861 /* L10: */
862             }
863         } else {
864
865 /*           A is lower triangular. */
866
867             i__1 = *n - 1;
868             for (j = 1; j <= i__1; ++j) {
869                 i__2 = *n - j;
870                 cnorm[j] = sasum_(&i__2, &a[j + 1 + j * a_dim1], &c__1);
871 /* L20: */
872             }
873             cnorm[*n] = 0.f;
874         }
875     }
876
877 /*     Scale the column norms by TSCAL if the maximum element in CNORM is */
878 /*     greater than BIGNUM. */
879
880     imax = isamax_(n, &cnorm[1], &c__1);
881     tmax = cnorm[imax];
882     if (tmax <= bignum) {
883         tscal = 1.f;
884     } else {
885         tscal = 1.f / (smlnum * tmax);
886         sscal_(n, &tscal, &cnorm[1], &c__1);
887     }
888
889 /*     Compute a bound on the computed solution vector to see if the */
890 /*     Level 2 BLAS routine STRSV can be used. */
891
892     j = isamax_(n, &x[1], &c__1);
893     xmax = (r__1 = x[j], abs(r__1));
894     xbnd = xmax;
895     if (notran) {
896
897 /*        Compute the growth in A * x = b. */
898
899         if (upper) {
900             jfirst = *n;
901             jlast = 1;
902             jinc = -1;
903         } else {
904             jfirst = 1;
905             jlast = *n;
906             jinc = 1;
907         }
908
909         if (tscal != 1.f) {
910             grow = 0.f;
911             goto L50;
912         }
913
914         if (nounit) {
915
916 /*           A is non-unit triangular. */
917
918 /*           Compute GROW = 1/G(j) and XBND = 1/M(j). */
919 /*           Initially, G(0) = f2cmax{x(i), i=1,...,n}. */
920
921             grow = 1.f / f2cmax(xbnd,smlnum);
922             xbnd = grow;
923             i__1 = jlast;
924             i__2 = jinc;
925             for (j = jfirst; i__2 < 0 ? j >= i__1 : j <= i__1; j += i__2) {
926
927 /*              Exit the loop if the growth factor is too small. */
928
929                 if (grow <= smlnum) {
930                     goto L50;
931                 }
932
933 /*              M(j) = G(j-1) / abs(A(j,j)) */
934
935                 tjj = (r__1 = a[j + j * a_dim1], abs(r__1));
936 /* Computing MIN */
937                 r__1 = xbnd, r__2 = f2cmin(1.f,tjj) * grow;
938                 xbnd = f2cmin(r__1,r__2);
939                 if (tjj + cnorm[j] >= smlnum) {
940
941 /*                 G(j) = G(j-1)*( 1 + CNORM(j) / abs(A(j,j)) ) */
942
943                     grow *= tjj / (tjj + cnorm[j]);
944                 } else {
945
946 /*                 G(j) could overflow, set GROW to 0. */
947
948                     grow = 0.f;
949                 }
950 /* L30: */
951             }
952             grow = xbnd;
953         } else {
954
955 /*           A is unit triangular. */
956
957 /*           Compute GROW = 1/G(j), where G(0) = f2cmax{x(i), i=1,...,n}. */
958
959 /* Computing MIN */
960             r__1 = 1.f, r__2 = 1.f / f2cmax(xbnd,smlnum);
961             grow = f2cmin(r__1,r__2);
962             i__2 = jlast;
963             i__1 = jinc;
964             for (j = jfirst; i__1 < 0 ? j >= i__2 : j <= i__2; j += i__1) {
965
966 /*              Exit the loop if the growth factor is too small. */
967
968                 if (grow <= smlnum) {
969                     goto L50;
970                 }
971
972 /*              G(j) = G(j-1)*( 1 + CNORM(j) ) */
973
974                 grow *= 1.f / (cnorm[j] + 1.f);
975 /* L40: */
976             }
977         }
978 L50:
979
980         ;
981     } else {
982
983 /*        Compute the growth in A**T * x = b. */
984
985         if (upper) {
986             jfirst = 1;
987             jlast = *n;
988             jinc = 1;
989         } else {
990             jfirst = *n;
991             jlast = 1;
992             jinc = -1;
993         }
994
995         if (tscal != 1.f) {
996             grow = 0.f;
997             goto L80;
998         }
999
1000         if (nounit) {
1001
1002 /*           A is non-unit triangular. */
1003
1004 /*           Compute GROW = 1/G(j) and XBND = 1/M(j). */
1005 /*           Initially, M(0) = f2cmax{x(i), i=1,...,n}. */
1006
1007             grow = 1.f / f2cmax(xbnd,smlnum);
1008             xbnd = grow;
1009             i__1 = jlast;
1010             i__2 = jinc;
1011             for (j = jfirst; i__2 < 0 ? j >= i__1 : j <= i__1; j += i__2) {
1012
1013 /*              Exit the loop if the growth factor is too small. */
1014
1015                 if (grow <= smlnum) {
1016                     goto L80;
1017                 }
1018
1019 /*              G(j) = f2cmax( G(j-1), M(j-1)*( 1 + CNORM(j) ) ) */
1020
1021                 xj = cnorm[j] + 1.f;
1022 /* Computing MIN */
1023                 r__1 = grow, r__2 = xbnd / xj;
1024                 grow = f2cmin(r__1,r__2);
1025
1026 /*              M(j) = M(j-1)*( 1 + CNORM(j) ) / abs(A(j,j)) */
1027
1028                 tjj = (r__1 = a[j + j * a_dim1], abs(r__1));
1029                 if (xj > tjj) {
1030                     xbnd *= tjj / xj;
1031                 }
1032 /* L60: */
1033             }
1034             grow = f2cmin(grow,xbnd);
1035         } else {
1036
1037 /*           A is unit triangular. */
1038
1039 /*           Compute GROW = 1/G(j), where G(0) = f2cmax{x(i), i=1,...,n}. */
1040
1041 /* Computing MIN */
1042             r__1 = 1.f, r__2 = 1.f / f2cmax(xbnd,smlnum);
1043             grow = f2cmin(r__1,r__2);
1044             i__2 = jlast;
1045             i__1 = jinc;
1046             for (j = jfirst; i__1 < 0 ? j >= i__2 : j <= i__2; j += i__1) {
1047
1048 /*              Exit the loop if the growth factor is too small. */
1049
1050                 if (grow <= smlnum) {
1051                     goto L80;
1052                 }
1053
1054 /*              G(j) = ( 1 + CNORM(j) )*G(j-1) */
1055
1056                 xj = cnorm[j] + 1.f;
1057                 grow /= xj;
1058 /* L70: */
1059             }
1060         }
1061 L80:
1062         ;
1063     }
1064
1065     if (grow * tscal > smlnum) {
1066
1067 /*        Use the Level 2 BLAS solve if the reciprocal of the bound on */
1068 /*        elements of X is not too small. */
1069
1070         strsv_(uplo, trans, diag, n, &a[a_offset], lda, &x[1], &c__1);
1071     } else {
1072
1073 /*        Use a Level 1 BLAS solve, scaling intermediate results. */
1074
1075         if (xmax > bignum) {
1076
1077 /*           Scale X so that its components are less than or equal to */
1078 /*           BIGNUM in absolute value. */
1079
1080             *scale = bignum / xmax;
1081             sscal_(n, scale, &x[1], &c__1);
1082             xmax = bignum;
1083         }
1084
1085         if (notran) {
1086
1087 /*           Solve A * x = b */
1088
1089             i__1 = jlast;
1090             i__2 = jinc;
1091             for (j = jfirst; i__2 < 0 ? j >= i__1 : j <= i__1; j += i__2) {
1092
1093 /*              Compute x(j) = b(j) / A(j,j), scaling x if necessary. */
1094
1095                 xj = (r__1 = x[j], abs(r__1));
1096                 if (nounit) {
1097                     tjjs = a[j + j * a_dim1] * tscal;
1098                 } else {
1099                     tjjs = tscal;
1100                     if (tscal == 1.f) {
1101                         goto L95;
1102                     }
1103                 }
1104                 tjj = abs(tjjs);
1105                 if (tjj > smlnum) {
1106
1107 /*                    abs(A(j,j)) > SMLNUM: */
1108
1109                     if (tjj < 1.f) {
1110                         if (xj > tjj * bignum) {
1111
1112 /*                          Scale x by 1/b(j). */
1113
1114                             rec = 1.f / xj;
1115                             sscal_(n, &rec, &x[1], &c__1);
1116                             *scale *= rec;
1117                             xmax *= rec;
1118                         }
1119                     }
1120                     x[j] /= tjjs;
1121                     xj = (r__1 = x[j], abs(r__1));
1122                 } else if (tjj > 0.f) {
1123
1124 /*                    0 < abs(A(j,j)) <= SMLNUM: */
1125
1126                     if (xj > tjj * bignum) {
1127
1128 /*                       Scale x by (1/abs(x(j)))*abs(A(j,j))*BIGNUM */
1129 /*                       to avoid overflow when dividing by A(j,j). */
1130
1131                         rec = tjj * bignum / xj;
1132                         if (cnorm[j] > 1.f) {
1133
1134 /*                          Scale by 1/CNORM(j) to avoid overflow when */
1135 /*                          multiplying x(j) times column j. */
1136
1137                             rec /= cnorm[j];
1138                         }
1139                         sscal_(n, &rec, &x[1], &c__1);
1140                         *scale *= rec;
1141                         xmax *= rec;
1142                     }
1143                     x[j] /= tjjs;
1144                     xj = (r__1 = x[j], abs(r__1));
1145                 } else {
1146
1147 /*                    A(j,j) = 0:  Set x(1:n) = 0, x(j) = 1, and */
1148 /*                    scale = 0, and compute a solution to A*x = 0. */
1149
1150                     i__3 = *n;
1151                     for (i__ = 1; i__ <= i__3; ++i__) {
1152                         x[i__] = 0.f;
1153 /* L90: */
1154                     }
1155                     x[j] = 1.f;
1156                     xj = 1.f;
1157                     *scale = 0.f;
1158                     xmax = 0.f;
1159                 }
1160 L95:
1161
1162 /*              Scale x if necessary to avoid overflow when adding a */
1163 /*              multiple of column j of A. */
1164
1165                 if (xj > 1.f) {
1166                     rec = 1.f / xj;
1167                     if (cnorm[j] > (bignum - xmax) * rec) {
1168
1169 /*                    Scale x by 1/(2*abs(x(j))). */
1170
1171                         rec *= .5f;
1172                         sscal_(n, &rec, &x[1], &c__1);
1173                         *scale *= rec;
1174                     }
1175                 } else if (xj * cnorm[j] > bignum - xmax) {
1176
1177 /*                 Scale x by 1/2. */
1178
1179                     sscal_(n, &c_b36, &x[1], &c__1);
1180                     *scale *= .5f;
1181                 }
1182
1183                 if (upper) {
1184                     if (j > 1) {
1185
1186 /*                    Compute the update */
1187 /*                       x(1:j-1) := x(1:j-1) - x(j) * A(1:j-1,j) */
1188
1189                         i__3 = j - 1;
1190                         r__1 = -x[j] * tscal;
1191                         saxpy_(&i__3, &r__1, &a[j * a_dim1 + 1], &c__1, &x[1],
1192                                  &c__1);
1193                         i__3 = j - 1;
1194                         i__ = isamax_(&i__3, &x[1], &c__1);
1195                         xmax = (r__1 = x[i__], abs(r__1));
1196                     }
1197                 } else {
1198                     if (j < *n) {
1199
1200 /*                    Compute the update */
1201 /*                       x(j+1:n) := x(j+1:n) - x(j) * A(j+1:n,j) */
1202
1203                         i__3 = *n - j;
1204                         r__1 = -x[j] * tscal;
1205                         saxpy_(&i__3, &r__1, &a[j + 1 + j * a_dim1], &c__1, &
1206                                 x[j + 1], &c__1);
1207                         i__3 = *n - j;
1208                         i__ = j + isamax_(&i__3, &x[j + 1], &c__1);
1209                         xmax = (r__1 = x[i__], abs(r__1));
1210                     }
1211                 }
1212 /* L100: */
1213             }
1214
1215         } else {
1216
1217 /*           Solve A**T * x = b */
1218
1219             i__2 = jlast;
1220             i__1 = jinc;
1221             for (j = jfirst; i__1 < 0 ? j >= i__2 : j <= i__2; j += i__1) {
1222
1223 /*              Compute x(j) = b(j) - sum A(k,j)*x(k). */
1224 /*                                    k<>j */
1225
1226                 xj = (r__1 = x[j], abs(r__1));
1227                 uscal = tscal;
1228                 rec = 1.f / f2cmax(xmax,1.f);
1229                 if (cnorm[j] > (bignum - xj) * rec) {
1230
1231 /*                 If x(j) could overflow, scale x by 1/(2*XMAX). */
1232
1233                     rec *= .5f;
1234                     if (nounit) {
1235                         tjjs = a[j + j * a_dim1] * tscal;
1236                     } else {
1237                         tjjs = tscal;
1238                     }
1239                     tjj = abs(tjjs);
1240                     if (tjj > 1.f) {
1241
1242 /*                       Divide by A(j,j) when scaling x if A(j,j) > 1. */
1243
1244 /* Computing MIN */
1245                         r__1 = 1.f, r__2 = rec * tjj;
1246                         rec = f2cmin(r__1,r__2);
1247                         uscal /= tjjs;
1248                     }
1249                     if (rec < 1.f) {
1250                         sscal_(n, &rec, &x[1], &c__1);
1251                         *scale *= rec;
1252                         xmax *= rec;
1253                     }
1254                 }
1255
1256                 sumj = 0.f;
1257                 if (uscal == 1.f) {
1258
1259 /*                 If the scaling needed for A in the dot product is 1, */
1260 /*                 call SDOT to perform the dot product. */
1261
1262                     if (upper) {
1263                         i__3 = j - 1;
1264                         sumj = sdot_(&i__3, &a[j * a_dim1 + 1], &c__1, &x[1], 
1265                                 &c__1);
1266                     } else if (j < *n) {
1267                         i__3 = *n - j;
1268                         sumj = sdot_(&i__3, &a[j + 1 + j * a_dim1], &c__1, &x[
1269                                 j + 1], &c__1);
1270                     }
1271                 } else {
1272
1273 /*                 Otherwise, use in-line code for the dot product. */
1274
1275                     if (upper) {
1276                         i__3 = j - 1;
1277                         for (i__ = 1; i__ <= i__3; ++i__) {
1278                             sumj += a[i__ + j * a_dim1] * uscal * x[i__];
1279 /* L110: */
1280                         }
1281                     } else if (j < *n) {
1282                         i__3 = *n;
1283                         for (i__ = j + 1; i__ <= i__3; ++i__) {
1284                             sumj += a[i__ + j * a_dim1] * uscal * x[i__];
1285 /* L120: */
1286                         }
1287                     }
1288                 }
1289
1290                 if (uscal == tscal) {
1291
1292 /*                 Compute x(j) := ( x(j) - sumj ) / A(j,j) if 1/A(j,j) */
1293 /*                 was not used to scale the dotproduct. */
1294
1295                     x[j] -= sumj;
1296                     xj = (r__1 = x[j], abs(r__1));
1297                     if (nounit) {
1298                         tjjs = a[j + j * a_dim1] * tscal;
1299                     } else {
1300                         tjjs = tscal;
1301                         if (tscal == 1.f) {
1302                             goto L135;
1303                         }
1304                     }
1305
1306 /*                    Compute x(j) = x(j) / A(j,j), scaling if necessary. */
1307
1308                     tjj = abs(tjjs);
1309                     if (tjj > smlnum) {
1310
1311 /*                       abs(A(j,j)) > SMLNUM: */
1312
1313                         if (tjj < 1.f) {
1314                             if (xj > tjj * bignum) {
1315
1316 /*                             Scale X by 1/abs(x(j)). */
1317
1318                                 rec = 1.f / xj;
1319                                 sscal_(n, &rec, &x[1], &c__1);
1320                                 *scale *= rec;
1321                                 xmax *= rec;
1322                             }
1323                         }
1324                         x[j] /= tjjs;
1325                     } else if (tjj > 0.f) {
1326
1327 /*                       0 < abs(A(j,j)) <= SMLNUM: */
1328
1329                         if (xj > tjj * bignum) {
1330
1331 /*                          Scale x by (1/abs(x(j)))*abs(A(j,j))*BIGNUM. */
1332
1333                             rec = tjj * bignum / xj;
1334                             sscal_(n, &rec, &x[1], &c__1);
1335                             *scale *= rec;
1336                             xmax *= rec;
1337                         }
1338                         x[j] /= tjjs;
1339                     } else {
1340
1341 /*                       A(j,j) = 0:  Set x(1:n) = 0, x(j) = 1, and */
1342 /*                       scale = 0, and compute a solution to A**T*x = 0. */
1343
1344                         i__3 = *n;
1345                         for (i__ = 1; i__ <= i__3; ++i__) {
1346                             x[i__] = 0.f;
1347 /* L130: */
1348                         }
1349                         x[j] = 1.f;
1350                         *scale = 0.f;
1351                         xmax = 0.f;
1352                     }
1353 L135:
1354                     ;
1355                 } else {
1356
1357 /*                 Compute x(j) := x(j) / A(j,j)  - sumj if the dot */
1358 /*                 product has already been divided by 1/A(j,j). */
1359
1360                     x[j] = x[j] / tjjs - sumj;
1361                 }
1362 /* Computing MAX */
1363                 r__2 = xmax, r__3 = (r__1 = x[j], abs(r__1));
1364                 xmax = f2cmax(r__2,r__3);
1365 /* L140: */
1366             }
1367         }
1368         *scale /= tscal;
1369     }
1370
1371 /*     Scale the column norms by 1/TSCAL for return. */
1372
1373     if (tscal != 1.f) {
1374         r__1 = 1.f / tscal;
1375         sscal_(n, &r__1, &cnorm[1], &c__1);
1376     }
1377
1378     return 0;
1379
1380 /*     End of SLATRS */
1381
1382 } /* slatrs_ */
1383