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

long double miscompiles #828

Open
VorpalBlade opened this issue Feb 13, 2023 · 0 comments
Open

long double miscompiles #828

VorpalBlade opened this issue Feb 13, 2023 · 0 comments

Comments

@VorpalBlade
Copy link

Given:

#define _GNU_SOURCE
#include <stdio.h>
#include <math.h>

#define _PI_180 (M_PIl / 180.0)
#define _180_PI (180.0 / M_PIl)

int main(void) {
    printf("%LF\n", _PI_180);
    printf("%LF\n", _180_PI);
    return 0;
}
$ ./c-test
0.017453
57.295780

But the converted rust version:

$ target/debug/test 
-NAN
-NAN

The generated code is:

#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_assignments, unused_mut)]
use ::c2rust_out::*;
extern "C" {
    fn printf(_: *const libc::c_char, _: ...) -> libc::c_int;
}
unsafe fn main_0() -> libc::c_int {
    printf(
        b"%LF\n\0" as *const u8 as *const libc::c_char,
        f128::f128::new(3.141592653589793238462643383279502884)
            / f128::f128::new(180.0f64),
    );
    printf(
        b"%LF\n\0" as *const u8 as *const libc::c_char,
        f128::f128::new(180.0f64)
            / f128::f128::new(3.141592653589793238462643383279502884),
    );
    return 0 as libc::c_int;
}
pub fn main() {
    unsafe { ::std::process::exit(main_0() as i32) }
}

I suspect (but have not checked that) f128 is not compatible with x87 80-bit long double and this conversion is just incorrect!

Software versions:

  • Arch Linux (rolling release distro).
  • glibc 2.37-2
  • clang 15.0.7-1
  • rustc 1.67.1 (from rustup)
  • c2rust 0.17.0 (from cargo install)
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

No branches or pull requests

1 participant