summaryrefslogtreecommitdiff
path: root/tdiv.c
blob: d93f8943f4f1cfe0ed6f731aaafe37ab84e1c06d (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/* tdiv -- test file for mpc_div.

Copyright (C) 2002 Andreas Enge, Paul Zimmermann

This file is part of the MPC Library.

The MPC Library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or (at your
option) any later version.

The MPC Library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
License for more details.

You should have received a copy of the GNU Lesser General Public License
along with the MPC Library; see the file COPYING.LIB.  If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */

#include <stdio.h>
#include <stdlib.h>
#include "gmp.h"
#include "mpfr.h"
#include "mpc.h"
#include "mpc-impl.h"

#define OUT(x) { mpc_out_str (stdout, 2, 0, x, MPC_RNDNN); putchar ('\n'); }
#define ERR(x) { mpc_out_str (stderr, 2, 0, x, MPC_RNDNN); \
                 fprintf (stderr, "\n"); }

int mpc_div_ref (mpc_ptr, mpc_srcptr, mpc_srcptr, mpc_rnd_t);

int
mpc_div_ref (mpc_ptr a, mpc_srcptr b, mpc_srcptr c, mpc_rnd_t rnd)
{
  int ok = 0, inexact_q, inex_re, inex_im = 0, cancel = 0, err, sgn;
  mpfr_t u, v, q, t;
  mp_prec_t prec;
  mp_rnd_t rnd_re, rnd_im;

  if (mpfr_cmp_ui (MPC_IM(c), 0) == 0) /* c is real */
    {
      /* compute imaginary part first in case a=b or a=c */
      inex_im = mpfr_div (MPC_IM(a), MPC_IM(b), MPC_RE(c), MPC_RND_IM(rnd));
      inex_re = mpfr_div (MPC_RE(a), MPC_RE(b), MPC_RE(c), MPC_RND_RE(rnd));
      return MPC_INEX(inex_re, inex_im);
    }

  prec = MPC_MAX_PREC(a);

  /* try to guess the signs of the real and imaginary parts */
  sgn = mpfr_sgn (MPC_RE(b)) * mpfr_sgn (MPC_RE(c))
    + mpfr_sgn (MPC_IM(b)) * mpfr_sgn (MPC_IM(c));
  rnd_re = (sgn > 0) ? GMP_RNDU : GMP_RNDD;
  sgn = mpfr_sgn (MPC_RE(b)) * mpfr_sgn (MPC_IM(c))
    - mpfr_sgn (MPC_IM(b)) * mpfr_sgn (MPC_RE(c));
  rnd_im = (sgn > 0) ? GMP_RNDU : GMP_RNDD;

  /* first try with only one real division */
      prec += mpc_ceil_log2 (prec) + 3;

      mpfr_init2 (u, prec);
      mpfr_init2 (v, prec);
      mpfr_init2 (q, prec);
      mpfr_init2 (t, prec);

      /* first compute 1/norm(c)^2 rounded away */
      inexact_q = mpfr_mul (u, MPC_RE(c), MPC_RE(c), GMP_RNDZ);
      inexact_q |= mpfr_mul (v, MPC_IM(c), MPC_IM(c), GMP_RNDZ);
      inexact_q |= mpfr_add (q, u, v, GMP_RNDZ); /* 3 ulps */
      inexact_q |= mpfr_ui_div (q, 1, q, GMP_RNDU); /* 7 ulps */

      /* now compute b*conjugate(c) */

      /* real part */
      inex_re = mpfr_mul (u, MPC_RE(b), MPC_RE(c), rnd_re); /* 1 */
      inex_re |= mpfr_mul (v, MPC_IM(b), MPC_IM(c), rnd_re); /* 1 */
      inex_re |= mpfr_add (t, u, v, rnd_re);
      if (MPFR_IS_ZERO(u))
        {
          if (MPFR_IS_ZERO(v))
            cancel = 0;
          else
            cancel = MPFR_EXP(v) - MPFR_EXP(t);
        }
      else if (MPFR_IS_ZERO(v))
        cancel = MPFR_EXP(u) - MPFR_EXP(t);
      else
        {
          cancel = (MPFR_IS_ZERO(t)) ? prec
            : MAX(MPFR_EXP(u), MPFR_EXP(v)) - MPFR_EXP(t);
        }
      /* err(t) <= [1 + 2*2^cancel] ulp(t) */
      inex_re |= mpfr_mul (t, t, q, rnd_re) || inexact_q;
      /* err(t) <= [1 + 3*(1 + 2*2^cancel) + 14] ulp(t)
       */
      err = (cancel <= 1) ? 5 : ((cancel == 2) ? 6 : 
                                 ((cancel <= 4) ? 7 : cancel + 3));
      ok = (inex_re == 0) || ((err < prec) && mpfr_can_round (t, prec - err,
                            rnd_re, MPC_RND_RE(rnd), MPFR_PREC(MPC_RE(a))));
      if ((rnd_re == GMP_RNDU && mpfr_sgn (t) < 0)
          || (rnd_re == GMP_RNDD && mpfr_sgn (t) > 0))
        {
          ok = 0;
          rnd_re = INV_RND(rnd_re);
        }

      if (ok) /* compute imaginary part */
        {
          inex_im = mpfr_mul (u, MPC_RE(b), MPC_IM(c), INV_RND(rnd_im));
          inex_im |= mpfr_mul (v, MPC_IM(b), MPC_RE(c), rnd_im);
          if (MPFR_IS_ZERO(u))
            {
              if (MPFR_IS_ZERO(v))
                cancel = 0;
              else
                cancel = MPFR_EXP(v);
            }
          else if (MPFR_IS_ZERO(v))
            cancel = MPFR_EXP(u);
          else
            cancel = MAX(MPFR_EXP(u), MPFR_EXP(v));
          inex_im |= mpfr_sub (u, v, u, rnd_im);
          if (!MPFR_IS_ZERO(u))
            cancel = cancel - MPFR_EXP(u);
          inex_im |= mpfr_mul (u, u, q, rnd_im) || inexact_q;
          err = (cancel <= 1) ? 5 : ((cancel == 2) ? 6 :
                                     ((cancel <= 4) ? 7 : cancel + 3));
          ok = (inex_im == 0) || ((err < prec) && mpfr_can_round (u,
             prec - err, GMP_RNDN, MPC_RND_IM(rnd), MPFR_PREC(MPC_IM(a))));
          if ((rnd_im == GMP_RNDU && mpfr_sgn (u) < 0)
              || (rnd_im == GMP_RNDD && mpfr_sgn (u) > 0))
            {
              ok = 0;
              rnd_im = INV_RND(rnd_im);
            }
        }

      /* now loop with two real divisions, to detect exact quotients */
  while (ok == 0)
    {
      prec += mpc_ceil_log2 (prec) + 3;

      mpfr_set_prec (u, prec);
      mpfr_set_prec (v, prec);
      mpfr_set_prec (q, prec);
      mpfr_set_prec (t, prec);

      /* first compute 1/norm(c)^2 rounded away */
      inexact_q = mpfr_mul (u, MPC_RE(c), MPC_RE(c), GMP_RNDZ);
      inexact_q |= mpfr_mul (v, MPC_IM(c), MPC_IM(c), GMP_RNDZ);
      inexact_q |= mpfr_add (q, u, v, GMP_RNDZ); /* 3 ulps */

      /* now compute b*conjugate(c) */

      /* real part */
      inex_re = mpfr_mul (u, MPC_RE(b), MPC_RE(c), rnd_re); /* 1 */
      inex_re |= mpfr_mul (v, MPC_IM(b), MPC_IM(c), rnd_re); /* 1 */
      inex_re |= mpfr_add (t, u, v, rnd_re);
      if (MPFR_IS_ZERO(u))
        {
          if (MPFR_IS_ZERO(v))
            cancel = 0;
          else
            cancel = MPFR_EXP(v) - MPFR_EXP(t);
        }
      else if (MPFR_IS_ZERO(v))
        cancel = MPFR_EXP(u) - MPFR_EXP(t);
      else
        cancel = (MPFR_IS_ZERO(t)) ? prec
          : MAX(MPFR_EXP(u), MPFR_EXP(v)) - MPFR_EXP(t);
      /* err(t) <= [1 + 2*2^cancel] ulp(t) */
      inex_re |= mpfr_div (t, t, q, rnd_re) || inexact_q;
      /* err(t) <= [1 + 2*(1 + 2*2^cancel) + 6] ulp(t)
       */
      err = (cancel <= 0) ? 4 : cancel + 3;
      ok = (inex_re == 0) || ((err < prec) && mpfr_can_round (t, prec - err,
                            rnd_re, MPC_RND_RE(rnd), MPFR_PREC(MPC_RE(a))));
      if ((rnd_re == GMP_RNDU && mpfr_sgn (t) < 0)
          || (rnd_re == GMP_RNDD && mpfr_sgn (t) > 0))
        {
          ok = 0;
          rnd_re = INV_RND(rnd_re);
        }

      if (ok) /* compute imaginary part */
        {
          inex_im = mpfr_mul (u, MPC_RE(b), MPC_IM(c), INV_RND(rnd_im));
          inex_im |= mpfr_mul (v, MPC_IM(b), MPC_RE(c), rnd_im);
          if (MPFR_IS_ZERO(u))
            {
              if (MPFR_IS_ZERO(v))
                cancel = 0;
              else
                cancel = MPFR_EXP(v);
            }
          else if (MPFR_IS_ZERO(v))
            cancel = MPFR_EXP(u);
          else
            cancel = MAX(MPFR_EXP(u), MPFR_EXP(v));
          inex_im |= mpfr_sub (u, v, u, rnd_im);
          if (!MPFR_IS_ZERO(u))
            cancel = cancel - MPFR_EXP(u);
          inex_im |= mpfr_div (u, u, q, rnd_im) || inexact_q;
          err = (cancel <= 0) ? 4 : cancel + 3;
          ok = (inex_im == 0) || ((err < prec) && mpfr_can_round (u,
             prec - err, GMP_RNDN, MPC_RND_IM(rnd), MPFR_PREC(MPC_IM(a))));
          if ((rnd_im == GMP_RNDU && mpfr_sgn (u) < 0)
              || (rnd_im == GMP_RNDD && mpfr_sgn (u) > 0))
            {
              ok = 0;
              rnd_im = INV_RND(rnd_im);
            }
        }
    }

  inex_re = mpfr_set (MPC_RE(a), t, MPC_RND_RE(rnd)) || inex_re;
  inex_im = mpfr_set (MPC_IM(a), u, MPC_RND_IM(rnd)) || inex_im;

  mpfr_clear (u);
  mpfr_clear (v);
  mpfr_clear (q);
  mpfr_clear (t);

  return MPC_INEX(inex_re, inex_im);
}

int
main()
{
  mpc_t b, c, q, q_ref;
  int inex, i;
  mp_prec_t prec;
  mp_rnd_t rnd_re, rnd_im;
  mpc_rnd_t rnd;

  mpc_init (b);
  mpc_init (c);
  mpc_init (q);
  mpc_init (q_ref);

  mpc_set_prec (b, 10);
  mpc_set_prec (c, 10);
  mpc_set_prec (q, 10);

  mpc_set_ui_ui (b, 973, 964, MPC_RNDNN);
  mpc_set_ui_ui (c, 725, 745, MPC_RNDNN);

  inex = mpc_div (q, b, c, MPC_RNDZZ);
  mpc_set_si_si (b, 43136, -787, MPC_RNDNN);
  mpc_div_2exp (b, b, 15, MPC_RNDNN);
  if (mpc_cmp (q, b))
    {
      fprintf (stderr, "mpc_div failed for (973+I*964)/(725+I*745)\n");
      exit (1);
    }

  mpc_set_si_si (b, -837, 637, MPC_RNDNN);
  mpc_set_si_si (c, 63, -5, MPC_RNDNN);
  inex = mpc_div (q, b, c, MPC_RNDZN);

  mpc_set_prec (b, 2);
  mpc_set_prec (c, 2);
  mpc_set_prec (q, 2);

  mpc_set_ui_ui (b, 4, 3, MPC_RNDNN);
  mpc_set_ui_ui (c, 1, 2, MPC_RNDNN);

  inex = mpc_div (q, b, c, MPC_RNDNN);

  for (prec = 2; prec < 1000; prec++)
    {
      mpc_set_prec (b, prec);
      mpc_set_prec (c, prec);
      mpc_set_prec (q, prec);
      mpc_set_prec (q_ref, prec);

      for (i = 0; i < 1000/prec; i++)
        {
          mpc_random (b);
          /* generate a non-zero divisor */
          do
            {
              mpc_random (c);
            }
          while (mpfr_sgn (MPC_RE(c)) == 0 && mpfr_sgn (MPC_IM(c)) == 0);

          for (rnd_re = (mp_rnd_t)0; rnd_re < (mp_rnd_t)4; ++rnd_re)
            for (rnd_im = (mp_rnd_t)0; rnd_im < (mp_rnd_t)4; ++rnd_im)
              {
                rnd = RNDC(rnd_re, rnd_im);
                mpc_div     (q,     b, c, rnd);
                mpc_div_ref (q_ref, b, c, rnd);
                if (mpc_cmp (q, q_ref))
                  {
                    fprintf (stderr, "mpc_div and mpc_div_ref differ"
                             "for prec=%lu rnd=(%s,%s)\n", prec,
                             mpfr_print_rnd_mode (rnd_re),
                             mpfr_print_rnd_mode (rnd_im));
                    fprintf (stderr, "b=");
                    ERR(b);
                    fprintf (stderr, "c=");
                    ERR(c);
                    fprintf (stderr, "q=");
                    ERR(q);
                    fprintf (stderr, "q_ref=");
                    ERR(q_ref);
                    exit (1);
                  }
              }
        }

    }

  mpc_clear (b);
  mpc_clear (c);
  mpc_clear (q);
  mpc_clear (q_ref);
  
  return 0;
}