Skip to content

Add operator for carryless multiply #16326

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

Open
Validark opened this issue Jul 5, 2023 · 0 comments
Open

Add operator for carryless multiply #16326

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

Comments

@Validark
Copy link
Contributor

Validark commented Jul 5, 2023

Since #9631 was accepted, we are expecting a @mulCarryless builtin to be added to the language to support carryless multiplication. However, after further consideration and thought, I think this is something that can be well-expressed with a new operator instead. I think *^ is a good candidate, because another name for carryless multiplication is XOR multiplication. So this proposal is to add an operator instead.

fn prefixXOR(bitmask: anytype) @TypeOf(bitmask) {
    const all_ones = std.math.maxInt(@TypeOf(bitmask)) + std.math.minInt(@TypeOf(bitmask));
    return bitmask *^ all_ones;
}

By introducing a specific operator for carryless multiplication, Zig can provide a clear and concise syntax for expressing this operation. I think this operation deserves a seat at the table with the other arithmetic and bitwise operators, and I would like to see more people come up with new use-cases for it (recently I wrote an article on how you can use the prefixXOR operation to isolate every other bit in a bitmask). I am excited to hear the verdict on this proposal.

@andrewrk andrewrk added the proposal This issue suggests modifications. If it also has the "accepted" label then it is planned. label Jul 22, 2023
@andrewrk andrewrk added this to the 0.12.0 milestone Jul 22, 2023
@andrewrk andrewrk modified the milestones: 0.14.0, 0.15.0 Feb 9, 2025
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