C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / zlatrs.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]/Cd(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 doublereal c_b36 = .5;
517
518 /* > \brief \b ZLATRS 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 ZLATRS + dependencies */
528 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/zlatrs.
529 f"> */
530 /* > [TGZ]</a> */
531 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/zlatrs.
532 f"> */
533 /* > [ZIP]</a> */
534 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/zlatrs.
535 f"> */
536 /* > [TXT]</a> */
537 /* > \endhtmlonly */
538
539 /*  Definition: */
540 /*  =========== */
541
542 /*       SUBROUTINE ZLATRS( 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 /*       DOUBLE PRECISION   SCALE */
548 /*       DOUBLE PRECISION   CNORM( * ) */
549 /*       COMPLEX*16         A( LDA, * ), X( * ) */
550
551
552 /* > \par Purpose: */
553 /*  ============= */
554 /* > */
555 /* > \verbatim */
556 /* > */
557 /* > ZLATRS solves one of the triangular systems */
558 /* > */
559 /* >    A * x = s*b,  A**T * x = s*b,  or  A**H * x = s*b, */
560 /* > */
561 /* > with scaling to prevent overflow.  Here A is an upper or lower */
562 /* > triangular matrix, A**T denotes the transpose of A, A**H denotes the */
563 /* > conjugate transpose of A, x and b are n-element vectors, and s is a */
564 /* > scaling factor, usually less than or equal to 1, chosen so that the */
565 /* > components of x will be less than the overflow threshold.  If the */
566 /* > unscaled problem will not cause overflow, the Level 2 BLAS routine */
567 /* > ZTRSV is called. If the matrix A is singular (A(j,j) = 0 for some j), */
568 /* > then s is set to 0 and a non-trivial solution to A*x = 0 is returned. */
569 /* > \endverbatim */
570
571 /*  Arguments: */
572 /*  ========== */
573
574 /* > \param[in] UPLO */
575 /* > \verbatim */
576 /* >          UPLO is CHARACTER*1 */
577 /* >          Specifies whether the matrix A is upper or lower triangular. */
578 /* >          = 'U':  Upper triangular */
579 /* >          = 'L':  Lower triangular */
580 /* > \endverbatim */
581 /* > */
582 /* > \param[in] TRANS */
583 /* > \verbatim */
584 /* >          TRANS is CHARACTER*1 */
585 /* >          Specifies the operation applied to A. */
586 /* >          = 'N':  Solve A * x = s*b     (No transpose) */
587 /* >          = 'T':  Solve A**T * x = s*b  (Transpose) */
588 /* >          = 'C':  Solve A**H * x = s*b  (Conjugate transpose) */
589 /* > \endverbatim */
590 /* > */
591 /* > \param[in] DIAG */
592 /* > \verbatim */
593 /* >          DIAG is CHARACTER*1 */
594 /* >          Specifies whether or not the matrix A is unit triangular. */
595 /* >          = 'N':  Non-unit triangular */
596 /* >          = 'U':  Unit triangular */
597 /* > \endverbatim */
598 /* > */
599 /* > \param[in] NORMIN */
600 /* > \verbatim */
601 /* >          NORMIN is CHARACTER*1 */
602 /* >          Specifies whether CNORM has been set or not. */
603 /* >          = 'Y':  CNORM contains the column norms on entry */
604 /* >          = 'N':  CNORM is not set on entry.  On exit, the norms will */
605 /* >                  be computed and stored in CNORM. */
606 /* > \endverbatim */
607 /* > */
608 /* > \param[in] N */
609 /* > \verbatim */
610 /* >          N is INTEGER */
611 /* >          The order of the matrix A.  N >= 0. */
612 /* > \endverbatim */
613 /* > */
614 /* > \param[in] A */
615 /* > \verbatim */
616 /* >          A is COMPLEX*16 array, dimension (LDA,N) */
617 /* >          The triangular matrix A.  If UPLO = 'U', the leading n by n */
618 /* >          upper triangular part of the array A contains the upper */
619 /* >          triangular matrix, and the strictly lower triangular part of */
620 /* >          A is not referenced.  If UPLO = 'L', the leading n by n lower */
621 /* >          triangular part of the array A contains the lower triangular */
622 /* >          matrix, and the strictly upper triangular part of A is not */
623 /* >          referenced.  If DIAG = 'U', the diagonal elements of A are */
624 /* >          also not referenced and are assumed to be 1. */
625 /* > \endverbatim */
626 /* > */
627 /* > \param[in] LDA */
628 /* > \verbatim */
629 /* >          LDA is INTEGER */
630 /* >          The leading dimension of the array A.  LDA >= f2cmax (1,N). */
631 /* > \endverbatim */
632 /* > */
633 /* > \param[in,out] X */
634 /* > \verbatim */
635 /* >          X is COMPLEX*16 array, dimension (N) */
636 /* >          On entry, the right hand side b of the triangular system. */
637 /* >          On exit, X is overwritten by the solution vector x. */
638 /* > \endverbatim */
639 /* > */
640 /* > \param[out] SCALE */
641 /* > \verbatim */
642 /* >          SCALE is DOUBLE PRECISION */
643 /* >          The scaling factor s for the triangular system */
644 /* >             A * x = s*b,  A**T * x = s*b,  or  A**H * x = s*b. */
645 /* >          If SCALE = 0, the matrix A is singular or badly scaled, and */
646 /* >          the vector x is an exact or approximate solution to A*x = 0. */
647 /* > \endverbatim */
648 /* > */
649 /* > \param[in,out] CNORM */
650 /* > \verbatim */
651 /* >          CNORM is DOUBLE PRECISION array, dimension (N) */
652 /* > */
653 /* >          If NORMIN = 'Y', CNORM is an input argument and CNORM(j) */
654 /* >          contains the norm of the off-diagonal part of the j-th column */
655 /* >          of A.  If TRANS = 'N', CNORM(j) must be greater than or equal */
656 /* >          to the infinity-norm, and if TRANS = 'T' or 'C', CNORM(j) */
657 /* >          must be greater than or equal to the 1-norm. */
658 /* > */
659 /* >          If NORMIN = 'N', CNORM is an output argument and CNORM(j) */
660 /* >          returns the 1-norm of the offdiagonal part of the j-th column */
661 /* >          of A. */
662 /* > \endverbatim */
663 /* > */
664 /* > \param[out] INFO */
665 /* > \verbatim */
666 /* >          INFO is INTEGER */
667 /* >          = 0:  successful exit */
668 /* >          < 0:  if INFO = -k, the k-th argument had an illegal value */
669 /* > \endverbatim */
670
671 /*  Authors: */
672 /*  ======== */
673
674 /* > \author Univ. of Tennessee */
675 /* > \author Univ. of California Berkeley */
676 /* > \author Univ. of Colorado Denver */
677 /* > \author NAG Ltd. */
678
679 /* > \date November 2017 */
680
681 /* > \ingroup complex16OTHERauxiliary */
682
683 /* > \par Further Details: */
684 /*  ===================== */
685 /* > */
686 /* > \verbatim */
687 /* > */
688 /* >  A rough bound on x is computed; if that is less than overflow, ZTRSV */
689 /* >  is called, otherwise, specific code is used which checks for possible */
690 /* >  overflow or divide-by-zero at every operation. */
691 /* > */
692 /* >  A columnwise scheme is used for solving A*x = b.  The basic algorithm */
693 /* >  if A is lower triangular is */
694 /* > */
695 /* >       x[1:n] := b[1:n] */
696 /* >       for j = 1, ..., n */
697 /* >            x(j) := x(j) / A(j,j) */
698 /* >            x[j+1:n] := x[j+1:n] - x(j) * A[j+1:n,j] */
699 /* >       end */
700 /* > */
701 /* >  Define bounds on the components of x after j iterations of the loop: */
702 /* >     M(j) = bound on x[1:j] */
703 /* >     G(j) = bound on x[j+1:n] */
704 /* >  Initially, let M(0) = 0 and G(0) = f2cmax{x(i), i=1,...,n}. */
705 /* > */
706 /* >  Then for iteration j+1 we have */
707 /* >     M(j+1) <= G(j) / | A(j+1,j+1) | */
708 /* >     G(j+1) <= G(j) + M(j+1) * | A[j+2:n,j+1] | */
709 /* >            <= G(j) ( 1 + CNORM(j+1) / | A(j+1,j+1) | ) */
710 /* > */
711 /* >  where CNORM(j+1) is greater than or equal to the infinity-norm of */
712 /* >  column j+1 of A, not counting the diagonal.  Hence */
713 /* > */
714 /* >     G(j) <= G(0) product ( 1 + CNORM(i) / | A(i,i) | ) */
715 /* >                  1<=i<=j */
716 /* >  and */
717 /* > */
718 /* >     |x(j)| <= ( G(0) / |A(j,j)| ) product ( 1 + CNORM(i) / |A(i,i)| ) */
719 /* >                                   1<=i< j */
720 /* > */
721 /* >  Since |x(j)| <= M(j), we use the Level 2 BLAS routine ZTRSV if the */
722 /* >  reciprocal of the largest M(j), j=1,..,n, is larger than */
723 /* >  f2cmax(underflow, 1/overflow). */
724 /* > */
725 /* >  The bound on x(j) is also used to determine when a step in the */
726 /* >  columnwise method can be performed without fear of overflow.  If */
727 /* >  the computed bound is greater than a large constant, x is scaled to */
728 /* >  prevent overflow, but if the bound overflows, x is set to 0, x(j) to */
729 /* >  1, and scale to 0, and a non-trivial solution to A*x = 0 is found. */
730 /* > */
731 /* >  Similarly, a row-wise scheme is used to solve A**T *x = b  or */
732 /* >  A**H *x = b.  The basic algorithm for A upper triangular is */
733 /* > */
734 /* >       for j = 1, ..., n */
735 /* >            x(j) := ( b(j) - A[1:j-1,j]' * x[1:j-1] ) / A(j,j) */
736 /* >       end */
737 /* > */
738 /* >  We simultaneously compute two bounds */
739 /* >       G(j) = bound on ( b(i) - A[1:i-1,i]' * x[1:i-1] ), 1<=i<=j */
740 /* >       M(j) = bound on x(i), 1<=i<=j */
741 /* > */
742 /* >  The initial values are G(0) = 0, M(0) = f2cmax{b(i), i=1,..,n}, and we */
743 /* >  add the constraint G(j) >= G(j-1) and M(j) >= M(j-1) for j >= 1. */
744 /* >  Then the bound on x(j) is */
745 /* > */
746 /* >       M(j) <= M(j-1) * ( 1 + CNORM(j) ) / | A(j,j) | */
747 /* > */
748 /* >            <= M(0) * product ( ( 1 + CNORM(i) ) / |A(i,i)| ) */
749 /* >                      1<=i<=j */
750 /* > */
751 /* >  and we can safely call ZTRSV if 1/M(n) and 1/G(n) are both greater */
752 /* >  than f2cmax(underflow, 1/overflow). */
753 /* > \endverbatim */
754 /* > */
755 /*  ===================================================================== */
756 /* Subroutine */ int zlatrs_(char *uplo, char *trans, char *diag, char *
757         normin, integer *n, doublecomplex *a, integer *lda, doublecomplex *x, 
758         doublereal *scale, doublereal *cnorm, integer *info)
759 {
760     /* System generated locals */
761     integer a_dim1, a_offset, i__1, i__2, i__3, i__4, i__5;
762     doublereal d__1, d__2, d__3, d__4;
763     doublecomplex z__1, z__2, z__3, z__4;
764
765     /* Local variables */
766     integer jinc;
767     doublereal xbnd;
768     integer imax;
769     doublereal tmax;
770     doublecomplex tjjs;
771     doublereal xmax, grow;
772     integer i__, j;
773     extern /* Subroutine */ int dscal_(integer *, doublereal *, doublereal *, 
774             integer *);
775     extern logical lsame_(char *, char *);
776     doublereal tscal;
777     doublecomplex uscal;
778     integer jlast;
779     doublecomplex csumj;
780     extern /* Double Complex */ VOID zdotc_(doublecomplex *, integer *, 
781             doublecomplex *, integer *, doublecomplex *, integer *);
782     logical upper;
783     extern /* Double Complex */ VOID zdotu_(doublecomplex *, integer *, 
784             doublecomplex *, integer *, doublecomplex *, integer *);
785     extern /* Subroutine */ int zaxpy_(integer *, doublecomplex *, 
786             doublecomplex *, integer *, doublecomplex *, integer *), ztrsv_(
787             char *, char *, char *, integer *, doublecomplex *, integer *, 
788             doublecomplex *, integer *), dlabad_(
789             doublereal *, doublereal *);
790     extern doublereal dlamch_(char *);
791     doublereal xj;
792     extern integer idamax_(integer *, doublereal *, integer *);
793     extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen), zdscal_(
794             integer *, doublereal *, doublecomplex *, integer *);
795     doublereal bignum;
796     extern integer izamax_(integer *, doublecomplex *, integer *);
797     extern /* Double Complex */ VOID zladiv_(doublecomplex *, doublecomplex *,
798              doublecomplex *);
799     logical notran;
800     integer jfirst;
801     extern doublereal dzasum_(integer *, doublecomplex *, integer *);
802     doublereal smlnum;
803     logical nounit;
804     doublereal rec, tjj;
805
806
807 /*  -- LAPACK auxiliary routine (version 3.8.0) -- */
808 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
809 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
810 /*     November 2017 */
811
812
813 /*  ===================================================================== */
814
815
816     /* Parameter adjustments */
817     a_dim1 = *lda;
818     a_offset = 1 + a_dim1 * 1;
819     a -= a_offset;
820     --x;
821     --cnorm;
822
823     /* Function Body */
824     *info = 0;
825     upper = lsame_(uplo, "U");
826     notran = lsame_(trans, "N");
827     nounit = lsame_(diag, "N");
828
829 /*     Test the input parameters. */
830
831     if (! upper && ! lsame_(uplo, "L")) {
832         *info = -1;
833     } else if (! notran && ! lsame_(trans, "T") && ! 
834             lsame_(trans, "C")) {
835         *info = -2;
836     } else if (! nounit && ! lsame_(diag, "U")) {
837         *info = -3;
838     } else if (! lsame_(normin, "Y") && ! lsame_(normin,
839              "N")) {
840         *info = -4;
841     } else if (*n < 0) {
842         *info = -5;
843     } else if (*lda < f2cmax(1,*n)) {
844         *info = -7;
845     }
846     if (*info != 0) {
847         i__1 = -(*info);
848         xerbla_("ZLATRS", &i__1, (ftnlen)6);
849         return 0;
850     }
851
852 /*     Quick return if possible */
853
854     if (*n == 0) {
855         return 0;
856     }
857
858 /*     Determine machine dependent parameters to control overflow. */
859
860     smlnum = dlamch_("Safe minimum");
861     bignum = 1. / smlnum;
862     dlabad_(&smlnum, &bignum);
863     smlnum /= dlamch_("Precision");
864     bignum = 1. / smlnum;
865     *scale = 1.;
866
867     if (lsame_(normin, "N")) {
868
869 /*        Compute the 1-norm of each column, not including the diagonal. */
870
871         if (upper) {
872
873 /*           A is upper triangular. */
874
875             i__1 = *n;
876             for (j = 1; j <= i__1; ++j) {
877                 i__2 = j - 1;
878                 cnorm[j] = dzasum_(&i__2, &a[j * a_dim1 + 1], &c__1);
879 /* L10: */
880             }
881         } else {
882
883 /*           A is lower triangular. */
884
885             i__1 = *n - 1;
886             for (j = 1; j <= i__1; ++j) {
887                 i__2 = *n - j;
888                 cnorm[j] = dzasum_(&i__2, &a[j + 1 + j * a_dim1], &c__1);
889 /* L20: */
890             }
891             cnorm[*n] = 0.;
892         }
893     }
894
895 /*     Scale the column norms by TSCAL if the maximum element in CNORM is */
896 /*     greater than BIGNUM/2. */
897
898     imax = idamax_(n, &cnorm[1], &c__1);
899     tmax = cnorm[imax];
900     if (tmax <= bignum * .5) {
901         tscal = 1.;
902     } else {
903         tscal = .5 / (smlnum * tmax);
904         dscal_(n, &tscal, &cnorm[1], &c__1);
905     }
906
907 /*     Compute a bound on the computed solution vector to see if the */
908 /*     Level 2 BLAS routine ZTRSV can be used. */
909
910     xmax = 0.;
911     i__1 = *n;
912     for (j = 1; j <= i__1; ++j) {
913 /* Computing MAX */
914         i__2 = j;
915         d__3 = xmax, d__4 = (d__1 = x[i__2].r / 2., abs(d__1)) + (d__2 = 
916                 d_imag(&x[j]) / 2., abs(d__2));
917         xmax = f2cmax(d__3,d__4);
918 /* L30: */
919     }
920     xbnd = xmax;
921
922     if (notran) {
923
924 /*        Compute the growth in A * x = b. */
925
926         if (upper) {
927             jfirst = *n;
928             jlast = 1;
929             jinc = -1;
930         } else {
931             jfirst = 1;
932             jlast = *n;
933             jinc = 1;
934         }
935
936         if (tscal != 1.) {
937             grow = 0.;
938             goto L60;
939         }
940
941         if (nounit) {
942
943 /*           A is non-unit triangular. */
944
945 /*           Compute GROW = 1/G(j) and XBND = 1/M(j). */
946 /*           Initially, G(0) = f2cmax{x(i), i=1,...,n}. */
947
948             grow = .5 / f2cmax(xbnd,smlnum);
949             xbnd = grow;
950             i__1 = jlast;
951             i__2 = jinc;
952             for (j = jfirst; i__2 < 0 ? j >= i__1 : j <= i__1; j += i__2) {
953
954 /*              Exit the loop if the growth factor is too small. */
955
956                 if (grow <= smlnum) {
957                     goto L60;
958                 }
959
960                 i__3 = j + j * a_dim1;
961                 tjjs.r = a[i__3].r, tjjs.i = a[i__3].i;
962                 tjj = (d__1 = tjjs.r, abs(d__1)) + (d__2 = d_imag(&tjjs), abs(
963                         d__2));
964
965                 if (tjj >= smlnum) {
966
967 /*                 M(j) = G(j-1) / abs(A(j,j)) */
968
969 /* Computing MIN */
970                     d__1 = xbnd, d__2 = f2cmin(1.,tjj) * grow;
971                     xbnd = f2cmin(d__1,d__2);
972                 } else {
973
974 /*                 M(j) could overflow, set XBND to 0. */
975
976                     xbnd = 0.;
977                 }
978
979                 if (tjj + cnorm[j] >= smlnum) {
980
981 /*                 G(j) = G(j-1)*( 1 + CNORM(j) / abs(A(j,j)) ) */
982
983                     grow *= tjj / (tjj + cnorm[j]);
984                 } else {
985
986 /*                 G(j) could overflow, set GROW to 0. */
987
988                     grow = 0.;
989                 }
990 /* L40: */
991             }
992             grow = xbnd;
993         } else {
994
995 /*           A is unit triangular. */
996
997 /*           Compute GROW = 1/G(j), where G(0) = f2cmax{x(i), i=1,...,n}. */
998
999 /* Computing MIN */
1000             d__1 = 1., d__2 = .5 / f2cmax(xbnd,smlnum);
1001             grow = f2cmin(d__1,d__2);
1002             i__2 = jlast;
1003             i__1 = jinc;
1004             for (j = jfirst; i__1 < 0 ? j >= i__2 : j <= i__2; j += i__1) {
1005
1006 /*              Exit the loop if the growth factor is too small. */
1007
1008                 if (grow <= smlnum) {
1009                     goto L60;
1010                 }
1011
1012 /*              G(j) = G(j-1)*( 1 + CNORM(j) ) */
1013
1014                 grow *= 1. / (cnorm[j] + 1.);
1015 /* L50: */
1016             }
1017         }
1018 L60:
1019
1020         ;
1021     } else {
1022
1023 /*        Compute the growth in A**T * x = b  or  A**H * x = b. */
1024
1025         if (upper) {
1026             jfirst = 1;
1027             jlast = *n;
1028             jinc = 1;
1029         } else {
1030             jfirst = *n;
1031             jlast = 1;
1032             jinc = -1;
1033         }
1034
1035         if (tscal != 1.) {
1036             grow = 0.;
1037             goto L90;
1038         }
1039
1040         if (nounit) {
1041
1042 /*           A is non-unit triangular. */
1043
1044 /*           Compute GROW = 1/G(j) and XBND = 1/M(j). */
1045 /*           Initially, M(0) = f2cmax{x(i), i=1,...,n}. */
1046
1047             grow = .5 / f2cmax(xbnd,smlnum);
1048             xbnd = grow;
1049             i__1 = jlast;
1050             i__2 = jinc;
1051             for (j = jfirst; i__2 < 0 ? j >= i__1 : j <= i__1; j += i__2) {
1052
1053 /*              Exit the loop if the growth factor is too small. */
1054
1055                 if (grow <= smlnum) {
1056                     goto L90;
1057                 }
1058
1059 /*              G(j) = f2cmax( G(j-1), M(j-1)*( 1 + CNORM(j) ) ) */
1060
1061                 xj = cnorm[j] + 1.;
1062 /* Computing MIN */
1063                 d__1 = grow, d__2 = xbnd / xj;
1064                 grow = f2cmin(d__1,d__2);
1065
1066                 i__3 = j + j * a_dim1;
1067                 tjjs.r = a[i__3].r, tjjs.i = a[i__3].i;
1068                 tjj = (d__1 = tjjs.r, abs(d__1)) + (d__2 = d_imag(&tjjs), abs(
1069                         d__2));
1070
1071                 if (tjj >= smlnum) {
1072
1073 /*                 M(j) = M(j-1)*( 1 + CNORM(j) ) / abs(A(j,j)) */
1074
1075                     if (xj > tjj) {
1076                         xbnd *= tjj / xj;
1077                     }
1078                 } else {
1079
1080 /*                 M(j) could overflow, set XBND to 0. */
1081
1082                     xbnd = 0.;
1083                 }
1084 /* L70: */
1085             }
1086             grow = f2cmin(grow,xbnd);
1087         } else {
1088
1089 /*           A is unit triangular. */
1090
1091 /*           Compute GROW = 1/G(j), where G(0) = f2cmax{x(i), i=1,...,n}. */
1092
1093 /* Computing MIN */
1094             d__1 = 1., d__2 = .5 / f2cmax(xbnd,smlnum);
1095             grow = f2cmin(d__1,d__2);
1096             i__2 = jlast;
1097             i__1 = jinc;
1098             for (j = jfirst; i__1 < 0 ? j >= i__2 : j <= i__2; j += i__1) {
1099
1100 /*              Exit the loop if the growth factor is too small. */
1101
1102                 if (grow <= smlnum) {
1103                     goto L90;
1104                 }
1105
1106 /*              G(j) = ( 1 + CNORM(j) )*G(j-1) */
1107
1108                 xj = cnorm[j] + 1.;
1109                 grow /= xj;
1110 /* L80: */
1111             }
1112         }
1113 L90:
1114         ;
1115     }
1116
1117     if (grow * tscal > smlnum) {
1118
1119 /*        Use the Level 2 BLAS solve if the reciprocal of the bound on */
1120 /*        elements of X is not too small. */
1121
1122         ztrsv_(uplo, trans, diag, n, &a[a_offset], lda, &x[1], &c__1);
1123     } else {
1124
1125 /*        Use a Level 1 BLAS solve, scaling intermediate results. */
1126
1127         if (xmax > bignum * .5) {
1128
1129 /*           Scale X so that its components are less than or equal to */
1130 /*           BIGNUM in absolute value. */
1131
1132             *scale = bignum * .5 / xmax;
1133             zdscal_(n, scale, &x[1], &c__1);
1134             xmax = bignum;
1135         } else {
1136             xmax *= 2.;
1137         }
1138
1139         if (notran) {
1140
1141 /*           Solve A * x = b */
1142
1143             i__1 = jlast;
1144             i__2 = jinc;
1145             for (j = jfirst; i__2 < 0 ? j >= i__1 : j <= i__1; j += i__2) {
1146
1147 /*              Compute x(j) = b(j) / A(j,j), scaling x if necessary. */
1148
1149                 i__3 = j;
1150                 xj = (d__1 = x[i__3].r, abs(d__1)) + (d__2 = d_imag(&x[j]), 
1151                         abs(d__2));
1152                 if (nounit) {
1153                     i__3 = j + j * a_dim1;
1154                     z__1.r = tscal * a[i__3].r, z__1.i = tscal * a[i__3].i;
1155                     tjjs.r = z__1.r, tjjs.i = z__1.i;
1156                 } else {
1157                     tjjs.r = tscal, tjjs.i = 0.;
1158                     if (tscal == 1.) {
1159                         goto L110;
1160                     }
1161                 }
1162                 tjj = (d__1 = tjjs.r, abs(d__1)) + (d__2 = d_imag(&tjjs), abs(
1163                         d__2));
1164                 if (tjj > smlnum) {
1165
1166 /*                    abs(A(j,j)) > SMLNUM: */
1167
1168                     if (tjj < 1.) {
1169                         if (xj > tjj * bignum) {
1170
1171 /*                          Scale x by 1/b(j). */
1172
1173                             rec = 1. / xj;
1174                             zdscal_(n, &rec, &x[1], &c__1);
1175                             *scale *= rec;
1176                             xmax *= rec;
1177                         }
1178                     }
1179                     i__3 = j;
1180                     zladiv_(&z__1, &x[j], &tjjs);
1181                     x[i__3].r = z__1.r, x[i__3].i = z__1.i;
1182                     i__3 = j;
1183                     xj = (d__1 = x[i__3].r, abs(d__1)) + (d__2 = d_imag(&x[j])
1184                             , abs(d__2));
1185                 } else if (tjj > 0.) {
1186
1187 /*                    0 < abs(A(j,j)) <= SMLNUM: */
1188
1189                     if (xj > tjj * bignum) {
1190
1191 /*                       Scale x by (1/abs(x(j)))*abs(A(j,j))*BIGNUM */
1192 /*                       to avoid overflow when dividing by A(j,j). */
1193
1194                         rec = tjj * bignum / xj;
1195                         if (cnorm[j] > 1.) {
1196
1197 /*                          Scale by 1/CNORM(j) to avoid overflow when */
1198 /*                          multiplying x(j) times column j. */
1199
1200                             rec /= cnorm[j];
1201                         }
1202                         zdscal_(n, &rec, &x[1], &c__1);
1203                         *scale *= rec;
1204                         xmax *= rec;
1205                     }
1206                     i__3 = j;
1207                     zladiv_(&z__1, &x[j], &tjjs);
1208                     x[i__3].r = z__1.r, x[i__3].i = z__1.i;
1209                     i__3 = j;
1210                     xj = (d__1 = x[i__3].r, abs(d__1)) + (d__2 = d_imag(&x[j])
1211                             , abs(d__2));
1212                 } else {
1213
1214 /*                    A(j,j) = 0:  Set x(1:n) = 0, x(j) = 1, and */
1215 /*                    scale = 0, and compute a solution to A*x = 0. */
1216
1217                     i__3 = *n;
1218                     for (i__ = 1; i__ <= i__3; ++i__) {
1219                         i__4 = i__;
1220                         x[i__4].r = 0., x[i__4].i = 0.;
1221 /* L100: */
1222                     }
1223                     i__3 = j;
1224                     x[i__3].r = 1., x[i__3].i = 0.;
1225                     xj = 1.;
1226                     *scale = 0.;
1227                     xmax = 0.;
1228                 }
1229 L110:
1230
1231 /*              Scale x if necessary to avoid overflow when adding a */
1232 /*              multiple of column j of A. */
1233
1234                 if (xj > 1.) {
1235                     rec = 1. / xj;
1236                     if (cnorm[j] > (bignum - xmax) * rec) {
1237
1238 /*                    Scale x by 1/(2*abs(x(j))). */
1239
1240                         rec *= .5;
1241                         zdscal_(n, &rec, &x[1], &c__1);
1242                         *scale *= rec;
1243                     }
1244                 } else if (xj * cnorm[j] > bignum - xmax) {
1245
1246 /*                 Scale x by 1/2. */
1247
1248                     zdscal_(n, &c_b36, &x[1], &c__1);
1249                     *scale *= .5;
1250                 }
1251
1252                 if (upper) {
1253                     if (j > 1) {
1254
1255 /*                    Compute the update */
1256 /*                       x(1:j-1) := x(1:j-1) - x(j) * A(1:j-1,j) */
1257
1258                         i__3 = j - 1;
1259                         i__4 = j;
1260                         z__2.r = -x[i__4].r, z__2.i = -x[i__4].i;
1261                         z__1.r = tscal * z__2.r, z__1.i = tscal * z__2.i;
1262                         zaxpy_(&i__3, &z__1, &a[j * a_dim1 + 1], &c__1, &x[1],
1263                                  &c__1);
1264                         i__3 = j - 1;
1265                         i__ = izamax_(&i__3, &x[1], &c__1);
1266                         i__3 = i__;
1267                         xmax = (d__1 = x[i__3].r, abs(d__1)) + (d__2 = d_imag(
1268                                 &x[i__]), abs(d__2));
1269                     }
1270                 } else {
1271                     if (j < *n) {
1272
1273 /*                    Compute the update */
1274 /*                       x(j+1:n) := x(j+1:n) - x(j) * A(j+1:n,j) */
1275
1276                         i__3 = *n - j;
1277                         i__4 = j;
1278                         z__2.r = -x[i__4].r, z__2.i = -x[i__4].i;
1279                         z__1.r = tscal * z__2.r, z__1.i = tscal * z__2.i;
1280                         zaxpy_(&i__3, &z__1, &a[j + 1 + j * a_dim1], &c__1, &
1281                                 x[j + 1], &c__1);
1282                         i__3 = *n - j;
1283                         i__ = j + izamax_(&i__3, &x[j + 1], &c__1);
1284                         i__3 = i__;
1285                         xmax = (d__1 = x[i__3].r, abs(d__1)) + (d__2 = d_imag(
1286                                 &x[i__]), abs(d__2));
1287                     }
1288                 }
1289 /* L120: */
1290             }
1291
1292         } else if (lsame_(trans, "T")) {
1293
1294 /*           Solve A**T * x = b */
1295
1296             i__2 = jlast;
1297             i__1 = jinc;
1298             for (j = jfirst; i__1 < 0 ? j >= i__2 : j <= i__2; j += i__1) {
1299
1300 /*              Compute x(j) = b(j) - sum A(k,j)*x(k). */
1301 /*                                    k<>j */
1302
1303                 i__3 = j;
1304                 xj = (d__1 = x[i__3].r, abs(d__1)) + (d__2 = d_imag(&x[j]), 
1305                         abs(d__2));
1306                 uscal.r = tscal, uscal.i = 0.;
1307                 rec = 1. / f2cmax(xmax,1.);
1308                 if (cnorm[j] > (bignum - xj) * rec) {
1309
1310 /*                 If x(j) could overflow, scale x by 1/(2*XMAX). */
1311
1312                     rec *= .5;
1313                     if (nounit) {
1314                         i__3 = j + j * a_dim1;
1315                         z__1.r = tscal * a[i__3].r, z__1.i = tscal * a[i__3]
1316                                 .i;
1317                         tjjs.r = z__1.r, tjjs.i = z__1.i;
1318                     } else {
1319                         tjjs.r = tscal, tjjs.i = 0.;
1320                     }
1321                     tjj = (d__1 = tjjs.r, abs(d__1)) + (d__2 = d_imag(&tjjs), 
1322                             abs(d__2));
1323                     if (tjj > 1.) {
1324
1325 /*                       Divide by A(j,j) when scaling x if A(j,j) > 1. */
1326
1327 /* Computing MIN */
1328                         d__1 = 1., d__2 = rec * tjj;
1329                         rec = f2cmin(d__1,d__2);
1330                         zladiv_(&z__1, &uscal, &tjjs);
1331                         uscal.r = z__1.r, uscal.i = z__1.i;
1332                     }
1333                     if (rec < 1.) {
1334                         zdscal_(n, &rec, &x[1], &c__1);
1335                         *scale *= rec;
1336                         xmax *= rec;
1337                     }
1338                 }
1339
1340                 csumj.r = 0., csumj.i = 0.;
1341                 if (uscal.r == 1. && uscal.i == 0.) {
1342
1343 /*                 If the scaling needed for A in the dot product is 1, */
1344 /*                 call ZDOTU to perform the dot product. */
1345
1346                     if (upper) {
1347                         i__3 = j - 1;
1348                         zdotu_(&z__1, &i__3, &a[j * a_dim1 + 1], &c__1, &x[1],
1349                                  &c__1);
1350                         csumj.r = z__1.r, csumj.i = z__1.i;
1351                     } else if (j < *n) {
1352                         i__3 = *n - j;
1353                         zdotu_(&z__1, &i__3, &a[j + 1 + j * a_dim1], &c__1, &
1354                                 x[j + 1], &c__1);
1355                         csumj.r = z__1.r, csumj.i = z__1.i;
1356                     }
1357                 } else {
1358
1359 /*                 Otherwise, use in-line code for the dot product. */
1360
1361                     if (upper) {
1362                         i__3 = j - 1;
1363                         for (i__ = 1; i__ <= i__3; ++i__) {
1364                             i__4 = i__ + j * a_dim1;
1365                             z__3.r = a[i__4].r * uscal.r - a[i__4].i * 
1366                                     uscal.i, z__3.i = a[i__4].r * uscal.i + a[
1367                                     i__4].i * uscal.r;
1368                             i__5 = i__;
1369                             z__2.r = z__3.r * x[i__5].r - z__3.i * x[i__5].i, 
1370                                     z__2.i = z__3.r * x[i__5].i + z__3.i * x[
1371                                     i__5].r;
1372                             z__1.r = csumj.r + z__2.r, z__1.i = csumj.i + 
1373                                     z__2.i;
1374                             csumj.r = z__1.r, csumj.i = z__1.i;
1375 /* L130: */
1376                         }
1377                     } else if (j < *n) {
1378                         i__3 = *n;
1379                         for (i__ = j + 1; i__ <= i__3; ++i__) {
1380                             i__4 = i__ + j * a_dim1;
1381                             z__3.r = a[i__4].r * uscal.r - a[i__4].i * 
1382                                     uscal.i, z__3.i = a[i__4].r * uscal.i + a[
1383                                     i__4].i * uscal.r;
1384                             i__5 = i__;
1385                             z__2.r = z__3.r * x[i__5].r - z__3.i * x[i__5].i, 
1386                                     z__2.i = z__3.r * x[i__5].i + z__3.i * x[
1387                                     i__5].r;
1388                             z__1.r = csumj.r + z__2.r, z__1.i = csumj.i + 
1389                                     z__2.i;
1390                             csumj.r = z__1.r, csumj.i = z__1.i;
1391 /* L140: */
1392                         }
1393                     }
1394                 }
1395
1396                 z__1.r = tscal, z__1.i = 0.;
1397                 if (uscal.r == z__1.r && uscal.i == z__1.i) {
1398
1399 /*                 Compute x(j) := ( x(j) - CSUMJ ) / A(j,j) if 1/A(j,j) */
1400 /*                 was not used to scale the dotproduct. */
1401
1402                     i__3 = j;
1403                     i__4 = j;
1404                     z__1.r = x[i__4].r - csumj.r, z__1.i = x[i__4].i - 
1405                             csumj.i;
1406                     x[i__3].r = z__1.r, x[i__3].i = z__1.i;
1407                     i__3 = j;
1408                     xj = (d__1 = x[i__3].r, abs(d__1)) + (d__2 = d_imag(&x[j])
1409                             , abs(d__2));
1410                     if (nounit) {
1411                         i__3 = j + j * a_dim1;
1412                         z__1.r = tscal * a[i__3].r, z__1.i = tscal * a[i__3]
1413                                 .i;
1414                         tjjs.r = z__1.r, tjjs.i = z__1.i;
1415                     } else {
1416                         tjjs.r = tscal, tjjs.i = 0.;
1417                         if (tscal == 1.) {
1418                             goto L160;
1419                         }
1420                     }
1421
1422 /*                    Compute x(j) = x(j) / A(j,j), scaling if necessary. */
1423
1424                     tjj = (d__1 = tjjs.r, abs(d__1)) + (d__2 = d_imag(&tjjs), 
1425                             abs(d__2));
1426                     if (tjj > smlnum) {
1427
1428 /*                       abs(A(j,j)) > SMLNUM: */
1429
1430                         if (tjj < 1.) {
1431                             if (xj > tjj * bignum) {
1432
1433 /*                             Scale X by 1/abs(x(j)). */
1434
1435                                 rec = 1. / xj;
1436                                 zdscal_(n, &rec, &x[1], &c__1);
1437                                 *scale *= rec;
1438                                 xmax *= rec;
1439                             }
1440                         }
1441                         i__3 = j;
1442                         zladiv_(&z__1, &x[j], &tjjs);
1443                         x[i__3].r = z__1.r, x[i__3].i = z__1.i;
1444                     } else if (tjj > 0.) {
1445
1446 /*                       0 < abs(A(j,j)) <= SMLNUM: */
1447
1448                         if (xj > tjj * bignum) {
1449
1450 /*                          Scale x by (1/abs(x(j)))*abs(A(j,j))*BIGNUM. */
1451
1452                             rec = tjj * bignum / xj;
1453                             zdscal_(n, &rec, &x[1], &c__1);
1454                             *scale *= rec;
1455                             xmax *= rec;
1456                         }
1457                         i__3 = j;
1458                         zladiv_(&z__1, &x[j], &tjjs);
1459                         x[i__3].r = z__1.r, x[i__3].i = z__1.i;
1460                     } else {
1461
1462 /*                       A(j,j) = 0:  Set x(1:n) = 0, x(j) = 1, and */
1463 /*                       scale = 0 and compute a solution to A**T *x = 0. */
1464
1465                         i__3 = *n;
1466                         for (i__ = 1; i__ <= i__3; ++i__) {
1467                             i__4 = i__;
1468                             x[i__4].r = 0., x[i__4].i = 0.;
1469 /* L150: */
1470                         }
1471                         i__3 = j;
1472                         x[i__3].r = 1., x[i__3].i = 0.;
1473                         *scale = 0.;
1474                         xmax = 0.;
1475                     }
1476 L160:
1477                     ;
1478                 } else {
1479
1480 /*                 Compute x(j) := x(j) / A(j,j) - CSUMJ if the dot */
1481 /*                 product has already been divided by 1/A(j,j). */
1482
1483                     i__3 = j;
1484                     zladiv_(&z__2, &x[j], &tjjs);
1485                     z__1.r = z__2.r - csumj.r, z__1.i = z__2.i - csumj.i;
1486                     x[i__3].r = z__1.r, x[i__3].i = z__1.i;
1487                 }
1488 /* Computing MAX */
1489                 i__3 = j;
1490                 d__3 = xmax, d__4 = (d__1 = x[i__3].r, abs(d__1)) + (d__2 = 
1491                         d_imag(&x[j]), abs(d__2));
1492                 xmax = f2cmax(d__3,d__4);
1493 /* L170: */
1494             }
1495
1496         } else {
1497
1498 /*           Solve A**H * x = b */
1499
1500             i__1 = jlast;
1501             i__2 = jinc;
1502             for (j = jfirst; i__2 < 0 ? j >= i__1 : j <= i__1; j += i__2) {
1503
1504 /*              Compute x(j) = b(j) - sum A(k,j)*x(k). */
1505 /*                                    k<>j */
1506
1507                 i__3 = j;
1508                 xj = (d__1 = x[i__3].r, abs(d__1)) + (d__2 = d_imag(&x[j]), 
1509                         abs(d__2));
1510                 uscal.r = tscal, uscal.i = 0.;
1511                 rec = 1. / f2cmax(xmax,1.);
1512                 if (cnorm[j] > (bignum - xj) * rec) {
1513
1514 /*                 If x(j) could overflow, scale x by 1/(2*XMAX). */
1515
1516                     rec *= .5;
1517                     if (nounit) {
1518                         d_cnjg(&z__2, &a[j + j * a_dim1]);
1519                         z__1.r = tscal * z__2.r, z__1.i = tscal * z__2.i;
1520                         tjjs.r = z__1.r, tjjs.i = z__1.i;
1521                     } else {
1522                         tjjs.r = tscal, tjjs.i = 0.;
1523                     }
1524                     tjj = (d__1 = tjjs.r, abs(d__1)) + (d__2 = d_imag(&tjjs), 
1525                             abs(d__2));
1526                     if (tjj > 1.) {
1527
1528 /*                       Divide by A(j,j) when scaling x if A(j,j) > 1. */
1529
1530 /* Computing MIN */
1531                         d__1 = 1., d__2 = rec * tjj;
1532                         rec = f2cmin(d__1,d__2);
1533                         zladiv_(&z__1, &uscal, &tjjs);
1534                         uscal.r = z__1.r, uscal.i = z__1.i;
1535                     }
1536                     if (rec < 1.) {
1537                         zdscal_(n, &rec, &x[1], &c__1);
1538                         *scale *= rec;
1539                         xmax *= rec;
1540                     }
1541                 }
1542
1543                 csumj.r = 0., csumj.i = 0.;
1544                 if (uscal.r == 1. && uscal.i == 0.) {
1545
1546 /*                 If the scaling needed for A in the dot product is 1, */
1547 /*                 call ZDOTC to perform the dot product. */
1548
1549                     if (upper) {
1550                         i__3 = j - 1;
1551                         zdotc_(&z__1, &i__3, &a[j * a_dim1 + 1], &c__1, &x[1],
1552                                  &c__1);
1553                         csumj.r = z__1.r, csumj.i = z__1.i;
1554                     } else if (j < *n) {
1555                         i__3 = *n - j;
1556                         zdotc_(&z__1, &i__3, &a[j + 1 + j * a_dim1], &c__1, &
1557                                 x[j + 1], &c__1);
1558                         csumj.r = z__1.r, csumj.i = z__1.i;
1559                     }
1560                 } else {
1561
1562 /*                 Otherwise, use in-line code for the dot product. */
1563
1564                     if (upper) {
1565                         i__3 = j - 1;
1566                         for (i__ = 1; i__ <= i__3; ++i__) {
1567                             d_cnjg(&z__4, &a[i__ + j * a_dim1]);
1568                             z__3.r = z__4.r * uscal.r - z__4.i * uscal.i, 
1569                                     z__3.i = z__4.r * uscal.i + z__4.i * 
1570                                     uscal.r;
1571                             i__4 = i__;
1572                             z__2.r = z__3.r * x[i__4].r - z__3.i * x[i__4].i, 
1573                                     z__2.i = z__3.r * x[i__4].i + z__3.i * x[
1574                                     i__4].r;
1575                             z__1.r = csumj.r + z__2.r, z__1.i = csumj.i + 
1576                                     z__2.i;
1577                             csumj.r = z__1.r, csumj.i = z__1.i;
1578 /* L180: */
1579                         }
1580                     } else if (j < *n) {
1581                         i__3 = *n;
1582                         for (i__ = j + 1; i__ <= i__3; ++i__) {
1583                             d_cnjg(&z__4, &a[i__ + j * a_dim1]);
1584                             z__3.r = z__4.r * uscal.r - z__4.i * uscal.i, 
1585                                     z__3.i = z__4.r * uscal.i + z__4.i * 
1586                                     uscal.r;
1587                             i__4 = i__;
1588                             z__2.r = z__3.r * x[i__4].r - z__3.i * x[i__4].i, 
1589                                     z__2.i = z__3.r * x[i__4].i + z__3.i * x[
1590                                     i__4].r;
1591                             z__1.r = csumj.r + z__2.r, z__1.i = csumj.i + 
1592                                     z__2.i;
1593                             csumj.r = z__1.r, csumj.i = z__1.i;
1594 /* L190: */
1595                         }
1596                     }
1597                 }
1598
1599                 z__1.r = tscal, z__1.i = 0.;
1600                 if (uscal.r == z__1.r && uscal.i == z__1.i) {
1601
1602 /*                 Compute x(j) := ( x(j) - CSUMJ ) / A(j,j) if 1/A(j,j) */
1603 /*                 was not used to scale the dotproduct. */
1604
1605                     i__3 = j;
1606                     i__4 = j;
1607                     z__1.r = x[i__4].r - csumj.r, z__1.i = x[i__4].i - 
1608                             csumj.i;
1609                     x[i__3].r = z__1.r, x[i__3].i = z__1.i;
1610                     i__3 = j;
1611                     xj = (d__1 = x[i__3].r, abs(d__1)) + (d__2 = d_imag(&x[j])
1612                             , abs(d__2));
1613                     if (nounit) {
1614                         d_cnjg(&z__2, &a[j + j * a_dim1]);
1615                         z__1.r = tscal * z__2.r, z__1.i = tscal * z__2.i;
1616                         tjjs.r = z__1.r, tjjs.i = z__1.i;
1617                     } else {
1618                         tjjs.r = tscal, tjjs.i = 0.;
1619                         if (tscal == 1.) {
1620                             goto L210;
1621                         }
1622                     }
1623
1624 /*                    Compute x(j) = x(j) / A(j,j), scaling if necessary. */
1625
1626                     tjj = (d__1 = tjjs.r, abs(d__1)) + (d__2 = d_imag(&tjjs), 
1627                             abs(d__2));
1628                     if (tjj > smlnum) {
1629
1630 /*                       abs(A(j,j)) > SMLNUM: */
1631
1632                         if (tjj < 1.) {
1633                             if (xj > tjj * bignum) {
1634
1635 /*                             Scale X by 1/abs(x(j)). */
1636
1637                                 rec = 1. / xj;
1638                                 zdscal_(n, &rec, &x[1], &c__1);
1639                                 *scale *= rec;
1640                                 xmax *= rec;
1641                             }
1642                         }
1643                         i__3 = j;
1644                         zladiv_(&z__1, &x[j], &tjjs);
1645                         x[i__3].r = z__1.r, x[i__3].i = z__1.i;
1646                     } else if (tjj > 0.) {
1647
1648 /*                       0 < abs(A(j,j)) <= SMLNUM: */
1649
1650                         if (xj > tjj * bignum) {
1651
1652 /*                          Scale x by (1/abs(x(j)))*abs(A(j,j))*BIGNUM. */
1653
1654                             rec = tjj * bignum / xj;
1655                             zdscal_(n, &rec, &x[1], &c__1);
1656                             *scale *= rec;
1657                             xmax *= rec;
1658                         }
1659                         i__3 = j;
1660                         zladiv_(&z__1, &x[j], &tjjs);
1661                         x[i__3].r = z__1.r, x[i__3].i = z__1.i;
1662                     } else {
1663
1664 /*                       A(j,j) = 0:  Set x(1:n) = 0, x(j) = 1, and */
1665 /*                       scale = 0 and compute a solution to A**H *x = 0. */
1666
1667                         i__3 = *n;
1668                         for (i__ = 1; i__ <= i__3; ++i__) {
1669                             i__4 = i__;
1670                             x[i__4].r = 0., x[i__4].i = 0.;
1671 /* L200: */
1672                         }
1673                         i__3 = j;
1674                         x[i__3].r = 1., x[i__3].i = 0.;
1675                         *scale = 0.;
1676                         xmax = 0.;
1677                     }
1678 L210:
1679                     ;
1680                 } else {
1681
1682 /*                 Compute x(j) := x(j) / A(j,j) - CSUMJ if the dot */
1683 /*                 product has already been divided by 1/A(j,j). */
1684
1685                     i__3 = j;
1686                     zladiv_(&z__2, &x[j], &tjjs);
1687                     z__1.r = z__2.r - csumj.r, z__1.i = z__2.i - csumj.i;
1688                     x[i__3].r = z__1.r, x[i__3].i = z__1.i;
1689                 }
1690 /* Computing MAX */
1691                 i__3 = j;
1692                 d__3 = xmax, d__4 = (d__1 = x[i__3].r, abs(d__1)) + (d__2 = 
1693                         d_imag(&x[j]), abs(d__2));
1694                 xmax = f2cmax(d__3,d__4);
1695 /* L220: */
1696             }
1697         }
1698         *scale /= tscal;
1699     }
1700
1701 /*     Scale the column norms by 1/TSCAL for return. */
1702
1703     if (tscal != 1.) {
1704         d__1 = 1. / tscal;
1705         dscal_(n, &d__1, &cnorm[1], &c__1);
1706     }
1707
1708     return 0;
1709
1710 /*     End of ZLATRS */
1711
1712 } /* zlatrs_ */
1713