Skip to content

Instantly share code, notes, and snippets.

View jxu's full-sized avatar
:shipit:
if it compiles it ships

jxu

:shipit:
if it compiles it ships
  • Wit's End
View GitHub Profile
f(std::vector<bool, std::allocator<bool>> const&, unsigned long):
mov rax, rdi ; rax = v
mov rdi, rsi ; rdi = i
mov ecx, esi ; rcx = i
mov rdx, QWORD PTR [rax] ; rdx = *v
shr rdi, 6 ; rdi >>= 6
mov eax, 1 ; rax = 1
sal rax, cl ; rax <<= (rcx & 0xFF)
and rax, QWORD PTR [rdx+rdi*8] ; rax &= rdx[rdi]
setne al ; al = (rax != 0)

These are simplified examples I came up with to go along with William Woodruff's excellent blog post on x86-64 addressing modes.

All the examples are compiled with GCC -O3 or -Os, which is indicative of what the compiler thinks the best option is in practice.

Base + Index

Use case: Indexing into a byte array (whose address is not fixed)

char f(char buf[], int index)
library(tidyverse)
# find duplicates by col
df %>% filter(n() > 1, .by = "col")
# drop duplicates by col
df %>% distinct(col)
# summarize per group by
mtcars %>%

x86-32 cdecl

Arguments pushed on stack, right-to-left.

Caller-saved: eax, ecx, edx. All others are callee-saved.

Return value in eax.

General purpose registers:

  1. EAX (Accumulator)
@jxu
jxu / ggplot_cheatsheet.R
Last active May 17, 2024 19:55
More googling
# Horizontal bar chart, ordered by value
ggplot(df, aes(x = reorder(name, value), y = value)) +
geom_bar(stat = "identity") +
labs(title = "Title", x = "Name", y = "Value") +
scale_y_continuous(expand = c(0,0)) +
coord_flip() +
theme_light()
@jxu
jxu / pandas_cheatsheet.py
Last active November 26, 2024 19:57
Pandas Cheatsheet (Avoid indexes)
import pandas as pd
### Data import ###
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html
df = pd.read_csv("file.csv")
### Data types ###
@jxu
jxu / SQL_cheatsheet.sql
Last active May 2, 2024 23:20
Specifically for T-SQL, probably for other SQLs
-- Syntax order
-- https://learn.microsoft.com/en-us/sql/t-sql/queries/select-transact-sql
WITH cte AS (cte_query)
SELECT cols
FROM table
WHERE cond
GROUP BY col
HAVING cond
WINDOW window_expr
@jxu
jxu / git-cheatsheet.sh
Last active December 4, 2024 18:56
Useful git commands
# push to new github repo
git remote add origin git@github.com:jxu/repo.git # use set-url if already set
git push -u origin master
# check out a remote branch
git fetch
git branch -v -a # list all branches
git switch branchname
# change remote a branch is tracking
@jxu
jxu / .vimrc
Created February 4, 2024 05:13
vimrc (again)
" Custom vim config with my comments
" (mostly from the ultimate Vim config amix/vimrc)
" => General
set nocompatible " Disable compatibility mode
set history=1000 " Save more commands
set autoread " Read file when changed externally
set autowrite " Autosave before commands like :make
set noswapfile " No more .swp!