slipstream icon indicating copy to clipboard operation
slipstream copied to clipboard

Trigonometry functions

Open vorner opened this issue 5 years ago • 0 comments

It seems processors don't have direct support for computing trigonometry functions like sin or cos, neither in scalar nor vector form. Standard libraries/libm supplies the scalar versions.

Therefore, the way other operations in slipstream are constructed (by calling a scalar operation for each lane and hoping for the best) won't get auto-vectorized.

So the alternative is borrow the scalar implementation somewhere, but rewrite it expressing the vectors (and blends instead of conditionals). These primitive operations should get autovectorized OK.

From a first glance, I don't see why this actually works (one example taken from over the internet): https://github.com/JuliaMath/openlibm/blob/97de1a46a9fedec7749e10da6770d189ff8012e9/src/s_sincos.c#L89.

What needs to be done:

  • [ ] Find some good source where we can borrow from (compatible licence, readable code, reasonably performant...)
  • [ ] Write our own version with vectors (we'll probably need to introduce some trait on the base types to abstract f32/f64?)
  • [ ] Check that it produces the same values and that it gets vectorized somehow.
  • [ ] Add benchmarks
  • [ ] Figure out how this plays with inlining. The sincos implementation might be already a bit heavy so it might not want to get inlined nor it is a good idea sometimes. But not inlining would lose the #[target_feature(enable = "..")] annotations added by the user or by multiversion, which is unfortunate. Would we want to use multiversion ourselves to provide dynamic dispatch?

If someone has the time and wants to play with it, please claim the issue so people don't collide (writing a comment is fine). I'm not sure myself when I might find the time for this, but I'll be happy to discuss anything.

vorner avatar Jun 29 '20 07:06 vorner