mpir/
prim.rs

1//! prim
2//!
3
4pub mod typ;
5pub mod mpz;
6pub mod mpf;
7pub mod mpq;
8pub mod randstate;
9pub mod gmp;
10
11use crate::prim::{typ::*, gmp::*}; // mpz::*, mpf::*, mpq::*, randstate::*
12
13/// trait SNew private
14trait SNew {
15  /// new
16  fn new() -> Self;
17}
18
19/// trait AsPtr
20pub trait AsPtr {
21  /// as_ptr
22  fn as_ptr(&self) -> mp_r { self as *const Self as mp_r }
23}
24
25/// trait AsPtrMut
26pub trait AsPtrMut {
27  /// as_ptr_mut
28  fn as_ptr_mut(&mut self) -> mp_t { self as *mut Self as mp_t }
29}
30
31/// to_u8z (&str)
32#[macro_export]
33macro_rules! to_u8z {
34  ($s:expr) => { (String::from($s)+"\0").as_bytes() }
35}
36pub use to_u8z;
37
38/// term_z ([u8])
39#[macro_export]
40macro_rules! term_z {
41  ($u:expr) => { (String::from_utf8($u.to_vec()).unwrap()+"\0").as_bytes() }
42}
43pub use term_z;
44
45/// get length from u8z buffer pointer (terminated 0)
46pub fn u8zlen(p: *mut u8) -> usize {
47  let mut l = 0usize;
48unsafe {
49  loop { // infinit
50    if std::slice::from_raw_parts_mut(p, l + 1)[l] == 0u8 { break; }
51    l += 1;
52  }
53}
54  l
55}
56
57/// get Option vec u8 from u8z buffer pointer (ff: true to free)
58pub fn u8zvec(p: *mut u8, ff: bool) -> Option<Vec<u8>> {
59unsafe {
60  if p == 0 as *mut u8 { None }
61  else {
62    let l = u8zlen(p);
63    let r = std::slice::from_raw_parts_mut(p, l).to_vec();
64    if ff {
65      let mut mp_free: FnPtrFree = __gmp_free_func; // dummy
66      __gmp_get_memory_functions(
67        0 as *mut FnPtrAllocate, 0 as *mut FnPtrReallocate, &mut mp_free);
68      mp_free(p, l + 1);
69    }
70    Some(r)
71  }
72}
73}
74
75/// gmp_printf
76#[inline]
77pub fn gmp_printf<'a, T: AsPtr>(f: &str, a: &'a T) -> () {
78  gmp_printf_u8z(to_u8z!(f), a)
79}
80
81/// gmp_printf_u8z
82#[inline]
83pub fn gmp_printf_u8z<'a, T: AsPtr>(f: &[u8], a: &'a T) -> () {
84  unsafe {
85    __gmp_printf(f as *const [u8] as mp_r,
86      a.as_ptr(), 0 as mp_r, 0 as mp_r, 0 as mp_r)
87  }
88}
89
90/// gmp_printf_1f
91#[inline]
92pub fn gmp_printf_1f<'a, T: AsPtr>(f: &str, p: int_t, a: &'a T) -> () {
93  gmp_printf_u8z_1f(to_u8z!(f), p, a)
94}
95
96/// gmp_printf_u8z_1f
97#[inline]
98pub fn gmp_printf_u8z_1f<'a, T: AsPtr>(f: &[u8], p: int_t, a: &'a T) -> () {
99  unsafe {
100    __gmp_printf(f as *const [u8] as mp_r,
101      p as mp_r, a.as_ptr(), 0 as mp_r, 0 as mp_r)
102  }
103}
104
105/// gmp_printf_2f
106#[inline]
107pub fn gmp_printf_2f<'a, T: AsPtr>(f: &str,
108  p: int_t, a: &'a T, q: int_t, b: &'a T) -> () {
109  gmp_printf_u8z_2f(to_u8z!(f), p, a, q, b)
110}
111
112/// gmp_printf_u8z_2f
113#[inline]
114pub fn gmp_printf_u8z_2f<'a, T: AsPtr>(f: &[u8],
115  p: int_t, a: &'a T, q: int_t, b: &'a T) -> () {
116  unsafe {
117    __gmp_printf(f as *const [u8] as mp_r,
118      p as mp_r, a.as_ptr(), q as mp_r, b.as_ptr())
119  }
120}
121
122/// mp_get_memory_functions
123#[inline]
124pub fn mp_get_memory_functions(
125  alloc: &mut FnPtrAllocate,
126  realloc: &mut FnPtrReallocate,
127  free: &mut FnPtrFree) -> () {
128  unsafe { __gmp_get_memory_functions(alloc, realloc, free) }
129}