Skip to content

Instantly share code, notes, and snippets.

@matu3ba
Last active Sep 3, 2021
Embed
What would you like to do?
helper program to cross check correctness of ffs with simpler algorithm
const std = @import("std");
const maxInt = std.math.maxInt;
const print = std.debug.print;
test "print ffs" {
//var in: u64 = 0;
//var in: u32 = 256;
//var in: u64 = 256;
var in: u128 = 256;
var x: @TypeOf(in) = 0;
var out: @TypeOf(in) = 0;
print("\n", .{});
//while (in <= 0xff) : (in += 1) {
while (in < maxInt(@TypeOf(in))) : (in = @mulWithSaturation(in, 2)) {
x = in; // must use temporary for correct iteration
out = 0;
// ffs - find first set
var index: u32 = 0;
if (x != 0) {
while (((~x) & 1) != 0) {
x = x >> 1;
index += 1;
}
}
//const one: @TypeOf(in) = 1;
//out = one << index;
out = index;
//print("{d:>40}", .{in});
print(" {x:>35}", .{in});
//print(" {b:>32}", .{in});
print(" {d:>5}\n", .{out}); //result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment