1 /* SPDX-License-Identifier: MIT */
3 * Copyright © 2018 Intel Corporation
13 #define FP_16_16_MAX ((uint_fixed_16_16_t){ .val = UINT_MAX })
15 static inline bool is_fixed16_zero(uint_fixed_16_16_t val)
20 static inline uint_fixed_16_16_t u32_to_fixed16(u32 val)
22 uint_fixed_16_16_t fp = { .val = val << 16 };
24 WARN_ON(val > U16_MAX);
29 static inline u32 fixed16_to_u32_round_up(uint_fixed_16_16_t fp)
31 return DIV_ROUND_UP(fp.val, 1 << 16);
34 static inline u32 fixed16_to_u32(uint_fixed_16_16_t fp)
39 static inline uint_fixed_16_16_t min_fixed16(uint_fixed_16_16_t min1,
40 uint_fixed_16_16_t min2)
42 uint_fixed_16_16_t min = { .val = min(min1.val, min2.val) };
47 static inline uint_fixed_16_16_t max_fixed16(uint_fixed_16_16_t max1,
48 uint_fixed_16_16_t max2)
50 uint_fixed_16_16_t max = { .val = max(max1.val, max2.val) };
55 static inline uint_fixed_16_16_t clamp_u64_to_fixed16(u64 val)
57 uint_fixed_16_16_t fp = { .val = (u32)val };
59 WARN_ON(val > U32_MAX);
64 static inline u32 div_round_up_fixed16(uint_fixed_16_16_t val,
67 return DIV_ROUND_UP(val.val, d.val);
70 static inline u32 mul_round_up_u32_fixed16(u32 val, uint_fixed_16_16_t mul)
74 tmp = (u64)val * mul.val;
75 tmp = DIV_ROUND_UP_ULL(tmp, 1 << 16);
76 WARN_ON(tmp > U32_MAX);
81 static inline uint_fixed_16_16_t mul_fixed16(uint_fixed_16_16_t val,
82 uint_fixed_16_16_t mul)
86 tmp = (u64)val.val * mul.val;
89 return clamp_u64_to_fixed16(tmp);
92 static inline uint_fixed_16_16_t div_fixed16(u32 val, u32 d)
97 tmp = DIV_ROUND_UP_ULL(tmp, d);
99 return clamp_u64_to_fixed16(tmp);
102 static inline u32 div_round_up_u32_fixed16(u32 val, uint_fixed_16_16_t d)
106 tmp = (u64)val << 16;
107 tmp = DIV_ROUND_UP_ULL(tmp, d.val);
108 WARN_ON(tmp > U32_MAX);
113 static inline uint_fixed_16_16_t mul_u32_fixed16(u32 val, uint_fixed_16_16_t mul)
117 tmp = (u64)val * mul.val;
119 return clamp_u64_to_fixed16(tmp);
122 static inline uint_fixed_16_16_t add_fixed16(uint_fixed_16_16_t add1,
123 uint_fixed_16_16_t add2)
127 tmp = (u64)add1.val + add2.val;
129 return clamp_u64_to_fixed16(tmp);
132 static inline uint_fixed_16_16_t add_fixed16_u32(uint_fixed_16_16_t add1,
135 uint_fixed_16_16_t tmp_add2 = u32_to_fixed16(add2);
138 tmp = (u64)add1.val + tmp_add2.val;
140 return clamp_u64_to_fixed16(tmp);
143 #endif /* _I915_FIXED_H_ */