spinn_common 7.2.2
Support code for SpiNNaker applications.
Loading...
Searching...
No Matches
Functions | Variables
sincos.c File Reference

Implementation of log for the accum type. More...

#include "stdfix-full-iso.h"
#include "assert.h"
#include "arm_acle.h"
#include "pair.h"

Functions

static int32_t sin_approx (int32_t x)
 This function calculates the sin function approximation.
 
static int32_t cos_approx (int32_t x)
 This function calculates the cos function approximation.
 
static int32_t sin_pi4 (int32_t x, int32_t cos_x, int break_point)
 This function calculates the sin function for the interval [0, π/4)
 
static int32_t cos_pi4 (int32_t x, int32_t cos_x, int break_point)
 This function calculates the cos function for the interval [0, π/4)
 
int32_t mul_sqrt_half (int32_t x)
 This function multiplies it's signed argument by sqrt(0.5).
 
static int32_t sin_pi4_x_sqrt_half (int32_t s)
 This function calculates the sin function for the interval [0, π/4) divided by sqrt(0.5).
 
static int32_t cos_pi4_x_sqrt_half (int32_t c)
 This function calculates the cos function for the interval [0, π/4) divided by sqrt(0.5).
 
static int reduced (pair_t p)
 Get the reduced range value out of the result of sincos_range_reduction()
 
static int quadrant (pair_t p)
 Get the octant out of the result of sincos_range_reduction()
 
pair_t sincos_range_reduction (int32_t x)
 This function returns the range-reduced argument and the quadrant.
 
static bool even (int x)
 Is an integer value even?
 
static int32_t sinkbits (int32_t x)
 Implementation of sink() on the bit-representation of fixed point values.
 
static int32_t coskbits (int32_t x)
 Implementation of cosk() on the bit-representation of fixed point values.
 
accum sink (accum x)
 This function calculates the sin function for the s1615 type.
 
accum cosk (accum x)
 This function calculates the cos function for the s1615 type.
 

Variables

static const int32_t sin_table [16]
 sin_table[k] = sin(k/16) * 2^31
 
static const int32_t cos_table [16]
 cos_table[k] = (cos(k/16) - 1) * 2^31
 

Detailed Description

Implementation of log for the accum type.

The details of the algorithm are from "Elementary Functions: Algorithms and Implementation", 2nd edn, Jean-Michel Muller, Birkhauser, 2006.

Author
Dave Lester (david.nosp@m..r.l.nosp@m.ester.nosp@m.@man.nosp@m.chest.nosp@m.er.a.nosp@m.c.uk)
Date
30 October, 2014

Function Documentation

◆ sin_approx()

static int32_t sin_approx ( int32_t  x)
inlinestatic

This function calculates the sin function approximation.

Parameters
[in]xan s0.31 representing a number in the interval [-1/32, 1/32); i.e. INT32_MIN represents -1/32
Returns
A value representing sin(x) in s0.31 format.

◆ cos_approx()

static int32_t cos_approx ( int32_t  x)
inlinestatic

This function calculates the cos function approximation.

Parameters
[in]xan s0.31 representing a number in the interval [-1/32, 1/32); i.e. INT32_MIN represents -1/32
Returns
A value representing (1 - cos(x)) in s0.31 format.

◆ sin_pi4()

static int32_t sin_pi4 ( int32_t  x,
int32_t  cos_x,
int  break_point 
)
inlinestatic

This function calculates the sin function for the interval [0, π/4)

Parameters
[in]xAn s0.31 representing a value in the range [-1/32,1/32), with INT32_MIN representing -1/32.
[in]cos_xThe value of cos_x (= (1 - cos(x)) * 2^31) represented as an s0.31
[in]break_pointA value in the range 0..13
Returns
sin(x + break_point / 16) as s0.31

◆ cos_pi4()

static int32_t cos_pi4 ( int32_t  x,
int32_t  cos_x,
int  break_point 
)
inlinestatic

This function calculates the cos function for the interval [0, π/4)

Parameters
[in]xAn s0.31 representing a value in the range [-1/32,1/32), with INT32_MIN representing -1/32.
[in]cos_xThe value of cos_x (= (1 - cos(x)) * 2^31) represented as an s0.31
[in]break_pointA value in the range 0..13
Returns
1 - cos(x + break_point / 16) as s0.31

Implementation notes

We are using the identity: cos(x+b) = cos(x)*cos(b) - sin(x)*sin(b)

However, we return 1-cos(x+b) = 1 - cos(x)*cos(b) + sin(x)*sin(b)

Let cx = 1-cos(x), and cb = 1-cos(b)

Then cx * cb = 1 + cos(x)*cos(b) - cos(x) - cos(b)

Thus 1 - cos(x)*cos(b) = -(cx*cb) + cx + cb

◆ mul_sqrt_half()

int32_t mul_sqrt_half ( int32_t  x)

This function multiplies it's signed argument by sqrt(0.5).

Parameters
[in]xis a signed 32-bit quantity.
Returns
x * sqrt(0.5) in the same format as x.

Implementation notes

Consider experimenting with smlawt/b instructions. The calculation

r = (x * 1518500250) >> 31;

is equivalent to:

r = smulwt(x, 23170);
r = smlawb(x, 31130, r) << 1; // n.b. two constants in same register

◆ sin_pi4_x_sqrt_half()

static int32_t sin_pi4_x_sqrt_half ( int32_t  s)
inlinestatic

This function calculates the sin function for the interval [0, π/4) divided by sqrt(0.5).

Parameters
[in]sAn s0.31 representing the value of sin(x).
Returns
An s0.31 representing sin(x) * sqrt(0.5).

◆ cos_pi4_x_sqrt_half()

static int32_t cos_pi4_x_sqrt_half ( int32_t  c)
inlinestatic

This function calculates the cos function for the interval [0, π/4) divided by sqrt(0.5).

Parameters
[in]cAn s0.31 representing the value of 1 - cos(x).
Returns
An s0.31 representing cos(x) * sqrt(0.5).

Implementation notes

Note that we have taken the opportunity to fold the subtraction back into the calculation. The reason is that we no longer have to represent 1.0000 as an s0.31.

◆ reduced()

static int reduced ( pair_t  p)
inlinestatic

Get the reduced range value out of the result of sincos_range_reduction()

Parameters
[in]pThe pair
Returns
the reduced range value (range [0,π/4] if fixed point)

◆ quadrant()

static int quadrant ( pair_t  p)
inlinestatic

Get the octant out of the result of sincos_range_reduction()

Parameters
[in]pThe pair
Returns
the octant (range [0,7])

◆ sincos_range_reduction()

pair_t sincos_range_reduction ( int32_t  x)

This function returns the range-reduced argument and the quadrant.

Parameters
[in]xis an s16.15.
Returns
A pair of values (accessible by use of fst() and snd()). The first component is the range reduced value of x represented by an s0.31 in the range [0,π/4). The second is a quadrant, in the range 0..7

◆ even()

static bool even ( int  x)
inlinestatic

Is an integer value even?

Parameters
[in]xThe value to test
Returns
True if x is even

◆ sinkbits()

static int32_t sinkbits ( int32_t  x)
inlinestatic

Implementation of sink() on the bit-representation of fixed point values.

Parameters
[in]xthe value to get the sine of
Returns
the sine of the value

◆ coskbits()

static int32_t coskbits ( int32_t  x)
inlinestatic

Implementation of cosk() on the bit-representation of fixed point values.

Parameters
[in]xthe value to get the cosine of
Returns
the cosine of the value

◆ sink()

accum sink ( s1615  x)

This function calculates the sin function for the s1615 type.

Parameters
[in]xis positive value represented as an s1615.
Returns
A value representing sin(x) in s1615 format.

◆ cosk()

accum cosk ( s1615  x)

This function calculates the cos function for the s1615 type.

Parameters
[in]xis positive value represented as an s1615.
Returns
A value representing cos(x) in s1615 format.

Variable Documentation

◆ sin_table

const int32_t sin_table[16]
static
Initial value:
= {
0, 134130364, 267736951, 400298032,
531295957, 660219183, 786564267, 909837834,
1029558505, 1145258771, 1256486826, 1362808327,
1463808091, 1559091715, 1648287120, 1731045999
}

sin_table[k] = sin(k/16) * 2^31

◆ cos_table

const int32_t cos_table[16]
static
Initial value:
= {
0, -4192939, -16755382, -37638274,
-66760066, -104007040, -149233746, -202263575,
-262889447, -330874618, -405953610, -487833239,
-576193767, -670690148, -770953377, -876591926
}

cos_table[k] = (cos(k/16) - 1) * 2^31