Skip to content

Instantly share code, notes, and snippets.

View engelmarkus's full-sized avatar

Markus Engel engelmarkus

View GitHub Profile
@engelmarkus
engelmarkus / AsmTest.java
Created March 15, 2016 00:11
Calling an assembly function from Java
public class AsmTest {
public static native int addThem(int a, int b);
static {
// Looks for libaddThem.so or addThem.dll, depending on the OS
System.loadLibrary("addThem");
}
public static void main(String[] args) {
int result = addThem(4, 5);
@engelmarkus
engelmarkus / fold.cpp
Created March 21, 2016 01:48
Test of C++ fold expressions.
#include <iostream>
using namespace std;
constexpr auto all(auto&&... args) {
return (args && ...);
}
int main() {
cout << boolalpha;
@engelmarkus
engelmarkus / coroutine.cpp
Created March 21, 2016 03:02
Using a boost coroutine for lazily generating an endless sequence of numbers.
#include <algorithm>
#include <iostream>
#include <vector>
#include <boost/asio/coroutine.hpp>
#include <boost/asio/yield.hpp>
using namespace std;
using namespace boost::asio;
@engelmarkus
engelmarkus / to_bin.cpp
Created April 20, 2016 23:21
Convert a compile-time constant to a char-array containing the binary representation.
#include <iostream>
template <unsigned long long N, char... C>
constexpr auto to_bin = to_bin<(N >> 1), '0' + (N & 1), C...>;
template <char... C>
constexpr char to_bin<0, C...>[] = { C..., '\0' };
template <unsigned long long N, size_t L, char... C>
@engelmarkus
engelmarkus / ring_buffer.cpp
Created April 21, 2016 00:04
Example implementation for a simple ring buffer.
#include <array>
#include <cstdint>
#include <iostream>
#include <stdexcept>
using namespace std;
template <unsigned long long n>
constexpr auto count_set_bits = (n & 1) + count_set_bits<(n >> 1)>;
@engelmarkus
engelmarkus / times.cs
Created June 30, 2016 12:21
C# - adding some rubyness: times extension method for ints
using System;
using System.Linq;
namespace Times {
static class Extensions {
public static void times(this int t, Action f) {
for (int i = 0; i < t; ++i) {
f();
}
}
@engelmarkus
engelmarkus / creator.cpp
Last active July 13, 2016 21:23
k-way merge sort - sorting data using a fixed amount of memory
#include <algorithm>
#include <array>
#include <fstream>
#include <numeric>
#include <random>
using namespace std;
// Step 1 - Create a file with 256 MiB of data
array<uint64_t, 256 * 1024 * 1024 / sizeof(uint64_t)> data;
@engelmarkus
engelmarkus / order_deps.rb
Last active December 21, 2016 18:58
Script for determining the correct order of static libraries for GNU ld
#!/usr/bin/env ruby
# When passing static libraries to GNU ld, one has to make sure the order is correct or undefined references will occur.
# This is due to the linker reading each static library just once.
# With "--start-group" and "--end-group" this behaviour can be overridden but using these options is discouraged.
# call the linker verbosely and print the correct library order to the shell
# ~> gcc prog.c -static -Wl,--verbose -Wl,-\( -llib1 -llib2 -lrest_of_the_libs -Wl,-\) | ./order_deps.rb
libs = Array.new
@engelmarkus
engelmarkus / Makefile
Created November 27, 2017 01:09
Runtime code generation and execution in C++
all: program
program: program.cpp
g++ -std=c++14 -o program program.cpp -ldl -rdynamic
run: program query.cpp
./program "`< query.cpp`"
.PHONY: clean
clean:
@engelmarkus
engelmarkus / tagged_ptr.cpp
Created November 27, 2017 01:17
A pointer class storing flags in unused address bits.
#include <cassert>
#include <cstdint>
#include <iostream>
#include <type_traits>
template <unsigned arg, unsigned result = 0>
constexpr unsigned log2 = log2<(arg >> 1), result + 1>;
template <unsigned result>
constexpr unsigned log2<1, result> = result;