Skip to content

Instantly share code, notes, and snippets.

View matu3ba's full-sized avatar

Jan Ph. H. matu3ba

View GitHub Profile
@matu3ba
matu3ba / crosscomp_zig_master.sh
Last active October 3, 2021 12:14
cross compiling zig master to have C and C++ compiler working with master
#!/bin/sh
# The main problem with building zig with zig are build system changes.
# Further I had problems using C test suite, so it seems to me that not all
# necessary configs are being reused within the build system.
# Therefore this script cross compiles Zig with Zig to track upstream.
# However stage2 does not work with this and fails with
# error: unknown command: -print-file-name=libstdc++.a
# The following command exited with error code 1:
# $HOME/dev/git/zig/zig-bootstrap/out/host/bin/zig -print-file-name=libstdc++.a
@matu3ba
matu3ba / neg_helper.zig
Created December 12, 2021 16:39
Helper to create test data for compiler_rt negation
const math = @import("std").math;
const print = @import("std").debug.print;
fn printTestCases(comptime T: type, min_divided: T, max_divided: T) !void {
print("try test__negsi2({d}, {d});\n", .{ min_divided, max_divided });
print("try test__negsi2({d}, {d});\n", .{ max_divided, min_divided });
}
pub fn main() !void {
const ST = i128;
@matu3ba
matu3ba / cmp_helper.zig
Created December 13, 2021 21:47
Helper to create test data for compiler_rt compare
const math = @import("std").math;
const print = @import("std").debug.print;
// a < b => 0
// a == b => 1
// a > b => 2
fn printTestCase(comptime T: type, a: T, b: T) !void {
var cmp: i32 = undefined;
if (a < b) {
@matu3ba
matu3ba / config.yaml
Last active December 17, 2021 20:23
zellij config
# Configuration for zellij.
# In order to troubleshoot your configuration try using the following command:
# `zellij setup --check`
# It should show current config locations and features that are enabled.
#
# Mode | Switch
# normal | Normal
# resize | Resize
# pane | Pane
@matu3ba
matu3ba / absvXi2.zig
Last active December 17, 2021 05:59
abs implementations
// Observation: only MIN has no abs
// 2s complement is ~x+1
// 2s compl of MIN == MIN, since ~0b100..00 + 1 == 0b011..11 + 1 == 0b100..00
// NOTE: 2s complement of 0 would overflow ~0x00..00 + 1 == 0xff..ff + 1 kaboom
// working version
fn absvXi_generic(comptime ST: type) fn (a: ST) callconv(.C) ST {
return struct {
fn f(a: ST) callconv(.C) ST {
var x = a;
if (x < 0) {
@matu3ba
matu3ba / print_wrapping_example.zig
Created December 18, 2021 15:43
wrappign negation: print hex with casting to unsigned does not work
const debug = @import("std").debug;
fn printRes(comptime ST: type, a: ST, b: ST) !void {
const UT = switch (ST) {
i2 => u2,
i32 => u32,
i64 => u64,
i128 => u128,
else => unreachable,
};
@matu3ba
matu3ba / test_addv.c
Last active December 27, 2021 11:10
testing add overflow in C
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
// yes, it breaks. Clearly Zig has better semantics, as it utilizes LLVM.
int32_t addv(int32_t a, int32_t b)
{
uint32_t usum = (uint32_t)a + (uint32_t)b;
int32_t isum = (int32_t)usum;
@matu3ba
matu3ba / comb_with_rep_iter.cpp
Created December 27, 2021 18:58
combinations with repitition iterative
#include <iostream>
#include <string>
#include <vector>
void print_vector(const std::vector<int> &pos,
const std::vector<std::string> &str) {
for (size_t i = 1; i < pos.size(); ++i) // offset: i:1..N
printf("%s\t", str[pos[i]].c_str()); // str:0..N-1
printf("\n");
}
@matu3ba
matu3ba / comb_with_rep_iter.c
Created December 27, 2021 19:12
combinations with repitition iterative
#include <stdio.h>
const char *donuts[] = {"iced", "jam", "plain",
"something completely different"};
int pos[] = {0, 0, 0, 0};
void printDonuts(int k) {
for (size_t i = 1; i < k + 1; i += 1) // offset: i:1..N, N=k+1
printf("%s\t", donuts[pos[i]]); // str:0..N-1
printf("\n");
@matu3ba
matu3ba / bracket_count.zig
Last active March 26, 2022 22:41
toy bracket counter in zig
const std = @import("std");
const process = std.process;
const stdout = std.io.getStdOut();
//const log = std.log;
const usage: []const u8 =
\\Usage: bracket_count file1 file2 ..
;
pub fn main() !void {