1/*	$NetBSD: amdgpu_bw_fixed.c,v 1.2 2021/12/18 23:45:01 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#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: amdgpu_bw_fixed.c,v 1.2 2021/12/18 23:45:01 riastradh Exp $");
29
30#include "dm_services.h"
31#include "bw_fixed.h"
32
33
34#define MIN_I64 \
35	(int64_t)(-(1LL << 63))
36
37#define MAX_I64 \
38	(int64_t)((1ULL << 63) - 1)
39
40#define FRACTIONAL_PART_MASK \
41	((1ULL << BW_FIXED_BITS_PER_FRACTIONAL_PART) - 1)
42
43#define GET_FRACTIONAL_PART(x) \
44	(FRACTIONAL_PART_MASK & (x))
45
46static uint64_t abs_i64(int64_t arg)
47{
48	if (arg >= 0)
49		return (uint64_t)(arg);
50	else
51		return (uint64_t)(-arg);
52}
53
54struct bw_fixed bw_int_to_fixed_nonconst(int64_t value)
55{
56	struct bw_fixed res;
57	ASSERT(value < BW_FIXED_MAX_I32 && value > BW_FIXED_MIN_I32);
58	res.value = value << BW_FIXED_BITS_PER_FRACTIONAL_PART;
59	return res;
60}
61
62struct bw_fixed bw_frc_to_fixed(int64_t numerator, int64_t denominator)
63{
64	struct bw_fixed res;
65	bool arg1_negative = numerator < 0;
66	bool arg2_negative = denominator < 0;
67	uint64_t arg1_value;
68	uint64_t arg2_value;
69	uint64_t remainder;
70
71	/* determine integer part */
72	uint64_t res_value;
73
74	ASSERT(denominator != 0);
75
76	arg1_value = abs_i64(numerator);
77	arg2_value = abs_i64(denominator);
78	res_value = div64_u64_rem(arg1_value, arg2_value, &remainder);
79
80	ASSERT(res_value <= BW_FIXED_MAX_I32);
81
82	/* determine fractional part */
83	{
84		uint32_t i = BW_FIXED_BITS_PER_FRACTIONAL_PART;
85
86		do
87		{
88			remainder <<= 1;
89
90			res_value <<= 1;
91
92			if (remainder >= arg2_value)
93			{
94				res_value |= 1;
95				remainder -= arg2_value;
96			}
97		} while (--i != 0);
98	}
99
100	/* round up LSB */
101	{
102		uint64_t summand = (remainder << 1) >= arg2_value;
103
104		ASSERT(res_value <= MAX_I64 - summand);
105
106		res_value += summand;
107	}
108
109	res.value = (int64_t)(res_value);
110
111	if (arg1_negative ^ arg2_negative)
112		res.value = -res.value;
113	return res;
114}
115
116struct bw_fixed bw_floor2(
117	const struct bw_fixed arg,
118	const struct bw_fixed significance)
119{
120	struct bw_fixed result;
121	int64_t multiplicand;
122
123	multiplicand = div64_s64(arg.value, abs_i64(significance.value));
124	result.value = abs_i64(significance.value) * multiplicand;
125	ASSERT(abs_i64(result.value) <= abs_i64(arg.value));
126	return result;
127}
128
129struct bw_fixed bw_ceil2(
130	const struct bw_fixed arg,
131	const struct bw_fixed significance)
132{
133	struct bw_fixed result;
134	int64_t multiplicand;
135
136	multiplicand = div64_s64(arg.value, abs_i64(significance.value));
137	result.value = abs_i64(significance.value) * multiplicand;
138	if (abs_i64(result.value) < abs_i64(arg.value)) {
139		if (arg.value < 0)
140			result.value -= abs_i64(significance.value);
141		else
142			result.value += abs_i64(significance.value);
143	}
144	return result;
145}
146
147struct bw_fixed bw_mul(const struct bw_fixed arg1, const struct bw_fixed arg2)
148{
149	struct bw_fixed res;
150
151	bool arg1_negative = arg1.value < 0;
152	bool arg2_negative = arg2.value < 0;
153
154	uint64_t arg1_value = abs_i64(arg1.value);
155	uint64_t arg2_value = abs_i64(arg2.value);
156
157	uint64_t arg1_int = BW_FIXED_GET_INTEGER_PART(arg1_value);
158	uint64_t arg2_int = BW_FIXED_GET_INTEGER_PART(arg2_value);
159
160	uint64_t arg1_fra = GET_FRACTIONAL_PART(arg1_value);
161	uint64_t arg2_fra = GET_FRACTIONAL_PART(arg2_value);
162
163	uint64_t tmp;
164
165	res.value = arg1_int * arg2_int;
166
167	ASSERT(res.value <= BW_FIXED_MAX_I32);
168
169	res.value <<= BW_FIXED_BITS_PER_FRACTIONAL_PART;
170
171	tmp = arg1_int * arg2_fra;
172
173	ASSERT(tmp <= (uint64_t)(MAX_I64 - res.value));
174
175	res.value += tmp;
176
177	tmp = arg2_int * arg1_fra;
178
179	ASSERT(tmp <= (uint64_t)(MAX_I64 - res.value));
180
181	res.value += tmp;
182
183	tmp = arg1_fra * arg2_fra;
184
185	tmp = (tmp >> BW_FIXED_BITS_PER_FRACTIONAL_PART) +
186		(tmp >= (uint64_t)(bw_frc_to_fixed(1, 2).value));
187
188	ASSERT(tmp <= (uint64_t)(MAX_I64 - res.value));
189
190	res.value += tmp;
191
192	if (arg1_negative ^ arg2_negative)
193		res.value = -res.value;
194	return res;
195}
196
197