1 // SPDX-License-Identifier: MIT
3 * Copyright 2023 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
26 #include "dm_services.h"
30 ((int64_t)((1ULL << 63) - 1))
35 #define FRACTIONAL_PART_MASK \
36 ((1ULL << BW_FIXED_BITS_PER_FRACTIONAL_PART) - 1)
38 #define GET_FRACTIONAL_PART(x) \
39 (FRACTIONAL_PART_MASK & (x))
41 static uint64_t abs_i64(int64_t arg)
44 return (uint64_t)(arg);
46 return (uint64_t)(-arg);
49 struct bw_fixed bw_int_to_fixed_nonconst(int64_t value)
53 ASSERT(value < BW_FIXED_MAX_I32 && value > BW_FIXED_MIN_I32);
54 res.value = value << BW_FIXED_BITS_PER_FRACTIONAL_PART;
58 struct bw_fixed bw_frc_to_fixed(int64_t numerator, int64_t denominator)
61 bool arg1_negative = numerator < 0;
62 bool arg2_negative = denominator < 0;
67 /* determine integer part */
70 ASSERT(denominator != 0);
72 arg1_value = abs_i64(numerator);
73 arg2_value = abs_i64(denominator);
74 res_value = div64_u64_rem(arg1_value, arg2_value, &remainder);
76 ASSERT(res_value <= BW_FIXED_MAX_I32);
78 /* determine fractional part */
80 uint32_t i = BW_FIXED_BITS_PER_FRACTIONAL_PART;
87 if (remainder >= arg2_value) {
89 remainder -= arg2_value;
96 uint64_t summand = (remainder << 1) >= arg2_value;
98 ASSERT(res_value <= MAX_I64 - summand);
100 res_value += summand;
103 res.value = (int64_t)(res_value);
105 if (arg1_negative ^ arg2_negative)
106 res.value = -res.value;
110 struct bw_fixed bw_floor2(const struct bw_fixed arg,
111 const struct bw_fixed significance)
113 struct bw_fixed result;
114 int64_t multiplicand;
116 multiplicand = div64_s64(arg.value, abs_i64(significance.value));
117 result.value = abs_i64(significance.value) * multiplicand;
118 ASSERT(abs_i64(result.value) <= abs_i64(arg.value));
122 struct bw_fixed bw_ceil2(const struct bw_fixed arg,
123 const struct bw_fixed significance)
125 struct bw_fixed result;
126 int64_t multiplicand;
128 multiplicand = div64_s64(arg.value, abs_i64(significance.value));
129 result.value = abs_i64(significance.value) * multiplicand;
130 if (abs_i64(result.value) < abs_i64(arg.value)) {
132 result.value -= abs_i64(significance.value);
134 result.value += abs_i64(significance.value);
139 struct bw_fixed bw_mul(const struct bw_fixed arg1, const struct bw_fixed arg2)
143 bool arg1_negative = arg1.value < 0;
144 bool arg2_negative = arg2.value < 0;
146 uint64_t arg1_value = abs_i64(arg1.value);
147 uint64_t arg2_value = abs_i64(arg2.value);
149 uint64_t arg1_int = BW_FIXED_GET_INTEGER_PART(arg1_value);
150 uint64_t arg2_int = BW_FIXED_GET_INTEGER_PART(arg2_value);
152 uint64_t arg1_fra = GET_FRACTIONAL_PART(arg1_value);
153 uint64_t arg2_fra = GET_FRACTIONAL_PART(arg2_value);
157 res.value = arg1_int * arg2_int;
159 ASSERT(res.value <= BW_FIXED_MAX_I32);
161 res.value <<= BW_FIXED_BITS_PER_FRACTIONAL_PART;
163 tmp = arg1_int * arg2_fra;
165 ASSERT(tmp <= (uint64_t)(MAX_I64 - res.value));
169 tmp = arg2_int * arg1_fra;
171 ASSERT(tmp <= (uint64_t)(MAX_I64 - res.value));
175 tmp = arg1_fra * arg2_fra;
177 tmp = (tmp >> BW_FIXED_BITS_PER_FRACTIONAL_PART) +
178 (tmp >= (uint64_t)(bw_frc_to_fixed(1, 2).value));
180 ASSERT(tmp <= (uint64_t)(MAX_I64 - res.value));
184 if (arg1_negative ^ arg2_negative)
185 res.value = -res.value;