2 * Copyright 2015 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
29 #define BW_FIXED_BITS_PER_FRACTIONAL_PART 24
31 #define BW_FIXED_GET_INTEGER_PART(x) ((x) >> BW_FIXED_BITS_PER_FRACTIONAL_PART)
36 #define BW_FIXED_MIN_I32 \
37 (int64_t)(-(1LL << (63 - BW_FIXED_BITS_PER_FRACTIONAL_PART)))
39 #define BW_FIXED_MAX_I32 \
40 (int64_t)((1ULL << (63 - BW_FIXED_BITS_PER_FRACTIONAL_PART)) - 1)
42 static inline struct bw_fixed
bw_min2(const struct bw_fixed arg1
,
43 const struct bw_fixed arg2
)
45 return (arg1
.value
<= arg2
.value
) ? arg1
: arg2
;
48 static inline struct bw_fixed
bw_max2(const struct bw_fixed arg1
,
49 const struct bw_fixed arg2
)
51 return (arg2
.value
<= arg1
.value
) ? arg1
: arg2
;
54 static inline struct bw_fixed
bw_min3(struct bw_fixed v1
,
58 return bw_min2(bw_min2(v1
, v2
), v3
);
61 static inline struct bw_fixed
bw_max3(struct bw_fixed v1
,
65 return bw_max2(bw_max2(v1
, v2
), v3
);
68 struct bw_fixed
bw_int_to_fixed_nonconst(int64_t value
);
69 static inline struct bw_fixed
bw_int_to_fixed(int64_t value
)
71 if (__builtin_constant_p(value
)) {
73 BUILD_BUG_ON(value
> BW_FIXED_MAX_I32
|| value
< BW_FIXED_MIN_I32
);
74 res
.value
= value
<< BW_FIXED_BITS_PER_FRACTIONAL_PART
;
77 return bw_int_to_fixed_nonconst(value
);
80 static inline int32_t bw_fixed_to_int(struct bw_fixed value
)
82 return BW_FIXED_GET_INTEGER_PART(value
.value
);
85 struct bw_fixed
bw_frc_to_fixed(int64_t num
, int64_t denum
);
87 static inline struct bw_fixed
fixed31_32_to_bw_fixed(int64_t raw
)
89 struct bw_fixed result
= { 0 };
93 result
.value
= -(raw
>> (32 - BW_FIXED_BITS_PER_FRACTIONAL_PART
));
95 result
.value
= raw
>> (32 - BW_FIXED_BITS_PER_FRACTIONAL_PART
);
101 static inline struct bw_fixed
bw_add(const struct bw_fixed arg1
,
102 const struct bw_fixed arg2
)
106 res
.value
= arg1
.value
+ arg2
.value
;
111 static inline struct bw_fixed
bw_sub(const struct bw_fixed arg1
, const struct bw_fixed arg2
)
115 res
.value
= arg1
.value
- arg2
.value
;
120 struct bw_fixed
bw_mul(const struct bw_fixed arg1
, const struct bw_fixed arg2
);
121 static inline struct bw_fixed
bw_div(const struct bw_fixed arg1
, const struct bw_fixed arg2
)
123 return bw_frc_to_fixed(arg1
.value
, arg2
.value
);
126 static inline struct bw_fixed
bw_mod(const struct bw_fixed arg1
, const struct bw_fixed arg2
)
129 div64_u64_rem(arg1
.value
, arg2
.value
, (uint64_t *)&res
.value
);
133 struct bw_fixed
bw_floor2(const struct bw_fixed arg
, const struct bw_fixed significance
);
134 struct bw_fixed
bw_ceil2(const struct bw_fixed arg
, const struct bw_fixed significance
);
136 static inline bool bw_equ(const struct bw_fixed arg1
, const struct bw_fixed arg2
)
138 return arg1
.value
== arg2
.value
;
141 static inline bool bw_neq(const struct bw_fixed arg1
, const struct bw_fixed arg2
)
143 return arg1
.value
!= arg2
.value
;
146 static inline bool bw_leq(const struct bw_fixed arg1
, const struct bw_fixed arg2
)
148 return arg1
.value
<= arg2
.value
;
151 static inline bool bw_meq(const struct bw_fixed arg1
, const struct bw_fixed arg2
)
153 return arg1
.value
>= arg2
.value
;
156 static inline bool bw_ltn(const struct bw_fixed arg1
, const struct bw_fixed arg2
)
158 return arg1
.value
< arg2
.value
;
161 static inline bool bw_mtn(const struct bw_fixed arg1
, const struct bw_fixed arg2
)
163 return arg1
.value
> arg2
.value
;