Skip to content

Instantly share code, notes, and snippets.

View matu3ba's full-sized avatar

Jan Ph. H. matu3ba

View GitHub Profile
@matu3ba
matu3ba / comptimeStr.zig
Created January 27, 2022 11:21
comptime append strings to buffer
const std = @import("std");
const io = std.io;
//try stdout.print("All your base belong to use!\n", .{});
pub fn main() !void {
comptime var names = [_]u8{0} ** 5000;
comptime var name_offsets = [_]u64{0} ** 100; // no comptime writing to uninitialized values
comptime var offset: usize = 0;
comptime var index_offsets = 0;
name_offsets[index_offsets] = offset;
@matu3ba
matu3ba / failed_mulv.zig
Last active January 30, 2022 13:57
unsuccesful mulv optimization with wrapping overflow and without division (case sum of clzs = 32 fails)
const clz = @import("count0bits.zig");
const std = @import("std");
const math = std.math;
// mulv - multiplication oVerflow
// * @panic, if result can not be represented
// - mulvXi4_genericPerf for generic performance implementation
// TODO benchmark this approach against the simpler division approach for good and bad cases
// TODO measure used binary space
@matu3ba
matu3ba / minimal_bitvec.cpp
Last active January 31, 2022 00:24
z3 proof that mulv works up to 64bit
// code proven:
// mulv - multiplication oVerflow
// var res: ST = a *% b;
// if (a != 0 and @divTrunc(res, a) != b) {
// return -5; // overflow case
// }
// return res;
void minimal_bitvec() {
z3::context ctx;
@matu3ba
matu3ba / results.txt
Last active September 17, 2022 18:40
benchmarks for muloti
Benchmark 1: ./mulo_fast
Time (mean ± σ): 1.008 s ± 0.009 s [User: 1.006 s, System: 0.001 s]
Range (min … max): 0.996 s … 1.027 s 10 runs
Warning: The first benchmarking run for this command was significantly slower than the
rest (1.027 s). This could be caused by (filesystem) caches that were not filled until af
ter the first run. You should consider using the '--warmup' option to fill those caches b
efore the actual benchmark. Alternatively, use the '--prepare' option to clear the caches
before each timing run.
@matu3ba
matu3ba / results.txt
Last active February 6, 2022 22:06
benchmarks addo including the suggested layout change
All together:
[user@pc tryzig]$ hyperfine ./addo_*
Benchmark 1: ./addo_fast
Time (mean ± σ): 14.682 s ± 0.337 s [User: 14.640 s, System: 0.006 s]
Range (min … max): 14.315 s … 15.149 s 10 runs
Benchmark 2: ./addo_noptr
Time (mean ± σ): 13.789 s ± 0.290 s [User: 13.755 s, System: 0.001 s]
Range (min … max): 13.299 s … 14.120 s 10 runs
@matu3ba
matu3ba / README.md
Last active February 15, 2022 13:11
wip docs compier_rt

If hardware lacks basic or specialized functionality, compiler-rt adds such functionality. One such example is 64-bit integer multiplication on 32-bit x86.

Open question about scope (tracking libgcc):

  • Integer library routines => implemented
  • Soft float library routines => mostly implemented
  • Decimal float library routines => ~120 functions
  • Fixed-point fractional library routines => ~300 functions
  • Exception handling routines? => 32 functions not including undocumented ones
  • Miscellaneous routines => cache control and stack function
@matu3ba
matu3ba / comptime_buf_write.zig
Last active February 17, 2022 00:40
miscompilation from comptime writing of buffer
const std = @import("std");
const mem = std.mem;
const strings = [_][]const u8{ "Hello", "There" };
pub fn main() !void {
comptime var buf_size = 0;
inline for (strings) |str| {
buf_size += str.len;
}
const len_offsets = strings.len;
@matu3ba
matu3ba / numberSystems.txt
Created February 21, 2022 10:10
Incomplete list of numbers systems with practical usage
Incomplete list of numbers systems with practical usage
(dynamic) https://en.wikipedia.org/wiki/Radix
- combinatorics: enumerate combinations/variations/permutations of recursive+discrete structures
https://en.wikipedia.org/wiki/Mixed_radix
- date (year-month-day), time (hour-minute-second)
- combinatorics: enumerate combinations/variations/permutations of non-recursive+discrete structures
https://en.wikipedia.org/wiki/Signed_number_representations
@matu3ba
matu3ba / build.sh
Last active March 2, 2022 14:12
parentchild_pipe on windows in C
#!/usr/bin/env sh
zig cc -target x86_64-windows parent.c -o parent.exe
zig cc -target x86_64-windows child.c -o child.exe
@matu3ba
matu3ba / child.zig
Last active March 4, 2022 15:29
parentchild_pipe on Windows naively ported to Zig
const std = @import("std");
const os = std.os;
pub fn main() !void {
const buf_size = 25;
var rd_h = os.windows.HANDLE;
var buf: [buf_size]u8 = undefined;
var read: os.windows.DWORD = undefined;
rd_h = try os.windows.GetStdHandle(os.windows.STD_INPUT_HANDLE);
if (try os.windows.ReadFile(rd_h, buf, buf_size, &read, null)) {