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
// This file is part of the un-prim.
//
// Copyright (C) 2022 Ade M Ramdani
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//!
//! unprim contains primitive types from 8 into 256 bit.
//! it is unstable and not intended for production use.
//!
//! ```rust
//! use un_prim::*;
//!
//! let a = U256::from(100);
//! let b = U256::from(2);
//!
//! assert_eq!(a * b, 200u64.into());
//! ```
//! Or you can use `.into()` method to init the types.
//!
//! ```rust
//! use un_prim::*;
//!
//! let a: U24 = 100u64.into();
//! let b: U24 = 2u64.into();
//! let c: u32 = (a * b).into();
//!
//! assert_eq!(c, 200);
//! ```
//!
//! You can use macro to define new types. In example if you want to
//! define a type with 512 bit, you can use the macro.
//! ```rust
//!
//! use un_prim::*;
//!
//! define!(U512, 64, "512 bit");
//!
//! fn main() {
//!     let a = U512::from(100);
//!     let b = U512::from(2);
//!     let c = a * b;
//!     assert_eq!(c, 200u64.into());
//! }
pub use std::{
    ops::{
        Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div,
        DivAssign, Mul, MulAssign, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub,
        SubAssign,
    },
    str::FromStr,
};

pub use serde::{Deserialize, Serialize};

#[macro_use]
pub mod macros;

pub fn mul8(x: u8, y: u8) -> (u8, u8) {
    let z = x as u16 * y as u16;
    ((z >> 8) as u8, z as u8)
}

pub fn mul_r(z: u8, x: u8, y: u8) -> (u8, u8) {
    let (hi, lo) = mul8(x, y);
    let (lo, c) = lo.overflowing_add(z);
    let (hi, _) = hi.overflowing_add(c as u8);
    (hi, lo)
}

pub fn mul_carry(z: u8, x: u8, y: u8, carry: u8) -> (u8, u8) {
    let (hi, lo) = mul8(x, y);
    let (lo, c) = lo.overflowing_add(carry);
    let (hi, _) = hi.overflowing_add(c as u8);
    let (lo, c) = lo.overflowing_add(z);
    let (hi, _) = hi.overflowing_add(c as u8);
    (hi, lo)
}

/// Computes 16-bit division of two 8-bit numbers and return the quotient and remainder.
pub fn div8(hi: u8, lo: u8, y: u8) -> (u8, u8) {
    if y != 0 && y <= hi {
        panic!("forbidden");
    }

    let z = (hi as u16) << 8 | lo as u16;
    ((z / y as u16) as u8, (z % y as u16) as u8)
}

/// Computes <!d, !0> / d.
pub fn reciprocal_2by1(d: u8) -> u8 {
    let (reciprocal, _) = div8(!d, !0u8, d);
    reciprocal
}

/// Devides <uh, ul> / d, returns the quotient and remainder.
/// It use provided d's reciprocal.
/// Implementation is ported from https://github.com/holiman/uint250.
pub fn div_rem_2by1(uh: u8, ul: u8, d: u8, reciprocal: u8) -> (u8, u8) {
    let (qh, ql) = mul8(reciprocal, uh);
    let (ql, carry) = ql.overflowing_add(ul);
    let (mut qh, _) = qh.overflowing_add(uh);
    qh = qh.overflowing_add(carry as u8 + 1).0;

    let (mut r, _) = ul.overflowing_sub((qh as u16 * d as u16) as u8);

    if r > ql {
        qh = qh.overflowing_sub(1).0;
        r = r.overflowing_add(d).0;
    }

    if r >= d {
        qh = qh.overflowing_add(1).0;
        r = r.overflowing_sub(d).0;
    }

    (qh, r)
}

/// Computes x -= y * multiplier.
/// requires: len(x) >= len(y).
pub fn sub_mul(nx: &[u8], y: &[u8], multiplier: u8) -> (Vec<u8>, u8) {
    let mut x = vec![0u8; nx.len()];
    x.copy_from_slice(nx);
    let mut borrow = 0u8;

    for i in 0..y.len() {
        let (s, carry1) = x[i].overflowing_sub(borrow);
        let (ph, pl) = mul8(y[i], multiplier);
        let (t, carry2) = s.overflowing_sub(pl);
        x[i] = t;
        borrow = ph + carry1 as u8 + carry2 as u8;
    }

    (x, borrow)
}

/// Computes x += y where x and y is a slice.
/// requires: len(x) >= len(y).
pub fn add_slice(x: &[u8], y: &[u8]) -> (Vec<u8>, u8) {
    let mut x = vec![0u8; x.len()];
    let mut carry = false;
    for i in 0..y.len() {
        (x[i], carry) = x[i].overflowing_add(y[i] + carry as u8);
    }
    (x, carry as u8)
}

define!(
    U24,
    3,
    "24-bit unsigned integer represented as little-endian byte order."
);

define!(
    U40,
    5,
    "40-bit unsigned integer represented as little-endian byte order."
);

define!(
    U48,
    6,
    "48-bit unsigned integer represented as little-endian byte order."
);

define!(
    U56,
    7,
    "56-bit unsigned integer represented as little-endian byte order."
);

define!(
    U72,
    9,
    "72-bit unsigned integer represented as little-endian byte order."
);

define!(
    U80,
    10,
    "80-bit unsigned integer represented as little-endian byte order."
);

define!(
    U88,
    11,
    "88-bit unsigned integer represented as little-endian byte order."
);

define!(
    U96,
    12,
    "96-bit unsigned integer represented as little-endian byte order."
);

define!(
    U104,
    13,
    "104-bit unsigned integer represented as little-endian byte order."
);

define!(
    U112,
    14,
    "112-bit unsigned integer represented as little-endian byte order."
);

define!(
    U120,
    15,
    "120-bit unsigned integer represented as little-endian byte order."
);

define!(
    U136,
    17,
    "136-bit unsigned integer represented as little-endian byte order."
);

define!(
    U144,
    18,
    "144-bit unsigned integer represented as little-endian byte order."
);

define!(
    U152,
    19,
    "152-bit unsigned integer represented as little-endian byte order."
);

define!(
    U160,
    20,
    "160-bit unsigned integer represented as little-endian byte order."
);

define!(
    U168,
    21,
    "168-bit unsigned integer represented as little-endian byte order."
);

define!(
    U176,
    22,
    "176-bit unsigned integer represented as little-endian byte order."
);

define!(
    U184,
    23,
    "184-bit unsigned integer represented as little-endian byte order."
);

define!(
    U192,
    24,
    "192-bit unsigned integer represented as little-endian byte order."
);

define!(
    U200,
    25,
    "200-bit unsigned integer represented as little-endian byte order."
);

define!(
    U208,
    26,
    "208-bit unsigned integer represented as little-endian byte order."
);

define!(
    U216,
    27,
    "216-bit unsigned integer represented as little-endian byte order."
);

define!(
    U224,
    28,
    "224-bit unsigned integer represented as little-endian byte order."
);

define!(
    U232,
    29,
    "232-bit unsigned integer represented as little-endian byte order."
);

define!(
    U240,
    30,
    "240-bit unsigned integer represented as little-endian byte order."
);

define!(
    U248,
    31,
    "248-bit unsigned integer represented as little-endian byte order."
);

define!(
    U256,
    32,
    "256-bit unsigned integer represented as little-endian byte order."
);

#[cfg(test)]
mod test_un_prim {
    use super::*;

    #[test]
    fn test_macro() {
        define!(
            U8,
            1,
            "8-bit unsigned integer represented as little-endian byte order."
        );

        let x: U8 = 200u8.into();
        assert_eq!(200u8, x.into());
    }
}