Skip to content

Instantly share code, notes, and snippets.

View Blokyk's full-sized avatar
👨‍🎓
Living and studying

Blokyk

👨‍🎓
Living and studying
  • 00:19 (UTC +02:00)
View GitHub Profile
#include <stdint.h>
// --- popcnt as a C integer constant expression ---
// this is the kinda bullshit required when you can't use C++ >:|
#define __const_popcnt2(val) ((val) == 0 ? 0 : ((val) == 2 ? 2 : 1))
#define __const_popcnt4(val) (__const_popcnt2((val) & 0b11) + __const_popcnt2((uint8_t)((val) & 0b1100) >> 2))
#define const_popcnt8(val) (__const_popcnt4((val) & 0b1111) + __const_popcnt4((uint8_t)(val) >> 4))
#define const_popcnt16(val) (const_popcnt8((val) & 0b11111111) + const_popcnt8((uint16_t)(val) >> 8))
@Blokyk
Blokyk / note.md
Created December 29, 2023 02:06
Notes: Delegates as custom attribute argument

Notes: Add support for delegates as attribute arguments

Original proposal

About dotnet/runtime

  • Don't open the repo in vscode with the c/c++ extension enabled, it will hoard enormous amounts of resources.

  • Be sure to build clr+libs in Debug the first time; then you can

@Blokyk
Blokyk / MyGenerator.csproj
Last active December 8, 2023 00:35
Generator reference refresh workaround (requires local nuget field)
<Project Sdk="Microsoft.NET.Sdk">
<!-- ...normal msbuild stuff... -->
<PropertyGroup>
<PackageId>YOUR.GENERATOR.NAME</PackageId>
<Version>0.1</Version> <!-- Actual release version -->
<!-- Ensure dynamic verison is only visible to normal builds
and not the IDE -->
@Blokyk
Blokyk / EnumInfo.cs
Created October 20, 2023 22:39
Access info about a generic TEnum type in C#
using System.Runtime.CompilerServices;
/// <summary>
/// Computes various properties of <typeparamref name="TEnum"/>
/// </summary>
/// <remarks>WARNING: This assumes that <typeparamref name="TEnum"/>'s underlying type is an <see langword="int"/></remarks>
public static class EnumInfo<TEnum> where TEnum : struct, Enum
{
private static readonly Lazy<HashSet<TEnum>> _valuesSet = new(static () => new HashSet<TEnum>(EnumValues!));
@Blokyk
Blokyk / SpanExtensions.cs
Created September 27, 2023 19:32
Reverse 4-bytes endianness of Span of bytes (not vectorized)
using System;
using System.Collections.Generic;
static class SpanExtensions
{
// just a quick and dirty version, could be a lot faster
// by just using Vector<byte>.Shuffle if available
public static void Reverse4ByteEndianness(Span<byte> bytes) {
byte b0, b1, b2, b3;
for (int i = 3; i < bytes.Length; i += 4) {
@Blokyk
Blokyk / better_merge_sort.c
Last active November 20, 2022 02:55
An implementation of MergeSort using two alternating buffers to avoid memcpy's (in pure C)
/*
* MIT License
*
* Copyright (c) 2022 blokyk
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@Blokyk
Blokyk / display_clock.sh
Last active April 21, 2022 14:41
display_clock
#!/bin/bash
function center_txt() {
txt=$1
line_nb=$(wc -l <<< $txt)
available_lines=$(($(($LINES - $line_nb)) / 2))
#echo $available_lines
#echo "$txt"
perl -E "say \"\n\" x $available_lines"
#echo $top | wc -l
@Blokyk
Blokyk / funcs.glsl
Last active February 23, 2022 16:39
Cool fluid flow functions -- from anvaka.github.io/fieldplay
// Pay attention to the scaling !
// Some results might look boring or glitchy from afar (or close), but don't fret !
// p.x and p.y are current coordinates
// v.x and v.y is a velocity at point p
// https://bit.ly/3bmvQ58 -- zoomed in
vec2 get_velocity(vec2 p) {
vec2 v = vec2(0., 0.);
@Blokyk
Blokyk / ValidOn.Verbose.cs
Last active June 15, 2021 15:21
All possible combinations of validity for graphviz attributes
[System.Flags]
public enum ValidOn {
Edge = 0,
EdgeNode = Edge & Node,
EdgeNodeGraph = EdgeNode & Graph,
EdgeNodeGraphSubgraph = EdgeNodeGraph & Subgraph,
EdgeNodeGraphSubgraphCluster = Edge & Node & Graph & Subgraph & Cluster,
EdgeNodeGraphCluster = EdgeNodeGraph & Cluster,
EdgeNodeSubgraph = EdgeNode & Subgraph,
EdgeNodeSubgraphCluster = EdgeNodeSubgraph & Cluster,
@Blokyk
Blokyk / colors.cs
Last active June 15, 2021 14:25
X11 graphviz color palette enum
public enum Colors {
AliceBlue,
AntiqueWhite, AntiqueWhite1, AntiqueWhite2, AntiqueWhite3, AntiqueWhite4,
Aqua,
AquaMarine, AquaMarine1, AquaMarine2, AquaMarin3, AquaMarine4,
Azure, Azure1, Azure2, Azure3, Azure4,