1 /* SPDX-License-Identifier: MIT */
3 * Copyright © 2018 Intel Corporation
10 #include <linux/kernel.h>
11 #include <linux/math64.h>
12 #include <linux/types.h>
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
)
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
);
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
)
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
) };
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
) };
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
);
69 static inline u32
div_round_up_fixed16(uint_fixed_16_16_t val
,
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
)
79 tmp
= mul_u32_u32(val
, mul
.val
);
80 tmp
= DIV_ROUND_UP_ULL(tmp
, 1 << 16);
81 WARN_ON(tmp
> U32_MAX
);
86 static inline uint_fixed_16_16_t
mul_fixed16(uint_fixed_16_16_t val
,
87 uint_fixed_16_16_t mul
)
91 tmp
= mul_u32_u32(val
.val
, mul
.val
);
94 return clamp_u64_to_fixed16(tmp
);
97 static inline uint_fixed_16_16_t
div_fixed16(u32 val
, u32 d
)
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
)
111 tmp
= (u64
)val
<< 16;
112 tmp
= DIV_ROUND_UP_ULL(tmp
, d
.val
);
113 WARN_ON(tmp
> U32_MAX
);
118 static inline uint_fixed_16_16_t
mul_u32_fixed16(u32 val
, uint_fixed_16_16_t mul
)
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
)
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
,
140 uint_fixed_16_16_t tmp_add2
= u32_to_fixed16(add2
);
143 tmp
= (u64
)add1
.val
+ tmp_add2
.val
;
145 return clamp_u64_to_fixed16(tmp
);
148 #endif /* _I915_FIXED_H_ */