Skip to content

Instantly share code, notes, and snippets.

View deplinenoise's full-sized avatar

Andreas Fredriksson deplinenoise

View GitHub Profile
@deplinenoise
deplinenoise / double-round-to-float
Created July 9, 2019 06:37
Round double to float hardware-matching precision for default SSE rounding mode
constexpr uint64_t mask(int i)
{
return (1ull << i) - 1;
}
float Round1(double tmp)
{
uint64_t i;
memcpy(&i, &tmp, sizeof i);
@deplinenoise
deplinenoise / build-gcc-toolchain.sh
Created June 5, 2016 19:15
Build m68k-unknown-elf toolchain into subdir of PWD on Mac, relies on brew for GCC dependencies
#! /usr/bin/env bash
echo Building GCC m68k toolchain...
mkdir -p sources
mkdir -p build
CORES=8
TARGET=m68k-unknown-elf
PREFIX=$PWD/m68k-unknown-elf
__m128 Uint32_Float_Round(__m128i in)
{
// Generate mask when input elements are > 2^31 - 1
__m128i mask = _mm_srai_epi32(in, 31);
// Version of the input shifted down one bit logically
__m128i t = _mm_srli_epi32(in, 1);
// Add lowest bit of each word to round.
__m128i in1 = _mm_or_epi32(t, _mm_and_si128(in, _mm_set1_epi32(1)));
@deplinenoise
deplinenoise / Asm
Created July 30, 2014 21:36 — forked from emoon/Asm
; Pure Lisp based assembler
(def my-fun (input1 :in d1
input2 :in d2
output :out d0)
(move.l input1 output)
(add.l input2 output))
----------------------------------------------------------------------------------------
; We can also have loop constructions by doing a macro (no need for it to be 'built-in')
// Type-safe varargs with C++11 variadic templates.
//
// Andreas Fredriksson <deplinenoise at gmail dott com>
// minor changes from:
// Thomas Hume <thomas dot hume at labri dot fr>
//
// This code is in the public domain.
#include <stdio.h>
#include <stdarg.h>
@deplinenoise
deplinenoise / typesafe_varargs
Last active July 13, 2022 00:29
You can pass along an array of info with varargs using C++ variadic templates. It generates really tight code; the only remaining overhead at runtime is the static arrays of type information (they end up in the `.rodata` segment). In this example I'm passing in an integer describing each arg, but you could just as well pass in function pointers …
// Type-safe varargs with C++11 variadic templates.
//
// Andreas Fredriksson <deplinenoise at gmail dott com>
//
// This code is in the public domain.
#include <stdio.h>
#include <stdarg.h>
; clang++ --version
; Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn)
; Target: x86_64-apple-darwin12.2.0
; Thread model: posix
Djb2HashNoCase(char const*):
.cfi_startproc
## BB#0:
pushq %rbp
Ltmp14:
uint32_t Djb2HashNoCase(const char *str_)
{
const uint8_t *str = (const uint8_t *) str_;
uint32_t hash = 5381;
int c;
while (0 != (c = *str++))
{
#if 1
// Branch free case folding for ASCII
@deplinenoise
deplinenoise / sum-bootblock.rb
Created June 5, 2011 08:15
Ruby bootblock checksummer
#! /usr/bin/env ruby
def compute_chksum(data)
sum = 0
(0...256).each do |x|
i = 4 * x
val = data[i,i+4].unpack('N')[0]
sum = sum + val
if sum > 0xffffffff
sum = (sum + 1) & 0xffffffff
@deplinenoise
deplinenoise / sum-bootblock.py
Created June 5, 2011 08:15
Python bootblock checksummer
#! /usr/bin/env python
import sys
import struct
def splice_checksum(block, chksum):
return block[0:4] + struct.pack('>I', chksum) + block[8:]
def makeit(fn, ofn):
f = open(fn, 'rb')