Skip to content

Instantly share code, notes, and snippets.

View hamsham's full-sized avatar

Miles Lacey hamsham

View GitHub Profile
@hamsham
hamsham / randMulCarry.cpp
Created November 15, 2015 08:45
PRNG using a "random multiply-with-carry" algorithm.
/**
* Testing the PRNG "Multiply with Carry" by George Marsaglia:
*
* http://en.wikipedia.org/wiki/Multiply-with-carry
*
* g++ -std=c++11 -Wall -Werror -Wextra -pedantic -pedantic-errors randMulCarry.cpp -o randMulCarry
*/
#include <algorithm>
@hamsham
hamsham / pow2.cpp
Created November 15, 2015 08:44
Power-of-2 functions (get the nearest, next, or previous power of 2).
/**
* Test of getting the next/previous powers of 2
*
* g++ -std=c++11 -Wall -Werror -Wextra -pedantic -pedantic-errors pow2.cpp -o pow2
*/
#include <iostream>
#include <sstream>
@hamsham
hamsham / fixed.cpp
Created November 15, 2015 08:41
Fixed-point number class with float->int conversions.
// testing fixed point numbers
// g++ -std=c++11 -W -Wall -Werror -Wextra fixed.cpp -o fixed
#include <iostream>
#include <cstdint>
#include <limits>
#include <typeinfo>
@hamsham
hamsham / fastlog.cpp
Created November 15, 2015 08:36
Fast approximation of logarithms.
/* testing a log function */
// g++ -std=c++11 -pedantic -Wall -Werror -Wextra -W fastlog.cpp -o fastlog
#include <cmath>
#include <iomanip>
#include <limits>
#include <iostream>
@hamsham
hamsham / factorial.cpp
Created November 15, 2015 08:35
Comparison of factorials using compile-time calculation vs runtime calculation.
#include <iostream>
// g++ -std=c++11 -pedantic -Wall -Werror -Wextra factorial.cpp -o factorial
template <typename type>
constexpr type constFactorial( type n ) {
return (n > 1) ? n * constFactorial( n - 1 ) : n;
}
@hamsham
hamsham / dotprod.c
Created November 15, 2015 08:33
Test to help visualize the result of a vector dot product.
/**
* Testing the result of a dot product
*
* gcc -std=c99 -Wall -Werror -Wextra -pedantic -pedantic-errors dotprod.c -o dotprod
*/
#include <stdio.h>
typedef struct vector
@hamsham
hamsham / bn_mul.cpp
Created November 15, 2015 08:32
Testing Big-Num multiplication (naive method)
/**
* Bignum multiplication
*
* g++ -std=c++11 -Wall -Wextra -Werror -pedantic-errors bn_mul.cpp -o bn_mul
*
* usage: ./bn_mul 123456789 987654321
*/
#include <cstring>
@hamsham
hamsham / bn_add.cpp
Created November 15, 2015 08:32
Testing the addition and subtraction of Big Numbers
/*
* Bignum Addition Test
* g++ -std=c++11 -Wall -Werror -Wextra -pedantic -pedantic-errors bignum_add.cpp -o bignum_add
*/
#include <utility> // std::move
#include <string> // std::string
#include <iostream> // std::cout
#include <cstdint> // uint8_t, uint16_t, UINT8_MAX
@hamsham
hamsham / avgnum.cpp
Created November 15, 2015 08:28
Compile-time test to get the average of an arbitrary set of numbers.
/**
* Simple test to get the average of an arbitrary set of numbers
*
* g++ -std=c++11 -Wall -Werror -Wextra -pedantic -pedantic-errors avgnum.cpp -o avgnum
*/
#include <iostream>
#define TEST_NUM 42
@hamsham
hamsham / aspect.cpp
Created November 15, 2015 08:27
Test to automatically get the aspect ratio of a display
// Test to automatically get the aspect ratio of a display
// g++ -std=c++11 -pedantic-errors -Wall -Wextra -Werror aspect.cpp -o aspect
#include <cmath>
#include <iostream>
template <typename int_t>
int_t gcd(int_t a, int_t b) {
while (b) {