Skip to content

Instantly share code, notes, and snippets.

@x100ex
Created June 2, 2017 13:39
Show Gist options
  • Save x100ex/426b29b3805302e2d54672699e8558b6 to your computer and use it in GitHub Desktop.
Save x100ex/426b29b3805302e2d54672699e8558b6 to your computer and use it in GitHub Desktop.
// 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),
"+r" (max)
: "r" (key), "g" (arr [middle]),
"g" (middle + 1), "g" (middle));
}
return min;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment