1*b843c749SSergey Zigachev /*
2*b843c749SSergey Zigachev  * Copyright 2017 Advanced Micro Devices, Inc.
3*b843c749SSergey Zigachev  *
4*b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5*b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6*b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7*b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9*b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10*b843c749SSergey Zigachev  *
11*b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12*b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13*b843c749SSergey Zigachev  *
14*b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17*b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21*b843c749SSergey Zigachev  *
22*b843c749SSergey Zigachev  * Authors: AMD
23*b843c749SSergey Zigachev  *
24*b843c749SSergey Zigachev  */
25*b843c749SSergey Zigachev #include "dm_services.h"
26*b843c749SSergey Zigachev #include "custom_float.h"
27*b843c749SSergey Zigachev 
28*b843c749SSergey Zigachev 
build_custom_float(struct fixed31_32 value,const struct custom_float_format * format,bool * negative,uint32_t * mantissa,uint32_t * exponenta)29*b843c749SSergey Zigachev static bool build_custom_float(
30*b843c749SSergey Zigachev 	struct fixed31_32 value,
31*b843c749SSergey Zigachev 	const struct custom_float_format *format,
32*b843c749SSergey Zigachev 	bool *negative,
33*b843c749SSergey Zigachev 	uint32_t *mantissa,
34*b843c749SSergey Zigachev 	uint32_t *exponenta)
35*b843c749SSergey Zigachev {
36*b843c749SSergey Zigachev 	uint32_t exp_offset = (1 << (format->exponenta_bits - 1)) - 1;
37*b843c749SSergey Zigachev 
38*b843c749SSergey Zigachev 	const struct fixed31_32 mantissa_constant_plus_max_fraction =
39*b843c749SSergey Zigachev 		dc_fixpt_from_fraction(
40*b843c749SSergey Zigachev 			(1LL << (format->mantissa_bits + 1)) - 1,
41*b843c749SSergey Zigachev 			1LL << format->mantissa_bits);
42*b843c749SSergey Zigachev 
43*b843c749SSergey Zigachev 	struct fixed31_32 mantiss;
44*b843c749SSergey Zigachev 
45*b843c749SSergey Zigachev 	if (dc_fixpt_eq(
46*b843c749SSergey Zigachev 		value,
47*b843c749SSergey Zigachev 		dc_fixpt_zero)) {
48*b843c749SSergey Zigachev 		*negative = false;
49*b843c749SSergey Zigachev 		*mantissa = 0;
50*b843c749SSergey Zigachev 		*exponenta = 0;
51*b843c749SSergey Zigachev 		return true;
52*b843c749SSergey Zigachev 	}
53*b843c749SSergey Zigachev 
54*b843c749SSergey Zigachev 	if (dc_fixpt_lt(
55*b843c749SSergey Zigachev 		value,
56*b843c749SSergey Zigachev 		dc_fixpt_zero)) {
57*b843c749SSergey Zigachev 		*negative = format->sign;
58*b843c749SSergey Zigachev 		value = dc_fixpt_neg(value);
59*b843c749SSergey Zigachev 	} else {
60*b843c749SSergey Zigachev 		*negative = false;
61*b843c749SSergey Zigachev 	}
62*b843c749SSergey Zigachev 
63*b843c749SSergey Zigachev 	if (dc_fixpt_lt(
64*b843c749SSergey Zigachev 		value,
65*b843c749SSergey Zigachev 		dc_fixpt_one)) {
66*b843c749SSergey Zigachev 		uint32_t i = 1;
67*b843c749SSergey Zigachev 
68*b843c749SSergey Zigachev 		do {
69*b843c749SSergey Zigachev 			value = dc_fixpt_shl(value, 1);
70*b843c749SSergey Zigachev 			++i;
71*b843c749SSergey Zigachev 		} while (dc_fixpt_lt(
72*b843c749SSergey Zigachev 			value,
73*b843c749SSergey Zigachev 			dc_fixpt_one));
74*b843c749SSergey Zigachev 
75*b843c749SSergey Zigachev 		--i;
76*b843c749SSergey Zigachev 
77*b843c749SSergey Zigachev 		if (exp_offset <= i) {
78*b843c749SSergey Zigachev 			*mantissa = 0;
79*b843c749SSergey Zigachev 			*exponenta = 0;
80*b843c749SSergey Zigachev 			return true;
81*b843c749SSergey Zigachev 		}
82*b843c749SSergey Zigachev 
83*b843c749SSergey Zigachev 		*exponenta = exp_offset - i;
84*b843c749SSergey Zigachev 	} else if (dc_fixpt_le(
85*b843c749SSergey Zigachev 		mantissa_constant_plus_max_fraction,
86*b843c749SSergey Zigachev 		value)) {
87*b843c749SSergey Zigachev 		uint32_t i = 1;
88*b843c749SSergey Zigachev 
89*b843c749SSergey Zigachev 		do {
90*b843c749SSergey Zigachev 			value = dc_fixpt_shr(value, 1);
91*b843c749SSergey Zigachev 			++i;
92*b843c749SSergey Zigachev 		} while (dc_fixpt_lt(
93*b843c749SSergey Zigachev 			mantissa_constant_plus_max_fraction,
94*b843c749SSergey Zigachev 			value));
95*b843c749SSergey Zigachev 
96*b843c749SSergey Zigachev 		*exponenta = exp_offset + i - 1;
97*b843c749SSergey Zigachev 	} else {
98*b843c749SSergey Zigachev 		*exponenta = exp_offset;
99*b843c749SSergey Zigachev 	}
100*b843c749SSergey Zigachev 
101*b843c749SSergey Zigachev 	mantiss = dc_fixpt_sub(
102*b843c749SSergey Zigachev 		value,
103*b843c749SSergey Zigachev 		dc_fixpt_one);
104*b843c749SSergey Zigachev 
105*b843c749SSergey Zigachev 	if (dc_fixpt_lt(
106*b843c749SSergey Zigachev 			mantiss,
107*b843c749SSergey Zigachev 			dc_fixpt_zero) ||
108*b843c749SSergey Zigachev 		dc_fixpt_lt(
109*b843c749SSergey Zigachev 			dc_fixpt_one,
110*b843c749SSergey Zigachev 			mantiss))
111*b843c749SSergey Zigachev 		mantiss = dc_fixpt_zero;
112*b843c749SSergey Zigachev 	else
113*b843c749SSergey Zigachev 		mantiss = dc_fixpt_shl(
114*b843c749SSergey Zigachev 			mantiss,
115*b843c749SSergey Zigachev 			format->mantissa_bits);
116*b843c749SSergey Zigachev 
117*b843c749SSergey Zigachev 	*mantissa = dc_fixpt_floor(mantiss);
118*b843c749SSergey Zigachev 
119*b843c749SSergey Zigachev 	return true;
120*b843c749SSergey Zigachev }
121*b843c749SSergey Zigachev 
setup_custom_float(const struct custom_float_format * format,bool negative,uint32_t mantissa,uint32_t exponenta,uint32_t * result)122*b843c749SSergey Zigachev static bool setup_custom_float(
123*b843c749SSergey Zigachev 	const struct custom_float_format *format,
124*b843c749SSergey Zigachev 	bool negative,
125*b843c749SSergey Zigachev 	uint32_t mantissa,
126*b843c749SSergey Zigachev 	uint32_t exponenta,
127*b843c749SSergey Zigachev 	uint32_t *result)
128*b843c749SSergey Zigachev {
129*b843c749SSergey Zigachev 	uint32_t i = 0;
130*b843c749SSergey Zigachev 	uint32_t j = 0;
131*b843c749SSergey Zigachev 
132*b843c749SSergey Zigachev 	uint32_t value = 0;
133*b843c749SSergey Zigachev 
134*b843c749SSergey Zigachev 	/* verification code:
135*b843c749SSergey Zigachev 	 * once calculation is ok we can remove it
136*b843c749SSergey Zigachev 	 */
137*b843c749SSergey Zigachev 
138*b843c749SSergey Zigachev 	const uint32_t mantissa_mask =
139*b843c749SSergey Zigachev 		(1 << (format->mantissa_bits + 1)) - 1;
140*b843c749SSergey Zigachev 
141*b843c749SSergey Zigachev 	const uint32_t exponenta_mask =
142*b843c749SSergey Zigachev 		(1 << (format->exponenta_bits + 1)) - 1;
143*b843c749SSergey Zigachev 
144*b843c749SSergey Zigachev 	if (mantissa & ~mantissa_mask) {
145*b843c749SSergey Zigachev 		BREAK_TO_DEBUGGER();
146*b843c749SSergey Zigachev 		mantissa = mantissa_mask;
147*b843c749SSergey Zigachev 	}
148*b843c749SSergey Zigachev 
149*b843c749SSergey Zigachev 	if (exponenta & ~exponenta_mask) {
150*b843c749SSergey Zigachev 		BREAK_TO_DEBUGGER();
151*b843c749SSergey Zigachev 		exponenta = exponenta_mask;
152*b843c749SSergey Zigachev 	}
153*b843c749SSergey Zigachev 
154*b843c749SSergey Zigachev 	/* end of verification code */
155*b843c749SSergey Zigachev 
156*b843c749SSergey Zigachev 	while (i < format->mantissa_bits) {
157*b843c749SSergey Zigachev 		uint32_t mask = 1 << i;
158*b843c749SSergey Zigachev 
159*b843c749SSergey Zigachev 		if (mantissa & mask)
160*b843c749SSergey Zigachev 			value |= mask;
161*b843c749SSergey Zigachev 
162*b843c749SSergey Zigachev 		++i;
163*b843c749SSergey Zigachev 	}
164*b843c749SSergey Zigachev 
165*b843c749SSergey Zigachev 	while (j < format->exponenta_bits) {
166*b843c749SSergey Zigachev 		uint32_t mask = 1 << j;
167*b843c749SSergey Zigachev 
168*b843c749SSergey Zigachev 		if (exponenta & mask)
169*b843c749SSergey Zigachev 			value |= mask << i;
170*b843c749SSergey Zigachev 
171*b843c749SSergey Zigachev 		++j;
172*b843c749SSergey Zigachev 	}
173*b843c749SSergey Zigachev 
174*b843c749SSergey Zigachev 	if (negative && format->sign)
175*b843c749SSergey Zigachev 		value |= 1 << (i + j);
176*b843c749SSergey Zigachev 
177*b843c749SSergey Zigachev 	*result = value;
178*b843c749SSergey Zigachev 
179*b843c749SSergey Zigachev 	return true;
180*b843c749SSergey Zigachev }
181*b843c749SSergey Zigachev 
convert_to_custom_float_format(struct fixed31_32 value,const struct custom_float_format * format,uint32_t * result)182*b843c749SSergey Zigachev bool convert_to_custom_float_format(
183*b843c749SSergey Zigachev 	struct fixed31_32 value,
184*b843c749SSergey Zigachev 	const struct custom_float_format *format,
185*b843c749SSergey Zigachev 	uint32_t *result)
186*b843c749SSergey Zigachev {
187*b843c749SSergey Zigachev 	uint32_t mantissa;
188*b843c749SSergey Zigachev 	uint32_t exponenta;
189*b843c749SSergey Zigachev 	bool negative;
190*b843c749SSergey Zigachev 
191*b843c749SSergey Zigachev 	return build_custom_float(
192*b843c749SSergey Zigachev 		value, format, &negative, &mantissa, &exponenta) &&
193*b843c749SSergey Zigachev 	setup_custom_float(
194*b843c749SSergey Zigachev 		format, negative, mantissa, exponenta, result);
195*b843c749SSergey Zigachev }
196*b843c749SSergey Zigachev 
197*b843c749SSergey Zigachev 
198