zig icon indicating copy to clipboard operation
zig copied to clipboard

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

Open chung-leong opened this issue 1 year ago • 2 comments

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 avatar Jul 02 '24 00:07 chung-leong

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 avatar Jul 06 '24 17:07 Rexicon226

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.

Rexicon226 avatar Jul 06 '24 17:07 Rexicon226