blob: d4686fe1cac752fb17dd182d8e976ac8053afba1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _FIXP_ARITH_H
2#define _FIXP_ARITH_H
3
Mauro Carvalho Chehab559addc2015-02-04 06:07:30 -03004#include <linux/math64.h>
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Simplistic fixed-point arithmetics.
8 * Hmm, I'm probably duplicating some code :(
9 *
10 * Copyright (c) 2002 Johann Deneux
11 */
12
13/*
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 * Should you need to contact me, the author, you can do so by
Johann Deneux118e78d2007-10-20 00:47:32 +020029 * e-mail - mail your message to <johann.deneux@gmail.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31
32#include <linux/types.h>
33
Mauro Carvalho Chehab559addc2015-02-04 06:07:30 -030034static const s32 sin_table[] = {
35 0x00000000, 0x023be165, 0x04779632, 0x06b2f1d2, 0x08edc7b6, 0x0b27eb5c,
36 0x0d61304d, 0x0f996a26, 0x11d06c96, 0x14060b67, 0x163a1a7d, 0x186c6ddd,
37 0x1a9cd9ac, 0x1ccb3236, 0x1ef74bf2, 0x2120fb82, 0x234815ba, 0x256c6f9e,
38 0x278dde6e, 0x29ac379f, 0x2bc750e8, 0x2ddf003f, 0x2ff31bdd, 0x32037a44,
39 0x340ff241, 0x36185aee, 0x381c8bb5, 0x3a1c5c56, 0x3c17a4e7, 0x3e0e3ddb,
40 0x3fffffff, 0x41ecc483, 0x43d464fa, 0x45b6bb5d, 0x4793a20f, 0x496af3e1,
41 0x4b3c8c11, 0x4d084650, 0x4ecdfec6, 0x508d9210, 0x5246dd48, 0x53f9be04,
42 0x55a6125a, 0x574bb8e5, 0x58ea90c2, 0x5a827999, 0x5c135399, 0x5d9cff82,
43 0x5f1f5ea0, 0x609a52d1, 0x620dbe8a, 0x637984d3, 0x64dd894f, 0x6639b039,
44 0x678dde6d, 0x68d9f963, 0x6a1de735, 0x6b598ea1, 0x6c8cd70a, 0x6db7a879,
45 0x6ed9eba0, 0x6ff389de, 0x71046d3c, 0x720c8074, 0x730baeec, 0x7401e4bf,
46 0x74ef0ebb, 0x75d31a5f, 0x76adf5e5, 0x777f903b, 0x7847d908, 0x7906c0af,
47 0x79bc384c, 0x7a6831b8, 0x7b0a9f8c, 0x7ba3751c, 0x7c32a67c, 0x7cb82884,
48 0x7d33f0c8, 0x7da5f5a3, 0x7e0e2e31, 0x7e6c924f, 0x7ec11aa3, 0x7f0bc095,
49 0x7f4c7e52, 0x7f834ecf, 0x7fb02dc4, 0x7fd317b3, 0x7fec09e1, 0x7ffb025e,
50 0x7fffffff
Linus Torvalds1da177e2005-04-16 15:20:36 -070051};
52
Mauro Carvalho Chehab559addc2015-02-04 06:07:30 -030053/**
54 * __fixp_sin32() returns the sin of an angle in degrees
55 *
56 * @degrees: angle, in degrees, from 0 to 360.
57 *
58 * The returned value ranges from -0x7fffffff to +0x7fffffff.
59 */
60static inline s32 __fixp_sin32(int degrees)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Mauro Carvalho Chehab559addc2015-02-04 06:07:30 -030062 s32 ret;
63 bool negative = false;
64
65 if (degrees > 180) {
66 negative = true;
67 degrees -= 180;
68 }
69 if (degrees > 90)
70 degrees = 180 - degrees;
71
72 ret = sin_table[degrees];
73
74 return negative ? -ret : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Mauro Carvalho Chehab559addc2015-02-04 06:07:30 -030077/**
78 * fixp_sin32() returns the sin of an angle in degrees
79 *
80 * @degrees: angle, in degrees. The angle can be positive or negative
81 *
82 * The returned value ranges from -0x7fffffff to +0x7fffffff.
83 */
84static inline s32 fixp_sin32(int degrees)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Mauro Carvalho Chehab559addc2015-02-04 06:07:30 -030086 degrees = (degrees % 360 + 360) % 360;
87
88 return __fixp_sin32(degrees);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Mauro Carvalho Chehab559addc2015-02-04 06:07:30 -030091/* cos(x) = sin(x + 90 degrees) */
92#define fixp_cos32(v) fixp_sin32((v) + 90)
93
94/*
95 * 16 bits variants
96 *
97 * The returned value ranges from -0x7fff to 0x7fff
98 */
99
100#define fixp_sin16(v) (fixp_sin32(v) >> 16)
101#define fixp_cos16(v) (fixp_cos32(v) >> 16)
102
103/**
104 * fixp_sin32_rad() - calculates the sin of an angle in radians
105 *
106 * @radians: angle, in radians
107 * @twopi: value to be used for 2*pi
108 *
109 * Provides a variant for the cases where just 360
110 * values is not enough. This function uses linear
111 * interpolation to a wider range of values given by
112 * twopi var.
113 *
114 * Experimental tests gave a maximum difference of
115 * 0.000038 between the value calculated by sin() and
116 * the one produced by this function, when twopi is
117 * equal to 360000. That seems to be enough precision
118 * for practical purposes.
119 *
120 * Please notice that two high numbers for twopi could cause
121 * overflows, so the routine will not allow values of twopi
122 * bigger than 1^18.
123 */
124static inline s32 fixp_sin32_rad(u32 radians, u32 twopi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Mauro Carvalho Chehab559addc2015-02-04 06:07:30 -0300126 int degrees;
127 s32 v1, v2, dx, dy;
128 s64 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Mauro Carvalho Chehab559addc2015-02-04 06:07:30 -0300130 /*
131 * Avoid too large values for twopi, as we don't want overflows.
132 */
133 BUG_ON(twopi > 1 << 18);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Mauro Carvalho Chehab559addc2015-02-04 06:07:30 -0300135 degrees = (radians * 360) / twopi;
136 tmp = radians - (degrees * twopi) / 360;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Mauro Carvalho Chehab559addc2015-02-04 06:07:30 -0300138 degrees = (degrees % 360 + 360) % 360;
139 v1 = __fixp_sin32(degrees);
140
141 v2 = fixp_sin32(degrees + 1);
142
143 dx = twopi / 360;
144 dy = v2 - v1;
145
146 tmp *= dy;
147
148 return v1 + div_s64(tmp, dx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Mauro Carvalho Chehab559addc2015-02-04 06:07:30 -0300151/* cos(x) = sin(x + pi/2 radians) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Mauro Carvalho Chehab559addc2015-02-04 06:07:30 -0300153#define fixp_cos32_rad(rad, twopi) \
154 fixp_sin32_rad(rad + twopi / 4, twopi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156#endif