Earlier quoted context omitted.
This has been the standard algorithm used by every libm for decades. Its not special to Musl.
But isn't this code rarely called in practice? I guess on intel architectures the compiler just calls the fsin instruction of the cpu.
Implementing Cosine in C from Scratch (2020)
61–70 of 139 posts
Re: Implementing Cosine in C from Scratch (2020)
#62cordic beautifully explained; i have used this approach on fixed point dsp processors in another life; its simplicity, and elegance is beautiful (on controllers with no fp)
Re: Implementing Cosine in C from Scratch (2020)
#63This is a very nice article, and I think the three initial objectives are fully attained (simple enough, accurate enough, fast enough). Some of the performance claims have a caveat, though: Lookup tables and micro-benchmarks don't mix well. At all. I just added a simple loop that thrashes the L3 cache every 500 iterations (diff here: https://goonlinetools.com/snapshot/code/#sm4fjqtvjyn36dyednc... ). Now the method re…
How long does the cache thrashing loop take compared to 500 iterations? I'm not sure how much you can trash the cache while still being performance bottle-necked by a table-based implementation of a trig function.
Re: Implementing Cosine in C from Scratch (2020)
#64Re: Implementing Cosine in C from Scratch (2020)
#65Mandatory Physics "troll" comment: For small angles, sin(x) ~= x and cos(x) ~= 1 -- the ["small angle approximation"]( https://en.wikipedia.org/wiki/Small-angle_approximation ) It's actually kind of ridiculous how many times it comes up and works well enough in undergraduate Mechanics.
Re: Implementing Cosine in C from Scratch (2020)
#66Earlier quoted context omitted.
This has been the standard algorithm used by every libm for decades. Its not special to Musl.
But isn't this code rarely called in practice? I guess on intel architectures the compiler just calls the fsin instruction of the cpu.
Re: Implementing Cosine in C from Scratch (2020)
#67My favorite implementation of a super fast but not that accurate cosinus is from a now defunct forum. Good thing we have webarchive ! https://web.archive.org/web/20111104163237/http://www.devmas...
You can also use the Chebyshev polynomials to economise a higher degree taylor series with bit of a loss of accuracy.
Re: Implementing Cosine in C from Scratch (2020)
#68Earlier quoted context omitted.
This has been the standard algorithm used by every libm for decades. Its not special to Musl.
But isn't this code rarely called in practice? I guess on intel architectures the compiler just calls the fsin instruction of the cpu.
Do people do that in practice? It's on the FPU, which is basically legacy emulated these days, and it's inaccurate.
Re: Implementing Cosine in C from Scratch (2020)
#69Earlier quoted context omitted.
This has been the standard algorithm used by every libm for decades. Its not special to Musl.
But isn't this code rarely called in practice? I guess on intel architectures the compiler just calls the fsin instruction of the cpu.
Re: Implementing Cosine in C from Scratch (2020)
#70The implementation of the __cos kernel in Musl is actually quite elegant. After reducing the input to the range [-pi/4, pi/4], it just applies the best degree-14 polynomial for approximating the cosine on this interval. It turns out that this suffices for having an error that is less than the machine precision. The coefficients of this polynomial can be computed with the Remez algorithm, but even truncating the Cheby…