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

[zmath] Generic casting function: U32x2 to F32x2 and vice versa #310

Open
lassade opened this issue Jun 4, 2023 · 1 comment
Open

[zmath] Generic casting function: U32x2 to F32x2 and vice versa #310

lassade opened this issue Jun 4, 2023 · 1 comment

Comments

@lassade
Copy link

lassade commented Jun 4, 2023

Hi I did a generic vector/array casting function that I use for by 2d game

pub inline fn cast(comptime T: type, value: anytype) @Vector(veclen(@TypeOf(value)), T) {
    // This routine won't handle nan, inf and numbers greater than 8_388_608.0 (will generate undefined values).
    @setRuntimeSafety(false);

    const len = veclen(@TypeOf(value));
    const child = vectype(@TypeOf(value));

    var dst: [len]T = undefined;
    switch (@typeInfo(child)) {
        .Int => {
            comptime var i: u32 = 0;
            inline while (i < len) : (i += 1) {
                dst[i] = @intToFloat(T, value[i]);
            }
        },
        .Float => {
            comptime var i: u32 = 0;
            inline while (i < len) : (i += 1) {
                dst[i] = @floatToInt(T, value[i]);
            }
        },
        else => {
            @compileError("cast not supported");
        },
    }

    return dst;
}

I had to modify veclen to add support to arrays, but what you guys think? is PR worth?

@lassade lassade changed the title Generic casting function: U32x2 to F32x2 and vice versa [zmath] Generic casting function: U32x2 to F32x2 and vice versa Jun 4, 2023
@michal-z
Copy link
Collaborator

michal-z commented Jun 8, 2023

Hi, yes, please make a PR.

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

2 participants