2 * Copyright 2017 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
25 #include "dm_services.h"
26 #include "custom_float.h"
29 static bool build_custom_float(
30 struct fixed31_32 value,
31 const struct custom_float_format *format,
36 uint32_t exp_offset = (1 << (format->exponenta_bits - 1)) - 1;
38 const struct fixed31_32 mantissa_constant_plus_max_fraction =
39 dal_fixed31_32_from_fraction(
40 (1LL << (format->mantissa_bits + 1)) - 1,
41 1LL << format->mantissa_bits);
43 struct fixed31_32 mantiss;
45 if (dal_fixed31_32_eq(
47 dal_fixed31_32_zero)) {
54 if (dal_fixed31_32_lt(
56 dal_fixed31_32_zero)) {
57 *negative = format->sign;
58 value = dal_fixed31_32_neg(value);
63 if (dal_fixed31_32_lt(
65 dal_fixed31_32_one)) {
69 value = dal_fixed31_32_shl(value, 1);
71 } while (dal_fixed31_32_lt(
77 if (exp_offset <= i) {
83 *exponenta = exp_offset - i;
84 } else if (dal_fixed31_32_le(
85 mantissa_constant_plus_max_fraction,
90 value = dal_fixed31_32_shr(value, 1);
92 } while (dal_fixed31_32_lt(
93 mantissa_constant_plus_max_fraction,
96 *exponenta = exp_offset + i - 1;
98 *exponenta = exp_offset;
101 mantiss = dal_fixed31_32_sub(
105 if (dal_fixed31_32_lt(
107 dal_fixed31_32_zero) ||
111 mantiss = dal_fixed31_32_zero;
113 mantiss = dal_fixed31_32_shl(
115 format->mantissa_bits);
117 *mantissa = dal_fixed31_32_floor(mantiss);
122 static bool setup_custom_float(
123 const struct custom_float_format *format,
134 /* verification code:
135 * once calculation is ok we can remove it
138 const uint32_t mantissa_mask =
139 (1 << (format->mantissa_bits + 1)) - 1;
141 const uint32_t exponenta_mask =
142 (1 << (format->exponenta_bits + 1)) - 1;
144 if (mantissa & ~mantissa_mask) {
146 mantissa = mantissa_mask;
149 if (exponenta & ~exponenta_mask) {
151 exponenta = exponenta_mask;
154 /* end of verification code */
156 while (i < format->mantissa_bits) {
157 uint32_t mask = 1 << i;
165 while (j < format->exponenta_bits) {
166 uint32_t mask = 1 << j;
168 if (exponenta & mask)
174 if (negative && format->sign)
175 value |= 1 << (i + j);
182 bool convert_to_custom_float_format(
183 struct fixed31_32 value,
184 const struct custom_float_format *format,
191 return build_custom_float(
192 value, format, &negative, &mantissa, &exponenta) &&
194 format, negative, mantissa, exponenta, result);