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
// Copyright 2018-2019 Cryptape Technologies LLC.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Define private methods about operators.

use crate::fixed_hash::HashConstructor;
use crate::utils;
use quote::quote;

impl HashConstructor {
    pub fn defun_priv_ops(&self) {
        self.defun_priv_bitwise();
        self.defun_priv_not();
        self.defun_priv_shift();
    }

    fn defun_priv_bitwise(&self) {
        let inner_type = &self.ts.inner_type;
        let loop_unit_amount = &utils::pure_uint_list_to_ts(0..self.info.unit_amount);
        let part = if self.info.expand {
            quote!(
                #[inline]
                fn _bitand(&self, rhs: &Self) -> Self {
                    let mut ret: #inner_type = unsafe { ::std::mem::MaybeUninit::uninit().assume_init() };
                    let inner = self.inner();
                    let rhs = rhs.inner();
                    #({
                        let idx = #loop_unit_amount;
                        ret[idx] = inner[idx] & rhs[idx];
                    })*
                    Self::new(ret)
                }
                #[inline]
                fn _bitor(&self, rhs: &Self) -> Self {
                    let mut ret: #inner_type = unsafe { ::std::mem::MaybeUninit::uninit().assume_init() };
                    let inner = self.inner();
                    let rhs = rhs.inner();
                    #({
                        let idx = #loop_unit_amount;
                        ret[idx] = inner[idx] | rhs[idx];
                    })*
                    Self::new(ret)
                }
                #[inline]
                fn _bitxor(&self, rhs: &Self) -> Self {
                    let mut ret: #inner_type = unsafe { ::std::mem::MaybeUninit::uninit().assume_init() };
                    let inner = self.inner();
                    let rhs = rhs.inner();
                    #({
                        let idx = #loop_unit_amount;
                        ret[idx] = inner[idx] ^ rhs[idx];
                    })*
                    Self::new(ret)
                }
            )
        } else {
            quote!(
                #[inline]
                fn _bitand(&self, rhs: &Self) -> Self {
                    let mut ret: #inner_type =
                        unsafe { ::std::mem::MaybeUninit::uninit().assume_init() };
                    let rhs = rhs.inner();
                    for (idx, lhs) in self.inner().iter().enumerate() {
                        ret[idx] = lhs & rhs[idx];
                    }
                    Self::new(ret)
                }
                #[inline]
                fn _bitor(&self, rhs: &Self) -> Self {
                    let mut ret: #inner_type =
                        unsafe { ::std::mem::MaybeUninit::uninit().assume_init() };
                    let rhs = rhs.inner();
                    for (idx, lhs) in self.inner().iter().enumerate() {
                        ret[idx] = lhs | rhs[idx];
                    }
                    Self::new(ret)
                }
                #[inline]
                fn _bitxor(&self, rhs: &Self) -> Self {
                    let mut ret: #inner_type =
                        unsafe { ::std::mem::MaybeUninit::uninit().assume_init() };
                    let rhs = rhs.inner();
                    for (idx, lhs) in self.inner().iter().enumerate() {
                        ret[idx] = lhs ^ rhs[idx];
                    }
                    Self::new(ret)
                }
            )
        };
        self.defun(part);
    }

    fn defun_priv_not(&self) {
        let inner_type = &self.ts.inner_type;
        let loop_unit_amount = &utils::pure_uint_list_to_ts(0..self.info.unit_amount);
        let part = if self.info.expand {
            quote!(
                #[inline]
                fn _not(&self) -> Self {
                    let mut ret: #inner_type = unsafe { ::std::mem::MaybeUninit::uninit().assume_init() };
                    let inner = self.inner();
                    #({
                        let idx = #loop_unit_amount;
                        ret[idx] = !inner[idx];
                    })*
                    Self::new(ret)
                }
            )
        } else {
            quote!(
                #[inline]
                fn _not(&self) -> Self {
                    let mut ret: #inner_type =
                        unsafe { ::std::mem::MaybeUninit::uninit().assume_init() };
                    let inner = self.inner();
                    for (idx, val) in self.inner().iter().enumerate() {
                        ret[idx] = !val;
                    }
                    Self::new(ret)
                }
            )
        };
        self.defun(part);
    }

    fn defun_priv_shift(&self) {
        let bits_size = &self.ts.bits_size;
        let unit_amount = &self.ts.unit_amount;
        let inner_type = &self.ts.inner_type;
        let part = quote!(
            #[inline]
            fn _ishl(&self, rhs: i128) -> Self {
                match rhs {
                    val if val > 0 => self._ushl(val as u128),
                    val if val < 0 => self._ushr((-val) as u128),
                    _ => self.clone(),
                }
            }
            #[inline]
            fn _ishr(&self, rhs: i128) -> Self {
                match rhs {
                    val if val > 0 => self._ushr(val as u128),
                    val if val < 0 => self._ushl((-val) as u128),
                    _ => self.clone(),
                }
            }
            #[inline]
            fn _ushl(&self, rhs: u128) -> Self {
                if rhs == 0 {
                    return self.clone();
                }
                if rhs < #bits_size {
                    let mut ret: #inner_type = [0; #unit_amount];
                    let src = self.inner();
                    let bit_offset = (rhs & 0x8) as usize;
                    let unit_offset = (rhs / 8) as usize;
                    let mut idx = unit_offset as usize;
                    if bit_offset == 0 {
                        ret[idx] = src[0];
                        idx += 1;
                        while idx < #unit_amount {
                            ret[idx] = src[idx - unit_offset];
                            idx += 1;
                        }
                    } else {
                        let bit_cover = 8 - bit_offset;
                        ret[idx] = src[0] << bit_offset;
                        idx += 1;
                        while idx < #unit_amount {
                            ret[idx] = (src[idx - unit_offset] << bit_offset)
                                | (src[idx - unit_offset - 1] >> bit_cover);
                            idx += 1;
                        }
                    }
                    Self::new(ret)
                } else {
                    Self::empty()
                }
            }
            #[inline]
            fn _ushr(&self, rhs: u128) -> Self {
                if rhs == 0 {
                    return self.clone();
                }
                if rhs < #bits_size {
                    let mut ret: #inner_type = [0; #unit_amount];
                    let src = self.inner();
                    let bit_offset = (rhs & 0x8) as usize;
                    let unit_offset = (rhs / 8) as usize;
                    let mut idx = 0;
                    if bit_offset == 0 {
                        while idx < #unit_amount - unit_offset - 1 {
                            ret[idx] = src[idx + unit_offset];
                            idx += 1;
                        }
                        ret[idx] = src[idx + unit_offset];
                    } else {
                        let bit_cover = 8 - bit_offset;
                        while idx < #unit_amount - unit_offset - 1 {
                            ret[idx] = (src[idx + unit_offset] >> bit_offset)
                                | (src[idx + unit_offset + 1] << bit_cover);
                            idx += 1;
                        }
                        ret[idx] = src[idx + unit_offset] >> bit_offset;
                    }
                    Self::new(ret)
                } else {
                    Self::empty()
                }
            }
        );
        self.defun(part);
    }
}