Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for Windows denormal bug in float_to_str_bytes_common #14080

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/libstd/num/strconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,34 @@ pub fn float_to_str_bytes_common<T:NumCast+Zero+One+Eq+Ord+Float+
ExpBin => (num.abs().log2().floor(), cast::<f64, T>(2.0f64).unwrap()),
ExpNone => unreachable!()
};
let iexp = cast::<T, i32>(exp).unwrap();

// For subnormal floats, powi and powf behave differently
// depending on the platform/compiler. For example, consider:
//
// 2f64.pow(-1074) [where pow = powf or powi]
//
// - On Mingw (vanilla), powf returns zero whereas powi
// returns a subnormal.
//
// - On Mingw-w64, both powi and powf return subnormals.
//
// - On Linux, powi returns zero whereas powf returns a
// subnormal.
//
// This workaround accounts for the difference in behavior and
// fixes part of issue #13439.
#[cfg(windows)]
fn pow<T: Float>(exp_base: T, _: T, iexp: i32) -> T {
exp_base.powi(iexp)
}

#[cfg(not(windows))]
fn pow<T: Float>(exp_base: T, exp: T, _: i32) -> T {
exp_base.powf(exp)
}

(num / exp_base.powf(exp), cast::<T, i32>(exp).unwrap())
(num / pow(exp_base, exp, iexp), iexp)
}
}
};
Expand Down