From f15ec9a59b777a2b7998518858f11f5da0dfcb2d Mon Sep 17 00:00:00 2001 From: emekoi Date: Thu, 1 Aug 2019 17:51:53 -0500 Subject: [PATCH 01/10] implemented runtime abi detetction for windows --- src/os.cpp | 32 ++++++++++++++++---------------- src/os.hpp | 3 +++ src/target.cpp | 15 +++++++++++++-- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/os.cpp b/src/os.cpp index 5fa70bd260..5844070609 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -1125,29 +1125,27 @@ Error os_get_cwd(Buf *out_cwd) { #endif } -#if defined(ZIG_OS_WINDOWS) #define is_wprefix(s, prefix) \ (wcsncmp((s), (prefix), sizeof(prefix) / sizeof(WCHAR) - 1) == 0) -static bool is_stderr_cyg_pty(void) { - HANDLE stderr_handle = GetStdHandle(STD_ERROR_HANDLE); - if (stderr_handle == INVALID_HANDLE_VALUE) +bool ATTRIBUTE_MUST_USE os_is_cygwin_pty(int fd) { +#if defined(ZIG_OS_WINDOWS) + HANDLE handle = (HANDLE)_get_osfhandle(fd); + + // Cygwin/msys's pty is a pipe. + if (handle == INVALID_HANDLE_VALUE || GetFileType(handle) != FILE_TYPE_PIPE) { return false; + } int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * MAX_PATH; - FILE_NAME_INFO *nameinfo; WCHAR *p = NULL; - - // Cygwin/msys's pty is a pipe. - if (GetFileType(stderr_handle) != FILE_TYPE_PIPE) { - return 0; - } - nameinfo = (FILE_NAME_INFO *)allocate(size); + + FILE_NAME_INFO *nameinfo = (FILE_NAME_INFO *)allocate(size); if (nameinfo == NULL) { - return 0; + return false; } // Check the name of the pipe: // '\{cygwin,msys}-XXXXXXXXXXXXXXXX-ptyN-{from,to}-master' - if (GetFileInformationByHandleEx(stderr_handle, FileNameInfo, nameinfo, size)) { + if (GetFileInformationByHandleEx(handle, FileNameInfo, nameinfo, size)) { nameinfo->FileName[nameinfo->FileNameLength / sizeof(WCHAR)] = L'\0'; p = nameinfo->FileName; if (is_wprefix(p, L"\\cygwin-")) { /* Cygwin */ @@ -1180,12 +1178,14 @@ static bool is_stderr_cyg_pty(void) { } free(nameinfo); return (p != NULL); -} +#else + return false #endif +} bool os_stderr_tty(void) { #if defined(ZIG_OS_WINDOWS) - return _isatty(_fileno(stderr)) != 0 || is_stderr_cyg_pty(); + return _isatty(_fileno(stderr)) != 0 || os_is_cygwin_pty(_fileno(stderr)); #elif defined(ZIG_OS_POSIX) return isatty(STDERR_FILENO) != 0; #else @@ -1486,7 +1486,7 @@ WORD original_console_attributes = FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BL void os_stderr_set_color(TermColor color) { #if defined(ZIG_OS_WINDOWS) - if (is_stderr_cyg_pty()) { + if (os_stderr_tty()) { set_color_posix(color); return; } diff --git a/src/os.hpp b/src/os.hpp index c8135e9844..fc0e1929f2 100644 --- a/src/os.hpp +++ b/src/os.hpp @@ -11,6 +11,7 @@ #include "list.hpp" #include "buffer.hpp" #include "error.hpp" +#include "target.hpp" #include "zig_llvm.h" #include "windows_sdk.h" @@ -152,6 +153,8 @@ Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_include_path(ZigWindowsSDK *sdk, Buf Error ATTRIBUTE_MUST_USE os_get_win32_ucrt_lib_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type); Error ATTRIBUTE_MUST_USE os_get_win32_kern32_path(ZigWindowsSDK *sdk, Buf *output_buf, ZigLLVM_ArchType platform_type); +bool ATTRIBUTE_MUST_USE os_is_cygwin_pty(int fd); + Error ATTRIBUTE_MUST_USE os_self_exe_shared_libs(ZigList &paths); #endif diff --git a/src/target.cpp b/src/target.cpp index 7bb248a35f..d34254f1d1 100644 --- a/src/target.cpp +++ b/src/target.cpp @@ -506,7 +506,7 @@ void get_native_target(ZigTarget *target) { &oformat); target->os = get_zig_os_type(os_type); target->is_native = true; - if (target->abi == ZigLLVM_UnknownEnvironment) { + if (target->os == OsWindows || target->abi == ZigLLVM_UnknownEnvironment) { target->abi = target_default_abi(target->arch, target->os); } if (target_is_glibc(target)) { @@ -1504,6 +1504,16 @@ bool target_is_single_threaded(const ZigTarget *target) { return target_is_wasm(target); } +static ZigLLVM_EnvironmentType target_get_win32_abi() { + FILE* files[] = { stdin, stdout, stderr, nullptr }; + for (int i = 0; files[i] != nullptr; i++) { + if (os_is_cygwin_pty(_fileno(files[i]))) { + return ZigLLVM_GNU; + } + } + return ZigLLVM_MSVC; +} + ZigLLVM_EnvironmentType target_default_abi(ZigLLVM_ArchType arch, Os os) { if (arch == ZigLLVM_wasm32 || arch == ZigLLVM_wasm64) { return ZigLLVM_Musl; @@ -1544,8 +1554,9 @@ ZigLLVM_EnvironmentType target_default_abi(ZigLLVM_ArchType arch, Os os) { case OsHurd: return ZigLLVM_GNU; case OsUefi: - case OsWindows: return ZigLLVM_MSVC; + case OsWindows: + return target_get_win32_abi(); case OsLinux: case OsWASI: return ZigLLVM_Musl; From 102d3f30c406e2818c6320a48f2405fa7e1e4237 Mon Sep 17 00:00:00 2001 From: emekoi Date: Thu, 1 Aug 2019 18:27:39 -0500 Subject: [PATCH 02/10] accept unix style paths on windows-gnu --- src/os.cpp | 8 ++++---- src/os.hpp | 5 +++++ src/target.cpp | 2 +- std/os/windows.zig | 7 +++---- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/os.cpp b/src/os.cpp index 5844070609..6c1a2581df 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -1130,7 +1130,7 @@ Error os_get_cwd(Buf *out_cwd) { bool ATTRIBUTE_MUST_USE os_is_cygwin_pty(int fd) { #if defined(ZIG_OS_WINDOWS) HANDLE handle = (HANDLE)_get_osfhandle(fd); - + // Cygwin/msys's pty is a pipe. if (handle == INVALID_HANDLE_VALUE || GetFileType(handle) != FILE_TYPE_PIPE) { return false; @@ -1138,7 +1138,7 @@ bool ATTRIBUTE_MUST_USE os_is_cygwin_pty(int fd) { int size = sizeof(FILE_NAME_INFO) + sizeof(WCHAR) * MAX_PATH; WCHAR *p = NULL; - + FILE_NAME_INFO *nameinfo = (FILE_NAME_INFO *)allocate(size); if (nameinfo == NULL) { return false; @@ -1179,13 +1179,13 @@ bool ATTRIBUTE_MUST_USE os_is_cygwin_pty(int fd) { free(nameinfo); return (p != NULL); #else - return false + return false; #endif } bool os_stderr_tty(void) { #if defined(ZIG_OS_WINDOWS) - return _isatty(_fileno(stderr)) != 0 || os_is_cygwin_pty(_fileno(stderr)); + return _isatty(fileno(stderr)) != 0 || os_is_cygwin_pty(fileno(stderr)); #elif defined(ZIG_OS_POSIX) return isatty(STDERR_FILENO) != 0; #else diff --git a/src/os.hpp b/src/os.hpp index fc0e1929f2..7354528c34 100644 --- a/src/os.hpp +++ b/src/os.hpp @@ -89,6 +89,11 @@ struct Termination { #define OsFile int #endif +#if defined(ZIG_OS_WINDOWS) +#undef fileno +#define fileno _fileno +#endif + struct OsTimeStamp { uint64_t sec; uint64_t nsec; diff --git a/src/target.cpp b/src/target.cpp index d34254f1d1..695a810e39 100644 --- a/src/target.cpp +++ b/src/target.cpp @@ -1507,7 +1507,7 @@ bool target_is_single_threaded(const ZigTarget *target) { static ZigLLVM_EnvironmentType target_get_win32_abi() { FILE* files[] = { stdin, stdout, stderr, nullptr }; for (int i = 0; files[i] != nullptr; i++) { - if (os_is_cygwin_pty(_fileno(files[i]))) { + if (os_is_cygwin_pty(fileno(files[i]))) { return ZigLLVM_GNU; } } diff --git a/std/os/windows.zig b/std/os/windows.zig index ac76e8f58f..954e56443f 100644 --- a/std/os/windows.zig +++ b/std/os/windows.zig @@ -65,7 +65,7 @@ pub const CreateFileError = error{ InvalidUtf8, /// On Windows, file paths cannot contain these characters: - /// '/', '*', '?', '"', '<', '>', '|' + /// '*', '?', '"', '<', '>', '|', and '/' (when the ABI is not GNU) BadPathName, Unexpected, @@ -831,8 +831,8 @@ pub fn sliceToPrefixedSuffixedFileW(s: []const u8, comptime suffix: []const u16) // disallow forward slashes in zig std lib file functions on Windows. for (s) |byte| { switch (byte) { - '/', '*', '?', '"', '<', '>', '|' => return error.BadPathName, - else => {}, + '*', '?', '"', '<', '>', '|' => return error.BadPathName, + else => if (builtin.abi == .msvc and byte == '/') return error.BadPathName, } } const start_index = if (mem.startsWith(u8, s, "\\\\") or !std.fs.path.isAbsolute(s)) 0 else blk: { @@ -866,7 +866,6 @@ pub fn unexpectedError(err: DWORD) std.os.UnexpectedError { return error.Unexpected; } - /// Call this when you made a windows NtDll call /// and you get an unexpected status. pub fn unexpectedStatus(status: NTSTATUS) std.os.UnexpectedError { From 59e2c87b4b89bf16321b499124d568ac5a19c300 Mon Sep 17 00:00:00 2001 From: emekoi Date: Sat, 3 Aug 2019 18:40:27 -0500 Subject: [PATCH 03/10] move windows abi detection to `get_native_target` --- src/target.cpp | 28 +++++++++++++++------------- std/os/windows.zig | 5 ++--- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/target.cpp b/src/target.cpp index 695a810e39..4a45707f78 100644 --- a/src/target.cpp +++ b/src/target.cpp @@ -491,6 +491,16 @@ Error target_parse_glibc_version(ZigGLibCVersion *glibc_ver, const char *text) { return ErrorNone; } +static ZigLLVM_EnvironmentType target_get_win32_abi() { + FILE* files[] = { stdin, stdout, stderr, nullptr }; + for (int i = 0; files[i] != nullptr; i++) { + if (os_is_cygwin_pty(fileno(files[i]))) { + return ZigLLVM_GNU; + } + } + return ZigLLVM_MSVC; +} + void get_native_target(ZigTarget *target) { // first zero initialize *target = {}; @@ -505,8 +515,11 @@ void get_native_target(ZigTarget *target) { &target->abi, &oformat); target->os = get_zig_os_type(os_type); + if (target->os == OsWindows) { + target->abi = target_get_win32_abi(); + } target->is_native = true; - if (target->os == OsWindows || target->abi == ZigLLVM_UnknownEnvironment) { + if (target->abi == ZigLLVM_UnknownEnvironment) { target->abi = target_default_abi(target->arch, target->os); } if (target_is_glibc(target)) { @@ -1504,16 +1517,6 @@ bool target_is_single_threaded(const ZigTarget *target) { return target_is_wasm(target); } -static ZigLLVM_EnvironmentType target_get_win32_abi() { - FILE* files[] = { stdin, stdout, stderr, nullptr }; - for (int i = 0; files[i] != nullptr; i++) { - if (os_is_cygwin_pty(fileno(files[i]))) { - return ZigLLVM_GNU; - } - } - return ZigLLVM_MSVC; -} - ZigLLVM_EnvironmentType target_default_abi(ZigLLVM_ArchType arch, Os os) { if (arch == ZigLLVM_wasm32 || arch == ZigLLVM_wasm64) { return ZigLLVM_Musl; @@ -1554,9 +1557,8 @@ ZigLLVM_EnvironmentType target_default_abi(ZigLLVM_ArchType arch, Os os) { case OsHurd: return ZigLLVM_GNU; case OsUefi: - return ZigLLVM_MSVC; case OsWindows: - return target_get_win32_abi(); + return ZigLLVM_MSVC; case OsLinux: case OsWASI: return ZigLLVM_Musl; diff --git a/std/os/windows.zig b/std/os/windows.zig index 954e56443f..eccc968561 100644 --- a/std/os/windows.zig +++ b/std/os/windows.zig @@ -827,12 +827,11 @@ pub fn sliceToPrefixedSuffixedFileW(s: []const u8, comptime suffix: []const u16) // > converting the name to an NT-style name, except when using the "\\?\" // > prefix as detailed in the following sections. // from https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file#maximum-path-length-limitation - // Because we want the larger maximum path length for absolute paths, we - // disallow forward slashes in zig std lib file functions on Windows. for (s) |byte| { switch (byte) { '*', '?', '"', '<', '>', '|' => return error.BadPathName, - else => if (builtin.abi == .msvc and byte == '/') return error.BadPathName, + '/' => if (builtin.abi == .msvc) return error.BadPathName, + else => {}, } } const start_index = if (mem.startsWith(u8, s, "\\\\") or !std.fs.path.isAbsolute(s)) 0 else blk: { From c050ec4e570caf53be924bcbbe4194512b6a022c Mon Sep 17 00:00:00 2001 From: Marc Tiehuis Date: Wed, 21 Aug 2019 20:34:12 +1200 Subject: [PATCH 04/10] Update hash/crypto benchmark scripts --- .../{throughput_test.zig => benchmark.zig} | 19 +++-- std/crypto/blake2.zig | 4 +- std/crypto/sha2.zig | 4 +- .../{throughput_test.zig => benchmark.zig} | 70 +++++++++++++++++-- 4 files changed, 81 insertions(+), 16 deletions(-) rename std/crypto/{throughput_test.zig => benchmark.zig} (91%) rename std/hash/{throughput_test.zig => benchmark.zig} (61%) diff --git a/std/crypto/throughput_test.zig b/std/crypto/benchmark.zig similarity index 91% rename from std/crypto/throughput_test.zig rename to std/crypto/benchmark.zig index aee06571a0..ae5087a847 100644 --- a/std/crypto/throughput_test.zig +++ b/std/crypto/benchmark.zig @@ -1,8 +1,10 @@ +// zig run benchmark.zig --release-fast --override-std-dir .. + const builtin = @import("builtin"); -const std = @import("std"); +const std = @import("../std.zig"); const time = std.time; const Timer = time.Timer; -const crypto = @import("../crypto.zig"); +const crypto = std.crypto; const KiB = 1024; const MiB = 1024 * KiB; @@ -14,7 +16,7 @@ const Crypto = struct { name: []const u8, }; -const hashes = []Crypto{ +const hashes = [_]Crypto{ Crypto{ .ty = crypto.Md5, .name = "md5" }, Crypto{ .ty = crypto.Sha1, .name = "sha1" }, Crypto{ .ty = crypto.Sha256, .name = "sha256" }, @@ -45,7 +47,7 @@ pub fn benchmarkHash(comptime Hash: var, comptime bytes: comptime_int) !u64 { return throughput; } -const macs = []Crypto{ +const macs = [_]Crypto{ Crypto{ .ty = crypto.Poly1305, .name = "poly1305" }, Crypto{ .ty = crypto.HmacMd5, .name = "hmac-md5" }, Crypto{ .ty = crypto.HmacSha1, .name = "hmac-sha1" }, @@ -75,7 +77,7 @@ pub fn benchmarkMac(comptime Mac: var, comptime bytes: comptime_int) !u64 { return throughput; } -const exchanges = []Crypto{Crypto{ .ty = crypto.X25519, .name = "x25519" }}; +const exchanges = [_]Crypto{Crypto{ .ty = crypto.X25519, .name = "x25519" }}; pub fn benchmarkKeyExchange(comptime DhKeyExchange: var, comptime exchange_count: comptime_int) !u64 { std.debug.assert(DhKeyExchange.minimum_key_length >= DhKeyExchange.secret_length); @@ -135,13 +137,16 @@ pub fn main() !void { var buffer: [1024]u8 = undefined; var fixed = std.heap.FixedBufferAllocator.init(buffer[0..]); - const args = try std.os.argsAlloc(&fixed.allocator); + const args = try std.process.argsAlloc(&fixed.allocator); var filter: ?[]u8 = ""; var i: usize = 1; while (i < args.len) : (i += 1) { - if (std.mem.eql(u8, args[i], "--seed")) { + if (std.mem.eql(u8, args[i], "--mode")) { + try stdout.print("{}\n", builtin.mode); + return; + } else if (std.mem.eql(u8, args[i], "--seed")) { i += 1; if (i == args.len) { usage(); diff --git a/std/crypto/blake2.zig b/std/crypto/blake2.zig index 2a2236dfd7..6bb2764b92 100644 --- a/std/crypto/blake2.zig +++ b/std/crypto/blake2.zig @@ -269,8 +269,8 @@ pub const Blake2b512 = Blake2b(512); fn Blake2b(comptime out_len: usize) type { return struct { const Self = @This(); - const block_length = 128; - const digest_length = out_len / 8; + pub const block_length = 128; + pub const digest_length = out_len / 8; const iv = [8]u64{ 0x6a09e667f3bcc908, diff --git a/std/crypto/sha2.zig b/std/crypto/sha2.zig index c1f7bd228d..b40a39d579 100644 --- a/std/crypto/sha2.zig +++ b/std/crypto/sha2.zig @@ -420,8 +420,8 @@ pub const Sha512 = Sha2_64(Sha512Params); fn Sha2_64(comptime params: Sha2Params64) type { return struct { const Self = @This(); - const block_length = 128; - const digest_length = params.out_len / 8; + pub const block_length = 128; + pub const digest_length = params.out_len / 8; s: [8]u64, // Streaming Cache diff --git a/std/hash/throughput_test.zig b/std/hash/benchmark.zig similarity index 61% rename from std/hash/throughput_test.zig rename to std/hash/benchmark.zig index 4b7e8ef344..b42a34ef31 100644 --- a/std/hash/throughput_test.zig +++ b/std/hash/benchmark.zig @@ -1,3 +1,5 @@ +// zig run benchmark.zig --release-fast --override-std-dir .. + const builtin = @import("builtin"); const std = @import("std"); const time = std.time; @@ -32,6 +34,8 @@ const Result = struct { throughput: u64, }; +const block_size: usize = 8192; + pub fn benchmarkHash(comptime H: var, bytes: usize) !Result { var h = blk: { if (H.init_u8s) |init| { @@ -43,7 +47,7 @@ pub fn benchmarkHash(comptime H: var, bytes: usize) !Result { break :blk H.ty.init(); }; - var block: [8192]u8 = undefined; + var block: [block_size]u8 = undefined; prng.random.bytes(block[0..]); var offset: usize = 0; @@ -63,6 +67,43 @@ pub fn benchmarkHash(comptime H: var, bytes: usize) !Result { }; } +pub fn benchmarkHashSmallKeys(comptime H: var, key_size: usize, bytes: usize) !Result { + const key_count = bytes / key_size; + var block: [block_size]u8 = undefined; + prng.random.bytes(block[0..]); + + var i: usize = 0; + var timer = try Timer.start(); + const start = timer.lap(); + + var sum: u64 = 0; + while (i < key_count) : (i += 1) { + const o = i % (block_size - key_size); + const small_key = block[o .. key_size + o]; + + const result = blk: { + if (H.init_u8s) |init| { + break :blk H.ty.hash(init, small_key); + } + if (H.init_u64) |init| { + break :blk H.ty.hash(init, small_key); + } + break :blk H.ty.hash(small_key); + }; + + sum +%= result; + } + const end = timer.read(); + + const elapsed_s = @intToFloat(f64, end - start) / time.ns_per_s; + const throughput = @floatToInt(u64, @intToFloat(f64, bytes) / elapsed_s); + + return Result{ + .hash = sum, + .throughput = throughput, + }; +} + fn usage() void { std.debug.warn( \\throughput_test [options] @@ -100,10 +141,14 @@ pub fn main() !void { var filter: ?[]u8 = ""; var count: usize = mode(128 * MiB); + var key_size: usize = 32; var i: usize = 1; while (i < args.len) : (i += 1) { - if (std.mem.eql(u8, args[i], "--seed")) { + if (std.mem.eql(u8, args[i], "--mode")) { + try stdout.print("{}\n", builtin.mode); + return; + } else if (std.mem.eql(u8, args[i], "--seed")) { i += 1; if (i == args.len) { usage(); @@ -127,8 +172,20 @@ pub fn main() !void { std.os.exit(1); } - const c = try std.fmt.parseUnsigned(u32, args[i], 10); + const c = try std.fmt.parseUnsigned(usize, args[i], 10); count = c * MiB; + } else if (std.mem.eql(u8, args[i], "--key-size")) { + i += 1; + if (i == args.len) { + usage(); + std.os.exit(1); + } + + key_size = try std.fmt.parseUnsigned(usize, args[i], 10); + if (key_size > block_size) { + try stdout.print("key_size cannot exceed block size of {}\n", block_size); + std.os.exit(1); + } } else if (std.mem.eql(u8, args[i], "--help")) { usage(); return; @@ -141,8 +198,11 @@ pub fn main() !void { inline for (hashes) |H| { if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) { const result = try benchmarkHash(H, count); - try printPad(stdout, H.name); - try stdout.print(": {:4} MiB/s [{:16}]\n", result.throughput / (1 * MiB), result.hash); + const result_small = try benchmarkHashSmallKeys(H, key_size, count); + + try stdout.print("{}\n", H.name); + try stdout.print(" iterative: {:4} MiB/s [{x:0<16}]\n", result.throughput / (1 * MiB), result.hash); + try stdout.print(" small keys: {:4} MiB/s [{x:0<16}]\n", result_small.throughput / (1 * MiB), result_small.hash); } } } From 48410943cbcbf53ae3c9895b2452218d6eacbc4b Mon Sep 17 00:00:00 2001 From: Marc Tiehuis Date: Wed, 21 Aug 2019 20:46:15 +1200 Subject: [PATCH 05/10] Add more hash functions to benchmark scripts Changed CRC api so the polynomial is specified as an enum for simpler construction. --- std/hash/benchmark.zig | 90 ++++++++++++++++++++++++++++++++++-------- std/hash/crc.zig | 26 ++++++------ 2 files changed, 86 insertions(+), 30 deletions(-) diff --git a/std/hash/benchmark.zig b/std/hash/benchmark.zig index b42a34ef31..ca948d21bf 100644 --- a/std/hash/benchmark.zig +++ b/std/hash/benchmark.zig @@ -15,6 +15,7 @@ var prng = std.rand.DefaultPrng.init(0); const Hash = struct { ty: type, name: []const u8, + has_iterative_api: bool = true, init_u8s: ?[]const u8 = null, init_u64: ?u64 = null, }; @@ -22,11 +23,62 @@ const Hash = struct { const siphash_key = "0123456789abcdef"; const hashes = [_]Hash{ - Hash{ .ty = hash.Wyhash, .name = "wyhash", .init_u64 = 0 }, - Hash{ .ty = hash.SipHash64(1, 3), .name = "siphash(1,3)", .init_u8s = siphash_key }, - Hash{ .ty = hash.SipHash64(2, 4), .name = "siphash(2,4)", .init_u8s = siphash_key }, - Hash{ .ty = hash.Fnv1a_64, .name = "fnv1a" }, - Hash{ .ty = hash.Crc32, .name = "crc32" }, + Hash{ + .ty = hash.Wyhash, + .name = "wyhash", + .init_u64 = 0, + }, + Hash{ + .ty = hash.SipHash64(1, 3), + .name = "siphash(1,3)", + .init_u8s = siphash_key, + }, + Hash{ + .ty = hash.SipHash64(2, 4), + .name = "siphash(2,4)", + .init_u8s = siphash_key, + }, + Hash{ + .ty = hash.Fnv1a_64, + .name = "fnv1a", + }, + Hash{ + .ty = hash.Adler32, + .name = "adler32", + }, + Hash{ + .ty = hash.crc.Crc32WithPoly(.IEEE), + .name = "crc32-slicing-by-8", + }, + Hash{ + .ty = hash.crc.Crc32SmallWithPoly(.IEEE), + .name = "crc32-half-byte-lookup", + }, + Hash{ + .ty = hash.CityHash32, + .name = "cityhash-32", + .has_iterative_api = false, + }, + Hash{ + .ty = hash.CityHash64, + .name = "cityhash-64", + .has_iterative_api = false, + }, + Hash{ + .ty = hash.Murmur2_32, + .name = "murmur2-32", + .has_iterative_api = false, + }, + Hash{ + .ty = hash.Murmur2_64, + .name = "murmur2-64", + .has_iterative_api = false, + }, + Hash{ + .ty = hash.Murmur3_32, + .name = "murmur3-32", + .has_iterative_api = false, + }, }; const Result = struct { @@ -78,10 +130,8 @@ pub fn benchmarkHashSmallKeys(comptime H: var, key_size: usize, bytes: usize) !R var sum: u64 = 0; while (i < key_count) : (i += 1) { - const o = i % (block_size - key_size); - const small_key = block[o .. key_size + o]; - - const result = blk: { + const small_key = block[0..key_size]; + sum +%= blk: { if (H.init_u8s) |init| { break :blk H.ty.hash(init, small_key); } @@ -90,8 +140,6 @@ pub fn benchmarkHashSmallKeys(comptime H: var, key_size: usize, bytes: usize) !R } break :blk H.ty.hash(small_key); }; - - sum +%= result; } const end = timer.read(); @@ -142,6 +190,7 @@ pub fn main() !void { var filter: ?[]u8 = ""; var count: usize = mode(128 * MiB); var key_size: usize = 32; + var seed: u32 = 0; var i: usize = 1; while (i < args.len) : (i += 1) { @@ -155,8 +204,8 @@ pub fn main() !void { std.os.exit(1); } - const seed = try std.fmt.parseUnsigned(u32, args[i], 10); - prng.seed(seed); + seed = try std.fmt.parseUnsigned(u32, args[i], 10); + // we seed later } else if (std.mem.eql(u8, args[i], "--filter")) { i += 1; if (i == args.len) { @@ -197,11 +246,18 @@ pub fn main() !void { inline for (hashes) |H| { if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) { - const result = try benchmarkHash(H, count); - const result_small = try benchmarkHashSmallKeys(H, key_size, count); - try stdout.print("{}\n", H.name); - try stdout.print(" iterative: {:4} MiB/s [{x:0<16}]\n", result.throughput / (1 * MiB), result.hash); + + // Always reseed prior to every call so we are hashing the same buffer contents. + // This allows easier comparison between different implementations. + if (H.has_iterative_api) { + prng.seed(seed); + const result = try benchmarkHash(H, count); + try stdout.print(" iterative: {:4} MiB/s [{x:0<16}]\n", result.throughput / (1 * MiB), result.hash); + } + + prng.seed(seed); + const result_small = try benchmarkHashSmallKeys(H, key_size, count); try stdout.print(" small keys: {:4} MiB/s [{x:0<16}]\n", result_small.throughput / (1 * MiB), result_small.hash); } } diff --git a/std/hash/crc.zig b/std/hash/crc.zig index 53b4262c93..73e5bb0371 100644 --- a/std/hash/crc.zig +++ b/std/hash/crc.zig @@ -9,17 +9,17 @@ const std = @import("../std.zig"); const debug = std.debug; const testing = std.testing; -pub const Polynomial = struct { - const IEEE = 0xedb88320; - const Castagnoli = 0x82f63b78; - const Koopman = 0xeb31d82e; +pub const Polynomial = enum(u32) { + IEEE = 0xedb88320, + Castagnoli = 0x82f63b78, + Koopman = 0xeb31d82e, }; // IEEE is by far the most common CRC and so is aliased by default. -pub const Crc32 = Crc32WithPoly(Polynomial.IEEE); +pub const Crc32 = Crc32WithPoly(.IEEE); // slicing-by-8 crc32 implementation. -pub fn Crc32WithPoly(comptime poly: u32) type { +pub fn Crc32WithPoly(comptime poly: Polynomial) type { return struct { const Self = @This(); const lookup_tables = comptime block: { @@ -31,7 +31,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type { var j: usize = 0; while (j < 8) : (j += 1) { if (crc & 1 == 1) { - crc = (crc >> 1) ^ poly; + crc = (crc >> 1) ^ @enumToInt(poly); } else { crc = (crc >> 1); } @@ -100,7 +100,7 @@ pub fn Crc32WithPoly(comptime poly: u32) type { } test "crc32 ieee" { - const Crc32Ieee = Crc32WithPoly(Polynomial.IEEE); + const Crc32Ieee = Crc32WithPoly(.IEEE); testing.expect(Crc32Ieee.hash("") == 0x00000000); testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43); @@ -108,7 +108,7 @@ test "crc32 ieee" { } test "crc32 castagnoli" { - const Crc32Castagnoli = Crc32WithPoly(Polynomial.Castagnoli); + const Crc32Castagnoli = Crc32WithPoly(.Castagnoli); testing.expect(Crc32Castagnoli.hash("") == 0x00000000); testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330); @@ -116,7 +116,7 @@ test "crc32 castagnoli" { } // half-byte lookup table implementation. -pub fn Crc32SmallWithPoly(comptime poly: u32) type { +pub fn Crc32SmallWithPoly(comptime poly: Polynomial) type { return struct { const Self = @This(); const lookup_table = comptime block: { @@ -127,7 +127,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type { var j: usize = 0; while (j < 8) : (j += 1) { if (crc & 1 == 1) { - crc = (crc >> 1) ^ poly; + crc = (crc >> 1) ^ @enumToInt(poly); } else { crc = (crc >> 1); } @@ -164,7 +164,7 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type { } test "small crc32 ieee" { - const Crc32Ieee = Crc32SmallWithPoly(Polynomial.IEEE); + const Crc32Ieee = Crc32SmallWithPoly(.IEEE); testing.expect(Crc32Ieee.hash("") == 0x00000000); testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43); @@ -172,7 +172,7 @@ test "small crc32 ieee" { } test "small crc32 castagnoli" { - const Crc32Castagnoli = Crc32SmallWithPoly(Polynomial.Castagnoli); + const Crc32Castagnoli = Crc32SmallWithPoly(.Castagnoli); testing.expect(Crc32Castagnoli.hash("") == 0x00000000); testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330); From 7854a52a6b8ba541e302c5466f61e0970e5d6062 Mon Sep 17 00:00:00 2001 From: Marc Tiehuis Date: Wed, 21 Aug 2019 21:02:24 +1200 Subject: [PATCH 06/10] Add iterative-only filter to hash benchmark --- std/hash/benchmark.zig | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/std/hash/benchmark.zig b/std/hash/benchmark.zig index ca948d21bf..f09d431fce 100644 --- a/std/hash/benchmark.zig +++ b/std/hash/benchmark.zig @@ -157,9 +157,11 @@ fn usage() void { \\throughput_test [options] \\ \\Options: - \\ --filter [test-name] - \\ --seed [int] - \\ --count [int] + \\ --filter [test-name] + \\ --seed [int] + \\ --count [int] + \\ --key-size [int] + \\ --iterative-only \\ --help \\ ); @@ -191,6 +193,7 @@ pub fn main() !void { var count: usize = mode(128 * MiB); var key_size: usize = 32; var seed: u32 = 0; + var test_iterative_only = false; var i: usize = 1; while (i < args.len) : (i += 1) { @@ -235,6 +238,8 @@ pub fn main() !void { try stdout.print("key_size cannot exceed block size of {}\n", block_size); std.os.exit(1); } + } else if (std.mem.eql(u8, args[i], "--iterative-only")) { + test_iterative_only = true; } else if (std.mem.eql(u8, args[i], "--help")) { usage(); return; @@ -246,19 +251,23 @@ pub fn main() !void { inline for (hashes) |H| { if (filter == null or std.mem.indexOf(u8, H.name, filter.?) != null) { - try stdout.print("{}\n", H.name); + if (!test_iterative_only or H.has_iterative_api) { + try stdout.print("{}\n", H.name); - // Always reseed prior to every call so we are hashing the same buffer contents. - // This allows easier comparison between different implementations. - if (H.has_iterative_api) { - prng.seed(seed); - const result = try benchmarkHash(H, count); - try stdout.print(" iterative: {:4} MiB/s [{x:0<16}]\n", result.throughput / (1 * MiB), result.hash); + // Always reseed prior to every call so we are hashing the same buffer contents. + // This allows easier comparison between different implementations. + if (H.has_iterative_api) { + prng.seed(seed); + const result = try benchmarkHash(H, count); + try stdout.print(" iterative: {:4} MiB/s [{x:0<16}]\n", result.throughput / (1 * MiB), result.hash); + } + + if (!test_iterative_only) { + prng.seed(seed); + const result_small = try benchmarkHashSmallKeys(H, key_size, count); + try stdout.print(" small keys: {:4} MiB/s [{x:0<16}]\n", result_small.throughput / (1 * MiB), result_small.hash); + } } - - prng.seed(seed); - const result_small = try benchmarkHashSmallKeys(H, key_size, count); - try stdout.print(" small keys: {:4} MiB/s [{x:0<16}]\n", result_small.throughput / (1 * MiB), result_small.hash); } } } From 16fa255f48ae2d290bc26ceb41489f2c3e21b96d Mon Sep 17 00:00:00 2001 From: Marc Tiehuis Date: Wed, 21 Aug 2019 21:34:42 +1200 Subject: [PATCH 07/10] Inline full slice hashing This gives moderate speed improvements when hashing small keys. The crc/adler/fnv inlining did not provide enough speed up to warrant the change. OLD: wyhash small keys: 2277 MiB/s [c14617a1e3800000] siphash(1,3) small keys: 937 MiB/s [b2919222ed400000] siphash(2,4) small keys: 722 MiB/s [3c3d974cc2800000] fnv1a small keys: 1580 MiB/s [70155e1cb7000000] adler32 small keys: 1898 MiB/s [00013883ef800000] crc32-slicing-by-8 small keys: 2323 MiB/s [0035bf3dcac00000] crc32-half-byte-lookup small keys: 218 MiB/s [0035bf3dcac00000] NEW: wyhash small keys: 2775 MiB/s [c14617a1e3800000] siphash(1,3) small keys: 1086 MiB/s [b2919222ed400000] siphash(2,4) small keys: 789 MiB/s [3c3d974cc2800000] fnv1a small keys: 1604 MiB/s [70155e1cb7000000] adler32 small keys: 1856 MiB/s [00013883ef800000] crc32-slicing-by-8 small keys: 2336 MiB/s [0035bf3dcac00000] crc32-half-byte-lookup small keys: 218 MiB/s [0035bf3dcac00000] --- std/hash/siphash.zig | 4 ++-- std/hash/wyhash.zig | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/std/hash/siphash.zig b/std/hash/siphash.zig index 8e83d67897..aa38c61863 100644 --- a/std/hash/siphash.zig +++ b/std/hash/siphash.zig @@ -152,8 +152,8 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) pub fn hash(key: []const u8, input: []const u8) T { var c = Self.init(key); - c.update(input); - return c.final(); + @inlineCall(c.update, input); + return @inlineCall(c.final); } }; } diff --git a/std/hash/wyhash.zig b/std/hash/wyhash.zig index dfa5156cad..f5cca121f4 100644 --- a/std/hash/wyhash.zig +++ b/std/hash/wyhash.zig @@ -116,8 +116,8 @@ pub const Wyhash = struct { pub fn hash(seed: u64, input: []const u8) u64 { var c = Wyhash.init(seed); - c.update(input); - return c.final(); + @inlineCall(c.update, input); + return @inlineCall(c.final); } }; From 3ec10ea174d5738098e9bd1ef709b552603cc3b4 Mon Sep 17 00:00:00 2001 From: Tetralux Date: Thu, 22 Aug 2019 20:41:33 +0000 Subject: [PATCH 08/10] parsing of align(N) on struct fields --- src/all_types.hpp | 2 ++ src/analyze.cpp | 16 ++++++++++++++-- src/parser.cpp | 8 +++++--- std/zig/ast.zig | 1 + std/zig/parse.zig | 15 +++++++++------ 5 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/all_types.hpp b/src/all_types.hpp index 559ebe8cda..2394bcb73b 100644 --- a/src/all_types.hpp +++ b/src/all_types.hpp @@ -849,6 +849,8 @@ struct AstNodeStructField { Buf *name; AstNode *type; AstNode *value; + // populated if the "align(A)" is present + AstNode *align_expr; }; struct AstNodeStringLiteral { diff --git a/src/analyze.cpp b/src/analyze.cpp index a06bba3f2a..aa625b6e22 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -2255,9 +2255,21 @@ static Error resolve_struct_alignment(CodeGen *g, ZigType *struct_type) { if (field->gen_index == SIZE_MAX) continue; - size_t this_field_align; - if (packed) { + uint32_t this_field_align; + + // TODO: Sets the field alignment in the type, but doesn't do anything + // to actually make that happen yet. + AstNode *align_expr = field->decl_node->data.struct_field.align_expr; + if (align_expr != nullptr) { + if (!analyze_const_align(g, &struct_type->data.structure.decls_scope->base, align_expr, &this_field_align)) { + field->type_entry = g->builtin_types.entry_invalid; + } else { + field->align = this_field_align; + } + } else if (packed) { // TODO: https://github.com/ziglang/zig/issues/1512 + // TODO: Validate requested alignment is possible, given packed, + // and given other field alignments. this_field_align = 1; // TODO If we have no type_entry for the field, we've already failed to // compile the program correctly. This stage1 compiler needs a deeper diff --git a/src/parser.cpp b/src/parser.cpp index 1e7e36d0bd..6cd6c2f045 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -782,24 +782,26 @@ static AstNode *ast_parse_var_decl(ParseContext *pc) { return res; } -// ContainerField <- IDENTIFIER (COLON TypeExpr)? (EQUAL Expr)? +// ContainerField <- IDENTIFIER (COLON TypeExpr ByteAlign?)? (EQUAL Expr)? static AstNode *ast_parse_container_field(ParseContext *pc) { Token *identifier = eat_token_if(pc, TokenIdSymbol); if (identifier == nullptr) return nullptr; AstNode *type_expr = nullptr; - if (eat_token_if(pc, TokenIdColon) != nullptr) + if (eat_token_if(pc, TokenIdColon) != nullptr) { type_expr = ast_expect(pc, ast_parse_type_expr); + } + AstNode *align_expr = ast_parse_byte_align(pc); AstNode *expr = nullptr; if (eat_token_if(pc, TokenIdEq) != nullptr) expr = ast_expect(pc, ast_parse_expr); - AstNode *res = ast_create_node(pc, NodeTypeStructField, identifier); res->data.struct_field.name = token_buf(identifier); res->data.struct_field.type = type_expr; res->data.struct_field.value = expr; + res->data.struct_field.align_expr = align_expr; return res; } diff --git a/std/zig/ast.zig b/std/zig/ast.zig index e5781da035..c2f369ff4a 100644 --- a/std/zig/ast.zig +++ b/std/zig/ast.zig @@ -761,6 +761,7 @@ pub const Node = struct { name_token: TokenIndex, type_expr: ?*Node, value_expr: ?*Node, + align_expr: ?*Node, pub fn iterate(self: *ContainerField, index: usize) ?*Node { var i = index; diff --git a/std/zig/parse.zig b/std/zig/parse.zig index 0a2fbb4fa1..0511b6dce0 100644 --- a/std/zig/parse.zig +++ b/std/zig/parse.zig @@ -380,16 +380,18 @@ fn parseVarDecl(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { return &node.base; } -/// ContainerField <- IDENTIFIER (COLON TypeExpr)? (EQUAL Expr)? +/// ContainerField <- IDENTIFIER (COLON TypeExpr ByteAlign?)? (EQUAL Expr)? fn parseContainerField(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*Node { const name_token = eatToken(it, .Identifier) orelse return null; - const type_expr = if (eatToken(it, .Colon)) |_| - try expectNode(arena, it, tree, parseTypeExpr, AstError{ + var align_expr: ?*Node = null; + var type_expr: ?*Node = null; + if (eatToken(it, .Colon)) |_| { + type_expr = try expectNode(arena, it, tree, parseTypeExpr, AstError{ .ExpectedTypeExpr = AstError.ExpectedTypeExpr{ .token = it.index }, - }) - else - null; + }); + align_expr = try parseByteAlign(arena, it, tree); + } const value_expr = if (eatToken(it, .Equal)) |_| try expectNode(arena, it, tree, parseExpr, AstError{ @@ -406,6 +408,7 @@ fn parseContainerField(arena: *Allocator, it: *TokenIterator, tree: *Tree) !?*No .name_token = name_token, .type_expr = type_expr, .value_expr = value_expr, + .align_expr = align_expr, }; return &node.base; } From 43587af01acba98d9c09c4e0cf20343be8d7c81b Mon Sep 17 00:00:00 2001 From: Tetralux Date: Thu, 22 Aug 2019 22:58:38 +0000 Subject: [PATCH 09/10] rendering of align(N) on struct fields --- std/zig/parser_test.zig | 9 +++++++++ std/zig/render.zig | 15 ++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig index 871195280f..73089f76fd 100644 --- a/std/zig/parser_test.zig +++ b/std/zig/parser_test.zig @@ -166,6 +166,15 @@ test "zig fmt: doc comments on param decl" { ); } +test "zig fmt: aligned struct field" { + try testCanonical( + \\pub const S = struct { + \\ f: i32 align(32), + \\}; + \\ + ); +} + test "zig fmt: preserve space between async fn definitions" { try testCanonical( \\async fn a() void {} diff --git a/std/zig/render.zig b/std/zig/render.zig index 035526ed11..66edf4daef 100644 --- a/std/zig/render.zig +++ b/std/zig/render.zig @@ -206,7 +206,20 @@ fn renderTopLevelDecl(allocator: *mem.Allocator, stream: var, tree: *ast.Tree, i } else if (field.type_expr != null and field.value_expr == null) { try renderToken(tree, stream, field.name_token, indent, start_col, Space.None); // name try renderToken(tree, stream, tree.nextToken(field.name_token), indent, start_col, Space.Space); // : - return renderExpression(allocator, stream, tree, indent, start_col, field.type_expr.?, Space.Comma); // type, + + if (field.align_expr) |align_value_expr| { + try renderExpression(allocator, stream, tree, indent, start_col, field.type_expr.?, Space.Space); // type + const lparen_token = tree.prevToken(align_value_expr.firstToken()); + const align_kw = tree.prevToken(lparen_token); + const rparen_token = tree.nextToken(align_value_expr.lastToken()); + try renderToken(tree, stream, align_kw, indent, start_col, Space.None); // align + try renderToken(tree, stream, lparen_token, indent, start_col, Space.None); // ( + try renderExpression(allocator, stream, tree, indent, start_col, align_value_expr, Space.None); // alignment + try renderToken(tree, stream, rparen_token, indent, start_col, Space.Comma); // ) + } else { + try renderExpression(allocator, stream, tree, indent, start_col, field.type_expr.?, Space.Comma); // type, + } + } else if (field.type_expr == null and field.value_expr != null) { try renderToken(tree, stream, field.name_token, indent, start_col, Space.Space); // name try renderToken(tree, stream, tree.nextToken(field.name_token), indent, start_col, Space.Space); // = From 9322eee80aadd6d9f80ebc11d29d09898237a14a Mon Sep 17 00:00:00 2001 From: Jonathan Marler Date: Thu, 22 Aug 2019 13:56:04 -0600 Subject: [PATCH 10/10] Encapsulate bigint representation, assert on cast data loss --- src/analyze.cpp | 8 ++++---- src/bigint.cpp | 23 ++++++++++++++++++++++- src/bigint.hpp | 5 ++++- src/codegen.cpp | 2 +- src/ir.cpp | 48 ++++++++++++++++++++++++------------------------ 5 files changed, 55 insertions(+), 31 deletions(-) diff --git a/src/analyze.cpp b/src/analyze.cpp index a06bba3f2a..6397a16818 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -1036,7 +1036,7 @@ static bool analyze_const_align(CodeGen *g, Scope *scope, AstNode *node, uint32_ if (type_is_invalid(align_result->type)) return false; - uint32_t align_bytes = bigint_as_unsigned(&align_result->data.x_bigint); + uint32_t align_bytes = bigint_as_u32(&align_result->data.x_bigint); if (align_bytes == 0) { add_node_error(g, node, buf_sprintf("alignment must be >= 1")); return false; @@ -1068,7 +1068,7 @@ static bool analyze_const_string(CodeGen *g, Scope *scope, AstNode *node, Buf ** return true; } expand_undef_array(g, array_val); - size_t len = bigint_as_unsigned(&len_field->data.x_bigint); + size_t len = bigint_as_usize(&len_field->data.x_bigint); Buf *result = buf_alloc(); buf_resize(result, len); for (size_t i = 0; i < len; i += 1) { @@ -1078,7 +1078,7 @@ static bool analyze_const_string(CodeGen *g, Scope *scope, AstNode *node, Buf ** add_node_error(g, node, buf_sprintf("use of undefined value")); return false; } - uint64_t big_c = bigint_as_unsigned(&char_val->data.x_bigint); + uint64_t big_c = bigint_as_u64(&char_val->data.x_bigint); assert(big_c <= UINT8_MAX); uint8_t c = (uint8_t)big_c; buf_ptr(result)[i] = c; @@ -5976,7 +5976,7 @@ void render_const_value(CodeGen *g, Buf *buf, ConstExprValue *const_val) { { if (is_slice(type_entry)) { ConstExprValue *len_val = &const_val->data.x_struct.fields[slice_len_index]; - size_t len = bigint_as_unsigned(&len_val->data.x_bigint); + size_t len = bigint_as_usize(&len_val->data.x_bigint); ConstExprValue *ptr_val = &const_val->data.x_struct.fields[slice_ptr_index]; if (ptr_val->special == ConstValSpecialUndef) { diff --git a/src/bigint.cpp b/src/bigint.cpp index da53a2b129..b227b3b4f7 100644 --- a/src/bigint.cpp +++ b/src/bigint.cpp @@ -15,6 +15,8 @@ #include #include +static uint64_t bigint_as_unsigned(const BigInt *bigint); + static void bigint_normalize(BigInt *dest) { const uint64_t *digits = bigint_ptr(dest); @@ -1660,7 +1662,7 @@ size_t bigint_clz(const BigInt *bi, size_t bit_count) { return count; } -uint64_t bigint_as_unsigned(const BigInt *bigint) { +static uint64_t bigint_as_unsigned(const BigInt *bigint) { assert(!bigint->is_negative); if (bigint->digit_count == 0) { return 0; @@ -1671,6 +1673,25 @@ uint64_t bigint_as_unsigned(const BigInt *bigint) { } } +uint64_t bigint_as_u64(const BigInt *bigint) +{ + return bigint_as_unsigned(bigint); +} + +uint32_t bigint_as_u32(const BigInt *bigint) { + uint64_t value64 = bigint_as_unsigned(bigint); + uint32_t value32 = (uint32_t)value64; + assert (value64 == value32); + return value32; +} + +size_t bigint_as_usize(const BigInt *bigint) { + uint64_t value64 = bigint_as_unsigned(bigint); + size_t valueUsize = (size_t)value64; + assert (value64 == valueUsize); + return valueUsize; +} + int64_t bigint_as_signed(const BigInt *bigint) { if (bigint->digit_count == 0) { return 0; diff --git a/src/bigint.hpp b/src/bigint.hpp index 48b222a227..276ccd64f9 100644 --- a/src/bigint.hpp +++ b/src/bigint.hpp @@ -36,7 +36,10 @@ void bigint_init_bigfloat(BigInt *dest, const BigFloat *op); void bigint_init_data(BigInt *dest, const uint64_t *digits, size_t digit_count, bool is_negative); // panics if number won't fit -uint64_t bigint_as_unsigned(const BigInt *bigint); +uint64_t bigint_as_u64(const BigInt *bigint); +uint32_t bigint_as_u32(const BigInt *bigint); +size_t bigint_as_usize(const BigInt *bigint); + int64_t bigint_as_signed(const BigInt *bigint); static inline const uint64_t *bigint_ptr(const BigInt *bigint) { diff --git a/src/codegen.cpp b/src/codegen.cpp index 622ade712a..6c87d3e0c3 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -2872,7 +2872,7 @@ static void add_error_range_check(CodeGen *g, ZigType *err_set_type, ZigType *in eval_min_max_value_int(g, int_type, &biggest_possible_err_val, true); if (bigint_fits_in_bits(&biggest_possible_err_val, 64, false) && - bigint_as_unsigned(&biggest_possible_err_val) < g->errors_by_index.length) + bigint_as_usize(&biggest_possible_err_val) < g->errors_by_index.length) { ok_bit = neq_zero_bit; } else { diff --git a/src/ir.cpp b/src/ir.cpp index cdddce1b18..5251e6fb78 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -5766,7 +5766,7 @@ static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode buf_sprintf("value %s too large for u32 bit offset", buf_ptr(val_buf))); return irb->codegen->invalid_instruction; } - bit_offset_start = bigint_as_unsigned(node->data.pointer_type.bit_offset_start); + bit_offset_start = bigint_as_u32(node->data.pointer_type.bit_offset_start); } uint32_t host_int_bytes = 0; @@ -5778,7 +5778,7 @@ static IrInstruction *ir_gen_pointer_type(IrBuilder *irb, Scope *scope, AstNode buf_sprintf("value %s too large for u32 byte count", buf_ptr(val_buf))); return irb->codegen->invalid_instruction; } - host_int_bytes = bigint_as_unsigned(node->data.pointer_type.host_int_bytes); + host_int_bytes = bigint_as_u32(node->data.pointer_type.host_int_bytes); } if (host_int_bytes != 0 && bit_offset_start >= host_int_bytes * 8) { @@ -11550,7 +11550,7 @@ static IrInstruction *ir_analyze_int_to_err(IrAnalyze *ira, IrInstruction *sourc return ira->codegen->invalid_instruction; } - size_t index = bigint_as_unsigned(&val->data.x_bigint); + size_t index = bigint_as_usize(&val->data.x_bigint); result->value.data.x_err_set = ira->codegen->errors_by_index.at(index); return result; } else { @@ -12520,7 +12520,7 @@ static bool ir_resolve_align(IrAnalyze *ira, IrInstruction *value, uint32_t *out if (!const_val) return false; - uint32_t align_bytes = bigint_as_unsigned(&const_val->data.x_bigint); + uint32_t align_bytes = bigint_as_u32(&const_val->data.x_bigint); if (align_bytes == 0) { ir_add_error(ira, value, buf_sprintf("alignment must be >= 1")); return false; @@ -12547,7 +12547,7 @@ static bool ir_resolve_unsigned(IrAnalyze *ira, IrInstruction *value, ZigType *i if (!const_val) return false; - *out = bigint_as_unsigned(&const_val->data.x_bigint); + *out = bigint_as_u64(&const_val->data.x_bigint); return true; } @@ -12595,7 +12595,7 @@ static bool ir_resolve_atomic_order(IrAnalyze *ira, IrInstruction *value, Atomic if (!const_val) return false; - *out = (AtomicOrder)bigint_as_unsigned(&const_val->data.x_enum_tag); + *out = (AtomicOrder)bigint_as_u32(&const_val->data.x_enum_tag); return true; } @@ -12615,7 +12615,7 @@ static bool ir_resolve_atomic_rmw_op(IrAnalyze *ira, IrInstruction *value, Atomi if (!const_val) return false; - *out = (AtomicRmwOp)bigint_as_unsigned(&const_val->data.x_enum_tag); + *out = (AtomicRmwOp)bigint_as_u32(&const_val->data.x_enum_tag); return true; } @@ -12635,7 +12635,7 @@ static bool ir_resolve_global_linkage(IrAnalyze *ira, IrInstruction *value, Glob if (!const_val) return false; - *out = (GlobalLinkageId)bigint_as_unsigned(&const_val->data.x_enum_tag); + *out = (GlobalLinkageId)bigint_as_u32(&const_val->data.x_enum_tag); return true; } @@ -12655,7 +12655,7 @@ static bool ir_resolve_float_mode(IrAnalyze *ira, IrInstruction *value, FloatMod if (!const_val) return false; - *out = (FloatMode)bigint_as_unsigned(&const_val->data.x_enum_tag); + *out = (FloatMode)bigint_as_u32(&const_val->data.x_enum_tag); return true; } @@ -12684,7 +12684,7 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) { return array_val->data.x_array.data.s_buf; } expand_undef_array(ira->codegen, array_val); - size_t len = bigint_as_unsigned(&len_field->data.x_bigint); + size_t len = bigint_as_usize(&len_field->data.x_bigint); Buf *result = buf_alloc(); buf_resize(result, len); for (size_t i = 0; i < len; i += 1) { @@ -12694,7 +12694,7 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) { ir_add_error(ira, casted_value, buf_sprintf("use of undefined value")); return nullptr; } - uint64_t big_c = bigint_as_unsigned(&char_val->data.x_bigint); + uint64_t big_c = bigint_as_u64(&char_val->data.x_bigint); assert(big_c <= UINT8_MAX); uint8_t c = (uint8_t)big_c; buf_ptr(result)[i] = c; @@ -13829,7 +13829,7 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i op1_array_val = ptr_val->data.x_ptr.data.base_array.array_val; op1_array_index = ptr_val->data.x_ptr.data.base_array.elem_index; ConstExprValue *len_val = &op1_val->data.x_struct.fields[slice_len_index]; - op1_array_end = op1_array_index + bigint_as_unsigned(&len_val->data.x_bigint); + op1_array_end = op1_array_index + bigint_as_usize(&len_val->data.x_bigint); } else { ir_add_error(ira, op1, buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op1->value.type->name))); @@ -13862,7 +13862,7 @@ static IrInstruction *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *i op2_array_val = ptr_val->data.x_ptr.data.base_array.array_val; op2_array_index = ptr_val->data.x_ptr.data.base_array.elem_index; ConstExprValue *len_val = &op2_val->data.x_struct.fields[slice_len_index]; - op2_array_end = op2_array_index + bigint_as_unsigned(&len_val->data.x_bigint); + op2_array_end = op2_array_index + bigint_as_usize(&len_val->data.x_bigint); } else { ir_add_error(ira, op2, buf_sprintf("expected array or C string literal, found '%s'", buf_ptr(&op2->value.type->name))); @@ -16734,7 +16734,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct uint64_t abi_align = get_abi_alignment(ira->codegen, return_type->data.pointer.child_type); uint64_t ptr_align = get_ptr_align(ira->codegen, return_type); if (instr_is_comptime(casted_elem_index)) { - uint64_t index = bigint_as_unsigned(&casted_elem_index->value.data.x_bigint); + uint64_t index = bigint_as_u64(&casted_elem_index->value.data.x_bigint); if (array_type->id == ZigTypeIdArray) { uint64_t array_len = array_type->data.array.len; if (index >= array_len) { @@ -16896,7 +16896,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct ConstExprValue *len_field = &array_ptr_val->data.x_struct.fields[slice_len_index]; IrInstruction *result = ir_const(ira, &elem_ptr_instruction->base, return_type); ConstExprValue *out_val = &result->value; - uint64_t slice_len = bigint_as_unsigned(&len_field->data.x_bigint); + uint64_t slice_len = bigint_as_u64(&len_field->data.x_bigint); if (index >= slice_len) { ir_add_error_node(ira, elem_ptr_instruction->base.source_node, buf_sprintf("index %" ZIG_PRI_u64 " outside slice of size %" ZIG_PRI_u64, @@ -21181,7 +21181,7 @@ static IrInstruction *ir_analyze_instruction_from_bytes(IrAnalyze *ira, IrInstru ConstExprValue *len_val = &val->data.x_struct.fields[slice_len_index]; if (value_is_comptime(len_val)) { - known_len = bigint_as_unsigned(&len_val->data.x_bigint); + known_len = bigint_as_u64(&len_val->data.x_bigint); have_known_len = true; } } @@ -21527,7 +21527,7 @@ static IrInstruction *ir_analyze_instruction_memset(IrAnalyze *ira, IrInstructio zig_panic("TODO memset on null ptr"); } - size_t count = bigint_as_unsigned(&casted_count->value.data.x_bigint); + size_t count = bigint_as_usize(&casted_count->value.data.x_bigint); size_t end = start + count; if (end > bound_end) { ir_add_error(ira, count_value, buf_sprintf("out of bounds pointer access")); @@ -21612,7 +21612,7 @@ static IrInstruction *ir_analyze_instruction_memcpy(IrAnalyze *ira, IrInstructio casted_count->value.special == ConstValSpecialStatic && casted_dest_ptr->value.data.x_ptr.special != ConstPtrSpecialHardCodedAddr) { - size_t count = bigint_as_unsigned(&casted_count->value.data.x_bigint); + size_t count = bigint_as_usize(&casted_count->value.data.x_bigint); ConstExprValue *dest_ptr_val = &casted_dest_ptr->value; ConstExprValue *dest_elements; @@ -21897,7 +21897,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction case ConstPtrSpecialBaseArray: array_val = parent_ptr->data.x_ptr.data.base_array.array_val; abs_offset = parent_ptr->data.x_ptr.data.base_array.elem_index; - rel_end = bigint_as_unsigned(&len_val->data.x_bigint); + rel_end = bigint_as_usize(&len_val->data.x_bigint); break; case ConstPtrSpecialBaseStruct: zig_panic("TODO slice const inner struct"); @@ -21910,7 +21910,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction case ConstPtrSpecialHardCodedAddr: array_val = nullptr; abs_offset = 0; - rel_end = bigint_as_unsigned(&len_val->data.x_bigint); + rel_end = bigint_as_usize(&len_val->data.x_bigint); break; case ConstPtrSpecialFunction: zig_panic("TODO slice of slice cast from function"); @@ -21921,7 +21921,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction zig_unreachable(); } - uint64_t start_scalar = bigint_as_unsigned(&casted_start->value.data.x_bigint); + uint64_t start_scalar = bigint_as_u64(&casted_start->value.data.x_bigint); if (!ptr_is_undef && start_scalar > rel_end) { ir_add_error(ira, &instruction->base, buf_sprintf("out of bounds slice")); return ira->codegen->invalid_instruction; @@ -21929,7 +21929,7 @@ static IrInstruction *ir_analyze_instruction_slice(IrAnalyze *ira, IrInstruction uint64_t end_scalar; if (end) { - end_scalar = bigint_as_unsigned(&end->value.data.x_bigint); + end_scalar = bigint_as_u64(&end->value.data.x_bigint); } else { end_scalar = rel_end; } @@ -23500,7 +23500,7 @@ static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *sou BigInt bn; bigint_read_twos_complement(&bn, buf, codegen->builtin_types.entry_usize->data.integral.bit_count, codegen->is_big_endian, false); - val->data.x_ptr.data.hard_coded_addr.addr = bigint_as_unsigned(&bn); + val->data.x_ptr.data.hard_coded_addr.addr = bigint_as_usize(&bn); return ErrorNone; } case ZigTypeIdArray: @@ -23693,7 +23693,7 @@ static IrInstruction *ir_analyze_int_to_ptr(IrAnalyze *ira, IrInstruction *sourc if (!val) return ira->codegen->invalid_instruction; - uint64_t addr = bigint_as_unsigned(&val->data.x_bigint); + uint64_t addr = bigint_as_u64(&val->data.x_bigint); if (!ptr_allows_addr_zero(ptr_type) && addr == 0) { ir_add_error(ira, source_instr, buf_sprintf("pointer type '%s' does not allow address zero", buf_ptr(&ptr_type->name)));