Skip to content

Instantly share code, notes, and snippets.

#include <iostream>
#include <vector>
class Tracker {
public:
void record(int val, int index);
int get_last_index(int val);
std::pair<int, int> get_stats() const;
private:
@ramntry
ramntry / main.cpp
Created May 5, 2019 05:55
Very pretty triangle (OpenGL)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdocumentation"
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#pragma clang diagnostic pop
#include <iostream>
@ramntry
ramntry / float.cpp
Last active August 9, 2018 05:50
IEEE-754 single precision software implementation
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <bitset>
#include <limits>
#include <cassert>
using namespace std;
struct single {
double foo(double *a, int n) {
double x = 0.0;
double y = 0.0;
for (int i = 0; i < n; i += 2) {
x += a[i];
y += a[i + 1];
}
return x * y;
}
  // |  Expression   |     ConstantRange      |       KnownBits       |
  // |---------------|------------------------|-----------------------|
  // | i8 4 * X      | [L: 0, U: 253)         | XXXX XX00             |
  // |               |   => Min: 0, Max: 252  |   => Min: 0, Max: 252 |
  // |               |                        |                       |
  // | i8 4 * X + 5  | [L: 5, U: 2) (wrapped) | YYYY YY01             |

// | (101) | => Min: 0, Max: 255 | => Min: 1, Max: 253 |

#import <Foundation/Foundation.h>
#include <stdio.h>
@interface NSString (MyPrintable)
- (const char *)toCString;
@end
@implementation NSString (MyPrintable)
// NSString is a library provided class that doesn't have
// a toCString method. We're dynamically extending the class
#include <algorithm>
#include <iostream>
#include <iterator>
#include <list>
template <typename T>
class Counter : public std::iterator<std::output_iterator_tag, T> {
size_t &Value;
public:
%dag = type { { { i8, { i8 } }, { { i8, { i8 } }, { i8 } } }, { { i8, { i8 } }, { i8 } } }
define void @test_const(%dag* %dst) {
entry:
%updated = insertvalue
; Check that we're visiting constants with shared parts
; (deduplicated via LLVMContext, forming a proper DAG) correctly:
%dag {
{ { i8, { i8 } }, { { i8, { i8 } }, { i8 } } } {
{ i8, { i8 } } {
define i64 @test_phi_loop(i32 %n) {
entry:
br label %loop
loop:
%counter = phi i32 [ %n, %entry ], [ %counter.dec, %loop ]
%elem = phi { i64, i64 } [ { i64 0, i64 1 }, %entry ], [ %updated, %loop ]
%prev = extractvalue { i64, i64 } %elem, 0
%curr = extractvalue { i64, i64 } %elem, 1
%next = add i64 %prev, %curr
define void @test_phi_diamond({ i8, i16, i32 }* %a.ptr, { i8, i16, i32 }* %b.ptr, i1 %selector, { i8, i16, i32 }* %dst) {
entry:
br i1 %selector, label %store.a, label %store.b
store.a:
%a = load { i8, i16, i32 }, { i8, i16, i32 }* %a.ptr
br label %join
store.b:
%b = load { i8, i16, i32 }, { i8, i16, i32 }* %b.ptr