Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / i915_fixed.h
bloba327094de2bd7e5d3576d9888cfae8ea40549fdb
1 /* SPDX-License-Identifier: MIT */
2 /*
3 * Copyright © 2018 Intel Corporation
4 */
6 #ifndef _I915_FIXED_H_
7 #define _I915_FIXED_H_
9 #include <linux/bug.h>
10 #include <linux/kernel.h>
11 #include <linux/math64.h>
12 #include <linux/types.h>
14 typedef struct {
15 u32 val;
16 } uint_fixed_16_16_t;
18 #define FP_16_16_MAX ((uint_fixed_16_16_t){ .val = UINT_MAX })
20 static inline bool is_fixed16_zero(uint_fixed_16_16_t val)
22 return val.val == 0;
25 static inline uint_fixed_16_16_t u32_to_fixed16(u32 val)
27 uint_fixed_16_16_t fp = { .val = val << 16 };
29 WARN_ON(val > U16_MAX);
31 return fp;
34 static inline u32 fixed16_to_u32_round_up(uint_fixed_16_16_t fp)
36 return DIV_ROUND_UP(fp.val, 1 << 16);
39 static inline u32 fixed16_to_u32(uint_fixed_16_16_t fp)
41 return fp.val >> 16;
44 static inline uint_fixed_16_16_t min_fixed16(uint_fixed_16_16_t min1,
45 uint_fixed_16_16_t min2)
47 uint_fixed_16_16_t min = { .val = min(min1.val, min2.val) };
49 return min;
52 static inline uint_fixed_16_16_t max_fixed16(uint_fixed_16_16_t max1,
53 uint_fixed_16_16_t max2)
55 uint_fixed_16_16_t max = { .val = max(max1.val, max2.val) };
57 return max;
60 static inline uint_fixed_16_16_t clamp_u64_to_fixed16(u64 val)
62 uint_fixed_16_16_t fp = { .val = (u32)val };
64 WARN_ON(val > U32_MAX);
66 return fp;
69 static inline u32 div_round_up_fixed16(uint_fixed_16_16_t val,
70 uint_fixed_16_16_t d)
72 return DIV_ROUND_UP(val.val, d.val);
75 static inline u32 mul_round_up_u32_fixed16(u32 val, uint_fixed_16_16_t mul)
77 u64 tmp;
79 tmp = mul_u32_u32(val, mul.val);
80 tmp = DIV_ROUND_UP_ULL(tmp, 1 << 16);
81 WARN_ON(tmp > U32_MAX);
83 return (u32)tmp;
86 static inline uint_fixed_16_16_t mul_fixed16(uint_fixed_16_16_t val,
87 uint_fixed_16_16_t mul)
89 u64 tmp;
91 tmp = mul_u32_u32(val.val, mul.val);
92 tmp = tmp >> 16;
94 return clamp_u64_to_fixed16(tmp);
97 static inline uint_fixed_16_16_t div_fixed16(u32 val, u32 d)
99 u64 tmp;
101 tmp = (u64)val << 16;
102 tmp = DIV_ROUND_UP_ULL(tmp, d);
104 return clamp_u64_to_fixed16(tmp);
107 static inline u32 div_round_up_u32_fixed16(u32 val, uint_fixed_16_16_t d)
109 u64 tmp;
111 tmp = (u64)val << 16;
112 tmp = DIV_ROUND_UP_ULL(tmp, d.val);
113 WARN_ON(tmp > U32_MAX);
115 return (u32)tmp;
118 static inline uint_fixed_16_16_t mul_u32_fixed16(u32 val, uint_fixed_16_16_t mul)
120 u64 tmp;
122 tmp = mul_u32_u32(val, mul.val);
124 return clamp_u64_to_fixed16(tmp);
127 static inline uint_fixed_16_16_t add_fixed16(uint_fixed_16_16_t add1,
128 uint_fixed_16_16_t add2)
130 u64 tmp;
132 tmp = (u64)add1.val + add2.val;
134 return clamp_u64_to_fixed16(tmp);
137 static inline uint_fixed_16_16_t add_fixed16_u32(uint_fixed_16_16_t add1,
138 u32 add2)
140 uint_fixed_16_16_t tmp_add2 = u32_to_fixed16(add2);
141 u64 tmp;
143 tmp = (u64)add1.val + tmp_add2.val;
145 return clamp_u64_to_fixed16(tmp);
148 #endif /* _I915_FIXED_H_ */