| 1 |
#include "f2c.h" |
| 2 |
|
| 3 |
#ifdef KR_headers |
| 4 |
double log(), f__cabs(), atan2(); |
| 5 |
#define ANSI(x) () |
| 6 |
#else |
| 7 |
#define ANSI(x) x |
| 8 |
#undef abs |
| 9 |
#include "math.h" |
| 10 |
#ifdef __cplusplus |
| 11 |
extern "C" { |
| 12 |
#endif |
| 13 |
extern double f__cabs(double, double); |
| 14 |
#endif |
| 15 |
|
| 16 |
#ifndef NO_DOUBLE_EXTENDED |
| 17 |
#ifndef GCC_COMPARE_BUG_FIXED |
| 18 |
#ifndef Pre20000310 |
| 19 |
#ifdef Comment |
| 20 |
Some versions of gcc, such as 2.95.3 and 3.0.4, are buggy under -O2 or -O3: |
| 21 |
on IA32 (Intel 80x87) systems, they may do comparisons on values computed |
| 22 |
in extended-precision registers. This can lead to the test "s > s0" that |
| 23 |
was used below being carried out incorrectly. The fix below cannot be |
| 24 |
spoiled by overzealous optimization, since the compiler cannot know |
| 25 |
whether gcc_bug_bypass_diff_F2C will be nonzero. (We expect it always |
| 26 |
to be zero. The weird name is unlikely to collide with anything.) |
| 27 |
|
| 28 |
An example (provided by Ulrich Jakobus) where the bug fix matters is |
| 29 |
|
| 30 |
double complex a, b |
| 31 |
a = (.1099557428756427618354862829619, .9857360542953131909982289471372) |
| 32 |
b = log(a) |
| 33 |
|
| 34 |
An alternative to the fix below would be to use 53-bit rounding precision, |
| 35 |
but the means of specifying this 80x87 feature are highly unportable. |
| 36 |
#endif /*Comment*/ |
| 37 |
#define BYPASS_GCC_COMPARE_BUG |
| 38 |
double (*gcc_bug_bypass_diff_F2C) ANSI((double*,double*)); |
| 39 |
static double |
| 40 |
#ifdef KR_headers |
| 41 |
diff1(a,b) double *a, *b; |
| 42 |
#else |
| 43 |
diff1(double *a, double *b) |
| 44 |
#endif |
| 45 |
{ return *a - *b; } |
| 46 |
#endif /*Pre20000310*/ |
| 47 |
#endif /*GCC_COMPARE_BUG_FIXED*/ |
| 48 |
#endif /*NO_DOUBLE_EXTENDED*/ |
| 49 |
|
| 50 |
#ifdef KR_headers |
| 51 |
VOID z_log(r, z) doublecomplex *r, *z; |
| 52 |
#else |
| 53 |
void z_log(doublecomplex *r, doublecomplex *z) |
| 54 |
#endif |
| 55 |
{ |
| 56 |
double s, s0, t, t2, u, v; |
| 57 |
double zi = z->i, zr = z->r; |
| 58 |
#ifdef BYPASS_GCC_COMPARE_BUG |
| 59 |
double (*diff) ANSI((double*,double*)); |
| 60 |
#endif |
| 61 |
|
| 62 |
r->i = atan2(zi, zr); |
| 63 |
#ifdef Pre20000310 |
| 64 |
r->r = log( f__cabs( zr, zi ) ); |
| 65 |
#else |
| 66 |
if (zi < 0) |
| 67 |
zi = -zi; |
| 68 |
if (zr < 0) |
| 69 |
zr = -zr; |
| 70 |
if (zr < zi) { |
| 71 |
t = zi; |
| 72 |
zi = zr; |
| 73 |
zr = t; |
| 74 |
} |
| 75 |
t = zi/zr; |
| 76 |
s = zr * sqrt(1 + t*t); |
| 77 |
/* now s = f__cabs(zi,zr), and zr = |zr| >= |zi| = zi */ |
| 78 |
if ((t = s - 1) < 0) |
| 79 |
t = -t; |
| 80 |
if (t > .01) |
| 81 |
r->r = log(s); |
| 82 |
else { |
| 83 |
|
| 84 |
#ifdef Comment |
| 85 |
|
| 86 |
log(1+x) = x - x^2/2 + x^3/3 - x^4/4 + - ... |
| 87 |
|
| 88 |
= x(1 - x/2 + x^2/3 -+...) |
| 89 |
|
| 90 |
[sqrt(y^2 + z^2) - 1] * [sqrt(y^2 + z^2) + 1] = y^2 + z^2 - 1, so |
| 91 |
|
| 92 |
sqrt(y^2 + z^2) - 1 = (y^2 + z^2 - 1) / [sqrt(y^2 + z^2) + 1] |
| 93 |
|
| 94 |
#endif /*Comment*/ |
| 95 |
|
| 96 |
#ifdef BYPASS_GCC_COMPARE_BUG |
| 97 |
if (!(diff = gcc_bug_bypass_diff_F2C)) |
| 98 |
diff = diff1; |
| 99 |
#endif |
| 100 |
t = ((zr*zr - 1.) + zi*zi) / (s + 1); |
| 101 |
t2 = t*t; |
| 102 |
s = 1. - 0.5*t; |
| 103 |
u = v = 1; |
| 104 |
do { |
| 105 |
s0 = s; |
| 106 |
u *= t2; |
| 107 |
v += 2; |
| 108 |
s += u/v - t*u/(v+1); |
| 109 |
} |
| 110 |
#ifdef BYPASS_GCC_COMPARE_BUG |
| 111 |
while(s - s0 > 1e-18 || (*diff)(&s,&s0) > 0.); |
| 112 |
#else |
| 113 |
while(s > s0); |
| 114 |
#endif |
| 115 |
r->r = s*t; |
| 116 |
} |
| 117 |
#endif |
| 118 |
} |
| 119 |
#ifdef __cplusplus |
| 120 |
} |
| 121 |
#endif |