1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
use alloc::alloc::{alloc, alloc_zeroed, dealloc, Layout};
use core::{
borrow::{Borrow, BorrowMut},
fmt,
hash::{Hash, Hasher},
mem,
num::NonZeroUsize,
ops::{Deref, DerefMut, Index, IndexMut, RangeFull},
ptr,
ptr::NonNull,
};
use awint_core::{Bits, InlAwi};
use const_fn::const_fn;
use crate::awint_internals::*;
#[inline]
pub(crate) const fn layout(w: NonZeroUsize) -> Layout {
unsafe {
Layout::from_size_align_unchecked(
raw_digits(w.get()) * mem::size_of::<Digit>(),
mem::align_of::<Digit>(),
)
}
}
#[repr(transparent)]
pub struct ExtAwi {
raw: NonNull<Bits>,
}
unsafe impl Send for ExtAwi {}
unsafe impl Sync for ExtAwi {}
impl<'a> ExtAwi {
#[doc(hidden)]
#[inline]
#[const_fn(cfg(feature = "const_support"))]
pub const unsafe fn from_raw_parts(ptr: *mut Digit, raw_len: usize) -> ExtAwi {
unsafe {
ExtAwi {
raw: NonNull::new_unchecked(Bits::from_raw_parts_mut(ptr, raw_len)),
}
}
}
#[inline]
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
const fn internal_as_ref(&'a self) -> &'a Bits {
unsafe { mem::transmute::<NonNull<Bits>, &Bits>(self.raw) }
}
#[inline]
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
const fn internal_as_mut(&'a mut self) -> &'a mut Bits {
unsafe { mem::transmute::<NonNull<Bits>, &mut Bits>(self.raw) }
}
#[inline]
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn nzbw(&self) -> NonZeroUsize {
self.internal_as_ref().nzbw()
}
#[inline]
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn bw(&self) -> usize {
self.internal_as_ref().bw()
}
#[inline]
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn len(&self) -> usize {
self.internal_as_ref().len()
}
#[doc(hidden)]
#[inline]
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn raw_len(&self) -> usize {
self.internal_as_ref().raw_len()
}
#[doc(hidden)]
#[inline]
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub fn as_ptr(&self) -> *const Digit {
self.internal_as_ref().as_ptr()
}
#[doc(hidden)]
#[inline]
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub fn as_mut_ptr(&mut self) -> *mut Digit {
self.internal_as_mut().as_mut_ptr()
}
#[doc(hidden)]
#[inline]
#[const_fn(cfg(feature = "const_support"))]
#[must_use]
pub const fn layout(&self) -> Layout {
layout(self.nzbw())
}
pub fn from_bits(bits: &Bits) -> ExtAwi {
let mut tmp = ExtAwi::zero(bits.nzbw());
tmp.const_as_mut().copy_(bits).unwrap();
tmp
}
pub fn zero(w: NonZeroUsize) -> Self {
unsafe {
let ptr: *mut Digit = alloc_zeroed(layout(w)).cast();
let regular_digits = regular_digits(w);
const_for!(i in {0..METADATA_DIGITS} {
ptr.add(i + regular_digits).write((w.get() >> (i * BITS)) as Digit);
});
ExtAwi::from_raw_parts(ptr, regular_digits + METADATA_DIGITS)
}
}
pub fn umax(w: NonZeroUsize) -> Self {
let mut x = unsafe {
let ptr: *mut Digit = alloc(layout(w)).cast();
let regular_digits = regular_digits(w);
ptr.write_bytes(u8::MAX, regular_digits);
const_for!(i in {0..METADATA_DIGITS} {
ptr.add(i + regular_digits).write((w.get() >> (i * BITS)) as Digit);
});
ExtAwi::from_raw_parts(ptr, regular_digits + METADATA_DIGITS)
};
x.const_as_mut().clear_unused_bits();
x
}
pub fn imax(w: NonZeroUsize) -> Self {
let mut awi = Self::umax(w);
*awi.const_as_mut().last_mut() = (MAX >> 1) >> awi.unused();
awi
}
pub fn imin(w: NonZeroUsize) -> Self {
let mut awi = Self::zero(w);
*awi.const_as_mut().last_mut() = (IDigit::MIN as Digit) >> awi.unused();
awi
}
pub fn uone(w: NonZeroUsize) -> Self {
let mut awi = Self::zero(w);
*awi.const_as_mut().first_mut() = 1;
awi
}
#[doc(hidden)]
pub fn panicking_zero(w: usize) -> Self {
Self::zero(NonZeroUsize::new(w).unwrap())
}
#[doc(hidden)]
pub fn panicking_umax(w: usize) -> Self {
Self::umax(NonZeroUsize::new(w).unwrap())
}
#[doc(hidden)]
pub fn panicking_imax(w: usize) -> Self {
Self::imax(NonZeroUsize::new(w).unwrap())
}
#[doc(hidden)]
pub fn panicking_imin(w: usize) -> Self {
Self::imin(NonZeroUsize::new(w).unwrap())
}
#[doc(hidden)]
pub fn panicking_uone(w: usize) -> Self {
Self::uone(NonZeroUsize::new(w).unwrap())
}
}
impl Drop for ExtAwi {
fn drop(&mut self) {
unsafe {
dealloc(self.as_mut_ptr().cast(), self.layout());
}
}
}
impl Clone for ExtAwi {
fn clone(&self) -> ExtAwi {
unsafe {
let dst = alloc(self.layout()).cast();
ptr::copy_nonoverlapping(self.as_ptr(), dst, self.raw_len());
ExtAwi::from_raw_parts(dst, self.raw_len())
}
}
}
impl PartialEq for ExtAwi {
fn eq(&self, rhs: &Self) -> bool {
self.as_ref() == rhs.as_ref()
}
}
impl Eq for ExtAwi {}
#[cfg(feature = "zeroize_support")]
impl zeroize::Zeroize for ExtAwi {
fn zeroize(&mut self) {
self.as_mut().zeroize()
}
}
macro_rules! impl_fmt {
($($ty:ident)*) => {
$(
impl fmt::$ty for ExtAwi {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::$ty::fmt(self.as_ref(), f)
}
}
)*
};
}
impl_fmt!(Debug Display LowerHex UpperHex Octal Binary);
impl Hash for ExtAwi {
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_ref().hash(state);
}
}
impl Deref for ExtAwi {
type Target = Bits;
#[inline]
fn deref(&self) -> &Self::Target {
self.internal_as_ref()
}
}
impl DerefMut for ExtAwi {
#[inline]
fn deref_mut(&mut self) -> &mut Bits {
self.internal_as_mut()
}
}
impl Index<RangeFull> for ExtAwi {
type Output = Bits;
#[inline]
fn index(&self, _i: RangeFull) -> &Bits {
self
}
}
impl Borrow<Bits> for ExtAwi {
#[inline]
fn borrow(&self) -> &Bits {
self
}
}
impl AsRef<Bits> for ExtAwi {
#[inline]
fn as_ref(&self) -> &Bits {
self
}
}
impl IndexMut<RangeFull> for ExtAwi {
#[inline]
fn index_mut(&mut self, _i: RangeFull) -> &mut Bits {
self
}
}
impl BorrowMut<Bits> for ExtAwi {
#[inline]
fn borrow_mut(&mut self) -> &mut Bits {
self
}
}
impl AsMut<Bits> for ExtAwi {
#[inline]
fn as_mut(&mut self) -> &mut Bits {
self
}
}
impl From<&Bits> for ExtAwi {
fn from(bits: &Bits) -> ExtAwi {
let mut tmp = ExtAwi::zero(bits.nzbw());
tmp.const_as_mut().copy_(bits).unwrap();
tmp
}
}
impl<const BW: usize, const LEN: usize> From<InlAwi<BW, LEN>> for ExtAwi {
fn from(awi: InlAwi<BW, LEN>) -> ExtAwi {
let mut tmp = ExtAwi::zero(awi.nzbw());
tmp.const_as_mut().copy_(&awi).unwrap();
tmp
}
}
macro_rules! extawi_from_ty {
($($ty:ident $from:ident $assign:ident);*;) => {
$(
pub fn $from(x: $ty) -> Self {
let mut tmp = ExtAwi::zero(bw($ty::BITS as usize));
tmp.$assign(x);
tmp
}
)*
};
}
impl ExtAwi {
extawi_from_ty!(
u8 from_u8 u8_;
u16 from_u16 u16_;
u32 from_u32 u32_;
u64 from_u64 u64_;
u128 from_u128 u128_;
usize from_usize usize_;
i8 from_i8 i8_;
i16 from_i16 i16_;
i32 from_i32 i32_;
i64 from_i64 i64_;
i128 from_i128 i128_;
isize from_isize isize_;
);
pub fn from_bool(x: bool) -> Self {
let mut tmp = ExtAwi::zero(bw(1));
tmp.bool_(x);
tmp
}
pub fn from_digit(x: Digit) -> Self {
let mut tmp = ExtAwi::zero(bw(BITS));
tmp.digit_(x);
tmp
}
}
impl From<bool> for ExtAwi {
fn from(x: bool) -> ExtAwi {
let mut tmp = ExtAwi::zero(bw(1));
tmp.bool_(x);
tmp
}
}
macro_rules! extawi_from {
($($ty:ident, $assign:ident);*;) => {
$(
impl From<$ty> for ExtAwi {
fn from(x: $ty) -> Self {
let mut tmp = ExtAwi::zero(bw($ty::BITS as usize));
tmp.$assign(x);
tmp
}
}
)*
};
}
extawi_from!(
u8, u8_;
u16, u16_;
u32, u32_;
u64, u64_;
u128, u128_;
usize, usize_;
i8, i8_;
i16, i16_;
i32, i32_;
i64, i64_;
i128, i128_;
isize, isize_;
);