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
// Copyright © 2018–2022 Trevor Spiteri

// This library is free software: you can redistribute it and/or
// modify it under the terms of either
//
//   * the Apache License, Version 2.0 or
//   * the MIT License
//
// at your option.
//
// You should have recieved copies of the Apache License and the MIT
// License along with the library. If not, see
// <https://www.apache.org/licenses/LICENSE-2.0> and
// <https://opensource.org/licenses/MIT>.

use crate::{
    FixedI128, FixedI16, FixedI32, FixedI64, FixedI8, FixedU128, FixedU16, FixedU32, FixedU64,
    FixedU8, F128,
};
use az::{Cast, CheckedCast, OverflowingCast, SaturatingCast, UnwrappedCast, WrappingCast};
use half::{bf16, f16};

macro_rules! cast {
    ($Src:ident($nbits_src:expr); $Dst:ident($nbits_dst:expr)) => {
        impl<const SRC_FRAC: i32, const DST_FRAC: i32> Cast<$Dst<DST_FRAC>> for $Src<SRC_FRAC> {
            #[inline]
            fn cast(self) -> $Dst<DST_FRAC> {
                self.to_num()
            }
        }

        impl<const SRC_FRAC: i32, const DST_FRAC: i32> CheckedCast<$Dst<DST_FRAC>>
            for $Src<SRC_FRAC>
        {
            #[inline]
            fn checked_cast(self) -> Option<$Dst<DST_FRAC>> {
                self.checked_to_num()
            }
        }

        impl<const SRC_FRAC: i32, const DST_FRAC: i32> SaturatingCast<$Dst<DST_FRAC>>
            for $Src<SRC_FRAC>
        {
            #[inline]
            fn saturating_cast(self) -> $Dst<DST_FRAC> {
                self.saturating_to_num()
            }
        }

        impl<const SRC_FRAC: i32, const DST_FRAC: i32> WrappingCast<$Dst<DST_FRAC>>
            for $Src<SRC_FRAC>
        {
            #[inline]
            fn wrapping_cast(self) -> $Dst<DST_FRAC> {
                self.wrapping_to_num()
            }
        }

        impl<const SRC_FRAC: i32, const DST_FRAC: i32> OverflowingCast<$Dst<DST_FRAC>>
            for $Src<SRC_FRAC>
        {
            #[inline]
            fn overflowing_cast(self) -> ($Dst<DST_FRAC>, bool) {
                self.overflowing_to_num()
            }
        }

        impl<const SRC_FRAC: i32, const DST_FRAC: i32> UnwrappedCast<$Dst<DST_FRAC>>
            for $Src<SRC_FRAC>
        {
            #[inline]
            #[track_caller]
            fn unwrapped_cast(self) -> $Dst<DST_FRAC> {
                self.unwrapped_to_num()
            }
        }
    };

    ($Fixed:ident($nbits:expr); $Dst:ident) => {
        impl<const FRAC: i32> Cast<$Dst> for $Fixed<FRAC> {
            #[inline]
            fn cast(self) -> $Dst {
                self.to_num()
            }
        }

        impl<const FRAC: i32> CheckedCast<$Dst> for $Fixed<FRAC> {
            #[inline]
            fn checked_cast(self) -> Option<$Dst> {
                self.checked_to_num()
            }
        }

        impl<const FRAC: i32> SaturatingCast<$Dst> for $Fixed<FRAC> {
            #[inline]
            fn saturating_cast(self) -> $Dst {
                self.saturating_to_num()
            }
        }

        impl<const FRAC: i32> WrappingCast<$Dst> for $Fixed<FRAC> {
            #[inline]
            fn wrapping_cast(self) -> $Dst {
                self.wrapping_to_num()
            }
        }

        impl<const FRAC: i32> OverflowingCast<$Dst> for $Fixed<FRAC> {
            #[inline]
            fn overflowing_cast(self) -> ($Dst, bool) {
                self.overflowing_to_num()
            }
        }

        impl<const FRAC: i32> UnwrappedCast<$Dst> for $Fixed<FRAC> {
            #[inline]
            #[track_caller]
            fn unwrapped_cast(self) -> $Dst {
                self.unwrapped_to_num()
            }
        }
    };

    ($Src:ident; $Fixed:ident($nbits:expr)) => {
        impl<const FRAC: i32> Cast<$Fixed<FRAC>> for $Src {
            #[inline]
            fn cast(self) -> $Fixed<FRAC> {
                <$Fixed<FRAC>>::from_num(self)
            }
        }

        impl<const FRAC: i32> CheckedCast<$Fixed<FRAC>> for $Src {
            #[inline]
            fn checked_cast(self) -> Option<$Fixed<FRAC>> {
                <$Fixed<FRAC>>::checked_from_num(self)
            }
        }

        impl<const FRAC: i32> SaturatingCast<$Fixed<FRAC>> for $Src {
            #[inline]
            fn saturating_cast(self) -> $Fixed<FRAC> {
                <$Fixed<FRAC>>::saturating_from_num(self)
            }
        }

        impl<const FRAC: i32> WrappingCast<$Fixed<FRAC>> for $Src {
            #[inline]
            fn wrapping_cast(self) -> $Fixed<FRAC> {
                <$Fixed<FRAC>>::wrapping_from_num(self)
            }
        }

        impl<const FRAC: i32> OverflowingCast<$Fixed<FRAC>> for $Src {
            #[inline]
            fn overflowing_cast(self) -> ($Fixed<FRAC>, bool) {
                <$Fixed<FRAC>>::overflowing_from_num(self)
            }
        }

        impl<const FRAC: i32> UnwrappedCast<$Fixed<FRAC>> for $Src {
            #[inline]
            #[track_caller]
            fn unwrapped_cast(self) -> $Fixed<FRAC> {
                <$Fixed<FRAC>>::unwrapped_from_num(self)
            }
        }
    };
}

macro_rules! cast_num {
    ($Src:ident($nbits_src:expr); $($Dst:ident($nbits_dst:expr),)*) => { $(
        cast! { $Src($nbits_src); $Dst($nbits_dst) }
    )* };
    ($Fixed:ident($nbits:expr); $($Num:ident,)*) => { $(
        cast! { $Fixed($nbits); $Num }
        cast! { $Num; $Fixed($nbits) }
    )* };
    ($($Fixed:ident($nbits:expr),)*) => { $(
        cast_num! {
            $Fixed($nbits);
            FixedI8(8), FixedI16(16), FixedI32(32), FixedI64(64), FixedI128(128),
            FixedU8(8), FixedU16(16), FixedU32(32), FixedU64(64), FixedU128(128),
        }
        cast! { bool; $Fixed($nbits) }
        cast_num! {
            $Fixed($nbits);
            i8, i16, i32, i64, i128, isize,
            u8, u16, u32, u64, u128, usize,
            f16, bf16, f32, f64, F128,
        }
    )* };
}

cast_num! {
    FixedI8(8), FixedI16(16), FixedI32(32), FixedI64(64), FixedI128(128),
    FixedU8(8), FixedU16(16), FixedU32(32), FixedU64(64), FixedU128(128),
}