168adac5eSBen Skeggs /*
268adac5eSBen Skeggs * Copyright 2009 Red Hat Inc.
368adac5eSBen Skeggs *
468adac5eSBen Skeggs * Permission is hereby granted, free of charge, to any person obtaining a
568adac5eSBen Skeggs * copy of this software and associated documentation files (the "Software"),
668adac5eSBen Skeggs * to deal in the Software without restriction, including without limitation
768adac5eSBen Skeggs * the rights to use, copy, modify, merge, publish, distribute, sublicense,
868adac5eSBen Skeggs * and/or sell copies of the Software, and to permit persons to whom the
968adac5eSBen Skeggs * Software is furnished to do so, subject to the following conditions:
1068adac5eSBen Skeggs *
1168adac5eSBen Skeggs * The above copyright notice and this permission notice shall be included in
1268adac5eSBen Skeggs * all copies or substantial portions of the Software.
1368adac5eSBen Skeggs *
1468adac5eSBen Skeggs * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1568adac5eSBen Skeggs * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1668adac5eSBen Skeggs * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1768adac5eSBen Skeggs * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1868adac5eSBen Skeggs * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1968adac5eSBen Skeggs * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2068adac5eSBen Skeggs * OTHER DEALINGS IN THE SOFTWARE.
2168adac5eSBen Skeggs *
2268adac5eSBen Skeggs * Authors: Dave Airlie
23210a0b9eSAlex Deucher * Christian König
2468adac5eSBen Skeggs */
2568adac5eSBen Skeggs #ifndef DRM_FIXED_H
2668adac5eSBen Skeggs #define DRM_FIXED_H
2768adac5eSBen Skeggs
28cde3d37bSThomas Zimmermann #include <linux/kernel.h>
29210a0b9eSAlex Deucher #include <linux/math64.h>
30210a0b9eSAlex Deucher
3168adac5eSBen Skeggs typedef union dfixed {
3268adac5eSBen Skeggs u32 full;
3368adac5eSBen Skeggs } fixed20_12;
3468adac5eSBen Skeggs
3568adac5eSBen Skeggs
3668adac5eSBen Skeggs #define dfixed_const(A) (u32)(((A) << 12))/* + ((B + 0.000122)*4096)) */
3768adac5eSBen Skeggs #define dfixed_const_half(A) (u32)(((A) << 12) + 2048)
3868adac5eSBen Skeggs #define dfixed_const_666(A) (u32)(((A) << 12) + 2731)
3968adac5eSBen Skeggs #define dfixed_const_8(A) (u32)(((A) << 12) + 3277)
4068adac5eSBen Skeggs #define dfixed_mul(A, B) ((u64)((u64)(A).full * (B).full + 2048) >> 12)
4168adac5eSBen Skeggs #define dfixed_init(A) { .full = dfixed_const((A)) }
4268adac5eSBen Skeggs #define dfixed_init_half(A) { .full = dfixed_const_half((A)) }
4368adac5eSBen Skeggs #define dfixed_trunc(A) ((A).full >> 12)
44f7f6c340SRobert Morell #define dfixed_frac(A) ((A).full & ((1 << 12) - 1))
4568adac5eSBen Skeggs
dfixed_floor(fixed20_12 A)4668adac5eSBen Skeggs static inline u32 dfixed_floor(fixed20_12 A)
4768adac5eSBen Skeggs {
4868adac5eSBen Skeggs u32 non_frac = dfixed_trunc(A);
4968adac5eSBen Skeggs
5068adac5eSBen Skeggs return dfixed_const(non_frac);
5168adac5eSBen Skeggs }
5268adac5eSBen Skeggs
dfixed_ceil(fixed20_12 A)5368adac5eSBen Skeggs static inline u32 dfixed_ceil(fixed20_12 A)
5468adac5eSBen Skeggs {
5568adac5eSBen Skeggs u32 non_frac = dfixed_trunc(A);
5668adac5eSBen Skeggs
5768adac5eSBen Skeggs if (A.full > dfixed_const(non_frac))
5868adac5eSBen Skeggs return dfixed_const(non_frac + 1);
5968adac5eSBen Skeggs else
6068adac5eSBen Skeggs return dfixed_const(non_frac);
6168adac5eSBen Skeggs }
6268adac5eSBen Skeggs
dfixed_div(fixed20_12 A,fixed20_12 B)6368adac5eSBen Skeggs static inline u32 dfixed_div(fixed20_12 A, fixed20_12 B)
6468adac5eSBen Skeggs {
6568adac5eSBen Skeggs u64 tmp = ((u64)A.full << 13);
6668adac5eSBen Skeggs
6768adac5eSBen Skeggs do_div(tmp, B.full);
6868adac5eSBen Skeggs tmp += 1;
6968adac5eSBen Skeggs tmp /= 2;
7068adac5eSBen Skeggs return lower_32_bits(tmp);
7168adac5eSBen Skeggs }
72210a0b9eSAlex Deucher
73210a0b9eSAlex Deucher #define DRM_FIXED_POINT 32
74210a0b9eSAlex Deucher #define DRM_FIXED_ONE (1ULL << DRM_FIXED_POINT)
75210a0b9eSAlex Deucher #define DRM_FIXED_DECIMAL_MASK (DRM_FIXED_ONE - 1)
76210a0b9eSAlex Deucher #define DRM_FIXED_DIGITS_MASK (~DRM_FIXED_DECIMAL_MASK)
7764566b5eSHarry Wentland #define DRM_FIXED_EPSILON 1LL
7864566b5eSHarry Wentland #define DRM_FIXED_ALMOST_ONE (DRM_FIXED_ONE - DRM_FIXED_EPSILON)
79210a0b9eSAlex Deucher
drm_int2fixp(int a)80210a0b9eSAlex Deucher static inline s64 drm_int2fixp(int a)
81210a0b9eSAlex Deucher {
82210a0b9eSAlex Deucher return ((s64)a) << DRM_FIXED_POINT;
83210a0b9eSAlex Deucher }
84210a0b9eSAlex Deucher
drm_fixp2int(s64 a)8564566b5eSHarry Wentland static inline int drm_fixp2int(s64 a)
86210a0b9eSAlex Deucher {
87210a0b9eSAlex Deucher return ((s64)a) >> DRM_FIXED_POINT;
88210a0b9eSAlex Deucher }
89210a0b9eSAlex Deucher
drm_fixp2int_round(s64 a)908b253208SMaíra Canal static inline int drm_fixp2int_round(s64 a)
918b253208SMaíra Canal {
92*296e6678SArthur Grillo return drm_fixp2int(a + DRM_FIXED_ONE / 2);
938b253208SMaíra Canal }
948b253208SMaíra Canal
drm_fixp2int_ceil(s64 a)9564566b5eSHarry Wentland static inline int drm_fixp2int_ceil(s64 a)
9664566b5eSHarry Wentland {
97bac3d37dSHarry Wentland if (a >= 0)
9864566b5eSHarry Wentland return drm_fixp2int(a + DRM_FIXED_ALMOST_ONE);
9964566b5eSHarry Wentland else
10064566b5eSHarry Wentland return drm_fixp2int(a - DRM_FIXED_ALMOST_ONE);
10164566b5eSHarry Wentland }
10264566b5eSHarry Wentland
drm_fixp_msbset(s64 a)10364566b5eSHarry Wentland static inline unsigned drm_fixp_msbset(s64 a)
104210a0b9eSAlex Deucher {
105210a0b9eSAlex Deucher unsigned shift, sign = (a >> 63) & 1;
106210a0b9eSAlex Deucher
107210a0b9eSAlex Deucher for (shift = 62; shift > 0; --shift)
108a838834bSAlex Deucher if (((a >> shift) & 1) != sign)
109210a0b9eSAlex Deucher return shift;
110210a0b9eSAlex Deucher
111210a0b9eSAlex Deucher return 0;
112210a0b9eSAlex Deucher }
113210a0b9eSAlex Deucher
drm_fixp_mul(s64 a,s64 b)114210a0b9eSAlex Deucher static inline s64 drm_fixp_mul(s64 a, s64 b)
115210a0b9eSAlex Deucher {
116210a0b9eSAlex Deucher unsigned shift = drm_fixp_msbset(a) + drm_fixp_msbset(b);
117210a0b9eSAlex Deucher s64 result;
118210a0b9eSAlex Deucher
119a838834bSAlex Deucher if (shift > 61) {
120a838834bSAlex Deucher shift = shift - 61;
121a838834bSAlex Deucher a >>= (shift >> 1) + (shift & 1);
122210a0b9eSAlex Deucher b >>= shift >> 1;
123210a0b9eSAlex Deucher } else
124210a0b9eSAlex Deucher shift = 0;
125210a0b9eSAlex Deucher
126210a0b9eSAlex Deucher result = a * b;
127210a0b9eSAlex Deucher
128210a0b9eSAlex Deucher if (shift > DRM_FIXED_POINT)
129210a0b9eSAlex Deucher return result << (shift - DRM_FIXED_POINT);
130210a0b9eSAlex Deucher
131210a0b9eSAlex Deucher if (shift < DRM_FIXED_POINT)
132210a0b9eSAlex Deucher return result >> (DRM_FIXED_POINT - shift);
133210a0b9eSAlex Deucher
134210a0b9eSAlex Deucher return result;
135210a0b9eSAlex Deucher }
136210a0b9eSAlex Deucher
drm_fixp_div(s64 a,s64 b)137210a0b9eSAlex Deucher static inline s64 drm_fixp_div(s64 a, s64 b)
138210a0b9eSAlex Deucher {
139a838834bSAlex Deucher unsigned shift = 62 - drm_fixp_msbset(a);
140210a0b9eSAlex Deucher s64 result;
141210a0b9eSAlex Deucher
142210a0b9eSAlex Deucher a <<= shift;
143210a0b9eSAlex Deucher
144210a0b9eSAlex Deucher if (shift < DRM_FIXED_POINT)
145210a0b9eSAlex Deucher b >>= (DRM_FIXED_POINT - shift);
146210a0b9eSAlex Deucher
147210a0b9eSAlex Deucher result = div64_s64(a, b);
148210a0b9eSAlex Deucher
149210a0b9eSAlex Deucher if (shift > DRM_FIXED_POINT)
150210a0b9eSAlex Deucher return result >> (shift - DRM_FIXED_POINT);
151210a0b9eSAlex Deucher
152210a0b9eSAlex Deucher return result;
153210a0b9eSAlex Deucher }
154210a0b9eSAlex Deucher
drm_fixp_from_fraction(s64 a,s64 b)15564566b5eSHarry Wentland static inline s64 drm_fixp_from_fraction(s64 a, s64 b)
15664566b5eSHarry Wentland {
15764566b5eSHarry Wentland s64 res;
15864566b5eSHarry Wentland bool a_neg = a < 0;
15964566b5eSHarry Wentland bool b_neg = b < 0;
16064566b5eSHarry Wentland u64 a_abs = a_neg ? -a : a;
16164566b5eSHarry Wentland u64 b_abs = b_neg ? -b : b;
16264566b5eSHarry Wentland u64 rem;
16364566b5eSHarry Wentland
16464566b5eSHarry Wentland /* determine integer part */
16564566b5eSHarry Wentland u64 res_abs = div64_u64_rem(a_abs, b_abs, &rem);
16664566b5eSHarry Wentland
16764566b5eSHarry Wentland /* determine fractional part */
16864566b5eSHarry Wentland {
16964566b5eSHarry Wentland u32 i = DRM_FIXED_POINT;
17064566b5eSHarry Wentland
17164566b5eSHarry Wentland do {
17264566b5eSHarry Wentland rem <<= 1;
17364566b5eSHarry Wentland res_abs <<= 1;
17464566b5eSHarry Wentland if (rem >= b_abs) {
17564566b5eSHarry Wentland res_abs |= 1;
17664566b5eSHarry Wentland rem -= b_abs;
17764566b5eSHarry Wentland }
17864566b5eSHarry Wentland } while (--i != 0);
17964566b5eSHarry Wentland }
18064566b5eSHarry Wentland
18164566b5eSHarry Wentland /* round up LSB */
18264566b5eSHarry Wentland {
18364566b5eSHarry Wentland u64 summand = (rem << 1) >= b_abs;
18464566b5eSHarry Wentland
18564566b5eSHarry Wentland res_abs += summand;
18664566b5eSHarry Wentland }
18764566b5eSHarry Wentland
18864566b5eSHarry Wentland res = (s64) res_abs;
18964566b5eSHarry Wentland if (a_neg ^ b_neg)
19064566b5eSHarry Wentland res = -res;
19164566b5eSHarry Wentland return res;
19264566b5eSHarry Wentland }
19364566b5eSHarry Wentland
drm_fixp_exp(s64 x)194210a0b9eSAlex Deucher static inline s64 drm_fixp_exp(s64 x)
195210a0b9eSAlex Deucher {
196210a0b9eSAlex Deucher s64 tolerance = div64_s64(DRM_FIXED_ONE, 1000000);
197210a0b9eSAlex Deucher s64 sum = DRM_FIXED_ONE, term, y = x;
198210a0b9eSAlex Deucher u64 count = 1;
199210a0b9eSAlex Deucher
200210a0b9eSAlex Deucher if (x < 0)
201210a0b9eSAlex Deucher y = -1 * x;
202210a0b9eSAlex Deucher
203210a0b9eSAlex Deucher term = y;
204210a0b9eSAlex Deucher
205210a0b9eSAlex Deucher while (term >= tolerance) {
206210a0b9eSAlex Deucher sum = sum + term;
207210a0b9eSAlex Deucher count = count + 1;
208210a0b9eSAlex Deucher term = drm_fixp_mul(term, div64_s64(y, count));
209210a0b9eSAlex Deucher }
210210a0b9eSAlex Deucher
211210a0b9eSAlex Deucher if (x < 0)
212a838834bSAlex Deucher sum = drm_fixp_div(DRM_FIXED_ONE, sum);
213210a0b9eSAlex Deucher
214210a0b9eSAlex Deucher return sum;
215210a0b9eSAlex Deucher }
216210a0b9eSAlex Deucher
21768adac5eSBen Skeggs #endif
218