c_prime/
func.rs

1use machine_prime::PRIME_INV_64;
2
3/// More efficient primality for compile-time evaluation
4const fn small_prime(x: u64) -> bool{
5   if x==2{
6      return true;
7   }
8   if x&1==0{
9     return false;
10   }
11   let mut counter=0usize;
12   
13   while counter<66{
14     let prod = PRIME_INV_64[counter].wrapping_mul(x);
15     if prod == 1{
16        return true
17     }
18     if prod < x {
19        return false;
20    }
21     counter+=1;
22   }
23   return true;
24}
25
26const fn bounded_search(mut x: u128, stride: u128) -> u128{
27    loop{
28     x= x.wrapping_add(stride);
29     
30     if x == 0 || x == u128::MAX{
31       panic!("Exceeded bounds");
32     }
33     
34     if x < 109560{
35      let n = x as u64;
36       if small_prime(n){
37          return x;     
38       }
39     }
40     else{
41     if machine_prime::is_prime_128(x){
42        return x;
43     }
44    } 
45   }
46}
47/// slightly faster to eek out performance in compile-time evaluation
48const fn bounded_search_64(mut x: u64, stride: u64) -> u64{
49loop{
50     x= x.wrapping_add(stride);
51     
52     if x == 0 || x == u64::MAX{
53       panic!("Exceeded bounds");
54     }
55     
56     if x < 109561{
57       if small_prime(x){
58          return x;     
59       }
60     }
61     else{
62     if machine_prime::is_prime(x){
63        return x;
64     }
65    } 
66   }
67}
68
69const fn next_prime_64(x: u64) -> u64{
70     bounded_search_64(x,1)
71}
72/// Returns the next prime
73pub const fn next_prime(x: u128) -> u128{
74    bounded_search(x,1)
75}
76
77/// Returns the previous prime
78pub const fn prev_prime(x: u128) -> u128{
79   bounded_search(x,u128::MAX)
80}
81
82/// Initialises an  array of sequential 128-bit primes
83///
84/// An array is initialised with the primes sequentially from START. This can be calculated at compile time
85pub const fn prime_array<const S: usize>(mut start: u128) -> [u128;S]{
86    let mut counter = 0usize;
87    let mut valarray : [u128;S] = [0u128;S];
88    loop{
89        start=next_prime(start);
90        valarray[counter]=start;
91        counter+=1;    
92        if counter == S{
93          return valarray;
94        }
95      }
96}
97
98
99/// Initialises an  array of sequential 64-bit primes
100pub const fn prime_array_64<const S: usize>(mut start: u64) -> [u64;S]{
101    let mut counter = 0usize;
102    let mut valarray : [u64;S] = [0u64;S];
103    loop{
104        let interim = next_prime_64(start);
105        start= interim;
106        valarray[counter]=start;
107        counter+=1;    
108        if counter == S{
109          return valarray;
110        }
111      }
112}
113
114/// Initialises an  array of sequential 32-bit primes
115pub const fn prime_array_32<const S: usize>(mut start: u32) -> [u32;S]{
116    let mut counter = 0usize;
117    let mut valarray : [u32;S] = [0u32;S];
118    loop{
119         let interim = next_prime_64(start as u64);
120        if interim >= 1u64<<32{
121           panic!("Exceeded the datatype max");
122        }
123        start=interim as u32;
124        valarray[counter]=start;
125        counter+=1;    
126        if counter == S{
127          return valarray;
128        }
129      }
130}
131
132/// Initialises an  array of sequential 16-bit primes
133pub const fn prime_array_16<const S: usize>(mut start: u16) -> [u16;S]{
134    let mut counter = 0usize;
135    let mut valarray : [u16;S] = [0u16;S];
136    loop{
137        
138         let interim = next_prime_64(start as u64);
139        if interim >= 1u64<<16{
140           panic!("Exceeded the datatype max");
141        }
142        start=interim as u16;
143        valarray[counter]=start;
144        counter+=1;    
145        if counter == S{
146          return valarray;
147        }
148      }
149}
150
151/// Initialises an array of sequantial multiplicative inverses of primes over Z/2^64
152pub const fn prime_inv_array_64<const S: usize>(mut start: u64) -> [u64;S]{
153    let mut counter = 0usize;
154    if start < 2{
155       panic!("2 has no inverse over 2^64");
156    }
157    let mut valarray : [u64;S] = [0u64;S];
158    loop{
159        start=next_prime_64(start);
160        valarray[counter]=machine_prime::mul_inv2(start);
161        counter+=1;    
162        if counter == S{
163          return valarray;
164        }
165      }
166}
167
168/// Initialises an  array of sequential multiplicative inverses of primes over Z/2^128
169pub const fn prime_inv_array<const S: usize>(mut start: u128) -> [u128;S]{
170    let mut counter = 0usize;
171    if start < 2{
172       panic!("2 has no multiplicative inverse over 2^128");
173    }
174    let mut valarray : [u128;S] = [0u128;S];
175    loop{
176        start=next_prime(start);
177        valarray[counter]=machine_prime::mul_inv2_128(start);
178        counter+=1;    
179        if counter == S{
180          return valarray;
181        }
182      }
183}