gmp_mpfr_sys/
gmp.rs

1// Copyright © 2017–2025 Trevor Spiteri
2
3// This program is free software: you can redistribute it and/or
4// modify it under the terms of the GNU Lesser General Public License
5// as published by the Free Software Foundation, either version 3 of
6// the License, or (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful, but
9// WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11// General Public License for more details.
12//
13// You should have received a copy of the GNU Lesser General Public
14// License and a copy of the GNU General Public License along with
15// this program. If not, see <https://www.gnu.org/licenses/>.
16
17/*!
18Function and type bindings for the [GMP] library.
19
20# Examples
21
22```rust
23use core::mem::MaybeUninit;
24use gmp_mpfr_sys::gmp;
25unsafe {
26    let mut z = {
27        let mut z = MaybeUninit::uninit();
28        gmp::mpz_init(z.as_mut_ptr());
29        z.assume_init()
30    };
31    gmp::mpz_set_ui(&mut z, 15);
32    let u = gmp::mpz_get_ui(&z);
33    assert_eq!(u, 15);
34    gmp::mpz_clear(&mut z);
35}
36```
37
38[GMP]: https://gmplib.org/
39*/
40#![allow(non_camel_case_types, non_snake_case)]
41
42use core::cmp::Ordering;
43use core::ffi::{c_char, c_int, c_long, c_uchar, c_uint, c_ulong, c_ushort, c_void};
44use core::fmt::{Debug, Formatter, Result as FmtResult};
45use core::mem::MaybeUninit;
46use core::ptr;
47use core::ptr::NonNull;
48use libc::FILE;
49
50include!(concat!(env!("OUT_DIR"), "/gmp_h.rs"));
51
52extern "C" {
53    /// See: [`mp_bits_per_limb`](../C/GMP/constant.GMP_Basics.html#index-mp_005fbits_005fper_005flimb)
54    #[link_name = "__gmp_bits_per_limb"]
55    pub static bits_per_limb: c_int;
56}
57/// See: [`__GNU_MP_VERSION`](../C/GMP/constant.GMP_Basics.html#index-_005f_005fGNU_005fMP_005fVERSION)
58pub const VERSION: c_int = GMP_VERSION;
59/// See: [`__GNU_MP_VERSION_MINOR`](../C/GMP/constant.GMP_Basics.html#index-_005f_005fGNU_005fMP_005fVERSION_005fMINOR)
60pub const VERSION_MINOR: c_int = GMP_VERSION_MINOR;
61/// See: [`__GNU_MP_VERSION_PATCHLEVEL`](../C/GMP/constant.GMP_Basics.html#index-_005f_005fGNU_005fMP_005fVERSION_005fPATCHLEVEL)
62pub const VERSION_PATCHLEVEL: c_int = GMP_VERSION_PATCHLEVEL;
63extern "C" {
64    /// See: [`gmp_version`](../C/GMP/constant.GMP_Basics.html#index-gmp_005fversion)
65    #[link_name = "__gmp_version"]
66    pub static version: *const c_char;
67}
68/// See: [`__GMP_CC`](../C/GMP/constant.GMP_Basics.html#index-_005f_005fGMP_005fCC)
69pub const CC: *const c_char = GMP_CC;
70/// See: [`__GMP_CFLAGS`](../C/GMP/constant.GMP_Basics.html#index-_005f_005fGMP_005fCFLAGS)
71pub const CFLAGS: *const c_char = GMP_CFLAGS;
72
73/// See: [`GMP_NAIL_BITS`](../C/GMP/constant.Low_level_Functions.html#index-GMP_005fNAIL_005fBITS)
74pub const NAIL_BITS: c_int = GMP_NAIL_BITS;
75/// See: [`GMP_NUMB_BITS`](../C/GMP/constant.Low_level_Functions.html#index-GMP_005fNUMB_005fBITS)
76pub const NUMB_BITS: c_int = LIMB_BITS - NAIL_BITS;
77/// See: [`GMP_LIMB_BITS`](../C/GMP/constant.Low_level_Functions.html#index-GMP_005fLIMB_005fBITS)
78pub const LIMB_BITS: c_int = GMP_LIMB_BITS;
79/// See: [`GMP_NAIL_MASK`](../C/GMP/constant.Low_level_Functions.html#index-GMP_005fNAIL_005fMASK)
80pub const NAIL_MASK: limb_t = !NUMB_MASK;
81/// See: [`GMP_NUMB_MASK`](../C/GMP/constant.Low_level_Functions.html#index-GMP_005fNUMB_005fMASK)
82pub const NUMB_MASK: limb_t = (!(0 as limb_t)) >> NAIL_BITS;
83/// See: [`GMP_NUMB_MAX`](../C/GMP/constant.Low_level_Functions.html#index-GMP_005fNUMB_005fMAX)
84pub const NUMB_MAX: limb_t = NUMB_MASK;
85
86/// See: [`mp_exp_t`](../C/GMP/constant.GMP_Basics.html#index-mp_005fexp_005ft)
87pub type exp_t = c_long;
88/// See: [`mp_limb_t`](../C/GMP/constant.GMP_Basics.html#index-mp_005flimb_005ft)
89pub type limb_t = GMP_LIMB_T;
90/// See: [`mp_size_t`](../C/GMP/constant.GMP_Basics.html#index-mp_005fsize_005ft)
91pub type size_t = c_long;
92/// See: [`mp_bitcnt_t`](../C/GMP/constant.GMP_Basics.html#index-mp_005fbitcnt_005ft)
93pub type bitcnt_t = c_ulong;
94
95/// See: [`mpz_t`](../C/GMP/constant.GMP_Basics.html#index-mpz_005ft)
96/// and [Integer Internals](../C/GMP/constant.Internals.html#Integer-Internals)
97///
98#[doc = include_str!("internal_fields.md")]
99#[repr(C)]
100#[derive(Clone, Copy, Debug)]
101pub struct mpz_t {
102    /// See: [Integer Internals](../C/GMP/constant.Internals.html#Integer-Internals)
103    pub alloc: c_int,
104    /// See: [Integer Internals](../C/GMP/constant.Internals.html#Integer-Internals)
105    pub size: c_int,
106    /// See: [Integer Internals](../C/GMP/constant.Internals.html#Integer-Internals)
107    pub d: NonNull<limb_t>,
108}
109
110/// See: [`mpq_t`](../C/GMP/constant.GMP_Basics.html#index-mpq_005ft)
111/// and [Rational Internals](../C/GMP/constant.Internals.html#Rational-Internals)
112///
113#[doc = include_str!("internal_fields.md")]
114#[repr(C)]
115#[derive(Clone, Copy, Debug)]
116pub struct mpq_t {
117    /// Internal implementation detail: numerator.
118    pub num: mpz_t,
119    /// Internal implementation detail: denominator.
120    pub den: mpz_t,
121}
122
123/// See: [`mpf_t`](../C/GMP/constant.GMP_Basics.html#index-mpf_005ft)
124/// and [Float Internals](../C/GMP/constant.Internals.html#Float-Internals)
125///
126#[doc = include_str!("internal_fields.md")]
127#[repr(C)]
128#[derive(Clone, Copy, Debug)]
129pub struct mpf_t {
130    /// See: [Float Internals](../C/GMP/constant.Internals.html#Float-Internals)
131    pub prec: c_int,
132    /// See: [Float Internals](../C/GMP/constant.Internals.html#Float-Internals)
133    pub size: c_int,
134    /// See: [Float Internals](../C/GMP/constant.Internals.html#Float-Internals)
135    pub exp: exp_t,
136    /// See: [Float Internals](../C/GMP/constant.Internals.html#Float-Internals)
137    pub d: NonNull<limb_t>,
138}
139
140/// See: [`gmp_randstate_t`](../C/GMP/constant.GMP_Basics.html#index-gmp_005frandstate_005ft)
141///
142#[doc = include_str!("internal_fields.md")]
143#[repr(C)]
144#[derive(Clone, Copy)]
145pub struct randstate_t {
146    /// Internal implementation detail: state of the generator.
147    pub seed: randseed_t,
148    /// Internal implementation detail: unused.
149    pub alg: MaybeUninit<c_int>,
150    /// Internal implementation detail: pointer to function pointers
151    /// structure.
152    pub algdata: *const randfnptr_t,
153}
154
155impl Debug for randstate_t {
156    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
157        f.debug_struct("randstate_t")
158            .field("seed", &self.seed)
159            .field("alg", &"unused")
160            .field("algdata", &self.algdata)
161            .finish()
162    }
163}
164
165/// The type for the [`seed`] field in the [`randstate_t`] struct.
166///
167/// [`seed`]: `randstate_t::seed`
168///
169#[doc = include_str!("internal_fields.md")]
170#[repr(C)]
171#[derive(Clone, Copy)]
172pub struct randseed_t {
173    /// Internal implementation detail: unused.
174    pub alloc: MaybeUninit<c_int>,
175    /// Internal implementation detail: unused.
176    pub size: MaybeUninit<c_int>,
177    /// Internal implementation detail: state of the generator.
178    pub d: NonNull<c_void>,
179}
180
181impl Debug for randseed_t {
182    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
183        f.debug_struct("randseed_t")
184            .field("alloc", &"unused")
185            .field("size", &"unused")
186            .field("d", &self.d)
187            .finish()
188    }
189}
190
191/// The type for the [`algdata`] field in the [`randstate_t`] struct.
192///
193/// [`algdata`]: `randstate_t::algdata`
194///
195#[doc = include_str!("internal_fields.md")]
196#[repr(C)]
197#[derive(Clone, Copy, Debug)]
198pub struct randfnptr_t {
199    /// Internal implementation detail: pointer to function.
200    pub seed: unsafe extern "C" fn(rstate: *mut randstate_t, seed: *const mpz_t),
201    /// Internal implementation detail: pointer to function.
202    pub get: unsafe extern "C" fn(rstate: *mut randstate_t, dest: *mut limb_t, nbits: c_ulong),
203    /// Internal implementation detail: pointer to function.
204    pub clear: unsafe extern "C" fn(rstate: *mut randstate_t),
205    /// Internal implementation detail: pointer to function.
206    pub iset: unsafe extern "C" fn(dst: *mut randstate_t, src: *const randstate_t),
207}
208
209// Types for function declarations in this file.
210
211/// See: [`mpz_ptr`](../C/GMP/constant.GMP_Basics.html#index-mpz_005fptr)
212pub type mpz_ptr = *mut mpz_t;
213/// See: [`mpz_srcptr`](../C/GMP/constant.GMP_Basics.html#index-mpz_005fsrcptr)
214pub type mpz_srcptr = *const mpz_t;
215/// See: [`mpq_ptr`](../C/GMP/constant.GMP_Basics.html#index-mpq_005fptr)
216pub type mpq_ptr = *mut mpq_t;
217/// See: [`mpq_srcptr`](../C/GMP/constant.GMP_Basics.html#index-mpq_005fsrcptr)
218pub type mpq_srcptr = *const mpq_t;
219/// See: [`mpf_ptr`](../C/GMP/constant.GMP_Basics.html#index-mpf_005fptr)
220pub type mpf_ptr = *mut mpf_t;
221/// See: [`mpf_srcptr`](../C/GMP/constant.GMP_Basics.html#index-mpf_005fsrcptr)
222pub type mpf_srcptr = *const mpf_t;
223/// See: [`gmp_randstate_ptr`](../C/GMP/constant.GMP_Basics.html#index-gmp_005frandstate_005fptr)
224pub type randstate_ptr = *mut randstate_t;
225/// See: [`gmp_randstate_srcptr`](../C/GMP/constant.GMP_Basics.html#index-gmp_005frandstate_005fsrcptr)
226pub type randstate_srcptr = *const randstate_t;
227
228type mp_ptr = *mut limb_t;
229type mp_srcptr = *const limb_t;
230
231// Integers
232
233// Initialization Functions
234
235extern "C" {
236    /// See: [`mpz_init`](../C/GMP/constant.Integer_Functions.html#index-mpz_005finit)
237    #[link_name = "__gmpz_init"]
238    pub fn mpz_init(x: mpz_ptr);
239    #[link_name = "__gmpz_inits"]
240    /// See: [`mpz_inits`](../C/GMP/constant.Integer_Functions.html#index-mpz_005finits)
241    pub fn mpz_inits(x: mpz_ptr, ...);
242    #[link_name = "__gmpz_init2"]
243    /// See: [`mpz_init2`](../C/GMP/constant.Integer_Functions.html#index-mpz_005finit2)
244    pub fn mpz_init2(x: mpz_ptr, n: bitcnt_t);
245    #[link_name = "__gmpz_clear"]
246    /// See: [`mpz_clear`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fclear)
247    pub fn mpz_clear(x: mpz_ptr);
248    #[link_name = "__gmpz_clears"]
249    /// See: [`mpz_clears`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fclears)
250    pub fn mpz_clears(x: mpz_ptr, ...);
251    #[link_name = "__gmpz_realloc2"]
252    /// See: [`mpz_realloc2`](../C/GMP/constant.Integer_Functions.html#index-mpz_005frealloc2)
253    pub fn mpz_realloc2(x: mpz_ptr, n: bitcnt_t);
254
255    // Assignment Functions
256
257    /// See: [`mpz_set`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fset)
258    #[link_name = "__gmpz_set"]
259    pub fn mpz_set(rop: mpz_ptr, op: mpz_srcptr);
260    /// See: [`mpz_set_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fset_005fui)
261    #[link_name = "__gmpz_set_ui"]
262    pub fn mpz_set_ui(rop: mpz_ptr, op: c_ulong);
263    /// See: [`mpz_set_si`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fset_005fsi)
264    #[link_name = "__gmpz_set_si"]
265    pub fn mpz_set_si(rop: mpz_ptr, op: c_long);
266    /// See: [`mpz_set_d`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fset_005fd)
267    #[link_name = "__gmpz_set_d"]
268    pub fn mpz_set_d(rop: mpz_ptr, op: f64);
269}
270/// See: [`mpz_set_q`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fset_005fq)
271#[inline]
272pub unsafe extern "C" fn mpz_set_q(rop: mpz_ptr, op: mpq_srcptr) {
273    unsafe { mpz_tdiv_q(rop, mpq_numref_const(op), mpq_denref_const(op)) }
274}
275extern "C" {
276    /// See: [`mpz_set_f`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fset_005ff)
277    #[link_name = "__gmpz_set_f"]
278    pub fn mpz_set_f(rop: mpz_ptr, op: mpf_srcptr);
279    /// See: [`mpz_set_str`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fset_005fstr)
280    #[link_name = "__gmpz_set_str"]
281    pub fn mpz_set_str(rop: mpz_ptr, str: *const c_char, base: c_int) -> c_int;
282    /// See: [`mpz_swap`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fswap)
283    #[link_name = "__gmpz_swap"]
284    pub fn mpz_swap(rop1: mpz_ptr, rop2: mpz_ptr);
285
286    // Combined Initialization and Assignment Functions
287
288    /// See: [`mpz_init_set`](../C/GMP/constant.Integer_Functions.html#index-mpz_005finit_005fset)
289    #[link_name = "__gmpz_init_set"]
290    pub fn mpz_init_set(rop: mpz_ptr, op: mpz_srcptr);
291    /// See: [`mpz_init_set_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005finit_005fset_005fui)
292    #[link_name = "__gmpz_init_set_ui"]
293    pub fn mpz_init_set_ui(rop: mpz_ptr, op: c_ulong);
294    /// See: [`mpz_init_set_si`](../C/GMP/constant.Integer_Functions.html#index-mpz_005finit_005fset_005fsi)
295    #[link_name = "__gmpz_init_set_si"]
296    pub fn mpz_init_set_si(rop: mpz_ptr, op: c_long);
297    /// See: [`mpz_init_set_d`](../C/GMP/constant.Integer_Functions.html#index-mpz_005finit_005fset_005fd)
298    #[link_name = "__gmpz_init_set_d"]
299    pub fn mpz_init_set_d(rop: mpz_ptr, op: f64);
300    /// See: [`mpz_init_set_str`](../C/GMP/constant.Integer_Functions.html#index-mpz_005finit_005fset_005fstr)
301    #[link_name = "__gmpz_init_set_str"]
302    pub fn mpz_init_set_str(rop: mpz_ptr, str: *const c_char, base: c_int) -> c_int;
303}
304
305// Conversion Functions
306
307/// See: [`mpz_get_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fget_005fui)
308#[inline]
309#[cfg(any(not(nails), long_long_limb))]
310pub unsafe extern "C" fn mpz_get_ui(op: mpz_srcptr) -> c_ulong {
311    if unsafe { (*op).size } != 0 {
312        unsafe {
313            let p = (*op).d.as_ptr();
314            (*p) as c_ulong
315        }
316    } else {
317        0
318    }
319}
320/// See: [`mpz_get_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fget_005fui)
321#[inline]
322#[cfg(all(nails, not(long_long_limb)))]
323pub unsafe extern "C" fn mpz_get_ui(op: mpz_srcptr) -> c_ulong {
324    let p = unsafe { (*op).d };
325    let n = unsafe { (*op).size }.abs();
326    if n == 0 {
327        0
328    } else if n == 1 {
329        unsafe { *p }
330    } else {
331        unsafe { *p + ((*(p.offset(1))) << NUMB_BITS) }
332    }
333}
334extern "C" {
335    /// See: [`mpz_get_si`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fget_005fsi)
336    #[link_name = "__gmpz_get_si"]
337    pub fn mpz_get_si(op: mpz_srcptr) -> c_long;
338    /// See: [`mpz_get_d`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fget_005fd)
339    #[link_name = "__gmpz_get_d"]
340    pub fn mpz_get_d(op: mpz_srcptr) -> f64;
341    /// See: [`mpz_get_d_2exp`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fget_005fd_005f2exp)
342    #[link_name = "__gmpz_get_d_2exp"]
343    pub fn mpz_get_d_2exp(exp: *mut c_long, op: mpz_srcptr) -> f64;
344    /// See: [`mpz_get_str`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fget_005fstr)
345    #[link_name = "__gmpz_get_str"]
346    pub fn mpz_get_str(str: *mut c_char, base: c_int, op: mpz_srcptr) -> *mut c_char;
347
348    // Arithmetic Functions
349
350    /// See: [`mpz_add`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fadd)
351    #[link_name = "__gmpz_add"]
352    pub fn mpz_add(rop: mpz_ptr, op1: mpz_srcptr, op2: mpz_srcptr);
353    /// See: [`mpz_add_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fadd_005fui)
354    #[link_name = "__gmpz_add_ui"]
355    pub fn mpz_add_ui(rop: mpz_ptr, op1: mpz_srcptr, op2: c_ulong);
356    /// See: [`mpz_sub`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fsub)
357    #[link_name = "__gmpz_sub"]
358    pub fn mpz_sub(rop: mpz_ptr, op1: mpz_srcptr, op2: mpz_srcptr);
359    /// See: [`mpz_sub_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fsub_005fui)
360    #[link_name = "__gmpz_sub_ui"]
361    pub fn mpz_sub_ui(rop: mpz_ptr, op1: mpz_srcptr, op2: c_ulong);
362    /// See: [`mpz_ui_sub`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fui_005fsub)
363    #[link_name = "__gmpz_ui_sub"]
364    pub fn mpz_ui_sub(rop: mpz_ptr, op1: c_ulong, op2: mpz_srcptr);
365    /// See: [`mpz_mul`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fmul)
366    #[link_name = "__gmpz_mul"]
367    pub fn mpz_mul(rop: mpz_ptr, op1: mpz_srcptr, op2: mpz_srcptr);
368    /// See: [`mpz_mul_si`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fmul_005fsi)
369    #[link_name = "__gmpz_mul_si"]
370    pub fn mpz_mul_si(rop: mpz_ptr, op1: mpz_srcptr, op2: c_long);
371    /// See: [`mpz_mul_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fmul_005fui)
372    #[link_name = "__gmpz_mul_ui"]
373    pub fn mpz_mul_ui(rop: mpz_ptr, op1: mpz_srcptr, op2: c_ulong);
374    /// See: [`mpz_addmul`](../C/GMP/constant.Integer_Functions.html#index-mpz_005faddmul)
375    #[link_name = "__gmpz_addmul"]
376    pub fn mpz_addmul(rop: mpz_ptr, op1: mpz_srcptr, op2: mpz_srcptr);
377    /// See: [`mpz_addmul_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005faddmul_005fui)
378    #[link_name = "__gmpz_addmul_ui"]
379    pub fn mpz_addmul_ui(rop: mpz_ptr, op1: mpz_srcptr, op2: c_ulong);
380    /// See: [`mpz_submul`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fsubmul)
381    #[link_name = "__gmpz_submul"]
382    pub fn mpz_submul(rop: mpz_ptr, op1: mpz_srcptr, op2: mpz_srcptr);
383    /// See: [`mpz_submul_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fsubmul_005fui)
384    #[link_name = "__gmpz_submul_ui"]
385    pub fn mpz_submul_ui(rop: mpz_ptr, op1: mpz_srcptr, op2: c_ulong);
386    /// See: [`mpz_mul_2exp`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fmul_005f2exp)
387    #[link_name = "__gmpz_mul_2exp"]
388    pub fn mpz_mul_2exp(rop: mpz_ptr, op1: mpz_srcptr, op2: bitcnt_t);
389}
390/// See: [`mpz_neg`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fneg)
391#[inline]
392pub unsafe extern "C" fn mpz_neg(rop: mpz_ptr, op: mpz_srcptr) {
393    if rop as mpz_srcptr != op {
394        unsafe {
395            mpz_set(rop, op);
396        }
397    }
398    unsafe {
399        (*rop).size = -(*rop).size;
400    }
401}
402/// See: [`mpz_abs`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fabs)
403#[inline]
404pub unsafe extern "C" fn mpz_abs(rop: mpz_ptr, op: mpz_srcptr) {
405    unsafe {
406        if rop as mpz_srcptr != op {
407            mpz_set(rop, op);
408        }
409        (*rop).size = (*rop).size.abs();
410    }
411}
412
413// Division Functions
414
415extern "C" {
416    /// See: [`mpz_cdiv_q`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcdiv_005fq)
417    #[link_name = "__gmpz_cdiv_q"]
418    pub fn mpz_cdiv_q(q: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr);
419    /// See: [`mpz_cdiv_r`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcdiv_005fr)
420    #[link_name = "__gmpz_cdiv_r"]
421    pub fn mpz_cdiv_r(r: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr);
422    /// See: [`mpz_cdiv_qr`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcdiv_005fqr)
423    #[link_name = "__gmpz_cdiv_qr"]
424    pub fn mpz_cdiv_qr(q: mpz_ptr, r: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr);
425    /// See: [`mpz_cdiv_q_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcdiv_005fq_005fui)
426    #[link_name = "__gmpz_cdiv_q_ui"]
427    pub fn mpz_cdiv_q_ui(q: mpz_ptr, n: mpz_srcptr, d: c_ulong) -> c_ulong;
428    /// See: [`mpz_cdiv_r_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcdiv_005fr_005fui)
429    #[link_name = "__gmpz_cdiv_r_ui"]
430    pub fn mpz_cdiv_r_ui(r: mpz_ptr, n: mpz_srcptr, d: c_ulong) -> c_ulong;
431    /// See: [`mpz_cdiv_qr_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcdiv_005fqr_005fui)
432    #[link_name = "__gmpz_cdiv_qr_ui"]
433    pub fn mpz_cdiv_qr_ui(q: mpz_ptr, r: mpz_ptr, n: mpz_srcptr, d: c_ulong) -> c_ulong;
434    /// See: [`mpz_cdiv_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcdiv_005fui)
435    #[link_name = "__gmpz_cdiv_ui"]
436    pub fn mpz_cdiv_ui(n: mpz_srcptr, d: c_ulong) -> c_ulong;
437    /// See: [`mpz_cdiv_q_2exp`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcdiv_005fq_005f2exp)
438    #[link_name = "__gmpz_cdiv_q_2exp"]
439    pub fn mpz_cdiv_q_2exp(q: mpz_ptr, n: mpz_srcptr, b: bitcnt_t);
440    /// See: [`mpz_cdiv_r_2exp`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcdiv_005fr_005f2exp)
441    #[link_name = "__gmpz_cdiv_r_2exp"]
442    pub fn mpz_cdiv_r_2exp(r: mpz_ptr, n: mpz_srcptr, b: bitcnt_t);
443    /// See: [`mpz_fdiv_q`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffdiv_005fq)
444    #[link_name = "__gmpz_fdiv_q"]
445    pub fn mpz_fdiv_q(q: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr);
446    /// See: [`mpz_fdiv_r`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffdiv_005fr)
447    #[link_name = "__gmpz_fdiv_r"]
448    pub fn mpz_fdiv_r(r: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr);
449    /// See: [`mpz_fdiv_qr`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffdiv_005fqr)
450    #[link_name = "__gmpz_fdiv_qr"]
451    pub fn mpz_fdiv_qr(q: mpz_ptr, r: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr);
452    /// See: [`mpz_fdiv_q_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffdiv_005fq_005fui)
453    #[link_name = "__gmpz_fdiv_q_ui"]
454    pub fn mpz_fdiv_q_ui(q: mpz_ptr, n: mpz_srcptr, d: c_ulong) -> c_ulong;
455    /// See: [`mpz_fdiv_r_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffdiv_005fr_005fui)
456    #[link_name = "__gmpz_fdiv_r_ui"]
457    pub fn mpz_fdiv_r_ui(r: mpz_ptr, n: mpz_srcptr, d: c_ulong) -> c_ulong;
458    /// See: [`mpz_fdiv_qr_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffdiv_005fqr_005fui)
459    #[link_name = "__gmpz_fdiv_qr_ui"]
460    pub fn mpz_fdiv_qr_ui(q: mpz_ptr, r: mpz_ptr, n: mpz_srcptr, d: c_ulong) -> c_ulong;
461    /// See: [`mpz_fdiv_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffdiv_005fui)
462    #[link_name = "__gmpz_fdiv_ui"]
463    pub fn mpz_fdiv_ui(n: mpz_srcptr, d: c_ulong) -> c_ulong;
464    /// See: [`mpz_fdiv_q_2exp`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffdiv_005fq_005f2exp)
465    #[link_name = "__gmpz_fdiv_q_2exp"]
466    pub fn mpz_fdiv_q_2exp(q: mpz_ptr, n: mpz_srcptr, b: bitcnt_t);
467    /// See: [`mpz_fdiv_r_2exp`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffdiv_005fr_005f2exp)
468    #[link_name = "__gmpz_fdiv_r_2exp"]
469    pub fn mpz_fdiv_r_2exp(r: mpz_ptr, n: mpz_srcptr, b: bitcnt_t);
470    /// See: [`mpz_tdiv_q`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ftdiv_005fq)
471    #[link_name = "__gmpz_tdiv_q"]
472    pub fn mpz_tdiv_q(q: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr);
473    /// See: [`mpz_tdiv_r`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ftdiv_005fr)
474    #[link_name = "__gmpz_tdiv_r"]
475    pub fn mpz_tdiv_r(r: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr);
476    /// See: [`mpz_tdiv_qr`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ftdiv_005fqr)
477    #[link_name = "__gmpz_tdiv_qr"]
478    pub fn mpz_tdiv_qr(q: mpz_ptr, r: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr);
479    /// See: [`mpz_tdiv_q_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ftdiv_005fq_005fui)
480    #[link_name = "__gmpz_tdiv_q_ui"]
481    pub fn mpz_tdiv_q_ui(q: mpz_ptr, n: mpz_srcptr, d: c_ulong) -> c_ulong;
482    /// See: [`mpz_tdiv_r_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ftdiv_005fr_005fui)
483    #[link_name = "__gmpz_tdiv_r_ui"]
484    pub fn mpz_tdiv_r_ui(r: mpz_ptr, n: mpz_srcptr, d: c_ulong) -> c_ulong;
485    /// See: [`mpz_tdiv_qr_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ftdiv_005fqr_005fui)
486    #[link_name = "__gmpz_tdiv_qr_ui"]
487    pub fn mpz_tdiv_qr_ui(q: mpz_ptr, r: mpz_ptr, n: mpz_srcptr, d: c_ulong) -> c_ulong;
488    /// See: [`mpz_tdiv_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ftdiv_005fui)
489    #[link_name = "__gmpz_tdiv_ui"]
490    pub fn mpz_tdiv_ui(n: mpz_srcptr, d: c_ulong) -> c_ulong;
491    /// See: [`mpz_tdiv_q_2exp`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ftdiv_005fq_005f2exp)
492    #[link_name = "__gmpz_tdiv_q_2exp"]
493    pub fn mpz_tdiv_q_2exp(q: mpz_ptr, n: mpz_srcptr, b: bitcnt_t);
494    /// See: [`mpz_tdiv_r_2exp`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ftdiv_005fr_005f2exp)
495    #[link_name = "__gmpz_tdiv_r_2exp"]
496    pub fn mpz_tdiv_r_2exp(r: mpz_ptr, n: mpz_srcptr, b: bitcnt_t);
497    /// See: [`mpz_mod`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fmod)
498    #[link_name = "__gmpz_mod"]
499    pub fn mpz_mod(r: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr);
500}
501/// See: [`mpz_mod_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fmod_005fui)
502#[inline]
503pub unsafe extern "C" fn mpz_mod_ui(r: mpz_ptr, n: mpz_srcptr, d: c_ulong) -> c_ulong {
504    unsafe { mpz_fdiv_r_ui(r, n, d) }
505}
506extern "C" {
507    /// See: [`mpz_divexact`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fdivexact)
508    #[link_name = "__gmpz_divexact"]
509    pub fn mpz_divexact(q: mpz_ptr, n: mpz_srcptr, d: mpz_srcptr);
510    /// See: [`mpz_divexact_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fdivexact_005fui)
511    #[link_name = "__gmpz_divexact_ui"]
512    pub fn mpz_divexact_ui(q: mpz_ptr, n: mpz_srcptr, d: c_ulong);
513    /// See: [`mpz_divisible_p`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fdivisible_005fp)
514    #[link_name = "__gmpz_divisible_p"]
515    pub fn mpz_divisible_p(n: mpz_srcptr, d: mpz_srcptr) -> c_int;
516    /// See: [`mpz_divisible_ui_p`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fdivisible_005fui_005fp)
517    #[link_name = "__gmpz_divisible_ui_p"]
518    pub fn mpz_divisible_ui_p(n: mpz_srcptr, d: c_ulong) -> c_int;
519    /// See: [`mpz_divisible_2exp_p`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fdivisible_005f2exp_005fp)
520    #[link_name = "__gmpz_divisible_2exp_p"]
521    pub fn mpz_divisible_2exp_p(n: mpz_srcptr, b: bitcnt_t) -> c_int;
522    /// See: [`mpz_congruent_p`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcongruent_005fp)
523    #[link_name = "__gmpz_congruent_p"]
524    pub fn mpz_congruent_p(n: mpz_srcptr, c: mpz_srcptr, d: mpz_srcptr) -> c_int;
525    /// See: [`mpz_congruent_ui_p`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcongruent_005fui_005fp)
526    #[link_name = "__gmpz_congruent_ui_p"]
527    pub fn mpz_congruent_ui_p(n: mpz_srcptr, c: c_ulong, d: c_ulong) -> c_int;
528    /// See: [`mpz_congruent_2exp_p`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcongruent_005f2exp_005fp)
529    #[link_name = "__gmpz_congruent_2exp_p"]
530    pub fn mpz_congruent_2exp_p(n: mpz_srcptr, c: mpz_srcptr, b: bitcnt_t) -> c_int;
531
532    // Exponentiation Functions
533
534    /// See: [`mpz_powm`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fpowm)
535    #[link_name = "__gmpz_powm"]
536    pub fn mpz_powm(rop: mpz_ptr, base: mpz_srcptr, exp: mpz_srcptr, modu: mpz_srcptr);
537    /// See: [`mpz_powm_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fpowm_005fui)
538    #[link_name = "__gmpz_powm_ui"]
539    pub fn mpz_powm_ui(rop: mpz_ptr, base: mpz_srcptr, exp: c_ulong, modu: mpz_srcptr);
540    /// See: [`mpz_powm_sec`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fpowm_005fsec)
541    #[link_name = "__gmpz_powm_sec"]
542    pub fn mpz_powm_sec(rop: mpz_ptr, base: mpz_srcptr, exp: mpz_srcptr, modu: mpz_srcptr);
543    /// See: [`mpz_pow_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fpow_005fui)
544    #[link_name = "__gmpz_pow_ui"]
545    pub fn mpz_pow_ui(rop: mpz_ptr, base: mpz_srcptr, exp: c_ulong);
546    /// See: [`mpz_ui_pow_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fui_005fpow_005fui)
547    #[link_name = "__gmpz_ui_pow_ui"]
548    pub fn mpz_ui_pow_ui(rop: mpz_ptr, base: c_ulong, exp: c_ulong);
549
550    // Root Extraction Functions
551
552    /// See: [`mpz_root`](../C/GMP/constant.Integer_Functions.html#index-mpz_005froot)
553    #[link_name = "__gmpz_root"]
554    pub fn mpz_root(rop: mpz_ptr, op: mpz_srcptr, n: c_ulong) -> c_int;
555    /// See: [`mpz_rootrem`](../C/GMP/constant.Integer_Functions.html#index-mpz_005frootrem)
556    #[link_name = "__gmpz_rootrem"]
557    pub fn mpz_rootrem(root: mpz_ptr, rem: mpz_ptr, op: mpz_srcptr, n: c_ulong);
558    /// See: [`mpz_sqrt`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fsqrt)
559    #[link_name = "__gmpz_sqrt"]
560    pub fn mpz_sqrt(rop: mpz_ptr, op: mpz_srcptr);
561    /// See: [`mpz_sqrtrem`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fsqrtrem)
562    #[link_name = "__gmpz_sqrtrem"]
563    pub fn mpz_sqrtrem(rop1: mpz_ptr, rop2: mpz_ptr, op: mpz_srcptr);
564    /// See: [`mpz_perfect_power_p`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fperfect_005fpower_005fp)
565    #[link_name = "__gmpz_perfect_power_p"]
566    pub fn mpz_perfect_power_p(op: mpz_srcptr) -> c_int;
567}
568/// See: [`mpz_perfect_square_p`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fperfect_005fsquare_005fp)
569#[inline]
570pub unsafe extern "C" fn mpz_perfect_square_p(op: mpz_srcptr) -> c_int {
571    let op_size = unsafe { (*op).size };
572    if op_size > 0 {
573        unsafe { mpn_perfect_square_p((*op).d.as_ptr(), op_size.into()) }
574    } else {
575        (op_size >= 0) as c_int
576    }
577}
578
579// Number Theoretic Functions
580
581extern "C" {
582    /// See: [`mpz_probab_prime_p`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fprobab_005fprime_005fp)
583    #[link_name = "__gmpz_probab_prime_p"]
584    pub fn mpz_probab_prime_p(n: mpz_srcptr, reps: c_int) -> c_int;
585    /// See: [`mpz_nextprime`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fnextprime)
586    #[link_name = "__gmpz_nextprime"]
587    pub fn mpz_nextprime(rop: mpz_ptr, op: mpz_srcptr);
588    /// See: [`mpz_prevprime`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fprevprime)
589    #[link_name = "__gmpz_prevprime"]
590    pub fn mpz_prevprime(rop: mpz_ptr, op: mpz_srcptr);
591    /// See: [`mpz_gcd`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fgcd)
592    #[link_name = "__gmpz_gcd"]
593    pub fn mpz_gcd(rop: mpz_ptr, op1: mpz_srcptr, op2: mpz_srcptr);
594    /// See: [`mpz_gcd_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fgcd_005fui)
595    #[link_name = "__gmpz_gcd_ui"]
596    pub fn mpz_gcd_ui(rop: mpz_ptr, op1: mpz_srcptr, op2: c_ulong) -> c_ulong;
597    /// See: [`mpz_gcdext`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fgcdext)
598    #[link_name = "__gmpz_gcdext"]
599    pub fn mpz_gcdext(g: mpz_ptr, s: mpz_ptr, t: mpz_ptr, a: mpz_srcptr, b: mpz_srcptr);
600    /// See: [`mpz_lcm`](../C/GMP/constant.Integer_Functions.html#index-mpz_005flcm)
601    #[link_name = "__gmpz_lcm"]
602    pub fn mpz_lcm(rop: mpz_ptr, op1: mpz_srcptr, op2: mpz_srcptr);
603    /// See: [`mpz_lcm_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005flcm_005fui)
604    #[link_name = "__gmpz_lcm_ui"]
605    pub fn mpz_lcm_ui(rop: mpz_ptr, op1: mpz_srcptr, op2: c_ulong);
606    /// See: [`mpz_invert`](../C/GMP/constant.Integer_Functions.html#index-mpz_005finvert)
607    #[link_name = "__gmpz_invert"]
608    pub fn mpz_invert(rop: mpz_ptr, op1: mpz_srcptr, op2: mpz_srcptr) -> c_int;
609    /// See: [`mpz_jacobi`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fjacobi)
610    #[link_name = "__gmpz_jacobi"]
611    pub fn mpz_jacobi(a: mpz_srcptr, b: mpz_srcptr) -> c_int;
612}
613/// See: [`mpz_legendre`](../C/GMP/constant.Integer_Functions.html#index-mpz_005flegendre)
614#[inline]
615pub unsafe extern "C" fn mpz_legendre(a: mpz_srcptr, p: mpz_srcptr) -> c_int {
616    unsafe { mpz_jacobi(a, p) }
617}
618/// See: [`mpz_kronecker`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fkronecker)
619#[inline]
620pub unsafe extern "C" fn mpz_kronecker(a: mpz_srcptr, b: mpz_srcptr) -> c_int {
621    unsafe { mpz_jacobi(a, b) }
622}
623extern "C" {
624    /// See: [`mpz_kronecker_si`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fkronecker_005fsi)
625    #[link_name = "__gmpz_kronecker_si"]
626    pub fn mpz_kronecker_si(a: mpz_srcptr, b: c_long) -> c_int;
627    /// See: [`mpz_kronecker_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fkronecker_005fui)
628    #[link_name = "__gmpz_kronecker_ui"]
629    pub fn mpz_kronecker_ui(a: mpz_srcptr, b: c_ulong) -> c_int;
630    /// See: [`mpz_si_kronecker`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fsi_005fkronecker)
631    #[link_name = "__gmpz_si_kronecker"]
632    pub fn mpz_si_kronecker(a: c_long, b: mpz_srcptr) -> c_int;
633    /// See: [`mpz_ui_kronecker`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fui_005fkronecker)
634    #[link_name = "__gmpz_ui_kronecker"]
635    pub fn mpz_ui_kronecker(a: c_ulong, b: mpz_srcptr) -> c_int;
636    /// See: [`mpz_remove`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fremove)
637    #[link_name = "__gmpz_remove"]
638    pub fn mpz_remove(rop: mpz_ptr, op: mpz_srcptr, f: mpz_srcptr) -> bitcnt_t;
639    /// See: [`mpz_fac_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffac_005fui)
640    #[link_name = "__gmpz_fac_ui"]
641    pub fn mpz_fac_ui(rop: mpz_ptr, n: c_ulong);
642    /// See: [`mpz_2fac_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005f2fac_005fui)
643    #[link_name = "__gmpz_2fac_ui"]
644    pub fn mpz_2fac_ui(rop: mpz_ptr, n: c_ulong);
645    /// See: [`mpz_mfac_uiui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fmfac_005fuiui)
646    #[link_name = "__gmpz_mfac_uiui"]
647    pub fn mpz_mfac_uiui(rop: mpz_ptr, n: c_ulong, m: c_ulong);
648    /// See: [`mpz_primorial_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fprimorial_005fui)
649    #[link_name = "__gmpz_primorial_ui"]
650    pub fn mpz_primorial_ui(r: mpz_ptr, n: c_ulong);
651    /// See: [`mpz_bin_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fbin_005fui)
652    #[link_name = "__gmpz_bin_ui"]
653    pub fn mpz_bin_ui(rop: mpz_ptr, n: mpz_srcptr, k: c_ulong);
654    /// See: [`mpz_bin_uiui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fbin_005fuiui)
655    #[link_name = "__gmpz_bin_uiui"]
656    pub fn mpz_bin_uiui(rop: mpz_ptr, n: c_ulong, k: c_ulong);
657    /// See: [`mpz_fib_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffib_005fui)
658    #[link_name = "__gmpz_fib_ui"]
659    pub fn mpz_fib_ui(f_n: mpz_ptr, n: c_ulong);
660    /// See: [`mpz_fib2_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffib2_005fui)
661    #[link_name = "__gmpz_fib2_ui"]
662    pub fn mpz_fib2_ui(f_n: mpz_ptr, fnsub1: mpz_ptr, n: c_ulong);
663    /// See: [`mpz_lucnum_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005flucnum_005fui)
664    #[link_name = "__gmpz_lucnum_ui"]
665    pub fn mpz_lucnum_ui(ln: mpz_ptr, n: c_ulong);
666    /// See: [`mpz_lucnum2_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005flucnum2_005fui)
667    #[link_name = "__gmpz_lucnum2_ui"]
668    pub fn mpz_lucnum2_ui(ln: mpz_ptr, lnsub1: mpz_ptr, n: c_ulong);
669
670    // Comparison Functions
671
672    /// See: [`mpz_cmp`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcmp)
673    #[link_name = "__gmpz_cmp"]
674    pub fn mpz_cmp(op1: mpz_srcptr, op2: mpz_srcptr) -> c_int;
675    /// See: [`mpz_cmp_d`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcmp_005fd)
676    #[link_name = "__gmpz_cmp_d"]
677    pub fn mpz_cmp_d(op1: mpz_srcptr, op2: f64) -> c_int;
678    /// See: [`mpz_cmp_si`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcmp_005fsi)
679    #[link_name = "__gmpz_cmp_si"]
680    pub fn mpz_cmp_si(op1: mpz_srcptr, op2: c_long) -> c_int;
681    /// See: [`mpz_cmp_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcmp_005fui)
682    #[link_name = "__gmpz_cmp_ui"]
683    pub fn mpz_cmp_ui(op1: mpz_srcptr, op2: c_ulong) -> c_int;
684    /// See: [`mpz_cmpabs`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcmpabs)
685    #[link_name = "__gmpz_cmpabs"]
686    pub fn mpz_cmpabs(op1: mpz_srcptr, op2: mpz_srcptr) -> c_int;
687    /// See: [`mpz_cmpabs_d`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcmpabs_005fd)
688    #[link_name = "__gmpz_cmpabs_d"]
689    pub fn mpz_cmpabs_d(op1: mpz_srcptr, op2: f64) -> c_int;
690    /// See: [`mpz_cmpabs_ui`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcmpabs_005fui)
691    #[link_name = "__gmpz_cmpabs_ui"]
692    pub fn mpz_cmpabs_ui(op1: mpz_srcptr, op2: c_ulong) -> c_int;
693}
694/// See: [`mpz_sgn`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fsgn)
695#[inline]
696pub const unsafe extern "C" fn mpz_sgn(op: mpz_srcptr) -> c_int {
697    let size = unsafe { (*op).size };
698    if size < 0 {
699        -1
700    } else {
701        (size > 0) as c_int
702    }
703}
704extern "C" {
705    /// See: [`mpz_and`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fand)
706    #[link_name = "__gmpz_and"]
707    pub fn mpz_and(rop: mpz_ptr, op1: mpz_srcptr, op2: mpz_srcptr);
708    /// See: [`mpz_ior`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fior)
709    #[link_name = "__gmpz_ior"]
710    pub fn mpz_ior(rop: mpz_ptr, op1: mpz_srcptr, op2: mpz_srcptr);
711    /// See: [`mpz_xor`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fxor)
712    #[link_name = "__gmpz_xor"]
713    pub fn mpz_xor(rop: mpz_ptr, op1: mpz_srcptr, op2: mpz_srcptr);
714    /// See: [`mpz_com`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcom)
715    #[link_name = "__gmpz_com"]
716    pub fn mpz_com(rop: mpz_ptr, op: mpz_srcptr);
717}
718/// See: [`mpz_popcount`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fpopcount)
719#[inline]
720pub unsafe extern "C" fn mpz_popcount(op: mpz_srcptr) -> bitcnt_t {
721    let size = unsafe { (*op).size };
722    match size.cmp(&0) {
723        Ordering::Less => !0,
724        Ordering::Equal => 0,
725        Ordering::Greater => unsafe { mpn_popcount((*op).d.as_ptr(), size.into()) },
726    }
727}
728extern "C" {
729    /// See: [`mpz_hamdist`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fhamdist)
730    #[link_name = "__gmpz_hamdist"]
731    pub fn mpz_hamdist(op1: mpz_srcptr, op2: mpz_srcptr) -> bitcnt_t;
732    /// See: [`mpz_scan0`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fscan0)
733    #[link_name = "__gmpz_scan0"]
734    pub fn mpz_scan0(op: mpz_srcptr, starting_bit: bitcnt_t) -> bitcnt_t;
735    /// See: [`mpz_scan1`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fscan1)
736    #[link_name = "__gmpz_scan1"]
737    pub fn mpz_scan1(op: mpz_srcptr, starting_bit: bitcnt_t) -> bitcnt_t;
738    /// See: [`mpz_setbit`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fsetbit)
739    #[link_name = "__gmpz_setbit"]
740    pub fn mpz_setbit(rop: mpz_ptr, bit_index: bitcnt_t);
741    /// See: [`mpz_clrbit`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fclrbit)
742    #[link_name = "__gmpz_clrbit"]
743    pub fn mpz_clrbit(rop: mpz_ptr, bit_index: bitcnt_t);
744    /// See: [`mpz_combit`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fcombit)
745    #[link_name = "__gmpz_combit"]
746    pub fn mpz_combit(rop: mpz_ptr, bit_index: bitcnt_t);
747    /// See: [`mpz_tstbit`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ftstbit)
748    #[link_name = "__gmpz_tstbit"]
749    pub fn mpz_tstbit(op: mpz_srcptr, bit_index: bitcnt_t) -> c_int;
750
751    // Input and Ouput Functions
752
753    /// See: [`mpz_out_str`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fout_005fstr)
754    #[link_name = "__gmpz_out_str"]
755    pub fn mpz_out_str(stream: *mut FILE, base: c_int, op: mpz_srcptr) -> usize;
756    /// See: [`mpz_inp_str`](../C/GMP/constant.Integer_Functions.html#index-mpz_005finp_005fstr)
757    #[link_name = "__gmpz_inp_str"]
758    pub fn mpz_inp_str(rop: mpz_ptr, stream: *mut FILE, base: c_int) -> usize;
759    /// See: [`mpz_out_raw`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fout_005fraw)
760    #[link_name = "__gmpz_out_raw"]
761    pub fn mpz_out_raw(stream: *mut FILE, op: mpz_srcptr) -> usize;
762    /// See: [`mpz_inp_raw`](../C/GMP/constant.Integer_Functions.html#index-mpz_005finp_005fraw)
763    #[link_name = "__gmpz_inp_raw"]
764    pub fn mpz_inp_raw(rop: mpz_ptr, stream: *mut FILE) -> usize;
765
766    // Random Number Functions
767
768    /// See: [`mpz_urandomb`](../C/GMP/constant.Integer_Functions.html#index-mpz_005furandomb)
769    #[link_name = "__gmpz_urandomb"]
770    pub fn mpz_urandomb(rop: mpz_ptr, state: randstate_ptr, n: bitcnt_t);
771    /// See: [`mpz_urandomm`](../C/GMP/constant.Integer_Functions.html#index-mpz_005furandomm)
772    #[link_name = "__gmpz_urandomm"]
773    pub fn mpz_urandomm(rop: mpz_ptr, state: randstate_ptr, n: mpz_srcptr);
774    /// See: [`mpz_rrandomb`](../C/GMP/constant.Integer_Functions.html#index-mpz_005frrandomb)
775    #[link_name = "__gmpz_rrandomb"]
776    pub fn mpz_rrandomb(rop: mpz_ptr, state: randstate_ptr, n: bitcnt_t);
777    /// See: [`mpz_random2`](../C/GMP/constant.Integer_Functions.html#index-mpz_005frandom2)
778    #[link_name = "__gmpz_random2"]
779    pub fn mpz_random2(rop: mpz_ptr, max_size: size_t);
780
781    // Integer Import and Export
782
783    /// See: [`mpz_import`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fimport)
784    #[link_name = "__gmpz_import"]
785    pub fn mpz_import(
786        rop: mpz_ptr,
787        count: usize,
788        order: c_int,
789        size: usize,
790        endian: c_int,
791        nails: usize,
792        op: *const c_void,
793    );
794    /// See: [`mpz_export`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fexport)
795    #[link_name = "__gmpz_export"]
796    pub fn mpz_export(
797        rop: *mut c_void,
798        countp: *mut usize,
799        order: c_int,
800        size: usize,
801        endian: c_int,
802        nails: usize,
803        op: mpz_srcptr,
804    ) -> *mut c_void;
805}
806
807// Miscellaneous Functions
808
809macro_rules! mpz_fits {
810    { $(#[$attr:meta])* fn $name:ident($max:expr); } => {
811        #[cfg(not(nails))]
812        $(#[$attr])*
813        #[inline]
814        pub const unsafe extern "C" fn $name(op: mpz_srcptr) -> c_int {
815            let n = unsafe { (*op).size };
816            let p = unsafe { (*op).d }.as_ptr().cast_const();
817            let fits = n == 0 || (n == 1 && unsafe { *p } <= ($max as limb_t));
818            fits as c_int
819        }
820        #[cfg(nails)]
821        $(#[$attr])*
822        #[inline]
823        pub const unsafe extern "C" fn $name(op: mpz_srcptr) -> c_int {
824            let n = unsafe { (*op).size };
825            let p = unsafe { (*op).d }.as_ptr().cast_const();
826            let fits = n == 0 || (n == 1 && unsafe { *p } <= ($max as limb_t))
827                || (n == 2
828                    && unsafe { *(p.offset(1)) } <= ($max as limb_t) >> NUMB_BITS);
829            fits as c_int
830        }
831    }
832}
833mpz_fits! {
834    /// See: [`mpz_fits_ulong_p`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffits_005fulong_005fp)
835    fn mpz_fits_ulong_p(c_ulong::MAX);
836}
837extern "C" {
838    /// See: [`mpz_fits_slong_p`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffits_005fslong_005fp)
839    #[link_name = "__gmpz_fits_slong_p"]
840    pub fn mpz_fits_slong_p(op: mpz_srcptr) -> c_int;
841}
842mpz_fits! {
843    /// See: [`mpz_fits_uint_p`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffits_005fuint_005fp)
844    fn mpz_fits_uint_p(c_uint::MAX);
845}
846extern "C" {
847    /// See: [`mpz_fits_sint_p`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffits_005fsint_005fp)
848    #[link_name = "__gmpz_fits_sint_p"]
849    pub fn mpz_fits_sint_p(op: mpz_srcptr) -> c_int;
850}
851mpz_fits! {
852    /// See: [`mpz_fits_ushort_p`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffits_005fushort_005fp)
853    fn mpz_fits_ushort_p(c_ushort::MAX);
854}
855extern "C" {
856    /// See: [`mpz_fits_sshort_p`](../C/GMP/constant.Integer_Functions.html#index-mpz_005ffits_005fsshort_005fp)
857    #[link_name = "__gmpz_fits_sshort_p"]
858    pub fn mpz_fits_sshort_p(op: mpz_srcptr) -> c_int;
859}
860/// See: [`mpz_odd_p`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fodd_005fp)
861#[inline]
862pub const unsafe extern "C" fn mpz_odd_p(op: mpz_srcptr) -> c_int {
863    if unsafe { (*op).size } == 0 {
864        0
865    } else {
866        1 & unsafe { *((*op).d.as_ptr().cast_const()) } as c_int
867    }
868}
869/// See: [`mpz_even_p`](../C/GMP/constant.Integer_Functions.html#index-mpz_005feven_005fp)
870#[inline]
871pub const unsafe extern "C" fn mpz_even_p(op: mpz_srcptr) -> c_int {
872    (unsafe { mpz_odd_p(op) } == 0) as c_int
873}
874extern "C" {
875    /// See: [`mpz_sizeinbase`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fsizeinbase)
876    #[link_name = "__gmpz_sizeinbase"]
877    pub fn mpz_sizeinbase(arg1: mpz_srcptr, arg2: c_int) -> usize;
878
879    // Special Functions
880
881    /// See: [`_mpz_realloc`](../C/GMP/constant.Integer_Functions.html#index-_005fmpz_005frealloc)
882    #[link_name = "__gmpz_realloc"]
883    pub fn _mpz_realloc(integer: mpz_ptr, new_alloc: size_t) -> *mut c_void;
884}
885/// See: [`mpz_getlimbn`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fgetlimbn)
886#[inline]
887pub const unsafe extern "C" fn mpz_getlimbn(op: mpz_srcptr, n: size_t) -> limb_t {
888    if n >= 0 && n < (unsafe { (*op).size }.abs() as size_t) {
889        unsafe { *(((*op).d.as_ptr().cast_const()).offset(n as isize)) }
890    } else {
891        0
892    }
893}
894/// See: [`mpz_size`](../C/GMP/constant.Integer_Functions.html#index-mpz_005fsize)
895#[inline]
896pub const unsafe extern "C" fn mpz_size(op: mpz_srcptr) -> usize {
897    unsafe { (*op).size }.unsigned_abs() as usize
898}
899extern "C" {
900    /// See: [`mpz_limbs_read`](../C/GMP/constant.Integer_Functions.html#index-mpz_005flimbs_005fread)
901    #[link_name = "__gmpz_limbs_read"]
902    pub fn mpz_limbs_read(x: mpz_srcptr) -> mp_srcptr;
903    /// See: [`mpz_limbs_write`](../C/GMP/constant.Integer_Functions.html#index-mpz_005flimbs_005fwrite)
904    #[link_name = "__gmpz_limbs_write"]
905    pub fn mpz_limbs_write(x: mpz_ptr, n: size_t) -> mp_ptr;
906    /// See: [`mpz_limbs_modify`](../C/GMP/constant.Integer_Functions.html#index-mpz_005flimbs_005fmodify)
907    #[link_name = "__gmpz_limbs_modify"]
908    pub fn mpz_limbs_modify(x: mpz_ptr, n: size_t) -> mp_ptr;
909    /// See: [`mpz_limbs_finish`](../C/GMP/constant.Integer_Functions.html#index-mpz_005flimbs_005ffinish)
910    #[link_name = "__gmpz_limbs_finish"]
911    pub fn mpz_limbs_finish(x: mpz_ptr, s: size_t);
912    /// See: [`mpz_roinit_n`](../C/GMP/constant.Integer_Functions.html#index-mpz_005froinit_005fn)
913    #[link_name = "__gmpz_roinit_n"]
914    pub fn mpz_roinit_n(x: mpz_ptr, xp: mp_srcptr, xs: size_t) -> mpz_srcptr;
915}
916/// See: [`MPZ_ROINIT_N`](../C/GMP/constant.Integer_Functions.html#index-MPZ_005fROINIT_005fN)
917#[inline]
918pub const unsafe extern "C" fn MPZ_ROINIT_N(xp: mp_ptr, xs: size_t) -> mpz_t {
919    mpz_t {
920        alloc: 0,
921        size: xs as c_int,
922        d: unsafe { NonNull::new_unchecked(xp) },
923    }
924}
925
926// Rational numbers
927
928extern "C" {
929    /// See: [`mpq_canonicalize`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fcanonicalize)
930    #[link_name = "__gmpq_canonicalize"]
931    pub fn mpq_canonicalize(op: mpq_ptr);
932
933    // Initialization and Assignment Functions
934
935    /// See: [`mpq_init`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005finit)
936    #[link_name = "__gmpq_init"]
937    pub fn mpq_init(x: mpq_ptr);
938    /// See: [`mpq_inits`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005finits)
939    #[link_name = "__gmpq_inits"]
940    pub fn mpq_inits(x: mpq_ptr, ...);
941    /// See: [`mpq_clear`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fclear)
942    #[link_name = "__gmpq_clear"]
943    pub fn mpq_clear(x: mpq_ptr);
944    /// See: [`mpq_clears`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fclears)
945    #[link_name = "__gmpq_clears"]
946    pub fn mpq_clears(x: mpq_ptr, ...);
947    /// See: [`mpq_set`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fset)
948    #[link_name = "__gmpq_set"]
949    pub fn mpq_set(rop: mpq_ptr, op: mpq_srcptr);
950    /// See: [`mpq_set_z`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fset_005fz)
951    #[link_name = "__gmpq_set_z"]
952    pub fn mpq_set_z(rop: mpq_ptr, op: mpz_srcptr);
953    /// See: [`mpq_set_ui`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fset_005fui)
954    #[link_name = "__gmpq_set_ui"]
955    pub fn mpq_set_ui(rop: mpq_ptr, op1: c_ulong, op2: c_ulong);
956    /// See: [`mpq_set_si`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fset_005fsi)
957    #[link_name = "__gmpq_set_si"]
958    pub fn mpq_set_si(rop: mpq_ptr, op1: c_long, op2: c_ulong);
959    /// See: [`mpq_set_str`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fset_005fstr)
960    #[link_name = "__gmpq_set_str"]
961    pub fn mpq_set_str(rop: mpq_ptr, str: *const c_char, base: c_int) -> c_int;
962    /// See: [`mpq_swap`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fswap)
963    #[link_name = "__gmpq_swap"]
964    pub fn mpq_swap(rop1: mpq_ptr, rop2: mpq_ptr);
965
966    // Conversion Functions
967
968    /// See: [`mpq_get_d`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fget_005fd)
969    #[link_name = "__gmpq_get_d"]
970    pub fn mpq_get_d(op: mpq_srcptr) -> f64;
971    /// See: [`mpq_set_d`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fset_005fd)
972    #[link_name = "__gmpq_set_d"]
973    pub fn mpq_set_d(rop: mpq_ptr, op: f64);
974    /// See: [`mpq_set_f`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fset_005ff)
975    #[link_name = "__gmpq_set_f"]
976    pub fn mpq_set_f(rop: mpq_ptr, op: mpf_srcptr);
977    /// See: [`mpq_get_str`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fget_005fstr)
978    #[link_name = "__gmpq_get_str"]
979    pub fn mpq_get_str(str: *mut c_char, base: c_int, op: mpq_srcptr) -> *mut c_char;
980
981    // Arithmetic Functions
982
983    /// See: [`mpq_add`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fadd)
984    #[link_name = "__gmpq_add"]
985    pub fn mpq_add(sum: mpq_ptr, addend1: mpq_srcptr, addend2: mpq_srcptr);
986    /// See: [`mpq_sub`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fsub)
987    #[link_name = "__gmpq_sub"]
988    pub fn mpq_sub(difference: mpq_ptr, minuend: mpq_srcptr, subtrahend: mpq_srcptr);
989    /// See: [`mpq_mul`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fmul)
990    #[link_name = "__gmpq_mul"]
991    pub fn mpq_mul(product: mpq_ptr, multiplier: mpq_srcptr, multiplicand: mpq_srcptr);
992    /// See: [`mpq_mul_2exp`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fmul_005f2exp)
993    #[link_name = "__gmpq_mul_2exp"]
994    pub fn mpq_mul_2exp(rop: mpq_ptr, op1: mpq_srcptr, op2: bitcnt_t);
995    /// See: [`mpq_div`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fdiv)
996    #[link_name = "__gmpq_div"]
997    pub fn mpq_div(quotient: mpq_ptr, dividend: mpq_srcptr, divisor: mpq_srcptr);
998    /// See: [`mpq_div_2exp`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fdiv_005f2exp)
999    #[link_name = "__gmpq_div_2exp"]
1000    pub fn mpq_div_2exp(rop: mpq_ptr, op1: mpq_srcptr, op2: bitcnt_t);
1001}
1002/// See: [`mpq_neg`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fneg)
1003#[inline]
1004pub unsafe extern "C" fn mpq_neg(negated_operand: mpq_ptr, operand: mpq_srcptr) {
1005    if negated_operand as mpq_srcptr != operand {
1006        unsafe { mpq_set(negated_operand, operand) };
1007    }
1008    unsafe {
1009        (*negated_operand).num.size = -(*negated_operand).num.size;
1010    }
1011}
1012/// See: [`mpq_abs`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fabs)
1013#[inline]
1014pub unsafe extern "C" fn mpq_abs(rop: mpq_ptr, op: mpq_srcptr) {
1015    if rop as mpq_srcptr != op {
1016        unsafe {
1017            mpq_set(rop, op);
1018        }
1019    }
1020    unsafe {
1021        (*rop).num.size = (*rop).num.size.abs();
1022    }
1023}
1024extern "C" {
1025    /// See: [`mpq_inv`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005finv)
1026    #[link_name = "__gmpq_inv"]
1027    pub fn mpq_inv(inverted_number: mpq_ptr, number: mpq_srcptr);
1028
1029    // Comparison Functions
1030
1031    /// See: [`mpq_cmp`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fcmp)
1032    #[link_name = "__gmpq_cmp"]
1033    pub fn mpq_cmp(op1: mpq_srcptr, op2: mpq_srcptr) -> c_int;
1034    /// See: [`mpq_cmp_z`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fcmp_005fz)
1035    #[link_name = "__gmpq_cmp_z"]
1036    pub fn mpq_cmp_z(op1: mpq_srcptr, op2: mpz_srcptr) -> c_int;
1037    /// See: [`mpq_cmp_ui`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fcmp_005fui)
1038    #[link_name = "__gmpq_cmp_ui"]
1039    pub fn mpq_cmp_ui(op1: mpq_srcptr, num2: c_ulong, den2: c_ulong) -> c_int;
1040    /// See: [`mpq_cmp_si`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fcmp_005fsi)
1041    #[link_name = "__gmpq_cmp_si"]
1042    pub fn mpq_cmp_si(op1: mpq_srcptr, num2: c_long, den2: c_ulong) -> c_int;
1043}
1044/// See: [`mpq_sgn`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fsgn)
1045#[inline]
1046pub const unsafe extern "C" fn mpq_sgn(op: mpq_srcptr) -> c_int {
1047    let size = unsafe { (*op).num.size };
1048    if size < 0 {
1049        -1
1050    } else {
1051        (size > 0) as c_int
1052    }
1053}
1054extern "C" {
1055    /// See: [`mpq_equal`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fequal)
1056    #[link_name = "__gmpq_equal"]
1057    pub fn mpq_equal(op1: mpq_srcptr, op2: mpq_srcptr) -> c_int;
1058}
1059
1060// Applying Integer Functions to Rationals
1061
1062/// See: [`mpq_numref`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fnumref)
1063#[inline]
1064pub const unsafe extern "C" fn mpq_numref(op: mpq_ptr) -> mpz_ptr {
1065    unsafe { mpq_numref_const(op) as mpz_ptr }
1066}
1067/// Constant version of [`mpq_numref`](fn.mpq_numref.html).
1068#[inline]
1069pub const unsafe extern "C" fn mpq_numref_const(op: mpq_srcptr) -> mpz_srcptr {
1070    unsafe { ptr::addr_of!((*op).num) }
1071}
1072/// See: [`mpq_denref`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fdenref)
1073#[inline]
1074pub const unsafe extern "C" fn mpq_denref(op: mpq_ptr) -> mpz_ptr {
1075    unsafe { mpq_denref_const(op) as mpz_ptr }
1076}
1077/// Constant version of [`mpq_denref`](fn.mpq_denref.html).
1078#[inline]
1079pub const unsafe extern "C" fn mpq_denref_const(op: mpq_srcptr) -> mpz_srcptr {
1080    unsafe { ptr::addr_of!((*op).den) }
1081}
1082extern "C" {
1083    /// See: [`mpq_get_num`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fget_005fnum)
1084    #[link_name = "__gmpq_get_num"]
1085    pub fn mpq_get_num(numerator: mpz_ptr, rational: mpq_srcptr);
1086    /// See: [`mpq_get_den`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fget_005fden)
1087    #[link_name = "__gmpq_get_den"]
1088    pub fn mpq_get_den(denominator: mpz_ptr, rational: mpq_srcptr);
1089    /// See: [`mpq_set_num`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fset_005fnum)
1090    #[link_name = "__gmpq_set_num"]
1091    pub fn mpq_set_num(rational: mpq_ptr, denominator: mpz_srcptr);
1092    /// See: [`mpq_set_den`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fset_005fden)
1093    #[link_name = "__gmpq_set_den"]
1094    pub fn mpq_set_den(rational: mpq_ptr, numerator: mpz_srcptr);
1095
1096    // Input and Output Functions
1097
1098    /// See: [`mpq_out_str`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fout_005fstr)
1099    #[link_name = "__gmpq_out_str"]
1100    pub fn mpq_out_str(stream: *mut FILE, base: c_int, op: mpq_srcptr) -> usize;
1101    /// See: [`mpq_inp_str`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005finp_005fstr)
1102    #[link_name = "__gmpq_inp_str"]
1103    pub fn mpq_inp_str(rop: mpq_ptr, stream: *mut FILE, base: c_int) -> usize;
1104}
1105
1106// Floating-point numbers
1107
1108// Initialization Functions
1109
1110extern "C" {
1111    /// See: [`mpf_set_default_prec`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fset_005fdefault_005fprec)
1112    #[link_name = "__gmpf_set_default_prec"]
1113    pub fn mpf_set_default_prec(prec: bitcnt_t);
1114    /// See: [`mpf_get_default_prec`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fget_005fdefault_005fprec)
1115    #[link_name = "__gmpf_get_default_prec"]
1116    pub fn mpf_get_default_prec() -> bitcnt_t;
1117    /// See: [`mpf_init`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005finit)
1118    #[link_name = "__gmpf_init"]
1119    pub fn mpf_init(x: mpf_ptr);
1120    /// See: [`mpf_init2`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005finit2)
1121    #[link_name = "__gmpf_init2"]
1122    pub fn mpf_init2(x: mpf_ptr, prec: bitcnt_t);
1123    /// See: [`mpf_inits`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005finits)
1124    #[link_name = "__gmpf_inits"]
1125    pub fn mpf_inits(x: mpf_ptr, ...);
1126    /// See: [`mpf_clear`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fclear)
1127    #[link_name = "__gmpf_clear"]
1128    pub fn mpf_clear(x: mpf_ptr);
1129    /// See: [`mpf_clears`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fclears)
1130    #[link_name = "__gmpf_clears"]
1131    pub fn mpf_clears(x: mpf_ptr, ...);
1132    /// See: [`mpf_get_prec`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fget_005fprec)
1133    #[link_name = "__gmpf_get_prec"]
1134    pub fn mpf_get_prec(op: mpf_srcptr) -> bitcnt_t;
1135    /// See: [`mpf_set_prec`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fset_005fprec)
1136    #[link_name = "__gmpf_set_prec"]
1137    pub fn mpf_set_prec(rop: mpf_ptr, prec: bitcnt_t);
1138    /// See: [`mpf_set_prec_raw`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fset_005fprec_005fraw)
1139    #[link_name = "__gmpf_set_prec_raw"]
1140    pub fn mpf_set_prec_raw(rop: mpf_ptr, prec: bitcnt_t);
1141
1142    // Assignment Functions
1143
1144    /// See: [`mpf_set`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fset)
1145    #[link_name = "__gmpf_set"]
1146    pub fn mpf_set(rop: mpf_ptr, op: mpf_srcptr);
1147    /// See: [`mpf_set_ui`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fset_005fui)
1148    #[link_name = "__gmpf_set_ui"]
1149    pub fn mpf_set_ui(rop: mpf_ptr, op: c_ulong);
1150    /// See: [`mpf_set_si`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fset_005fsi)
1151    #[link_name = "__gmpf_set_si"]
1152    pub fn mpf_set_si(rop: mpf_ptr, op: c_long);
1153    /// See: [`mpf_set_default_prec`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fset_005fdefault_005fprec)
1154    #[link_name = "__gmpf_set_d"]
1155    pub fn mpf_set_d(rop: mpf_ptr, op: f64);
1156    /// See: [`mpf_set_z`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fset_005fz)
1157    #[link_name = "__gmpf_set_z"]
1158    pub fn mpf_set_z(rop: mpf_ptr, op: mpz_srcptr);
1159    /// See: [`mpf_set_q`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fset_005fq)
1160    #[link_name = "__gmpf_set_q"]
1161    pub fn mpf_set_q(rop: mpf_ptr, op: mpq_srcptr);
1162    /// See: [`mpf_set_str`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fset_005fstr)
1163    #[link_name = "__gmpf_set_str"]
1164    pub fn mpf_set_str(rop: mpf_ptr, str: *const c_char, base: c_int) -> c_int;
1165    /// See: [`mpf_swap`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fswap)
1166    #[link_name = "__gmpf_swap"]
1167    pub fn mpf_swap(rop1: mpf_ptr, rop2: mpf_ptr);
1168
1169    // Combined Initialization and Assignment Functions
1170
1171    /// See: [`mpf_init_set`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005finit_005fset)
1172    #[link_name = "__gmpf_init_set"]
1173    pub fn mpf_init_set(rop: mpf_ptr, op: mpf_srcptr);
1174    /// See: [`mpf_init_set_ui`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005finit_005fset_005fui)
1175    #[link_name = "__gmpf_init_set_ui"]
1176    pub fn mpf_init_set_ui(rop: mpf_ptr, op: c_ulong);
1177    /// See: [`mpf_init_set_si`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005finit_005fset_005fsi)
1178    #[link_name = "__gmpf_init_set_si"]
1179    pub fn mpf_init_set_si(rop: mpf_ptr, op: c_long);
1180    /// See: [`mpf_init_set_d`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005finit_005fset_005fd)
1181    #[link_name = "__gmpf_init_set_d"]
1182    pub fn mpf_init_set_d(rop: mpf_ptr, op: f64);
1183    /// See: [`mpf_init_set_str`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005finit_005fset_005fstr)
1184    #[link_name = "__gmpf_init_set_str"]
1185    pub fn mpf_init_set_str(rop: mpf_ptr, str: *const c_char, base: c_int) -> c_int;
1186
1187    // Conversion Functions
1188
1189    /// See: [`mpf_get_d`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fget_005fd)
1190    #[link_name = "__gmpf_get_d"]
1191    pub fn mpf_get_d(op: mpf_srcptr) -> f64;
1192    /// See: [`mpf_get_d_2exp`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fget_005fd_005f2exp)
1193    #[link_name = "__gmpf_get_d_2exp"]
1194    pub fn mpf_get_d_2exp(exp: *mut c_long, op: mpf_srcptr) -> f64;
1195    /// See: [`mpf_get_si`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fget_005fsi)
1196    #[link_name = "__gmpf_get_si"]
1197    pub fn mpf_get_si(op: mpf_srcptr) -> c_long;
1198    /// See: [`mpf_get_ui`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fget_005fui)
1199    #[link_name = "__gmpf_get_ui"]
1200    pub fn mpf_get_ui(op: mpf_srcptr) -> c_ulong;
1201    /// See: [`mpf_get_str`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fget_005fstr)
1202    #[link_name = "__gmpf_get_str"]
1203    pub fn mpf_get_str(
1204        str: *mut c_char,
1205        expptr: *mut exp_t,
1206        base: c_int,
1207        n_digits: usize,
1208        op: mpf_srcptr,
1209    ) -> *mut c_char;
1210
1211    // Arithmetic Functions
1212
1213    /// See: [`mpf_add`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fadd)
1214    #[link_name = "__gmpf_add"]
1215    pub fn mpf_add(rop: mpf_ptr, op1: mpf_srcptr, op2: mpf_srcptr);
1216    /// See: [`mpf_add_ui`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fadd_005fui)
1217    #[link_name = "__gmpf_add_ui"]
1218    pub fn mpf_add_ui(rop: mpf_ptr, op1: mpf_srcptr, op2: c_ulong);
1219    /// See: [`mpf_sub`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fsub)
1220    #[link_name = "__gmpf_sub"]
1221    pub fn mpf_sub(rop: mpf_ptr, op1: mpf_srcptr, op2: mpf_srcptr);
1222    /// See: [`mpf_ui_sub`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fui_005fsub)
1223    #[link_name = "__gmpf_ui_sub"]
1224    pub fn mpf_ui_sub(rop: mpf_ptr, op1: c_ulong, op2: mpf_srcptr);
1225    /// See: [`mpf_sub_ui`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fsub_005fui)
1226    #[link_name = "__gmpf_sub_ui"]
1227    pub fn mpf_sub_ui(rop: mpf_ptr, op1: mpf_srcptr, op2: c_ulong);
1228    /// See: [`mpf_mul`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fmul)
1229    #[link_name = "__gmpf_mul"]
1230    pub fn mpf_mul(rop: mpf_ptr, op1: mpf_srcptr, op2: mpf_srcptr);
1231    /// See: [`mpf_mul_ui`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fmul_005fui)
1232    #[link_name = "__gmpf_mul_ui"]
1233    pub fn mpf_mul_ui(rop: mpf_ptr, op1: mpf_srcptr, op2: c_ulong);
1234    /// See: [`mpf_div`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fdiv)
1235    #[link_name = "__gmpf_div"]
1236    pub fn mpf_div(rop: mpf_ptr, op1: mpf_srcptr, op2: mpf_srcptr);
1237    /// See: [`mpf_ui_div`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fui_005fdiv)
1238    #[link_name = "__gmpf_ui_div"]
1239    pub fn mpf_ui_div(rop: mpf_ptr, op1: c_ulong, op2: mpf_srcptr);
1240    /// See: [`mpf_div_ui`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fdiv_005fui)
1241    #[link_name = "__gmpf_div_ui"]
1242    pub fn mpf_div_ui(rop: mpf_ptr, op1: mpf_srcptr, op2: c_ulong);
1243    /// See: [`mpf_sqrt`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fsqrt)
1244    #[link_name = "__gmpf_sqrt"]
1245    pub fn mpf_sqrt(rop: mpf_ptr, op: mpf_srcptr);
1246    /// See: [`mpf_sqrt_ui`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fsqrt_005fui)
1247    #[link_name = "__gmpf_sqrt_ui"]
1248    pub fn mpf_sqrt_ui(rop: mpf_ptr, op: c_ulong);
1249    /// See: [`mpf_pow_ui`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fpow_005fui)
1250    #[link_name = "__gmpf_pow_ui"]
1251    pub fn mpf_pow_ui(rop: mpf_ptr, op1: mpf_srcptr, op2: c_ulong);
1252    /// See: [`mpf_neg`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fneg)
1253    #[link_name = "__gmpf_neg"]
1254    pub fn mpf_neg(rop: mpf_ptr, op: mpf_srcptr);
1255    /// See: [`mpf_abs`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fabs)
1256    #[link_name = "__gmpf_abs"]
1257    pub fn mpf_abs(rop: mpf_ptr, op: mpf_srcptr);
1258    /// See: [`mpf_mul_2exp`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fmul_005f2exp)
1259    #[link_name = "__gmpf_mul_2exp"]
1260    pub fn mpf_mul_2exp(rop: mpf_ptr, op1: mpf_srcptr, op2: bitcnt_t);
1261    /// See: [`mpf_div_2exp`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fdiv_005f2exp)
1262    #[link_name = "__gmpf_div_2exp"]
1263    pub fn mpf_div_2exp(rop: mpf_ptr, op1: mpf_srcptr, op2: bitcnt_t);
1264
1265    // Comparison Functions
1266
1267    /// See: [`mpn_cmp`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fcmp)
1268    #[link_name = "__gmpf_cmp"]
1269    pub fn mpf_cmp(op1: mpf_srcptr, op2: mpf_srcptr) -> c_int;
1270    /// See: [`mpq_cmp_z`](../C/GMP/constant.Rational_Number_Functions.html#index-mpq_005fcmp_005fz)
1271    #[link_name = "__gmpf_cmp_z"]
1272    pub fn mpf_cmp_z(op1: mpf_srcptr, op2: mpz_srcptr) -> c_int;
1273    /// See: [`mpf_cmp_d`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fcmp_005fd)
1274    #[link_name = "__gmpf_cmp_d"]
1275    pub fn mpf_cmp_d(op1: mpf_srcptr, op2: f64) -> c_int;
1276    /// See: [`mpf_cmp_ui`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fcmp_005fui)
1277    #[link_name = "__gmpf_cmp_ui"]
1278    pub fn mpf_cmp_ui(op1: mpf_srcptr, op2: c_ulong) -> c_int;
1279    /// See: [`mpf_cmp_si`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fcmp_005fsi)
1280    #[link_name = "__gmpf_cmp_si"]
1281    pub fn mpf_cmp_si(op1: mpf_srcptr, op2: c_long) -> c_int;
1282    /// See: [`mpf_eq`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005feq)
1283    #[link_name = "__gmpf_eq"]
1284    pub fn mpf_eq(op1: mpf_srcptr, op2: mpf_srcptr, op3: bitcnt_t) -> c_int;
1285    /// See: [`mpf_reldiff`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005freldiff)
1286    #[link_name = "__gmpf_reldiff"]
1287    pub fn mpf_reldiff(rop: mpf_ptr, op1: mpf_srcptr, op2: mpf_srcptr);
1288}
1289/// See: [`mpf_sgn`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fsgn)
1290#[inline]
1291pub const unsafe extern "C" fn mpf_sgn(op: mpf_srcptr) -> c_int {
1292    let size = unsafe { (*op).size };
1293    if size < 0 {
1294        -1
1295    } else {
1296        (size > 0) as c_int
1297    }
1298}
1299
1300// Input and Output Functions
1301
1302extern "C" {
1303    /// See: [`mpf_out_str`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fout_005fstr)
1304    #[link_name = "__gmpf_out_str"]
1305    pub fn mpf_out_str(stream: *mut FILE, base: c_int, n_digits: usize, op: mpf_srcptr) -> usize;
1306    /// See: [`mpf_inp_str`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005finp_005fstr)
1307    #[link_name = "__gmpf_inp_str"]
1308    pub fn mpf_inp_str(rop: mpf_ptr, stream: *mut FILE, base: c_int) -> usize;
1309
1310    // Miscellaneous Functions
1311
1312    /// See: [`mpf_ceil`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005fceil)
1313    #[link_name = "__gmpf_ceil"]
1314    pub fn mpf_ceil(rop: mpf_ptr, op: mpf_srcptr);
1315    /// See: [`mpf_floor`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005ffloor)
1316    #[link_name = "__gmpf_floor"]
1317    pub fn mpf_floor(rop: mpf_ptr, op: mpf_srcptr);
1318    /// See: [`mpf_trunc`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005ftrunc)
1319    #[link_name = "__gmpf_trunc"]
1320    pub fn mpf_trunc(rop: mpf_ptr, op: mpf_srcptr);
1321    /// See: [`mpf_integer_p`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005finteger_005fp)
1322    #[link_name = "__gmpf_integer_p"]
1323    pub fn mpf_integer_p(op: mpf_srcptr) -> c_int;
1324    /// See: [`mpf_fits_ulong_p`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005ffits_005fulong_005fp)
1325    #[link_name = "__gmpf_fits_ulong_p"]
1326    pub fn mpf_fits_ulong_p(op: mpf_srcptr) -> c_int;
1327    /// See: [`mpf_fits_slong_p`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005ffits_005fslong_005fp)
1328    #[link_name = "__gmpf_fits_slong_p"]
1329    pub fn mpf_fits_slong_p(op: mpf_srcptr) -> c_int;
1330    /// See: [`mpf_fits_uint_p`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005ffits_005fuint_005fp)
1331    #[link_name = "__gmpf_fits_uint_p"]
1332    pub fn mpf_fits_uint_p(op: mpf_srcptr) -> c_int;
1333    /// See: [`mpf_fits_sint_p`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005ffits_005fsint_005fp)
1334    #[link_name = "__gmpf_fits_sint_p"]
1335    pub fn mpf_fits_sint_p(op: mpf_srcptr) -> c_int;
1336    /// See: [`mpf_fits_ushort_p`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005ffits_005fushort_005fp)
1337    #[link_name = "__gmpf_fits_ushort_p"]
1338    pub fn mpf_fits_ushort_p(op: mpf_srcptr) -> c_int;
1339    /// See: [`mpf_fits_sshort_p`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005ffits_005fsshort_005fp)
1340    #[link_name = "__gmpf_fits_sshort_p"]
1341    pub fn mpf_fits_sshort_p(op: mpf_srcptr) -> c_int;
1342    /// See: [`mpf_urandomb`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005furandomb)
1343    #[link_name = "__gmpf_urandomb"]
1344    pub fn mpf_urandomb(rop: mpf_t, state: randstate_ptr, nbits: bitcnt_t);
1345    /// See: [`mpf_random2`](../C/GMP/constant.Floating_point_Functions.html#index-mpf_005frandom2)
1346    #[link_name = "__gmpf_random2"]
1347    pub fn mpf_random2(rop: mpf_ptr, max_size: size_t, exp: exp_t);
1348}
1349
1350// Low-Level Functions
1351
1352extern "C" {
1353    /// See: [`mpn_add_n`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fadd_005fn)
1354    #[link_name = "__gmpn_add_n"]
1355    pub fn mpn_add_n(rp: mp_ptr, s1p: mp_srcptr, s2p: mp_srcptr, n: size_t) -> limb_t;
1356    /// See: [`mpn_add_1`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fadd_005f1)
1357    #[link_name = "__gmpn_add_1"]
1358    pub fn mpn_add_1(rp: mp_ptr, s1p: mp_srcptr, n: size_t, s2limb: limb_t) -> limb_t;
1359    /// See: [`mpn_add`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fadd)
1360    #[link_name = "__gmpn_add"]
1361    pub fn mpn_add(rp: mp_ptr, s1p: mp_srcptr, s1n: size_t, s2p: mp_srcptr, s2n: size_t) -> limb_t;
1362    /// See: [`mpn_cnd_sub_n`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fcnd_005fsub_005fn)
1363    #[link_name = "__gmpn_sub_n"]
1364    pub fn mpn_sub_n(rp: mp_ptr, s1p: mp_srcptr, s2p: mp_srcptr, n: size_t) -> limb_t;
1365    /// See: [`mpn_sub_1`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsub_005f1)
1366    #[link_name = "__gmpn_sub_1"]
1367    pub fn mpn_sub_1(rp: mp_ptr, s1p: mp_srcptr, n: size_t, s2limb: limb_t) -> limb_t;
1368    /// See: [`mpn_sub`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsub)
1369    #[link_name = "__gmpn_sub"]
1370    pub fn mpn_sub(rp: mp_ptr, s1p: mp_srcptr, s1n: size_t, s2p: mp_srcptr, s2n: size_t) -> limb_t;
1371    /// See: [`mpn_neg`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fneg)
1372    #[link_name = "__gmpn_neg"]
1373    pub fn mpn_neg(rp: mp_ptr, sp: mp_srcptr, n: size_t) -> limb_t;
1374    /// See: [`mpn_mul_n`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fmul_005fn)
1375    #[link_name = "__gmpn_mul_n"]
1376    pub fn mpn_mul_n(rp: mp_ptr, s1p: mp_srcptr, s2p: mp_srcptr, n: size_t);
1377    /// See: [`mpn_mul`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fmul)
1378    #[link_name = "__gmpn_mul"]
1379    pub fn mpn_mul(rp: mp_ptr, s1p: mp_srcptr, s1n: size_t, s2p: mp_srcptr, s2n: size_t) -> limb_t;
1380    /// See: [`mpn_sqr`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsqr)
1381    #[link_name = "__gmpn_sqr"]
1382    pub fn mpn_sqr(rp: mp_ptr, s1p: mp_srcptr, n: size_t);
1383    /// See: [`mpn_mul_1`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fmul_005f1)
1384    #[link_name = "__gmpn_mul_1"]
1385    pub fn mpn_mul_1(rp: mp_ptr, s1p: mp_srcptr, n: size_t, s2limb: limb_t) -> limb_t;
1386    /// See: [`mpn_addmul_1`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005faddmul_005f1)
1387    #[link_name = "__gmpn_addmul_1"]
1388    pub fn mpn_addmul_1(rp: mp_ptr, s1p: mp_srcptr, n: size_t, s2limb: limb_t) -> limb_t;
1389    /// See: [`mpn_submul_1`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsubmul_005f1)
1390    #[link_name = "__gmpn_submul_1"]
1391    pub fn mpn_submul_1(rp: mp_ptr, s1p: mp_srcptr, n: size_t, s2limb: limb_t) -> limb_t;
1392    /// See: [`mpn_tdiv_qr`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005ftdiv_005fqr)
1393    #[link_name = "__gmpn_tdiv_qr"]
1394    pub fn mpn_tdiv_qr(
1395        qp: mp_ptr,
1396        rp: mp_ptr,
1397        qxn: size_t,
1398        np: mp_srcptr,
1399        nn: size_t,
1400        dp: mp_srcptr,
1401        dn: size_t,
1402    );
1403    /// See: [`mpn_divrem_1`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fdivrem_005f1)
1404    #[link_name = "__gmpn_divrem_1"]
1405    pub fn mpn_divrem_1(
1406        r1p: mp_ptr,
1407        qxn: size_t,
1408        s2p: mp_srcptr,
1409        s2n: size_t,
1410        s3limb: limb_t,
1411    ) -> limb_t;
1412}
1413/// See: [`mpn_divmod_1`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fdivmod_005f1)
1414#[inline]
1415pub unsafe extern "C" fn mpn_divmod_1(
1416    r1p: mp_ptr,
1417    s2p: mp_srcptr,
1418    s2n: size_t,
1419    s3limb: limb_t,
1420) -> limb_t {
1421    unsafe { mpn_divrem_1(r1p, 0, s2p, s2n, s3limb) }
1422}
1423extern "C" {
1424    /// See: [`mpn_divexact_1`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fdivexact_005f1)
1425    #[link_name = "__gmpn_divexact_1"]
1426    pub fn mpn_divexact_1(rp: mp_ptr, sp: mp_srcptr, n: size_t, d: limb_t);
1427}
1428/// See: [`mpn_divexact_by3`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fdivexact_005fby3)
1429#[inline]
1430pub unsafe extern "C" fn mpn_divexact_by3(rp: mp_ptr, sp: mp_srcptr, n: size_t) -> limb_t {
1431    unsafe { mpn_divexact_by3c(rp, sp, n, 0) }
1432}
1433extern "C" {
1434    /// See: [`mpn_divexact_by3c`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fdivexact_005fby3c)
1435    #[link_name = "__gmpn_divexact_by3c"]
1436    pub fn mpn_divexact_by3c(rp: mp_ptr, sp: mp_srcptr, n: size_t, carry: limb_t) -> limb_t;
1437    /// See: [`mpn_divmod_1`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fdivmod_005f1)
1438    #[link_name = "__gmpn_mod_1"]
1439    pub fn mpn_mod_1(s1p: mp_srcptr, s1n: size_t, s2limb: limb_t) -> limb_t;
1440    /// See: [`mpn_lshift`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005flshift)
1441    #[link_name = "__gmpn_lshift"]
1442    pub fn mpn_lshift(rp: mp_ptr, sp: mp_srcptr, n: size_t, count: c_uint) -> limb_t;
1443    /// See: [`mpn_rshift`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005frshift)
1444    #[link_name = "__gmpn_rshift"]
1445    pub fn mpn_rshift(rp: mp_ptr, sp: mp_srcptr, n: size_t, count: c_uint) -> limb_t;
1446    /// See: [`mpn_cmp`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fcmp)
1447    #[link_name = "__gmpn_cmp"]
1448    pub fn mpn_cmp(s1p: mp_srcptr, s2p: mp_srcptr, n: size_t) -> c_int;
1449    /// See: [`mpn_zero_p`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fzero_005fp)
1450    #[link_name = "__gmpn_zero_p"]
1451    pub fn mpn_zero_p(sp: mp_srcptr, n: size_t) -> c_int;
1452    /// See: [`mpn_gcd`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fgcd)
1453    #[link_name = "__gmpn_gcd"]
1454    pub fn mpn_gcd(rp: mp_ptr, xp: mp_ptr, xn: size_t, yp: mp_ptr, yn: size_t) -> size_t;
1455    /// See: [`mpn_gcd_1`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fgcd_005f1)
1456    #[link_name = "__gmpn_gcd_1"]
1457    pub fn mpn_gcd_1(xp: mp_srcptr, xn: size_t, yimb: limb_t) -> limb_t;
1458    /// See: [`mpn_gcdext`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fgcdext)
1459    #[link_name = "__gmpn_gcdext"]
1460    pub fn mpn_gcdext(
1461        gp: mp_ptr,
1462        sp: mp_ptr,
1463        sn: *mut size_t,
1464        up: mp_ptr,
1465        un: size_t,
1466        vp: mp_ptr,
1467        vn: size_t,
1468    ) -> size_t;
1469    /// See: [`mpn_sqrtrem`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsqrtrem)
1470    #[link_name = "__gmpn_sqrtrem"]
1471    pub fn mpn_sqrtrem(r1p: mp_ptr, r2p: mp_ptr, sp: mp_srcptr, n: size_t) -> size_t;
1472    /// See: [`mpn_sizeinbase`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsizeinbase)
1473    #[link_name = "__gmpn_sizeinbase"]
1474    pub fn mpn_sizeinbase(xp: mp_srcptr, n: size_t, base: c_int) -> usize;
1475    /// See: [`mpn_get_str`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fget_005fstr)
1476    #[link_name = "__gmpn_get_str"]
1477    pub fn mpn_get_str(str: *mut c_uchar, base: c_int, s1p: mp_ptr, s1n: size_t) -> usize;
1478    /// See: [`mpn_set_str`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fset_005fstr)
1479    #[link_name = "__gmpn_set_str"]
1480    pub fn mpn_set_str(rp: mp_ptr, str: *const c_uchar, strsize: usize, base: c_int) -> size_t;
1481    /// See: [`mpn_scan0`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fscan0)
1482    #[link_name = "__gmpn_scan0"]
1483    pub fn mpn_scan0(s1p: mp_srcptr, bit: bitcnt_t) -> bitcnt_t;
1484    /// See: [`mpn_scan1`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fscan1)
1485    #[link_name = "__gmpn_scan1"]
1486    pub fn mpn_scan1(s1p: mp_srcptr, bit: bitcnt_t) -> bitcnt_t;
1487    /// See: [`mpn_random`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005frandom)
1488    #[link_name = "__gmpn_random"]
1489    pub fn mpn_random(r1p: mp_ptr, r1n: size_t);
1490    /// See: [`mpn_random2`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005frandom2)
1491    #[link_name = "__gmpn_random2"]
1492    pub fn mpn_random2(r1p: mp_ptr, r1n: size_t);
1493    /// See: [`mpn_popcount`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fpopcount)
1494    #[link_name = "__gmpn_popcount"]
1495    pub fn mpn_popcount(s1p: mp_srcptr, n: size_t) -> bitcnt_t;
1496    /// See: [`mpn_hamdist`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fhamdist)
1497    #[link_name = "__gmpn_hamdist"]
1498    pub fn mpn_hamdist(s1p: mp_srcptr, s2p: mp_srcptr, n: size_t) -> bitcnt_t;
1499    /// See: [`mpn_perfect_square_p`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fperfect_005fsquare_005fp)
1500    #[link_name = "__gmpn_perfect_square_p"]
1501    pub fn mpn_perfect_square_p(s1p: mp_srcptr, n: size_t) -> c_int;
1502    /// See: [`mpn_and_n`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fand_005fn)
1503    #[link_name = "__gmpn_and_n"]
1504    pub fn mpn_and_n(rp: mp_ptr, s1p: mp_srcptr, s2p: mp_srcptr, n: size_t);
1505    /// See: [`mpn_ior_n`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fior_005fn)
1506    #[link_name = "__gmpn_ior_n"]
1507    pub fn mpn_ior_n(rp: mp_ptr, s1p: mp_srcptr, s2p: mp_srcptr, n: size_t);
1508    /// See: [`mpn_xor_n`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fxor_005fn)
1509    #[link_name = "__gmpn_xor_n"]
1510    pub fn mpn_xor_n(rp: mp_ptr, s1p: mp_srcptr, s2p: mp_srcptr, n: size_t);
1511    /// See: [`mpn_andn_n`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fandn_005fn)
1512    #[link_name = "__gmpn_andn_n"]
1513    pub fn mpn_andn_n(rp: mp_ptr, s1p: mp_srcptr, s2p: mp_srcptr, n: size_t);
1514    /// See: [`mpn_iorn_n`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fiorn_005fn)
1515    #[link_name = "__gmpn_iorn_n"]
1516    pub fn mpn_iorn_n(rp: mp_ptr, s1p: mp_srcptr, s2p: mp_srcptr, n: size_t);
1517    /// See: [`mpn_nand_n`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fnand_005fn)
1518    #[link_name = "__gmpn_nand_n"]
1519    pub fn mpn_nand_n(rp: mp_ptr, s1p: mp_srcptr, s2p: mp_srcptr, n: size_t);
1520    /// See: [`mpn_nior_n`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fnior_005fn)
1521    #[link_name = "__gmpn_nior_n"]
1522    pub fn mpn_nior_n(rp: mp_ptr, s1p: mp_srcptr, s2p: mp_srcptr, n: size_t);
1523    /// See: [`mpn_xnor_n`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fxnor_005fn)
1524    #[link_name = "__gmpn_xnor_n"]
1525    pub fn mpn_xnor_n(rp: mp_ptr, s1p: mp_srcptr, s2p: mp_srcptr, n: size_t);
1526    /// See: [`mpn_com`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fcom)
1527    #[link_name = "__gmpn_com"]
1528    pub fn mpn_com(rp: mp_ptr, sp: mp_srcptr, n: size_t);
1529    /// See: [`mpn_copyi`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fcopyi)
1530    #[link_name = "__gmpn_copyi"]
1531    pub fn mpn_copyi(rp: mp_ptr, s1p: mp_srcptr, n: size_t);
1532    /// See: [`mpn_copyd`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fcopyd)
1533    #[link_name = "__gmpn_copyd"]
1534    pub fn mpn_copyd(rp: mp_ptr, s1p: mp_srcptr, n: size_t);
1535    /// See: [`mpn_zero`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fzero)
1536    #[link_name = "__gmpn_zero"]
1537    pub fn mpn_zero(rp: mp_ptr, n: size_t);
1538
1539    // Low-level functions for cryptography
1540
1541    /// See: [`mpn_cnd_add_n`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fcnd_005fadd_005fn)
1542    #[link_name = "__gmpn_cnd_add_n"]
1543    pub fn mpn_cnd_add_n(
1544        cnd: limb_t,
1545        rp: mp_ptr,
1546        s1p: mp_srcptr,
1547        s2p: mp_srcptr,
1548        n: size_t,
1549    ) -> limb_t;
1550    /// See: [`mpn_cnd_sub_n`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fcnd_005fsub_005fn)
1551    #[link_name = "__gmpn_cnd_sub_n"]
1552    pub fn mpn_cnd_sub_n(
1553        cnd: limb_t,
1554        rp: mp_ptr,
1555        s1p: mp_srcptr,
1556        s2p: mp_srcptr,
1557        n: size_t,
1558    ) -> limb_t;
1559    /// See: [`mpn_sec_add_1`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsec_005fadd_005f1)
1560    #[link_name = "__gmpn_sec_add_1"]
1561    pub fn mpn_sec_add_1(rp: mp_ptr, ap: mp_srcptr, n: size_t, b: limb_t, tp: mp_ptr) -> limb_t;
1562    /// See: [`mpn_sec_add_1`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsec_005fadd_005f1)
1563    #[link_name = "__gmpn_sec_add_1_itch"]
1564    pub fn mpn_sec_add_1_itch(n: size_t) -> size_t;
1565    /// See: [`mpn_sec_sub_1`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsec_005fsub_005f1)
1566    #[link_name = "__gmpn_sec_sub_1"]
1567    pub fn mpn_sec_sub_1(rp: mp_ptr, ap: mp_srcptr, n: size_t, b: limb_t, tp: mp_ptr) -> limb_t;
1568    /// See: [`mpn_sec_sub_1`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsec_005fsub_005f1)
1569    #[link_name = "__gmpn_sec_sub_1_itch"]
1570    pub fn mpn_sec_sub_1_itch(n: size_t) -> size_t;
1571    /// See: [`mpn_cnd_swap`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fcnd_005fswap)
1572    #[link_name = "__gmpn_cnd_swap"]
1573    pub fn mpn_cnd_swap(cnd: limb_t, ap: *mut limb_t, bp: *mut limb_t, n: size_t);
1574    /// See: [`mpn_sec_mul`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsec_005fmul)
1575    #[link_name = "__gmpn_sec_mul"]
1576    pub fn mpn_sec_mul(
1577        rp: mp_ptr,
1578        ap: mp_srcptr,
1579        an: size_t,
1580        bp: mp_srcptr,
1581        bn: size_t,
1582        tp: mp_ptr,
1583    );
1584    /// See: [`mpn_sec_mul_itch`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsec_005fmul_005fitch)
1585    #[link_name = "__gmpn_sec_mul_itch"]
1586    pub fn mpn_sec_mul_itch(an: size_t, bn: size_t) -> size_t;
1587    /// See: [`mpn_sec_sqr`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsec_005fsqr)
1588    #[link_name = "__gmpn_sec_sqr"]
1589    pub fn mpn_sec_sqr(rp: mp_ptr, ap: mp_srcptr, an: size_t, tp: mp_ptr);
1590    /// See: [`mpn_sec_sqr_itch`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsec_005fsqr_005fitch)
1591    #[link_name = "__gmpn_sec_sqr_itch"]
1592    pub fn mpn_sec_sqr_itch(an: size_t) -> size_t;
1593    /// See: [`mpn_sec_powm`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsec_005fpowm)
1594    #[link_name = "__gmpn_sec_powm"]
1595    pub fn mpn_sec_powm(
1596        rp: mp_ptr,
1597        bp: mp_srcptr,
1598        bn: size_t,
1599        ep: mp_srcptr,
1600        enb: bitcnt_t,
1601        mp: mp_srcptr,
1602        n: size_t,
1603        tp: mp_ptr,
1604    );
1605    /// See: [`mpn_sec_powm_itch`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsec_005fpowm_005fitch)
1606    #[link_name = "__gmpn_sec_powm_itch"]
1607    pub fn mpn_sec_powm_itch(bn: size_t, enb: bitcnt_t, n: size_t) -> size_t;
1608    /// See: [`mpn_sec_tabselect`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsec_005ftabselect)
1609    #[link_name = "__gmpn_sec_tabselect"]
1610    pub fn mpn_sec_tabselect(
1611        rp: *mut limb_t,
1612        tab: *const limb_t,
1613        n: size_t,
1614        nents: size_t,
1615        which: size_t,
1616    );
1617    /// See: [`mpn_sec_div_qr`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsec_005fdiv_005fqr)
1618    #[link_name = "__gmpn_sec_div_qr"]
1619    pub fn mpn_sec_div_qr(
1620        qp: mp_ptr,
1621        np: mp_ptr,
1622        nn: size_t,
1623        dp: mp_srcptr,
1624        dn: size_t,
1625        tp: mp_ptr,
1626    ) -> limb_t;
1627    /// See: [`mpn_sec_div_qr_itch`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsec_005fdiv_005fqr_005fitch)
1628    #[link_name = "__gmpn_sec_div_qr_itch"]
1629    pub fn mpn_sec_div_qr_itch(nn: size_t, dn: size_t) -> size_t;
1630    /// See: [`mpn_sec_div_r`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsec_005fdiv_005fr)
1631    #[link_name = "__gmpn_sec_div_r"]
1632    pub fn mpn_sec_div_r(np: mp_ptr, nn: size_t, dp: mp_srcptr, dn: size_t, tp: mp_ptr);
1633    /// See: [`mpn_sec_div_r_itch`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsec_005fdiv_005fr_005fitch)
1634    #[link_name = "__gmpn_sec_div_r_itch"]
1635    pub fn mpn_sec_div_r_itch(nn: size_t, dn: size_t) -> size_t;
1636    /// See: [`mpn_sec_invert`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsec_005finvert)
1637    #[link_name = "__gmpn_sec_invert"]
1638    pub fn mpn_sec_invert(
1639        rp: mp_ptr,
1640        ap: mp_ptr,
1641        mp: mp_srcptr,
1642        n: size_t,
1643        nbcnt: bitcnt_t,
1644        tp: mp_ptr,
1645    ) -> c_int;
1646    /// See: [`mpn_sec_invert_itch`](../C/GMP/constant.Low_level_Functions.html#index-mpn_005fsec_005finvert_005fitch)
1647    #[link_name = "__gmpn_sec_invert_itch"]
1648    pub fn mpn_sec_invert_itch(n: size_t) -> size_t;
1649}
1650
1651// Random Numbers
1652
1653// Random State Initialization
1654
1655extern "C" {
1656    /// See: [`gmp_randinit_default`](../C/GMP/constant.Random_Number_Functions.html#index-gmp_005frandinit_005fdefault)
1657    #[link_name = "__gmp_randinit_default"]
1658    pub fn randinit_default(state: randstate_ptr);
1659    /// See: [`gmp_randinit_mt`](../C/GMP/constant.Random_Number_Functions.html#index-gmp_005frandinit_005fmt)
1660    #[link_name = "__gmp_randinit_mt"]
1661    pub fn randinit_mt(state: randstate_ptr);
1662    /// See: [`gmp_randinit_lc_2exp`](../C/GMP/constant.Random_Number_Functions.html#index-gmp_005frandinit_005flc_005f2exp)
1663    #[link_name = "__gmp_randinit_lc_2exp"]
1664    pub fn randinit_lc_2exp(state: randstate_ptr, a: mpz_srcptr, c: c_ulong, m2exp: bitcnt_t);
1665    /// See: [`gmp_randinit_lc_2exp_size`](../C/GMP/constant.Random_Number_Functions.html#index-gmp_005frandinit_005flc_005f2exp_005fsize)
1666    #[link_name = "__gmp_randinit_lc_2exp_size"]
1667    pub fn randinit_lc_2exp_size(state: randstate_ptr, size: bitcnt_t) -> c_int;
1668    /// See: [`gmp_randinit_set`](../C/GMP/constant.Random_Number_Functions.html#index-gmp_005frandinit_005fset)
1669    #[link_name = "__gmp_randinit_set"]
1670    pub fn randinit_set(rop: randstate_ptr, op: randstate_srcptr);
1671    /// See: [`gmp_randclear`](../C/GMP/constant.Random_Number_Functions.html#index-gmp_005frandclear)
1672    #[link_name = "__gmp_randclear"]
1673    pub fn randclear(state: randstate_ptr);
1674
1675    // Random State Seeding
1676
1677    /// See: [`gmp_randseed`](../C/GMP/constant.Random_Number_Functions.html#index-gmp_005frandseed)
1678    #[link_name = "__gmp_randseed"]
1679    pub fn randseed(state: randstate_ptr, seed: mpz_srcptr);
1680    /// See: [`gmp_randseed_ui`](../C/GMP/constant.Random_Number_Functions.html#index-gmp_005frandseed_005fui)
1681    #[link_name = "__gmp_randseed_ui"]
1682    pub fn randseed_ui(state: randstate_ptr, seed: c_ulong);
1683
1684    // Random State Miscellaneous
1685
1686    /// See: [`gmp_urandomb_ui`](../C/GMP/constant.Random_Number_Functions.html#index-gmp_005furandomb_005fui)
1687    #[link_name = "__gmp_urandomb_ui"]
1688    pub fn urandomb_ui(state: randstate_ptr, n: c_ulong) -> c_ulong;
1689    /// See: [`gmp_urandomm_ui`](../C/GMP/constant.Random_Number_Functions.html#index-gmp_005furandomm_005fui)
1690    #[link_name = "__gmp_urandomm_ui"]
1691    pub fn urandomm_ui(state: randstate_ptr, n: c_ulong) -> c_ulong;
1692}
1693
1694// Formatted Output
1695
1696extern "C" {
1697    /// See: [`gmp_printf`](../C/GMP/constant.Formatted_Output.html#index-gmp_005fprintf)
1698    #[link_name = "__gmp_printf"]
1699    pub fn printf(fmt: *const c_char, ...) -> c_int;
1700    /// See: [`gmp_fprintf`](../C/GMP/constant.Formatted_Output.html#index-gmp_005ffprintf)
1701    #[link_name = "__gmp_fprintf"]
1702    pub fn fprintf(fp: *mut FILE, fmt: *const c_char, ...) -> c_int;
1703    /// See: [`gmp_sprintf`](../C/GMP/constant.Formatted_Output.html#index-gmp_005fsprintf)
1704    #[link_name = "__gmp_sprintf"]
1705    pub fn sprintf(buf: *mut c_char, fmt: *const c_char, ...) -> c_int;
1706    /// See: [`gmp_snprintf`](../C/GMP/constant.Formatted_Output.html#index-gmp_005fsnprintf)
1707    #[link_name = "__gmp_snprintf"]
1708    pub fn snprintf(buf: *mut c_char, size: usize, fmt: *const c_char, ...) -> c_int;
1709    /// See: [`gmp_asprintf`](../C/GMP/constant.Formatted_Output.html#index-gmp_005fasprintf)
1710    #[link_name = "__gmp_asprintf"]
1711    pub fn asprintf(pp: *mut *mut c_char, fmt: *const c_char, ...) -> c_int;
1712}
1713
1714// Formatted Input
1715
1716extern "C" {
1717    /// See: [`gmp_scanf`](../C/GMP/constant.Formatted_Input.html#index-gmp_005fscanf)
1718    #[link_name = "__gmp_scanf"]
1719    pub fn scanf(fmt: *const c_char, ...) -> c_int;
1720    /// See: [`gmp_fscanf`](../C/GMP/constant.Formatted_Input.html#index-gmp_005ffscanf)
1721    #[link_name = "__gmp_fscanf"]
1722    pub fn fscanf(fp: *mut FILE, fmt: *const c_char, ...) -> c_int;
1723    /// See: [`gmp_sscanf`](../C/GMP/constant.Formatted_Input.html#index-gmp_005fsscanf)
1724    #[link_name = "__gmp_sscanf"]
1725    pub fn sscanf(s: *const c_char, fmt: *const c_char, ...) -> c_int;
1726}
1727
1728// Custom Allocation
1729
1730/// See: [`allocate_function`](../C/GMP/constant.Custom_Allocation.html#index-allocate_005ffunction)
1731///
1732/// # Planned change
1733///
1734/// In the next major version of the crate (version 2), this will be
1735/// changed to
1736/// `unsafe extern "C" fn(alloc_size: usize) -> *mut c_void`, that is
1737/// it will no longer be an [`Option`], and the function can also be
1738/// unsafe.
1739pub type allocate_function = Option<extern "C" fn(alloc_size: usize) -> *mut c_void>;
1740/// See: [`reallocate_function`](../C/GMP/constant.Custom_Allocation.html#index-reallocate_005ffunction)
1741///
1742/// # Planned change
1743///
1744/// In the next major version of the crate (version 2), this will be
1745/// changed to
1746/// `unsafe extern "C" fn(ptr: *mut c_void, old_size: usize, new_size: usize) -> *mut c_void`,
1747/// that is it will no longer be an [`Option`].
1748pub type reallocate_function =
1749    Option<unsafe extern "C" fn(ptr: *mut c_void, old_size: usize, new_size: usize) -> *mut c_void>;
1750/// See: [`free_function`](../C/GMP/constant.Custom_Allocation.html#index-free_005ffunction)
1751///
1752/// # Planned change
1753///
1754/// In the next major version of the crate (version 2), this will be
1755/// changed to `unsafe extern "C" fn(ptr: *mut c_void, size: usize)`,
1756/// that is it will no longer be an [`Option`].
1757pub type free_function = Option<unsafe extern "C" fn(ptr: *mut c_void, size: usize)>;
1758extern "C" {
1759    /// See: [`mp_set_memory_functions`](../C/GMP/constant.Custom_Allocation.html#index-mp_005fset_005fmemory_005ffunctions)
1760    ///
1761    /// # Planned change
1762    ///
1763    /// In the next major version of the crate (version 2), the arguments will be of types
1764    /// <code>[Option][`Option`]&lt;[allocate\_function][`allocate_function`]&gt;</code>,
1765    /// <code>[Option][`Option`]&lt;[reallocate\_function][`reallocate_function`]&gt;</code>
1766    /// and
1767    /// <code>[Option][`Option`]&lt;[free\_function][`free_function`]&gt;</code>,
1768    /// since the function types themselves will no longer be [`Option`].
1769    #[link_name = "__gmp_set_memory_functions"]
1770    pub fn set_memory_functions(
1771        alloc_func_ptr: allocate_function,
1772        realloc_func_ptr: reallocate_function,
1773        free_func_ptr: free_function,
1774    );
1775    /// See: [`mp_get_memory_functions`](../C/GMP/constant.Custom_Allocation.html#index-mp_005fget_005fmemory_005ffunctions)
1776    #[link_name = "__gmp_get_memory_functions"]
1777    pub fn get_memory_functions(
1778        alloc_func_ptr: *mut allocate_function,
1779        realloc_func_ptr: *mut reallocate_function,
1780        free_func_ptr: *mut free_function,
1781    );
1782}
1783
1784#[cfg(test)]
1785mod tests {
1786    use crate::gmp;
1787    use core::mem;
1788    use core::ptr::NonNull;
1789
1790    #[test]
1791    fn check_mpq_num_den_offsets() {
1792        let mut limbs: [gmp::limb_t; 2] = [1, 1];
1793        let mut q = gmp::mpq_t {
1794            num: gmp::mpz_t {
1795                alloc: 1,
1796                size: 1,
1797                d: NonNull::from(&mut limbs[0]),
1798            },
1799            den: gmp::mpz_t {
1800                alloc: 1,
1801                size: 1,
1802                d: NonNull::from(&mut limbs[1]),
1803            },
1804        };
1805        unsafe {
1806            assert_eq!(&q.num as *const gmp::mpz_t, gmp::mpq_numref_const(&q));
1807            assert_eq!(&q.den as *const gmp::mpz_t, gmp::mpq_denref_const(&q));
1808            assert_eq!(&mut q.num as *mut gmp::mpz_t, gmp::mpq_numref(&mut q));
1809            assert_eq!(&mut q.den as *mut gmp::mpz_t, gmp::mpq_denref(&mut q));
1810        }
1811    }
1812
1813    #[test]
1814    fn check_limb_size() {
1815        let from_static = unsafe { gmp::bits_per_limb };
1816        let from_type = mem::size_of::<gmp::limb_t>() * 8;
1817        let from_constant = gmp::LIMB_BITS;
1818        assert_eq!(from_static as usize, from_type);
1819        assert_eq!(from_static, from_constant);
1820    }
1821
1822    #[test]
1823    fn check_version() {
1824        use crate::tests;
1825
1826        let (major, minor, patchlevel) = (6, 3, 0);
1827        let version = "6.3.0";
1828
1829        assert_eq!(gmp::VERSION, major);
1830        assert!(gmp::VERSION_MINOR >= minor);
1831        if cfg!(not(feature = "use-system-libs")) {
1832            assert!(gmp::VERSION_MINOR > minor || gmp::VERSION_PATCHLEVEL >= patchlevel);
1833            if gmp::VERSION_MINOR == minor && gmp::VERSION_PATCHLEVEL == patchlevel {
1834                assert_eq!(unsafe { tests::str_from_cstr(gmp::version) }, version);
1835            }
1836        }
1837    }
1838}