summaryrefslogtreecommitdiffhomepage
path: root/digital/avr/modules/math/fixed/fixed_cosin_f824.c
blob: d5148f42d56bfd018504e8187f50cf96c7f6fc9c (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
/* fixed_cosin_f824.c */
/* avr.math.fixed - Fixed point math module. {{{
 *
 * Copyright (C) 2005 Nicolas Schodet
 *
 * Robot APB Team/Efrei 2006.
 *        Web: http://assos.efrei.fr/robot/
 *      Email: robot AT efrei DOT fr
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "common.h"
#include "fixed.h"

/** Compute cosinus for angles between [0,pi/2]. */
static int32_t
fixed_cos_dli (int32_t a)
{
    static const int32_t f[] = {
	(1L << 24) * -26.42625678337439745096,
	(1L << 24) * 60.24464137187666035919,
	(1L << 24) * -85.45681720669372773226,
	(1L << 24) * 64.93939402266829148905,
	(1L << 24) * -19.73920880217871723738,
	(1L << 24) * 1
    };
    int32_t r;
    int32_t a2;
    uint8_t i;
    a2 = fixed_mul_f824 (a, a);
    r = f[0];
    for (i = 1; i < sizeof (f) / sizeof (f[0]); i++)
	r = fixed_mul_f824 (r, a2) + f[i];
    return r;
}

/** Compute cosinus, angle f8.24, result f8.24. */
int32_t
fixed_cos_f824 (int32_t a)
{
    a &= (1L << 24) - 1;
    uint8_t z = ((uint32_t) a >> 16) & 0xc0;
    if (z == 0)
	return fixed_cos_dli (a);
    else if (z == 1 << 6)
	return -fixed_cos_dli ((1L << 23) - a);
    else if (z == 2 << 6)
	return -fixed_cos_dli (a & 0xff7fffff);
    else
	return fixed_cos_dli ((1L << 24) - a);
}

/** Compute sinus, angle f8.24, result f8.24. */
int32_t
fixed_sin_f824 (int32_t a)
{
    a &= (1L << 24) - 1;
    uint8_t z = ((uint32_t) a >> 16) & 0xc0;
    if (z == 0)
	return fixed_cos_dli ((1L << 22) - a);
    else if (z == 1 << 6)
	return fixed_cos_dli (a - (1L << 22));
    else if (z == 2 << 6)
	return -fixed_cos_dli ((1L << 23) + (1L << 22) - a);
    else
	return -fixed_cos_dli (a - (1L << 23) - (1L << 22));
}