Skip to content

Commit 02a5650

Browse files
chore: replace magic numbers with expressions
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent f8f2a71 commit 02a5650

File tree

9 files changed

+149
-146
lines changed

9 files changed

+149
-146
lines changed

crates/tinywasm/src/interpreter/num_helpers.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,17 @@ where
99
/// Doing the actual conversion from float to int is a bit tricky, because
1010
/// we need to check for overflow. This macro generates the min/max values
1111
/// for a specific conversion, which are then used in the actual conversion.
12-
/// Rust sadly doesn't have wrapping casts for floats yet, maybe never.
13-
/// Alternatively, <https://crates.io/crates/az> could be used for this but
14-
/// it's not worth the dependency.
15-
#[rustfmt::skip]
12+
/// Rust sadly doesn't have wrapping casts for floats yet.
13+
#[rustfmt::skip]
1614
macro_rules! float_min_max {
17-
(f32, i32) => {(-2147483904.0_f32, 2147483648.0_f32)};
18-
(f64, i32) => {(-2147483649.0_f64, 2147483648.0_f64)};
19-
(f32, u32) => {(-1.0_f32, 4294967296.0_f32)}; // 2^32
20-
(f64, u32) => {(-1.0_f64, 4294967296.0_f64)}; // 2^32
21-
(f32, i64) => {(-9223373136366403584.0_f32, 9223372036854775808.0_f32)}; // 2^63 + 2^40 | 2^63
22-
(f64, i64) => {(-9223372036854777856.0_f64, 9223372036854775808.0_f64)}; // 2^63 + 2^40 | 2^63
23-
(f32, u64) => {(-1.0_f32, 18446744073709551616.0_f32)}; // 2^64
24-
(f64, u64) => {(-1.0_f64, 18446744073709551616.0_f64)}; // 2^64
25-
// other conversions are not allowed
15+
(f32, i32) => {(i32::MIN as f32 - (1 << 8) as f32, i32::MAX as f32 + 1.0)}; // 2^8: f32 precision margin
16+
(f64, i32) => {(i32::MIN as f64 - 1.0, i32::MAX as f64 + 1.0)};
17+
(f32, u32) => {(-1.0_f32, u32::MAX as f32 + 1.0)};
18+
(f64, u32) => {(-1.0_f64, u32::MAX as f64 + 1.0)};
19+
(f32, i64) => {(i64::MIN as f32 - (1i64 << 40) as f32, i64::MAX as f32 + 1.0)}; // 2^40: f32 has 23 mantissa bits
20+
(f64, i64) => {(i64::MIN as f64 - (1i64 << 11) as f64, i64::MAX as f64 + 1.0)}; // 2^11: f64 precision margin
21+
(f32, u64) => {(-1.0_f32, u64::MAX as f32 + 1.0)};
22+
(f64, u64) => {(-1.0_f64, u64::MAX as f64 + 1.0)};
2623
($from:ty, $to:ty) => {compile_error!("invalid float conversion")};
2724
}
2825

0 commit comments

Comments
 (0)