Skip to content

Instantly share code, notes, and snippets.

View imaami's full-sized avatar
🐺
C/C++/Linux ░▒▓▉▊▋▌▍▎▏

Juuso Alasuutari imaami

🐺
C/C++/Linux ░▒▓▉▊▋▌▍▎▏
  • Finland
View GitHub Profile
@imaami
imaami / popcnt.c
Created April 16, 2024 20:01
Generic popcnt
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#define popcnt(x) (size_t)({\
typeof (_Generic(x, \
int32_t: (uint32_t)1, \
int64_t: (uint64_t)1, \
uint32_t: (uint32_t)1, \
uint64_t: (uint64_t)1))y=x; \
@imaami
imaami / bitpr.c
Created April 7, 2024 21:28
Print bits with x86_64 intrinsics
/* Compile with "gcc -std=c23 -mbmi2 -mlzcnt" (gcc-14)
* or "gcc -std=c2x -mbmi2 -mlzcnt" (gcc-13)
*/
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <immintrin.h>
static void
bitpr (uint64_t n)
// Behavior of gcc-14 and clang-19 diverge on this.
// Compile with -std=gnu23.
#include <stdio.h>
int main(void) {
printf("%s\n", _Generic(
(struct{_BitInt(8) x : 2;}){}.x,
_BitInt(2): "gcc", _BitInt(8): "clang"));
}
@imaami
imaami / compile-time-whitespace.c
Created February 6, 2024 00:51
Compile-time generation of correctly aligned text
#include <stddef.h>
#include <stdio.h>
#define options(X) \
X('h', "help", "", "print this help") \
X('s', "segfault", "", "commit sudoku") \
X('d', "directory", "DIR", "touch a directory") \
X('u', "uid-of", "NAME", "print a user ID")
#ifdef __clang__
@imaami
imaami / stringy_check.c
Last active January 12, 2024 13:43
Check for stringy primes
/* SPDX-License-Identifier: GPL-2.0-or-later */
/**
* @file stringy_check.c
* @author Juuso Alasuutari
*
* @brief Parse the raw content bytes of text strings as if the strings were
* arbitrarily large integers, and check if the result is a different
* prime number in both the big- and little-endian byte orders.
*
* I call the group of primes generated by this method "stringy primes"; run
@imaami
imaami / dstr.c
Last active March 4, 2024 19:50
A dumb string thing.
/* SPDX-License-Identifier: GPL-2.0-or-later */
/** @file dstr.c
*
* @author Juuso Alasuutari
*/
#include <errno.h>
#include "dstr.h"
/**
@imaami
imaami / bashplainer.sh
Last active December 22, 2023 11:40
Document your bash scripts or Meth Santa visits you in your sleep
#!/usr/bin/env bash
#
# IMPORTANT: The HTTP authorization header must be found
# in ~/.openai in full, not just the API key.
#
# Usage: ./bashplainer.sh /path/to/file.sh
#
# Requirements: dos2unix, jq, curl
#
@imaami
imaami / documentarist2.sh
Created December 20, 2023 19:49
Let GPT describe how your code sucks.
#!/usr/bin/env bash
#
# IMPORTANT: The HTTP authorization header must be found
# in ~/.openai in full, not just the API key.
#
# Usage: ./documentarist.sh [options] /path/to/file.c
#
# Requirements: dos2unix, cpp, clang-format, jq, curl
#
@imaami
imaami / etc_hosts_dig.sh
Created December 18, 2023 23:32
Cisco Umbrella is a perfect system without flaws
#!/usr/bin/env bash
#
# Usage (_only_ if you really know what you're doing):
# etc_hosts_dig.sh >> /etc/hosts
# expand this list as needed
declare -a hosts_list=(
api.openai.com
auth0.openai.com
cdn.openai.com
@imaami
imaami / 99-xdm.rules
Last active December 18, 2023 23:15
Correctly defer xdm.service until /dev/dri/card0 exists.
# /etc/udev/rules.d/99-xdm.rules
# Create a dev-dri-card0.device unit for xdm.service to wait on.
SUBSYSTEM=="drm", KERNEL=="card0", TAG+="systemd"