Skip to content

Missing cmath function: ldexp #821

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

Closed
RalfJung opened this issue Jul 3, 2019 · 9 comments · Fixed by #877 or #898
Closed

Missing cmath function: ldexp #821

RalfJung opened this issue Jul 3, 2019 · 9 comments · Fixed by #877 or #898
Labels
A-shims Area: This affects the external function shims C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement

Comments

@RalfJung
Copy link
Member

RalfJung commented Jul 3, 2019

The libcore test suites calls ldexp via FFI. Unfortunately, libstd does not expose this function in any way. What would be the best way for us to implement this shim?

I found this crate. Alternatively we could try to just define the extern symbol ourselves and see if that works on all our host platforms?^^

@RalfJung RalfJung added C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement A-shims Area: This affects the external function shims labels Jul 3, 2019
@pvdrz
Copy link
Contributor

pvdrz commented Aug 2, 2019

I am taking care of this one

@RalfJung
Copy link
Member Author

RalfJung commented Aug 2, 2019

Awesome!

Do you need mentoring? The file to edit is this one.

@pvdrz
Copy link
Contributor

pvdrz commented Aug 2, 2019

Thank you Ralf. It seems my gut feeling was right this time :D that's the file I modified. I just added a new match under // math functions based on the description for ldexp:

        "ldexp" => {
            let x = f32::from_bits(this.read_scalar(args[0])?.to_u32()?);
            let exp = f32::from_bits(this.read_scalar(args[1])?.to_u32()?);
            this.write_scalar(Scalar::from_u32((x * 2.0f32.powf(exp)).to_bits()), dest)?;
        }

Should that be enough? Or should I use the crate you mentioned?

I also suppose we should remove https://github.com/rust-lang/rust/blob/0f11354a9c1bf0c5ac250c7fa2bafc289a662f42/src/libcore/tests/num/flt2dec/mod.rs#L1, is that right?

@RalfJung
Copy link
Member Author

RalfJung commented Aug 2, 2019

I just added a new match under // math functions based on the description for ldexp

Hm. I guess it is a reasonable start. Maybe add a FIXME saying that if we see imprecise results, we should try to use the C math lib ldexp instead.

Your code should also, like the other math functions, have a FIXME because it uses host floats.

And finally, find an appropriate test case to extend and call the function there.

I also suppose we should remove

Once this lands in Miri and Miri has been updated in rustc, that can be removed, yes. One step after the other. :)

@pvdrz
Copy link
Contributor

pvdrz commented Aug 2, 2019

I just added a new match under // math functions based on the description for ldexp

Hm. I guess it is a reasonable start. Maybe add a FIXME saying that if we see imprecise results, we should try to use the C math lib ldexp instead.

I can use the C math library directly if you think that's a more definitive solution.

I also suppose we should remove

Once this lands in Miri and Miri has been updated in rustc, that can be removed, yes. One step after the other. :)

Ok, then I will do the miri PR first and wait :)

@RalfJung
Copy link
Member Author

RalfJung commented Aug 2, 2019

I can use the C math library directly if you think that's a more definitive solution.

That is more in line with what we do with the other functions. I just don't know if it will work on all platforms.

bors added a commit that referenced this issue Aug 3, 2019
@bors bors closed this as completed in #877 Aug 3, 2019
@RalfJung
Copy link
Member Author

RalfJung commented Aug 8, 2019

Seems like ldexp is part of the normal C math lib so we can rely on it being present in Miri. So @christianpoveda if you want to switch it to import the function via extern instead, be my guest. :)

@RalfJung
Copy link
Member Author

RalfJung commented Aug 8, 2019

I am seeing test failures when turning on the libcore tests that use ldexp, so maybe we actually need the higher precision.

@RalfJung RalfJung reopened this Aug 8, 2019
@pvdrz
Copy link
Contributor

pvdrz commented Aug 8, 2019

I'll fix this ASAP :)

bors added a commit that referenced this issue Aug 8, 2019
@bors bors closed this as completed in #898 Aug 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-shims Area: This affects the external function shims C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants