miden_stdlib_sys/intrinsics/
word.rs

1use core::ops::{Index, IndexMut};
2
3use super::felt::Felt;
4use crate::felt;
5
6#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
7#[repr(C, align(32))]
8pub struct Word {
9    pub inner: (Felt, Felt, Felt, Felt),
10}
11impl Word {
12    pub const fn new(word: [Felt; 4]) -> Self {
13        Self {
14            inner: (word[0], word[1], word[2], word[3]),
15        }
16    }
17}
18impl From<[Felt; 4]> for Word {
19    fn from(word: [Felt; 4]) -> Self {
20        Self {
21            inner: (word[0], word[1], word[2], word[3]),
22        }
23    }
24}
25impl From<Word> for [Felt; 4] {
26    #[inline(always)]
27    fn from(word: Word) -> Self {
28        [word.inner.0, word.inner.1, word.inner.2, word.inner.3]
29    }
30}
31impl From<Felt> for Word {
32    fn from(value: Felt) -> Self {
33        Word {
34            inner: (felt!(0), felt!(0), felt!(0), value),
35        }
36    }
37}
38impl From<Word> for Felt {
39    fn from(value: Word) -> Self {
40        value.inner.3
41    }
42}
43impl Index<usize> for Word {
44    type Output = Felt;
45
46    #[inline(always)]
47    fn index(&self, index: usize) -> &Self::Output {
48        match index {
49            0 => &self.inner.0,
50            1 => &self.inner.1,
51            2 => &self.inner.2,
52            3 => &self.inner.3,
53            _ => unreachable!(),
54        }
55    }
56}
57impl IndexMut<usize> for Word {
58    #[inline(always)]
59    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
60        match index {
61            0 => &mut self.inner.0,
62            1 => &mut self.inner.1,
63            2 => &mut self.inner.2,
64            3 => &mut self.inner.3,
65            _ => unreachable!(),
66        }
67    }
68}
69
70impl AsRef<Word> for Word {
71    fn as_ref(&self) -> &Word {
72        self
73    }
74}