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
// numera::number::integer::z::ops::neg
//
//! Implement the negation operations.
//
use crate::number::integer::*;
use core::ops::Neg;
use devela::paste;
macro_rules! impl_integer_neg {
// impl Neg ops for multiple integer types
//
// # Args
// $t: integer base name. e.g. Integer
// $p: inner primitive base name. e.g. i
// $b: integer and primitive bitsize. e.g. 8
( $($t:ident + $p:ident + $b:literal, cast: $bcast:literal);+ ) => {
$( impl_integer_neg![neg: $t + $p + $b]; )+
};
// impl the negation operations
//
// impl variants:
// - neg
// - checked_
// - saturating_
// - wrapping_
(neg: $t:ident + $p:ident + $b:literal) => { paste! {
impl Neg for [<$t$b>] {
type Output = [<$t$b>];
/// Performs the unary `-` operation.
#[inline]
#[must_use]
fn neg(self) -> Self::Output {
self.neg()
}
}
/// # Negation
impl [<$t$b>] {
/// Basic negation. Computes `-self`.
///
/// # Panics
/// Panics in debug, if `self == MIN`.
/// While in release, it returns `MIN`,
#[doc = "same as [`wrapping_neg`][" [<$t$b>] "#method.wrapping_neg]"]
#[inline]
#[must_use]
pub const fn neg(self) -> [<$t$b>] {
[<$t$b>](-self.0)
}
/// Checked negation.
/// Computes `-self`, returning `None` if `self == MIN` instead of overflowing.
#[inline]
#[must_use]
pub const fn checked_neg(self) -> Option<[<$t$b>]> {
if let Some(result) = self.0.checked_neg() {
Some(Self(result))
} else {
None
}
}
/// Saturating negation.
/// Computes `-self`, returning `MAX` if `self == MIN` instead of overflowing.
#[inline]
#[must_use]
pub const fn saturating_neg(self) -> [<$t$b>] {
Self(self.0.saturating_neg())
}
/// Wrapping negation.
/// Computes `-self`, returning `MIN` if `self == MIN` instead of overflowing.
#[inline]
#[must_use]
pub const fn wrapping_neg(self) -> [<$t$b>] {
Self(self.0.wrapping_neg())
}
/// Overflowing negation.
/// Negates self, overflowing if this is equal to the minimum value.
///
/// Returns a tuple of the negated version along with a boolean
/// indicating whether an arithmetic overflow happened.
/// If `self == MIN` then (`MIN`, `true`) is returned.
#[inline]
#[must_use]
pub const fn overflowing_neg(self) -> ([<$t$b>], bool) {
let (result, overflown) = self.0.overflowing_neg();
(Self(result), overflown)
}
}
}};
}
impl_integer_neg![
Integer+i+8, cast:16;
Integer+i+16, cast:32;
Integer+i+32, cast:64;
Integer+i+64, cast:128;
Integer+i+128, cast:128
];
#[cfg(feature = "dashu-int")]
mod big {
use super::*;
impl Neg for IntegerBig {
type Output = IntegerBig;
/// Performs the unary `-` operation.
#[inline]
#[must_use]
fn neg(self) -> Self::Output {
Self(-self.0)
}
}
}