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
mod constants;
pub use constants::{BTRIT_0, BTRIT_1, BTRIT_NEG_1, UTRIT_0, UTRIT_1, UTRIT_2, UTRIT_U384_MAX, UTRIT_U384_MAX_HALF};
use crate::ternary::bigint::{
binary_representation::{U32Repr, U8Repr},
endianness::{BigEndian, LittleEndian},
I384, T242, U384,
};
use bee_ternary::{Btrit, ShiftTernary, T1B1Buf, Trit, TritBuf, Utrit};
use std::cmp::Ordering;
def_and_impl_ternary!(T243, 243);
impl<T: Trit> T243<T> {
pub fn into_t242(self) -> T242<T> {
let mut trit_buf = self.into_inner();
trit_buf.pop();
T242::new(trit_buf)
}
}
impl T243<Utrit> {
pub fn from_u384(value: U384<BigEndian, U32Repr>) -> Self {
let mut u384_value = value;
let mut u384_inner_slice = &mut u384_value.inner[..];
let mut trit_buf = T243::<Utrit>::zero().into_inner();
unsafe {
for trit in trit_buf.as_i8_slice_mut() {
let mut rem = 0;
let mut first_digit = 0;
for (i, digit) in u384_inner_slice.iter().enumerate() {
first_digit = i;
if *digit != 0 {
break;
}
}
u384_inner_slice = &mut u384_inner_slice[first_digit..];
for digit in u384_inner_slice.iter_mut() {
let digit_with_rem = (u64::from(rem) << 32) | u64::from(*digit);
#[allow(clippy::cast_possible_truncation)]
{
*digit = (digit_with_rem / 3u64) as u32;
rem = (digit_with_rem % 3u64) as u32;
}
}
#[allow(clippy::cast_possible_truncation)]
{
*trit = rem as i8;
}
}
}
Self(trit_buf)
}
}
impl<T: Trit> From<T242<T>> for T243<T> {
fn from(value: T242<T>) -> Self {
value.into_t243()
}
}
impl From<I384<BigEndian, U8Repr>> for T243<Btrit> {
fn from(value: I384<BigEndian, U8Repr>) -> Self {
let be_u32 = Into::<I384<BigEndian, U32Repr>>::into(value);
let le_u32 = Into::<I384<LittleEndian, U32Repr>>::into(be_u32);
le_u32.into()
}
}
impl From<I384<BigEndian, U32Repr>> for T243<Btrit> {
fn from(value: I384<BigEndian, U32Repr>) -> Self {
let value_little_endian: I384<LittleEndian, U32Repr> = value.into();
value_little_endian.into()
}
}
impl From<I384<LittleEndian, U32Repr>> for T243<Btrit> {
fn from(value: I384<LittleEndian, U32Repr>) -> Self {
let u384_value = value.shift_into_u384();
let t243_unbalanced = T243::<Utrit>::from(u384_value);
t243_unbalanced.into_shifted()
}
}
impl From<U384<BigEndian, U32Repr>> for T243<Utrit> {
fn from(value: U384<BigEndian, U32Repr>) -> Self {
Self::from_u384(value)
}
}
impl From<U384<LittleEndian, U32Repr>> for T243<Utrit> {
fn from(value: U384<LittleEndian, U32Repr>) -> Self {
let value: U384<BigEndian, U32Repr> = value.into();
value.into()
}
}