Skip to content

Instantly share code, notes, and snippets.

View amosr's full-sized avatar

Amos Robinson amosr

View GitHub Profile
@amosr
amosr / run.sh
Created January 30, 2016 02:30
UInt can't hold (2^63)+1
amos@localhost option-test $ swiftc uint.swift && ./main
2^62 = 4611686018427387904
2^63 - 1 = 9223372036854775807
Illegal instruction
# Running with -Ounchecked gives wrong result.. (last two numbers are same)
amos@localhost option-test $ swiftc tx1-uint.swift -Ounchecked && ./main
2^62 = 4611686018427387904
2^63 - 1 = 9223372036854775807
2^63 = 9223372036854775807
@amosr
amosr / repl.swift
Created January 28, 2016 07:24
cast :: UnsafeMutablePointer<T> -> UnsafeMutablePointer<U> in Swift?
13> let y : UnsafeMutablePointer<UInt8> = unsafeDowncast(malloc(50))
repl.swift:13:39: error: generic parameter 'T' could not be inferred
let y : UnsafeMutablePointer<UInt8> = unsafeDowncast(malloc(50))
^
Swift.unsafeDowncast:12:13: note: in call to function 'unsafeDowncast'
public func unsafeDowncast<T : AnyObject>(x: AnyObject) -> T
^
13> let y : UnsafeMutablePointer<UInt8> = unsafeDowncast<UnsafeMutablePointer<UInt8>>(malloc(50))
repl.swift:13:39: error: cannot explicitly specialize a generic function
@amosr
amosr / int.c
Last active December 8, 2015 10:45
SSE int parsing
#include <x86intrin.h>
#include <stdint.h>
#include <stdio.h>
static const uint32_t powers_of_ten_multipliers[]
= { 10000000, 1000000, 100000, 10000
, 1000 , 100 , 10 , 1
, 0 , 0 , 0 , 0
, 0 , 0 , 0 , 0
@amosr
amosr / merge.c
Last active December 2, 2015 03:44
merging EAVTs
#include <fcntl.h>
#include <math.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/stat.h>
@amosr
amosr / split.sh
Last active December 1, 2015 00:52
## $FILES is names of all files to be split
## splits files into two
# find largest file
largest=`ls $FILES -l | sort -k5 -nr | head -n1 | awk '{ print $9 }'`
# count how many lines in it
lines=`wc -l $largest`
@amosr
amosr / postgres.md
Last active November 26, 2015 03:37
Playing with postgres

Comparing the Icicle against Postgres

Using the ASX data. SQL to create table and index is:

create table asx (company char(3), date date, open double precision, high double precision, low double precision, close double precision, volume double precision, adjclose double precision);
#include <stdio.h>
#include <string.h>
#include <time.h>
typedef long long int uint64_t;
int cmp8_mask (const char *as, const char* bs, uint64_t len)
{
uint64_t rem = len;
while (rem > 8) {
/*
attempt at writing a faster memcmp
*/
static iint_t INLINE memcmp8 (const char *as, const char* bs, iint_t len)
{
uint64_t *a = (uint64_t*)as;
uint64_t *b = (uint64_t*)bs;
while (len > 8) {
if (*a != *b) {
return 1;
@amosr
amosr / read.c
Created November 18, 2015 02:27
12 instruction case-insensitive string equality (for reading booleans)
int read_bool (char *p)
{
static const uint64_t true_mask = 0x00000000ffffffff;
static const uint64_t true_bits = 0x0000000065757274;
static const uint64_t false_mask = 0x000000ffffffffff;
static const uint64_t false_bits = 0x00000065736c6166;
static const uint64_t to_lower = 0x2020202020202020;
uint64_t next8 = *(uint64_t *)p | to_lower;
@amosr
amosr / gist:8989c5c65e684bf76c2d
Created November 13, 2015 22:13
beating grep
$ ls -lah asx.psv
-rw-r--r-- 1 amos staff 59G 13 Nov 18:07 asx.psv
$ time grep -v EntryError asx.psv > /dev/null
real 14m33.885s
user 14m10.081s
sys 0m22.390s