1 /* $NetBSD: bw_fixed.h,v 1.2 2021/12/18 23:45:05 riastradh Exp $ */
2
3 /*
4 * Copyright 2015 Advanced Micro Devices, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: AMD
25 *
26 */
27
28 #ifndef BW_FIXED_H_
29 #define BW_FIXED_H_
30
31 #define BW_FIXED_BITS_PER_FRACTIONAL_PART 24
32
33 #define BW_FIXED_GET_INTEGER_PART(x) ((x) >> BW_FIXED_BITS_PER_FRACTIONAL_PART)
34 struct bw_fixed {
35 int64_t value;
36 };
37
38 #define BW_FIXED_MIN_I32 \
39 (int64_t)(-(1LL << (63 - BW_FIXED_BITS_PER_FRACTIONAL_PART)))
40
41 #define BW_FIXED_MAX_I32 \
42 (int64_t)((1ULL << (63 - BW_FIXED_BITS_PER_FRACTIONAL_PART)) - 1)
43
bw_min2(const struct bw_fixed arg1,const struct bw_fixed arg2)44 static inline struct bw_fixed bw_min2(const struct bw_fixed arg1,
45 const struct bw_fixed arg2)
46 {
47 return (arg1.value <= arg2.value) ? arg1 : arg2;
48 }
49
bw_max2(const struct bw_fixed arg1,const struct bw_fixed arg2)50 static inline struct bw_fixed bw_max2(const struct bw_fixed arg1,
51 const struct bw_fixed arg2)
52 {
53 return (arg2.value <= arg1.value) ? arg1 : arg2;
54 }
55
bw_min3(struct bw_fixed v1,struct bw_fixed v2,struct bw_fixed v3)56 static inline struct bw_fixed bw_min3(struct bw_fixed v1,
57 struct bw_fixed v2,
58 struct bw_fixed v3)
59 {
60 return bw_min2(bw_min2(v1, v2), v3);
61 }
62
bw_max3(struct bw_fixed v1,struct bw_fixed v2,struct bw_fixed v3)63 static inline struct bw_fixed bw_max3(struct bw_fixed v1,
64 struct bw_fixed v2,
65 struct bw_fixed v3)
66 {
67 return bw_max2(bw_max2(v1, v2), v3);
68 }
69
70 struct bw_fixed bw_int_to_fixed_nonconst(int64_t value);
bw_int_to_fixed(int64_t value)71 static inline struct bw_fixed bw_int_to_fixed(int64_t value)
72 {
73 if (__builtin_constant_p(value)) {
74 struct bw_fixed res;
75 BUILD_BUG_ON(value > BW_FIXED_MAX_I32 || value < BW_FIXED_MIN_I32);
76 res.value = value << BW_FIXED_BITS_PER_FRACTIONAL_PART;
77 return res;
78 } else
79 return bw_int_to_fixed_nonconst(value);
80 }
81
bw_fixed_to_int(struct bw_fixed value)82 static inline int32_t bw_fixed_to_int(struct bw_fixed value)
83 {
84 return BW_FIXED_GET_INTEGER_PART(value.value);
85 }
86
87 struct bw_fixed bw_frc_to_fixed(int64_t num, int64_t denum);
88
fixed31_32_to_bw_fixed(int64_t raw)89 static inline struct bw_fixed fixed31_32_to_bw_fixed(int64_t raw)
90 {
91 struct bw_fixed result = { 0 };
92
93 if (raw < 0) {
94 raw = -raw;
95 result.value = -(raw >> (32 - BW_FIXED_BITS_PER_FRACTIONAL_PART));
96 } else {
97 result.value = raw >> (32 - BW_FIXED_BITS_PER_FRACTIONAL_PART);
98 }
99
100 return result;
101 }
102
bw_add(const struct bw_fixed arg1,const struct bw_fixed arg2)103 static inline struct bw_fixed bw_add(const struct bw_fixed arg1,
104 const struct bw_fixed arg2)
105 {
106 struct bw_fixed res;
107
108 res.value = arg1.value + arg2.value;
109
110 return res;
111 }
112
bw_sub(const struct bw_fixed arg1,const struct bw_fixed arg2)113 static inline struct bw_fixed bw_sub(const struct bw_fixed arg1, const struct bw_fixed arg2)
114 {
115 struct bw_fixed res;
116
117 res.value = arg1.value - arg2.value;
118
119 return res;
120 }
121
122 struct bw_fixed bw_mul(const struct bw_fixed arg1, const struct bw_fixed arg2);
bw_div(const struct bw_fixed arg1,const struct bw_fixed arg2)123 static inline struct bw_fixed bw_div(const struct bw_fixed arg1, const struct bw_fixed arg2)
124 {
125 return bw_frc_to_fixed(arg1.value, arg2.value);
126 }
127
bw_mod(const struct bw_fixed arg1,const struct bw_fixed arg2)128 static inline struct bw_fixed bw_mod(const struct bw_fixed arg1, const struct bw_fixed arg2)
129 {
130 struct bw_fixed res;
131 div64_u64_rem(arg1.value, arg2.value, (uint64_t *)&res.value);
132 return res;
133 }
134
135 struct bw_fixed bw_floor2(const struct bw_fixed arg, const struct bw_fixed significance);
136 struct bw_fixed bw_ceil2(const struct bw_fixed arg, const struct bw_fixed significance);
137
bw_equ(const struct bw_fixed arg1,const struct bw_fixed arg2)138 static inline bool bw_equ(const struct bw_fixed arg1, const struct bw_fixed arg2)
139 {
140 return arg1.value == arg2.value;
141 }
142
bw_neq(const struct bw_fixed arg1,const struct bw_fixed arg2)143 static inline bool bw_neq(const struct bw_fixed arg1, const struct bw_fixed arg2)
144 {
145 return arg1.value != arg2.value;
146 }
147
bw_leq(const struct bw_fixed arg1,const struct bw_fixed arg2)148 static inline bool bw_leq(const struct bw_fixed arg1, const struct bw_fixed arg2)
149 {
150 return arg1.value <= arg2.value;
151 }
152
bw_meq(const struct bw_fixed arg1,const struct bw_fixed arg2)153 static inline bool bw_meq(const struct bw_fixed arg1, const struct bw_fixed arg2)
154 {
155 return arg1.value >= arg2.value;
156 }
157
bw_ltn(const struct bw_fixed arg1,const struct bw_fixed arg2)158 static inline bool bw_ltn(const struct bw_fixed arg1, const struct bw_fixed arg2)
159 {
160 return arg1.value < arg2.value;
161 }
162
bw_mtn(const struct bw_fixed arg1,const struct bw_fixed arg2)163 static inline bool bw_mtn(const struct bw_fixed arg1, const struct bw_fixed arg2)
164 {
165 return arg1.value > arg2.value;
166 }
167
168 #endif //BW_FIXED_H_
169