Skip to content

Instantly share code, notes, and snippets.

View x100ex's full-sized avatar

Aleksander A. Popov x100ex

View GitHub Profile
// https://schani.wordpress.com/2010/04/30/linear-vs-binary-search/
static int
binary_cmov (const int *arr, int n, int key) {
int min = 0, max = n;
while (min < max) {
int middle = (min + max) >> 1;
asm ("cmpl %3, %2\n\tcmovg %4, %0\n\tcmovle %5, %1"
: "+r" (min),
// https://schani.wordpress.com/2010/04/30/linear-vs-binary-search/
static int
binary (const int *arr, int n, int key) {
int min = 0, max = n;
while (min < max) {
int middle = (min + max) >> 1;
if (key > arr [middle])
min = middle + 1;
// https://schani.wordpress.com/2010/04/30/linear-vs-binary-search/
static int
linear_sentinel_sse2_nobranch (const int *arr, int n, int key) {
v4si *in_data = (v4si*)arr;
v4si key4 = { key, key, key, key };
int i = 0;
for (;;) {
v4si cmp0 = __builtin_ia32_pcmpgtd128 (key4, in_data [i + 0]);
// https://schani.wordpress.com/2010/04/30/linear-vs-binary-search/
static int
linear_sentinel_sse2 (const int *arr, int n, int key) {
v4si *in_data = (v4si*)arr;
v4si key4 = { key, key, key, key };
int i = 0;
for (;;) {
v4si tmp = __builtin_ia32_pcmpgtd128 (key4, in_data [i]);
// https://schani.wordpress.com/2010/04/30/linear-vs-binary-search/
static int
linear_sentinel (const int *arr, int key) {
int i = 0;
for (;;) {
if (arr [i] >= key)
return i;
++i;
@x100ex
x100ex / linear_4.c
Created June 2, 2017 13:34
linear_4.c
// https://schani.wordpress.com/2010/04/30/linear-vs-binary-search/
static int
linear_4 (const int *arr, int n, int key) {
int i = 0;
while (i + 3 < n) {
if (arr [i + 0] >= key) return i + 0;
if (arr [i + 1] >= key) return i + 1;
if (arr [i + 2] >= key) return i + 2;
@x100ex
x100ex / abspath.sh
Last active August 29, 2015 14:05
pure bash abspath
#!/bin/bash
abspath() {
local thePath
if [[ ! "$1" =~ ^/ ]]
then
thePath="$PWD/$1"
else
thePath="$1"
fi