Skip to content

Instantly share code, notes, and snippets.

@inspirit
inspirit / lfstack_dcas.hpp
Created June 20, 2015 22:35
Lock-free Stack using Double width CAS and Packed/Tagged pointer (based on Go)
#pragma once
#include <atomic>
#include <type_traits>
template<typename T, size_t Capacity>
struct lfstack {
struct node_t final {
T value;
node_t *next;
@inspirit
inspirit / gist:a14b0a1f227a2d288b46
Last active August 29, 2015 14:23
lock-free 8bit mask
// we only allow no more than 8 worker in pool
std::atomic_uint_fast8_t idle_mask = {0};
// this function is called by each thread when it is about to sleep
void register_idle(const size_t thread_id)
{
idle_mask.fetch_or(1u << thread_id, std::memory_order_release);
}
// this function can be called from anywhere at anytime
@inspirit
inspirit / LinkedBlockingQueue.h
Created June 12, 2015 23:04
Objective-C LinkedBlockingQueue
#import <Foundation/Foundation.h>
@interface LinkedBlockingQueue : NSObject
{
// NOP
}
@property(nonatomic, readonly) NSUInteger count;
@property(nonatomic, readonly) NSUInteger freeCount;
@inspirit
inspirit / Convolution.hpp
Created October 2, 2013 08:26
Separable Convolution and Gaussian Blur
#pragma once
#ifndef CONVOLUTION_H_INCLUDED
#define CONVOLUTION_H_INCLUDED
/**
* Separable Convolution routines with SSE and NEON intrinsics
*
* this implementation is based on OpenCV Filter Class
* with template optimizations and SIMD intrinsic
*
@inspirit
inspirit / cholesky
Created October 24, 2012 13:15
Cholesky solver using simple math only
function cholesky_solve(A:Vector.<Number>, size:int, b:Vector.<Number>):Boolean
{
var col:int, row:int, col2:int;
var val:Number;
for (col = 0; col < size; ++col)
{
var inv_diag:Number = 1;
var cs:int = (col * size);
var rs:int = cs;
@inspirit
inspirit / box_blur_noscale.c
Created October 11, 2012 08:04
Const time box blur (no scale version)
void box_blur_noscale(const uint8_t* input, int32_t* out, const int w, const int h, int hwin)
{
const int win = 2*hwin+1;
int *_buf = (int*)alloca((w*win+w) * sizeof(int));
int* sums = _buf + w*win;
int* next_row = _buf;
int* oldest_row = _buf;
int i, j;
memset(sums, 0, w*sizeof(int));
@inspirit
inspirit / RNDMarsaglia.as
Created June 24, 2011 18:50
A 32-bit random number generator by George Marsaglia
var __x:uint = 123456789;
var __y:uint = 362436069;
var __z:uint = 21288629;
var __w:uint = 14921776;
var __c:uint = 0;
function rnd():uint
{
__x = __x + 545925293;
__y = __y ^ (__y<<13);
@inspirit
inspirit / LUSolveMxN.as
Created June 21, 2011 20:39
Solve MxN system using LU Decomposition
/**
* LU decomposition/solve is used for NxN (square) matrices
* but using additional computations we can use LU decomposition
* to solve MxN system (since LU routines less computation expensive)
*/
// u can find LU and MatrixMath Classes at my Google repo
var lu:LU = new LU();
@inspirit
inspirit / LList.as
Created June 19, 2011 16:56
Fast & Simple Dual Linked List
package ru.inspirit.asfeat.struct
{
/**
* Simple but fast Dual Linked List approach
* Core implementation idea grabbed from Linux Kernel C source
*
* @author Eugene Zatepyakin
*/
public final class LList
{
@inspirit
inspirit / HomographyDecomposition.as
Created December 14, 2010 19:57
Decompose Homography into Rotation matrix & Translation vector
var intrinsic:Vector.<Number> = new Vector.<Number>(9, true);
var intrinsicInverse:Vector.<Number> = new Vector.<Number>(9, true);
var R:Vector.<Number> = new Vector.<Number>( 9, true );
var t:Vector.<Number> = new Vector.<Number>( 3, true );
// SVD routine
var svd:SVD = new SVD();
// input homography[9] - 3x3 Matrix