Skip to content

@bitCast() and meta.Vector components don't work together as expected #9768

@Opioid

Description

@Opioid

Consider the following program:

const std = @import("std");
pub const Vec4f = std.meta.Vector(4, f32);

pub fn garbage(a: Vec4f) void {
    const b = Vec4f{
        @bitCast(f32, @bitCast(i32, a[0])),
        @bitCast(f32, @bitCast(i32, a[1])),
        @bitCast(f32, @bitCast(i32, a[2])),
        @bitCast(f32, @bitCast(i32, a[3])),
    };

    std.debug.print("{} {}\n", .{ a, b });
}

pub fn noGarbage(a: Vec4f) void {
    const b0 = @bitCast(f32, @bitCast(i32, a[0]));
    const b1 = @bitCast(f32, @bitCast(i32, a[1]));
    const b2 = @bitCast(f32, @bitCast(i32, a[2]));
    const b3 = @bitCast(f32, @bitCast(i32, a[3]));

    const b = Vec4f{ b0, b1, b2, b3 };

    std.debug.print("{} {}\n", .{ a, b });
}

pub fn main() void {
    const p = Vec4f{ 100.0, 100.0, 100.0, 100.0 };

    garbage(p);
    noGarbage(p);
}

The b vector does not print as expected when calling garbage(), it works as expected with noGarbage(). Example output:

{ 1.0e+02, 1.0e+02, 1.0e+02, 1.0e+02 } { 1.0e+02, 4.59121428e-41, 3.24416114e-03, 2.03188277e-43 }
{ 1.0e+02, 1.0e+02, 1.0e+02, 1.0e+02 } { 1.0e+02, 1.0e+02, 1.0e+02, 1.0e+02 }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions