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 / b24_expand.c
Last active September 13, 2024 18:39
Expand all overlapping 4-bit subsequences of a circular 16-bit sequence
static uint64_t
b24_expand (uint16_t seq)
{
#define seq_prep(x) 0, (uint32_t)((x) << 12U | \
(x) >> 4U) << 16U | (x)
__m128i k = _mm_shuffle_epi8(
_mm_set_epi64x(seq_prep(seq)),
_mm_set_epi64x(UINT64_C(0x0303030301010101), \
UINT64_C(0x0202020200000000)));
#undef seq_prep
@imaami
imaami / shuffle_u8x16.c
Last active September 15, 2024 11:25
Expand subsequences
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __aarch64__
#include <arm_neon.h>
@imaami
imaami / popcnt.h
Last active September 16, 2024 19:51
Generic popcount
/** @file popcnt.h
*/
#ifndef POPCNT_H_
#define POPCNT_H_
#include <limits.h>
#include <stdint.h>
#undef define_popcnt
#undef popcnt16_impl
@imaami
imaami / uwu.c
Last active September 10, 2024 19:30
uWu
#include <stdio.h>
#include <math.h>
int main(void)
{
typeof(sizeof M_E)
t=pow ( M_E,
((typeof(sizeof M_E))01 << ' ')
/(typeof( M_E))0xf0f04ef)
/ M_PI;
@imaami
imaami / fizzbuzz_by_claude_35_sonnet.c
Created September 9, 2024 16:36
FizzBuzz Enterprise Edition by claude-3.5-sonnet
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_FIZZBUZZ_LENGTH 8
#define MAX_NUMBER_LENGTH 10
typedef struct {
int (*evaluate)(int);
char *(*transform)(void);
@imaami
imaami / b24.c
Last active September 23, 2024 21:32
B(2, 4)
/**
* @file b24.c
*
* Compiling with MSVC 19.41 or later:
*
* cl.exe /TC /std:clatest /O2 /Oi /GL /GF /Zo- /favor:AMD64 /arch:AVX2 b24.c /Fe: b24.exe /MD
*/
#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
@imaami
imaami / Makefile.gnu23
Created July 9, 2024 12:48
Experimenting with compiler introspection in GNU Make
.PHONY: default gnu23 gnu23.mk
default:| gnu23; $(info $(CC) -std=$(gnu23))
gnu23:| gnu23.mk; $(eval include gnu23.mk)
gnu23.mk: gnu23.h; @$(CC) -E -xc -P $< >$@
gnu23.h: $(lastword $(MAKEFILE_LIST)); $(call cstd,$@) @:
override cstd =$(file >$1,$(.cstd))
override .cstd =$(call \
P,#ifdef __clang_major__) $(call \
L,#if __clang_major__ > 17) $(call \
L,override gnu23 := gnu23) $(call \
@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)