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 / stringy.sh
Created April 30, 2024 10:21
stringy_prime thing
#!/usr/bin/env bash
{ base64 -d | tar xJf - && make >/dev/null && ./stringy_prime "$@"; } << EOF
/Td6WFoAAATm1rRGAgAhAQwAAACPmEGc4Hf/E3hdACaYSWZp2dmN5MBFtReU64IZsyM5tnRakSFu
UPbdIR/QuF5EFRUUhH/5HwelTiMRTMQTMB8haLGg9CMnIdukTtlgXKTJM/Kd0AxEruxtr46KoLCE
5wH2u0auKErz8PuO5Sohr/jU5hsMknDSkidKBpsGJPDBc0JRlAJaJ1w2Yvn75B8bpsgQvr+vPFR4
98UB7klgs9DC4xH4PakHpWzf7Oj8YqPgZDnqj+mqcWPQ6dhdjsbkbQw+SUZRRN5EpBKyVa2q4QJs
4o9u+lIkKVTDrFwnLqgMoHogm/VKoolkGLBFD1gyNxNdxMU6M1PX8Ja6m/JVu/xhQFMAuKWAe1p7
79K1RWjGJLTnJCXLHvxxn33La80M6RAPZf0hPo93iTz8Lhu5NsP0bfe1QgX3WqXB70f5TMt8nZSN
UYmb+8HtRCjMv/xBowOuxcYHY2oeUuHb2+lavETFfeqc/7wuFwMBUwhrkCf++AC0k6oBgbcte5X4
Th79/TqLq+NYm/7sEobxti/sqRvtI6onS3CyiJeBdFUDEBJ22zkZ/B65PagiVkkgCnbXeal21NWb
@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 / typeof_unqual.c
Created April 15, 2023 13:17
Backport of C23's typeof_unqual in plain C18
#define typeof_unqual(x) __typeof__(((void)1, *(__typeof__(x) *)(void *)0))
int main(void) {
const int ci = 1;
typeof_unqual(ci) i1 = ci;
typeof_unqual(const int) i2 = ci;
i1 = 0;
i2 = 0;
return i1 + i2;
}
@imaami
imaami / debruijn26.c
Last active April 2, 2024 00:00
Generate all 67108864 De Bruijn sequences B(2, 6)
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#define force_inline __attribute__((always_inline)) inline
@imaami
imaami / Makefile
Last active March 6, 2024 19:52
#template<>
override BIN := test
override SRC := test.c msg.c
override OBJ := $(SRC:%=%.o)
override DEP := $(SRC:%=%.d)
CFLAGS := -O2 -march=native -mtune=native -flto
$(BIN): $(OBJ)
$(CC) $(CFLAGS) -o $@ $^
@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 / emanate_metaname.c
Last active March 1, 2024 21:26
Emanate metaname
/*
e e e e e
e m a n a t e m e t a n a m e
a a a a a
n n n n n
a a a a a
t t t t t
e e e e e
e m a n a t e m e t a n a m e
e e e e e
@imaami
imaami / documentarist.sh
Last active February 22, 2024 13:46
Use ChatGPT to document your C/C++ code.
#!/usr/bin/env bash
#
# IMPORTANT: The HTTP authorization header must be found
# in ~/.openai in full, not just the API key.
#
# Usage: ./documentarist.sh /path/file.c "my_function()"
#
# Requirements: dos2unix, cpp, clang-format, jq, curl
#