The 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…
https://github.com/ifduyue/musl/blob/master/src/math/__cos.c
Implementing Cosine in C from Scratch (2020)
111–120 of 139 posts
Re: Implementing Cosine in C from Scratch (2020)
#112Earlier quoted context omitted.
FSIN only works on x87 registers which you will rarely use on AMD64 systems -- you really want to use at least scalar SSE2 today (since that is whence you receive your inputs as per typical AMD64 calling conventions anyway). Moving data from SSE registers to the FP stack just to calculate FSIN and then moving it back to SSE will probably kill your performance even if your FSIN implementation is good. If you're vector…
Moving between x87 and xmm registers is actually fairly cheap (it's through memory, so it's not free, but it's also not _that_ bad). FSIN itself is catastrophically slow.
Re: Implementing Cosine in C from Scratch (2020)
#113I learned how to write programs from Scratch[0]. Back in the pre-1.4 days (IIRC) there was no native sin/cos functions, so they had to be "implemented" somehow to translate arbitrary angles into delta (x,y). The way that I found people did this was as follows (for sin(A), cos(A)): Invisible sprite: [ when I receive "sine" ]: [ go to center ] [ turn A degrees ] [ move 1 step forward ] Now, anyone that wants to know si…
I guess scratch (or LOGO) is Turing complete, so someone could make a CPU built using a little turtle in a box where all functionality is built out of LOGO commands.
Re: Implementing Cosine in C from Scratch (2020)
#114Re: Implementing Cosine in C from Scratch (2020)
#115Earlier quoted context omitted.
Julia?
Julia uses a very similar approach to musl here. Reduction mod 2pi followed by polynomial kernel. We use tables for `exp` and `log`, but the trig functions have good reductions so we use those instead. The implementation is here https://github.com/JuliaLang/julia/blob/03af78108afe6058f54c... for anyone interested.
Re: Implementing Cosine in C from Scratch (2020)
#116Re: Implementing Cosine in C from Scratch (2020)
#117I was surprised that pre-calculating x x as double xx = x x made a difference. The author's compiler must not be great at optimization if this is the case.
Some compilers have options such as -ffast-math that result in ignoring some precision issues. This could be worth a try as an alternative to manual optimisation.
Re: Implementing Cosine in C from Scratch (2020)
#118Mandatory 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)
#119So 57 years ago, there weren’t any programming books. Well, there was one. McCracken and Dorn, Numerical Methods and Fortran Programming. A kindly childless couple, Ben and Bluma Goldin, who lived in the same apartment house in Brooklyn, bought me this book as a gift. An axiomatic lesson from the book was distrust of Taylor expansions, and the necessity of moving the expansion point of any function to a favorable loc…
Why tell us the couple's names and tell us they were childless? Strange. I'm not inclined to trust anything you said after that.
Re: Implementing Cosine in C from Scratch (2020)
#120CORDIC is still very common in embedded systems, especially with CPUs that have no hardware multiply/divide.