Skip to content

Instantly share code, notes, and snippets.

// Unigine C++ School 2
// Homework 1
//
// by griff
//
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
@pankkor
pankkor / EventTest.swift
Last active October 30, 2018 17:38
Quick sketch of 2 Event implementations
enum EventTest {
// + brain dead simple
// - need to manually unsubscribe
class Event<Args> {
typealias CallbackType = (Args) -> Void
private var id: UInt64 = 0
private var callbacks = [UInt64: CallbackType]()
func subscribe(_ callback: @escaping CallbackType) -> UInt64 {
@pankkor
pankkor / str_reverse.c
Last active August 21, 2022 21:14
Reverse char string in place using simd
// Reverse char string in place
//
// Build:
// clang -O2 -DTEST -mavx2 -Wall -Wpedantic -Wextra str_reverse.c -o str_reverse
// or
// clang -O2 -DTEST -mssse3 -Wall -Wpedantic -Wextra str_reverse.c -o str_reverse
//
#include <stdint.h> // uint64_t uint32_t
#include <stdlib.h> // abort()
#include <sys/sysctl.h> // sysctlbyname
i64 cache_line_size() {
i64 ret = 0;
u64 size = sizeof ret;
if (sysctlbyname("hw.cachelinesize", &ret, &size, NULL, 0) == -1) {
return -1;
}
return ret;
}
#ifndef __aarch64__
#error Arches other than arm64 are not supported
#endif // #ifndef __aarch64__
#define FORCE_INLINE inline __attribute__((always_inline))
typedef unsigned long u64;
static FORCE_INLINE u64 rdtsc(void) {
u64 val;
// disable optimisations
#ifdef _MSC_VER
#pragma optimize("", off)
inline void bench_opt_off(const void*) {}
#pragma optimize("", on)
#define ESCAPE(p) bench_opt_off(p)
#define CLOBBER(p) bench_opt_off(p)
@pankkor
pankkor / bench_array_sum_aarch64.c
Last active February 26, 2023 17:52
Benchmark u32 array sum(); CFLAGS = -std=c11 -Wall -Wextra -Wpedantic -O3 -mcpu=apple-m1
#ifndef __aarch64__
#error Architectures other than arm64 are not supported
#endif // #ifndef __aarch64__
#include <arm_neon.h>
#include <stdio.h> // printf
typedef int i32;
typedef unsigned int u32;
typedef long i64;
#define ISB(option) __asm__ volatile ("isb " #option : : : "memory")
#define DSB(option) __asm__ volatile ("dsb " #option : : : "memory")
#define DMB(option) __asm__ volatile ("dmb " #option : : : "memory")
# Chrome: disable swipe navigation
defaults write com.google.Chrome AppleEnableSwipeNavigateWithScrolls -bool false
# Finder: show full path in the title
defaults write com.apple.finder _FXShowPosixPathInTitle -bool YES
# Xcode: show build times
defaults write com.apple.dt.Xcode ShowBuildOperationDuration YES
@pankkor
pankkor / sv.c
Created June 6, 2023 08:31
C string view + basename()
// string view
struct sv {
const char *begin;
const char *end;
};
const char *s_dot = ".";
// Custom basename returning string view
struct sv basename(const char *path) {