Skip to content
View lilweege's full-sized avatar
Block or Report

Block or report lilweege

Block user

Prevent this user from interacting with your repositories and sending you notifications. Learn more about blocking users.

You must be logged in to block users.

Please don't include any personal information such as legal names or email addresses. Maximum 100 characters, markdown supported. This note will be visible to only you.
Report abuse

Contact GitHub support about this user’s behavior. Learn more about reporting abuse.

Report abuse
lilweege/README.md

Hi, I'm Luigi

linkedin | website | email


Full time:

  • 🏫 SWE at McMaster University
  • 💼 Intern at Intel

Free time:

  • 👨‍💻 Von Neumann Enjoyer
  • 🐧 FOSS Enthusiast

Interesting snippets

Stack UB (-O0)

void A() { int x = 42; }
int B() { int x; return x; }

int main() {
    A();
    printf("%d\n", B());
}

Register UB (-O0)

void A(int x) {}
int B() { int x; return x; }

int main() {
    A(42);
    printf("%d\n", B());
}

Computed GOTO

void jump(void* p) { goto* p; }

int main() {
    void* p = &&foo;
    jump(p);

    printf("A\n");
foo:
    printf("B\n");
}

Faster switch

#include <cstdio>
#include <cstdint>

enum Instruction : uint8_t { Done, A, B, C };

void execute(const Instruction* list) {
    constexpr void* labels[] = {
        [Instruction::Done] = &&Done,
        [Instruction::A] = &&A,
        [Instruction::B] = &&B,
        [Instruction::C] = &&C,
    };
    const auto next = [&] { return labels[*list++]; };
    goto* next();

Done: return;
A:  printf("A\n"); goto* next();
B:  printf("B\n"); goto* next();
C:  printf("C\n"); goto* next();
}

int main() {
    constexpr Instruction list[] = {C, B, A, Done};
    execute(list);
}

Duff's device

#include <stddef.h>
#include <stdio.h>

void better_memcpy(void* restrict dest, void* restrict src, size_t n) {
    unsigned char* to = dest;
    unsigned char* from = src;
    int remain = (n + 7) / 8;
    switch (n % 8) {
        case 0: do { *to++ = *from++;
        case 7:      *to++ = *from++;
        case 6:      *to++ = *from++;
        case 5:      *to++ = *from++;
        case 4:      *to++ = *from++;
        case 3:      *to++ = *from++;
        case 2:      *to++ = *from++;
        case 1:      *to++ = *from++;
        } while (--remain > 0);
    }
}

int main() {
    int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
    int b[sizeof(a) / sizeof(a[0])];
    better_memcpy(b, a, sizeof(b));
    printf("%d\n", b[8]);
}

Hello, World!

#define print(...) puts(#__VA_ARGS__)
int main() { print(Hello, World!); }
int main() {
    __uint128_t x = ((__uint128_t)0x290E4D9CB7 << 64) | 0xD740B37ECD996400;
    while (x >>= 7) putchar(x & 0x7F);
}

Pinned

  1. os os Public

    A toy 32-bit operating system for x86

    C 4

  2. cpu cpu Public

    A 32-bit RISC-V emulator

    C++

  3. ymalloc ymalloc Public

    Yet another implementation of malloc and free

    C

  4. cppkalman cppkalman Public

    A port of pykalman to C++

    C++

  5. editor editor Public

    A text editor

    C

  6. trash-lang trash-lang Public

    A toy programming language written in C++

    C++ 2