armsimdneoncortex-a8

Neon: isnan(val) intrinsics


I want to use isnan()functionality in NEON intrinsics .Below is my code :input1,input2 and output is of type float .These values are getting updated from ROI of input image/frame.(image processing example)

for(x = 0;x<ht;x++){
for(y = 0;y<width;y++){
float a  = (input1[x + (y * width)]);
float b  = (input2[x + (y * width)]);
// check for division by zero
output = 0.0f;
if (!(isnan(a) | isnan(b) | (b == 0)))
{
        output[x + (y * width)] = a / b;
}
}

}

By using newton Raphson method I tried to do division by using neon intrinsic . But i am not able to get any intrinsics for isnan .I got __builtin_isnan() which is is not an intrinsics .How Can I use isnan for float32x4_t a and float32x4_t b


Solution

  • A useful property of IEEE-754 floating point values is that comparing two NaN values always returns false. You can use this property to test for NaN, as follows:

    bool isNaN(float x)
    {
        return !(x == x);
    }
    

    This same test can be applied to SIMD operations, where a float vector may be compared with itself, and the result will be false for any element which is a NaN, e.g.

    float32x4_t vx = { ... };
    
    uint32x4_t vcmp = vceqq_f32(vx, vx);
    

    The elements of vcmp will be true (UINT_MAX) for non-NaN values in x, and false (0) for any NaN values.