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
//! Buffer for holding mantissa gidits.

use crate::defs::Error;
use crate::defs::Word;
use crate::defs::WORD_BIT_SIZE;
use core::ops::Deref;
use core::ops::DerefMut;
use core::ops::Index;
use core::ops::IndexMut;
use core::slice::SliceIndex;
use smallvec::SmallVec;

const STATIC_ALLOCATION: usize = 5;

/// Buffer for holding mantissa gidits.
#[derive(Debug)]
pub struct WordBuf {
    inner: SmallVec<[Word; STATIC_ALLOCATION]>,
}

impl WordBuf {
    #[inline]
    pub fn new(sz: usize) -> Result<Self, Error> {
        let mut inner = SmallVec::new();
        inner
            .try_reserve_exact(sz)
            .map_err(Error::MemoryAllocation)?;
        unsafe {
            // values of the newely allocated words stay unitialized for performance reasons
            inner.set_len(sz);
        }
        Ok(WordBuf { inner })
    }

    #[inline]
    pub fn fill(&mut self, d: Word) {
        self.inner.fill(d);
    }

    #[inline]
    pub fn len(&self) -> usize {
        self.inner.len()
    }

    /// Decrease length of mantissa to l bits.
    pub fn trunc_to(&mut self, l: usize) {
        let n = (l + WORD_BIT_SIZE - 1) / WORD_BIT_SIZE;
        let sz = self.len();
        self.inner.rotate_left(sz - n);
        self.inner.truncate(n);
    }

    /// Try to exted the size to fit the precision p. Fill new elements with 0. Data is shifted to the left.
    pub fn try_extend(&mut self, p: usize) -> Result<(), Error> {
        let n = (p + WORD_BIT_SIZE - 1) / WORD_BIT_SIZE;
        let l = self.inner.len();
        self.inner.try_grow(n).map_err(Error::MemoryAllocation)?;
        unsafe {
            // values of the newely allocated words stay unitialized for performance reasons
            self.inner.set_len(n);
        }
        self.inner.rotate_right(n - l);
        self.inner[..n - l].fill(0);
        Ok(())
    }

    /// Try to exted the size to fit the precision p. Fill new elements with 0. Data is not moved.
    pub fn try_extend_2(&mut self, p: usize) -> Result<(), Error> {
        let n = (p + WORD_BIT_SIZE - 1) / WORD_BIT_SIZE;
        let l = self.inner.len();
        self.inner.try_grow(n).map_err(Error::MemoryAllocation)?;
        unsafe {
            // values of the newely allocated words stay unitialized for performance reasons
            self.inner.set_len(n);
        }
        self.inner[l..].fill(0);
        Ok(())
    }

    // Remove trailing words containing zeroes.
    pub fn trunc_trailing_zeroes(&mut self) {
        let mut n = 0;

        for v in self.inner.iter() {
            if *v == 0 {
                n += 1;
            } else {
                break;
            }
        }

        if n > 0 {
            let sz = self.len();
            self.inner.rotate_left(n);
            self.inner.truncate(sz - n);
        }
    }

    // Remove leading words containing zeroes.
    pub fn trunc_leading_zeroes(&mut self) {
        let mut n = 0;

        for v in self.inner.iter().rev() {
            if *v == 0 {
                n += 1;
            } else {
                break;
            }
        }

        if n > 0 {
            let sz = self.len();
            self.inner.truncate(sz - n);
        }
    }
}

impl<I: SliceIndex<[Word]>> IndexMut<I> for WordBuf {
    #[inline]
    fn index_mut(&mut self, index: I) -> &mut Self::Output {
        self.inner.index_mut(index)
    }
}

impl<I: SliceIndex<[Word]>> Index<I> for WordBuf {
    type Output = I::Output;

    #[inline]
    fn index(&self, index: I) -> &Self::Output {
        self.inner.index(index)
    }
}

impl Deref for WordBuf {
    type Target = [Word];

    #[inline]
    fn deref(&self) -> &[Word] {
        self.inner.deref()
    }
}

impl DerefMut for WordBuf {
    #[inline]
    fn deref_mut(&mut self) -> &mut [Word] {
        self.inner.deref_mut()
    }
}