summaryrefslogtreecommitdiff
path: root/cesar/lib/src/fixed.c
blob: 574300becc8fb54425a80b65f2f0cfe472d01a05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/* Cesar project {{{
 *
 * Copyright (C) 2009 Spidcom
 *
 * <<<Licence>>>
 *
 * }}} */
/**
 * \file    lib/src/fixed.c
 * \brief   Fixed point math.
 * \ingroup lib
 */
#include "common/std.h"

#include "lib/fixed.h"

s32
fixed_sin_limited_q24 (s32 a)
{
    uint i;
    /* sin(pi*x) approximation parameters.
     * format: q7.24, guard: 4. */
    static const s32 poly[] = {
        +0x01507835,  /* x^9 */
        -0x09969667,  /* x^7 */
        +0x28cd78cf,  /* x^5 */
        -0x52aef399,  /* x^3 */
        +0x3243f6a9,  /* x^1 */
    };
    s32 as = fixed_mul (a, a, 24 - 4);
    s32 r = poly[0];
    for (i = 1; i < COUNT (poly); i++)
    {
        r = fixed_mul (r, as, 24 + 4);
        r += poly[i];
    }
    r = fixed_mul (r, a, 24 + 4);
    return r;
}

s32
fixed_cos_limited_q24 (s32 a)
{
    uint i;
    /* cos(pi*x) approximation parameters.
     * format: q7.24, guard: 4. */
    static const s32 poly[] = {
        +0x03c3ea0d,  /* x^8 */
        -0x155d3c7e,  /* x^6 */
        +0x40f07c20,  /* x^4 */
        -0x4ef4f327,  /* x^2 */
        +0x10000000,  /* x^0 */
    };
    s32 as = fixed_mul (a, a, 24 - 4);
    s32 r = poly[0];
    for (i = 1; i < COUNT (poly); i++)
    {
        r = fixed_mul (r, as, 24 + 4);
        r += poly[i];
    }
    /* Remove guard bits. */
    r = (r + (1 << 3)) >> 4;
    return r;
}

s32
fixed_sin_q24 (s32 a)
{
    /* For determination, add pi/4 and push bits to MSB. */
    s32 ad = (a + (1 << 22)) << 7;
    if (ad >= 0)
    {
        if ((ad << 1) >= 0)
        {
            /* [-pi/4:pi/4] => sin (a) = sin (a) */
            return fixed_sin_limited_q24 (a << 7 >> 7);
        }
        else
        {
            /* [pi/4:3pi/4] => sin (a) = cos (a - pi/2) */
            return fixed_cos_limited_q24 ((a - (1 << 23)) << 7 >> 7);
        }
    }
    else
    {
        if ((ad << 1) >= 0)
        {
            /* [3pi/4:5pi/4] => sin (a) = sin (pi - a) */
            return fixed_sin_limited_q24 (((1 << 24) - a) << 7 >> 7);
        }
        else
        {
            /* [-3pi/4:-pi/4] => sin (a) = -cos (a + pi/2) */
            return -fixed_cos_limited_q24 ((a + (1 << 23)) << 7 >> 7);
        }
    }
}

s32
fixed_cos_q24 (s32 a)
{
    /* For determination, add pi/4 and push bits to MSB. */
    s32 ad = (a + (1 << 22)) << 7;
    if (ad >= 0)
    {
        if ((ad << 1) >= 0)
        {
            /* [-pi/4:pi/4] => cos (a) = cos (a) */
            return fixed_cos_limited_q24 (a << 7 >> 7);
        }
        else
        {
            /* [pi/4:3pi/4] => cos (a) = sin (pi/2 - a) */
            return fixed_sin_limited_q24 (((1 << 23) - a) << 7 >> 7);
        }
    }
    else
    {
        if ((ad << 1) >= 0)
        {
            /* [3pi/4:5pi/4] => cos (a) = -cos (pi - a) */
            return -fixed_cos_limited_q24 (((1 << 24) - a) << 7 >> 7);
        }
        else
        {
            /* [-3pi/4:-pi/4] => cos (a) = sin (a + pi/2) */
            return fixed_sin_limited_q24 ((a + (1 << 23)) << 7 >> 7);
        }
    }
}

s32
fixed_sqrt (s32 x, uint shift)
{
    dbg_assert (x >= 0);
    dbg_assert (!(shift & 1));
    /* See Ken Turkowski, Fixed Point Square Root, 3 October 1994. */
    u32 root, rem_hi, rem_lo, test, count;
    root = 0;
    rem_hi = 0;
    rem_lo = x;
    count = 30 / 2 + shift / 2;
    do
    {
        rem_hi = (rem_hi << 2) | (rem_lo >> 30);
        rem_lo <<= 2;
        root <<= 1;
        test = (root << 1) + 1;
        if (rem_hi >= test)
        {
            rem_hi -= test;
            root++;
        }
    } while (count-- != 0);
    /* Rounding. */
    rem_hi = (rem_hi << 2) | (rem_lo >> 30);
    rem_lo <<= 2;
    test = (root << 2) + 1;
    if (rem_hi >= test)
    {
        rem_hi -= test;
        root++;
    }
    return root;
}