1// SPDX-License-Identifier: MIT
2/*
3 * Copyright 2023 Advanced Micro Devices, Inc.
4 *
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:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
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.
22 *
23 * Authors: AMD
24 *
25 */
26#include "dm_services.h"
27#include "custom_float.h"
28
29static bool build_custom_float(struct fixed31_32 value,
30			       const struct custom_float_format *format,
31			       bool *negative,
32			       uint32_t *mantissa,
33			       uint32_t *exponenta)
34{
35	uint32_t exp_offset = (1 << (format->exponenta_bits - 1)) - 1;
36
37	const struct fixed31_32 mantissa_constant_plus_max_fraction =
38		dc_fixpt_from_fraction((1LL << (format->mantissa_bits + 1)) - 1,
39				       1LL << format->mantissa_bits);
40
41	struct fixed31_32 mantiss;
42
43	if (dc_fixpt_eq(value, dc_fixpt_zero)) {
44		*negative = false;
45		*mantissa = 0;
46		*exponenta = 0;
47		return true;
48	}
49
50	if (dc_fixpt_lt(value, dc_fixpt_zero)) {
51		*negative = format->sign;
52		value = dc_fixpt_neg(value);
53	} else {
54		*negative = false;
55	}
56
57	if (dc_fixpt_lt(value, dc_fixpt_one)) {
58		uint32_t i = 1;
59
60		do {
61			value = dc_fixpt_shl(value, 1);
62			++i;
63		} while (dc_fixpt_lt(value, dc_fixpt_one));
64
65		--i;
66
67		if (exp_offset <= i) {
68			*mantissa = 0;
69			*exponenta = 0;
70			return true;
71		}
72
73		*exponenta = exp_offset - i;
74	} else if (dc_fixpt_le(mantissa_constant_plus_max_fraction, value)) {
75		uint32_t i = 1;
76
77		do {
78			value = dc_fixpt_shr(value, 1);
79			++i;
80		} while (dc_fixpt_lt(mantissa_constant_plus_max_fraction, value));
81
82		*exponenta = exp_offset + i - 1;
83	} else {
84		*exponenta = exp_offset;
85	}
86
87	mantiss = dc_fixpt_sub(value, dc_fixpt_one);
88
89	if (dc_fixpt_lt(mantiss, dc_fixpt_zero) ||
90	    dc_fixpt_lt(dc_fixpt_one, mantiss))
91		mantiss = dc_fixpt_zero;
92	else
93		mantiss = dc_fixpt_shl(mantiss, format->mantissa_bits);
94
95	*mantissa = dc_fixpt_floor(mantiss);
96
97	return true;
98}
99
100static bool setup_custom_float(const struct custom_float_format *format,
101			       bool negative,
102			       uint32_t mantissa,
103			       uint32_t exponenta,
104			       uint32_t *result)
105{
106	uint32_t i = 0;
107	uint32_t j = 0;
108	uint32_t value = 0;
109
110	/* verification code:
111	 * once calculation is ok we can remove it
112	 */
113
114	const uint32_t mantissa_mask =
115		(1 << (format->mantissa_bits + 1)) - 1;
116
117	const uint32_t exponenta_mask =
118		(1 << (format->exponenta_bits + 1)) - 1;
119
120	if (mantissa & ~mantissa_mask) {
121		BREAK_TO_DEBUGGER();
122		mantissa = mantissa_mask;
123	}
124
125	if (exponenta & ~exponenta_mask) {
126		BREAK_TO_DEBUGGER();
127		exponenta = exponenta_mask;
128	}
129
130	/* end of verification code */
131
132	while (i < format->mantissa_bits) {
133		uint32_t mask = 1 << i;
134
135		if (mantissa & mask)
136			value |= mask;
137
138		++i;
139	}
140
141	while (j < format->exponenta_bits) {
142		uint32_t mask = 1 << j;
143
144		if (exponenta & mask)
145			value |= mask << i;
146
147		++j;
148	}
149
150	if (negative && format->sign)
151		value |= 1 << (i + j);
152
153	*result = value;
154
155	return true;
156}
157
158bool convert_to_custom_float_format(struct fixed31_32 value,
159				    const struct custom_float_format *format,
160				    uint32_t *result)
161{
162	uint32_t mantissa;
163	uint32_t exponenta;
164	bool negative;
165
166	return build_custom_float(value, format, &negative, &mantissa, &exponenta) &&
167				  setup_custom_float(format,
168						     negative,
169						     mantissa,
170						     exponenta,
171						     result);
172}
173
174