可能最好的办法是比较这两个元素,然后使用vec_mergeh
/vec_mergel
来测试结果的每一半,例如
size_t vec_search_u64(const uint64_t key, const uint64_t array[], const size_t len)
{
const vector signed int vkey = { key >> 32, key & 0xffffffff, key >> 32, key & 0xffffffff };
const vector bool int vk1 = { -1, -1, -1, -1 };
for (i = 0; i < len - 1; i += 2) // iterate two elements at a time
{
vector signed int v = vec_ld(0, (int *)&array[i]);
// load 2 elements
vector bool int vcmp = vec_cmpeq(v, vkey);
// compare 2 elements with key
if (vec_all_eq(vec_mergeh(vcmp, vcmp), vk1))
{ // if high element matches
return i; // return match found at element i
}
if (vec_all_eq(vec_mergel(vcmp, vcmp), vk1))
{ // if low element matches
return i + 1; // return match found at element i + 1
}
}
if (i < len) // if array size is odd
{
if (array[i] == key) // test last element
{
return i;
}
}
return (size_t)(-1); // match not found - return suitable value
}
(注意:未经测试的代码 - 仅用于一般指导 - 可能需要强制转换和/或实际的错误修复!)