Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create linear_search.rs * Create binary_search_tree.rs * Create binary_search.rs
- Loading branch information
1 parent
0eaa9c2
commit d8ddcd9
Showing
3 changed files
with
516 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
const BLOCK_SIZE: usize = 128; | ||
|
||
// Binary search over an array of 128 els. | ||
// | ||
// Returns the first i such that | ||
// block[i] >= target. | ||
// | ||
// # Assumes | ||
// | ||
// That block is sorted, and that the last `doc` in | ||
// block is `>= target`. | ||
// If none of the elements is greater than the last doc, | ||
// this function returns 127, which does not make much sense. | ||
#[inline(never)] | ||
fn binary_search(block: &[u32; BLOCK_SIZE], target: u32) -> usize { | ||
// Full block of 128 els. | ||
// | ||
// We do a branchless, unrolled binary search. | ||
let mut start = 0; | ||
let mut half: usize = BLOCK_SIZE / 2; | ||
for _ in 0..7 { | ||
let middle = start + half; | ||
unsafe { | ||
let pivot: u32 = *block.get_unchecked(middle); | ||
asm!("cmpl $2, $1\ncmovge $3, $0" | ||
: "+r"(start) | ||
: "r"(target), "r"(pivot), "r"(middle)) | ||
; | ||
} | ||
half /= 2; | ||
} | ||
start | ||
} | ||
|
||
fn main() { | ||
let mut arr = [0u32; 128]; | ||
for i in 0..128 { | ||
arr[i] = i as u32; | ||
} | ||
for i in 0..129 { | ||
println!("{}", binary_search(&arr, i)); | ||
} | ||
} |
Oops, something went wrong.