Skip to content

Commit

Permalink
x86: Improve handling of x87
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Aug 25, 2024
1 parent 072558e commit c6f914b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,11 @@ jobs:
RUSTFLAGS: ${{ env.RUSTFLAGS }} -C target-feature=-sse2
RUSTDOCFLAGS: ${{ env.RUSTDOCFLAGS }} -C target-feature=-sse2
if: startsWith(matrix.target, 'i686')
# x86 x87
# x86 -x87
- run: tools/test.sh -vv --tests $TARGET $BUILD_STD $RELEASE
env:
# i586 is -C target-feature=+x87 by default, but cfg(target_feature = "x87") doesn't work.
RUSTFLAGS: ${{ env.RUSTFLAGS }} --cfg target_feature="x87"
RUSTDOCFLAGS: ${{ env.RUSTDOCFLAGS }} --cfg target_feature="x87"
RUSTFLAGS: ${{ env.RUSTFLAGS }} -C target-feature=+soft-float,-x87,-mmx
RUSTDOCFLAGS: ${{ env.RUSTDOCFLAGS }} -C target-feature=+soft-float,-x87,-mmx
if: startsWith(matrix.target, 'i586')
# aarch64 +lse
- run: tools/test.sh -vv --tests $TARGET $BUILD_STD $RELEASE
Expand Down
11 changes: 10 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn main() {
// TODO: handle multi-line target_feature_fallback
// grep -E 'target_feature_fallback\("' build.rs | sed -E 's/^.*target_feature_fallback\(//; s/",.*$/"/' | LC_ALL=C sort -u | tr '\n' ','
println!(
r#"cargo:rustc-check-cfg=cfg(atomic_maybe_uninit_target_feature,values("a","cmpxchg16b","fast-serialization","lse","lse128","lse2","mclass","partword-atomics","quadword-atomics","rcpc","rcpc3","v5te","v6","v7","v8","v8m"))"#
r#"cargo:rustc-check-cfg=cfg(atomic_maybe_uninit_target_feature,values("a","cmpxchg16b","fast-serialization","lse","lse128","lse2","mclass","partword-atomics","quadword-atomics","rcpc","rcpc3","v5te","v6","v7","v8","v8m","x87"))"#
);
}

Expand Down Expand Up @@ -108,6 +108,15 @@ fn main() {
|| target_os == "watchos"
|| target_os == "visionos";
match target_arch {
"x86" => {
// i586 is -C target-feature=+x87 by default, but cfg(target_feature = "x87") doesn't work.
// And core assumes x87 is always available.
// https://github.com/rust-lang/rust/blob/1.80.0/library/core/src/num/dec2flt/fpu.rs#L6
// https://github.com/rust-lang/rust/blob/1.80.0/compiler/rustc_target/src/spec/targets/i686_unknown_uefi.rs#L24
// However, custom bare metal targets tend to disable x87 and do not use floats.
let has_x87 = target_os != "none";
target_feature_fallback("x87", has_x87);
}
"x86_64" => {
// cmpxchg16b_target_feature stabilized in Rust 1.69.
if needs_target_feature_fallback(&version, Some(69)) {
Expand Down
12 changes: 4 additions & 8 deletions src/arch/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,7 @@ macro_rules! atomic64 {
core::mem::transmute::<_, [MaybeUninit<Self>; 2]>(out)[0]
}
#[cfg(not(target_feature = "sse"))]
// It seems core assumes x87 target_feature is always available?
// https://github.com/rust-lang/rust/blob/1.80.0/library/core/src/num/dec2flt/fpu.rs#L6
#[cfg(target_feature = "x87")]
#[cfg(any(target_feature = "x87", atomic_maybe_uninit_target_feature = "x87"))]
// SAFETY: the caller must uphold the safety contract.
//
// Refs:
Expand Down Expand Up @@ -279,7 +277,7 @@ macro_rules! atomic64 {
out
}
#[cfg(not(target_feature = "sse"))]
#[cfg(not(target_feature = "x87"))]
#[cfg(not(any(target_feature = "x87", atomic_maybe_uninit_target_feature = "x87")))]
// SAFETY: the caller must uphold the safety contract.
//
// Refs: https://www.felixcloutier.com/x86/cmpxchg8b:cmpxchg16b
Expand Down Expand Up @@ -348,9 +346,7 @@ macro_rules! atomic64 {
}
}
#[cfg(not(target_feature = "sse"))]
// It seems core assumes x87 target_feature is always available?
// https://github.com/rust-lang/rust/blob/1.80.0/library/core/src/num/dec2flt/fpu.rs#L6
#[cfg(target_feature = "x87")]
#[cfg(any(target_feature = "x87", atomic_maybe_uninit_target_feature = "x87"))]
// SAFETY: the caller must uphold the safety contract.
//
// Refs:
Expand Down Expand Up @@ -402,7 +398,7 @@ macro_rules! atomic64 {
}
}
#[cfg(not(target_feature = "sse"))]
#[cfg(not(target_feature = "x87"))]
#[cfg(not(any(target_feature = "x87", atomic_maybe_uninit_target_feature = "x87")))]
// SAFETY: the caller must uphold the safety contract.
//
// Refs: https://www.felixcloutier.com/x86/cmpxchg8b:cmpxchg16b
Expand Down
5 changes: 2 additions & 3 deletions tools/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,8 @@ build() {
x_cargo "${args[@]}" "$@"
;;
i586*)
# i586 is -C target-feature=+x87 by default, but cfg(target_feature = "x87") doesn't work.
CARGO_TARGET_DIR="${target_dir}/x87" \
RUSTFLAGS="${target_rustflags} --cfg target_feature=\"x87\"" \
CARGO_TARGET_DIR="${target_dir}/no-x87" \
RUSTFLAGS="${target_rustflags} -C target-feature=-x87" \
x_cargo "${args[@]}" "$@"
;;
aarch64* | arm64*)
Expand Down

0 comments on commit c6f914b

Please sign in to comment.