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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
// Copyright © 2017 University of Malta

// This program 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.
//
// This program 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
// General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// this program. If not, see <http://www.gnu.org/licenses/>.

#![allow(non_camel_case_types, non_snake_case, non_upper_case_globals)]

macro_rules! c_fn {
    { $($c:ident
        $( ($($par:ident: $ty:ty),* $(; $dots:tt)*) $(-> $ret:ty)*
        )* ;
    )* } => {
        $(
            $(
                #[link(name = "mpfr", kind = "static")]
                extern "C" {
                    pub fn $c($($par: $ty),* $(, $dots)*) $(-> $ret)*;
                }
            )*
        )*
    };
}

use ::gmp::*;
use std::os::raw::{c_char, c_int, c_long, c_ulong, c_void};

type mpz_srcptr = *const mpz_t;
type mpz_ptr = *mut mpz_t;
type mpq_srcptr = *const mpq_t;
type mpf_srcptr = *const mpf_t;
type mpf_ptr = *mut mpf_t;
type randstate_ptr = *mut gmp_randstate_t;

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub enum mpfr_rnd_t {
    MPFR_RNDN = 0,
    MPFR_RNDZ = 1,
    MPFR_RNDU = 2,
    MPFR_RNDD = 3,
    MPFR_RNDA = 4,
    MPFR_RNDF = 5,
    MPFR_RNDNA = -1,
}
pub const GMP_RNDN: mpfr_rnd_t = mpfr_rnd_t::MPFR_RNDN;
pub const GMP_RNDZ: mpfr_rnd_t = mpfr_rnd_t::MPFR_RNDZ;
pub const GMP_RNDU: mpfr_rnd_t = mpfr_rnd_t::MPFR_RNDU;
pub const GMP_RNDD: mpfr_rnd_t = mpfr_rnd_t::MPFR_RNDD;

pub type mpfr_prec_t = c_long;
pub type mpfr_uprec_t = c_ulong;
pub type mpfr_sign_t = c_int;
pub type mpfr_exp_t = c_long;
pub type mpfr_uexp_t = c_ulong;

pub const MPFR_PREC_MIN: mpfr_prec_t = 2;
pub const MPFR_PREC_MAX: mpfr_prec_t = (!(0 as mpfr_uprec_t) >> 1) as
                                       mpfr_prec_t;

pub const MPFR_EMAX_DEFAULT: mpfr_exp_t = ((1 << 30) - 1) as mpfr_exp_t;
pub const MPFR_EMIN_DEFAULT: mpfr_exp_t = -MPFR_EMAX_DEFAULT;

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct __mpfr_struct {
    pub _mpfr_prec: mpfr_prec_t,
    pub _mpfr_sign: mpfr_sign_t,
    pub _mpfr_exp: mpfr_exp_t,
    pub _mpfr_d: *mut mp_limb_t,
}
pub type mpfr_t = __mpfr_struct;

type mpfr_ptr = *mut __mpfr_struct;
type mpfr_srcptr = *const __mpfr_struct;

#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub enum mpfr_kind_t {
    MPFR_NAN_KIND = 0,
    MPFR_INF_KIND = 1,
    MPFR_ZERO_KIND = 2,
    MPFR_REGULAR_KIND = 3,
}

c_fn! {
    // Initialization Functions
    mpfr_init2(x: mpfr_ptr, prec: mpfr_prec_t);
    mpfr_inits2(prec: mpfr_prec_t, x: mpfr_ptr; ...);
    mpfr_clear(x: mpfr_ptr);
    mpfr_clears(x: mpfr_ptr; ...);
    mpfr_init(x: mpfr_ptr);
    mpfr_inits(x: mpfr_ptr; ...);
    mpfr_set_default_prec(prec: mpfr_prec_t);
    mpfr_get_default_prec() -> mpfr_prec_t;
    mpfr_set_prec(x: mpfr_ptr, prec: mpfr_prec_t);
    mpfr_get_prec(x: mpfr_srcptr) -> mpfr_prec_t;

    // Assignment Functions
    mpfr_set(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_set_ui(rop: mpfr_ptr, op: c_ulong, rnd: mpfr_rnd_t) -> c_int;
    mpfr_set_si(rop: mpfr_ptr, op: c_long, rnd: mpfr_rnd_t) -> c_int;
    mpfr_set_flt(rop: mpfr_ptr, op: f32, rnd: mpfr_rnd_t) -> c_int;
    mpfr_set_d(rop: mpfr_ptr, op: f64, rnd: mpfr_rnd_t) -> c_int;
    mpfr_set_ld(rop: mpfr_ptr, op: f64, rnd: mpfr_rnd_t) -> c_int;
    mpfr_set_z(rop: mpfr_ptr, op: mpz_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_set_q(rop: mpfr_ptr, op: mpq_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_set_f(rop: mpfr_ptr, op: mpf_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_set_ui_2exp(rop: mpfr_ptr,
                     op: c_ulong,
                     e: mpfr_exp_t,
                     rnd: mpfr_rnd_t)
                     -> c_int;
    mpfr_set_si_2exp(rop: mpfr_ptr,
                     op: c_long,
                     e: mpfr_exp_t,
                     rnd: mpfr_rnd_t)
                     -> c_int;
    mpfr_set_z_2exp(rop: mpfr_ptr,
                    op: mpz_srcptr,
                    e: mpfr_exp_t,
                    rnd: mpfr_rnd_t)
                    -> c_int;
    mpfr_set_str(rop: mpfr_ptr,
                 s: *const c_char,
                 base: c_int,
                 rnd: mpfr_rnd_t)
                 -> c_int;
    mpfr_strtofr(rop: mpfr_ptr,
                 nptr: *const c_char,
                 endptr: *mut *mut c_char,
                 base: c_int,
                 rnd: mpfr_rnd_t)
                 -> c_int;
    mpfr_set_nan(x: mpfr_ptr);
    mpfr_set_inf(x: mpfr_ptr, sign: c_int);
    mpfr_set_zero(x: mpfr_ptr, sign: c_int);
    mpfr_swap(x: mpfr_ptr, y: mpfr_ptr);

    // Combined Initialization and Assignment Functions
}
#[inline]
pub unsafe fn mpfr_init_set(rop: mpfr_ptr,
                            op: mpfr_srcptr,
                            rnd: mpfr_rnd_t)
                            -> c_int {
    mpfr_init(rop);
    mpfr_set(rop, op, rnd)
}
#[inline]
pub unsafe fn mpfr_init_set_ui(rop: mpfr_ptr,
                               op: c_ulong,
                               rnd: mpfr_rnd_t)
                               -> c_int {
    mpfr_init(rop);
    mpfr_set_ui(rop, op, rnd)
}
#[inline]
pub unsafe fn mpfr_init_set_si(rop: mpfr_ptr,
                               op: c_long,
                               rnd: mpfr_rnd_t)
                               -> c_int {
    mpfr_init(rop);
    mpfr_set_si(rop, op, rnd)
}
#[inline]
pub unsafe fn mpfr_init_set_d(rop: mpfr_ptr,
                              op: f64,
                              rnd: mpfr_rnd_t)
                              -> c_int {
    mpfr_init(rop);
    mpfr_set_d(rop, op, rnd)
}
#[inline]
pub unsafe fn mpfr_init_set_ld(rop: mpfr_ptr,
                               op: f64,
                               rnd: mpfr_rnd_t)
                               -> c_int {
    mpfr_init(rop);
    mpfr_set_ld(rop, op, rnd)
}
#[inline]
pub unsafe fn mpfr_init_set_z(rop: mpfr_ptr,
                              op: mpz_srcptr,
                              rnd: mpfr_rnd_t)
                              -> c_int {
    mpfr_init(rop);
    mpfr_set_z(rop, op, rnd)
}
#[inline]
pub unsafe fn mpfr_init_set_q(rop: mpfr_ptr,
                              op: mpq_srcptr,
                              rnd: mpfr_rnd_t)
                              -> c_int {
    mpfr_init(rop);
    mpfr_set_q(rop, op, rnd)
}
#[inline]
pub unsafe fn mpfr_init_set_f(rop: mpfr_ptr,
                              op: mpf_srcptr,
                              rnd: mpfr_rnd_t)
                              -> c_int {
    mpfr_init(rop);
    mpfr_set_f(rop, op, rnd)
}
c_fn! {
    mpfr_init_set_str(x: mpfr_ptr,
                      s: *const c_char,
                      base: c_int,
                      rnd: mpfr_rnd_t)
                      -> c_int;

    // Conversion Functions
    mpfr_get_flt(op: mpfr_srcptr, rnd: mpfr_rnd_t) -> f32;
    mpfr_get_d(op: mpfr_srcptr, rnd: mpfr_rnd_t) -> f64;
    mpfr_get_ld(op: mpfr_srcptr, rnd: mpfr_rnd_t) -> f64;
    mpfr_get_si(op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_long;
    mpfr_get_ui(op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_ulong;
    mpfr_get_d_2exp(exp: *mut c_long, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> f64;
    mpfr_get_ld_2exp(exp: *mut c_long, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> f64;
    mpfr_frexp(exp: *mut mpfr_exp_t,
               y: mpfr_ptr,
               x: mpfr_srcptr,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_get_z_2exp(rop: mpz_ptr, op: mpfr_srcptr) -> mpfr_exp_t;
    mpfr_get_z(z: mpz_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_get_f(rop: mpf_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_get_str(str: *mut c_char,
                 expptr: *mut mpfr_exp_t,
                 b: c_int,
                 n: usize,
                 op: mpfr_srcptr,
                 rnd: mpfr_rnd_t)
                 -> *mut c_char;
    mpfr_free_str(str: *mut c_char);
    mpfr_fits_ulong_p(op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_fits_slong_p(op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_fits_uint_p(op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_fits_sint_p(op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_fits_ushort_p(op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_fits_sshort_p(op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_fits_uintmax_p(op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_fits_intmax_p(op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;

    // Basic Arithmetic Functions
    mpfr_add(rop: mpfr_ptr,
             op1: mpfr_srcptr,
             op2: mpfr_srcptr,
             rnd: mpfr_rnd_t)
             -> c_int;
    mpfr_add_ui(rop: mpfr_ptr,
                op1: mpfr_srcptr,
                op2: c_ulong,
                rnd: mpfr_rnd_t)
                -> c_int;
    mpfr_add_si(rop: mpfr_ptr,
                op1: mpfr_srcptr,
                op2: c_long,
                rnd: mpfr_rnd_t)
                -> c_int;
    mpfr_add_d(rop: mpfr_ptr,
               op1: mpfr_srcptr,
               op2: f64,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_add_z(rop: mpfr_ptr,
               op1: mpfr_srcptr,
               op2: mpz_srcptr,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_add_q(rop: mpfr_ptr,
               op1: mpfr_srcptr,
               op2: mpq_srcptr,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_sub(rop: mpfr_ptr,
             op1: mpfr_srcptr,
             op2: mpfr_srcptr,
             rnd: mpfr_rnd_t)
             -> c_int;
    mpfr_ui_sub(rop: mpfr_ptr,
                op1: c_ulong,
                op2: mpfr_srcptr,
                rnd: mpfr_rnd_t)
                -> c_int;
    mpfr_sub_ui(rop: mpfr_ptr,
                op1: mpfr_srcptr,
                op2: c_ulong,
                rnd: mpfr_rnd_t)
                -> c_int;
    mpfr_si_sub(rop: mpfr_ptr,
                op1: c_long,
                op2: mpfr_srcptr,
                rnd: mpfr_rnd_t)
                -> c_int;
    mpfr_sub_si(rop: mpfr_ptr,
                op1: mpfr_srcptr,
                op2: c_long,
                rnd: mpfr_rnd_t)
                -> c_int;
    mpfr_d_sub(rop: mpfr_ptr,
               op1: f64,
               op2: mpfr_srcptr,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_sub_d(rop: mpfr_ptr,
               op1: mpfr_srcptr,
               op2: f64,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_z_sub(rop: mpfr_ptr,
               op1: mpz_srcptr,
               op2: mpfr_srcptr,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_sub_z(rop: mpfr_ptr,
               op1: mpfr_srcptr,
               op2: mpz_srcptr,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_sub_q(rop: mpfr_ptr,
               op1: mpfr_srcptr,
               op2: mpq_srcptr,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_mul(rop: mpfr_ptr,
             op1: mpfr_srcptr,
             op2: mpfr_srcptr,
             rnd: mpfr_rnd_t)
             -> c_int;
    mpfr_mul_ui(rop: mpfr_ptr,
                op1: mpfr_srcptr,
                op2: c_ulong,
                rnd: mpfr_rnd_t)
                -> c_int;
    mpfr_mul_si(rop: mpfr_ptr,
                op1: mpfr_srcptr,
                op2: c_long,
                rnd: mpfr_rnd_t)
                -> c_int;
    mpfr_mul_d(rop: mpfr_ptr,
               op1: mpfr_srcptr,
               op2: f64,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_mul_z(rop: mpfr_ptr,
               op1: mpfr_srcptr,
               op2: mpz_srcptr,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_mul_q(rop: mpfr_ptr,
               op1: mpfr_srcptr,
               op2: mpq_srcptr,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_sqr(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_div(rop: mpfr_ptr,
             op1: mpfr_srcptr,
             op2: mpfr_srcptr,
             rnd: mpfr_rnd_t)
             -> c_int;
    mpfr_ui_div(rop: mpfr_ptr,
                op1: c_ulong,
                op2: mpfr_srcptr,
                rnd: mpfr_rnd_t)
                -> c_int;
    mpfr_div_ui(rop: mpfr_ptr,
                op1: mpfr_srcptr,
                op2: c_ulong,
                rnd: mpfr_rnd_t)
                -> c_int;
    mpfr_si_div(rop: mpfr_ptr,
                op1: c_long,
                op2: mpfr_srcptr,
                rnd: mpfr_rnd_t)
                -> c_int;
    mpfr_div_si(rop: mpfr_ptr,
                op1: mpfr_srcptr,
                op2: c_long,
                rnd: mpfr_rnd_t)
                -> c_int;
    mpfr_d_div(rop: mpfr_ptr,
               op1: f64,
               op2: mpfr_srcptr,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_div_d(rop: mpfr_ptr,
               op1: mpfr_srcptr,
               op2: f64,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_div_z(rop: mpfr_ptr,
               op1: mpfr_srcptr,
               op2: mpz_srcptr,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_div_q(rop: mpfr_ptr,
               op1: mpfr_srcptr,
               op2: mpq_srcptr,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_sqrt(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_sqrt_ui(rop: mpfr_ptr, op: c_ulong, rnd: mpfr_rnd_t) -> c_int;
    mpfr_rec_sqrt(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_cbrt(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_root(rop: mpfr_ptr,
              op: mpfr_srcptr,
              k: c_ulong,
              rnd: mpfr_rnd_t)
              -> c_int;
    mpfr_pow(rop: mpfr_ptr,
             op1: mpfr_srcptr,
             op2: mpfr_srcptr,
             rnd: mpfr_rnd_t)
             -> c_int;
    mpfr_pow_ui(rop: mpfr_ptr,
                op1: mpfr_srcptr,
                op2: c_ulong,
                rnd: mpfr_rnd_t)
                -> c_int;
    mpfr_pow_si(rop: mpfr_ptr,
                op1: mpfr_srcptr,
                op2: c_long,
                rnd: mpfr_rnd_t)
                -> c_int;
    mpfr_pow_z(rop: mpfr_ptr,
               op1: mpfr_srcptr,
               op2: mpz_srcptr,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_ui_pow_ui(rop: mpfr_ptr,
                   op1: c_ulong,
                   op2: c_ulong,
                   rnd: mpfr_rnd_t)
                   -> c_int;
    mpfr_ui_pow(rop: mpfr_ptr,
                op1: c_ulong,
                op2: mpfr_srcptr,
                rnd: mpfr_rnd_t)
                -> c_int;
    mpfr_neg(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_abs(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_dim(rop: mpfr_ptr,
             op1: mpfr_srcptr,
             op2: mpfr_srcptr,
             rnd: mpfr_rnd_t)
             -> c_int;
    mpfr_mul_2ui(rop: mpfr_ptr,
                 op1: mpfr_srcptr,
                 op2: c_ulong,
                 rnd: mpfr_rnd_t)
                 -> c_int;
    mpfr_mul_2si(rop: mpfr_ptr,
                 op1: mpfr_srcptr,
                 op2: c_long,
                 rnd: mpfr_rnd_t)
                 -> c_int;
    mpfr_div_2ui(rop: mpfr_ptr,
                 op1: mpfr_srcptr,
                 op2: c_ulong,
                 rnd: mpfr_rnd_t)
                 -> c_int;
    mpfr_div_2si(rop: mpfr_ptr,
                 op1: mpfr_srcptr,
                 op2: c_long,
                 rnd: mpfr_rnd_t)
                 -> c_int;

    // Comparison Functions
    mpfr_cmp(op1: mpfr_srcptr, op2: mpfr_srcptr) -> c_int;
    mpfr_cmp_ui(op1: mpfr_srcptr, op2: c_ulong) -> c_int;
    mpfr_cmp_si(op1: mpfr_srcptr, op2: c_long) -> c_int;
    mpfr_cmp_d(op1: mpfr_srcptr, op2: f64) -> c_int;
    mpfr_cmp_ld(op1: mpfr_srcptr, op2: f64) -> c_int;
    mpfr_cmp_z(op1: mpfr_srcptr, op2: mpz_srcptr) -> c_int;
    mpfr_cmp_q(op1: mpfr_srcptr, op2: mpq_srcptr) -> c_int;
    mpfr_cmp_f(op1: mpfr_srcptr, op2: mpf_srcptr) -> c_int;
    mpfr_cmp_ui_2exp(op1: mpfr_srcptr, op2: c_ulong, e: mpfr_exp_t) -> c_int;
    mpfr_cmp_si_2exp(op1: mpfr_srcptr, op2: c_long, e: mpfr_exp_t) -> c_int;
    mpfr_cmpabs(op1: mpfr_srcptr, op2: mpfr_srcptr) -> c_int;
    mpfr_nan_p(op: mpfr_srcptr) -> c_int;
    mpfr_inf_p(op: mpfr_srcptr) -> c_int;
    mpfr_number_p(op: mpfr_srcptr) -> c_int;
    mpfr_zero_p(op: mpfr_srcptr) -> c_int;
    mpfr_regular_p(op: mpfr_srcptr) -> c_int;
    mpfr_sgn(op: mpfr_srcptr) -> c_int;
    mpfr_greater_p(op1: mpfr_srcptr, op2: mpfr_srcptr) -> c_int;
    mpfr_greaterequal_p(op1: mpfr_srcptr, op2: mpfr_srcptr) -> c_int;
    mpfr_less_p(op1: mpfr_srcptr, op2: mpfr_srcptr) -> c_int;
    mpfr_lessequal_p(op1: mpfr_srcptr, op2: mpfr_srcptr) -> c_int;
    mpfr_equal_p(op1: mpfr_srcptr, op2: mpfr_srcptr) -> c_int;
    mpfr_lessgreater_p(op1: mpfr_srcptr, op2: mpfr_srcptr) -> c_int;
    mpfr_unordered_p(op1: mpfr_srcptr, op2: mpfr_srcptr) -> c_int;

    // Special Functions
    mpfr_log(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_log2(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_log10(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_exp(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_exp2(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_exp10(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_cos(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_sin(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_tan(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_sin_cos(sop: mpfr_ptr,
                 cop: mpfr_ptr,
                 op: mpfr_srcptr,
                 rnd: mpfr_rnd_t)
                 -> c_int;
    mpfr_sec(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_csc(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_cot(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_acos(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_asin(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_atan(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_atan2(rop: mpfr_ptr,
               y: mpfr_srcptr,
               x: mpfr_srcptr,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_cosh(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_sinh(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_tanh(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_sinh_cosh(sop: mpfr_ptr,
                   cop: mpfr_ptr,
                   op: mpfr_srcptr,
                   rnd: mpfr_rnd_t)
                   -> c_int;
    mpfr_sech(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_csch(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_coth(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_acosh(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_asinh(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_atanh(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_fac_ui(rop: mpfr_ptr, op: c_ulong, rnd: mpfr_rnd_t) -> c_int;
    mpfr_log1p(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_expm1(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_eint(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_li2(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_gamma(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_lngamma(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_lgamma(rop: mpfr_ptr,
                signp: *mut c_int,
                op: mpfr_srcptr,
                rnd: mpfr_rnd_t)
                -> c_int;
    mpfr_digamma(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_zeta(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_zeta_ui(rop: mpfr_ptr, op: c_ulong, rnd: mpfr_rnd_t) -> c_int;
    mpfr_erf(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_erfc(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_j0(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_j1(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_jn(rop: mpfr_ptr,
            n: c_long,
            op: mpfr_srcptr,
            rnd: mpfr_rnd_t)
            -> c_int;
    mpfr_y0(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_y1(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_yn(rop: mpfr_ptr,
            n: c_long,
            op: mpfr_srcptr,
            rnd: mpfr_rnd_t)
            -> c_int;
    mpfr_fma(rop: mpfr_ptr,
             op1: mpfr_srcptr,
             op2: mpfr_srcptr,
             op3: mpfr_srcptr,
             rnd: mpfr_rnd_t)
             -> c_int;
    mpfr_fms(rop: mpfr_ptr,
             op1: mpfr_srcptr,
             op2: mpfr_srcptr,
             op3: mpfr_srcptr,
             rnd: mpfr_rnd_t)
             -> c_int;
    mpfr_agm(rop: mpfr_ptr,
             op1: mpfr_srcptr,
             op2: mpfr_srcptr,
             rnd: mpfr_rnd_t)
             -> c_int;
    mpfr_hypot(rop: mpfr_ptr,
               x: mpfr_srcptr,
               y: mpfr_srcptr,
               rnd: mpfr_rnd_t)
               -> c_int;
    mpfr_ai(rop: mpfr_ptr, x: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_const_log2(rop: mpfr_ptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_const_pi(rop: mpfr_ptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_const_euler(rop: mpfr_ptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_const_catalan(rop: mpfr_ptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_free_cache();
    mpfr_sum(rop: mpfr_ptr,
             tab: *mut mpfr_ptr,
             n: c_ulong,
             rnd: mpfr_rnd_t)
             -> c_int;

    // Formatted Output Functions
    mpfr_printf(template: *const c_char; ...) -> c_int;
    mpfr_sprintf(buf: *mut c_char, template: *const c_char; ...) -> c_int;
    mpfr_snprintf(buf: *mut c_char,
                  n: usize,
                  template: *const c_char;
                  ...)
                  -> c_int;
    mpfr_asprintf(str: *mut *mut c_char, template: *const c_char; ...) -> c_int;

    // Integer and Remainder Related Functions
    mpfr_rint(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_ceil(rop: mpfr_ptr, op: mpfr_srcptr) -> c_int;
    mpfr_floor(rop: mpfr_ptr, op: mpfr_srcptr) -> c_int;
    mpfr_round(rop: mpfr_ptr, op: mpfr_srcptr) -> c_int;
    mpfr_trunc(rop: mpfr_ptr, op: mpfr_srcptr) -> c_int;
    mpfr_rint_ceil(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_rint_floor(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_rint_round(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_rint_trunc(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_frac(rop: mpfr_ptr, op: mpfr_srcptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_modf(iop: mpfr_ptr,
              fop: mpfr_ptr,
              op: mpfr_srcptr,
              rnd: mpfr_rnd_t)
              -> c_int;
    mpfr_fmod(r: mpfr_ptr,
              x: mpfr_srcptr,
              y: mpfr_srcptr,
              rnd: mpfr_rnd_t)
              -> c_int;
    mpfr_remainder(r: mpfr_ptr,
                   x: mpfr_srcptr,
                   y: mpfr_srcptr,
                   rnd: mpfr_rnd_t)
                   -> c_int;
    mpfr_remquo(r: mpfr_ptr,
                q: *mut c_long,
                x: mpfr_srcptr,
                y: mpfr_srcptr,
                rnd: mpfr_rnd_t)
                -> c_int;
    mpfr_integer_p(op: mpfr_srcptr) -> c_int;

    // Rounding Related Functions
    mpfr_set_default_rounding_mode(rnd: mpfr_rnd_t);
    mpfr_get_default_rounding_mode() -> mpfr_rnd_t;
    mpfr_prec_round(x: mpfr_ptr, prec: mpfr_prec_t, rnd: mpfr_rnd_t) -> c_int;
    mpfr_can_round(b: mpfr_srcptr,
                   err: mpfr_exp_t,
                   rnd1: mpfr_rnd_t,
                   rnd2: mpfr_rnd_t,
                   prec: mpfr_prec_t)
                   -> c_int;
    mpfr_min_prec(x: mpfr_srcptr) -> mpfr_prec_t;
    mpfr_print_rnd_mode(rnd: mpfr_rnd_t) -> *const c_char;

    // Miscellaneous Functions
    mpfr_nexttoward(x: mpfr_ptr, y: mpfr_srcptr);
    mpfr_nextabove(x: mpfr_ptr);
    mpfr_nextbelow(x: mpfr_ptr);
    mpfr_min(rop: mpfr_ptr,
             op1: mpfr_srcptr,
             op2: mpfr_srcptr,
             rnd: mpfr_rnd_t)
             -> c_int;
    mpfr_max(rop: mpfr_ptr,
             op1: mpfr_srcptr,
             op2: mpfr_srcptr,
             rnd: mpfr_rnd_t)
             -> c_int;
    mpfr_urandomb(rop: mpfr_ptr, state: randstate_ptr) -> c_int;
    mpfr_urandom(rop: mpfr_ptr, state: randstate_ptr, rnd: mpfr_rnd_t) -> c_int;
    mpfr_grandom(rop1: mpfr_ptr,
                 rop2: mpfr_ptr,
                 state: randstate_ptr,
                 rnd: mpfr_rnd_t)
                 -> c_int;
    mpfr_get_exp(x: mpfr_srcptr) -> mpfr_exp_t;
    mpfr_set_exp(x: mpfr_ptr, e: mpfr_exp_t) -> c_int;
    mpfr_signbit(op: mpfr_srcptr) -> c_int;
    mpfr_setsign(rop: mpfr_ptr,
                 op: mpfr_srcptr,
                 s: c_int,
                 rnd: mpfr_rnd_t)
                 -> c_int;
    mpfr_copysign(rop: mpfr_ptr,
                  op1: mpfr_srcptr,
                  op2: mpfr_srcptr,
                  rnd: mpfr_rnd_t)
                  -> c_int;
    mpfr_get_version() -> *const c_char;
}
pub const MPFR_VERSION: c_int = (MPFR_VERSION_MAJOR << 16) |
                                (MPFR_VERSION_MINOR << 8) |
                                MPFR_VERSION_PATCHLEVEL;
pub const MPFR_VERSION_MAJOR: c_int = 3;
pub const MPFR_VERSION_MINOR: c_int = 1;
pub const MPFR_VERSION_PATCHLEVEL: c_int = 5;
const MPFR_VERSION_BUFFER: &'static [u8] = b"3.1.5\0";
pub const MPFR_VERSION_STRING: *const c_char =
    &MPFR_VERSION_BUFFER[0] as *const _ as *const c_char;
#[inline]
pub fn MPFR_VERSION_NUM(major: c_int,
                        minor: c_int,
                        patchlevel: c_int)
                        -> c_int {
    (major << 16) | (minor << 8) | patchlevel
}
c_fn! {
    mpfr_get_patches() -> *const c_char;
    mpfr_buildopt_tls_p() -> c_int;
    mpfr_buildopt_decimal_p() -> c_int;
    mpfr_buildopt_gmpinternals_p() -> c_int;
    mpfr_buildopt_tune_case() -> *const c_char;

    // Exception Related Functions
    mpfr_get_emin() -> mpfr_exp_t;
    mpfr_get_emax() -> mpfr_exp_t;
    mpfr_set_emin(exp: mpfr_exp_t) -> c_int;
    mpfr_set_emax(exp: mpfr_exp_t) -> c_int;
    mpfr_get_emin_min() -> mpfr_exp_t;
    mpfr_get_emin_max() -> mpfr_exp_t;
    mpfr_get_emax_min() -> mpfr_exp_t;
    mpfr_get_emax_max() -> mpfr_exp_t;
    mpfr_check_range(x: mpfr_ptr, t: c_int, rnd: mpfr_rnd_t) -> c_int;
    mpfr_subnormalize(x: mpfr_ptr, t: c_int, rnd: mpfr_rnd_t) -> c_int;
    mpfr_clear_underflow();
    mpfr_clear_overflow();
    mpfr_clear_divby0();
    mpfr_clear_nanflag();
    mpfr_clear_inexflag();
    mpfr_clear_erangeflag();
    mpfr_set_underflow();
    mpfr_set_overflow();
    mpfr_set_divby0();
    mpfr_set_nanflag();
    mpfr_set_inexflag();
    mpfr_set_erangeflag();
    mpfr_clear_flags();
    mpfr_underflow_p() -> c_int;
    mpfr_overflow_p() -> c_int;
    mpfr_divby0_p() -> c_int;
    mpfr_nanflag_p() -> c_int;
    mpfr_inexflag_p() -> c_int;
    mpfr_erangeflag_p() -> c_int;

    // Compatibility with MPF
    mpfr_set_prec_raw(x: mpfr_ptr, prec: mpfr_prec_t);
    mpfr_eq(op1: mpfr_srcptr, op2: mpfr_srcptr, op3: c_ulong) -> c_int;
    mpfr_reldiff(rop: mpfr_ptr,
                 op1: mpfr_srcptr,
                 op2: mpfr_srcptr,
                 rnd: mpfr_rnd_t);
    mpfr_mul_2exp(rop: mpfr_ptr,
                  op1: mpfr_srcptr,
                  op2: c_ulong,
                  rnd: mpfr_rnd_t)
                  -> c_int;
    mpfr_div_2exp(rop: mpfr_ptr,
                  op1: mpfr_srcptr,
                  op2: c_ulong,
                  rnd: mpfr_rnd_t)
                  -> c_int;

    // Custom Interface
    mpfr_custom_get_size(prec: mpfr_prec_t) -> usize;
    mpfr_custom_init(significand: *mut c_void, prec: mpfr_prec_t);
    mpfr_custom_init_set(x: mpfr_ptr,
                         kind: c_int,
                         exp: mpfr_exp_t,
                         prec: mpfr_prec_t,
                         significand: *mut c_void);
    mpfr_custom_get_kind(x: mpfr_srcptr) -> c_int;
    mpfr_custom_get_significand(x: mpfr_srcptr) -> *mut c_void;
    mpfr_custom_get_exp(x: mpfr_srcptr) -> mpfr_exp_t;
    mpfr_custom_move(x: mpfr_ptr, new_position: *mut c_void);
}