C_LAPACK: Fixes to make it compile with MSVC (#3605)
[platform/upstream/openblas.git] / lapack-netlib / SRC / ssytf2.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
517 /* > \brief \b SSYTF2 computes the factorization of a real symmetric indefinite matrix, using the diagonal piv
518 oting method (unblocked algorithm). */
519
520 /*  =========== DOCUMENTATION =========== */
521
522 /* Online html documentation available at */
523 /*            http://www.netlib.org/lapack/explore-html/ */
524
525 /* > \htmlonly */
526 /* > Download SSYTF2 + dependencies */
527 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.tgz?format=tgz&filename=/lapack/lapack_routine/ssytf2.
528 f"> */
529 /* > [TGZ]</a> */
530 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.zip?format=zip&filename=/lapack/lapack_routine/ssytf2.
531 f"> */
532 /* > [ZIP]</a> */
533 /* > <a href="http://www.netlib.org/cgi-bin/netlibfiles.txt?format=txt&filename=/lapack/lapack_routine/ssytf2.
534 f"> */
535 /* > [TXT]</a> */
536 /* > \endhtmlonly */
537
538 /*  Definition: */
539 /*  =========== */
540
541 /*       SUBROUTINE SSYTF2( UPLO, N, A, LDA, IPIV, INFO ) */
542
543 /*       CHARACTER          UPLO */
544 /*       INTEGER            INFO, LDA, N */
545 /*       INTEGER            IPIV( * ) */
546 /*       REAL               A( LDA, * ) */
547
548
549 /* > \par Purpose: */
550 /*  ============= */
551 /* > */
552 /* > \verbatim */
553 /* > */
554 /* > SSYTF2 computes the factorization of a real symmetric matrix A using */
555 /* > the Bunch-Kaufman diagonal pivoting method: */
556 /* > */
557 /* >    A = U*D*U**T  or  A = L*D*L**T */
558 /* > */
559 /* > where U (or L) is a product of permutation and unit upper (lower) */
560 /* > triangular matrices, U**T is the transpose of U, and D is symmetric and */
561 /* > block diagonal with 1-by-1 and 2-by-2 diagonal blocks. */
562 /* > */
563 /* > This is the unblocked version of the algorithm, calling Level 2 BLAS. */
564 /* > \endverbatim */
565
566 /*  Arguments: */
567 /*  ========== */
568
569 /* > \param[in] UPLO */
570 /* > \verbatim */
571 /* >          UPLO is CHARACTER*1 */
572 /* >          Specifies whether the upper or lower triangular part of the */
573 /* >          symmetric matrix A is stored: */
574 /* >          = 'U':  Upper triangular */
575 /* >          = 'L':  Lower triangular */
576 /* > \endverbatim */
577 /* > */
578 /* > \param[in] N */
579 /* > \verbatim */
580 /* >          N is INTEGER */
581 /* >          The order of the matrix A.  N >= 0. */
582 /* > \endverbatim */
583 /* > */
584 /* > \param[in,out] A */
585 /* > \verbatim */
586 /* >          A is REAL array, dimension (LDA,N) */
587 /* >          On entry, the symmetric matrix A.  If UPLO = 'U', the leading */
588 /* >          n-by-n upper triangular part of A contains the upper */
589 /* >          triangular part of the matrix A, and the strictly lower */
590 /* >          triangular part of A is not referenced.  If UPLO = 'L', the */
591 /* >          leading n-by-n lower triangular part of A contains the lower */
592 /* >          triangular part of the matrix A, and the strictly upper */
593 /* >          triangular part of A is not referenced. */
594 /* > */
595 /* >          On exit, the block diagonal matrix D and the multipliers used */
596 /* >          to obtain the factor U or L (see below for further details). */
597 /* > \endverbatim */
598 /* > */
599 /* > \param[in] LDA */
600 /* > \verbatim */
601 /* >          LDA is INTEGER */
602 /* >          The leading dimension of the array A.  LDA >= f2cmax(1,N). */
603 /* > \endverbatim */
604 /* > */
605 /* > \param[out] IPIV */
606 /* > \verbatim */
607 /* >          IPIV is INTEGER array, dimension (N) */
608 /* >          Details of the interchanges and the block structure of D. */
609 /* > */
610 /* >          If UPLO = 'U': */
611 /* >             If IPIV(k) > 0, then rows and columns k and IPIV(k) were */
612 /* >             interchanged and D(k,k) is a 1-by-1 diagonal block. */
613 /* > */
614 /* >             If IPIV(k) = IPIV(k-1) < 0, then rows and columns */
615 /* >             k-1 and -IPIV(k) were interchanged and D(k-1:k,k-1:k) */
616 /* >             is a 2-by-2 diagonal block. */
617 /* > */
618 /* >          If UPLO = 'L': */
619 /* >             If IPIV(k) > 0, then rows and columns k and IPIV(k) were */
620 /* >             interchanged and D(k,k) is a 1-by-1 diagonal block. */
621 /* > */
622 /* >             If IPIV(k) = IPIV(k+1) < 0, then rows and columns */
623 /* >             k+1 and -IPIV(k) were interchanged and D(k:k+1,k:k+1) */
624 /* >             is a 2-by-2 diagonal block. */
625 /* > \endverbatim */
626 /* > */
627 /* > \param[out] INFO */
628 /* > \verbatim */
629 /* >          INFO is INTEGER */
630 /* >          = 0: successful exit */
631 /* >          < 0: if INFO = -k, the k-th argument had an illegal value */
632 /* >          > 0: if INFO = k, D(k,k) is exactly zero.  The factorization */
633 /* >               has been completed, but the block diagonal matrix D is */
634 /* >               exactly singular, and division by zero will occur if it */
635 /* >               is used to solve a system of equations. */
636 /* > \endverbatim */
637
638 /*  Authors: */
639 /*  ======== */
640
641 /* > \author Univ. of Tennessee */
642 /* > \author Univ. of California Berkeley */
643 /* > \author Univ. of Colorado Denver */
644 /* > \author NAG Ltd. */
645
646 /* > \date December 2016 */
647
648 /* > \ingroup realSYcomputational */
649
650 /* > \par Further Details: */
651 /*  ===================== */
652 /* > */
653 /* > \verbatim */
654 /* > */
655 /* >  If UPLO = 'U', then A = U*D*U**T, where */
656 /* >     U = P(n)*U(n)* ... *P(k)U(k)* ..., */
657 /* >  i.e., U is a product of terms P(k)*U(k), where k decreases from n to */
658 /* >  1 in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1 */
659 /* >  and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as */
660 /* >  defined by IPIV(k), and U(k) is a unit upper triangular matrix, such */
661 /* >  that if the diagonal block D(k) is of order s (s = 1 or 2), then */
662 /* > */
663 /* >             (   I    v    0   )   k-s */
664 /* >     U(k) =  (   0    I    0   )   s */
665 /* >             (   0    0    I   )   n-k */
666 /* >                k-s   s   n-k */
667 /* > */
668 /* >  If s = 1, D(k) overwrites A(k,k), and v overwrites A(1:k-1,k). */
669 /* >  If s = 2, the upper triangle of D(k) overwrites A(k-1,k-1), A(k-1,k), */
670 /* >  and A(k,k), and v overwrites A(1:k-2,k-1:k). */
671 /* > */
672 /* >  If UPLO = 'L', then A = L*D*L**T, where */
673 /* >     L = P(1)*L(1)* ... *P(k)*L(k)* ..., */
674 /* >  i.e., L is a product of terms P(k)*L(k), where k increases from 1 to */
675 /* >  n in steps of 1 or 2, and D is a block diagonal matrix with 1-by-1 */
676 /* >  and 2-by-2 diagonal blocks D(k).  P(k) is a permutation matrix as */
677 /* >  defined by IPIV(k), and L(k) is a unit lower triangular matrix, such */
678 /* >  that if the diagonal block D(k) is of order s (s = 1 or 2), then */
679 /* > */
680 /* >             (   I    0     0   )  k-1 */
681 /* >     L(k) =  (   0    I     0   )  s */
682 /* >             (   0    v     I   )  n-k-s+1 */
683 /* >                k-1   s  n-k-s+1 */
684 /* > */
685 /* >  If s = 1, D(k) overwrites A(k,k), and v overwrites A(k+1:n,k). */
686 /* >  If s = 2, the lower triangle of D(k) overwrites A(k,k), A(k+1,k), */
687 /* >  and A(k+1,k+1), and v overwrites A(k+2:n,k:k+1). */
688 /* > \endverbatim */
689
690 /* > \par Contributors: */
691 /*  ================== */
692 /* > */
693 /* > \verbatim */
694 /* > */
695 /* >  09-29-06 - patch from */
696 /* >    Bobby Cheng, MathWorks */
697 /* > */
698 /* >    Replace l.204 and l.372 */
699 /* >         IF( MAX( ABSAKK, COLMAX ).EQ.ZERO ) THEN */
700 /* >    by */
701 /* >         IF( (MAX( ABSAKK, COLMAX ).EQ.ZERO) .OR. SISNAN(ABSAKK) ) THEN */
702 /* > */
703 /* >  01-01-96 - Based on modifications by */
704 /* >    J. Lewis, Boeing Computer Services Company */
705 /* >    A. Petitet, Computer Science Dept., Univ. of Tenn., Knoxville, USA */
706 /* >  1-96 - Based on modifications by J. Lewis, Boeing Computer Services */
707 /* >         Company */
708 /* > */
709 /* > \endverbatim */
710
711 /*  ===================================================================== */
712 /* Subroutine */ int ssytf2_(char *uplo, integer *n, real *a, integer *lda, 
713         integer *ipiv, integer *info)
714 {
715     /* System generated locals */
716     integer a_dim1, a_offset, i__1, i__2;
717     real r__1, r__2, r__3;
718
719     /* Local variables */
720     integer imax, jmax;
721     extern /* Subroutine */ int ssyr_(char *, integer *, real *, real *, 
722             integer *, real *, integer *);
723     integer i__, j, k;
724     real t, alpha;
725     extern logical lsame_(char *, char *);
726     extern /* Subroutine */ int sscal_(integer *, real *, real *, integer *);
727     integer kstep;
728     logical upper;
729     extern /* Subroutine */ int sswap_(integer *, real *, integer *, real *, 
730             integer *);
731     real r1, d11, d12, d21, d22;
732     integer kk, kp;
733     real absakk, wk;
734     extern /* Subroutine */ int xerbla_(char *, integer *, ftnlen);
735     extern integer isamax_(integer *, real *, integer *);
736     real colmax;
737     extern logical sisnan_(real *);
738     real rowmax, wkm1, wkp1;
739
740
741 /*  -- LAPACK computational routine (version 3.7.0) -- */
742 /*  -- LAPACK is a software package provided by Univ. of Tennessee,    -- */
743 /*  -- Univ. of California Berkeley, Univ. of Colorado Denver and NAG Ltd..-- */
744 /*     December 2016 */
745
746
747 /*  ===================================================================== */
748
749
750 /*     Test the input parameters. */
751
752     /* Parameter adjustments */
753     a_dim1 = *lda;
754     a_offset = 1 + a_dim1 * 1;
755     a -= a_offset;
756     --ipiv;
757
758     /* Function Body */
759     *info = 0;
760     upper = lsame_(uplo, "U");
761     if (! upper && ! lsame_(uplo, "L")) {
762         *info = -1;
763     } else if (*n < 0) {
764         *info = -2;
765     } else if (*lda < f2cmax(1,*n)) {
766         *info = -4;
767     }
768     if (*info != 0) {
769         i__1 = -(*info);
770         xerbla_("SSYTF2", &i__1, (ftnlen)6);
771         return 0;
772     }
773
774 /*     Initialize ALPHA for use in choosing pivot block size. */
775
776     alpha = (sqrt(17.f) + 1.f) / 8.f;
777
778     if (upper) {
779
780 /*        Factorize A as U*D*U**T using the upper triangle of A */
781
782 /*        K is the main loop index, decreasing from N to 1 in steps of */
783 /*        1 or 2 */
784
785         k = *n;
786 L10:
787
788 /*        If K < 1, exit from loop */
789
790         if (k < 1) {
791             goto L70;
792         }
793         kstep = 1;
794
795 /*        Determine rows and columns to be interchanged and whether */
796 /*        a 1-by-1 or 2-by-2 pivot block will be used */
797
798         absakk = (r__1 = a[k + k * a_dim1], abs(r__1));
799
800 /*        IMAX is the row-index of the largest off-diagonal element in */
801 /*        column K, and COLMAX is its absolute value. */
802 /*        Determine both COLMAX and IMAX. */
803
804         if (k > 1) {
805             i__1 = k - 1;
806             imax = isamax_(&i__1, &a[k * a_dim1 + 1], &c__1);
807             colmax = (r__1 = a[imax + k * a_dim1], abs(r__1));
808         } else {
809             colmax = 0.f;
810         }
811
812         if (f2cmax(absakk,colmax) == 0.f || sisnan_(&absakk)) {
813
814 /*           Column K is zero or underflow, or contains a NaN: */
815 /*           set INFO and continue */
816
817             if (*info == 0) {
818                 *info = k;
819             }
820             kp = k;
821         } else {
822             if (absakk >= alpha * colmax) {
823
824 /*              no interchange, use 1-by-1 pivot block */
825
826                 kp = k;
827             } else {
828
829 /*              JMAX is the column-index of the largest off-diagonal */
830 /*              element in row IMAX, and ROWMAX is its absolute value */
831
832                 i__1 = k - imax;
833                 jmax = imax + isamax_(&i__1, &a[imax + (imax + 1) * a_dim1], 
834                         lda);
835                 rowmax = (r__1 = a[imax + jmax * a_dim1], abs(r__1));
836                 if (imax > 1) {
837                     i__1 = imax - 1;
838                     jmax = isamax_(&i__1, &a[imax * a_dim1 + 1], &c__1);
839 /* Computing MAX */
840                     r__2 = rowmax, r__3 = (r__1 = a[jmax + imax * a_dim1], 
841                             abs(r__1));
842                     rowmax = f2cmax(r__2,r__3);
843                 }
844
845                 if (absakk >= alpha * colmax * (colmax / rowmax)) {
846
847 /*                 no interchange, use 1-by-1 pivot block */
848
849                     kp = k;
850                 } else if ((r__1 = a[imax + imax * a_dim1], abs(r__1)) >= 
851                         alpha * rowmax) {
852
853 /*                 interchange rows and columns K and IMAX, use 1-by-1 */
854 /*                 pivot block */
855
856                     kp = imax;
857                 } else {
858
859 /*                 interchange rows and columns K-1 and IMAX, use 2-by-2 */
860 /*                 pivot block */
861
862                     kp = imax;
863                     kstep = 2;
864                 }
865             }
866
867             kk = k - kstep + 1;
868             if (kp != kk) {
869
870 /*              Interchange rows and columns KK and KP in the leading */
871 /*              submatrix A(1:k,1:k) */
872
873                 i__1 = kp - 1;
874                 sswap_(&i__1, &a[kk * a_dim1 + 1], &c__1, &a[kp * a_dim1 + 1],
875                          &c__1);
876                 i__1 = kk - kp - 1;
877                 sswap_(&i__1, &a[kp + 1 + kk * a_dim1], &c__1, &a[kp + (kp + 
878                         1) * a_dim1], lda);
879                 t = a[kk + kk * a_dim1];
880                 a[kk + kk * a_dim1] = a[kp + kp * a_dim1];
881                 a[kp + kp * a_dim1] = t;
882                 if (kstep == 2) {
883                     t = a[k - 1 + k * a_dim1];
884                     a[k - 1 + k * a_dim1] = a[kp + k * a_dim1];
885                     a[kp + k * a_dim1] = t;
886                 }
887             }
888
889 /*           Update the leading submatrix */
890
891             if (kstep == 1) {
892
893 /*              1-by-1 pivot block D(k): column k now holds */
894
895 /*              W(k) = U(k)*D(k) */
896
897 /*              where U(k) is the k-th column of U */
898
899 /*              Perform a rank-1 update of A(1:k-1,1:k-1) as */
900
901 /*              A := A - U(k)*D(k)*U(k)**T = A - W(k)*1/D(k)*W(k)**T */
902
903                 r1 = 1.f / a[k + k * a_dim1];
904                 i__1 = k - 1;
905                 r__1 = -r1;
906                 ssyr_(uplo, &i__1, &r__1, &a[k * a_dim1 + 1], &c__1, &a[
907                         a_offset], lda);
908
909 /*              Store U(k) in column k */
910
911                 i__1 = k - 1;
912                 sscal_(&i__1, &r1, &a[k * a_dim1 + 1], &c__1);
913             } else {
914
915 /*              2-by-2 pivot block D(k): columns k and k-1 now hold */
916
917 /*              ( W(k-1) W(k) ) = ( U(k-1) U(k) )*D(k) */
918
919 /*              where U(k) and U(k-1) are the k-th and (k-1)-th columns */
920 /*              of U */
921
922 /*              Perform a rank-2 update of A(1:k-2,1:k-2) as */
923
924 /*              A := A - ( U(k-1) U(k) )*D(k)*( U(k-1) U(k) )**T */
925 /*                 = A - ( W(k-1) W(k) )*inv(D(k))*( W(k-1) W(k) )**T */
926
927                 if (k > 2) {
928
929                     d12 = a[k - 1 + k * a_dim1];
930                     d22 = a[k - 1 + (k - 1) * a_dim1] / d12;
931                     d11 = a[k + k * a_dim1] / d12;
932                     t = 1.f / (d11 * d22 - 1.f);
933                     d12 = t / d12;
934
935                     for (j = k - 2; j >= 1; --j) {
936                         wkm1 = d12 * (d11 * a[j + (k - 1) * a_dim1] - a[j + k 
937                                 * a_dim1]);
938                         wk = d12 * (d22 * a[j + k * a_dim1] - a[j + (k - 1) * 
939                                 a_dim1]);
940                         for (i__ = j; i__ >= 1; --i__) {
941                             a[i__ + j * a_dim1] = a[i__ + j * a_dim1] - a[i__ 
942                                     + k * a_dim1] * wk - a[i__ + (k - 1) * 
943                                     a_dim1] * wkm1;
944 /* L20: */
945                         }
946                         a[j + k * a_dim1] = wk;
947                         a[j + (k - 1) * a_dim1] = wkm1;
948 /* L30: */
949                     }
950
951                 }
952
953             }
954         }
955
956 /*        Store details of the interchanges in IPIV */
957
958         if (kstep == 1) {
959             ipiv[k] = kp;
960         } else {
961             ipiv[k] = -kp;
962             ipiv[k - 1] = -kp;
963         }
964
965 /*        Decrease K and return to the start of the main loop */
966
967         k -= kstep;
968         goto L10;
969
970     } else {
971
972 /*        Factorize A as L*D*L**T using the lower triangle of A */
973
974 /*        K is the main loop index, increasing from 1 to N in steps of */
975 /*        1 or 2 */
976
977         k = 1;
978 L40:
979
980 /*        If K > N, exit from loop */
981
982         if (k > *n) {
983             goto L70;
984         }
985         kstep = 1;
986
987 /*        Determine rows and columns to be interchanged and whether */
988 /*        a 1-by-1 or 2-by-2 pivot block will be used */
989
990         absakk = (r__1 = a[k + k * a_dim1], abs(r__1));
991
992 /*        IMAX is the row-index of the largest off-diagonal element in */
993 /*        column K, and COLMAX is its absolute value. */
994 /*        Determine both COLMAX and IMAX. */
995
996         if (k < *n) {
997             i__1 = *n - k;
998             imax = k + isamax_(&i__1, &a[k + 1 + k * a_dim1], &c__1);
999             colmax = (r__1 = a[imax + k * a_dim1], abs(r__1));
1000         } else {
1001             colmax = 0.f;
1002         }
1003
1004         if (f2cmax(absakk,colmax) == 0.f || sisnan_(&absakk)) {
1005
1006 /*           Column K is zero or underflow, or contains a NaN: */
1007 /*           set INFO and continue */
1008
1009             if (*info == 0) {
1010                 *info = k;
1011             }
1012             kp = k;
1013         } else {
1014             if (absakk >= alpha * colmax) {
1015
1016 /*              no interchange, use 1-by-1 pivot block */
1017
1018                 kp = k;
1019             } else {
1020
1021 /*              JMAX is the column-index of the largest off-diagonal */
1022 /*              element in row IMAX, and ROWMAX is its absolute value */
1023
1024                 i__1 = imax - k;
1025                 jmax = k - 1 + isamax_(&i__1, &a[imax + k * a_dim1], lda);
1026                 rowmax = (r__1 = a[imax + jmax * a_dim1], abs(r__1));
1027                 if (imax < *n) {
1028                     i__1 = *n - imax;
1029                     jmax = imax + isamax_(&i__1, &a[imax + 1 + imax * a_dim1],
1030                              &c__1);
1031 /* Computing MAX */
1032                     r__2 = rowmax, r__3 = (r__1 = a[jmax + imax * a_dim1], 
1033                             abs(r__1));
1034                     rowmax = f2cmax(r__2,r__3);
1035                 }
1036
1037                 if (absakk >= alpha * colmax * (colmax / rowmax)) {
1038
1039 /*                 no interchange, use 1-by-1 pivot block */
1040
1041                     kp = k;
1042                 } else if ((r__1 = a[imax + imax * a_dim1], abs(r__1)) >= 
1043                         alpha * rowmax) {
1044
1045 /*                 interchange rows and columns K and IMAX, use 1-by-1 */
1046 /*                 pivot block */
1047
1048                     kp = imax;
1049                 } else {
1050
1051 /*                 interchange rows and columns K+1 and IMAX, use 2-by-2 */
1052 /*                 pivot block */
1053
1054                     kp = imax;
1055                     kstep = 2;
1056                 }
1057             }
1058
1059             kk = k + kstep - 1;
1060             if (kp != kk) {
1061
1062 /*              Interchange rows and columns KK and KP in the trailing */
1063 /*              submatrix A(k:n,k:n) */
1064
1065                 if (kp < *n) {
1066                     i__1 = *n - kp;
1067                     sswap_(&i__1, &a[kp + 1 + kk * a_dim1], &c__1, &a[kp + 1 
1068                             + kp * a_dim1], &c__1);
1069                 }
1070                 i__1 = kp - kk - 1;
1071                 sswap_(&i__1, &a[kk + 1 + kk * a_dim1], &c__1, &a[kp + (kk + 
1072                         1) * a_dim1], lda);
1073                 t = a[kk + kk * a_dim1];
1074                 a[kk + kk * a_dim1] = a[kp + kp * a_dim1];
1075                 a[kp + kp * a_dim1] = t;
1076                 if (kstep == 2) {
1077                     t = a[k + 1 + k * a_dim1];
1078                     a[k + 1 + k * a_dim1] = a[kp + k * a_dim1];
1079                     a[kp + k * a_dim1] = t;
1080                 }
1081             }
1082
1083 /*           Update the trailing submatrix */
1084
1085             if (kstep == 1) {
1086
1087 /*              1-by-1 pivot block D(k): column k now holds */
1088
1089 /*              W(k) = L(k)*D(k) */
1090
1091 /*              where L(k) is the k-th column of L */
1092
1093                 if (k < *n) {
1094
1095 /*                 Perform a rank-1 update of A(k+1:n,k+1:n) as */
1096
1097 /*                 A := A - L(k)*D(k)*L(k)**T = A - W(k)*(1/D(k))*W(k)**T */
1098
1099                     d11 = 1.f / a[k + k * a_dim1];
1100                     i__1 = *n - k;
1101                     r__1 = -d11;
1102                     ssyr_(uplo, &i__1, &r__1, &a[k + 1 + k * a_dim1], &c__1, &
1103                             a[k + 1 + (k + 1) * a_dim1], lda);
1104
1105 /*                 Store L(k) in column K */
1106
1107                     i__1 = *n - k;
1108                     sscal_(&i__1, &d11, &a[k + 1 + k * a_dim1], &c__1);
1109                 }
1110             } else {
1111
1112 /*              2-by-2 pivot block D(k) */
1113
1114                 if (k < *n - 1) {
1115
1116 /*                 Perform a rank-2 update of A(k+2:n,k+2:n) as */
1117
1118 /*                 A := A - ( (A(k) A(k+1))*D(k)**(-1) ) * (A(k) A(k+1))**T */
1119
1120 /*                 where L(k) and L(k+1) are the k-th and (k+1)-th */
1121 /*                 columns of L */
1122
1123                     d21 = a[k + 1 + k * a_dim1];
1124                     d11 = a[k + 1 + (k + 1) * a_dim1] / d21;
1125                     d22 = a[k + k * a_dim1] / d21;
1126                     t = 1.f / (d11 * d22 - 1.f);
1127                     d21 = t / d21;
1128
1129                     i__1 = *n;
1130                     for (j = k + 2; j <= i__1; ++j) {
1131
1132                         wk = d21 * (d11 * a[j + k * a_dim1] - a[j + (k + 1) * 
1133                                 a_dim1]);
1134                         wkp1 = d21 * (d22 * a[j + (k + 1) * a_dim1] - a[j + k 
1135                                 * a_dim1]);
1136
1137                         i__2 = *n;
1138                         for (i__ = j; i__ <= i__2; ++i__) {
1139                             a[i__ + j * a_dim1] = a[i__ + j * a_dim1] - a[i__ 
1140                                     + k * a_dim1] * wk - a[i__ + (k + 1) * 
1141                                     a_dim1] * wkp1;
1142 /* L50: */
1143                         }
1144
1145                         a[j + k * a_dim1] = wk;
1146                         a[j + (k + 1) * a_dim1] = wkp1;
1147
1148 /* L60: */
1149                     }
1150                 }
1151             }
1152         }
1153
1154 /*        Store details of the interchanges in IPIV */
1155
1156         if (kstep == 1) {
1157             ipiv[k] = kp;
1158         } else {
1159             ipiv[k] = -kp;
1160             ipiv[k + 1] = -kp;
1161         }
1162
1163 /*        Increase K and return to the start of the main loop */
1164
1165         k += kstep;
1166         goto L40;
1167
1168     }
1169
1170 L70:
1171
1172     return 0;
1173
1174 /*     End of SSYTF2 */
1175
1176 } /* ssytf2_ */
1177