1 /* $NetBSD: amdgpu_custom_float.c,v 1.2 2021/12/18 23:45:01 riastradh Exp $ */
2
3 /*
4 * Copyright 2017 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_custom_float.c,v 1.2 2021/12/18 23:45:01 riastradh Exp $");
29
30 #include "dm_services.h"
31 #include "custom_float.h"
32
33
build_custom_float(struct fixed31_32 value,const struct custom_float_format * format,bool * negative,uint32_t * mantissa,uint32_t * exponenta)34 static bool build_custom_float(
35 struct fixed31_32 value,
36 const struct custom_float_format *format,
37 bool *negative,
38 uint32_t *mantissa,
39 uint32_t *exponenta)
40 {
41 uint32_t exp_offset = (1 << (format->exponenta_bits - 1)) - 1;
42
43 const struct fixed31_32 mantissa_constant_plus_max_fraction =
44 dc_fixpt_from_fraction(
45 (1LL << (format->mantissa_bits + 1)) - 1,
46 1LL << format->mantissa_bits);
47
48 struct fixed31_32 mantiss;
49
50 if (dc_fixpt_eq(
51 value,
52 dc_fixpt_zero)) {
53 *negative = false;
54 *mantissa = 0;
55 *exponenta = 0;
56 return true;
57 }
58
59 if (dc_fixpt_lt(
60 value,
61 dc_fixpt_zero)) {
62 *negative = format->sign;
63 value = dc_fixpt_neg(value);
64 } else {
65 *negative = false;
66 }
67
68 if (dc_fixpt_lt(
69 value,
70 dc_fixpt_one)) {
71 uint32_t i = 1;
72
73 do {
74 value = dc_fixpt_shl(value, 1);
75 ++i;
76 } while (dc_fixpt_lt(
77 value,
78 dc_fixpt_one));
79
80 --i;
81
82 if (exp_offset <= i) {
83 *mantissa = 0;
84 *exponenta = 0;
85 return true;
86 }
87
88 *exponenta = exp_offset - i;
89 } else if (dc_fixpt_le(
90 mantissa_constant_plus_max_fraction,
91 value)) {
92 uint32_t i = 1;
93
94 do {
95 value = dc_fixpt_shr(value, 1);
96 ++i;
97 } while (dc_fixpt_lt(
98 mantissa_constant_plus_max_fraction,
99 value));
100
101 *exponenta = exp_offset + i - 1;
102 } else {
103 *exponenta = exp_offset;
104 }
105
106 mantiss = dc_fixpt_sub(
107 value,
108 dc_fixpt_one);
109
110 if (dc_fixpt_lt(
111 mantiss,
112 dc_fixpt_zero) ||
113 dc_fixpt_lt(
114 dc_fixpt_one,
115 mantiss))
116 mantiss = dc_fixpt_zero;
117 else
118 mantiss = dc_fixpt_shl(
119 mantiss,
120 format->mantissa_bits);
121
122 *mantissa = dc_fixpt_floor(mantiss);
123
124 return true;
125 }
126
setup_custom_float(const struct custom_float_format * format,bool negative,uint32_t mantissa,uint32_t exponenta,uint32_t * result)127 static bool setup_custom_float(
128 const struct custom_float_format *format,
129 bool negative,
130 uint32_t mantissa,
131 uint32_t exponenta,
132 uint32_t *result)
133 {
134 uint32_t i = 0;
135 uint32_t j = 0;
136
137 uint32_t value = 0;
138
139 /* verification code:
140 * once calculation is ok we can remove it
141 */
142
143 const uint32_t mantissa_mask =
144 (1 << (format->mantissa_bits + 1)) - 1;
145
146 const uint32_t exponenta_mask =
147 (1 << (format->exponenta_bits + 1)) - 1;
148
149 if (mantissa & ~mantissa_mask) {
150 BREAK_TO_DEBUGGER();
151 mantissa = mantissa_mask;
152 }
153
154 if (exponenta & ~exponenta_mask) {
155 BREAK_TO_DEBUGGER();
156 exponenta = exponenta_mask;
157 }
158
159 /* end of verification code */
160
161 while (i < format->mantissa_bits) {
162 uint32_t mask = 1 << i;
163
164 if (mantissa & mask)
165 value |= mask;
166
167 ++i;
168 }
169
170 while (j < format->exponenta_bits) {
171 uint32_t mask = 1 << j;
172
173 if (exponenta & mask)
174 value |= mask << i;
175
176 ++j;
177 }
178
179 if (negative && format->sign)
180 value |= 1 << (i + j);
181
182 *result = value;
183
184 return true;
185 }
186
convert_to_custom_float_format(struct fixed31_32 value,const struct custom_float_format * format,uint32_t * result)187 bool convert_to_custom_float_format(
188 struct fixed31_32 value,
189 const struct custom_float_format *format,
190 uint32_t *result)
191 {
192 uint32_t mantissa;
193 uint32_t exponenta;
194 bool negative;
195
196 return build_custom_float(
197 value, format, &negative, &mantissa, &exponenta) &&
198 setup_custom_float(
199 format, negative, mantissa, exponenta, result);
200 }
201
202
203