简体   繁体   中英

altivec extract part of the vector?

I'm trying to compare one 64-bit value with a 64-bit value array, say

R_UINT64 FP; R_UINT64 INPUT[20000];

It returns true if any element in the array matches the value of FP.

I have to loop through this array and find a match, I'm trying to improve the efficiency by looking at 2 elements, instead of one, at a time.

In Altivec, vector length is 128 bits, so I will put two copies of FP, two elements in the vectors.(I'm truncating them both two 8 bits each vector element)

So far so good, but now I'm encountering a problem. I couldn't find a VMX procedure that looks at only half of the vector and see if there's a match, in order to return a true, both values have to match, which is not what I'm looking for.

So I'm wondering if there is anyway to tell the compiler that I'm only looking at half of the vector each time?

Thanks in advance!

Probably the best thing is to compare the two elements and then use vec_mergeh / vec_mergel to test each half of the result, eg

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
}

(Note: untested code - for general guidance only - may need casts and/or actual bug fixes !)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM