Skip to content

Instantly share code, notes, and snippets.

View Sam-Belliveau's full-sized avatar

Sam Belliveau Sam-Belliveau

  • Cornell University
  • Ithaca, NY
View GitHub Profile
@Sam-Belliveau
Sam-Belliveau / Timer.c
Last active October 21, 2018 15:44
This is a timer that times in clock cycles. It uses __rdtscp(). It only workes on unix. I will not port it to windows as i dont think an application like this would make much use there. You can port it easily.
/*** PRINTING STUFF ***/
#include <stdio.h> /* printf() */
#include <stdlib.h> /* system() */
#include <stdint.h> /* fixed width ints */
/*** UNIX STUFF ***/
#include <x86intrin.h> /* __rdtscp() */
#include <unistd.h> /* sleep() */
#define SHOW_CPUID 0 /* True/False */
@Sam-Belliveau
Sam-Belliveau / BFCompiler.c
Last active November 24, 2018 19:38
Here is a brainfuck compiler I made using C. It compiles into C, then compiles that C into assembly using gcc. You can change between a letters and ints. You can even change it to keep the C code.
#include <stdio.h>
#include <stdlib.h>
/* You can change between */
/* Letters and Numbers */
#define USE_LETTERS 1
#if (USE_LETTERS == 1)
#define CELL_TYPE "char"
#define PRINT_COMMAND "putchar(*p);"
#include <stdio.h>
#include <stdlib.h>
int main(){char *m=(char*)calloc(0x8000,sizeof(char));char *p=m;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;while(*p){--*p;++p;++*p;++*p;++p;++p;++p;++*p;++*p;++*p;++*p;++*p;++p;++*p;++*p;++p;++*p;--p;--p;--p;--p;--p;--p;}++p;++p;++p;++p;++p;++*p;++*p;++*p;++*p;++*p;++*p;++p;--*p;--*p;--*p;++p;++p;++p;++p;++p;++p;++p;++p;++p;++p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;++*p;while(*p){while(*p){++p;++p;++p;++p;++p;++p;++p;++p;++p;}++*p;while(*p){--p;--p;--p;--p;--p;--p;--p;--p;--p;}++p;++p;++p;++p;++p;++p;++p;++p;++p;--*p;}++*p;while(*p){++p;++p;++p;++p;++p;++p;++p;++p;while(*p){--*p;}++p;}--p;--p;--p;--p;--p;--p;--p;--p;--p;while(*p){--p;--p;--p;--p;--p;--p;--p;--p;--p;}++p;++p;++p;++p;++p;++p;++p;++p;while(*p){--*p;}++*p;--p;--p;--p;--p;--p;--p;--p;++*p;++*p;++*p;++*p;++*p;while(*p){--*p;while(*p){--*p;++p;++p;++p;++p;++p;++p;++p;++p;++p;++*p;--p;--p;--p;--p;--p;--p;--p;--p;--p;}++p;++p;++p;++p;++p;++p;++p;++p;++p;}++p;+
import java.lang.String;
public class ColorTerm {
private static final String ESC = "\033";
private int width = 16, height = 9;
private boolean open = true;
public static final int Black = 40;
public static final int Red = 41;
@Sam-Belliveau
Sam-Belliveau / XORShift.hpp
Last active November 30, 2018 19:50
Simple variable bit XOR Shift using std::bitset as a state. Works like anyother class in <random>, and according to an entropy test, can out performe std::mt19937
#ifndef XOR_RAND_HPP
#define XOR_RAND_HPP
#include <initializer_list> // std::initializer_list
#include <cstdint> // Fixed Width Int
#include <limits> // std::numeric_limits
#include <bitset> // std::bitset
namespace XORShift // Definitions
{
@Sam-Belliveau
Sam-Belliveau / FixedPoint.hpp
Last active April 29, 2019 02:41
Simple C++ Fixed Point Number Library
#ifndef FIXED_POINT_NUMBERS_HPP
#define FIXED_POINT_NUMBERS_HPP 1
namespace Fixed
{
namespace FixedMath
{
template<class T>
constexpr T GCD(T a, T b) { return (b == 0) ? a : GCD(b, a % b); }
}
@Sam-Belliveau
Sam-Belliveau / Rot13.c
Last active November 24, 2018 19:22
Here is a really simple example for ROT13 cipher. Used for demonstation purposes only. Made to be as readable as possible.
#include <stdio.h> /* putchar, printf */
void printRot13(const char* str)
{
/* loop through string. Strings end with a \0 */
for(; *str != '\0'; ++str)
{
/* If letter is in the lower 13 letters, add 13 */
if (*str >= 'A' && *str <= 'M') { putchar(*str + 13); }
else if (*str >= 'a' && *str <= 'm') { putchar(*str + 13); }
/*
Copyright 2018, Sam R. Belliveau
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
@Sam-Belliveau
Sam-Belliveau / RangeArray.hpp
Last active January 3, 2019 19:42
Portable class that lets you handle every type of array
#ifndef SAMS_ARRAY_CONTAINER_CLASS_HPP
#define SAMS_ARRAY_CONTAINER_CLASS_HPP 1
#include <cstdint> // std::size_t, std::ptrdiff_t
#include <algorithm> // std::sort, std::swap, etc...
#include <iterator> // std::reverse_iterator
#include <stdexcept> // std::out_of_range
/*
Array is not ment to be used to allocate/store arrays.
#ifndef SAMS_BLUM_BLUM_SHUB_HPP
#define SAMS_BLUM_BLUM_SHUB_HPP 1
#include <cstdint> // fixed width integers
#include <limits> // std::numeric_limits
namespace BlumBlumShub
{
using internal_t = std::uint32_t;
using cast_int_t = std::uint64_t; // Needs to be twice internal_t size