Skip to content

Instantly share code, notes, and snippets.

@detomon
detomon / Makefile
Created November 19, 2019 08:51
Makefile for compiling Elm modules
MODULES = src/Main.elm # add more if needed...
TARGET_JS = elm.js
TARGET_JS_MIN = elm.min.js
ELM = elm
ELM_FLAGS = --optimize
UGLIFY = uglifyjs
UGLIFY_ELM_FLAGS = 'pure_funcs="F2,F3,F4,F5,F6,F7,F8,F9,A2,A3,A4,A5,A6,A7,A8,A9",pure_getters,keep_fargs=false,unsafe_comps,unsafe'
main: \
$(TARGET_JS_MIN)
#define VALUE_REF(S) (&((struct { typeof(S) s; }) { (S) }).s)
typedef struct {
int a;
int b;
} value;
int* i = VALUE_REF(12);
float* f = VALUE_REF(45.6f);
value* v = VALUE_REF(((value) { .a = 12, .b = 13 }));
@detomon
detomon / swift-enum.c
Created September 9, 2017 17:13
Emulate Swift enum types in C
#include <stdio.h>
/**
* Emulate Swift enum type.
*/
typedef struct {
// define enum types
// (enums inside structs are not really private)
enum {
MyEnumInt,

Keybase proof

I hereby claim:

  • I am detomon on github.
  • I am detomon (https://keybase.io/detomon) on keybase.
  • I have a public key ASDSgRQCCkC9JCQ1gus0l45K4_e73TPja93u5Xypmsq6Uwo

To claim this, I am signing this object:

@detomon
detomon / run-in-shell.c
Last active July 23, 2017 14:40
Compiling and running a C program in the shell by executing itself as shell script
#if 0
clang -Wall -O2 -o /tmp/`basename $0` $0 && /tmp/`basename $0` $@; exit
#endif
//
// sh run-in-shell.c `whoami`
//
#include <stdio.h>
@detomon
detomon / endianess.cpp
Created May 18, 2016 09:48
Check the system endianness at compile time with C++
#include <cstdint>
#include <iostream>
struct Endian {
constexpr static bool is_big() {
union {
uint32_t i;
uint8_t c[4];
} check = {
.i = 0x01000000
@detomon
detomon / intrinsic.md
Last active March 6, 2024 17:57
SSE Intrinsic Cheat Sheet (SSE3)

SSE Intrinsic Cheat Sheet (SSE3)

Load and Store

__m128 _mm_load_ps (float * a)
{
@detomon
detomon / bitfield.h
Last active August 29, 2015 14:19
A collection of macro functions to manipulate bitfields of arbitrary size
/**
* Bitfield function collection
*
* Can be used to define variables which require more bits than the biggest data
* type can hold. Bits are organized within multiple `slots` which is an array
* of the biggest integer type available.
*
* However, multiple bits cannot overlap two slots:
* bit_get_field (mybits, 62, 8) will only contain bits from the first slot
*
@detomon
detomon / strnatcmp.c
Last active June 22, 2017 06:44
Compare strings using natural ordering by interpreting continuous digits as integers
#include <stdio.h>
#include <stdlib.h>
#include <Block.h>
/**
* Compare strings using natural ordering
*
* Interprets continuous digits as integers, e.g, the string "year 2015" will be
* greater than "year 308", although "3" has a higher character value than "2".
* [Trailing and pending whitespace is ignored.] Whitespace between tokens is
@detomon
detomon / CGAffineTransform.swift
Last active January 10, 2024 15:26
Convenient operator overloadings for CGPoint, CGSize and CGRect
import Foundation
/**
* CGAffineTransform
*
* var a = CGAffineTransformMakeRotation(45.0 * M_PI / 180.0)
* var b = CGPointMake(30.0, 43.3)
*/
/**