1#![cfg_attr(feature = "no_std", no_std)]
49#![cfg_attr(feature = "const_trait_impl", feature(const_trait_impl))]
50#![cfg_attr(feature = "const_mut_refs", feature(const_mut_refs))]
51
52pub mod soft_f32;
53pub mod soft_f64;
54
55const fn abs_diff(a: i32, b: i32) -> u32 {
56 a.wrapping_sub(b).wrapping_abs() as u32
57}
58
59#[cfg(test)]
60mod tests {
61 use crate::soft_f32::SoftF32;
62 use crate::soft_f64::SoftF64;
63
64 const RANGE: core::ops::Range<i32> = -1000..1000;
65 const F32_FACTOR: f32 = 10.0;
66 const F64_FACTOR: f64 = 1000.0;
67
68 #[test]
69 fn f32_add() {
70 for a in RANGE {
71 let a = a as f32 * F32_FACTOR;
72 for b in RANGE {
73 let b = b as f32 * F32_FACTOR;
74 assert_eq!(SoftF32(a).add(SoftF32(b)).0, a + b);
75 }
76 }
77 }
78
79 #[test]
80 fn f32_sub() {
81 for a in RANGE {
82 let a = a as f32 * F32_FACTOR;
83 for b in RANGE {
84 let b = b as f32 * F32_FACTOR;
85 assert_eq!(SoftF32(a).sub(SoftF32(b)).0, a - b);
86 }
87 }
88 }
89
90 #[test]
91 fn f32_mul() {
92 for a in RANGE {
93 let a = a as f32 * F32_FACTOR;
94 for b in RANGE {
95 let b = b as f32 * F32_FACTOR;
96 assert_eq!(SoftF32(a).mul(SoftF32(b)).0, a * b);
97 }
98 }
99 }
100
101 #[test]
102 fn f32_div() {
103 for a in RANGE {
104 let a = a as f32 * F32_FACTOR;
105 for b in RANGE {
106 let b = b as f32 * F32_FACTOR;
107 let x = SoftF32(a).div(SoftF32(b)).0;
108 let y = a / b;
109 assert!(x == y || x.is_nan() && y.is_nan())
110 }
111 }
112 }
113
114 #[test]
115 fn f64_add() {
116 for a in RANGE {
117 let a = a as f64 * F64_FACTOR;
118 for b in RANGE {
119 let b = b as f64 * F64_FACTOR;
120 assert_eq!(SoftF64(a).sub(SoftF64(b)).0, a - b);
121 }
122 }
123 }
124
125 #[test]
126 fn f64_sub() {
127 for a in RANGE {
128 let a = a as f64 * F64_FACTOR;
129 for b in RANGE {
130 let b = b as f64 * F64_FACTOR;
131 assert_eq!(SoftF64(a).sub(SoftF64(b)).0, a - b);
132 }
133 }
134 }
135
136 #[test]
137 fn f64_mul() {
138 for a in RANGE {
139 let a = a as f64 * F64_FACTOR;
140 for b in RANGE {
141 let b = b as f64 * F64_FACTOR;
142 assert_eq!(SoftF64(a).mul(SoftF64(b)).0, a * b);
143 }
144 }
145 }
146
147 #[test]
148 fn f64_div() {
149 for a in RANGE {
150 let a = a as f64 * F64_FACTOR;
151 for b in RANGE {
152 let b = b as f64 * F64_FACTOR;
153 let x = SoftF64(a).div(SoftF64(b)).0;
154 let y = a / b;
155 assert!(x == y || x.is_nan() && y.is_nan())
156 }
157 }
158 }
159}