xref: /qemu/target/arm/vfp_helper.c (revision 8b0898f8)
137356079SRichard Henderson /*
237356079SRichard Henderson  * ARM VFP floating-point operations
337356079SRichard Henderson  *
437356079SRichard Henderson  *  Copyright (c) 2003 Fabrice Bellard
537356079SRichard Henderson  *
637356079SRichard Henderson  * This library is free software; you can redistribute it and/or
737356079SRichard Henderson  * modify it under the terms of the GNU Lesser General Public
837356079SRichard Henderson  * License as published by the Free Software Foundation; either
937356079SRichard Henderson  * version 2.1 of the License, or (at your option) any later version.
1037356079SRichard Henderson  *
1137356079SRichard Henderson  * This library is distributed in the hope that it will be useful,
1237356079SRichard Henderson  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1337356079SRichard Henderson  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1437356079SRichard Henderson  * Lesser General Public License for more details.
1537356079SRichard Henderson  *
1637356079SRichard Henderson  * You should have received a copy of the GNU Lesser General Public
1737356079SRichard Henderson  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
1837356079SRichard Henderson  */
1937356079SRichard Henderson 
2037356079SRichard Henderson #include "qemu/osdep.h"
2137356079SRichard Henderson #include "cpu.h"
2237356079SRichard Henderson #include "exec/helper-proto.h"
2337356079SRichard Henderson #include "internals.h"
245a534314SPeter Maydell #include "cpu-features.h"
254a15527cSPhilippe Mathieu-Daudé #ifdef CONFIG_TCG
264a15527cSPhilippe Mathieu-Daudé #include "qemu/log.h"
274a15527cSPhilippe Mathieu-Daudé #include "fpu/softfloat.h"
284a15527cSPhilippe Mathieu-Daudé #endif
2937356079SRichard Henderson 
3037356079SRichard Henderson /* VFP support.  We follow the convention used for VFP instructions:
3137356079SRichard Henderson    Single precision routines have a "s" suffix, double precision a
3237356079SRichard Henderson    "d" suffix.  */
3337356079SRichard Henderson 
344a15527cSPhilippe Mathieu-Daudé #ifdef CONFIG_TCG
354a15527cSPhilippe Mathieu-Daudé 
3637356079SRichard Henderson /* Convert host exception flags to vfp form.  */
vfp_exceptbits_from_host(int host_bits)3737356079SRichard Henderson static inline int vfp_exceptbits_from_host(int host_bits)
3837356079SRichard Henderson {
3937356079SRichard Henderson     int target_bits = 0;
4037356079SRichard Henderson 
419798ac71SPhilippe Mathieu-Daudé     if (host_bits & float_flag_invalid) {
4237356079SRichard Henderson         target_bits |= 1;
439798ac71SPhilippe Mathieu-Daudé     }
449798ac71SPhilippe Mathieu-Daudé     if (host_bits & float_flag_divbyzero) {
4537356079SRichard Henderson         target_bits |= 2;
469798ac71SPhilippe Mathieu-Daudé     }
479798ac71SPhilippe Mathieu-Daudé     if (host_bits & float_flag_overflow) {
4837356079SRichard Henderson         target_bits |= 4;
499798ac71SPhilippe Mathieu-Daudé     }
509798ac71SPhilippe Mathieu-Daudé     if (host_bits & (float_flag_underflow | float_flag_output_denormal)) {
5137356079SRichard Henderson         target_bits |= 8;
529798ac71SPhilippe Mathieu-Daudé     }
539798ac71SPhilippe Mathieu-Daudé     if (host_bits & float_flag_inexact) {
5437356079SRichard Henderson         target_bits |= 0x10;
559798ac71SPhilippe Mathieu-Daudé     }
569798ac71SPhilippe Mathieu-Daudé     if (host_bits & float_flag_input_denormal) {
5737356079SRichard Henderson         target_bits |= 0x80;
589798ac71SPhilippe Mathieu-Daudé     }
5937356079SRichard Henderson     return target_bits;
6037356079SRichard Henderson }
6137356079SRichard Henderson 
6237356079SRichard Henderson /* Convert vfp exception flags to target form.  */
vfp_exceptbits_to_host(int target_bits)6337356079SRichard Henderson static inline int vfp_exceptbits_to_host(int target_bits)
6437356079SRichard Henderson {
6537356079SRichard Henderson     int host_bits = 0;
6637356079SRichard Henderson 
679798ac71SPhilippe Mathieu-Daudé     if (target_bits & 1) {
6837356079SRichard Henderson         host_bits |= float_flag_invalid;
699798ac71SPhilippe Mathieu-Daudé     }
709798ac71SPhilippe Mathieu-Daudé     if (target_bits & 2) {
7137356079SRichard Henderson         host_bits |= float_flag_divbyzero;
729798ac71SPhilippe Mathieu-Daudé     }
739798ac71SPhilippe Mathieu-Daudé     if (target_bits & 4) {
7437356079SRichard Henderson         host_bits |= float_flag_overflow;
759798ac71SPhilippe Mathieu-Daudé     }
769798ac71SPhilippe Mathieu-Daudé     if (target_bits & 8) {
7737356079SRichard Henderson         host_bits |= float_flag_underflow;
789798ac71SPhilippe Mathieu-Daudé     }
799798ac71SPhilippe Mathieu-Daudé     if (target_bits & 0x10) {
8037356079SRichard Henderson         host_bits |= float_flag_inexact;
819798ac71SPhilippe Mathieu-Daudé     }
829798ac71SPhilippe Mathieu-Daudé     if (target_bits & 0x80) {
8337356079SRichard Henderson         host_bits |= float_flag_input_denormal;
849798ac71SPhilippe Mathieu-Daudé     }
8537356079SRichard Henderson     return host_bits;
8637356079SRichard Henderson }
8737356079SRichard Henderson 
vfp_get_fpsr_from_host(CPUARMState * env)882de7cf9eSPeter Maydell static uint32_t vfp_get_fpsr_from_host(CPUARMState *env)
890c6ad948SPhilippe Mathieu-Daudé {
900c6ad948SPhilippe Mathieu-Daudé     uint32_t i;
910c6ad948SPhilippe Mathieu-Daudé 
920c6ad948SPhilippe Mathieu-Daudé     i = get_float_exception_flags(&env->vfp.fp_status);
930c6ad948SPhilippe Mathieu-Daudé     i |= get_float_exception_flags(&env->vfp.standard_fp_status);
940c6ad948SPhilippe Mathieu-Daudé     /* FZ16 does not generate an input denormal exception.  */
950c6ad948SPhilippe Mathieu-Daudé     i |= (get_float_exception_flags(&env->vfp.fp_status_f16)
960c6ad948SPhilippe Mathieu-Daudé           & ~float_flag_input_denormal);
97aaae563bSPeter Maydell     i |= (get_float_exception_flags(&env->vfp.standard_fp_status_f16)
98aaae563bSPeter Maydell           & ~float_flag_input_denormal);
990c6ad948SPhilippe Mathieu-Daudé     return vfp_exceptbits_from_host(i);
1000c6ad948SPhilippe Mathieu-Daudé }
1010c6ad948SPhilippe Mathieu-Daudé 
vfp_set_fpsr_to_host(CPUARMState * env,uint32_t val)102b167325eSPeter Maydell static void vfp_set_fpsr_to_host(CPUARMState *env, uint32_t val)
10337356079SRichard Henderson {
104b167325eSPeter Maydell     /*
105b167325eSPeter Maydell      * The exception flags are ORed together when we read fpscr so we
106b167325eSPeter Maydell      * only need to preserve the current state in one of our
107b167325eSPeter Maydell      * float_status values.
108b167325eSPeter Maydell      */
109b167325eSPeter Maydell     int i = vfp_exceptbits_to_host(val);
110b167325eSPeter Maydell     set_float_exception_flags(i, &env->vfp.fp_status);
111b167325eSPeter Maydell     set_float_exception_flags(0, &env->vfp.fp_status_f16);
112b167325eSPeter Maydell     set_float_exception_flags(0, &env->vfp.standard_fp_status);
113b167325eSPeter Maydell     set_float_exception_flags(0, &env->vfp.standard_fp_status_f16);
114b167325eSPeter Maydell }
115b167325eSPeter Maydell 
vfp_set_fpcr_to_host(CPUARMState * env,uint32_t val,uint32_t mask)116a8ab8706SPeter Maydell static void vfp_set_fpcr_to_host(CPUARMState *env, uint32_t val, uint32_t mask)
117b167325eSPeter Maydell {
118ce07ea61SPeter Maydell     uint64_t changed = env->vfp.fpcr;
11937356079SRichard Henderson 
12037356079SRichard Henderson     changed ^= val;
121a8ab8706SPeter Maydell     changed &= mask;
12237356079SRichard Henderson     if (changed & (3 << 22)) {
123b167325eSPeter Maydell         int i = (val >> 22) & 3;
12437356079SRichard Henderson         switch (i) {
12537356079SRichard Henderson         case FPROUNDING_TIEEVEN:
12637356079SRichard Henderson             i = float_round_nearest_even;
12737356079SRichard Henderson             break;
12837356079SRichard Henderson         case FPROUNDING_POSINF:
12937356079SRichard Henderson             i = float_round_up;
13037356079SRichard Henderson             break;
13137356079SRichard Henderson         case FPROUNDING_NEGINF:
13237356079SRichard Henderson             i = float_round_down;
13337356079SRichard Henderson             break;
13437356079SRichard Henderson         case FPROUNDING_ZERO:
13537356079SRichard Henderson             i = float_round_to_zero;
13637356079SRichard Henderson             break;
13737356079SRichard Henderson         }
13837356079SRichard Henderson         set_float_rounding_mode(i, &env->vfp.fp_status);
13937356079SRichard Henderson         set_float_rounding_mode(i, &env->vfp.fp_status_f16);
14037356079SRichard Henderson     }
14137356079SRichard Henderson     if (changed & FPCR_FZ16) {
14237356079SRichard Henderson         bool ftz_enabled = val & FPCR_FZ16;
14337356079SRichard Henderson         set_flush_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
144aaae563bSPeter Maydell         set_flush_to_zero(ftz_enabled, &env->vfp.standard_fp_status_f16);
14537356079SRichard Henderson         set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
146aaae563bSPeter Maydell         set_flush_inputs_to_zero(ftz_enabled, &env->vfp.standard_fp_status_f16);
14737356079SRichard Henderson     }
14837356079SRichard Henderson     if (changed & FPCR_FZ) {
14937356079SRichard Henderson         bool ftz_enabled = val & FPCR_FZ;
15037356079SRichard Henderson         set_flush_to_zero(ftz_enabled, &env->vfp.fp_status);
15137356079SRichard Henderson         set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status);
15237356079SRichard Henderson     }
15337356079SRichard Henderson     if (changed & FPCR_DN) {
15437356079SRichard Henderson         bool dnan_enabled = val & FPCR_DN;
15537356079SRichard Henderson         set_default_nan_mode(dnan_enabled, &env->vfp.fp_status);
15637356079SRichard Henderson         set_default_nan_mode(dnan_enabled, &env->vfp.fp_status_f16);
15737356079SRichard Henderson     }
15837356079SRichard Henderson }
15937356079SRichard Henderson 
1604a15527cSPhilippe Mathieu-Daudé #else
1614a15527cSPhilippe Mathieu-Daudé 
vfp_get_fpsr_from_host(CPUARMState * env)1622de7cf9eSPeter Maydell static uint32_t vfp_get_fpsr_from_host(CPUARMState *env)
1634a15527cSPhilippe Mathieu-Daudé {
1644a15527cSPhilippe Mathieu-Daudé     return 0;
1654a15527cSPhilippe Mathieu-Daudé }
1664a15527cSPhilippe Mathieu-Daudé 
vfp_set_fpsr_to_host(CPUARMState * env,uint32_t val)167b167325eSPeter Maydell static void vfp_set_fpsr_to_host(CPUARMState *env, uint32_t val)
168b167325eSPeter Maydell {
169b167325eSPeter Maydell }
170b167325eSPeter Maydell 
vfp_set_fpcr_to_host(CPUARMState * env,uint32_t val,uint32_t mask)171a8ab8706SPeter Maydell static void vfp_set_fpcr_to_host(CPUARMState *env, uint32_t val, uint32_t mask)
1724a15527cSPhilippe Mathieu-Daudé {
1734a15527cSPhilippe Mathieu-Daudé }
1744a15527cSPhilippe Mathieu-Daudé 
1754a15527cSPhilippe Mathieu-Daudé #endif
1764a15527cSPhilippe Mathieu-Daudé 
vfp_get_fpcr(CPUARMState * env)1772de7cf9eSPeter Maydell uint32_t vfp_get_fpcr(CPUARMState *env)
178e9d65282SPhilippe Mathieu-Daudé {
179ce07ea61SPeter Maydell     uint32_t fpcr = env->vfp.fpcr
180e9d65282SPhilippe Mathieu-Daudé         | (env->vfp.vec_len << 16)
181e9d65282SPhilippe Mathieu-Daudé         | (env->vfp.vec_stride << 20);
182e9d65282SPhilippe Mathieu-Daudé 
1838128c8e8SPeter Maydell     /*
184ea861838SPeter Maydell      * M-profile LTPSIZE is the same bits [18:16] as A-profile Len; whichever
185ea861838SPeter Maydell      * of the two is not applicable to this CPU will always be zero.
1868128c8e8SPeter Maydell      */
1872de7cf9eSPeter Maydell     fpcr |= env->v7m.ltpsize << 16;
1888128c8e8SPeter Maydell 
1892de7cf9eSPeter Maydell     return fpcr;
1902de7cf9eSPeter Maydell }
1912de7cf9eSPeter Maydell 
vfp_get_fpsr(CPUARMState * env)1922de7cf9eSPeter Maydell uint32_t vfp_get_fpsr(CPUARMState *env)
1932de7cf9eSPeter Maydell {
194ce07ea61SPeter Maydell     uint32_t fpsr = env->vfp.fpsr;
1952de7cf9eSPeter Maydell     uint32_t i;
1962de7cf9eSPeter Maydell 
1972de7cf9eSPeter Maydell     fpsr |= vfp_get_fpsr_from_host(env);
198e9d65282SPhilippe Mathieu-Daudé 
199e9d65282SPhilippe Mathieu-Daudé     i = env->vfp.qc[0] | env->vfp.qc[1] | env->vfp.qc[2] | env->vfp.qc[3];
200a26db547SPeter Maydell     fpsr |= i ? FPSR_QC : 0;
2012de7cf9eSPeter Maydell     return fpsr;
2022de7cf9eSPeter Maydell }
203e9d65282SPhilippe Mathieu-Daudé 
HELPER(vfp_get_fpscr)2042de7cf9eSPeter Maydell uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
2052de7cf9eSPeter Maydell {
206db397a81SPeter Maydell     return (vfp_get_fpcr(env) & FPSCR_FPCR_MASK) |
207db397a81SPeter Maydell         (vfp_get_fpsr(env) & FPSCR_FPSR_MASK);
208e9d65282SPhilippe Mathieu-Daudé }
209e9d65282SPhilippe Mathieu-Daudé 
vfp_get_fpscr(CPUARMState * env)210e9d65282SPhilippe Mathieu-Daudé uint32_t vfp_get_fpscr(CPUARMState *env)
211e9d65282SPhilippe Mathieu-Daudé {
212e9d65282SPhilippe Mathieu-Daudé     return HELPER(vfp_get_fpscr)(env);
213e9d65282SPhilippe Mathieu-Daudé }
214e9d65282SPhilippe Mathieu-Daudé 
vfp_set_fpsr(CPUARMState * env,uint32_t val)215b167325eSPeter Maydell void vfp_set_fpsr(CPUARMState *env, uint32_t val)
216b167325eSPeter Maydell {
217b167325eSPeter Maydell     ARMCPU *cpu = env_archcpu(env);
218b167325eSPeter Maydell 
219b167325eSPeter Maydell     vfp_set_fpsr_to_host(env, val);
220b167325eSPeter Maydell 
221b167325eSPeter Maydell     if (arm_feature(env, ARM_FEATURE_NEON) ||
222b167325eSPeter Maydell         cpu_isar_feature(aa32_mve, cpu)) {
223b167325eSPeter Maydell         /*
224b167325eSPeter Maydell          * The bit we set within vfp.qc[] is arbitrary; the array as a
225b167325eSPeter Maydell          * whole being zero/non-zero is what counts.
226b167325eSPeter Maydell          */
227a26db547SPeter Maydell         env->vfp.qc[0] = val & FPSR_QC;
228b167325eSPeter Maydell         env->vfp.qc[1] = 0;
229b167325eSPeter Maydell         env->vfp.qc[2] = 0;
230b167325eSPeter Maydell         env->vfp.qc[3] = 0;
231b167325eSPeter Maydell     }
232b167325eSPeter Maydell 
233b167325eSPeter Maydell     /*
234ce07ea61SPeter Maydell      * The only FPSR bits we keep in vfp.fpsr are NZCV:
235b167325eSPeter Maydell      * the exception flags IOC|DZC|OFC|UFC|IXC|IDC are stored in
236b167325eSPeter Maydell      * fp_status, and QC is in vfp.qc[]. Store the NZCV bits there,
237ce07ea61SPeter Maydell      * and zero any of the other FPSR bits.
238b167325eSPeter Maydell      */
239a26db547SPeter Maydell     val &= FPSR_NZCV_MASK;
240ce07ea61SPeter Maydell     env->vfp.fpsr = val;
241b167325eSPeter Maydell }
242b167325eSPeter Maydell 
vfp_set_fpcr_masked(CPUARMState * env,uint32_t val,uint32_t mask)243a8ab8706SPeter Maydell static void vfp_set_fpcr_masked(CPUARMState *env, uint32_t val, uint32_t mask)
244e9d65282SPhilippe Mathieu-Daudé {
245a8ab8706SPeter Maydell     /*
246a8ab8706SPeter Maydell      * We only set FPCR bits defined by mask, and leave the others alone.
247a8ab8706SPeter Maydell      * We assume the mask is sensible (e.g. doesn't try to set only
248a8ab8706SPeter Maydell      * part of a field)
249a8ab8706SPeter Maydell      */
250b26b5629SPeter Maydell     ARMCPU *cpu = env_archcpu(env);
251b26b5629SPeter Maydell 
252e9d65282SPhilippe Mathieu-Daudé     /* When ARMv8.2-FP16 is not supported, FZ16 is RES0.  */
253b26b5629SPeter Maydell     if (!cpu_isar_feature(any_fp16, cpu)) {
254e9d65282SPhilippe Mathieu-Daudé         val &= ~FPCR_FZ16;
255e9d65282SPhilippe Mathieu-Daudé     }
256e9d65282SPhilippe Mathieu-Daudé 
257*8b0898f8SPeter Maydell     if (!cpu_isar_feature(aa64_ebf16, cpu)) {
258*8b0898f8SPeter Maydell         val &= ~FPCR_EBF;
259*8b0898f8SPeter Maydell     }
260*8b0898f8SPeter Maydell 
261a8ab8706SPeter Maydell     vfp_set_fpcr_to_host(env, val, mask);
26285795187SPhilippe Mathieu-Daudé 
263a8ab8706SPeter Maydell     if (mask & (FPCR_LEN_MASK | FPCR_STRIDE_MASK)) {
264d31e2ce6SPeter Maydell         if (!arm_feature(env, ARM_FEATURE_M)) {
265e9d65282SPhilippe Mathieu-Daudé             /*
266d31e2ce6SPeter Maydell              * Short-vector length and stride; on M-profile these bits
267d31e2ce6SPeter Maydell              * are used for different purposes.
268d31e2ce6SPeter Maydell              * We can't make this conditional be "if MVFR0.FPShVec != 0",
269d31e2ce6SPeter Maydell              * because in v7A no-short-vector-support cores still had to
270d31e2ce6SPeter Maydell              * allow Stride/Len to be written with the only effect that
271d31e2ce6SPeter Maydell              * some insns are required to UNDEF if the guest sets them.
272e9d65282SPhilippe Mathieu-Daudé              */
273d31e2ce6SPeter Maydell             env->vfp.vec_len = extract32(val, 16, 3);
274d31e2ce6SPeter Maydell             env->vfp.vec_stride = extract32(val, 20, 2);
275b26b5629SPeter Maydell         } else if (cpu_isar_feature(aa32_mve, cpu)) {
276b26b5629SPeter Maydell             env->v7m.ltpsize = extract32(val, FPCR_LTPSIZE_SHIFT,
277b26b5629SPeter Maydell                                          FPCR_LTPSIZE_LENGTH);
278d31e2ce6SPeter Maydell         }
279a8ab8706SPeter Maydell     }
280e9d65282SPhilippe Mathieu-Daudé 
281d31e2ce6SPeter Maydell     /*
282d31e2ce6SPeter Maydell      * We don't implement trapped exception handling, so the
283d31e2ce6SPeter Maydell      * trap enable bits, IDE|IXE|UFE|OFE|DZE|IOE are all RAZ/WI (not RES0!)
284d31e2ce6SPeter Maydell      *
285*8b0898f8SPeter Maydell      * The FPCR bits we keep in vfp.fpcr are AHP, DN, FZ, RMode, EBF
286b167325eSPeter Maydell      * and FZ16. Len, Stride and LTPSIZE we just handled. Store those bits
287b167325eSPeter Maydell      * there, and zero any of the other FPCR bits and the RES0 and RAZ/WI
288b167325eSPeter Maydell      * bits.
289d31e2ce6SPeter Maydell      */
290*8b0898f8SPeter Maydell     val &= FPCR_AHP | FPCR_DN | FPCR_FZ | FPCR_RMODE_MASK | FPCR_FZ16 | FPCR_EBF;
291a8ab8706SPeter Maydell     env->vfp.fpcr &= ~mask;
292a8ab8706SPeter Maydell     env->vfp.fpcr |= val;
293a8ab8706SPeter Maydell }
294a8ab8706SPeter Maydell 
vfp_set_fpcr(CPUARMState * env,uint32_t val)295a8ab8706SPeter Maydell void vfp_set_fpcr(CPUARMState *env, uint32_t val)
296a8ab8706SPeter Maydell {
297a8ab8706SPeter Maydell     vfp_set_fpcr_masked(env, val, MAKE_64BIT_MASK(0, 32));
298b167325eSPeter Maydell }
299b167325eSPeter Maydell 
HELPER(vfp_set_fpscr)300b167325eSPeter Maydell void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
301b167325eSPeter Maydell {
302a8ab8706SPeter Maydell     vfp_set_fpcr_masked(env, val, FPSCR_FPCR_MASK);
303db397a81SPeter Maydell     vfp_set_fpsr(env, val & FPSCR_FPSR_MASK);
304d31e2ce6SPeter Maydell }
305d31e2ce6SPeter Maydell 
vfp_set_fpscr(CPUARMState * env,uint32_t val)30637356079SRichard Henderson void vfp_set_fpscr(CPUARMState *env, uint32_t val)
30737356079SRichard Henderson {
30837356079SRichard Henderson     HELPER(vfp_set_fpscr)(env, val);
30937356079SRichard Henderson }
31037356079SRichard Henderson 
3114a15527cSPhilippe Mathieu-Daudé #ifdef CONFIG_TCG
3124a15527cSPhilippe Mathieu-Daudé 
31337356079SRichard Henderson #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
31437356079SRichard Henderson 
31537356079SRichard Henderson #define VFP_BINOP(name) \
316120a0eb3SPeter Maydell dh_ctype_f16 VFP_HELPER(name, h)(dh_ctype_f16 a, dh_ctype_f16 b, void *fpstp) \
317120a0eb3SPeter Maydell { \
318120a0eb3SPeter Maydell     float_status *fpst = fpstp; \
319120a0eb3SPeter Maydell     return float16_ ## name(a, b, fpst); \
320120a0eb3SPeter Maydell } \
32137356079SRichard Henderson float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
32237356079SRichard Henderson { \
32337356079SRichard Henderson     float_status *fpst = fpstp; \
32437356079SRichard Henderson     return float32_ ## name(a, b, fpst); \
32537356079SRichard Henderson } \
32637356079SRichard Henderson float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
32737356079SRichard Henderson { \
32837356079SRichard Henderson     float_status *fpst = fpstp; \
32937356079SRichard Henderson     return float64_ ## name(a, b, fpst); \
33037356079SRichard Henderson }
33137356079SRichard Henderson VFP_BINOP(add)
VFP_BINOP(sub)33237356079SRichard Henderson VFP_BINOP(sub)
33337356079SRichard Henderson VFP_BINOP(mul)
33437356079SRichard Henderson VFP_BINOP(div)
33537356079SRichard Henderson VFP_BINOP(min)
33637356079SRichard Henderson VFP_BINOP(max)
33737356079SRichard Henderson VFP_BINOP(minnum)
33837356079SRichard Henderson VFP_BINOP(maxnum)
33937356079SRichard Henderson #undef VFP_BINOP
34037356079SRichard Henderson 
341ce2d65a5SPeter Maydell dh_ctype_f16 VFP_HELPER(sqrt, h)(dh_ctype_f16 a, CPUARMState *env)
342ce2d65a5SPeter Maydell {
343ce2d65a5SPeter Maydell     return float16_sqrt(a, &env->vfp.fp_status_f16);
344ce2d65a5SPeter Maydell }
345ce2d65a5SPeter Maydell 
VFP_HELPER(sqrt,s)34637356079SRichard Henderson float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
34737356079SRichard Henderson {
34837356079SRichard Henderson     return float32_sqrt(a, &env->vfp.fp_status);
34937356079SRichard Henderson }
35037356079SRichard Henderson 
VFP_HELPER(sqrt,d)35137356079SRichard Henderson float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
35237356079SRichard Henderson {
35337356079SRichard Henderson     return float64_sqrt(a, &env->vfp.fp_status);
35437356079SRichard Henderson }
35537356079SRichard Henderson 
softfloat_to_vfp_compare(CPUARMState * env,FloatRelation cmp)35671bfd65cSRichard Henderson static void softfloat_to_vfp_compare(CPUARMState *env, FloatRelation cmp)
35737356079SRichard Henderson {
35837356079SRichard Henderson     uint32_t flags;
35937356079SRichard Henderson     switch (cmp) {
36037356079SRichard Henderson     case float_relation_equal:
36137356079SRichard Henderson         flags = 0x6;
36237356079SRichard Henderson         break;
36337356079SRichard Henderson     case float_relation_less:
36437356079SRichard Henderson         flags = 0x8;
36537356079SRichard Henderson         break;
36637356079SRichard Henderson     case float_relation_greater:
36737356079SRichard Henderson         flags = 0x2;
36837356079SRichard Henderson         break;
36937356079SRichard Henderson     case float_relation_unordered:
37037356079SRichard Henderson         flags = 0x3;
37137356079SRichard Henderson         break;
37237356079SRichard Henderson     default:
37337356079SRichard Henderson         g_assert_not_reached();
37437356079SRichard Henderson     }
375ce07ea61SPeter Maydell     env->vfp.fpsr = deposit64(env->vfp.fpsr, 28, 4, flags); /* NZCV */
37637356079SRichard Henderson }
37737356079SRichard Henderson 
37837356079SRichard Henderson /* XXX: check quiet/signaling case */
3791b88b054SPeter Maydell #define DO_VFP_cmp(P, FLOATTYPE, ARGTYPE, FPST) \
3801b88b054SPeter Maydell void VFP_HELPER(cmp, P)(ARGTYPE a, ARGTYPE b, CPUARMState *env)  \
38137356079SRichard Henderson { \
38237356079SRichard Henderson     softfloat_to_vfp_compare(env, \
3831b88b054SPeter Maydell         FLOATTYPE ## _compare_quiet(a, b, &env->vfp.FPST)); \
38437356079SRichard Henderson } \
3851b88b054SPeter Maydell void VFP_HELPER(cmpe, P)(ARGTYPE a, ARGTYPE b, CPUARMState *env) \
38637356079SRichard Henderson { \
38737356079SRichard Henderson     softfloat_to_vfp_compare(env, \
3881b88b054SPeter Maydell         FLOATTYPE ## _compare(a, b, &env->vfp.FPST)); \
38937356079SRichard Henderson }
DO_VFP_cmp(h,float16,dh_ctype_f16,fp_status_f16)3901b88b054SPeter Maydell DO_VFP_cmp(h, float16, dh_ctype_f16, fp_status_f16)
3911b88b054SPeter Maydell DO_VFP_cmp(s, float32, float32, fp_status)
3921b88b054SPeter Maydell DO_VFP_cmp(d, float64, float64, fp_status)
39337356079SRichard Henderson #undef DO_VFP_cmp
39437356079SRichard Henderson 
39537356079SRichard Henderson /* Integer to float and float to integer conversions */
39637356079SRichard Henderson 
39737356079SRichard Henderson #define CONV_ITOF(name, ftype, fsz, sign)                           \
39837356079SRichard Henderson ftype HELPER(name)(uint32_t x, void *fpstp)                         \
39937356079SRichard Henderson {                                                                   \
40037356079SRichard Henderson     float_status *fpst = fpstp;                                     \
40137356079SRichard Henderson     return sign##int32_to_##float##fsz((sign##int32_t)x, fpst);     \
40237356079SRichard Henderson }
40337356079SRichard Henderson 
40437356079SRichard Henderson #define CONV_FTOI(name, ftype, fsz, sign, round)                \
40537356079SRichard Henderson sign##int32_t HELPER(name)(ftype x, void *fpstp)                \
40637356079SRichard Henderson {                                                               \
40737356079SRichard Henderson     float_status *fpst = fpstp;                                 \
40837356079SRichard Henderson     if (float##fsz##_is_any_nan(x)) {                           \
40937356079SRichard Henderson         float_raise(float_flag_invalid, fpst);                  \
41037356079SRichard Henderson         return 0;                                               \
41137356079SRichard Henderson     }                                                           \
41237356079SRichard Henderson     return float##fsz##_to_##sign##int32##round(x, fpst);       \
41337356079SRichard Henderson }
41437356079SRichard Henderson 
41537356079SRichard Henderson #define FLOAT_CONVS(name, p, ftype, fsz, sign)            \
41637356079SRichard Henderson     CONV_ITOF(vfp_##name##to##p, ftype, fsz, sign)        \
41737356079SRichard Henderson     CONV_FTOI(vfp_to##name##p, ftype, fsz, sign, )        \
41837356079SRichard Henderson     CONV_FTOI(vfp_to##name##z##p, ftype, fsz, sign, _round_to_zero)
41937356079SRichard Henderson 
42037356079SRichard Henderson FLOAT_CONVS(si, h, uint32_t, 16, )
42137356079SRichard Henderson FLOAT_CONVS(si, s, float32, 32, )
42237356079SRichard Henderson FLOAT_CONVS(si, d, float64, 64, )
42337356079SRichard Henderson FLOAT_CONVS(ui, h, uint32_t, 16, u)
42437356079SRichard Henderson FLOAT_CONVS(ui, s, float32, 32, u)
42537356079SRichard Henderson FLOAT_CONVS(ui, d, float64, 64, u)
42637356079SRichard Henderson 
42737356079SRichard Henderson #undef CONV_ITOF
42837356079SRichard Henderson #undef CONV_FTOI
42937356079SRichard Henderson #undef FLOAT_CONVS
43037356079SRichard Henderson 
43137356079SRichard Henderson /* floating point conversion */
43237356079SRichard Henderson float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
43337356079SRichard Henderson {
43437356079SRichard Henderson     return float32_to_float64(x, &env->vfp.fp_status);
43537356079SRichard Henderson }
43637356079SRichard Henderson 
VFP_HELPER(fcvts,d)43737356079SRichard Henderson float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
43837356079SRichard Henderson {
43937356079SRichard Henderson     return float64_to_float32(x, &env->vfp.fp_status);
44037356079SRichard Henderson }
44137356079SRichard Henderson 
HELPER(bfcvt)4423a98ac40SRichard Henderson uint32_t HELPER(bfcvt)(float32 x, void *status)
4433a98ac40SRichard Henderson {
4443a98ac40SRichard Henderson     return float32_to_bfloat16(x, status);
4453a98ac40SRichard Henderson }
4463a98ac40SRichard Henderson 
HELPER(bfcvt_pair)447d29b17caSRichard Henderson uint32_t HELPER(bfcvt_pair)(uint64_t pair, void *status)
448d29b17caSRichard Henderson {
449d29b17caSRichard Henderson     bfloat16 lo = float32_to_bfloat16(extract64(pair, 0, 32), status);
450d29b17caSRichard Henderson     bfloat16 hi = float32_to_bfloat16(extract64(pair, 32, 32), status);
451d29b17caSRichard Henderson     return deposit32(lo, 16, 16, hi);
452d29b17caSRichard Henderson }
453d29b17caSRichard Henderson 
45461db12d9SPeter Maydell /*
45561db12d9SPeter Maydell  * VFP3 fixed point conversion. The AArch32 versions of fix-to-float
45661db12d9SPeter Maydell  * must always round-to-nearest; the AArch64 ones honour the FPSCR
45761db12d9SPeter Maydell  * rounding mode. (For AArch32 Neon the standard-FPSCR is set to
45861db12d9SPeter Maydell  * round-to-nearest so either helper will work.) AArch32 float-to-fix
45961db12d9SPeter Maydell  * must round-to-zero.
46061db12d9SPeter Maydell  */
4615366f6adSPeter Maydell #define VFP_CONV_FIX_FLOAT(name, p, fsz, ftype, isz, itype)            \
4625366f6adSPeter Maydell ftype HELPER(vfp_##name##to##p)(uint##isz##_t  x, uint32_t shift,      \
46337356079SRichard Henderson                                      void *fpstp) \
46437356079SRichard Henderson { return itype##_to_##float##fsz##_scalbn(x, -shift, fpstp); }
46537356079SRichard Henderson 
46661db12d9SPeter Maydell #define VFP_CONV_FIX_FLOAT_ROUND(name, p, fsz, ftype, isz, itype)      \
46761db12d9SPeter Maydell     ftype HELPER(vfp_##name##to##p##_round_to_nearest)(uint##isz##_t  x, \
46861db12d9SPeter Maydell                                                      uint32_t shift,   \
46961db12d9SPeter Maydell                                                      void *fpstp)      \
47061db12d9SPeter Maydell     {                                                                  \
47161db12d9SPeter Maydell         ftype ret;                                                     \
47261db12d9SPeter Maydell         float_status *fpst = fpstp;                                    \
47361db12d9SPeter Maydell         FloatRoundMode oldmode = fpst->float_rounding_mode;            \
47461db12d9SPeter Maydell         fpst->float_rounding_mode = float_round_nearest_even;          \
47561db12d9SPeter Maydell         ret = itype##_to_##float##fsz##_scalbn(x, -shift, fpstp);      \
47661db12d9SPeter Maydell         fpst->float_rounding_mode = oldmode;                           \
47761db12d9SPeter Maydell         return ret;                                                    \
47861db12d9SPeter Maydell     }
47961db12d9SPeter Maydell 
4805366f6adSPeter Maydell #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, ftype, isz, itype, ROUND, suff) \
4815366f6adSPeter Maydell uint##isz##_t HELPER(vfp_to##name##p##suff)(ftype x, uint32_t shift,      \
48237356079SRichard Henderson                                             void *fpst)                   \
48337356079SRichard Henderson {                                                                         \
48437356079SRichard Henderson     if (unlikely(float##fsz##_is_any_nan(x))) {                           \
48537356079SRichard Henderson         float_raise(float_flag_invalid, fpst);                            \
48637356079SRichard Henderson         return 0;                                                         \
48737356079SRichard Henderson     }                                                                     \
48837356079SRichard Henderson     return float##fsz##_to_##itype##_scalbn(x, ROUND, shift, fpst);       \
48937356079SRichard Henderson }
49037356079SRichard Henderson 
4915366f6adSPeter Maydell #define VFP_CONV_FIX(name, p, fsz, ftype, isz, itype)            \
4925366f6adSPeter Maydell VFP_CONV_FIX_FLOAT(name, p, fsz, ftype, isz, itype)              \
49361db12d9SPeter Maydell VFP_CONV_FIX_FLOAT_ROUND(name, p, fsz, ftype, isz, itype)        \
4945366f6adSPeter Maydell VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, ftype, isz, itype,        \
49537356079SRichard Henderson                          float_round_to_zero, _round_to_zero)    \
4965366f6adSPeter Maydell VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, ftype, isz, itype,        \
49737356079SRichard Henderson                          get_float_rounding_mode(fpst), )
49837356079SRichard Henderson 
4995366f6adSPeter Maydell #define VFP_CONV_FIX_A64(name, p, fsz, ftype, isz, itype)        \
5005366f6adSPeter Maydell VFP_CONV_FIX_FLOAT(name, p, fsz, ftype, isz, itype)              \
5015366f6adSPeter Maydell VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, ftype, isz, itype,        \
50237356079SRichard Henderson                          get_float_rounding_mode(fpst), )
50337356079SRichard Henderson 
5045366f6adSPeter Maydell VFP_CONV_FIX(sh, d, 64, float64, 64, int16)
5055366f6adSPeter Maydell VFP_CONV_FIX(sl, d, 64, float64, 64, int32)
5065366f6adSPeter Maydell VFP_CONV_FIX_A64(sq, d, 64, float64, 64, int64)
5075366f6adSPeter Maydell VFP_CONV_FIX(uh, d, 64, float64, 64, uint16)
5085366f6adSPeter Maydell VFP_CONV_FIX(ul, d, 64, float64, 64, uint32)
5095366f6adSPeter Maydell VFP_CONV_FIX_A64(uq, d, 64, float64, 64, uint64)
5105366f6adSPeter Maydell VFP_CONV_FIX(sh, s, 32, float32, 32, int16)
5115366f6adSPeter Maydell VFP_CONV_FIX(sl, s, 32, float32, 32, int32)
5125366f6adSPeter Maydell VFP_CONV_FIX_A64(sq, s, 32, float32, 64, int64)
5135366f6adSPeter Maydell VFP_CONV_FIX(uh, s, 32, float32, 32, uint16)
5145366f6adSPeter Maydell VFP_CONV_FIX(ul, s, 32, float32, 32, uint32)
5155366f6adSPeter Maydell VFP_CONV_FIX_A64(uq, s, 32, float32, 64, uint64)
516414ba270SPeter Maydell VFP_CONV_FIX(sh, h, 16, dh_ctype_f16, 32, int16)
517414ba270SPeter Maydell VFP_CONV_FIX(sl, h, 16, dh_ctype_f16, 32, int32)
518414ba270SPeter Maydell VFP_CONV_FIX_A64(sq, h, 16, dh_ctype_f16, 64, int64)
519414ba270SPeter Maydell VFP_CONV_FIX(uh, h, 16, dh_ctype_f16, 32, uint16)
520414ba270SPeter Maydell VFP_CONV_FIX(ul, h, 16, dh_ctype_f16, 32, uint32)
521414ba270SPeter Maydell VFP_CONV_FIX_A64(uq, h, 16, dh_ctype_f16, 64, uint64)
52237356079SRichard Henderson 
52337356079SRichard Henderson #undef VFP_CONV_FIX
52437356079SRichard Henderson #undef VFP_CONV_FIX_FLOAT
52537356079SRichard Henderson #undef VFP_CONV_FLOAT_FIX_ROUND
52637356079SRichard Henderson #undef VFP_CONV_FIX_A64
52737356079SRichard Henderson 
52837356079SRichard Henderson /* Set the current fp rounding mode and return the old one.
52937356079SRichard Henderson  * The argument is a softfloat float_round_ value.
53037356079SRichard Henderson  */
HELPER(set_rmode)53137356079SRichard Henderson uint32_t HELPER(set_rmode)(uint32_t rmode, void *fpstp)
53237356079SRichard Henderson {
53337356079SRichard Henderson     float_status *fp_status = fpstp;
53437356079SRichard Henderson 
53537356079SRichard Henderson     uint32_t prev_rmode = get_float_rounding_mode(fp_status);
53637356079SRichard Henderson     set_float_rounding_mode(rmode, fp_status);
53737356079SRichard Henderson 
53837356079SRichard Henderson     return prev_rmode;
53937356079SRichard Henderson }
54037356079SRichard Henderson 
54137356079SRichard Henderson /* Half precision conversions.  */
HELPER(vfp_fcvt_f16_to_f32)54237356079SRichard Henderson float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, void *fpstp, uint32_t ahp_mode)
54337356079SRichard Henderson {
54437356079SRichard Henderson     /* Squash FZ16 to 0 for the duration of conversion.  In this case,
54537356079SRichard Henderson      * it would affect flushing input denormals.
54637356079SRichard Henderson      */
54737356079SRichard Henderson     float_status *fpst = fpstp;
548c120391cSRichard Henderson     bool save = get_flush_inputs_to_zero(fpst);
54937356079SRichard Henderson     set_flush_inputs_to_zero(false, fpst);
55037356079SRichard Henderson     float32 r = float16_to_float32(a, !ahp_mode, fpst);
55137356079SRichard Henderson     set_flush_inputs_to_zero(save, fpst);
55237356079SRichard Henderson     return r;
55337356079SRichard Henderson }
55437356079SRichard Henderson 
HELPER(vfp_fcvt_f32_to_f16)55537356079SRichard Henderson uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, void *fpstp, uint32_t ahp_mode)
55637356079SRichard Henderson {
55737356079SRichard Henderson     /* Squash FZ16 to 0 for the duration of conversion.  In this case,
55837356079SRichard Henderson      * it would affect flushing output denormals.
55937356079SRichard Henderson      */
56037356079SRichard Henderson     float_status *fpst = fpstp;
561c120391cSRichard Henderson     bool save = get_flush_to_zero(fpst);
56237356079SRichard Henderson     set_flush_to_zero(false, fpst);
56337356079SRichard Henderson     float16 r = float32_to_float16(a, !ahp_mode, fpst);
56437356079SRichard Henderson     set_flush_to_zero(save, fpst);
56537356079SRichard Henderson     return r;
56637356079SRichard Henderson }
56737356079SRichard Henderson 
HELPER(vfp_fcvt_f16_to_f64)56837356079SRichard Henderson float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, void *fpstp, uint32_t ahp_mode)
56937356079SRichard Henderson {
57037356079SRichard Henderson     /* Squash FZ16 to 0 for the duration of conversion.  In this case,
57137356079SRichard Henderson      * it would affect flushing input denormals.
57237356079SRichard Henderson      */
57337356079SRichard Henderson     float_status *fpst = fpstp;
574c120391cSRichard Henderson     bool save = get_flush_inputs_to_zero(fpst);
57537356079SRichard Henderson     set_flush_inputs_to_zero(false, fpst);
57637356079SRichard Henderson     float64 r = float16_to_float64(a, !ahp_mode, fpst);
57737356079SRichard Henderson     set_flush_inputs_to_zero(save, fpst);
57837356079SRichard Henderson     return r;
57937356079SRichard Henderson }
58037356079SRichard Henderson 
HELPER(vfp_fcvt_f64_to_f16)58137356079SRichard Henderson uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, void *fpstp, uint32_t ahp_mode)
58237356079SRichard Henderson {
58337356079SRichard Henderson     /* Squash FZ16 to 0 for the duration of conversion.  In this case,
58437356079SRichard Henderson      * it would affect flushing output denormals.
58537356079SRichard Henderson      */
58637356079SRichard Henderson     float_status *fpst = fpstp;
587c120391cSRichard Henderson     bool save = get_flush_to_zero(fpst);
58837356079SRichard Henderson     set_flush_to_zero(false, fpst);
58937356079SRichard Henderson     float16 r = float64_to_float16(a, !ahp_mode, fpst);
59037356079SRichard Henderson     set_flush_to_zero(save, fpst);
59137356079SRichard Henderson     return r;
59237356079SRichard Henderson }
59337356079SRichard Henderson 
59437356079SRichard Henderson /* NEON helpers.  */
59537356079SRichard Henderson 
59637356079SRichard Henderson /* Constants 256 and 512 are used in some helpers; we avoid relying on
59737356079SRichard Henderson  * int->float conversions at run-time.  */
59837356079SRichard Henderson #define float64_256 make_float64(0x4070000000000000LL)
59937356079SRichard Henderson #define float64_512 make_float64(0x4080000000000000LL)
60037356079SRichard Henderson #define float16_maxnorm make_float16(0x7bff)
60137356079SRichard Henderson #define float32_maxnorm make_float32(0x7f7fffff)
60237356079SRichard Henderson #define float64_maxnorm make_float64(0x7fefffffffffffffLL)
60337356079SRichard Henderson 
60437356079SRichard Henderson /* Reciprocal functions
60537356079SRichard Henderson  *
60637356079SRichard Henderson  * The algorithm that must be used to calculate the estimate
60737356079SRichard Henderson  * is specified by the ARM ARM, see FPRecipEstimate()/RecipEstimate
60837356079SRichard Henderson  */
60937356079SRichard Henderson 
61037356079SRichard Henderson /* See RecipEstimate()
61137356079SRichard Henderson  *
61237356079SRichard Henderson  * input is a 9 bit fixed point number
61337356079SRichard Henderson  * input range 256 .. 511 for a number from 0.5 <= x < 1.0.
61437356079SRichard Henderson  * result range 256 .. 511 for a number from 1.0 to 511/256.
61537356079SRichard Henderson  */
61637356079SRichard Henderson 
recip_estimate(int input)61737356079SRichard Henderson static int recip_estimate(int input)
61837356079SRichard Henderson {
61937356079SRichard Henderson     int a, b, r;
62037356079SRichard Henderson     assert(256 <= input && input < 512);
62137356079SRichard Henderson     a = (input * 2) + 1;
62237356079SRichard Henderson     b = (1 << 19) / a;
62337356079SRichard Henderson     r = (b + 1) >> 1;
62437356079SRichard Henderson     assert(256 <= r && r < 512);
62537356079SRichard Henderson     return r;
62637356079SRichard Henderson }
62737356079SRichard Henderson 
62837356079SRichard Henderson /*
62937356079SRichard Henderson  * Common wrapper to call recip_estimate
63037356079SRichard Henderson  *
63137356079SRichard Henderson  * The parameters are exponent and 64 bit fraction (without implicit
63237356079SRichard Henderson  * bit) where the binary point is nominally at bit 52. Returns a
63337356079SRichard Henderson  * float64 which can then be rounded to the appropriate size by the
63437356079SRichard Henderson  * callee.
63537356079SRichard Henderson  */
63637356079SRichard Henderson 
call_recip_estimate(int * exp,int exp_off,uint64_t frac)63737356079SRichard Henderson static uint64_t call_recip_estimate(int *exp, int exp_off, uint64_t frac)
63837356079SRichard Henderson {
63937356079SRichard Henderson     uint32_t scaled, estimate;
64037356079SRichard Henderson     uint64_t result_frac;
64137356079SRichard Henderson     int result_exp;
64237356079SRichard Henderson 
64337356079SRichard Henderson     /* Handle sub-normals */
64437356079SRichard Henderson     if (*exp == 0) {
64537356079SRichard Henderson         if (extract64(frac, 51, 1) == 0) {
64637356079SRichard Henderson             *exp = -1;
64737356079SRichard Henderson             frac <<= 2;
64837356079SRichard Henderson         } else {
64937356079SRichard Henderson             frac <<= 1;
65037356079SRichard Henderson         }
65137356079SRichard Henderson     }
65237356079SRichard Henderson 
65337356079SRichard Henderson     /* scaled = UInt('1':fraction<51:44>) */
65437356079SRichard Henderson     scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8));
65537356079SRichard Henderson     estimate = recip_estimate(scaled);
65637356079SRichard Henderson 
65737356079SRichard Henderson     result_exp = exp_off - *exp;
65837356079SRichard Henderson     result_frac = deposit64(0, 44, 8, estimate);
65937356079SRichard Henderson     if (result_exp == 0) {
66037356079SRichard Henderson         result_frac = deposit64(result_frac >> 1, 51, 1, 1);
66137356079SRichard Henderson     } else if (result_exp == -1) {
66237356079SRichard Henderson         result_frac = deposit64(result_frac >> 2, 50, 2, 1);
66337356079SRichard Henderson         result_exp = 0;
66437356079SRichard Henderson     }
66537356079SRichard Henderson 
66637356079SRichard Henderson     *exp = result_exp;
66737356079SRichard Henderson 
66837356079SRichard Henderson     return result_frac;
66937356079SRichard Henderson }
67037356079SRichard Henderson 
round_to_inf(float_status * fpst,bool sign_bit)67137356079SRichard Henderson static bool round_to_inf(float_status *fpst, bool sign_bit)
67237356079SRichard Henderson {
67337356079SRichard Henderson     switch (fpst->float_rounding_mode) {
67437356079SRichard Henderson     case float_round_nearest_even: /* Round to Nearest */
67537356079SRichard Henderson         return true;
67637356079SRichard Henderson     case float_round_up: /* Round to +Inf */
67737356079SRichard Henderson         return !sign_bit;
67837356079SRichard Henderson     case float_round_down: /* Round to -Inf */
67937356079SRichard Henderson         return sign_bit;
68037356079SRichard Henderson     case float_round_to_zero: /* Round to Zero */
68137356079SRichard Henderson         return false;
6823dede407SRichard Henderson     default:
68337356079SRichard Henderson         g_assert_not_reached();
68437356079SRichard Henderson     }
6853dede407SRichard Henderson }
68637356079SRichard Henderson 
HELPER(recpe_f16)68737356079SRichard Henderson uint32_t HELPER(recpe_f16)(uint32_t input, void *fpstp)
68837356079SRichard Henderson {
68937356079SRichard Henderson     float_status *fpst = fpstp;
69037356079SRichard Henderson     float16 f16 = float16_squash_input_denormal(input, fpst);
69137356079SRichard Henderson     uint32_t f16_val = float16_val(f16);
69237356079SRichard Henderson     uint32_t f16_sign = float16_is_neg(f16);
69337356079SRichard Henderson     int f16_exp = extract32(f16_val, 10, 5);
69437356079SRichard Henderson     uint32_t f16_frac = extract32(f16_val, 0, 10);
69537356079SRichard Henderson     uint64_t f64_frac;
69637356079SRichard Henderson 
69737356079SRichard Henderson     if (float16_is_any_nan(f16)) {
69837356079SRichard Henderson         float16 nan = f16;
69937356079SRichard Henderson         if (float16_is_signaling_nan(f16, fpst)) {
70037356079SRichard Henderson             float_raise(float_flag_invalid, fpst);
701103e7579SJoe Komlodi             if (!fpst->default_nan_mode) {
70237356079SRichard Henderson                 nan = float16_silence_nan(f16, fpst);
70337356079SRichard Henderson             }
704103e7579SJoe Komlodi         }
70537356079SRichard Henderson         if (fpst->default_nan_mode) {
70637356079SRichard Henderson             nan =  float16_default_nan(fpst);
70737356079SRichard Henderson         }
70837356079SRichard Henderson         return nan;
70937356079SRichard Henderson     } else if (float16_is_infinity(f16)) {
71037356079SRichard Henderson         return float16_set_sign(float16_zero, float16_is_neg(f16));
71137356079SRichard Henderson     } else if (float16_is_zero(f16)) {
71237356079SRichard Henderson         float_raise(float_flag_divbyzero, fpst);
71337356079SRichard Henderson         return float16_set_sign(float16_infinity, float16_is_neg(f16));
71437356079SRichard Henderson     } else if (float16_abs(f16) < (1 << 8)) {
71537356079SRichard Henderson         /* Abs(value) < 2.0^-16 */
71637356079SRichard Henderson         float_raise(float_flag_overflow | float_flag_inexact, fpst);
71737356079SRichard Henderson         if (round_to_inf(fpst, f16_sign)) {
71837356079SRichard Henderson             return float16_set_sign(float16_infinity, f16_sign);
71937356079SRichard Henderson         } else {
72037356079SRichard Henderson             return float16_set_sign(float16_maxnorm, f16_sign);
72137356079SRichard Henderson         }
72237356079SRichard Henderson     } else if (f16_exp >= 29 && fpst->flush_to_zero) {
72337356079SRichard Henderson         float_raise(float_flag_underflow, fpst);
72437356079SRichard Henderson         return float16_set_sign(float16_zero, float16_is_neg(f16));
72537356079SRichard Henderson     }
72637356079SRichard Henderson 
72737356079SRichard Henderson     f64_frac = call_recip_estimate(&f16_exp, 29,
72837356079SRichard Henderson                                    ((uint64_t) f16_frac) << (52 - 10));
72937356079SRichard Henderson 
73037356079SRichard Henderson     /* result = sign : result_exp<4:0> : fraction<51:42> */
73137356079SRichard Henderson     f16_val = deposit32(0, 15, 1, f16_sign);
73237356079SRichard Henderson     f16_val = deposit32(f16_val, 10, 5, f16_exp);
73337356079SRichard Henderson     f16_val = deposit32(f16_val, 0, 10, extract64(f64_frac, 52 - 10, 10));
73437356079SRichard Henderson     return make_float16(f16_val);
73537356079SRichard Henderson }
73637356079SRichard Henderson 
HELPER(recpe_f32)73737356079SRichard Henderson float32 HELPER(recpe_f32)(float32 input, void *fpstp)
73837356079SRichard Henderson {
73937356079SRichard Henderson     float_status *fpst = fpstp;
74037356079SRichard Henderson     float32 f32 = float32_squash_input_denormal(input, fpst);
74137356079SRichard Henderson     uint32_t f32_val = float32_val(f32);
74237356079SRichard Henderson     bool f32_sign = float32_is_neg(f32);
74337356079SRichard Henderson     int f32_exp = extract32(f32_val, 23, 8);
74437356079SRichard Henderson     uint32_t f32_frac = extract32(f32_val, 0, 23);
74537356079SRichard Henderson     uint64_t f64_frac;
74637356079SRichard Henderson 
74737356079SRichard Henderson     if (float32_is_any_nan(f32)) {
74837356079SRichard Henderson         float32 nan = f32;
74937356079SRichard Henderson         if (float32_is_signaling_nan(f32, fpst)) {
75037356079SRichard Henderson             float_raise(float_flag_invalid, fpst);
751103e7579SJoe Komlodi             if (!fpst->default_nan_mode) {
75237356079SRichard Henderson                 nan = float32_silence_nan(f32, fpst);
75337356079SRichard Henderson             }
754103e7579SJoe Komlodi         }
75537356079SRichard Henderson         if (fpst->default_nan_mode) {
75637356079SRichard Henderson             nan =  float32_default_nan(fpst);
75737356079SRichard Henderson         }
75837356079SRichard Henderson         return nan;
75937356079SRichard Henderson     } else if (float32_is_infinity(f32)) {
76037356079SRichard Henderson         return float32_set_sign(float32_zero, float32_is_neg(f32));
76137356079SRichard Henderson     } else if (float32_is_zero(f32)) {
76237356079SRichard Henderson         float_raise(float_flag_divbyzero, fpst);
76337356079SRichard Henderson         return float32_set_sign(float32_infinity, float32_is_neg(f32));
76437356079SRichard Henderson     } else if (float32_abs(f32) < (1ULL << 21)) {
76537356079SRichard Henderson         /* Abs(value) < 2.0^-128 */
76637356079SRichard Henderson         float_raise(float_flag_overflow | float_flag_inexact, fpst);
76737356079SRichard Henderson         if (round_to_inf(fpst, f32_sign)) {
76837356079SRichard Henderson             return float32_set_sign(float32_infinity, f32_sign);
76937356079SRichard Henderson         } else {
77037356079SRichard Henderson             return float32_set_sign(float32_maxnorm, f32_sign);
77137356079SRichard Henderson         }
77237356079SRichard Henderson     } else if (f32_exp >= 253 && fpst->flush_to_zero) {
77337356079SRichard Henderson         float_raise(float_flag_underflow, fpst);
77437356079SRichard Henderson         return float32_set_sign(float32_zero, float32_is_neg(f32));
77537356079SRichard Henderson     }
77637356079SRichard Henderson 
77737356079SRichard Henderson     f64_frac = call_recip_estimate(&f32_exp, 253,
77837356079SRichard Henderson                                    ((uint64_t) f32_frac) << (52 - 23));
77937356079SRichard Henderson 
78037356079SRichard Henderson     /* result = sign : result_exp<7:0> : fraction<51:29> */
78137356079SRichard Henderson     f32_val = deposit32(0, 31, 1, f32_sign);
78237356079SRichard Henderson     f32_val = deposit32(f32_val, 23, 8, f32_exp);
78337356079SRichard Henderson     f32_val = deposit32(f32_val, 0, 23, extract64(f64_frac, 52 - 23, 23));
78437356079SRichard Henderson     return make_float32(f32_val);
78537356079SRichard Henderson }
78637356079SRichard Henderson 
HELPER(recpe_f64)78737356079SRichard Henderson float64 HELPER(recpe_f64)(float64 input, void *fpstp)
78837356079SRichard Henderson {
78937356079SRichard Henderson     float_status *fpst = fpstp;
79037356079SRichard Henderson     float64 f64 = float64_squash_input_denormal(input, fpst);
79137356079SRichard Henderson     uint64_t f64_val = float64_val(f64);
79237356079SRichard Henderson     bool f64_sign = float64_is_neg(f64);
79337356079SRichard Henderson     int f64_exp = extract64(f64_val, 52, 11);
79437356079SRichard Henderson     uint64_t f64_frac = extract64(f64_val, 0, 52);
79537356079SRichard Henderson 
79637356079SRichard Henderson     /* Deal with any special cases */
79737356079SRichard Henderson     if (float64_is_any_nan(f64)) {
79837356079SRichard Henderson         float64 nan = f64;
79937356079SRichard Henderson         if (float64_is_signaling_nan(f64, fpst)) {
80037356079SRichard Henderson             float_raise(float_flag_invalid, fpst);
801103e7579SJoe Komlodi             if (!fpst->default_nan_mode) {
80237356079SRichard Henderson                 nan = float64_silence_nan(f64, fpst);
80337356079SRichard Henderson             }
804103e7579SJoe Komlodi         }
80537356079SRichard Henderson         if (fpst->default_nan_mode) {
80637356079SRichard Henderson             nan =  float64_default_nan(fpst);
80737356079SRichard Henderson         }
80837356079SRichard Henderson         return nan;
80937356079SRichard Henderson     } else if (float64_is_infinity(f64)) {
81037356079SRichard Henderson         return float64_set_sign(float64_zero, float64_is_neg(f64));
81137356079SRichard Henderson     } else if (float64_is_zero(f64)) {
81237356079SRichard Henderson         float_raise(float_flag_divbyzero, fpst);
81337356079SRichard Henderson         return float64_set_sign(float64_infinity, float64_is_neg(f64));
81437356079SRichard Henderson     } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
81537356079SRichard Henderson         /* Abs(value) < 2.0^-1024 */
81637356079SRichard Henderson         float_raise(float_flag_overflow | float_flag_inexact, fpst);
81737356079SRichard Henderson         if (round_to_inf(fpst, f64_sign)) {
81837356079SRichard Henderson             return float64_set_sign(float64_infinity, f64_sign);
81937356079SRichard Henderson         } else {
82037356079SRichard Henderson             return float64_set_sign(float64_maxnorm, f64_sign);
82137356079SRichard Henderson         }
82237356079SRichard Henderson     } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
82337356079SRichard Henderson         float_raise(float_flag_underflow, fpst);
82437356079SRichard Henderson         return float64_set_sign(float64_zero, float64_is_neg(f64));
82537356079SRichard Henderson     }
82637356079SRichard Henderson 
82737356079SRichard Henderson     f64_frac = call_recip_estimate(&f64_exp, 2045, f64_frac);
82837356079SRichard Henderson 
82937356079SRichard Henderson     /* result = sign : result_exp<10:0> : fraction<51:0>; */
83037356079SRichard Henderson     f64_val = deposit64(0, 63, 1, f64_sign);
83137356079SRichard Henderson     f64_val = deposit64(f64_val, 52, 11, f64_exp);
83237356079SRichard Henderson     f64_val = deposit64(f64_val, 0, 52, f64_frac);
83337356079SRichard Henderson     return make_float64(f64_val);
83437356079SRichard Henderson }
83537356079SRichard Henderson 
83637356079SRichard Henderson /* The algorithm that must be used to calculate the estimate
83737356079SRichard Henderson  * is specified by the ARM ARM.
83837356079SRichard Henderson  */
83937356079SRichard Henderson 
do_recip_sqrt_estimate(int a)84037356079SRichard Henderson static int do_recip_sqrt_estimate(int a)
84137356079SRichard Henderson {
84237356079SRichard Henderson     int b, estimate;
84337356079SRichard Henderson 
84437356079SRichard Henderson     assert(128 <= a && a < 512);
84537356079SRichard Henderson     if (a < 256) {
84637356079SRichard Henderson         a = a * 2 + 1;
84737356079SRichard Henderson     } else {
84837356079SRichard Henderson         a = (a >> 1) << 1;
84937356079SRichard Henderson         a = (a + 1) * 2;
85037356079SRichard Henderson     }
85137356079SRichard Henderson     b = 512;
85237356079SRichard Henderson     while (a * (b + 1) * (b + 1) < (1 << 28)) {
85337356079SRichard Henderson         b += 1;
85437356079SRichard Henderson     }
85537356079SRichard Henderson     estimate = (b + 1) / 2;
85637356079SRichard Henderson     assert(256 <= estimate && estimate < 512);
85737356079SRichard Henderson 
85837356079SRichard Henderson     return estimate;
85937356079SRichard Henderson }
86037356079SRichard Henderson 
86137356079SRichard Henderson 
recip_sqrt_estimate(int * exp,int exp_off,uint64_t frac)86237356079SRichard Henderson static uint64_t recip_sqrt_estimate(int *exp , int exp_off, uint64_t frac)
86337356079SRichard Henderson {
86437356079SRichard Henderson     int estimate;
86537356079SRichard Henderson     uint32_t scaled;
86637356079SRichard Henderson 
86737356079SRichard Henderson     if (*exp == 0) {
86837356079SRichard Henderson         while (extract64(frac, 51, 1) == 0) {
86937356079SRichard Henderson             frac = frac << 1;
87037356079SRichard Henderson             *exp -= 1;
87137356079SRichard Henderson         }
87237356079SRichard Henderson         frac = extract64(frac, 0, 51) << 1;
87337356079SRichard Henderson     }
87437356079SRichard Henderson 
87537356079SRichard Henderson     if (*exp & 1) {
87637356079SRichard Henderson         /* scaled = UInt('01':fraction<51:45>) */
87737356079SRichard Henderson         scaled = deposit32(1 << 7, 0, 7, extract64(frac, 45, 7));
87837356079SRichard Henderson     } else {
87937356079SRichard Henderson         /* scaled = UInt('1':fraction<51:44>) */
88037356079SRichard Henderson         scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8));
88137356079SRichard Henderson     }
88237356079SRichard Henderson     estimate = do_recip_sqrt_estimate(scaled);
88337356079SRichard Henderson 
88437356079SRichard Henderson     *exp = (exp_off - *exp) / 2;
88537356079SRichard Henderson     return extract64(estimate, 0, 8) << 44;
88637356079SRichard Henderson }
88737356079SRichard Henderson 
HELPER(rsqrte_f16)88837356079SRichard Henderson uint32_t HELPER(rsqrte_f16)(uint32_t input, void *fpstp)
88937356079SRichard Henderson {
89037356079SRichard Henderson     float_status *s = fpstp;
89137356079SRichard Henderson     float16 f16 = float16_squash_input_denormal(input, s);
89237356079SRichard Henderson     uint16_t val = float16_val(f16);
89337356079SRichard Henderson     bool f16_sign = float16_is_neg(f16);
89437356079SRichard Henderson     int f16_exp = extract32(val, 10, 5);
89537356079SRichard Henderson     uint16_t f16_frac = extract32(val, 0, 10);
89637356079SRichard Henderson     uint64_t f64_frac;
89737356079SRichard Henderson 
89837356079SRichard Henderson     if (float16_is_any_nan(f16)) {
89937356079SRichard Henderson         float16 nan = f16;
90037356079SRichard Henderson         if (float16_is_signaling_nan(f16, s)) {
90137356079SRichard Henderson             float_raise(float_flag_invalid, s);
902103e7579SJoe Komlodi             if (!s->default_nan_mode) {
903103e7579SJoe Komlodi                 nan = float16_silence_nan(f16, fpstp);
904103e7579SJoe Komlodi             }
90537356079SRichard Henderson         }
90637356079SRichard Henderson         if (s->default_nan_mode) {
90737356079SRichard Henderson             nan =  float16_default_nan(s);
90837356079SRichard Henderson         }
90937356079SRichard Henderson         return nan;
91037356079SRichard Henderson     } else if (float16_is_zero(f16)) {
91137356079SRichard Henderson         float_raise(float_flag_divbyzero, s);
91237356079SRichard Henderson         return float16_set_sign(float16_infinity, f16_sign);
91337356079SRichard Henderson     } else if (f16_sign) {
91437356079SRichard Henderson         float_raise(float_flag_invalid, s);
91537356079SRichard Henderson         return float16_default_nan(s);
91637356079SRichard Henderson     } else if (float16_is_infinity(f16)) {
91737356079SRichard Henderson         return float16_zero;
91837356079SRichard Henderson     }
91937356079SRichard Henderson 
92037356079SRichard Henderson     /* Scale and normalize to a double-precision value between 0.25 and 1.0,
92137356079SRichard Henderson      * preserving the parity of the exponent.  */
92237356079SRichard Henderson 
92337356079SRichard Henderson     f64_frac = ((uint64_t) f16_frac) << (52 - 10);
92437356079SRichard Henderson 
92537356079SRichard Henderson     f64_frac = recip_sqrt_estimate(&f16_exp, 44, f64_frac);
92637356079SRichard Henderson 
92737356079SRichard Henderson     /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(2) */
92837356079SRichard Henderson     val = deposit32(0, 15, 1, f16_sign);
92937356079SRichard Henderson     val = deposit32(val, 10, 5, f16_exp);
93037356079SRichard Henderson     val = deposit32(val, 2, 8, extract64(f64_frac, 52 - 8, 8));
93137356079SRichard Henderson     return make_float16(val);
93237356079SRichard Henderson }
93337356079SRichard Henderson 
HELPER(rsqrte_f32)93437356079SRichard Henderson float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
93537356079SRichard Henderson {
93637356079SRichard Henderson     float_status *s = fpstp;
93737356079SRichard Henderson     float32 f32 = float32_squash_input_denormal(input, s);
93837356079SRichard Henderson     uint32_t val = float32_val(f32);
93937356079SRichard Henderson     uint32_t f32_sign = float32_is_neg(f32);
94037356079SRichard Henderson     int f32_exp = extract32(val, 23, 8);
94137356079SRichard Henderson     uint32_t f32_frac = extract32(val, 0, 23);
94237356079SRichard Henderson     uint64_t f64_frac;
94337356079SRichard Henderson 
94437356079SRichard Henderson     if (float32_is_any_nan(f32)) {
94537356079SRichard Henderson         float32 nan = f32;
94637356079SRichard Henderson         if (float32_is_signaling_nan(f32, s)) {
94737356079SRichard Henderson             float_raise(float_flag_invalid, s);
948103e7579SJoe Komlodi             if (!s->default_nan_mode) {
949103e7579SJoe Komlodi                 nan = float32_silence_nan(f32, fpstp);
950103e7579SJoe Komlodi             }
95137356079SRichard Henderson         }
95237356079SRichard Henderson         if (s->default_nan_mode) {
95337356079SRichard Henderson             nan =  float32_default_nan(s);
95437356079SRichard Henderson         }
95537356079SRichard Henderson         return nan;
95637356079SRichard Henderson     } else if (float32_is_zero(f32)) {
95737356079SRichard Henderson         float_raise(float_flag_divbyzero, s);
95837356079SRichard Henderson         return float32_set_sign(float32_infinity, float32_is_neg(f32));
95937356079SRichard Henderson     } else if (float32_is_neg(f32)) {
96037356079SRichard Henderson         float_raise(float_flag_invalid, s);
96137356079SRichard Henderson         return float32_default_nan(s);
96237356079SRichard Henderson     } else if (float32_is_infinity(f32)) {
96337356079SRichard Henderson         return float32_zero;
96437356079SRichard Henderson     }
96537356079SRichard Henderson 
96637356079SRichard Henderson     /* Scale and normalize to a double-precision value between 0.25 and 1.0,
96737356079SRichard Henderson      * preserving the parity of the exponent.  */
96837356079SRichard Henderson 
96937356079SRichard Henderson     f64_frac = ((uint64_t) f32_frac) << 29;
97037356079SRichard Henderson 
97137356079SRichard Henderson     f64_frac = recip_sqrt_estimate(&f32_exp, 380, f64_frac);
97237356079SRichard Henderson 
97337356079SRichard Henderson     /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(15) */
97437356079SRichard Henderson     val = deposit32(0, 31, 1, f32_sign);
97537356079SRichard Henderson     val = deposit32(val, 23, 8, f32_exp);
97637356079SRichard Henderson     val = deposit32(val, 15, 8, extract64(f64_frac, 52 - 8, 8));
97737356079SRichard Henderson     return make_float32(val);
97837356079SRichard Henderson }
97937356079SRichard Henderson 
HELPER(rsqrte_f64)98037356079SRichard Henderson float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
98137356079SRichard Henderson {
98237356079SRichard Henderson     float_status *s = fpstp;
98337356079SRichard Henderson     float64 f64 = float64_squash_input_denormal(input, s);
98437356079SRichard Henderson     uint64_t val = float64_val(f64);
98537356079SRichard Henderson     bool f64_sign = float64_is_neg(f64);
98637356079SRichard Henderson     int f64_exp = extract64(val, 52, 11);
98737356079SRichard Henderson     uint64_t f64_frac = extract64(val, 0, 52);
98837356079SRichard Henderson 
98937356079SRichard Henderson     if (float64_is_any_nan(f64)) {
99037356079SRichard Henderson         float64 nan = f64;
99137356079SRichard Henderson         if (float64_is_signaling_nan(f64, s)) {
99237356079SRichard Henderson             float_raise(float_flag_invalid, s);
993103e7579SJoe Komlodi             if (!s->default_nan_mode) {
994103e7579SJoe Komlodi                 nan = float64_silence_nan(f64, fpstp);
995103e7579SJoe Komlodi             }
99637356079SRichard Henderson         }
99737356079SRichard Henderson         if (s->default_nan_mode) {
99837356079SRichard Henderson             nan =  float64_default_nan(s);
99937356079SRichard Henderson         }
100037356079SRichard Henderson         return nan;
100137356079SRichard Henderson     } else if (float64_is_zero(f64)) {
100237356079SRichard Henderson         float_raise(float_flag_divbyzero, s);
100337356079SRichard Henderson         return float64_set_sign(float64_infinity, float64_is_neg(f64));
100437356079SRichard Henderson     } else if (float64_is_neg(f64)) {
100537356079SRichard Henderson         float_raise(float_flag_invalid, s);
100637356079SRichard Henderson         return float64_default_nan(s);
100737356079SRichard Henderson     } else if (float64_is_infinity(f64)) {
100837356079SRichard Henderson         return float64_zero;
100937356079SRichard Henderson     }
101037356079SRichard Henderson 
101137356079SRichard Henderson     f64_frac = recip_sqrt_estimate(&f64_exp, 3068, f64_frac);
101237356079SRichard Henderson 
101337356079SRichard Henderson     /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(44) */
101437356079SRichard Henderson     val = deposit64(0, 61, 1, f64_sign);
101537356079SRichard Henderson     val = deposit64(val, 52, 11, f64_exp);
101637356079SRichard Henderson     val = deposit64(val, 44, 8, extract64(f64_frac, 52 - 8, 8));
101737356079SRichard Henderson     return make_float64(val);
101837356079SRichard Henderson }
101937356079SRichard Henderson 
HELPER(recpe_u32)1020fe6fb4beSRichard Henderson uint32_t HELPER(recpe_u32)(uint32_t a)
102137356079SRichard Henderson {
102237356079SRichard Henderson     int input, estimate;
102337356079SRichard Henderson 
102437356079SRichard Henderson     if ((a & 0x80000000) == 0) {
102537356079SRichard Henderson         return 0xffffffff;
102637356079SRichard Henderson     }
102737356079SRichard Henderson 
102837356079SRichard Henderson     input = extract32(a, 23, 9);
102937356079SRichard Henderson     estimate = recip_estimate(input);
103037356079SRichard Henderson 
103137356079SRichard Henderson     return deposit32(0, (32 - 9), 9, estimate);
103237356079SRichard Henderson }
103337356079SRichard Henderson 
HELPER(rsqrte_u32)1034fe6fb4beSRichard Henderson uint32_t HELPER(rsqrte_u32)(uint32_t a)
103537356079SRichard Henderson {
103637356079SRichard Henderson     int estimate;
103737356079SRichard Henderson 
103837356079SRichard Henderson     if ((a & 0xc0000000) == 0) {
103937356079SRichard Henderson         return 0xffffffff;
104037356079SRichard Henderson     }
104137356079SRichard Henderson 
104237356079SRichard Henderson     estimate = do_recip_sqrt_estimate(extract32(a, 23, 9));
104337356079SRichard Henderson 
104437356079SRichard Henderson     return deposit32(0, 23, 9, estimate);
104537356079SRichard Henderson }
104637356079SRichard Henderson 
104737356079SRichard Henderson /* VFPv4 fused multiply-accumulate */
VFP_HELPER(muladd,h)10489886fe28SPeter Maydell dh_ctype_f16 VFP_HELPER(muladd, h)(dh_ctype_f16 a, dh_ctype_f16 b,
10499886fe28SPeter Maydell                                    dh_ctype_f16 c, void *fpstp)
10509886fe28SPeter Maydell {
10519886fe28SPeter Maydell     float_status *fpst = fpstp;
10529886fe28SPeter Maydell     return float16_muladd(a, b, c, 0, fpst);
10539886fe28SPeter Maydell }
10549886fe28SPeter Maydell 
VFP_HELPER(muladd,s)105537356079SRichard Henderson float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
105637356079SRichard Henderson {
105737356079SRichard Henderson     float_status *fpst = fpstp;
105837356079SRichard Henderson     return float32_muladd(a, b, c, 0, fpst);
105937356079SRichard Henderson }
106037356079SRichard Henderson 
VFP_HELPER(muladd,d)106137356079SRichard Henderson float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
106237356079SRichard Henderson {
106337356079SRichard Henderson     float_status *fpst = fpstp;
106437356079SRichard Henderson     return float64_muladd(a, b, c, 0, fpst);
106537356079SRichard Henderson }
106637356079SRichard Henderson 
106737356079SRichard Henderson /* ARMv8 round to integral */
HELPER(rinth_exact)10680a6f4b4cSPeter Maydell dh_ctype_f16 HELPER(rinth_exact)(dh_ctype_f16 x, void *fp_status)
10690a6f4b4cSPeter Maydell {
10700a6f4b4cSPeter Maydell     return float16_round_to_int(x, fp_status);
10710a6f4b4cSPeter Maydell }
10720a6f4b4cSPeter Maydell 
HELPER(rints_exact)107337356079SRichard Henderson float32 HELPER(rints_exact)(float32 x, void *fp_status)
107437356079SRichard Henderson {
107537356079SRichard Henderson     return float32_round_to_int(x, fp_status);
107637356079SRichard Henderson }
107737356079SRichard Henderson 
HELPER(rintd_exact)107837356079SRichard Henderson float64 HELPER(rintd_exact)(float64 x, void *fp_status)
107937356079SRichard Henderson {
108037356079SRichard Henderson     return float64_round_to_int(x, fp_status);
108137356079SRichard Henderson }
108237356079SRichard Henderson 
HELPER(rinth)10830a6f4b4cSPeter Maydell dh_ctype_f16 HELPER(rinth)(dh_ctype_f16 x, void *fp_status)
10840a6f4b4cSPeter Maydell {
10850a6f4b4cSPeter Maydell     int old_flags = get_float_exception_flags(fp_status), new_flags;
10860a6f4b4cSPeter Maydell     float16 ret;
10870a6f4b4cSPeter Maydell 
10880a6f4b4cSPeter Maydell     ret = float16_round_to_int(x, fp_status);
10890a6f4b4cSPeter Maydell 
10900a6f4b4cSPeter Maydell     /* Suppress any inexact exceptions the conversion produced */
10910a6f4b4cSPeter Maydell     if (!(old_flags & float_flag_inexact)) {
10920a6f4b4cSPeter Maydell         new_flags = get_float_exception_flags(fp_status);
10930a6f4b4cSPeter Maydell         set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
10940a6f4b4cSPeter Maydell     }
10950a6f4b4cSPeter Maydell 
10960a6f4b4cSPeter Maydell     return ret;
10970a6f4b4cSPeter Maydell }
10980a6f4b4cSPeter Maydell 
HELPER(rints)109937356079SRichard Henderson float32 HELPER(rints)(float32 x, void *fp_status)
110037356079SRichard Henderson {
110137356079SRichard Henderson     int old_flags = get_float_exception_flags(fp_status), new_flags;
110237356079SRichard Henderson     float32 ret;
110337356079SRichard Henderson 
110437356079SRichard Henderson     ret = float32_round_to_int(x, fp_status);
110537356079SRichard Henderson 
110637356079SRichard Henderson     /* Suppress any inexact exceptions the conversion produced */
110737356079SRichard Henderson     if (!(old_flags & float_flag_inexact)) {
110837356079SRichard Henderson         new_flags = get_float_exception_flags(fp_status);
110937356079SRichard Henderson         set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
111037356079SRichard Henderson     }
111137356079SRichard Henderson 
111237356079SRichard Henderson     return ret;
111337356079SRichard Henderson }
111437356079SRichard Henderson 
HELPER(rintd)111537356079SRichard Henderson float64 HELPER(rintd)(float64 x, void *fp_status)
111637356079SRichard Henderson {
111737356079SRichard Henderson     int old_flags = get_float_exception_flags(fp_status), new_flags;
111837356079SRichard Henderson     float64 ret;
111937356079SRichard Henderson 
112037356079SRichard Henderson     ret = float64_round_to_int(x, fp_status);
112137356079SRichard Henderson 
112237356079SRichard Henderson     new_flags = get_float_exception_flags(fp_status);
112337356079SRichard Henderson 
112437356079SRichard Henderson     /* Suppress any inexact exceptions the conversion produced */
112537356079SRichard Henderson     if (!(old_flags & float_flag_inexact)) {
112637356079SRichard Henderson         new_flags = get_float_exception_flags(fp_status);
112737356079SRichard Henderson         set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
112837356079SRichard Henderson     }
112937356079SRichard Henderson 
113037356079SRichard Henderson     return ret;
113137356079SRichard Henderson }
113237356079SRichard Henderson 
113337356079SRichard Henderson /* Convert ARM rounding mode to softfloat */
11346ce21abdSRichard Henderson const FloatRoundMode arm_rmode_to_sf_map[] = {
11356ce21abdSRichard Henderson     [FPROUNDING_TIEEVEN] = float_round_nearest_even,
11366ce21abdSRichard Henderson     [FPROUNDING_POSINF] = float_round_up,
11376ce21abdSRichard Henderson     [FPROUNDING_NEGINF] = float_round_down,
11386ce21abdSRichard Henderson     [FPROUNDING_ZERO] = float_round_to_zero,
11396ce21abdSRichard Henderson     [FPROUNDING_TIEAWAY] = float_round_ties_away,
11406ce21abdSRichard Henderson     [FPROUNDING_ODD] = float_round_to_odd,
11416ce21abdSRichard Henderson };
11426c1f6f27SRichard Henderson 
11436c1f6f27SRichard Henderson /*
11446c1f6f27SRichard Henderson  * Implement float64 to int32_t conversion without saturation;
11456c1f6f27SRichard Henderson  * the result is supplied modulo 2^32.
11466c1f6f27SRichard Henderson  */
HELPER(fjcvtzs)11476c1f6f27SRichard Henderson uint64_t HELPER(fjcvtzs)(float64 value, void *vstatus)
11486c1f6f27SRichard Henderson {
11496c1f6f27SRichard Henderson     float_status *status = vstatus;
11507619129fSRichard Henderson     uint32_t frac, e_old, e_new;
11517619129fSRichard Henderson     bool inexact;
11526c1f6f27SRichard Henderson 
115334d03ad9SRichard Henderson     e_old = get_float_exception_flags(status);
115434d03ad9SRichard Henderson     set_float_exception_flags(0, status);
115534d03ad9SRichard Henderson     frac = float64_to_int32_modulo(value, float_round_to_zero, status);
115634d03ad9SRichard Henderson     e_new = get_float_exception_flags(status);
115734d03ad9SRichard Henderson     set_float_exception_flags(e_old | e_new, status);
11586c1f6f27SRichard Henderson 
11597619129fSRichard Henderson     /* Normal inexact, denormal with flush-to-zero, or overflow or NaN */
11607619129fSRichard Henderson     inexact = e_new & (float_flag_inexact |
11617619129fSRichard Henderson                        float_flag_input_denormal |
11627619129fSRichard Henderson                        float_flag_invalid);
11637619129fSRichard Henderson 
11646c1f6f27SRichard Henderson     /* While not inexact for IEEE FP, -0.0 is inexact for JavaScript. */
11657619129fSRichard Henderson     inexact |= value == float64_chs(float64_zero);
11666c1f6f27SRichard Henderson 
11676c1f6f27SRichard Henderson     /* Pack the result and the env->ZF representation of Z together.  */
11686c1f6f27SRichard Henderson     return deposit64(frac, 32, 32, inexact);
11696c1f6f27SRichard Henderson }
11706c1f6f27SRichard Henderson 
HELPER(vjcvt)11716c1f6f27SRichard Henderson uint32_t HELPER(vjcvt)(float64 value, CPUARMState *env)
11726c1f6f27SRichard Henderson {
11736c1f6f27SRichard Henderson     uint64_t pair = HELPER(fjcvtzs)(value, &env->vfp.fp_status);
11746c1f6f27SRichard Henderson     uint32_t result = pair;
11756c1f6f27SRichard Henderson     uint32_t z = (pair >> 32) == 0;
11766c1f6f27SRichard Henderson 
11776c1f6f27SRichard Henderson     /* Store Z, clear NCV, in FPSCR.NZCV.  */
1178a26db547SPeter Maydell     env->vfp.fpsr = (env->vfp.fpsr & ~FPSR_NZCV_MASK) | (z * FPSR_Z);
11796c1f6f27SRichard Henderson 
11806c1f6f27SRichard Henderson     return result;
11816c1f6f27SRichard Henderson }
11826bea2563SRichard Henderson 
11836bea2563SRichard Henderson /* Round a float32 to an integer that fits in int32_t or int64_t.  */
frint_s(float32 f,float_status * fpst,int intsize)11846bea2563SRichard Henderson static float32 frint_s(float32 f, float_status *fpst, int intsize)
11856bea2563SRichard Henderson {
11866bea2563SRichard Henderson     int old_flags = get_float_exception_flags(fpst);
11876bea2563SRichard Henderson     uint32_t exp = extract32(f, 23, 8);
11886bea2563SRichard Henderson 
11896bea2563SRichard Henderson     if (unlikely(exp == 0xff)) {
11906bea2563SRichard Henderson         /* NaN or Inf.  */
11916bea2563SRichard Henderson         goto overflow;
11926bea2563SRichard Henderson     }
11936bea2563SRichard Henderson 
11946bea2563SRichard Henderson     /* Round and re-extract the exponent.  */
11956bea2563SRichard Henderson     f = float32_round_to_int(f, fpst);
11966bea2563SRichard Henderson     exp = extract32(f, 23, 8);
11976bea2563SRichard Henderson 
11986bea2563SRichard Henderson     /* Validate the range of the result.  */
11996bea2563SRichard Henderson     if (exp < 126 + intsize) {
12006bea2563SRichard Henderson         /* abs(F) <= INT{N}_MAX */
12016bea2563SRichard Henderson         return f;
12026bea2563SRichard Henderson     }
12036bea2563SRichard Henderson     if (exp == 126 + intsize) {
12046bea2563SRichard Henderson         uint32_t sign = extract32(f, 31, 1);
12056bea2563SRichard Henderson         uint32_t frac = extract32(f, 0, 23);
12066bea2563SRichard Henderson         if (sign && frac == 0) {
12076bea2563SRichard Henderson             /* F == INT{N}_MIN */
12086bea2563SRichard Henderson             return f;
12096bea2563SRichard Henderson         }
12106bea2563SRichard Henderson     }
12116bea2563SRichard Henderson 
12126bea2563SRichard Henderson  overflow:
12136bea2563SRichard Henderson     /*
12146bea2563SRichard Henderson      * Raise Invalid and return INT{N}_MIN as a float.  Revert any
12156bea2563SRichard Henderson      * inexact exception float32_round_to_int may have raised.
12166bea2563SRichard Henderson      */
12176bea2563SRichard Henderson     set_float_exception_flags(old_flags | float_flag_invalid, fpst);
12186bea2563SRichard Henderson     return (0x100u + 126u + intsize) << 23;
12196bea2563SRichard Henderson }
12206bea2563SRichard Henderson 
HELPER(frint32_s)12216bea2563SRichard Henderson float32 HELPER(frint32_s)(float32 f, void *fpst)
12226bea2563SRichard Henderson {
12236bea2563SRichard Henderson     return frint_s(f, fpst, 32);
12246bea2563SRichard Henderson }
12256bea2563SRichard Henderson 
HELPER(frint64_s)12266bea2563SRichard Henderson float32 HELPER(frint64_s)(float32 f, void *fpst)
12276bea2563SRichard Henderson {
12286bea2563SRichard Henderson     return frint_s(f, fpst, 64);
12296bea2563SRichard Henderson }
12306bea2563SRichard Henderson 
12316bea2563SRichard Henderson /* Round a float64 to an integer that fits in int32_t or int64_t.  */
frint_d(float64 f,float_status * fpst,int intsize)12326bea2563SRichard Henderson static float64 frint_d(float64 f, float_status *fpst, int intsize)
12336bea2563SRichard Henderson {
12346bea2563SRichard Henderson     int old_flags = get_float_exception_flags(fpst);
12356bea2563SRichard Henderson     uint32_t exp = extract64(f, 52, 11);
12366bea2563SRichard Henderson 
12376bea2563SRichard Henderson     if (unlikely(exp == 0x7ff)) {
12386bea2563SRichard Henderson         /* NaN or Inf.  */
12396bea2563SRichard Henderson         goto overflow;
12406bea2563SRichard Henderson     }
12416bea2563SRichard Henderson 
12426bea2563SRichard Henderson     /* Round and re-extract the exponent.  */
12436bea2563SRichard Henderson     f = float64_round_to_int(f, fpst);
12446bea2563SRichard Henderson     exp = extract64(f, 52, 11);
12456bea2563SRichard Henderson 
12466bea2563SRichard Henderson     /* Validate the range of the result.  */
12476bea2563SRichard Henderson     if (exp < 1022 + intsize) {
12486bea2563SRichard Henderson         /* abs(F) <= INT{N}_MAX */
12496bea2563SRichard Henderson         return f;
12506bea2563SRichard Henderson     }
12516bea2563SRichard Henderson     if (exp == 1022 + intsize) {
12526bea2563SRichard Henderson         uint64_t sign = extract64(f, 63, 1);
12536bea2563SRichard Henderson         uint64_t frac = extract64(f, 0, 52);
12546bea2563SRichard Henderson         if (sign && frac == 0) {
12556bea2563SRichard Henderson             /* F == INT{N}_MIN */
12566bea2563SRichard Henderson             return f;
12576bea2563SRichard Henderson         }
12586bea2563SRichard Henderson     }
12596bea2563SRichard Henderson 
12606bea2563SRichard Henderson  overflow:
12616bea2563SRichard Henderson     /*
12626bea2563SRichard Henderson      * Raise Invalid and return INT{N}_MIN as a float.  Revert any
12636bea2563SRichard Henderson      * inexact exception float64_round_to_int may have raised.
12646bea2563SRichard Henderson      */
12656bea2563SRichard Henderson     set_float_exception_flags(old_flags | float_flag_invalid, fpst);
12666bea2563SRichard Henderson     return (uint64_t)(0x800 + 1022 + intsize) << 52;
12676bea2563SRichard Henderson }
12686bea2563SRichard Henderson 
HELPER(frint32_d)12696bea2563SRichard Henderson float64 HELPER(frint32_d)(float64 f, void *fpst)
12706bea2563SRichard Henderson {
12716bea2563SRichard Henderson     return frint_d(f, fpst, 32);
12726bea2563SRichard Henderson }
12736bea2563SRichard Henderson 
HELPER(frint64_d)12746bea2563SRichard Henderson float64 HELPER(frint64_d)(float64 f, void *fpst)
12756bea2563SRichard Henderson {
12766bea2563SRichard Henderson     return frint_d(f, fpst, 64);
12776bea2563SRichard Henderson }
12784a15527cSPhilippe Mathieu-Daudé 
HELPER(check_hcr_el2_trap)12799ca1d776SMarc Zyngier void HELPER(check_hcr_el2_trap)(CPUARMState *env, uint32_t rt, uint32_t reg)
12809ca1d776SMarc Zyngier {
12819ca1d776SMarc Zyngier     uint32_t syndrome;
12829ca1d776SMarc Zyngier 
12839ca1d776SMarc Zyngier     switch (reg) {
12849ca1d776SMarc Zyngier     case ARM_VFP_MVFR0:
12859ca1d776SMarc Zyngier     case ARM_VFP_MVFR1:
12869ca1d776SMarc Zyngier     case ARM_VFP_MVFR2:
12879ca1d776SMarc Zyngier         if (!(arm_hcr_el2_eff(env) & HCR_TID3)) {
12889ca1d776SMarc Zyngier             return;
12899ca1d776SMarc Zyngier         }
12909ca1d776SMarc Zyngier         break;
12919ca1d776SMarc Zyngier     case ARM_VFP_FPSID:
12929ca1d776SMarc Zyngier         if (!(arm_hcr_el2_eff(env) & HCR_TID0)) {
12939ca1d776SMarc Zyngier             return;
12949ca1d776SMarc Zyngier         }
12959ca1d776SMarc Zyngier         break;
12969ca1d776SMarc Zyngier     default:
12979ca1d776SMarc Zyngier         g_assert_not_reached();
12989ca1d776SMarc Zyngier     }
12999ca1d776SMarc Zyngier 
13009ca1d776SMarc Zyngier     syndrome = ((EC_FPIDTRAP << ARM_EL_EC_SHIFT)
13019ca1d776SMarc Zyngier                 | ARM_EL_IL
13029ca1d776SMarc Zyngier                 | (1 << 24) | (0xe << 20) | (7 << 14)
13039ca1d776SMarc Zyngier                 | (reg << 10) | (rt << 5) | 1);
13049ca1d776SMarc Zyngier 
13059ca1d776SMarc Zyngier     raise_exception(env, EXCP_HYP_TRAP, syndrome, 2);
13069ca1d776SMarc Zyngier }
13079ca1d776SMarc Zyngier 
13084a15527cSPhilippe Mathieu-Daudé #endif
1309