Skip to content

Instantly share code, notes, and snippets.

View rcc's full-sized avatar

Robert Curtis rcc

  • Napa, CA
  • 14:40 (UTC -07:00)
View GitHub Profile
@rcc
rcc / callback.cpp
Last active August 29, 2015 14:07
C++ Templated Bounce
#include <stdio.h>
class Foo {
public:
Foo(int val) : val_(val) { }
void operator()(int v) const
{
printf("%p : val = %d\n", this, val_ + v);
}
@rcc
rcc / bitfield.h
Created July 15, 2011 23:27
bitfields
#ifndef I__BITFIELD_H__
#define I__BITFIELD_H__
/* Bitfield Macros
*
* A bitfield is any range of bits in a word. A bitfield definition
* needs to specify the SIZE and SHIFT in the following format:
* #define BITFIELD_NAME SIZE:SHIFT
* Examples (X is don't care, F is the field):
* Register: XXXXXFFX = Bitfield 2:1
@rcc
rcc / lfsr.c
Created April 27, 2010 16:58
C LFSR Library
/*
* lfsr.c - lfsr library
*
* Copyright (C) 2009 Robert C. Curtis
*
* lfsr.c is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
@rcc
rcc / bits_set
Last active September 4, 2015 20:44
Log(N) method for calculating the number of bits set in a word
/* Log(N) method for calculating the number of bits set in a word */
uint8_t bits_set(uint32_t word)
{
word = (word & 0x55555555) + ((word >> 1) & 0x55555555);
word = (word & 0x33333333) + ((word >> 2) & 0x33333333);
word = (word & 0x0F0F0F0F) + ((word >> 4) & 0x0F0F0F0F);
word = (word & 0x00FF00FF) + ((word >> 8) & 0x00FF00FF);
word = (word & 0x0000FFFF) + ((word >> 16) & 0x0000FFFF);
return (uint8_t)word;