This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Best way to compute sign()

Tags: None
(comma "," separated)
dudurighi
Registered Member
Posts
2
Karma
0

Best way to compute sign()

Fri Jun 25, 2021 4:05 pm
Hello!
I'm trying to compute the sign() matrix (vector in this case) that is similar to GLSL's sign() https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/sign.xhtml
The main difference is in the behavior of when the input is 0. GLSL returns 1 and cwiseSign returns 0. I've found two propose methods of replicating this behavior in Eigen:

Using select:
Code: Select all
(vector.array() >= 0.0f).select(1.0f, -1.0f)

Using unary expressions:
Code: Select all
vector.unaryExpr([](float in) {return in < 0 ? -1 : 1; });

What would be the most efficient way to achieve this?

Thanks!
dudurighi
Registered Member
Posts
2
Karma
0

Re: Best way to compute sign()

Sat Jun 26, 2021 2:06 am
Wanted to loop back on this. After some investigation, it looks like the most efficient way I could find was to use the unaryExpr with std::copysignf()
Code: Select all
vector.unaryExpr([](float in) {return std::copysignf(1.0f,in);});


This, for Vector4fs translates to only 6 SSE instructions:
https://godbolt.org/z/6q15ddTE1


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Google [Bot], Yahoo [Bot]