Skip to content
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
**/zig-cache
**/.zig-cache
**/zig-out
**/artifacts
.DS_Store
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
MV=mv bin/Avalanche $(EXE)

default:
zig build -Drelease-fast --prefix ./ -Dtarget-name="Avalanche"
zig build --release=fast -Dtarget-name="Avalanche"

ifdef EXE
$(MV)
Expand Down
60 changes: 37 additions & 23 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const std = @import("std");
pub const DateTime = struct { day: u8, month: u8, year: u16, hour: u8, minute: u8, second: u8 };

pub fn timestamp2DateTime(timestamp: i64) DateTime {
const unixtime = @intCast(u64, timestamp);
const unixtime: u64 = @intCast(timestamp);
const SECONDS_PER_DAY = 86400;
const DAYS_IN_COMMON_YEAR = 365;
const DAYS_IN_4_YEARS = 1461;
Expand All @@ -15,29 +15,29 @@ pub fn timestamp2DateTime(timestamp: i64) DateTime {
const DAYS_ON_1970_01_01 = 719468;

var dayN: u64 = DAYS_ON_1970_01_01 + unixtime / SECONDS_PER_DAY;
var seconds_since_midnight: u64 = unixtime % SECONDS_PER_DAY;
const seconds_since_midnight: u64 = unixtime % SECONDS_PER_DAY;
var temp: u64 = 0;

temp = 4 * (dayN + DAYS_IN_100_YEARS + 1) / DAYS_IN_400_YEARS - 1;
var year = @intCast(u16, 100 * temp);
var year: u16 = @intCast(100 * temp);
dayN -= DAYS_IN_100_YEARS * temp + temp / 4;

temp = 4 * (dayN + DAYS_IN_COMMON_YEAR + 1) / DAYS_IN_4_YEARS - 1;
year += @intCast(u16, temp);
year += @intCast(temp);
dayN -= DAYS_IN_COMMON_YEAR * temp + temp / 4;

var month = @intCast(u8, (5 * dayN + 2) / 153);
var day = @intCast(u8, dayN - (@intCast(u64, month) * 153 + 2) / 5 + 1);
var month: u8 = @intCast((5 * dayN + 2) / 153);
const day: u8 = @intCast(dayN - (@as(u64, month) * 153 + 2) / 5 + 1);

month += 3;
if (month > 12) {
month -= 12;
year += 1;
}

var hour = @intCast(u8, seconds_since_midnight / 3600);
var minute = @intCast(u8, seconds_since_midnight % 3600 / 60);
var second = @intCast(u8, seconds_since_midnight % 60);
const hour: u8 = @intCast(seconds_since_midnight / 3600);
const minute: u8 = @intCast(seconds_since_midnight % 3600 / 60);
const second: u8 = @intCast(seconds_since_midnight % 60);

return DateTime{ .day = day, .month = month, .year = year, .hour = hour, .minute = minute, .second = second };
}
Expand All @@ -47,35 +47,47 @@ fn dtToString(dt: DateTime, buf: []u8) []const u8 {
return std.fmt.bufPrint(buf, "Compiled at {:0>4}-{:0>2}-{:0>2}-{:0>2}:{:0>2}", .{ dt.year, dt.month, dt.day, dt.hour, dt.minute }) catch unreachable;
}

pub fn build(b: *std.build.Builder) void {
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
const targetName = b.option([]const u8, "target-name", "Change the out name of the binary") orelse "Avalanche";

const net_module = b.createModule(
.{
.root_source_file = b.path("nets/bingshan.nnue"),
},
);
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();

const exe = b.addExecutable(targetName, "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
const optimize = b.standardOptimizeOption(.{});
const omit_frame_ptr = switch (optimize) {
.ReleaseFast, .ReleaseSmall => true,
.Debug, .ReleaseSafe => false,
};
const exe = b.addExecutable(.{
.name = targetName,
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.omit_frame_pointer = omit_frame_ptr,
});

exe.root_module.addImport("net", net_module);

const build_options = b.addOptions();
exe.addOptions("build_options", build_options);
exe.root_module.addOptions("build_options", build_options);
b.installArtifact(exe);

var buf: [64]u8 = undefined;
build_options.addOption([]const u8, "version", dtToString(timestamp2DateTime(std.time.timestamp()), &buf));
// build_options.addOption([]const u8, "version", "2.2.0");

exe.use_stage1 = true;

exe.linkLibC();
exe.install();

const run_cmd = exe.run();
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
Expand All @@ -84,9 +96,11 @@ pub fn build(b: *std.build.Builder) void {
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);

const exe_tests = b.addTest("src/tests.zig");
exe_tests.setTarget(target);
exe_tests.setBuildMode(mode);
const exe_tests = b.addTest(.{
.root_source_file = b.path("src/tests.zig"),
.target = target,
.optimize = optimize,
});

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step);
Expand Down
12 changes: 6 additions & 6 deletions src/chess/perft.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ pub fn perft(comptime color: types.Color, pos: *position.Position, depth: u32) u
}

var nodes: usize = 0;
comptime var opp = if (color == types.Color.White) types.Color.Black else types.Color.White;
const opp = comptime if (color == types.Color.White) types.Color.Black else types.Color.White;

var list = std.ArrayList(types.Move).initCapacity(std.heap.c_allocator, 48) catch unreachable;
defer list.deinit();

pos.generate_legal_moves(color, &list);
if (depth == 1) {
return @intCast(usize, list.items.len);
return @intCast(list.items.len);
}

for (list.items) |move| {
Expand All @@ -32,7 +32,7 @@ pub fn perft(comptime color: types.Color, pos: *position.Position, depth: u32) u
pub fn perft_div(comptime color: types.Color, pos: *position.Position, depth: u32) void {
var nodes: usize = 0;
var branch: usize = 0;
comptime var opp = if (color == types.Color.White) types.Color.Black else types.Color.White;
const opp = comptime if (color == types.Color.White) types.Color.Black else types.Color.White;

var list = std.ArrayList(types.Move).initCapacity(std.heap.c_allocator, 48) catch unreachable;
defer list.deinit();
Expand Down Expand Up @@ -66,11 +66,11 @@ pub fn perft_test(pos: *position.Position, depth: u32) void {
nodes = perft(types.Color.Black, pos, depth);
}

var elapsed = timer.read();
const elapsed = timer.read();
std.debug.print("\n", .{});
std.debug.print("Nodes: {}\n", .{nodes});
var mcs = @intToFloat(f64, elapsed) / 1000.0;
const mcs = @as(f64, @floatFromInt(elapsed)) / 1000.0;
std.debug.print("Elapsed: {d:.2} microseconds (or {d:.6} seconds)\n", .{ mcs, mcs / 1000.0 / 1000.0 });
var nps = @intToFloat(f64, nodes) / (@intToFloat(f64, elapsed) / 1000.0 / 1000.0 / 1000.0);
const nps = @as(f64, @floatFromInt(nodes)) / (@as(f64, @floatFromInt(elapsed)) / 1000.0 / 1000.0 / 1000.0);
std.debug.print("NPS: {d:.2} nodes/s (or {d:.4} mn/s)\n", .{ nps, nps / 1000.0 / 1000.0 });
}
Loading