1 1.4 riastrad /* $NetBSD: drm_fixed.h,v 1.4 2021/12/18 23:45:46 riastradh Exp $ */ 2 1.2 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2009 Red Hat Inc. 5 1.1 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice shall be included in 14 1.1 riastrad * all copies or substantial portions of the Software. 15 1.1 riastrad * 16 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 1.1 riastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 23 1.1 riastrad * 24 1.1 riastrad * Authors: Dave Airlie 25 1.2 riastrad * Christian Knig 26 1.1 riastrad */ 27 1.1 riastrad #ifndef DRM_FIXED_H 28 1.1 riastrad #define DRM_FIXED_H 29 1.1 riastrad 30 1.2 riastrad #include <linux/math64.h> 31 1.2 riastrad 32 1.1 riastrad typedef union dfixed { 33 1.1 riastrad u32 full; 34 1.1 riastrad } fixed20_12; 35 1.1 riastrad 36 1.1 riastrad 37 1.3 msaitoh #define dfixed_const(A) (((u32)(A) << 12))/* + ((B + 0.000122)*4096)) */ 38 1.1 riastrad #define dfixed_const_half(A) (u32)(((A) << 12) + 2048) 39 1.1 riastrad #define dfixed_const_666(A) (u32)(((A) << 12) + 2731) 40 1.1 riastrad #define dfixed_const_8(A) (u32)(((A) << 12) + 3277) 41 1.1 riastrad #define dfixed_mul(A, B) ((u64)((u64)(A).full * (B).full + 2048) >> 12) 42 1.1 riastrad #define dfixed_init(A) { .full = dfixed_const((A)) } 43 1.1 riastrad #define dfixed_init_half(A) { .full = dfixed_const_half((A)) } 44 1.1 riastrad #define dfixed_trunc(A) ((A).full >> 12) 45 1.1 riastrad #define dfixed_frac(A) ((A).full & ((1 << 12) - 1)) 46 1.1 riastrad 47 1.1 riastrad static inline u32 dfixed_floor(fixed20_12 A) 48 1.1 riastrad { 49 1.1 riastrad u32 non_frac = dfixed_trunc(A); 50 1.1 riastrad 51 1.1 riastrad return dfixed_const(non_frac); 52 1.1 riastrad } 53 1.1 riastrad 54 1.1 riastrad static inline u32 dfixed_ceil(fixed20_12 A) 55 1.1 riastrad { 56 1.1 riastrad u32 non_frac = dfixed_trunc(A); 57 1.1 riastrad 58 1.1 riastrad if (A.full > dfixed_const(non_frac)) 59 1.1 riastrad return dfixed_const(non_frac + 1); 60 1.1 riastrad else 61 1.1 riastrad return dfixed_const(non_frac); 62 1.1 riastrad } 63 1.1 riastrad 64 1.1 riastrad static inline u32 dfixed_div(fixed20_12 A, fixed20_12 B) 65 1.1 riastrad { 66 1.1 riastrad u64 tmp = ((u64)A.full << 13); 67 1.1 riastrad 68 1.1 riastrad do_div(tmp, B.full); 69 1.1 riastrad tmp += 1; 70 1.1 riastrad tmp /= 2; 71 1.1 riastrad return lower_32_bits(tmp); 72 1.1 riastrad } 73 1.2 riastrad 74 1.2 riastrad #define DRM_FIXED_POINT 32 75 1.2 riastrad #define DRM_FIXED_ONE (1ULL << DRM_FIXED_POINT) 76 1.2 riastrad #define DRM_FIXED_DECIMAL_MASK (DRM_FIXED_ONE - 1) 77 1.2 riastrad #define DRM_FIXED_DIGITS_MASK (~DRM_FIXED_DECIMAL_MASK) 78 1.2 riastrad #define DRM_FIXED_EPSILON 1LL 79 1.2 riastrad #define DRM_FIXED_ALMOST_ONE (DRM_FIXED_ONE - DRM_FIXED_EPSILON) 80 1.2 riastrad 81 1.2 riastrad static inline s64 drm_int2fixp(int a) 82 1.2 riastrad { 83 1.2 riastrad return ((s64)a) << DRM_FIXED_POINT; 84 1.2 riastrad } 85 1.2 riastrad 86 1.2 riastrad static inline int drm_fixp2int(s64 a) 87 1.2 riastrad { 88 1.2 riastrad return ((s64)a) >> DRM_FIXED_POINT; 89 1.2 riastrad } 90 1.2 riastrad 91 1.2 riastrad static inline int drm_fixp2int_ceil(s64 a) 92 1.2 riastrad { 93 1.2 riastrad if (a > 0) 94 1.2 riastrad return drm_fixp2int(a + DRM_FIXED_ALMOST_ONE); 95 1.2 riastrad else 96 1.2 riastrad return drm_fixp2int(a - DRM_FIXED_ALMOST_ONE); 97 1.2 riastrad } 98 1.2 riastrad 99 1.2 riastrad static inline unsigned drm_fixp_msbset(s64 a) 100 1.2 riastrad { 101 1.2 riastrad unsigned shift, sign = (a >> 63) & 1; 102 1.2 riastrad 103 1.2 riastrad for (shift = 62; shift > 0; --shift) 104 1.2 riastrad if (((a >> shift) & 1) != sign) 105 1.2 riastrad return shift; 106 1.2 riastrad 107 1.2 riastrad return 0; 108 1.2 riastrad } 109 1.2 riastrad 110 1.2 riastrad static inline s64 drm_fixp_mul(s64 a, s64 b) 111 1.2 riastrad { 112 1.2 riastrad unsigned shift = drm_fixp_msbset(a) + drm_fixp_msbset(b); 113 1.2 riastrad s64 result; 114 1.2 riastrad 115 1.2 riastrad if (shift > 61) { 116 1.2 riastrad shift = shift - 61; 117 1.2 riastrad a >>= (shift >> 1) + (shift & 1); 118 1.2 riastrad b >>= shift >> 1; 119 1.2 riastrad } else 120 1.2 riastrad shift = 0; 121 1.2 riastrad 122 1.2 riastrad result = a * b; 123 1.2 riastrad 124 1.2 riastrad if (shift > DRM_FIXED_POINT) 125 1.2 riastrad return result << (shift - DRM_FIXED_POINT); 126 1.2 riastrad 127 1.2 riastrad if (shift < DRM_FIXED_POINT) 128 1.2 riastrad return result >> (DRM_FIXED_POINT - shift); 129 1.2 riastrad 130 1.2 riastrad return result; 131 1.2 riastrad } 132 1.2 riastrad 133 1.2 riastrad static inline s64 drm_fixp_div(s64 a, s64 b) 134 1.2 riastrad { 135 1.2 riastrad unsigned shift = 62 - drm_fixp_msbset(a); 136 1.2 riastrad s64 result; 137 1.2 riastrad 138 1.2 riastrad a <<= shift; 139 1.2 riastrad 140 1.2 riastrad if (shift < DRM_FIXED_POINT) 141 1.2 riastrad b >>= (DRM_FIXED_POINT - shift); 142 1.2 riastrad 143 1.2 riastrad result = div64_s64(a, b); 144 1.2 riastrad 145 1.2 riastrad if (shift > DRM_FIXED_POINT) 146 1.2 riastrad return result >> (shift - DRM_FIXED_POINT); 147 1.2 riastrad 148 1.2 riastrad return result; 149 1.2 riastrad } 150 1.2 riastrad 151 1.2 riastrad static inline s64 drm_fixp_from_fraction(s64 a, s64 b) 152 1.2 riastrad { 153 1.2 riastrad s64 res; 154 1.2 riastrad bool a_neg = a < 0; 155 1.2 riastrad bool b_neg = b < 0; 156 1.2 riastrad u64 a_abs = a_neg ? -a : a; 157 1.2 riastrad u64 b_abs = b_neg ? -b : b; 158 1.2 riastrad u64 rem; 159 1.2 riastrad 160 1.2 riastrad /* determine integer part */ 161 1.2 riastrad u64 res_abs = div64_u64_rem(a_abs, b_abs, &rem); 162 1.2 riastrad 163 1.2 riastrad /* determine fractional part */ 164 1.2 riastrad { 165 1.2 riastrad u32 i = DRM_FIXED_POINT; 166 1.2 riastrad 167 1.2 riastrad do { 168 1.2 riastrad rem <<= 1; 169 1.2 riastrad res_abs <<= 1; 170 1.2 riastrad if (rem >= b_abs) { 171 1.2 riastrad res_abs |= 1; 172 1.2 riastrad rem -= b_abs; 173 1.2 riastrad } 174 1.2 riastrad } while (--i != 0); 175 1.2 riastrad } 176 1.2 riastrad 177 1.2 riastrad /* round up LSB */ 178 1.2 riastrad { 179 1.2 riastrad u64 summand = (rem << 1) >= b_abs; 180 1.2 riastrad 181 1.2 riastrad res_abs += summand; 182 1.2 riastrad } 183 1.2 riastrad 184 1.2 riastrad res = (s64) res_abs; 185 1.2 riastrad if (a_neg ^ b_neg) 186 1.2 riastrad res = -res; 187 1.2 riastrad return res; 188 1.2 riastrad } 189 1.2 riastrad 190 1.2 riastrad static inline s64 drm_fixp_exp(s64 x) 191 1.2 riastrad { 192 1.2 riastrad s64 tolerance = div64_s64(DRM_FIXED_ONE, 1000000); 193 1.2 riastrad s64 sum = DRM_FIXED_ONE, term, y = x; 194 1.2 riastrad u64 count = 1; 195 1.2 riastrad 196 1.2 riastrad if (x < 0) 197 1.2 riastrad y = -1 * x; 198 1.2 riastrad 199 1.2 riastrad term = y; 200 1.2 riastrad 201 1.2 riastrad while (term >= tolerance) { 202 1.2 riastrad sum = sum + term; 203 1.2 riastrad count = count + 1; 204 1.2 riastrad term = drm_fixp_mul(term, div64_s64(y, count)); 205 1.2 riastrad } 206 1.2 riastrad 207 1.2 riastrad if (x < 0) 208 1.2 riastrad sum = drm_fixp_div(DRM_FIXED_ONE, sum); 209 1.2 riastrad 210 1.2 riastrad return sum; 211 1.2 riastrad } 212 1.2 riastrad 213 1.1 riastrad #endif 214