Skip to content

Instantly share code, notes, and snippets.

! Demo fortran program for some syntax candies
program fortran_candies
implicit none ! Disallow F77 naming conventions
integer, parameter :: M = 4, N = 3 ! parameter attribute declares constant
real :: A(M, N)
! Creates a random matrix
call random_number(A)
call print_matrix(A, 'A')
! Numpy-style slice and set value
subroutine print_matrix(A, var_name)
real, intent(in) :: A(:,:)
character(len = *), intent(in) :: var_name
integer :: i, M, N
M = size(A, 1)
N = size(A, 2)
print *, var_name, '='
do i = 1, M
print *, (A(i, j), j = 1,N)
end do
# Install homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install python and nodejs
brew install python3
brew install nodejs
# Install python packages
pip3 install numpy
pip3 install scipy
heap->occupy_bmap[0] = 0x01;
/* becomes
598 .loc 2 134 3 is_stmt 1 ## lookup_helper_test.c:134:3
599 leaq 72(%r13), %rsi
600 .loc 2 134 24 is_stmt 0 ## lookup_helper_test.c:134:24
601 movq $1, -56(%rbp)
602 leaq -56(%rbp), %rdx
603 movl $8, %edi
604 movl $5, %ecx
@dryman
dryman / union.c
Created March 20, 2017 14:37
Polymorphism by GCC transparent union extension (works in clang as well)
typedef enum GenericType GenericType;
typedef struct A A;
typedef struct B B;
enum GenericType
{
TYPE_A = 0,
TYPE_B = 1,
};
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdbool.h>
static atomic_bool lock = false;
static volatile int counter = 0;
void* thread_start(void *arg)
@dryman
dryman / tls_bench.c
Created October 20, 2016 04:35
thread local bench
// gcc -O2 -Wall -pthread tls_bench.c -o tls_bench
// ./tls_bench <num_threads> <loop>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#include <assert.h>
#include <stdatomic.h>
__thread int tls = -1;
#include <sys/mman.h>
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
int main()
{
ptrdiff_t offset = 1L << 36;
void* addr = NULL + offset;
void* map_addr;
@dryman
dryman / .emacs
Created August 5, 2016 05:26
Minimal org-mode setup
(require 'package)
(setq package-archives
'(("org" . "http://orgmode.org/elpa/")
("gnu" . "http://elpa.gnu.org/packages")
("marmalade"."http://marmalade-repo.org/packages/")
("melpa"."http://melpa.org/packages/")))
(package-initialize)
(defun ensure-package-installed (&rest packages)
"Download the package if it were not installed"
AC_DEFUN_ONCE([_AX_GCC_X86_CPU_INIT],
[AC_LANG_PUSH([C])
AC_CACHE_CHECK([for gcc __builtin_cpu_init function],
[ax_cv_gcc_check_x86_cpu_init],
[AC_RUN_IFELSE(
[AC_LANG_PROGRAM([#include <stdlib.h>],
[__builtin_cpu_init ();])
],
[ax_cv_gcc_check_x86_cpu_init=yes],
[ax_cv_gcc_check_x86_cpu_init=no])])