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

@cVaArg() sporadically fails with f128 on 32-bit Windows #20470

Open
chung-leong opened this issue Jul 2, 2024 · 2 comments
Open

@cVaArg() sporadically fails with f128 on 32-bit Windows #20470

chung-leong opened this issue Jul 2, 2024 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code. os-windows
Milestone

Comments

@chung-leong
Copy link

Zig Version

0.13.0

Steps to Reproduce and Observed Behavior

const std = @import("std");

pub fn printFloats(bits: u8, count: usize, ...) callconv(.C) void {
    var va_list = @cVaStart();
    defer @cVaEnd(&va_list);
    for (0..count) |_| {
        inline for (.{ f16, f32, f64, f80, f128 }) |T| {
            if (bits == @typeInfo(T).Float.bits) {
                const number = @cVaArg(&va_list, T);
                if (bits <= 64) {
                    std.debug.print("{d}\n", .{number});
                } else {
                    std.debug.print("{d}\n", .{@as(f64, @floatCast(number))});
                }
            }
        }
    }
}

var a: f128 = 1.5;
var b: f128 = 2.5;
var c: f128 = 3.5;

pub fn main() void {
    printFloats(128, 3, a, b, c);
}

Sometimes the above code works:

$ zig run test.zig -target x86-windows
1.5
2.5
3.5

Other times it doesn't:

$ zig run test.zig -target x86-windows
0
0
0

The same behavior is observed with 0.12.0.

Expected Behavior

$ zig run test.zig -target x86-windows
1.5
2.5
3.5
@chung-leong chung-leong added the bug Observed behavior contradicts documented or intended behavior label Jul 2, 2024
@Rexicon226
Copy link
Contributor

I can confirm this.
It seems that a @caVaArg miscomp is already known on x86_64:
https://github.com/ziglang/zig/blame/bf588f67d8c6261105f81fd468c420d662541d2a/lib/std/builtin.zig#L624

@Rexicon226
Copy link
Contributor

Interestingly, when I run this on an actual windows machine, I get it going between the right answer and 0 0 0. When I run it in wine, it is always 0 0 0.

@andrewrk andrewrk added this to the 0.15.0 milestone Aug 12, 2024
@andrewrk andrewrk added the miscompilation The compiler reports success but produces semantically incorrect code. label Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code. os-windows
Projects
None yet
Development

No branches or pull requests

3 participants