Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: MIT */ |
| 2 | /* |
| 3 | * Copyright © 2018 Intel Corporation |
| 4 | */ |
| 5 | |
| 6 | #ifndef _I915_FIXED_H_ |
| 7 | #define _I915_FIXED_H_ |
| 8 | |
Jani Nikula | c75299a | 2019-06-26 17:40:10 +0300 | [diff] [blame] | 9 | #include <linux/bug.h> |
| 10 | #include <linux/kernel.h> |
| 11 | #include <linux/math64.h> |
| 12 | #include <linux/types.h> |
| 13 | |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 14 | typedef struct { |
Jani Nikula | 3122b9c | 2018-11-16 14:07:26 +0200 | [diff] [blame] | 15 | u32 val; |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 16 | } uint_fixed_16_16_t; |
| 17 | |
Jani Nikula | e7a2381 | 2018-11-16 14:07:27 +0200 | [diff] [blame] | 18 | #define FP_16_16_MAX ((uint_fixed_16_16_t){ .val = UINT_MAX }) |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 19 | |
| 20 | static inline bool is_fixed16_zero(uint_fixed_16_16_t val) |
| 21 | { |
Jani Nikula | f1eea89 | 2018-11-16 14:07:28 +0200 | [diff] [blame] | 22 | return val.val == 0; |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 23 | } |
| 24 | |
Jani Nikula | 3122b9c | 2018-11-16 14:07:26 +0200 | [diff] [blame] | 25 | static inline uint_fixed_16_16_t u32_to_fixed16(u32 val) |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 26 | { |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 27 | uint_fixed_16_16_t fp = { .val = val << 16 }; |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 28 | |
| 29 | WARN_ON(val > U16_MAX); |
| 30 | |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 31 | return fp; |
| 32 | } |
| 33 | |
Jani Nikula | 3122b9c | 2018-11-16 14:07:26 +0200 | [diff] [blame] | 34 | static inline u32 fixed16_to_u32_round_up(uint_fixed_16_16_t fp) |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 35 | { |
| 36 | return DIV_ROUND_UP(fp.val, 1 << 16); |
| 37 | } |
| 38 | |
Jani Nikula | 3122b9c | 2018-11-16 14:07:26 +0200 | [diff] [blame] | 39 | static inline u32 fixed16_to_u32(uint_fixed_16_16_t fp) |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 40 | { |
| 41 | return fp.val >> 16; |
| 42 | } |
| 43 | |
| 44 | static inline uint_fixed_16_16_t min_fixed16(uint_fixed_16_16_t min1, |
| 45 | uint_fixed_16_16_t min2) |
| 46 | { |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 47 | uint_fixed_16_16_t min = { .val = min(min1.val, min2.val) }; |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 48 | |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 49 | return min; |
| 50 | } |
| 51 | |
| 52 | static inline uint_fixed_16_16_t max_fixed16(uint_fixed_16_16_t max1, |
| 53 | uint_fixed_16_16_t max2) |
| 54 | { |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 55 | uint_fixed_16_16_t max = { .val = max(max1.val, max2.val) }; |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 56 | |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 57 | return max; |
| 58 | } |
| 59 | |
Jani Nikula | 3122b9c | 2018-11-16 14:07:26 +0200 | [diff] [blame] | 60 | static inline uint_fixed_16_16_t clamp_u64_to_fixed16(u64 val) |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 61 | { |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 62 | uint_fixed_16_16_t fp = { .val = (u32)val }; |
| 63 | |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 64 | WARN_ON(val > U32_MAX); |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 65 | |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 66 | return fp; |
| 67 | } |
| 68 | |
Jani Nikula | 3122b9c | 2018-11-16 14:07:26 +0200 | [diff] [blame] | 69 | static inline u32 div_round_up_fixed16(uint_fixed_16_16_t val, |
| 70 | uint_fixed_16_16_t d) |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 71 | { |
| 72 | return DIV_ROUND_UP(val.val, d.val); |
| 73 | } |
| 74 | |
Jani Nikula | 3122b9c | 2018-11-16 14:07:26 +0200 | [diff] [blame] | 75 | static inline u32 mul_round_up_u32_fixed16(u32 val, uint_fixed_16_16_t mul) |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 76 | { |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 77 | u64 tmp; |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 78 | |
Ville Syrjälä | d492a29 | 2019-04-08 18:27:01 +0300 | [diff] [blame] | 79 | tmp = mul_u32_u32(val, mul.val); |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 80 | tmp = DIV_ROUND_UP_ULL(tmp, 1 << 16); |
| 81 | WARN_ON(tmp > U32_MAX); |
| 82 | |
| 83 | return (u32)tmp; |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static inline uint_fixed_16_16_t mul_fixed16(uint_fixed_16_16_t val, |
| 87 | uint_fixed_16_16_t mul) |
| 88 | { |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 89 | u64 tmp; |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 90 | |
Ville Syrjälä | d492a29 | 2019-04-08 18:27:01 +0300 | [diff] [blame] | 91 | tmp = mul_u32_u32(val.val, mul.val); |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 92 | tmp = tmp >> 16; |
| 93 | |
| 94 | return clamp_u64_to_fixed16(tmp); |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 95 | } |
| 96 | |
Jani Nikula | 3122b9c | 2018-11-16 14:07:26 +0200 | [diff] [blame] | 97 | static inline uint_fixed_16_16_t div_fixed16(u32 val, u32 d) |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 98 | { |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 99 | u64 tmp; |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 100 | |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 101 | tmp = (u64)val << 16; |
| 102 | tmp = DIV_ROUND_UP_ULL(tmp, d); |
| 103 | |
| 104 | return clamp_u64_to_fixed16(tmp); |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Jani Nikula | 3122b9c | 2018-11-16 14:07:26 +0200 | [diff] [blame] | 107 | static inline u32 div_round_up_u32_fixed16(u32 val, uint_fixed_16_16_t d) |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 108 | { |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 109 | u64 tmp; |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 110 | |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 111 | tmp = (u64)val << 16; |
| 112 | tmp = DIV_ROUND_UP_ULL(tmp, d.val); |
| 113 | WARN_ON(tmp > U32_MAX); |
| 114 | |
| 115 | return (u32)tmp; |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 116 | } |
| 117 | |
Jani Nikula | 3122b9c | 2018-11-16 14:07:26 +0200 | [diff] [blame] | 118 | static inline uint_fixed_16_16_t mul_u32_fixed16(u32 val, uint_fixed_16_16_t mul) |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 119 | { |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 120 | u64 tmp; |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 121 | |
Ville Syrjälä | d492a29 | 2019-04-08 18:27:01 +0300 | [diff] [blame] | 122 | tmp = mul_u32_u32(val, mul.val); |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 123 | |
| 124 | return clamp_u64_to_fixed16(tmp); |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | static inline uint_fixed_16_16_t add_fixed16(uint_fixed_16_16_t add1, |
| 128 | uint_fixed_16_16_t add2) |
| 129 | { |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 130 | u64 tmp; |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 131 | |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 132 | tmp = (u64)add1.val + add2.val; |
| 133 | |
| 134 | return clamp_u64_to_fixed16(tmp); |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static inline uint_fixed_16_16_t add_fixed16_u32(uint_fixed_16_16_t add1, |
Jani Nikula | 3122b9c | 2018-11-16 14:07:26 +0200 | [diff] [blame] | 138 | u32 add2) |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 139 | { |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 140 | uint_fixed_16_16_t tmp_add2 = u32_to_fixed16(add2); |
| 141 | u64 tmp; |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 142 | |
Jani Nikula | f48cc64 | 2018-11-16 14:07:29 +0200 | [diff] [blame] | 143 | tmp = (u64)add1.val + tmp_add2.val; |
| 144 | |
| 145 | return clamp_u64_to_fixed16(tmp); |
Jani Nikula | 2d332ee | 2018-11-16 14:07:25 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | #endif /* _I915_FIXED_H_ */ |