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

Clarify behavior of x87 FP registers in inline assembly #1126

Merged
merged 2 commits into from
Jan 3, 2022

Conversation

Amanieu
Copy link
Member

@Amanieu Amanieu commented Jan 1, 2022

This came up in a discussion on Zulip.

cc @joshtriplett

@joshtriplett
Copy link
Member

Can you clarify and spell out how the writer of an asm! block can clobber all x87 registers? Do they need to write out a clobber for each register individually? You wrote st(*) here; someone might expect that syntax to work. Does it, or could it?

@Amanieu
Copy link
Member Author

Amanieu commented Jan 2, 2022

All 8 registers need to be explicitly clobbered, there is no wildcard syntax. The reason for this is that in the future we may decide to support x87 registers as proper operands. Also there is no precedent for a wildcard register name that covers multiple real registers.

@joshtriplett
Copy link
Member

Also there is no precedent for a wildcard register name that covers multiple real registers.

In theory we could do that with something like clobber_abi("x87"), but perhaps that's not worth catering for.

@joshtriplett
Copy link
Member

@bors r+ rollup

@ehuss ehuss merged commit 58a301e into rust-lang:master Jan 3, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 4, 2022
Update books

## reference

3 commits in 06f9e61931bcf58b91dfe6c924057e42ce273ee1..f8ba2f12df60ee19b96de24ae5b73af3de8a446b
2021-12-17 07:31:40 -0800 to 2022-01-03 11:02:08 -0800
- Switch the default edition for examples to 2021 (rust-lang/reference#1125)
- Clarify behavior of x87 FP registers in inline assembly (rust-lang/reference#1126)
- Add inline assembly to the reference (rust-lang/reference#1105)

## book

36 commits in 8a0bb3c96e71927b80fa2286d7a5a5f2547c6aa4..d3740fb7aad0ea4a80ae20f64dee3a8cfc0c5c3c
2021-12-22 20:54:27 -0500 to 2022-01-03 21:46:04 -0500
- Add a concrete example of an optional value. Fixes rust-lang/book#2848.
- match isn't really an operator. Fixes rust-lang/book#2859
- Edits to edits of chapter 6
- Make fixes recommended by shellcheck
- Use shellcheck
- SIGH fix all the typos that were missed while spellcheck was broken
- SIGH add all the words to the dictionary that were missed while spellcheck was broken
- Remove test_harness from the dictionary
- sigh, the xkcd sandwich one
- Install aspell in CI
- set -eu in all bash scripts
- typo: assignement -> assignment
- Fix quotes
- Snapshot of ch12 for nostarch
- Use 'static lifetime earlier because that's more correct. Fixes rust-lang/book#2864.
- Add does_not_compile annotation to intermediate steps that don't compile
- Sidestep who provides output streams. Fixes rust-lang/book#2933.
- Remove note about primitive obsession. Fixes rust-lang/book#2863.
- Remove sentence encouraging writing tests on your own. Fixes rust-lang/book#2223.
- Bump mdBook version to 0.4.14 in workflow main.yml
- Past tense make better sense
- Past tense makes better sense
- Update the edition in all the listings' Cargo.toml
- Update the book to either say 2021 edition or not talk about editions
- Remove most of the 2018 edition text from the title page. Fixes rust-lang/book#2852.
- Fix word wrapping
- Emphasize return type is mandatory
- fix title capitalization
- Further edits to mention of --include-ignored, propagate to src
- feat: mention `cargo test -- --include-ignored`
- wording: get rid of "to from"
- interchanged position of `binary` and `library`
- Fix wrong word typo
- Further edits in rust-analyzer text
- appendix-04 IDE integration: Replaced rls by rust-analyzer
- Update link to Italian translation. Connects to rust-lang/book#2484.

## rustc-dev-guide

3 commits in 9bf0028..8754644
2021-12-20 21:53:57 +0900 to 2021-12-28 22:17:49 -0600
- Update link to moved section (rust-lang/rustc-dev-guide#1282)
- Fix link in contributing.md (rust-lang/rustc-dev-guide#1280)
- Streamline "Getting Started" (rust-lang/rustc-dev-guide#1279)
@loveJesus
Copy link

loveJesus commented Apr 16, 2022

Happy Easter and hello thanks for your work on rust. I am trying to implement the following in the new asm! syntax but i'm not really sure particularly about how to go about having the fstp instruction store into a rust variable. Thank you!

#[inline(always)]
pub fn cos_chirho(a: f32) -> f32 { 
let mut res: f32 = unsafe { mem::uninitialized() };

unsafe { asm!(
    r##"
        flds $1;
        fcos;
        fstps $0;
    "##
    : "=*m"(&mut res as *mut f32)
    : "*m"(&a as *const f32)
) };

res
}

@Amanieu
Copy link
Member Author

Amanieu commented Apr 16, 2022

Just pass the memory address in a reg and then use it as normal:

asm!(
	"flds [{}]",
	"fcos",
	"fstps [{}]",
	in(reg) &a
	in(reg) &mut res,
);

@loveJesus
Copy link

God is good! thank you.
Changed the flds to fld dword ptr and likewise for fstp. It works!

I did not know however to use an in(reg) instead of out(reg) for the &mut parameter. It was giving me (exit code: 0xc0000005, STATUS_ACCESS_VIOLATION) when it would compile with out(reg) *&mut. It makes sense that an in can work since we modify the contents pointed to by the register and not the register itself. Sorry to have bothered you with this. Do you think me attempting to make a pull request with this example would be helpful?

@Amanieu
Copy link
Member Author

Amanieu commented Apr 19, 2022

Rust-by-example might be a better place for some example code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants