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