summaryrefslogtreecommitdiff
path: root/src/ai.c
blob: 4f8a2aab327766c3336ff382b2a93fcc1c7d2d91 (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/* mpfr_ai -- Airy function Ai

Copyright 2010-2021 Free Software Foundation, Inc.
Contributed by the AriC and Caramba projects, INRIA.

This file is part of the GNU MPFR Library.

The GNU MPFR 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 3 of the License, or (at your
option) any later version.

The GNU MPFR 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 GNU MPFR Library; see the file COPYING.LESSER.  If not, see
https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */

#define MPFR_NEED_LONGLONG_H
#include "mpfr-impl.h"

/* Reminder and notations:
   -----------------------

   Ai is the solution of:
        / y'' - x*y = 0
       {    Ai(0)   = 1/ ( 9^(1/3)*Gamma(2/3) )
        \  Ai'(0)   = -1/ ( 3^(1/3)*Gamma(1/3) )

   Series development:
       Ai(x) = sum (a_i*x^i)
             = sum (t_i)

   Recurrences:
       a_(i+3) = a_i / ((i+2)*(i+3))
       t_(i+3) = t_i * x^3 / ((i+2)*(i+3))

   Values:
       a_0 = Ai(0)  ~  0.355
       a_1 = Ai'(0) ~ -0.259
*/


/* Airy function Ai evaluated by the most naive algorithm.
   Assume that x is a finite number. */
static int
mpfr_ai1 (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd)
{
  MPFR_ZIV_DECL (loop);
  MPFR_SAVE_EXPO_DECL (expo);
  mpfr_prec_t wprec;             /* working precision */
  mpfr_prec_t prec;              /* target precision */
  mpfr_prec_t err;               /* used to estimate the evaluation error */
  mpfr_prec_t correct_bits;      /* estimates the number of correct bits*/
  unsigned long int k;
  unsigned long int cond;        /* condition number of the series */
  unsigned long int assumed_exponent; /* used as a lowerbound of |EXP(Ai(x))| */
  int r;
  mpfr_t s;                      /* used to store the partial sum */
  mpfr_t ti, tip1;   /* used to store successive values of t_i */
  mpfr_t x3;                     /* used to store x^3 */
  mpfr_t tmp_sp, tmp2_sp;        /* small precision variables */
  unsigned long int x3u;         /* used to store ceil(x^3) */
  mpfr_t temp1, temp2;
  int test1, test2;

  /* Logging */
  MPFR_LOG_FUNC (
    ("x[%Pu]=%.*Rg rnd=%d", mpfr_get_prec (x), mpfr_log_prec, x, rnd),
    ("y[%Pu]=%.*Rg", mpfr_get_prec (y), mpfr_log_prec, y) );

  /* Save current exponents range */
  MPFR_SAVE_EXPO_MARK (expo);

  if (MPFR_UNLIKELY (MPFR_IS_ZERO (x)))
    {
      mpfr_t y1, y2;
      prec = MPFR_ADD_PREC (MPFR_PREC (y), 3);
      mpfr_init2 (y1, prec);
      mpfr_init2 (y2, prec);
      MPFR_ZIV_INIT (loop, prec);

      /* ZIV loop */
      for (;;)
        {
          mpfr_gamma_one_and_two_third (y1, y2, prec); /* y2 = Gamma(2/3)(1 + delta1), |delta1| <= 2^{1-prec}. */

          r = mpfr_set_ui (y1, 9, MPFR_RNDN);
          MPFR_ASSERTD (r == 0);
          mpfr_cbrt (y1, y1, MPFR_RNDN); /* y1 = cbrt(9)(1 + delta2), |delta2| <= 2^{-prec}. */
          mpfr_mul (y1, y1, y2, MPFR_RNDN);
          mpfr_ui_div (y1, 1, y1, MPFR_RNDN);
          if (MPFR_LIKELY (MPFR_CAN_ROUND (y1, prec - 3, MPFR_PREC (y), rnd)))
            break;
          MPFR_ZIV_NEXT (loop, prec);
        }
      r = mpfr_set (y, y1, rnd);
      MPFR_ZIV_FREE (loop);
      MPFR_SAVE_EXPO_FREE (expo);
      mpfr_clear (y1);
      mpfr_clear (y2);
      return mpfr_check_range (y, r, rnd);
    }

  /* now x is not zero */
  MPFR_ASSERTD(!MPFR_IS_ZERO(x));

  /* FIXME: underflow for large values of |x| ? */


  /* Set initial precision */
  /* If we compute sum(i=0, N-1, t_i), the relative error is bounded by  */
  /*       2*(4N)*2^(1-wprec)*C(|x|)/Ai(x)                               */
  /* where C(|x|) = 1 if 0<=x<=1                                         */
  /*   and C(|x|) = (1/2)*x^(-1/4)*exp(2/3 x^(3/2))  if x >= 1           */

  /* A priori, we do not know N, so we estimate it to ~ prec             */
  /* If 0<=x<=1, we estimate Ai(x) ~ 1/8                                 */
  /* if 1<=x,    we estimate Ai(x) ~ (1/4)*x^(-1/4)*exp(-2/3 * x^(3/2))  */
  /* if x<=0,    ?????                                                   */

  /* We begin with 11 guard bits */
  prec = MPFR_ADD_PREC (MPFR_PREC (y), 11);
  MPFR_ZIV_INIT (loop, prec);

  /* The working precision is heuristically chosen in order to obtain  */
  /* approximately prec correct bits in the sum. To sum up: the sum    */
  /* is stopped when the *exact* sum gives ~ prec correct bit. And     */
  /* when it is stopped, the accuracy of the computed sum, with respect*/
  /* to the exact one should be ~prec bits.                            */
  mpfr_init2 (tmp_sp, MPFR_SMALL_PRECISION);
  mpfr_init2 (tmp2_sp, MPFR_SMALL_PRECISION);
  mpfr_abs (tmp_sp, x, MPFR_RNDU);
  mpfr_pow_ui (tmp_sp, tmp_sp, 3, MPFR_RNDU);
  mpfr_sqrt (tmp_sp, tmp_sp, MPFR_RNDU); /* tmp_sp ~ |x|^(3/2) */

  /* 0.96179669392597567 >~ 2/3 * log2(e). See algorithms.tex */
  mpfr_set_str (tmp2_sp, "0.96179669392597567", 10, MPFR_RNDU);
  mpfr_mul (tmp2_sp, tmp_sp, tmp2_sp, MPFR_RNDU);

  /* cond represents the number of lost bits in the evaluation of the sum */
  if (MPFR_GET_EXP (x) <= 0)
    cond = 0;
  else
    {
      MPFR_BLOCK_DECL (flags);

      MPFR_BLOCK (flags, cond = mpfr_get_ui (tmp2_sp, MPFR_RNDU));
      MPFR_ASSERTN (! MPFR_ERANGEFLAG (flags));
      cond -= (MPFR_GET_EXP (x) - 1) / 4 + 1;
    }

  /* The variable assumed_exponent is used to store the maximal assumed */
  /* exponent of Ai(x). More precisely, we assume that |Ai(x)| will be  */
  /* greater than 2^{-assumed_exponent}.                                */
  if (MPFR_IS_POS (x))
    {
      if (MPFR_GET_EXP (x) <= 0)
        assumed_exponent = 3;
      else
        {
          unsigned long int t;
          MPFR_BLOCK_DECL (flags);

          MPFR_BLOCK (flags, t = mpfr_get_ui (tmp2_sp, MPFR_RNDU));
          MPFR_ASSERTN (! MPFR_ERANGEFLAG (flags));
          assumed_exponent = t + 2 + (MPFR_GET_EXP (x) / 4 + 1);
          MPFR_ASSERTN (assumed_exponent > t);
        }
    }
  /* We do not know Ai (x) yet */
  /* We cover the case when EXP (Ai (x))>=-10 */
  else
    assumed_exponent = 10;

  {
    unsigned long int t, u;

    t = assumed_exponent + cond;
    MPFR_ASSERTN (t >= cond);
    u = MPFR_INT_CEIL_LOG2 (prec) + 5;
    t += u;
    MPFR_ASSERTN (t >= u);
    wprec = MPFR_ADD_PREC (prec, t);
  }

  mpfr_init2 (ti, wprec);
  mpfr_init2 (tip1, wprec);
  mpfr_init2 (temp1, wprec);
  mpfr_init2 (temp2, wprec);
  mpfr_init2 (x3, wprec);
  mpfr_init2 (s, wprec);

  /* ZIV loop */
  for (;;)
    {
      MPFR_LOG_MSG (("Working precision: %Pu\n", wprec));
      mpfr_set_prec (ti, wprec);
      mpfr_set_prec (tip1, wprec);
      mpfr_set_prec (x3, wprec);
      mpfr_set_prec (s, wprec);

      mpfr_sqr (x3, x, MPFR_RNDU);
      mpfr_mul (x3, x3, x, (MPFR_IS_POS (x)?MPFR_RNDU:MPFR_RNDD));  /* x3=x^3 */
      if (MPFR_IS_NEG (x))
        MPFR_CHANGE_SIGN (x3);
      x3u = mpfr_get_ui (x3, MPFR_RNDU);   /* x3u >= ceil(x^3) */
      if (MPFR_IS_NEG (x))
        MPFR_CHANGE_SIGN (x3);

      mpfr_gamma_one_and_two_third (temp1, temp2, wprec);
      mpfr_set_ui (ti, 9, MPFR_RNDN);
      mpfr_cbrt (ti, ti, MPFR_RNDN);
      mpfr_mul (ti, ti, temp2, MPFR_RNDN);
      mpfr_ui_div (ti, 1, ti , MPFR_RNDN); /* ti = 1/( Gamma (2/3)*9^(1/3) ) */

      mpfr_set_ui (tip1, 3, MPFR_RNDN);
      mpfr_cbrt (tip1, tip1, MPFR_RNDN);
      mpfr_mul (tip1, tip1, temp1, MPFR_RNDN);
      mpfr_neg (tip1, tip1, MPFR_RNDN);
      mpfr_div (tip1, x, tip1, MPFR_RNDN); /* tip1 = -x/(Gamma (1/3)*3^(1/3)) */

      mpfr_add (s, ti, tip1, MPFR_RNDN);


      /* Evaluation of the series */
      k = 2;
      for (;;)
        {
          mpfr_mul (ti, ti, x3, MPFR_RNDN);
          mpfr_mul (tip1, tip1, x3, MPFR_RNDN);

          mpfr_div_ui2 (ti, ti, k, (k+1), MPFR_RNDN);
          mpfr_div_ui2 (tip1, tip1, (k+1), (k+2), MPFR_RNDN);

          k += 3;
          mpfr_add (s, s, ti, MPFR_RNDN);
          mpfr_add (s, s, tip1, MPFR_RNDN);

          /* FIXME: if s==0 */
          test1 = MPFR_IS_ZERO (ti)
            || (MPFR_GET_EXP (ti) + (mpfr_exp_t)prec + 3 <= MPFR_GET_EXP (s));
          test2 = MPFR_IS_ZERO (tip1)
            || (MPFR_GET_EXP (tip1) + (mpfr_exp_t)prec + 3 <= MPFR_GET_EXP (s));

          if ( test1 && test2 && (x3u <= k*(k+1)/2) )
            break; /* FIXME: if k*(k+1) overflows */
        }

      MPFR_LOG_MSG (("Truncation rank: %lu\n", k));

      err = 4 + MPFR_INT_CEIL_LOG2 (k) + cond - MPFR_GET_EXP (s);

      /* err is the number of bits lost due to the evaluation error */
      /* wprec-(prec+1): number of bits lost due to the approximation error */
      MPFR_LOG_MSG (("Roundoff error: %Pu\n", err));
      MPFR_LOG_MSG (("Approxim error: %Pu\n", wprec-prec-1));

      if (wprec < err + 1)
        correct_bits = 0;
      else if (wprec < err + prec +1)
        correct_bits =  wprec - err - 1; /* since wprec > err + 1,
                                            correct_bits > 0 */
      else
        correct_bits = prec;

      if (MPFR_LIKELY (MPFR_CAN_ROUND (s, correct_bits, MPFR_PREC (y), rnd)))
        break;

      if (correct_bits == 0)
        {
          assumed_exponent *= 2;
          MPFR_LOG_MSG (("Not a single bit correct (assumed_exponent=%lu)\n",
                         assumed_exponent));
          wprec = prec + 5 + MPFR_INT_CEIL_LOG2 (prec) + cond +
            assumed_exponent;
        }
      else if (correct_bits < prec)
        { /* The precision was badly chosen */
          MPFR_LOG_MSG (("Bad assumption on the exponent of Ai(x)"
                         " (E=%" MPFR_EXP_FSPEC "d)\n",
                         (mpfr_eexp_t) MPFR_GET_EXP (s)));
          wprec = prec + err + 1;
        }
      else
        { /* We are really in a bad case of the TMD */
          MPFR_ZIV_NEXT (loop, prec);

          /* We update wprec */
          /* We assume that K will not be multiplied by more than 4 */
          wprec = prec + (MPFR_INT_CEIL_LOG2 (k) + 2) + 5 + cond
            - MPFR_GET_EXP (s);
        }

    } /* End of ZIV loop */

  MPFR_ZIV_FREE (loop);

  r = mpfr_set (y, s, rnd);

  mpfr_clear (ti);
  mpfr_clear (tip1);
  mpfr_clear (temp1);
  mpfr_clear (temp2);
  mpfr_clear (x3);
  mpfr_clear (s);
  mpfr_clear (tmp_sp);
  mpfr_clear (tmp2_sp);

  MPFR_SAVE_EXPO_FREE (expo);
  return mpfr_check_range (y, r, rnd);
}


/* Airy function Ai evaluated by Smith algorithm.
   Assume that x is a finite non-zero number. */
static int
mpfr_ai2 (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd)
{
  MPFR_ZIV_DECL (loop);
  MPFR_SAVE_EXPO_DECL (expo);
  mpfr_prec_t wprec;             /* working precision */
  mpfr_prec_t prec;              /* target precision */
  mpfr_prec_t err;               /* used to estimate the evaluation error */
  mpfr_prec_t correctBits;       /* estimates the number of correct bits*/
  unsigned long int i, j, L, t;
  unsigned long int cond;        /* condition number of the series */
  unsigned long int assumed_exponent; /* used as a lowerbound of |EXP(Ai(x))| */
  int r;                         /* returned ternary value */
  mpfr_t s;                      /* used to store the partial sum */
  mpfr_t u0, u1;
  mpfr_t *z;                     /* used to store the (x^3j) */
  mpfr_t result;
  mpfr_t tmp_sp, tmp2_sp;        /* small precision variables */
  unsigned long int x3u;         /* used to store ceil (x^3) */
  mpfr_t temp1, temp2;
  int test0, test1;

  /* Logging */
  MPFR_LOG_FUNC (
    ("x[%Pu]=%.*Rg rnd=%d", mpfr_get_prec (x),  mpfr_log_prec, x, rnd),
    ("y[%Pu]=%.*Rg", mpfr_get_prec (y), mpfr_log_prec, y));

  /* Save current exponents range */
  MPFR_SAVE_EXPO_MARK (expo);

  /* FIXME: underflow for large values of |x| */

  /* Set initial precision */
  /* See the analysis for the naive evaluation */

  /* We begin with 11 guard bits */
  prec = MPFR_PREC (y) + 11;
  MPFR_ZIV_INIT (loop, prec);

  mpfr_init2 (tmp_sp, MPFR_SMALL_PRECISION);
  mpfr_init2 (tmp2_sp, MPFR_SMALL_PRECISION);
  mpfr_abs (tmp_sp, x, MPFR_RNDU);
  mpfr_pow_ui (tmp_sp, tmp_sp, 3, MPFR_RNDU);
  mpfr_sqrt (tmp_sp, tmp_sp, MPFR_RNDU); /* tmp_sp ~ |x|^(3/2) */

  /* 0.96179669392597567 >~ 2/3 * log2(e). See algorithms.tex */
  mpfr_set_str (tmp2_sp, "0.96179669392597567", 10, MPFR_RNDU);
  mpfr_mul (tmp2_sp, tmp_sp, tmp2_sp, MPFR_RNDU);

  /* cond represents the number of lost bits in the evaluation of the sum */
  if (MPFR_GET_EXP (x) <= 0)
    cond = 0;
  else
    {
      MPFR_BLOCK_DECL (flags);

      MPFR_BLOCK (flags, cond = mpfr_get_ui (tmp2_sp, MPFR_RNDU));
      MPFR_ASSERTN (! MPFR_ERANGEFLAG (flags));
      cond -= (MPFR_GET_EXP (x) - 1) / 4 + 1;
    }

  /* This variable is used to store the maximal assumed exponent of       */
  /* Ai(x). More precisely, we assume that |Ai(x)| will be greater than   */
  /* 2^{-assumed_exponent}.                                               */
  if (MPFR_IS_POS (x))
    {
      if (MPFR_GET_EXP (x) <= 0)
        assumed_exponent = 3;
      else
        {
          unsigned long int t;
          MPFR_BLOCK_DECL (flags);

          MPFR_BLOCK (flags, t = mpfr_get_ui (tmp2_sp, MPFR_RNDU));
          MPFR_ASSERTN (! MPFR_ERANGEFLAG (flags));
          assumed_exponent = t + 2 + (MPFR_GET_EXP (x) / 4 + 1);
          MPFR_ASSERTN (assumed_exponent > t);
        }
    }
  /* We do not know Ai(x) yet */
  /* We cover the case when EXP(Ai(x))>=-10 */
  else
    assumed_exponent = 10;

  {
    unsigned long int t, u;

    t = assumed_exponent + cond;
    MPFR_ASSERTN (t >= cond);
    u = MPFR_INT_CEIL_LOG2 (prec) + 6;
    t += u;
    MPFR_ASSERTN (t >= u);
    wprec = MPFR_ADD_PREC (prec, t);
  }

  /* We assume that the truncation rank will be ~ prec */
  L = __gmpfr_isqrt (prec);
  MPFR_LOG_MSG (("size of blocks L = %lu\n", L));

  z = (mpfr_t *) mpfr_allocate_func ( (L + 1) * sizeof (mpfr_t) );
  MPFR_ASSERTN (z != NULL);
  for (j=0; j<=L; j++)
    mpfr_init2 (z[j], wprec);

  mpfr_init2 (s, wprec);
  mpfr_init2 (u0, wprec); mpfr_init2 (u1, wprec);
  mpfr_init2 (result, wprec);
  mpfr_init2 (temp1, wprec);
  mpfr_init2 (temp2, wprec);

  /* ZIV loop */
  for (;;)
    {
      MPFR_LOG_MSG (("working precision: %Pu\n", wprec));

      for (j=0; j<=L; j++)
        mpfr_set_prec (z[j], wprec);
      mpfr_set_prec (s, wprec);
      mpfr_set_prec (u0, wprec); mpfr_set_prec (u1, wprec);
      mpfr_set_prec (result, wprec);

      mpfr_set_ui (u0, 1, MPFR_RNDN);
      mpfr_set (u1, x, MPFR_RNDN);

      mpfr_set_ui (z[0], 1, MPFR_RNDU);
      mpfr_sqr (z[1], u1, MPFR_RNDU);
      mpfr_mul (z[1], z[1], x, (MPFR_IS_POS (x) ? MPFR_RNDU : MPFR_RNDD) );

      if (MPFR_IS_NEG (x))
        MPFR_CHANGE_SIGN (z[1]);
      x3u = mpfr_get_ui (z[1], MPFR_RNDU);   /* x3u >= ceil (x^3) */
      if (MPFR_IS_NEG (x))
        MPFR_CHANGE_SIGN (z[1]);

      for (j=2; j<=L ;j++)
        {
          if (j%2 == 0)
            mpfr_sqr (z[j], z[j/2], MPFR_RNDN);
          else
            mpfr_mul (z[j], z[j-1], z[1], MPFR_RNDN);
        }

      mpfr_gamma_one_and_two_third (temp1, temp2, wprec);
      mpfr_set_ui (u0, 9, MPFR_RNDN);
      mpfr_cbrt (u0, u0, MPFR_RNDN);
      mpfr_mul (u0, u0, temp2, MPFR_RNDN);
      mpfr_ui_div (u0, 1, u0 , MPFR_RNDN); /* u0 = 1/( Gamma (2/3)*9^(1/3) ) */

      mpfr_set_ui (u1, 3, MPFR_RNDN);
      mpfr_cbrt (u1, u1, MPFR_RNDN);
      mpfr_mul (u1, u1, temp1, MPFR_RNDN);
      mpfr_neg (u1, u1, MPFR_RNDN);
      mpfr_div (u1, x, u1, MPFR_RNDN); /* u1 = -x/(Gamma (1/3)*3^(1/3)) */

      mpfr_set_ui (result, 0, MPFR_RNDN);
      t = 0;

      /* Evaluation of the series by Smith' method    */
      for (i=0; ; i++)
        {
          t += 3 * L;

          /* k = 0 */
          t -= 3;
          mpfr_set (s, z[L-1], MPFR_RNDN);
          for (j=L-2; ; j--)
            {
              t -= 3;
              mpfr_div_ui2 (s, s, (t+2), (t+3), MPFR_RNDN);
              mpfr_add (s, s, z[j], MPFR_RNDN);
              if (j==0)
                break;
            }
          mpfr_mul (s, s, u0, MPFR_RNDN);
          mpfr_add (result, result, s, MPFR_RNDN);

          mpfr_mul (u0, u0, z[L], MPFR_RNDN);
          for (j=0; j<=L-1; j++)
            {
              mpfr_div_ui2 (u0, u0, (t + 2), (t + 3), MPFR_RNDN);
              t += 3;
            }

          t++;

          /* k = 1 */
          t -= 3;
          mpfr_set (s, z[L-1], MPFR_RNDN);
          for (j=L-2; ; j--)
            {
              t -= 3;
              mpfr_div_ui2 (s, s, (t + 2), (t + 3), MPFR_RNDN);
              mpfr_add (s, s, z[j], MPFR_RNDN);
              if (j==0)
                break;
            }
          mpfr_mul (s, s, u1, MPFR_RNDN);
          mpfr_add (result, result, s, MPFR_RNDN);

          mpfr_mul (u1, u1, z[L], MPFR_RNDN);
          for (j=0; j<=L-1; j++)
            {
              mpfr_div_ui2 (u1, u1, (t + 2), (t + 3), MPFR_RNDN);
              t += 3;
            }

          t++;

          /* k = 2 */
          t++;

          /* End of the loop over k */
          t -= 3;

          test0 = MPFR_IS_ZERO (u0) ||
            MPFR_GET_EXP (u0) + (mpfr_exp_t)prec + 4 <= MPFR_GET_EXP (result);
          test1 = MPFR_IS_ZERO (u1) ||
            MPFR_GET_EXP (u1) + (mpfr_exp_t)prec + 4 <= MPFR_GET_EXP (result);

          if ( test0 && test1 && (x3u <= (t + 2) * (t + 3) / 2) )
            break;
        }

      MPFR_LOG_MSG (("Truncation rank: %lu\n", t));

      err = (5 + MPFR_INT_CEIL_LOG2 (L+1) + MPFR_INT_CEIL_LOG2 (i+1)
             + cond - MPFR_GET_EXP (result));

      /* err is the number of bits lost due to the evaluation error */
      /* wprec-(prec+1): number of bits lost due to the approximation error */
      MPFR_LOG_MSG (("Roundoff error: %Pu\n", err));
      MPFR_LOG_MSG (("Approxim error: %Pu\n", wprec - prec - 1));

      if (wprec < err+1)
        correctBits = 0;
      else
        {
          if (wprec < err+prec+1)
            correctBits = wprec - err - 1;
          else
            correctBits = prec;
        }

      if (MPFR_LIKELY (MPFR_CAN_ROUND (result, correctBits,
                                       MPFR_PREC (y), rnd)))
        break;

      for (j=0; j<=L; j++)
        mpfr_clear (z[j]);
      mpfr_free_func (z, (L + 1) * sizeof (mpfr_t));
      L = __gmpfr_isqrt (t);
      MPFR_LOG_MSG (("size of blocks L = %lu\n", L));
      z = (mpfr_t *) mpfr_allocate_func ( (L + 1) * sizeof (mpfr_t));
      MPFR_ASSERTN (z != NULL);
      for (j=0; j<=L; j++)
        mpfr_init2 (z[j], wprec);

      if (correctBits == 0)
        {
          assumed_exponent *= 2;
          MPFR_LOG_MSG (("Not a single bit correct (assumed_exponent=%lu)\n",
                         assumed_exponent));
          wprec = prec + 6 + MPFR_INT_CEIL_LOG2 (t) + cond + assumed_exponent;
        }
    else
      {
        if (correctBits < prec)
          { /* The precision was badly chosen */
            MPFR_LOG_MSG (("Bad assumption on the exponent of Ai(x)"
                           " (E=%" MPFR_EXP_FSPEC "d)\n",
                           (mpfr_eexp_t) MPFR_GET_EXP (result)));
            wprec = prec + err + 1;
          }
        else
          { /* We are really in a bad case of the TMD */
            MPFR_ZIV_NEXT (loop, prec);

            /* We update wprec */
            /* We assume that t will not be multiplied by more than 4 */
            wprec = (prec + (MPFR_INT_CEIL_LOG2 (t) + 2) + 6 + cond
                     - MPFR_GET_EXP (result));
          }
      }
    } /* End of ZIV loop */

  MPFR_ZIV_FREE (loop);

  r = mpfr_set (y, result, rnd);

  mpfr_clear (tmp_sp);
  mpfr_clear (tmp2_sp);
  for (j=0; j<=L; j++)
    mpfr_clear (z[j]);
  mpfr_free_func (z, (L + 1) * sizeof (mpfr_t));

  mpfr_clear (s);
  mpfr_clear (u0); mpfr_clear (u1);
  mpfr_clear (result);
  mpfr_clear (temp1);
  mpfr_clear (temp2);

  MPFR_SAVE_EXPO_FREE (expo);
  return mpfr_check_range (y, r, rnd);
}

/* We consider that the boundary between the area where the naive method
   should preferably be used and the area where Smith' method should preferably
   be used has the following form:
   it is a triangle defined by two lines (one for the negative values of x, and
   one for the positive values of x) crossing at x=0.

   More precisely,

   * If x<0 and MPFR_AI_THRESHOLD1*x + MPFR_AI_THRESHOLD2*prec > MPFR_AI_SCALE,
   use Smith' algorithm;
   * If x>0 and MPFR_AI_THRESHOLD3*x + MPFR_AI_THRESHOLD2*prec > MPFR_AI_SCALE,
   use Smith' algorithm;
   * otherwise, use the naive method.
*/

#define MPFR_AI_SCALE 1048576

int
mpfr_ai (mpfr_ptr y, mpfr_srcptr x, mpfr_rnd_t rnd)
{
  mpfr_t temp1, temp2;
  int use_ai2;
  MPFR_SAVE_EXPO_DECL (expo);

  /* Special cases */
  if (MPFR_UNLIKELY (MPFR_IS_SINGULAR (x)))
    {
      if (MPFR_IS_NAN (x))
        {
          MPFR_SET_NAN (y);
          MPFR_RET_NAN;
        }
      else if (MPFR_IS_INF (x))
        return mpfr_set_ui (y, 0, rnd);
      /* the cases x = +0 or -0 will be treated below */
    }

  /* The exponent range must be large enough for the computation of temp1. */
  MPFR_SAVE_EXPO_MARK (expo);

  mpfr_init2 (temp1, MPFR_SMALL_PRECISION);
  mpfr_init2 (temp2, MPFR_SMALL_PRECISION);

  mpfr_set (temp1, x, MPFR_RNDN);
  mpfr_set_si (temp2, MPFR_AI_THRESHOLD2, MPFR_RNDN);
  mpfr_mul_ui (temp2, temp2, MPFR_PREC (y) > ULONG_MAX ?
               ULONG_MAX : (unsigned long) MPFR_PREC (y), MPFR_RNDN);

  if (MPFR_IS_NEG (x))
      mpfr_mul_si (temp1, temp1, MPFR_AI_THRESHOLD1, MPFR_RNDN);
  else
      mpfr_mul_si (temp1, temp1, MPFR_AI_THRESHOLD3, MPFR_RNDN);

  mpfr_add (temp1, temp1, temp2, MPFR_RNDN);
  mpfr_clear (temp2);

  use_ai2 = mpfr_cmp_si (temp1, MPFR_AI_SCALE) > 0;
  mpfr_clear (temp1);

  MPFR_SAVE_EXPO_FREE (expo); /* Ignore all previous exceptions. */

  /* we use ai2 if |x|*AI_THRESHOLD1/3 + PREC(y)*AI_THRESHOLD2 > AI_SCALE,
     which means x cannot be zero in mpfr_ai2 */
  return use_ai2 ? mpfr_ai2 (y, x, rnd) : mpfr_ai1 (y, x, rnd);
}