1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev * Copyright 2015 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev *
4b843c749SSergey Zigachev * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev *
11b843c749SSergey Zigachev * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev *
14b843c749SSergey Zigachev * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17b843c749SSergey Zigachev * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev *
22b843c749SSergey Zigachev * Authors: AMD
23b843c749SSergey Zigachev *
24b843c749SSergey Zigachev */
25b843c749SSergey Zigachev #include "dm_services.h"
26b843c749SSergey Zigachev #include "bw_fixed.h"
27b843c749SSergey Zigachev
28b843c749SSergey Zigachev
29b843c749SSergey Zigachev #define MIN_I64 \
30b843c749SSergey Zigachev (int64_t)(-(1LL << 63))
31b843c749SSergey Zigachev
32b843c749SSergey Zigachev #define MAX_I64 \
33b843c749SSergey Zigachev (int64_t)((1ULL << 63) - 1)
34b843c749SSergey Zigachev
35b843c749SSergey Zigachev #define FRACTIONAL_PART_MASK \
36b843c749SSergey Zigachev ((1ULL << BW_FIXED_BITS_PER_FRACTIONAL_PART) - 1)
37b843c749SSergey Zigachev
38b843c749SSergey Zigachev #define GET_FRACTIONAL_PART(x) \
39b843c749SSergey Zigachev (FRACTIONAL_PART_MASK & (x))
40b843c749SSergey Zigachev
abs_i64(int64_t arg)41b843c749SSergey Zigachev static uint64_t abs_i64(int64_t arg)
42b843c749SSergey Zigachev {
43b843c749SSergey Zigachev if (arg >= 0)
44b843c749SSergey Zigachev return (uint64_t)(arg);
45b843c749SSergey Zigachev else
46b843c749SSergey Zigachev return (uint64_t)(-arg);
47b843c749SSergey Zigachev }
48b843c749SSergey Zigachev
bw_int_to_fixed_nonconst(int64_t value)49b843c749SSergey Zigachev struct bw_fixed bw_int_to_fixed_nonconst(int64_t value)
50b843c749SSergey Zigachev {
51b843c749SSergey Zigachev struct bw_fixed res;
52b843c749SSergey Zigachev ASSERT(value < BW_FIXED_MAX_I32 && value > BW_FIXED_MIN_I32);
53b843c749SSergey Zigachev res.value = value << BW_FIXED_BITS_PER_FRACTIONAL_PART;
54b843c749SSergey Zigachev return res;
55b843c749SSergey Zigachev }
56b843c749SSergey Zigachev
bw_frc_to_fixed(int64_t numerator,int64_t denominator)57b843c749SSergey Zigachev struct bw_fixed bw_frc_to_fixed(int64_t numerator, int64_t denominator)
58b843c749SSergey Zigachev {
59b843c749SSergey Zigachev struct bw_fixed res;
60b843c749SSergey Zigachev bool arg1_negative = numerator < 0;
61b843c749SSergey Zigachev bool arg2_negative = denominator < 0;
62b843c749SSergey Zigachev uint64_t arg1_value;
63b843c749SSergey Zigachev uint64_t arg2_value;
64b843c749SSergey Zigachev uint64_t remainder;
65b843c749SSergey Zigachev
66b843c749SSergey Zigachev /* determine integer part */
67b843c749SSergey Zigachev uint64_t res_value;
68b843c749SSergey Zigachev
69b843c749SSergey Zigachev ASSERT(denominator != 0);
70b843c749SSergey Zigachev
71b843c749SSergey Zigachev arg1_value = abs_i64(numerator);
72b843c749SSergey Zigachev arg2_value = abs_i64(denominator);
73*78973132SSergey Zigachev /* XXX: int64_t* -> u64* conversion! */
74*78973132SSergey Zigachev res_value = div64_u64_rem(arg1_value, arg2_value, (u64 *)&remainder);
75b843c749SSergey Zigachev
76b843c749SSergey Zigachev ASSERT(res_value <= BW_FIXED_MAX_I32);
77b843c749SSergey Zigachev
78b843c749SSergey Zigachev /* determine fractional part */
79b843c749SSergey Zigachev {
80b843c749SSergey Zigachev uint32_t i = BW_FIXED_BITS_PER_FRACTIONAL_PART;
81b843c749SSergey Zigachev
82b843c749SSergey Zigachev do
83b843c749SSergey Zigachev {
84b843c749SSergey Zigachev remainder <<= 1;
85b843c749SSergey Zigachev
86b843c749SSergey Zigachev res_value <<= 1;
87b843c749SSergey Zigachev
88b843c749SSergey Zigachev if (remainder >= arg2_value)
89b843c749SSergey Zigachev {
90b843c749SSergey Zigachev res_value |= 1;
91b843c749SSergey Zigachev remainder -= arg2_value;
92b843c749SSergey Zigachev }
93b843c749SSergey Zigachev } while (--i != 0);
94b843c749SSergey Zigachev }
95b843c749SSergey Zigachev
96b843c749SSergey Zigachev /* round up LSB */
97b843c749SSergey Zigachev {
98b843c749SSergey Zigachev uint64_t summand = (remainder << 1) >= arg2_value;
99b843c749SSergey Zigachev
100b843c749SSergey Zigachev ASSERT(res_value <= MAX_I64 - summand);
101b843c749SSergey Zigachev
102b843c749SSergey Zigachev res_value += summand;
103b843c749SSergey Zigachev }
104b843c749SSergey Zigachev
105b843c749SSergey Zigachev res.value = (int64_t)(res_value);
106b843c749SSergey Zigachev
107b843c749SSergey Zigachev if (arg1_negative ^ arg2_negative)
108b843c749SSergey Zigachev res.value = -res.value;
109b843c749SSergey Zigachev return res;
110b843c749SSergey Zigachev }
111b843c749SSergey Zigachev
bw_floor2(const struct bw_fixed arg,const struct bw_fixed significance)112b843c749SSergey Zigachev struct bw_fixed bw_floor2(
113b843c749SSergey Zigachev const struct bw_fixed arg,
114b843c749SSergey Zigachev const struct bw_fixed significance)
115b843c749SSergey Zigachev {
116b843c749SSergey Zigachev struct bw_fixed result;
117b843c749SSergey Zigachev int64_t multiplicand;
118b843c749SSergey Zigachev
119b843c749SSergey Zigachev multiplicand = div64_s64(arg.value, abs_i64(significance.value));
120b843c749SSergey Zigachev result.value = abs_i64(significance.value) * multiplicand;
121b843c749SSergey Zigachev ASSERT(abs_i64(result.value) <= abs_i64(arg.value));
122b843c749SSergey Zigachev return result;
123b843c749SSergey Zigachev }
124b843c749SSergey Zigachev
bw_ceil2(const struct bw_fixed arg,const struct bw_fixed significance)125b843c749SSergey Zigachev struct bw_fixed bw_ceil2(
126b843c749SSergey Zigachev const struct bw_fixed arg,
127b843c749SSergey Zigachev const struct bw_fixed significance)
128b843c749SSergey Zigachev {
129b843c749SSergey Zigachev struct bw_fixed result;
130b843c749SSergey Zigachev int64_t multiplicand;
131b843c749SSergey Zigachev
132b843c749SSergey Zigachev multiplicand = div64_s64(arg.value, abs_i64(significance.value));
133b843c749SSergey Zigachev result.value = abs_i64(significance.value) * multiplicand;
134b843c749SSergey Zigachev if (abs_i64(result.value) < abs_i64(arg.value)) {
135b843c749SSergey Zigachev if (arg.value < 0)
136b843c749SSergey Zigachev result.value -= abs_i64(significance.value);
137b843c749SSergey Zigachev else
138b843c749SSergey Zigachev result.value += abs_i64(significance.value);
139b843c749SSergey Zigachev }
140b843c749SSergey Zigachev return result;
141b843c749SSergey Zigachev }
142b843c749SSergey Zigachev
bw_mul(const struct bw_fixed arg1,const struct bw_fixed arg2)143b843c749SSergey Zigachev struct bw_fixed bw_mul(const struct bw_fixed arg1, const struct bw_fixed arg2)
144b843c749SSergey Zigachev {
145b843c749SSergey Zigachev struct bw_fixed res;
146b843c749SSergey Zigachev
147b843c749SSergey Zigachev bool arg1_negative = arg1.value < 0;
148b843c749SSergey Zigachev bool arg2_negative = arg2.value < 0;
149b843c749SSergey Zigachev
150b843c749SSergey Zigachev uint64_t arg1_value = abs_i64(arg1.value);
151b843c749SSergey Zigachev uint64_t arg2_value = abs_i64(arg2.value);
152b843c749SSergey Zigachev
153b843c749SSergey Zigachev uint64_t arg1_int = BW_FIXED_GET_INTEGER_PART(arg1_value);
154b843c749SSergey Zigachev uint64_t arg2_int = BW_FIXED_GET_INTEGER_PART(arg2_value);
155b843c749SSergey Zigachev
156b843c749SSergey Zigachev uint64_t arg1_fra = GET_FRACTIONAL_PART(arg1_value);
157b843c749SSergey Zigachev uint64_t arg2_fra = GET_FRACTIONAL_PART(arg2_value);
158b843c749SSergey Zigachev
159b843c749SSergey Zigachev uint64_t tmp;
160b843c749SSergey Zigachev
161b843c749SSergey Zigachev res.value = arg1_int * arg2_int;
162b843c749SSergey Zigachev
163b843c749SSergey Zigachev ASSERT(res.value <= BW_FIXED_MAX_I32);
164b843c749SSergey Zigachev
165b843c749SSergey Zigachev res.value <<= BW_FIXED_BITS_PER_FRACTIONAL_PART;
166b843c749SSergey Zigachev
167b843c749SSergey Zigachev tmp = arg1_int * arg2_fra;
168b843c749SSergey Zigachev
169b843c749SSergey Zigachev ASSERT(tmp <= (uint64_t)(MAX_I64 - res.value));
170b843c749SSergey Zigachev
171b843c749SSergey Zigachev res.value += tmp;
172b843c749SSergey Zigachev
173b843c749SSergey Zigachev tmp = arg2_int * arg1_fra;
174b843c749SSergey Zigachev
175b843c749SSergey Zigachev ASSERT(tmp <= (uint64_t)(MAX_I64 - res.value));
176b843c749SSergey Zigachev
177b843c749SSergey Zigachev res.value += tmp;
178b843c749SSergey Zigachev
179b843c749SSergey Zigachev tmp = arg1_fra * arg2_fra;
180b843c749SSergey Zigachev
181b843c749SSergey Zigachev tmp = (tmp >> BW_FIXED_BITS_PER_FRACTIONAL_PART) +
182b843c749SSergey Zigachev (tmp >= (uint64_t)(bw_frc_to_fixed(1, 2).value));
183b843c749SSergey Zigachev
184b843c749SSergey Zigachev ASSERT(tmp <= (uint64_t)(MAX_I64 - res.value));
185b843c749SSergey Zigachev
186b843c749SSergey Zigachev res.value += tmp;
187b843c749SSergey Zigachev
188b843c749SSergey Zigachev if (arg1_negative ^ arg2_negative)
189b843c749SSergey Zigachev res.value = -res.value;
190b843c749SSergey Zigachev return res;
191b843c749SSergey Zigachev }
192b843c749SSergey Zigachev
193