summaryrefslogtreecommitdiff
path: root/src/fpfilter.cc
blob: 993130edf94e5976f3f882258fa333c12ec2788e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <config.h>

#include <fpfilter.h>

#if (defined(_Linux_i386_))
#include <fpu_control.h>
#elif ((defined(_SunOS_)) || (defined(_IRIX_)))
#include <ieeefp.h>
#endif

//#include <float.h>
//  Are u using Microsoft?!? Sorry I have no idea what a mess they do!

int round_nearest()
{
//#ifdef _Linux_i386_
  
  fpu_control_t w;
  
  _FPU_GETCW(w);
  w = w & 0xf3ff | _FPU_RC_NEAREST;
  _FPU_SETCW(w);
  
//#endif
//#ifdef _SunOS_
//  
//  fp_rnd m = FP_RN;
//  
//  fpsetround(m);
//  
//#endif
//#ifdef _IRIX_
//  
//  fp_rnd m = FP_RN;
//  
//  fpsetround(m);
//  
//#endif
  
  return 0;
}

int round_down()
{
//#ifdef _Linux_i386_
  
  fpu_control_t w;
  
  _FPU_GETCW(w);
  w = w & 0xf3ff | _FPU_RC_DOWN;
  _FPU_SETCW(w);
  
//#endif
//#ifdef _SunOS_
//  
//  fp_rnd m = FP_RM;
//  
//  fpsetround(m);
//  
//#endif
//#ifdef _IRIX_
//  
//  fp_rnd m = FP_RM;
//  
//  fpsetround(m);
//  
//#endif
  
  return 0;
}

int round_up()
{
//#ifdef _Linux_i386_
  
  fpu_control_t w;
  
  _FPU_GETCW(w);
  w = w & 0xf3ff | _FPU_RC_UP;
  _FPU_SETCW(w);
  
//#endif
//#ifdef _SunOS_
//  
//  fp_rnd m = FP_RP;
//  
//  fpsetround(m);
//  
//#endif
//#ifdef _IRIX_
//  
//  fp_rnd m = FP_RP;
//  
//  fpsetround(m);
//  
//#endif
  
  return 0;
}

int round_zero()
{
//#ifdef _Linux_i386_
  
  fpu_control_t w;
  
  _FPU_GETCW(w);
  w = w & 0xf3ff | _FPU_RC_ZERO;
  _FPU_SETCW(w);
  
//#endif
//#ifdef _SunOS_
//  
//  fp_rnd m = FP_RZ;
//  
//  fpsetround(m);
//  
//#endif
//#ifdef _IRIX_
//  
//  fp_rnd m = FP_RZ;
//  
//  fpsetround(m);
//  
//#endif
  
  return 0;
}

int horner_with_err(const double* const poly,
                    const unsigned long deg,
                    const double        coeff_err,
                    const double        in_val,
                    const double        in_err,
                    double&             out_val,
                    double&             out_err)
{
  unsigned long i;
  int           f;
  double        abs_in_val;
  double        rel_err;
  
  f = finite(in_val);
  
  //  1. Evaluate poly at in_val.
  
  if (f)
    f = finite(out_val = poly[0]);
  
  for (i = 1; f && i <= deg; i++)
    if (f = finite(out_val *= in_val))
      f = finite(out_val += poly[i]);
  
  //  2. Compute a bound on error.  We round towards +infinity
  //     to make sure the bound is sufficient. Since the errors
  //     on each term could conspire in this way, the error is
  //     bounded by
  //       (max per-term error) * (value of abs-val-coeff poly).
  
  //  2-1. Set MPU mode.
  
  round_up();
  
  //  2-2. Evaluate the polynomial whose coeffients are
  //       the absolute values of the coeffients of poly.
  
  if (f)
    if (f = finite(abs_in_val = fabs(in_val)))
      f = finite(out_err = fabs(poly[0]));
  
  for (i = 1; f && i <= deg; i++)
    if (f = finite(out_err *= abs_in_val))
      f = finite(out_err += fabs(poly[i]));
  
  //  2-3. Compute the maximum relative error for any single term.
  
  if (f)
    f = finite(rel_err = deg * (2 * DBL_EPSILON + in_err) + coeff_err);
  
  //  2-4. Compute a bound on error.
  
  if (f)
    f = finite(out_err *= rel_err);
  
  //  2-5. Retrieve the original rounding mode.
  
  round_nearest();
  
  return f;
}

//  int sign_given_err(double val, double err)
//    returns some positive value if val is certainly positive,
//            some negative value if val is certainly negative, and
//            0 otherwise.

int sign_given_err(const double val, const double err)
{
  assert(finite(val));
  assert(finite(err));
  
  double v_l, v_h;
  
  if (finite(v_l = val - err) && val > 0.0 && v_l > 0.0)
    return 1;
  else if (finite(v_h = val + err) && val < 0.0 && v_h < 0.0)
    return - 1;
  else
    return 0;
}

double newton_for_pseudoroot(const double* const poly,
                             const unsigned long deg,
                             const double coeff_err,
                             const double* const deriv,
                             const double l0,
                             const double h0)
{
  double l, h, l_val, l_err, h_val, h_err, m, p_val, p_err, d_val, d_err;
  int    l_sign, h_sign, p_sign, d_sign;
  
  l = l0;
  horner_with_err(poly, deg, coeff_err, l, 0.0, l_val, l_err);
  l_sign = sign_given_err(l_val, l_err);
  h = h0;
  horner_with_err(poly, deg, coeff_err, h, 0.0, h_val, h_err);
  h_sign = sign_given_err(h_val, h_err);
  assert(l_sign * h_sign < 0);
  
  m = (l + h) / 2.0;
  
  while (1)
  {
    horner_with_err(poly, deg, coeff_err, m, 0.0, p_val, p_err);
    p_sign = sign_given_err(p_val, p_err);
    
    if (p_sign == 0 || h - l < DBL_EPSILON * h)
      return m;
    else if (p_sign == l_sign)
      l = m;
    else
      h = m;
    
    horner_with_err(deriv, deg - 1, coeff_err + DBL_EPSILON,
                    m, 0.0,
                    d_val, d_err);
    m -= p_val / d_val;
    
    if (m <= l || m >= h)
      m = (l + h) / 2.0;
  }
  
  return 0.0;
}