Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

[media] fixp-arith: replace sin/cos table by a better precision one

The cos table used at fixp-arith.h has only 8 bits of precision.
That causes problems if it is reused on other drivers.

As some media drivers require a higher precision sin/cos
implementation, replace the current implementation by one that
will provide 32 bits precision.

The values generated by the new implementation matches the
32 bit precision of glibc's sin for an angle measured in
integer degrees.

It also provides support for fractional angles via linear
interpolation. On experimental calculus, when used a table
with a 0.001 degree angle, the maximum error for sin is
0.000038, which is likely good enough for practical purposes.

There are some logic there that seems to be specific to the
usage inside ff-memless.c. Move those logic to there, as they're
not needed elsewhere.

Cc: Hans de Goede <hdegoede@redhat.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
Signed-off-by: Prashant Laddha <prladdha@cisco.com>
Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>

+126 -50
+15 -3
drivers/input/ff-memless.c
··· 237 237 (force + new_force)) << 1; 238 238 } 239 239 240 + #define FRAC_N 8 241 + static inline s16 fixp_new16(s16 a) 242 + { 243 + return ((s32)a) >> (16 - FRAC_N); 244 + } 245 + 246 + static inline s16 fixp_mult(s16 a, s16 b) 247 + { 248 + a = ((s32)a * 0x100) / 0x7fff; 249 + return ((s32)(a * b)) >> FRAC_N; 250 + } 251 + 240 252 /* 241 253 * Combine two effects and apply gain. 242 254 */ ··· 259 247 struct ff_effect *new = state->effect; 260 248 unsigned int strong, weak, i; 261 249 int x, y; 262 - fixp_t level; 250 + s16 level; 263 251 264 252 switch (new->type) { 265 253 case FF_CONSTANT: ··· 267 255 level = fixp_new16(apply_envelope(state, 268 256 new->u.constant.level, 269 257 &new->u.constant.envelope)); 270 - x = fixp_mult(fixp_sin(i), level) * gain / 0xffff; 271 - y = fixp_mult(-fixp_cos(i), level) * gain / 0xffff; 258 + x = fixp_mult(fixp_sin16(i), level) * gain / 0xffff; 259 + y = fixp_mult(-fixp_cos16(i), level) * gain / 0xffff; 272 260 /* 273 261 * here we abuse ff_ramp to hold x and y of constant force 274 262 * If in future any driver wants something else than x and y
+3 -8
drivers/media/usb/gspca/ov534.c
··· 816 816 s16 huesin; 817 817 s16 huecos; 818 818 819 - /* fixp_sin and fixp_cos accept only positive values, while 820 - * our val is between -90 and 90 821 - */ 822 - val += 360; 823 - 824 819 /* According to the datasheet the registers expect HUESIN and 825 820 * HUECOS to be the result of the trigonometric functions, 826 821 * scaled by 0x80. 827 822 * 828 - * The 0x100 here represents the maximun absolute value 823 + * The 0x7fff here represents the maximum absolute value 829 824 * returned byt fixp_sin and fixp_cos, so the scaling will 830 825 * consider the result like in the interval [-1.0, 1.0]. 831 826 */ 832 - huesin = fixp_sin(val) * 0x80 / 0x100; 833 - huecos = fixp_cos(val) * 0x80 / 0x100; 827 + huesin = fixp_sin16(val) * 0x80 / 0x7fff; 828 + huecos = fixp_cos16(val) * 0x80 / 0x7fff; 834 829 835 830 if (huesin < 0) { 836 831 sccb_reg_write(gspca_dev, 0xab,
+108 -39
include/linux/fixp-arith.h
··· 1 1 #ifndef _FIXP_ARITH_H 2 2 #define _FIXP_ARITH_H 3 3 4 + #include <linux/math64.h> 5 + 4 6 /* 5 7 * Simplistic fixed-point arithmetics. 6 8 * Hmm, I'm probably duplicating some code :( ··· 31 29 32 30 #include <linux/types.h> 33 31 34 - /* The type representing fixed-point values */ 35 - typedef s16 fixp_t; 36 - 37 - #define FRAC_N 8 38 - #define FRAC_MASK ((1<<FRAC_N)-1) 39 - 40 - /* Not to be used directly. Use fixp_{cos,sin} */ 41 - static const fixp_t cos_table[46] = { 42 - 0x0100, 0x00FF, 0x00FF, 0x00FE, 0x00FD, 0x00FC, 0x00FA, 0x00F8, 43 - 0x00F6, 0x00F3, 0x00F0, 0x00ED, 0x00E9, 0x00E6, 0x00E2, 0x00DD, 44 - 0x00D9, 0x00D4, 0x00CF, 0x00C9, 0x00C4, 0x00BE, 0x00B8, 0x00B1, 45 - 0x00AB, 0x00A4, 0x009D, 0x0096, 0x008F, 0x0087, 0x0080, 0x0078, 46 - 0x0070, 0x0068, 0x005F, 0x0057, 0x004F, 0x0046, 0x003D, 0x0035, 47 - 0x002C, 0x0023, 0x001A, 0x0011, 0x0008, 0x0000 32 + static const s32 sin_table[] = { 33 + 0x00000000, 0x023be165, 0x04779632, 0x06b2f1d2, 0x08edc7b6, 0x0b27eb5c, 34 + 0x0d61304d, 0x0f996a26, 0x11d06c96, 0x14060b67, 0x163a1a7d, 0x186c6ddd, 35 + 0x1a9cd9ac, 0x1ccb3236, 0x1ef74bf2, 0x2120fb82, 0x234815ba, 0x256c6f9e, 36 + 0x278dde6e, 0x29ac379f, 0x2bc750e8, 0x2ddf003f, 0x2ff31bdd, 0x32037a44, 37 + 0x340ff241, 0x36185aee, 0x381c8bb5, 0x3a1c5c56, 0x3c17a4e7, 0x3e0e3ddb, 38 + 0x3fffffff, 0x41ecc483, 0x43d464fa, 0x45b6bb5d, 0x4793a20f, 0x496af3e1, 39 + 0x4b3c8c11, 0x4d084650, 0x4ecdfec6, 0x508d9210, 0x5246dd48, 0x53f9be04, 40 + 0x55a6125a, 0x574bb8e5, 0x58ea90c2, 0x5a827999, 0x5c135399, 0x5d9cff82, 41 + 0x5f1f5ea0, 0x609a52d1, 0x620dbe8a, 0x637984d3, 0x64dd894f, 0x6639b039, 42 + 0x678dde6d, 0x68d9f963, 0x6a1de735, 0x6b598ea1, 0x6c8cd70a, 0x6db7a879, 43 + 0x6ed9eba0, 0x6ff389de, 0x71046d3c, 0x720c8074, 0x730baeec, 0x7401e4bf, 44 + 0x74ef0ebb, 0x75d31a5f, 0x76adf5e5, 0x777f903b, 0x7847d908, 0x7906c0af, 45 + 0x79bc384c, 0x7a6831b8, 0x7b0a9f8c, 0x7ba3751c, 0x7c32a67c, 0x7cb82884, 46 + 0x7d33f0c8, 0x7da5f5a3, 0x7e0e2e31, 0x7e6c924f, 0x7ec11aa3, 0x7f0bc095, 47 + 0x7f4c7e52, 0x7f834ecf, 0x7fb02dc4, 0x7fd317b3, 0x7fec09e1, 0x7ffb025e, 48 + 0x7fffffff 48 49 }; 49 50 50 - 51 - /* a: 123 -> 123.0 */ 52 - static inline fixp_t fixp_new(s16 a) 51 + /** 52 + * __fixp_sin32() returns the sin of an angle in degrees 53 + * 54 + * @degrees: angle, in degrees, from 0 to 360. 55 + * 56 + * The returned value ranges from -0x7fffffff to +0x7fffffff. 57 + */ 58 + static inline s32 __fixp_sin32(int degrees) 53 59 { 54 - return a<<FRAC_N; 60 + s32 ret; 61 + bool negative = false; 62 + 63 + if (degrees > 180) { 64 + negative = true; 65 + degrees -= 180; 66 + } 67 + if (degrees > 90) 68 + degrees = 180 - degrees; 69 + 70 + ret = sin_table[degrees]; 71 + 72 + return negative ? -ret : ret; 55 73 } 56 74 57 - /* a: 0xFFFF -> -1.0 58 - 0x8000 -> 1.0 59 - 0x0000 -> 0.0 60 - */ 61 - static inline fixp_t fixp_new16(s16 a) 75 + /** 76 + * fixp_sin32() returns the sin of an angle in degrees 77 + * 78 + * @degrees: angle, in degrees. The angle can be positive or negative 79 + * 80 + * The returned value ranges from -0x7fffffff to +0x7fffffff. 81 + */ 82 + static inline s32 fixp_sin32(int degrees) 62 83 { 63 - return ((s32)a)>>(16-FRAC_N); 84 + degrees = (degrees % 360 + 360) % 360; 85 + 86 + return __fixp_sin32(degrees); 64 87 } 65 88 66 - static inline fixp_t fixp_cos(unsigned int degrees) 89 + /* cos(x) = sin(x + 90 degrees) */ 90 + #define fixp_cos32(v) fixp_sin32((v) + 90) 91 + 92 + /* 93 + * 16 bits variants 94 + * 95 + * The returned value ranges from -0x7fff to 0x7fff 96 + */ 97 + 98 + #define fixp_sin16(v) (fixp_sin32(v) >> 16) 99 + #define fixp_cos16(v) (fixp_cos32(v) >> 16) 100 + 101 + /** 102 + * fixp_sin32_rad() - calculates the sin of an angle in radians 103 + * 104 + * @radians: angle, in radians 105 + * @twopi: value to be used for 2*pi 106 + * 107 + * Provides a variant for the cases where just 360 108 + * values is not enough. This function uses linear 109 + * interpolation to a wider range of values given by 110 + * twopi var. 111 + * 112 + * Experimental tests gave a maximum difference of 113 + * 0.000038 between the value calculated by sin() and 114 + * the one produced by this function, when twopi is 115 + * equal to 360000. That seems to be enough precision 116 + * for practical purposes. 117 + * 118 + * Please notice that two high numbers for twopi could cause 119 + * overflows, so the routine will not allow values of twopi 120 + * bigger than 1^18. 121 + */ 122 + static inline s32 fixp_sin32_rad(u32 radians, u32 twopi) 67 123 { 68 - int quadrant = (degrees / 90) & 3; 69 - unsigned int i = degrees % 90; 124 + int degrees; 125 + s32 v1, v2, dx, dy; 126 + s64 tmp; 70 127 71 - if (quadrant == 1 || quadrant == 3) 72 - i = 90 - i; 128 + /* 129 + * Avoid too large values for twopi, as we don't want overflows. 130 + */ 131 + BUG_ON(twopi > 1 << 18); 73 132 74 - i >>= 1; 133 + degrees = (radians * 360) / twopi; 134 + tmp = radians - (degrees * twopi) / 360; 75 135 76 - return (quadrant == 1 || quadrant == 2)? -cos_table[i] : cos_table[i]; 136 + degrees = (degrees % 360 + 360) % 360; 137 + v1 = __fixp_sin32(degrees); 138 + 139 + v2 = fixp_sin32(degrees + 1); 140 + 141 + dx = twopi / 360; 142 + dy = v2 - v1; 143 + 144 + tmp *= dy; 145 + 146 + return v1 + div_s64(tmp, dx); 77 147 } 78 148 79 - static inline fixp_t fixp_sin(unsigned int degrees) 80 - { 81 - return -fixp_cos(degrees + 90); 82 - } 149 + /* cos(x) = sin(x + pi/2 radians) */ 83 150 84 - static inline fixp_t fixp_mult(fixp_t a, fixp_t b) 85 - { 86 - return ((s32)(a*b))>>FRAC_N; 87 - } 151 + #define fixp_cos32_rad(rad, twopi) \ 152 + fixp_sin32_rad(rad + twopi / 4, twopi) 88 153 89 154 #endif