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
//! InstructionNumber provides a way to represent the number of Cpu Instruction Number

use std::fmt::{self, Display, Formatter};

use derive_more::{
    Add, AddAssign, Constructor, Div, DivAssign, From, Into, Mul, MulAssign, Neg, Rem, RemAssign,
    Sub, SubAssign, Sum,
};
use libc::c_longlong as c_ll;

/// Represent the number of cpu instruction number
#[derive(
    Clone,
    Copy,
    PartialEq,
    Add,
    Sub,
    From,
    Into,
    Mul,
    Div,
    Sum,
    Constructor,
    Debug,
    PartialOrd,
    Eq,
    Ord,
    Neg,
    Rem,
    SubAssign,
    AddAssign,
    RemAssign,
    DivAssign,
    MulAssign,
)]
pub struct InstructionNumber {
    raw: c_ll,
}

#[allow(clippy::cast_precision_loss)]
impl Display for InstructionNumber {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "{}", self.raw)
    }
}

impl Default for InstructionNumber {
    fn default() -> Self {
        Self::ZERO
    }
}

impl InstructionNumber {
    pub const MAX: Self = Self { raw: c_ll::MAX };
    pub const ZERO: Self = Self { raw: 0 };

    #[must_use]
    pub fn mul_f64(&self, rhs: f64) -> Self {
        Self::new((self.raw as f64 * rhs) as i64)
    }

    #[must_use]
    pub fn div_f64(&self, rhs: f64) -> Self {
        Self::new((self.raw as f64 / rhs) as i64)
    }

    #[must_use]
    pub fn mul_f32(&self, rhs: f32) -> Self {
        Self::new((self.raw as f32 * rhs) as i64)
    }

    #[must_use]
    pub fn div_f32(&self, rhs: f32) -> Self {
        Self::new((self.raw as f32 / rhs) as i64)
    }
}