Code Monkey home page Code Monkey logo

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);
}

Luigi Quattrociocchi's Projects

a-bit-off icon a-bit-off

Java GUI game for grade 12 computer studies class cpt

aoc-2021 icon aoc-2021

My solutions to Advent of Code 2021 written in C.

circuit-diagram-app icon circuit-diagram-app

Generate parallel or series circuit diagrams with variable resistors and visualizer.

cpu icon cpu

A 32-bit RISC-V emulator

fluid-simulation-gl icon fluid-simulation-gl

an opengl visualization of 3d realtime fluid dynamics, compiled to javascript for the web browser using emscripten

ics4u icon ics4u

grade 12 computer science class assignments

littlec icon littlec

LittleC Interpreter from Herbert Schildt's C: The Complete Reference (4th Ed.)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.