Skip to content

Instantly share code, notes, and snippets.

View matu3ba's full-sized avatar

Jan Ph. H. matu3ba

View GitHub Profile
@matu3ba
matu3ba / reproduce.sh
Created February 8, 2023 20:14
sema_dump
#!/usr/bin/env sh
# Building zig in debug mode
cd zigrepo
mkdir -p build/ && cd build/ && cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_PREFIX_PATH="$HOME/dev/git/bootstrap/zig-bootstrap/musl/out/host/" -GNinja && /usr/bin/time -v ninja install && cd ..
# add zig stages 3 to PATH
zig test --debug-log compilation -freference-trace --test-filter fubar arenas.zig
zig test --debug-log sema -freference-trace thread.zig
@matu3ba
matu3ba / initialization_footgun.zig
Last active February 5, 2023 19:55
Zig Result-Location-Semantics footgun
const std = @import("std");
fn t1init(alloc: std.mem.Allocator, path1: []const u8) ![]const u8 {
const stdout_wr = std.io.getStdOut().writer();
try stdout_wr.writeAll("t1init"); // prevent compiler to optimize away this fn
return try alloc.dupe(u8, path1);
}
fn t2init(alloc: std.mem.Allocator, path2: []const u8) ![]const u8 {
_ = path2;
@matu3ba
matu3ba / results.txt
Created February 1, 2023 21:48
Using c struct instead of return and writing overflow via pointer bears the same performance.
128bit
(ins)[misterspoon@pc tryzig]$ hyperfine ./addo_crt ./addo_cstruct
Benchmark 1: ./addo_crt
Time (mean ± σ): 1.083 s ± 0.006 s [User: 1.082 s, System: 0.001 s]
Range (min … max): 1.072 s … 1.094 s 10 runs
Benchmark 2: ./addo_cstruct
Time (mean ± σ): 1.077 s ± 0.003 s [User: 1.075 s, System: 0.001 s]
Range (min … max): 1.073 s … 1.082 s 10 runs
@matu3ba
matu3ba / bit_tricks.md
Last active February 1, 2023 10:48
some more bit tricks

Assuming MIN < x < MAX:

trick operation/effect note
x & (x - 1) clear lowest 1 bit if result 0, then x is 2
`x (x + 1)` set lowest 0 bit
`x (x - 1)` set all bits to right of lowest 1 bit
x & (x + 1) clear all bits to right of lowest 0 bit
x & -x extract lowest 1 bit
~x &amp; (x + 1) extract lowest 0 bit (as 1 bit)
@matu3ba
matu3ba / fs.diff
Created January 22, 2023 01:59
Count Steps to Directory With Entry as diff file
diff --git a/lib/std/fs.zig b/lib/std/fs.zig
index 31354a278..f06db4943 100644
--- a/lib/std/fs.zig
+++ b/lib/std/fs.zig
@@ -2085,6 +2085,44 @@ pub const Dir = struct {
}
}
+ pub const CountStepsToFolderWithEntryError = error{
+ InvalidKind,
@matu3ba
matu3ba / buildprocess.md
Created January 22, 2023 01:39
Bootstep Zig from wasm file
wasm2.c ──────►┌──────┐   ┌──────┐   ┌────┐   ┌──────┐   ┌────┐   ┌────┐   ┌────┐
               │wasm2c│──►│zig1.c│──►│zig1│──►│zig2.c│──►│zig2│──►│zig3│──►│zig4│
          ┌───►└──────┘ ┌►└──────┘┌─►└────┘   └──────┘┌─►└────┘   └────┘   └────┘
          │  zig1.wasm ─┘         │                   │     ▲        ▲        ▲
CC ───────┴───────────────────────┴───────────────────┘     │      binary-identical
                                                            │
idealized, see TODO:                                        │(optional)
used LLVM libs ─────────────────────►┌────┐                 │
 │LLVM│─────────────────┘
@matu3ba
matu3ba / child_process.zig
Created January 9, 2023 19:15
spawnMac changes for posix_spawn that I found no use case for yet: user owns the actions + attribute or users doesnt.
/// Use this for additional posix spawn attributes.
/// Do not set spawn attribute flags and do not modify stdin, stdout, stderr
/// behavior, because those are set in the platform-specific spawn method.
posix_attr: if (os.hasPosixSpawn) ?os.posix_spawn.Attr else void,
posix_actions: if (os.hasPosixSpawn) ?os.posix_spawn.Actions else void,
.posix_attr = if (os.hasPosixSpawn) null else undefined,
.posix_actions = if (os.hasPosixSpawn) null else undefined,
@matu3ba
matu3ba / mkVid.sh
Created December 24, 2022 22:40
download chunklist streams (m3u8) with ffmpeg
#!/usr/bin/env sh
#sript to download chunklist streams videos
#ffmpeg -i "url_path_to_file.m3u8" -bsf:a aac_adtstoasc -vcodec copy -c copy -crf 50 file.mp4
ffmpeg -i "uri/chunklist_somehash.m3u8" -bsf:a aac_adtstoasc -vcodec copy -c copy -crf 50 file.mp4
@matu3ba
matu3ba / bZig.ps1
Created December 22, 2022 00:01
Build Zig with previous Zig version by parsing devkit.
# powershell script to parse devkit version and install Zig
# assume: 1. devkit in dir devkit, zig in dir zig in same dir
# 2. powershell scripts executable (execution in shell):
# Get-Content bZig.ps1 | PowerShell.exe -noprofile -
# parse pip install
$draftpathninja = pip show ninja
$pathninja = $draftpathninja[7].Split(" ")[1]
$joinpath = -join($pathninja, "\ninja\data\bin")
$env:Path += $joinpath
@matu3ba
matu3ba / fpZig.ps1
Created December 21, 2022 23:58
Fetch and patch devkit zig
$response = Invoke-RestMethod -Uri "https://ziglang.org/download/index.json"
$downloadlink = $response.master.'x86_64-windows'.tarball
Invoke-WebRequest -Uri "$downloadlink" -OutFile "$zigdl.zip"
# TODO finish this up