xref: /openbmc/qemu/target/xtensa/fpu_helper.c (revision 83a71719)
1 /*
2  * Copyright (c) 2011 - 2019, Max Filippov, Open Source and Linux Lab.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of the Open Source and Linux Lab nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include "qemu/osdep.h"
29 #include "qemu/main-loop.h"
30 #include "cpu.h"
31 #include "exec/helper-proto.h"
32 #include "qemu/host-utils.h"
33 #include "exec/exec-all.h"
34 #include "fpu/softfloat.h"
35 
36 void HELPER(wur_fcr)(CPUXtensaState *env, uint32_t v)
37 {
38     static const int rounding_mode[] = {
39         float_round_nearest_even,
40         float_round_to_zero,
41         float_round_up,
42         float_round_down,
43     };
44 
45     env->uregs[FCR] = v & 0xfffff07f;
46     set_float_rounding_mode(rounding_mode[v & 3], &env->fp_status);
47 }
48 
49 float32 HELPER(abs_s)(float32 v)
50 {
51     return float32_abs(v);
52 }
53 
54 float32 HELPER(neg_s)(float32 v)
55 {
56     return float32_chs(v);
57 }
58 
59 float32 HELPER(add_s)(CPUXtensaState *env, float32 a, float32 b)
60 {
61     return float32_add(a, b, &env->fp_status);
62 }
63 
64 float32 HELPER(sub_s)(CPUXtensaState *env, float32 a, float32 b)
65 {
66     return float32_sub(a, b, &env->fp_status);
67 }
68 
69 float32 HELPER(mul_s)(CPUXtensaState *env, float32 a, float32 b)
70 {
71     return float32_mul(a, b, &env->fp_status);
72 }
73 
74 float32 HELPER(madd_s)(CPUXtensaState *env, float32 a, float32 b, float32 c)
75 {
76     return float32_muladd(b, c, a, 0, &env->fp_status);
77 }
78 
79 float32 HELPER(msub_s)(CPUXtensaState *env, float32 a, float32 b, float32 c)
80 {
81     return float32_muladd(b, c, a, float_muladd_negate_product,
82                           &env->fp_status);
83 }
84 
85 uint32_t HELPER(ftoi)(float32 v, uint32_t rounding_mode, uint32_t scale)
86 {
87     float_status fp_status = {0};
88 
89     set_float_rounding_mode(rounding_mode, &fp_status);
90     return float32_to_int32(float32_scalbn(v, scale, &fp_status), &fp_status);
91 }
92 
93 uint32_t HELPER(ftoui)(float32 v, uint32_t rounding_mode, uint32_t scale)
94 {
95     float_status fp_status = {0};
96     float32 res;
97 
98     set_float_rounding_mode(rounding_mode, &fp_status);
99 
100     res = float32_scalbn(v, scale, &fp_status);
101 
102     if (float32_is_neg(v) && !float32_is_any_nan(v)) {
103         return float32_to_int32(res, &fp_status);
104     } else {
105         return float32_to_uint32(res, &fp_status);
106     }
107 }
108 
109 float32 HELPER(itof)(CPUXtensaState *env, uint32_t v, uint32_t scale)
110 {
111     return float32_scalbn(int32_to_float32(v, &env->fp_status),
112                           (int32_t)scale, &env->fp_status);
113 }
114 
115 float32 HELPER(uitof)(CPUXtensaState *env, uint32_t v, uint32_t scale)
116 {
117     return float32_scalbn(uint32_to_float32(v, &env->fp_status),
118                           (int32_t)scale, &env->fp_status);
119 }
120 
121 static inline void set_br(CPUXtensaState *env, bool v, uint32_t br)
122 {
123     if (v) {
124         env->sregs[BR] |= br;
125     } else {
126         env->sregs[BR] &= ~br;
127     }
128 }
129 
130 void HELPER(un_s)(CPUXtensaState *env, uint32_t br, float32 a, float32 b)
131 {
132     set_br(env, float32_unordered_quiet(a, b, &env->fp_status), br);
133 }
134 
135 void HELPER(oeq_s)(CPUXtensaState *env, uint32_t br, float32 a, float32 b)
136 {
137     set_br(env, float32_eq_quiet(a, b, &env->fp_status), br);
138 }
139 
140 void HELPER(ueq_s)(CPUXtensaState *env, uint32_t br, float32 a, float32 b)
141 {
142     int v = float32_compare_quiet(a, b, &env->fp_status);
143     set_br(env, v == float_relation_equal || v == float_relation_unordered, br);
144 }
145 
146 void HELPER(olt_s)(CPUXtensaState *env, uint32_t br, float32 a, float32 b)
147 {
148     set_br(env, float32_lt_quiet(a, b, &env->fp_status), br);
149 }
150 
151 void HELPER(ult_s)(CPUXtensaState *env, uint32_t br, float32 a, float32 b)
152 {
153     int v = float32_compare_quiet(a, b, &env->fp_status);
154     set_br(env, v == float_relation_less || v == float_relation_unordered, br);
155 }
156 
157 void HELPER(ole_s)(CPUXtensaState *env, uint32_t br, float32 a, float32 b)
158 {
159     set_br(env, float32_le_quiet(a, b, &env->fp_status), br);
160 }
161 
162 void HELPER(ule_s)(CPUXtensaState *env, uint32_t br, float32 a, float32 b)
163 {
164     int v = float32_compare_quiet(a, b, &env->fp_status);
165     set_br(env, v != float_relation_greater, br);
166 }
167