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

Make @shlExact() more ergonomic when type casting #4076

Open
momumi opened this issue Jan 5, 2020 · 0 comments
Open

Make @shlExact() more ergonomic when type casting #4076

momumi opened this issue Jan 5, 2020 · 0 comments
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Milestone

Comments

@momumi
Copy link
Contributor

momumi commented Jan 5, 2020

Currently using @shlExact(value: T, shift: Log2T) T gets very messy when using values that aren't comptime, for example:

fn makePixel(info: c.pixel_info, r: u8, b: u8, g: u8) u32 {
    return (
          @shlExact(@intCast(u32, r), @intCast(u5, info.red.offset))
        | @shlExact(@intCast(u32, g), @intCast(u5, info.green.offset))
        | @shlExact(@intCast(u32, b), @intCast(u5, info.blue.offset))
    );
}

I think it would be much cleaner to use @shlExact(T: type, value: T, shift: T) T thus:

fn makePixel(info: c.pixel_info, r: u8, b: u8, g: u8) u32 {
    return (
          @shlExact(u32, r, info.red.offset)
        | @shlExact(u32, g, info.green.offset)
        | @shlExact(u32, b, info.blue.offset)
    );
}

It would still provide all the same safety checks, it is just way neater.

Also, it might be nice to have a matching function @shl like:

@shl(u32, r, offset) == (@intCast(u32, r) << @intCast(u5, offset))

That's kinda redundant I guess, but it would give a standard and clean way to do this cast and the symmetry between @shl and @shlExact seems nice to me.

@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Jan 5, 2020
@andrewrk andrewrk added this to the 0.7.0 milestone Jan 5, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Oct 27, 2020
@andrewrk andrewrk modified the milestones: 0.8.0, 0.9.0 May 19, 2021
@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 Nov 23, 2021
@andrewrk andrewrk modified the milestones: 0.10.0, 0.11.0 Apr 16, 2022
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Apr 9, 2023
@andrewrk andrewrk modified the milestones: 0.13.0, 0.12.0 Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proposal This issue suggests modifications. If it also has the "accepted" label then it is planned.
Projects
None yet
Development

No branches or pull requests

2 participants