xref: /aosp_15_r20/external/mesa3d/src/amd/vpelib/src/utils/custom_float.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /* Copyright 2022 Advanced Micro Devices, Inc.
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a
4  * copy of this software and associated documentation files (the "Software"),
5  * to deal in the Software without restriction, including without limitation
6  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7  * and/or sell copies of the Software, and to permit persons to whom the
8  * Software is furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
16  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
17  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19  * OTHER DEALINGS IN THE SOFTWARE.
20  *
21  * Authors: AMD
22  *
23  */
24 #include "custom_float.h"
25 
build_custom_float(struct fixed31_32 value,const struct custom_float_format * format,bool * negative,uint32_t * mantissa,uint32_t * exponenta)26 static bool build_custom_float(struct fixed31_32 value, const struct custom_float_format *format,
27     bool *negative, uint32_t *mantissa, uint32_t *exponenta)
28 {
29     uint32_t exp_offset = (1 << (format->exponenta_bits - 1)) - 1;
30 
31     const struct fixed31_32 mantissa_constant_plus_max_fraction = vpe_fixpt_from_fraction(
32         (1LL << (format->mantissa_bits + 1)) - 1, 1LL << format->mantissa_bits);
33 
34     struct fixed31_32 mantiss;
35 
36     if (vpe_fixpt_eq(value, vpe_fixpt_zero)) {
37         *negative  = false;
38         *mantissa  = 0;
39         *exponenta = 0;
40         return true;
41     }
42 
43     if (vpe_fixpt_lt(value, vpe_fixpt_zero)) {
44         *negative = format->sign;
45         value     = vpe_fixpt_neg(value);
46     } else {
47         *negative = false;
48     }
49 
50     if (vpe_fixpt_lt(value, vpe_fixpt_one)) {
51         uint32_t i = 1;
52 
53         do {
54             value = vpe_fixpt_shl(value, 1);
55             ++i;
56         } while (vpe_fixpt_lt(value, vpe_fixpt_one));
57 
58         --i;
59 
60         if (exp_offset <= i) {
61             *mantissa  = 0;
62             *exponenta = 0;
63             return true;
64         }
65 
66         *exponenta = exp_offset - i;
67     } else if (vpe_fixpt_le(mantissa_constant_plus_max_fraction, value)) {
68         uint32_t i = 1;
69 
70         do {
71             value = vpe_fixpt_shr(value, 1);
72             ++i;
73         } while (vpe_fixpt_lt(mantissa_constant_plus_max_fraction, value));
74 
75         *exponenta = exp_offset + i - 1;
76     } else {
77         *exponenta = exp_offset;
78     }
79 
80     mantiss = vpe_fixpt_sub(value, vpe_fixpt_one);
81 
82     if (vpe_fixpt_lt(mantiss, vpe_fixpt_zero) || vpe_fixpt_lt(vpe_fixpt_one, mantiss))
83         mantiss = vpe_fixpt_zero;
84     else
85         mantiss = vpe_fixpt_shl(mantiss, (unsigned char)format->mantissa_bits);
86 
87     *mantissa = (uint32_t)vpe_fixpt_floor(mantiss);
88 
89     return true;
90 }
91 
setup_custom_float(const struct custom_float_format * format,bool negative,uint32_t mantissa,uint32_t exponenta,uint32_t * result)92 static bool setup_custom_float(const struct custom_float_format *format, bool negative,
93     uint32_t mantissa, uint32_t exponenta, uint32_t *result)
94 {
95     uint32_t i = 0;
96     uint32_t j = 0;
97 
98     uint32_t value = 0;
99 
100     /* verification code:
101      * once calculation is ok we can remove it
102      */
103 
104     const uint32_t mantissa_mask = (1 << (format->mantissa_bits + 1)) - 1;
105 
106     const uint32_t exponenta_mask = (1 << (format->exponenta_bits + 1)) - 1;
107 
108     if (mantissa & ~mantissa_mask) {
109         VPE_ASSERT(0);
110         mantissa = mantissa_mask;
111     }
112 
113     if (exponenta & ~exponenta_mask) {
114         VPE_ASSERT(0);
115         exponenta = exponenta_mask;
116     }
117 
118     /* end of verification code */
119 
120     while (i < format->mantissa_bits) {
121         uint32_t mask = 1 << i;
122 
123         if (mantissa & mask)
124             value |= mask;
125 
126         ++i;
127     }
128 
129     while (j < format->exponenta_bits) {
130         uint32_t mask = 1 << j;
131 
132         if (exponenta & mask)
133             value |= mask << i;
134 
135         ++j;
136     }
137 
138     if (negative && format->sign)
139         value |= 1 << (i + j);
140 
141     *result = value;
142 
143     return true;
144 }
145 
vpe_convert_to_custom_float_format(struct fixed31_32 value,const struct custom_float_format * format,uint32_t * result)146 bool vpe_convert_to_custom_float_format(
147     struct fixed31_32 value, const struct custom_float_format *format, uint32_t *result)
148 {
149     uint32_t mantissa;
150     uint32_t exponenta;
151     bool     negative;
152 
153     return build_custom_float(value, format, &negative, &mantissa, &exponenta) &&
154            setup_custom_float(format, negative, mantissa, exponenta, result);
155 }
156