Do you have a way to measure that to be sure?

It’s better to be safe than sorry.

E.g. It’s possible that the truncations from `double`

to `float`

are also consuming time.

Unfortunately maths isn’t my strongpoint, and being written in C rather than C++ limits how much I can infer from the code, and I don’t know much about optimising code for FPGAs because I don’t actually know much about how they physically work compared to standard general-purpose CPUs,

but I’ll have a think about it anyway.

On first read the code looks pretty sensible.

(I’ll resist the urge to review the quality. There are good points and bad points.)

It presumably maps `x`

to quadrant 1 of the unit circle and then performs a 10 step taylor series approximation of sine.

(I say presumably because I’m not entirely sure what the 4 `if`

s are doing, I only know for definite what the `fmod`

and `for`

are doing.)

Off the top of my head, a few options:

- It might be possible to rewrite the code to employ more lookup tables
- You could reduce the number of iterations of the taylor series,

which would give less accurate results in exchange for faster execution time
- You could make a
`float`

version of `sin`

, `float`

s can be faster than `double`

s and have less precision (so fewer iterations of the taylor series may be required)

One thing I do know that may be of use is that the `sin`

used on the Arduboy is actually written in assembly.

(It’s part of avr-libc, the documentation is here, and the source code is here.)

Lastly, one surprisingly obvious optimisation.

(Obvious for someone who knows what an alternating series is. `:P`

)

(Assuming that eliminating branches is an optimisation for FPGAs.)

The `if`

in the `for`

loop can be eliminated by simply negating every other entry of the lookup table:

```
/************************************************************************
* libc/math/lib_sin.c
*
* This file is a part of NuttX:
*
* Copyright (C) 2012 Gregory Nutt. All rights reserved.
* Ported by: Darcy Gong
*
* It derives from the Rhombs OS math library by Nick Johnson which has
* a compatibile, MIT-style license:
*
* Copyright (C) 2009-2011 Nick Johnson <nickbjohnson4224 at gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
************************************************************************/
/************************************************************************
* Included Files
************************************************************************/
#include <sys/types.h>
#include <math.h>
/************************************************************************
* Private Data
************************************************************************/
static double _dbl_inv_fact[] =
{
1.0 / 1.0, // 1 / 1!
-1.0 / 6.0, // -1 / 3!
1.0 / 120.0, // 1 / 5!
-1.0 / 5040.0, // -1 / 7!
1.0 / 362880.0, // 1 / 9!
-1.0 / 39916800.0, // -1 / 11!
1.0 / 6227020800.0, // 1 / 13!
-1.0 / 1307674368000.0, // -1 / 15!
1.0 / 355687428096000.0, // 1 / 17!
-1.0 / 121645100408832000.0, // -1 / 19!
};
/************************************************************************
* Public Functions
************************************************************************/
double sin(double x)
{
double x_squared;
double sin_x;
size_t i;
/* Move x to [-pi, pi) */
x = fmod(x, 2 * M_PI);
if (x >= M_PI)
{
x -= 2 * M_PI;
}
if (x < -M_PI)
{
x += 2 * M_PI;
}
/* Move x to [-pi/2, pi/2) */
if (x >= M_PI_2)
{
x = M_PI - x;
}
if (x < -M_PI_2)
{
x = -M_PI - x;
}
x_squared = x * x;
sin_x = 0.0;
/* Perform Taylor series approximation for sin(x) with ten terms */
for (i = 0; i < 10; i++)
{
sin_x += x * _dbl_inv_fact[i];
x *= x_squared;
}
return sin_x;
}
```

Completely untested, but I can’t see any logical reason why it wouldn’t work.

While we’re on the subject of circles, tau (τ) is better than pi (π).