Skip to content

Instantly share code, notes, and snippets.

@pfultz2
pfultz2 / clang_refactor.py
Created May 3, 2023 16:58
Python script to do clang refactoring
# Create a find function to refactor nodes. This takes a node which is a clang
# cursor and ClangReplacements class. It will return true if it should
# traverse the children.
#
# def find(node, r):
# ...
# return True
#
# refactor = ClangRefactor(compiler_path='/usr')
# refactor.walk(find)
@pfultz2
pfultz2 / kogge_stone_adder.cpp
Created March 23, 2021 22:19
Kogge-Stone adder
#include <bitset>
// Kogge-Stone adder
template<std::size_t N>
struct carry
{
std::bitset<N> g = 0;
std::bitset<N> p = 0;
static constexpr carry apply(carry x, carry y)
{
@pfultz2
pfultz2 / creduce.py
Last active October 28, 2019 13:59
Python script to reduce using creduce
import argparse, contextlib, multiprocessing, os, tempfile, shutil, subprocess
@contextlib.contextmanager
def mkdtemp():
d = tempfile.mkdtemp()
yield d
shutil.rmtree(d, ignore_errors=True)
def print_lines(lines):
for line in lines:
@pfultz2
pfultz2 / ackerman.cpp
Created June 4, 2019 01:38
Ackerman recursive function implemented in the C99 preprocessor
#define EMPTY()
#define DEFER(id) id EMPTY()
#define OBSTRUCT(...) __VA_ARGS__ DEFER(EMPTY)()
#define EXPAND(...) __VA_ARGS__
#define EVAL(...) EVAL1(EVAL1(EVAL1(__VA_ARGS__)))
#define EVAL1(...) EVAL2(EVAL2(EVAL2(__VA_ARGS__)))
#define EVAL2(...) EVAL3(EVAL3(EVAL3(__VA_ARGS__)))
#define EVAL3(...) EVAL4(EVAL4(EVAL4(__VA_ARGS__)))
@pfultz2
pfultz2 / AutoconfHelper.cmake
Created March 14, 2017 03:55
Helper functions for translating autoconf projects
# Helper functions for translating autoconf projects. Several functions
# are lifted from the Mono sources
include (CheckCSourceCompiles)
include (CheckIncludeFile)
include (TestBigEndian)
include (CheckFunctionExists)
include (CheckTypeSize)
include (CheckCSourceRuns)
template <class am> void an(long r, am) {
const long aq(r);
[&] {
[&] { aq; };
aq;
};
}
int main() { an(0, 0); }
#=============================================================================
# Copyright 2004-2011 Kitware, Inc.
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
template<class Interface>
struct GenericInterface
{
std::string query_interface() const
{
return fit::conditional(
[](auto&& x) FIT_RETURNS(x.interface()),
[](auto&& x) FIT_RETURNS(x.get_my_interface()),
[](auto&& x) FIT_RETURNS(x.myInterface())
)(i);
template<class Interface>
struct GenericInterface
{
template<class T = Interface>
auto query_interface() const -> decltype(std::declval<T>().interface(), std::string())
{
return i.interface();
}
template<class T = Interface>
auto query_interface() const -> decltype(std::declval<T>().get_my_interface(), std::string())
@pfultz2
pfultz2 / download_file.py
Last active February 23, 2016 20:09
Download file in python
import urllib
from tqdm import tqdm
def my_hook(t):
"""
Wraps tqdm instance. Don't forget to close() or __exit__()
the tqdm instance once you're done with it (easiest using `with` syntax).
Example
-------