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
//! Definitions.


/// A word.
#[cfg(target_arch = "x86_64")] 
pub type Word = u64;

/// Doubled word.
#[cfg(target_arch = "x86_64")]
pub type DoubleWord = u128;

/// Word with sign.
#[cfg(target_arch = "x86_64")]
pub type SignedWord = i128;

/// A word.
#[cfg(target_arch = "x86")] 
pub type Word = u32;

/// Doubled word.
#[cfg(target_arch = "x86")] 
pub type DoubleWord = u64;

/// Word with sign.
#[cfg(target_arch = "x86")] 
pub type SignedWord = i64;

/// An exponent.
pub type Exponent = i32;

/// Maximum exponent value.
pub const EXPONENT_MAX: Exponent = Exponent::MAX;

/// Minimum exponent value.
pub const EXPONENT_MIN: Exponent = Exponent::MIN;


/// Maximum value of a word.
pub const WORD_MAX: Word = Word::MAX;

/// Base of words.
pub const WORD_BASE: DoubleWord = WORD_MAX as DoubleWord + 1;

/// Size of a word in bits.
pub const WORD_BIT_SIZE: usize = std::mem::size_of::<Word>() * 8;

// word with the most significant bit set.
pub const WORD_SIGNIFICANT_BIT: Word = WORD_MAX << (WORD_BIT_SIZE - 1);

/// Sign.
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum Sign {

    /// Negative.
    Neg = -1,

    /// Positive.
    Pos = 1,
}

impl Sign {
    /// Changes the sign to the opposite.
    pub fn invert(&self) -> Self {
        match *self {
            Sign::Pos => Sign::Neg,
            Sign::Neg => Sign::Pos,
        }
    }
}

use smallvec::CollectionAllocErr;

/// Possible errors.
#[derive(Debug)]
pub enum Error {
    
    /// The exponent value becomes greater than the upper limit of exponent values or less than the lower limit.
    ExponentOverflow(Sign),

    /// Divizor is zero.
    DivisionByZero,

    /// Invalid argument.
    InvalidArgument,

    /// Memory allocation error.
    MemoryAllocation(CollectionAllocErr),
}


impl PartialEq for Error {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::ExponentOverflow(l0), Self::ExponentOverflow(r0)) => l0 == r0,
            (Self::MemoryAllocation(l0), Self::MemoryAllocation(r0)) => {
                core::mem::discriminant(l0) == core::mem::discriminant(r0)
            },
            _ => core::mem::discriminant(self) == core::mem::discriminant(other),
        }
    }
}


/// Radix.
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
pub enum Radix {

    /// Binary.
    Bin = 2,

    /// Octal.
    Oct = 8,

    /// Decimal.
    Dec = 10,

    /// Hexadecimal.
    Hex = 16,
}

/// Rounding modes.
#[derive(Eq, PartialEq, Debug, Copy, Clone)]
pub enum RoundingMode {
    
    /// Skip rounding operation.
    None,

    /// Round half toward positive infinity.
    Up,

    /// Round half toward negative infinity.
    Down,

    /// Round half toward zero.
    ToZero,

    /// Round half away from zero.
    FromZero,

    /// Round half to even.
    ToEven,

    /// Round half to odd.
    ToOdd,
}