| 1 | use crate::{ |
| 2 | AssertFloatEq, AssertFloatEqAll, DebugUlpsDiff, FloatEq, FloatEqAll, FloatEqDebugUlpsDiff, |
| 3 | FloatEqUlpsTol, UlpsTol, |
| 4 | }; |
| 5 | use core::mem::MaybeUninit; |
| 6 | |
| 7 | // Uses the same technique as MaybeUninit::uninit_array. |
| 8 | #[inline(always)] |
| 9 | fn uninit_array<T, const N: usize>() -> [MaybeUninit<T>; N] { |
| 10 | unsafe { MaybeUninit::<[MaybeUninit<T>; N]>::uninit().assume_init() } |
| 11 | } |
| 12 | |
| 13 | // Uses the same technique as MaybeUninit::array_assume_init. |
| 14 | #[inline(always)] |
| 15 | unsafe fn array_assume_init<T, const N: usize>(array: [MaybeUninit<T>; N]) -> [T; N] { |
| 16 | (&array as *const _ as *const [T; N]).read() |
| 17 | } |
| 18 | |
| 19 | impl<T: FloatEqUlpsTol, const N: usize> FloatEqUlpsTol for [T; N] |
| 20 | where |
| 21 | UlpsTol<T>: Sized, |
| 22 | { |
| 23 | type UlpsTol = [UlpsTol<T>; N]; |
| 24 | } |
| 25 | |
| 26 | impl<T: FloatEqDebugUlpsDiff, const N: usize> FloatEqDebugUlpsDiff for [T; N] { |
| 27 | type DebugUlpsDiff = [DebugUlpsDiff<T>; N]; |
| 28 | } |
| 29 | |
| 30 | impl<A, B, const N: usize> FloatEq<[B; N]> for [A; N] |
| 31 | where |
| 32 | A: FloatEq<B>, |
| 33 | A::Tol: Sized, |
| 34 | UlpsTol<A::Tol>: Sized, |
| 35 | { |
| 36 | type Tol = [A::Tol; N]; |
| 37 | |
| 38 | #[inline] |
| 39 | fn eq_abs(&self, other: &[B; N], tol: &Self::Tol) -> bool { |
| 40 | for i in 0..N { |
| 41 | if !self[i].eq_abs(&other[i], &tol[i]) { |
| 42 | return false; |
| 43 | } |
| 44 | } |
| 45 | true |
| 46 | } |
| 47 | |
| 48 | #[inline] |
| 49 | fn eq_rmax(&self, other: &[B; N], tol: &Self::Tol) -> bool { |
| 50 | for i in 0..N { |
| 51 | if !self[i].eq_rmax(&other[i], &tol[i]) { |
| 52 | return false; |
| 53 | } |
| 54 | } |
| 55 | true |
| 56 | } |
| 57 | |
| 58 | #[inline] |
| 59 | fn eq_rmin(&self, other: &[B; N], tol: &Self::Tol) -> bool { |
| 60 | for i in 0..N { |
| 61 | if !self[i].eq_rmin(&other[i], &tol[i]) { |
| 62 | return false; |
| 63 | } |
| 64 | } |
| 65 | true |
| 66 | } |
| 67 | |
| 68 | #[inline] |
| 69 | fn eq_r1st(&self, other: &[B; N], tol: &Self::Tol) -> bool { |
| 70 | for i in 0..N { |
| 71 | if !self[i].eq_r1st(&other[i], &tol[i]) { |
| 72 | return false; |
| 73 | } |
| 74 | } |
| 75 | true |
| 76 | } |
| 77 | |
| 78 | #[inline] |
| 79 | fn eq_r2nd(&self, other: &[B; N], tol: &Self::Tol) -> bool { |
| 80 | for i in 0..N { |
| 81 | if !self[i].eq_r2nd(&other[i], &tol[i]) { |
| 82 | return false; |
| 83 | } |
| 84 | } |
| 85 | true |
| 86 | } |
| 87 | |
| 88 | #[inline] |
| 89 | fn eq_ulps(&self, other: &[B; N], tol: &UlpsTol<Self::Tol>) -> bool { |
| 90 | for i in 0..N { |
| 91 | if !self[i].eq_ulps(&other[i], &tol[i]) { |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | true |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | impl<A, B, const N: usize> FloatEqAll<[B; N]> for [A; N] |
| 100 | where |
| 101 | A: FloatEqAll<B>, |
| 102 | { |
| 103 | type AllTol = A::AllTol; |
| 104 | |
| 105 | #[inline] |
| 106 | fn eq_abs_all(&self, other: &[B; N], tol: &Self::AllTol) -> bool { |
| 107 | self.iter() |
| 108 | .zip(other.iter()) |
| 109 | .all(|(a, b)| a.eq_abs_all(b, tol)) |
| 110 | } |
| 111 | |
| 112 | #[inline] |
| 113 | fn eq_rmax_all(&self, other: &[B; N], tol: &Self::AllTol) -> bool { |
| 114 | self.iter() |
| 115 | .zip(other.iter()) |
| 116 | .all(|(a, b)| a.eq_rmax_all(b, tol)) |
| 117 | } |
| 118 | |
| 119 | #[inline] |
| 120 | fn eq_rmin_all(&self, other: &[B; N], tol: &Self::AllTol) -> bool { |
| 121 | self.iter() |
| 122 | .zip(other.iter()) |
| 123 | .all(|(a, b)| a.eq_rmin_all(b, tol)) |
| 124 | } |
| 125 | |
| 126 | #[inline] |
| 127 | fn eq_r1st_all(&self, other: &[B; N], tol: &Self::AllTol) -> bool { |
| 128 | self.iter() |
| 129 | .zip(other.iter()) |
| 130 | .all(|(a, b)| a.eq_r1st_all(b, tol)) |
| 131 | } |
| 132 | |
| 133 | #[inline] |
| 134 | fn eq_r2nd_all(&self, other: &[B; N], tol: &Self::AllTol) -> bool { |
| 135 | self.iter() |
| 136 | .zip(other.iter()) |
| 137 | .all(|(a, b)| a.eq_r2nd_all(b, tol)) |
| 138 | } |
| 139 | |
| 140 | #[inline] |
| 141 | fn eq_ulps_all(&self, other: &[B; N], tol: &UlpsTol<Self::AllTol>) -> bool { |
| 142 | self.iter() |
| 143 | .zip(other.iter()) |
| 144 | .all(|(a, b)| a.eq_ulps_all(b, tol)) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | impl<A, B, const N: usize> AssertFloatEq<[B; N]> for [A; N] |
| 149 | where |
| 150 | A: AssertFloatEq<B>, |
| 151 | A::Tol: Sized, |
| 152 | A::DebugTol: Sized, |
| 153 | UlpsTol<A::Tol>: Sized, |
| 154 | UlpsTol<A::DebugTol>: Sized, |
| 155 | { |
| 156 | type DebugAbsDiff = [A::DebugAbsDiff; N]; |
| 157 | type DebugTol = [A::DebugTol; N]; |
| 158 | |
| 159 | #[inline] |
| 160 | fn debug_abs_diff(&self, other: &[B; N]) -> Self::DebugAbsDiff { |
| 161 | let mut result: [MaybeUninit<A::DebugAbsDiff>; N] = uninit_array(); |
| 162 | for i in 0..N { |
| 163 | result[i] = MaybeUninit::new(self[i].debug_abs_diff(&other[i])); |
| 164 | } |
| 165 | unsafe { array_assume_init(result) } |
| 166 | } |
| 167 | |
| 168 | #[inline] |
| 169 | fn debug_ulps_diff(&self, other: &[B; N]) -> DebugUlpsDiff<Self::DebugAbsDiff> { |
| 170 | let mut result: [MaybeUninit<DebugUlpsDiff<A::DebugAbsDiff>>; N] = uninit_array(); |
| 171 | for i in 0..N { |
| 172 | result[i] = MaybeUninit::new(self[i].debug_ulps_diff(&other[i])); |
| 173 | } |
| 174 | unsafe { array_assume_init(result) } |
| 175 | } |
| 176 | |
| 177 | #[inline] |
| 178 | fn debug_abs_tol(&self, other: &[B; N], tol: &Self::Tol) -> Self::DebugTol { |
| 179 | let mut result: [MaybeUninit<A::DebugTol>; N] = uninit_array(); |
| 180 | for i in 0..N { |
| 181 | result[i] = MaybeUninit::new(self[i].debug_abs_tol(&other[i], &tol[i])); |
| 182 | } |
| 183 | unsafe { array_assume_init(result) } |
| 184 | } |
| 185 | |
| 186 | #[inline] |
| 187 | fn debug_rmax_tol(&self, other: &[B; N], tol: &Self::Tol) -> Self::DebugTol { |
| 188 | let mut result: [MaybeUninit<A::DebugTol>; N] = uninit_array(); |
| 189 | for i in 0..N { |
| 190 | result[i] = MaybeUninit::new(self[i].debug_rmax_tol(&other[i], &tol[i])); |
| 191 | } |
| 192 | unsafe { array_assume_init(result) } |
| 193 | } |
| 194 | |
| 195 | #[inline] |
| 196 | fn debug_rmin_tol(&self, other: &[B; N], tol: &Self::Tol) -> Self::DebugTol { |
| 197 | let mut result: [MaybeUninit<A::DebugTol>; N] = uninit_array(); |
| 198 | for i in 0..N { |
| 199 | result[i] = MaybeUninit::new(self[i].debug_rmin_tol(&other[i], &tol[i])); |
| 200 | } |
| 201 | unsafe { array_assume_init(result) } |
| 202 | } |
| 203 | |
| 204 | #[inline] |
| 205 | fn debug_r1st_tol(&self, other: &[B; N], tol: &Self::Tol) -> Self::DebugTol { |
| 206 | let mut result: [MaybeUninit<A::DebugTol>; N] = uninit_array(); |
| 207 | for i in 0..N { |
| 208 | result[i] = MaybeUninit::new(self[i].debug_r1st_tol(&other[i], &tol[i])); |
| 209 | } |
| 210 | unsafe { array_assume_init(result) } |
| 211 | } |
| 212 | |
| 213 | #[inline] |
| 214 | fn debug_r2nd_tol(&self, other: &[B; N], tol: &Self::Tol) -> Self::DebugTol { |
| 215 | let mut result: [MaybeUninit<A::DebugTol>; N] = uninit_array(); |
| 216 | for i in 0..N { |
| 217 | result[i] = MaybeUninit::new(self[i].debug_r2nd_tol(&other[i], &tol[i])); |
| 218 | } |
| 219 | unsafe { array_assume_init(result) } |
| 220 | } |
| 221 | |
| 222 | #[inline] |
| 223 | fn debug_ulps_tol(&self, other: &[B; N], tol: &UlpsTol<Self::Tol>) -> UlpsTol<Self::DebugTol> { |
| 224 | let mut result: [MaybeUninit<UlpsTol<A::DebugTol>>; N] = uninit_array(); |
| 225 | for i in 0..N { |
| 226 | result[i] = MaybeUninit::new(self[i].debug_ulps_tol(&other[i], &tol[i])); |
| 227 | } |
| 228 | unsafe { array_assume_init(result) } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | impl<A, B, const N: usize> AssertFloatEqAll<[B; N]> for [A; N] |
| 233 | where |
| 234 | A: AssertFloatEqAll<B>, |
| 235 | UlpsTol<A::AllDebugTol>: Sized, |
| 236 | { |
| 237 | type AllDebugTol = [A::AllDebugTol; N]; |
| 238 | |
| 239 | #[inline] |
| 240 | fn debug_abs_all_tol(&self, other: &[B; N], tol: &Self::AllTol) -> Self::AllDebugTol { |
| 241 | let mut result: [MaybeUninit<A::AllDebugTol>; N] = uninit_array(); |
| 242 | for i in 0..N { |
| 243 | result[i] = MaybeUninit::new(self[i].debug_abs_all_tol(&other[i], tol)); |
| 244 | } |
| 245 | unsafe { array_assume_init(result) } |
| 246 | } |
| 247 | |
| 248 | #[inline] |
| 249 | fn debug_rmax_all_tol(&self, other: &[B; N], tol: &Self::AllTol) -> Self::AllDebugTol { |
| 250 | let mut result: [MaybeUninit<A::AllDebugTol>; N] = uninit_array(); |
| 251 | for i in 0..N { |
| 252 | result[i] = MaybeUninit::new(self[i].debug_rmax_all_tol(&other[i], tol)); |
| 253 | } |
| 254 | unsafe { array_assume_init(result) } |
| 255 | } |
| 256 | |
| 257 | #[inline] |
| 258 | fn debug_rmin_all_tol(&self, other: &[B; N], tol: &Self::AllTol) -> Self::AllDebugTol { |
| 259 | let mut result: [MaybeUninit<A::AllDebugTol>; N] = uninit_array(); |
| 260 | for i in 0..N { |
| 261 | result[i] = MaybeUninit::new(self[i].debug_rmin_all_tol(&other[i], tol)); |
| 262 | } |
| 263 | unsafe { array_assume_init(result) } |
| 264 | } |
| 265 | |
| 266 | #[inline] |
| 267 | fn debug_r1st_all_tol(&self, other: &[B; N], tol: &Self::AllTol) -> Self::AllDebugTol { |
| 268 | let mut result: [MaybeUninit<A::AllDebugTol>; N] = uninit_array(); |
| 269 | for i in 0..N { |
| 270 | result[i] = MaybeUninit::new(self[i].debug_r1st_all_tol(&other[i], tol)); |
| 271 | } |
| 272 | unsafe { array_assume_init(result) } |
| 273 | } |
| 274 | |
| 275 | #[inline] |
| 276 | fn debug_r2nd_all_tol(&self, other: &[B; N], tol: &Self::AllTol) -> Self::AllDebugTol { |
| 277 | let mut result: [MaybeUninit<A::AllDebugTol>; N] = uninit_array(); |
| 278 | for i in 0..N { |
| 279 | result[i] = MaybeUninit::new(self[i].debug_r2nd_all_tol(&other[i], tol)); |
| 280 | } |
| 281 | unsafe { array_assume_init(result) } |
| 282 | } |
| 283 | |
| 284 | #[inline] |
| 285 | fn debug_ulps_all_tol( |
| 286 | &self, |
| 287 | other: &[B; N], |
| 288 | tol: &UlpsTol<Self::AllTol>, |
| 289 | ) -> UlpsTol<Self::AllDebugTol> { |
| 290 | let mut result: [MaybeUninit<UlpsTol<A::AllDebugTol>>; N] = uninit_array(); |
| 291 | for i in 0..N { |
| 292 | result[i] = MaybeUninit::new(self[i].debug_ulps_all_tol(&other[i], tol)); |
| 293 | } |
| 294 | unsafe { array_assume_init(result) } |
| 295 | } |
| 296 | } |