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

Use SIMD for Zig #128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/leibniz.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@ pub fn main() anyerror!void {
var file = try std.fs.cwd().openFile("rounds.txt", .{});
defer file.close();
var buffer: [1024]u8 = undefined;
var n = try file.read(buffer[0..buffer.len]);
var rounds = try std.fmt.parseInt(i64, std.mem.trim(u8, buffer[0..n], "\n"), 10);
rounds += 2;
const n = try file.read(buffer[0..buffer.len]);
const rounds = try std.fmt.parseInt(u32, std.mem.trim(u8, buffer[0..n], "\n"), 10);

const V = @Vector(4, f64);

const sign = V{-1.0,1.0,-1.0,1.0};

var i: usize = 2;
var x: f64 = 1.0;
var pi: f64 = 1.0;
while (i < rounds) : (i += 1) {
x = -x;
pi += (x / @intToFloat(f64, 2 * i - 1));
var i: usize = 0;

while (i < rounds) : (i += 4) {
const div = V{
@floatFromInt(2 * i + 3),
@floatFromInt(2 * i + 5),
@floatFromInt(2 * i + 7),
@floatFromInt(2 * i + 9),
};
pi += @reduce(.Add, sign / div);
}
pi *= 4;

try std.io.getStdOut().writer().print("{}", .{pi});
}