target/arm: Make FPSCR.LTPSIZE writable for MVE
[qemu/ar7.git] / target / arm / vfp_helper.c
blobe0886ab5a56123be7b5be4cb1e6992bc0caa7c35
1 /*
2 * ARM VFP floating-point operations
4 * Copyright (c) 2003 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "cpu.h"
22 #include "exec/helper-proto.h"
23 #include "internals.h"
24 #ifdef CONFIG_TCG
25 #include "qemu/log.h"
26 #include "fpu/softfloat.h"
27 #endif
29 /* VFP support. We follow the convention used for VFP instructions:
30 Single precision routines have a "s" suffix, double precision a
31 "d" suffix. */
33 #ifdef CONFIG_TCG
35 /* Convert host exception flags to vfp form. */
36 static inline int vfp_exceptbits_from_host(int host_bits)
38 int target_bits = 0;
40 if (host_bits & float_flag_invalid) {
41 target_bits |= 1;
43 if (host_bits & float_flag_divbyzero) {
44 target_bits |= 2;
46 if (host_bits & float_flag_overflow) {
47 target_bits |= 4;
49 if (host_bits & (float_flag_underflow | float_flag_output_denormal)) {
50 target_bits |= 8;
52 if (host_bits & float_flag_inexact) {
53 target_bits |= 0x10;
55 if (host_bits & float_flag_input_denormal) {
56 target_bits |= 0x80;
58 return target_bits;
61 /* Convert vfp exception flags to target form. */
62 static inline int vfp_exceptbits_to_host(int target_bits)
64 int host_bits = 0;
66 if (target_bits & 1) {
67 host_bits |= float_flag_invalid;
69 if (target_bits & 2) {
70 host_bits |= float_flag_divbyzero;
72 if (target_bits & 4) {
73 host_bits |= float_flag_overflow;
75 if (target_bits & 8) {
76 host_bits |= float_flag_underflow;
78 if (target_bits & 0x10) {
79 host_bits |= float_flag_inexact;
81 if (target_bits & 0x80) {
82 host_bits |= float_flag_input_denormal;
84 return host_bits;
87 static uint32_t vfp_get_fpscr_from_host(CPUARMState *env)
89 uint32_t i;
91 i = get_float_exception_flags(&env->vfp.fp_status);
92 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
93 /* FZ16 does not generate an input denormal exception. */
94 i |= (get_float_exception_flags(&env->vfp.fp_status_f16)
95 & ~float_flag_input_denormal);
96 i |= (get_float_exception_flags(&env->vfp.standard_fp_status_f16)
97 & ~float_flag_input_denormal);
98 return vfp_exceptbits_from_host(i);
101 static void vfp_set_fpscr_to_host(CPUARMState *env, uint32_t val)
103 int i;
104 uint32_t changed = env->vfp.xregs[ARM_VFP_FPSCR];
106 changed ^= val;
107 if (changed & (3 << 22)) {
108 i = (val >> 22) & 3;
109 switch (i) {
110 case FPROUNDING_TIEEVEN:
111 i = float_round_nearest_even;
112 break;
113 case FPROUNDING_POSINF:
114 i = float_round_up;
115 break;
116 case FPROUNDING_NEGINF:
117 i = float_round_down;
118 break;
119 case FPROUNDING_ZERO:
120 i = float_round_to_zero;
121 break;
123 set_float_rounding_mode(i, &env->vfp.fp_status);
124 set_float_rounding_mode(i, &env->vfp.fp_status_f16);
126 if (changed & FPCR_FZ16) {
127 bool ftz_enabled = val & FPCR_FZ16;
128 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
129 set_flush_to_zero(ftz_enabled, &env->vfp.standard_fp_status_f16);
130 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
131 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.standard_fp_status_f16);
133 if (changed & FPCR_FZ) {
134 bool ftz_enabled = val & FPCR_FZ;
135 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status);
136 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status);
138 if (changed & FPCR_DN) {
139 bool dnan_enabled = val & FPCR_DN;
140 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status);
141 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status_f16);
145 * The exception flags are ORed together when we read fpscr so we
146 * only need to preserve the current state in one of our
147 * float_status values.
149 i = vfp_exceptbits_to_host(val);
150 set_float_exception_flags(i, &env->vfp.fp_status);
151 set_float_exception_flags(0, &env->vfp.fp_status_f16);
152 set_float_exception_flags(0, &env->vfp.standard_fp_status);
153 set_float_exception_flags(0, &env->vfp.standard_fp_status_f16);
156 #else
158 static uint32_t vfp_get_fpscr_from_host(CPUARMState *env)
160 return 0;
163 static void vfp_set_fpscr_to_host(CPUARMState *env, uint32_t val)
167 #endif
169 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
171 uint32_t i, fpscr;
173 fpscr = env->vfp.xregs[ARM_VFP_FPSCR]
174 | (env->vfp.vec_len << 16)
175 | (env->vfp.vec_stride << 20);
178 * M-profile LTPSIZE overlaps A-profile Stride; whichever of the
179 * two is not applicable to this CPU will always be zero.
181 fpscr |= env->v7m.ltpsize << 16;
183 fpscr |= vfp_get_fpscr_from_host(env);
185 i = env->vfp.qc[0] | env->vfp.qc[1] | env->vfp.qc[2] | env->vfp.qc[3];
186 fpscr |= i ? FPCR_QC : 0;
188 return fpscr;
191 uint32_t vfp_get_fpscr(CPUARMState *env)
193 return HELPER(vfp_get_fpscr)(env);
196 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
198 ARMCPU *cpu = env_archcpu(env);
200 /* When ARMv8.2-FP16 is not supported, FZ16 is RES0. */
201 if (!cpu_isar_feature(any_fp16, cpu)) {
202 val &= ~FPCR_FZ16;
205 vfp_set_fpscr_to_host(env, val);
207 if (!arm_feature(env, ARM_FEATURE_M)) {
209 * Short-vector length and stride; on M-profile these bits
210 * are used for different purposes.
211 * We can't make this conditional be "if MVFR0.FPShVec != 0",
212 * because in v7A no-short-vector-support cores still had to
213 * allow Stride/Len to be written with the only effect that
214 * some insns are required to UNDEF if the guest sets them.
216 env->vfp.vec_len = extract32(val, 16, 3);
217 env->vfp.vec_stride = extract32(val, 20, 2);
218 } else if (cpu_isar_feature(aa32_mve, cpu)) {
219 env->v7m.ltpsize = extract32(val, FPCR_LTPSIZE_SHIFT,
220 FPCR_LTPSIZE_LENGTH);
223 if (arm_feature(env, ARM_FEATURE_NEON)) {
225 * The bit we set within fpscr_q is arbitrary; the register as a
226 * whole being zero/non-zero is what counts.
227 * TODO: M-profile MVE also has a QC bit.
229 env->vfp.qc[0] = val & FPCR_QC;
230 env->vfp.qc[1] = 0;
231 env->vfp.qc[2] = 0;
232 env->vfp.qc[3] = 0;
236 * We don't implement trapped exception handling, so the
237 * trap enable bits, IDE|IXE|UFE|OFE|DZE|IOE are all RAZ/WI (not RES0!)
239 * The exception flags IOC|DZC|OFC|UFC|IXC|IDC are stored in
240 * fp_status; QC, Len and Stride are stored separately earlier.
241 * Clear out all of those and the RES0 bits: only NZCV, AHP, DN,
242 * FZ, RMode and FZ16 are kept in vfp.xregs[FPSCR].
244 env->vfp.xregs[ARM_VFP_FPSCR] = val & 0xf7c80000;
247 void vfp_set_fpscr(CPUARMState *env, uint32_t val)
249 HELPER(vfp_set_fpscr)(env, val);
252 #ifdef CONFIG_TCG
254 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
256 #define VFP_BINOP(name) \
257 dh_ctype_f16 VFP_HELPER(name, h)(dh_ctype_f16 a, dh_ctype_f16 b, void *fpstp) \
259 float_status *fpst = fpstp; \
260 return float16_ ## name(a, b, fpst); \
262 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
264 float_status *fpst = fpstp; \
265 return float32_ ## name(a, b, fpst); \
267 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
269 float_status *fpst = fpstp; \
270 return float64_ ## name(a, b, fpst); \
272 VFP_BINOP(add)
273 VFP_BINOP(sub)
274 VFP_BINOP(mul)
275 VFP_BINOP(div)
276 VFP_BINOP(min)
277 VFP_BINOP(max)
278 VFP_BINOP(minnum)
279 VFP_BINOP(maxnum)
280 #undef VFP_BINOP
282 dh_ctype_f16 VFP_HELPER(neg, h)(dh_ctype_f16 a)
284 return float16_chs(a);
287 float32 VFP_HELPER(neg, s)(float32 a)
289 return float32_chs(a);
292 float64 VFP_HELPER(neg, d)(float64 a)
294 return float64_chs(a);
297 dh_ctype_f16 VFP_HELPER(abs, h)(dh_ctype_f16 a)
299 return float16_abs(a);
302 float32 VFP_HELPER(abs, s)(float32 a)
304 return float32_abs(a);
307 float64 VFP_HELPER(abs, d)(float64 a)
309 return float64_abs(a);
312 dh_ctype_f16 VFP_HELPER(sqrt, h)(dh_ctype_f16 a, CPUARMState *env)
314 return float16_sqrt(a, &env->vfp.fp_status_f16);
317 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
319 return float32_sqrt(a, &env->vfp.fp_status);
322 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
324 return float64_sqrt(a, &env->vfp.fp_status);
327 static void softfloat_to_vfp_compare(CPUARMState *env, FloatRelation cmp)
329 uint32_t flags;
330 switch (cmp) {
331 case float_relation_equal:
332 flags = 0x6;
333 break;
334 case float_relation_less:
335 flags = 0x8;
336 break;
337 case float_relation_greater:
338 flags = 0x2;
339 break;
340 case float_relation_unordered:
341 flags = 0x3;
342 break;
343 default:
344 g_assert_not_reached();
346 env->vfp.xregs[ARM_VFP_FPSCR] =
347 deposit32(env->vfp.xregs[ARM_VFP_FPSCR], 28, 4, flags);
350 /* XXX: check quiet/signaling case */
351 #define DO_VFP_cmp(P, FLOATTYPE, ARGTYPE, FPST) \
352 void VFP_HELPER(cmp, P)(ARGTYPE a, ARGTYPE b, CPUARMState *env) \
354 softfloat_to_vfp_compare(env, \
355 FLOATTYPE ## _compare_quiet(a, b, &env->vfp.FPST)); \
357 void VFP_HELPER(cmpe, P)(ARGTYPE a, ARGTYPE b, CPUARMState *env) \
359 softfloat_to_vfp_compare(env, \
360 FLOATTYPE ## _compare(a, b, &env->vfp.FPST)); \
362 DO_VFP_cmp(h, float16, dh_ctype_f16, fp_status_f16)
363 DO_VFP_cmp(s, float32, float32, fp_status)
364 DO_VFP_cmp(d, float64, float64, fp_status)
365 #undef DO_VFP_cmp
367 /* Integer to float and float to integer conversions */
369 #define CONV_ITOF(name, ftype, fsz, sign) \
370 ftype HELPER(name)(uint32_t x, void *fpstp) \
372 float_status *fpst = fpstp; \
373 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
376 #define CONV_FTOI(name, ftype, fsz, sign, round) \
377 sign##int32_t HELPER(name)(ftype x, void *fpstp) \
379 float_status *fpst = fpstp; \
380 if (float##fsz##_is_any_nan(x)) { \
381 float_raise(float_flag_invalid, fpst); \
382 return 0; \
384 return float##fsz##_to_##sign##int32##round(x, fpst); \
387 #define FLOAT_CONVS(name, p, ftype, fsz, sign) \
388 CONV_ITOF(vfp_##name##to##p, ftype, fsz, sign) \
389 CONV_FTOI(vfp_to##name##p, ftype, fsz, sign, ) \
390 CONV_FTOI(vfp_to##name##z##p, ftype, fsz, sign, _round_to_zero)
392 FLOAT_CONVS(si, h, uint32_t, 16, )
393 FLOAT_CONVS(si, s, float32, 32, )
394 FLOAT_CONVS(si, d, float64, 64, )
395 FLOAT_CONVS(ui, h, uint32_t, 16, u)
396 FLOAT_CONVS(ui, s, float32, 32, u)
397 FLOAT_CONVS(ui, d, float64, 64, u)
399 #undef CONV_ITOF
400 #undef CONV_FTOI
401 #undef FLOAT_CONVS
403 /* floating point conversion */
404 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
406 return float32_to_float64(x, &env->vfp.fp_status);
409 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
411 return float64_to_float32(x, &env->vfp.fp_status);
415 * VFP3 fixed point conversion. The AArch32 versions of fix-to-float
416 * must always round-to-nearest; the AArch64 ones honour the FPSCR
417 * rounding mode. (For AArch32 Neon the standard-FPSCR is set to
418 * round-to-nearest so either helper will work.) AArch32 float-to-fix
419 * must round-to-zero.
421 #define VFP_CONV_FIX_FLOAT(name, p, fsz, ftype, isz, itype) \
422 ftype HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
423 void *fpstp) \
424 { return itype##_to_##float##fsz##_scalbn(x, -shift, fpstp); }
426 #define VFP_CONV_FIX_FLOAT_ROUND(name, p, fsz, ftype, isz, itype) \
427 ftype HELPER(vfp_##name##to##p##_round_to_nearest)(uint##isz##_t x, \
428 uint32_t shift, \
429 void *fpstp) \
431 ftype ret; \
432 float_status *fpst = fpstp; \
433 FloatRoundMode oldmode = fpst->float_rounding_mode; \
434 fpst->float_rounding_mode = float_round_nearest_even; \
435 ret = itype##_to_##float##fsz##_scalbn(x, -shift, fpstp); \
436 fpst->float_rounding_mode = oldmode; \
437 return ret; \
440 #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, ftype, isz, itype, ROUND, suff) \
441 uint##isz##_t HELPER(vfp_to##name##p##suff)(ftype x, uint32_t shift, \
442 void *fpst) \
444 if (unlikely(float##fsz##_is_any_nan(x))) { \
445 float_raise(float_flag_invalid, fpst); \
446 return 0; \
448 return float##fsz##_to_##itype##_scalbn(x, ROUND, shift, fpst); \
451 #define VFP_CONV_FIX(name, p, fsz, ftype, isz, itype) \
452 VFP_CONV_FIX_FLOAT(name, p, fsz, ftype, isz, itype) \
453 VFP_CONV_FIX_FLOAT_ROUND(name, p, fsz, ftype, isz, itype) \
454 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, ftype, isz, itype, \
455 float_round_to_zero, _round_to_zero) \
456 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, ftype, isz, itype, \
457 get_float_rounding_mode(fpst), )
459 #define VFP_CONV_FIX_A64(name, p, fsz, ftype, isz, itype) \
460 VFP_CONV_FIX_FLOAT(name, p, fsz, ftype, isz, itype) \
461 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, ftype, isz, itype, \
462 get_float_rounding_mode(fpst), )
464 VFP_CONV_FIX(sh, d, 64, float64, 64, int16)
465 VFP_CONV_FIX(sl, d, 64, float64, 64, int32)
466 VFP_CONV_FIX_A64(sq, d, 64, float64, 64, int64)
467 VFP_CONV_FIX(uh, d, 64, float64, 64, uint16)
468 VFP_CONV_FIX(ul, d, 64, float64, 64, uint32)
469 VFP_CONV_FIX_A64(uq, d, 64, float64, 64, uint64)
470 VFP_CONV_FIX(sh, s, 32, float32, 32, int16)
471 VFP_CONV_FIX(sl, s, 32, float32, 32, int32)
472 VFP_CONV_FIX_A64(sq, s, 32, float32, 64, int64)
473 VFP_CONV_FIX(uh, s, 32, float32, 32, uint16)
474 VFP_CONV_FIX(ul, s, 32, float32, 32, uint32)
475 VFP_CONV_FIX_A64(uq, s, 32, float32, 64, uint64)
476 VFP_CONV_FIX(sh, h, 16, dh_ctype_f16, 32, int16)
477 VFP_CONV_FIX(sl, h, 16, dh_ctype_f16, 32, int32)
478 VFP_CONV_FIX_A64(sq, h, 16, dh_ctype_f16, 64, int64)
479 VFP_CONV_FIX(uh, h, 16, dh_ctype_f16, 32, uint16)
480 VFP_CONV_FIX(ul, h, 16, dh_ctype_f16, 32, uint32)
481 VFP_CONV_FIX_A64(uq, h, 16, dh_ctype_f16, 64, uint64)
483 #undef VFP_CONV_FIX
484 #undef VFP_CONV_FIX_FLOAT
485 #undef VFP_CONV_FLOAT_FIX_ROUND
486 #undef VFP_CONV_FIX_A64
488 /* Set the current fp rounding mode and return the old one.
489 * The argument is a softfloat float_round_ value.
491 uint32_t HELPER(set_rmode)(uint32_t rmode, void *fpstp)
493 float_status *fp_status = fpstp;
495 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
496 set_float_rounding_mode(rmode, fp_status);
498 return prev_rmode;
501 /* Half precision conversions. */
502 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, void *fpstp, uint32_t ahp_mode)
504 /* Squash FZ16 to 0 for the duration of conversion. In this case,
505 * it would affect flushing input denormals.
507 float_status *fpst = fpstp;
508 bool save = get_flush_inputs_to_zero(fpst);
509 set_flush_inputs_to_zero(false, fpst);
510 float32 r = float16_to_float32(a, !ahp_mode, fpst);
511 set_flush_inputs_to_zero(save, fpst);
512 return r;
515 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, void *fpstp, uint32_t ahp_mode)
517 /* Squash FZ16 to 0 for the duration of conversion. In this case,
518 * it would affect flushing output denormals.
520 float_status *fpst = fpstp;
521 bool save = get_flush_to_zero(fpst);
522 set_flush_to_zero(false, fpst);
523 float16 r = float32_to_float16(a, !ahp_mode, fpst);
524 set_flush_to_zero(save, fpst);
525 return r;
528 float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, void *fpstp, uint32_t ahp_mode)
530 /* Squash FZ16 to 0 for the duration of conversion. In this case,
531 * it would affect flushing input denormals.
533 float_status *fpst = fpstp;
534 bool save = get_flush_inputs_to_zero(fpst);
535 set_flush_inputs_to_zero(false, fpst);
536 float64 r = float16_to_float64(a, !ahp_mode, fpst);
537 set_flush_inputs_to_zero(save, fpst);
538 return r;
541 uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, void *fpstp, uint32_t ahp_mode)
543 /* Squash FZ16 to 0 for the duration of conversion. In this case,
544 * it would affect flushing output denormals.
546 float_status *fpst = fpstp;
547 bool save = get_flush_to_zero(fpst);
548 set_flush_to_zero(false, fpst);
549 float16 r = float64_to_float16(a, !ahp_mode, fpst);
550 set_flush_to_zero(save, fpst);
551 return r;
554 /* NEON helpers. */
556 /* Constants 256 and 512 are used in some helpers; we avoid relying on
557 * int->float conversions at run-time. */
558 #define float64_256 make_float64(0x4070000000000000LL)
559 #define float64_512 make_float64(0x4080000000000000LL)
560 #define float16_maxnorm make_float16(0x7bff)
561 #define float32_maxnorm make_float32(0x7f7fffff)
562 #define float64_maxnorm make_float64(0x7fefffffffffffffLL)
564 /* Reciprocal functions
566 * The algorithm that must be used to calculate the estimate
567 * is specified by the ARM ARM, see FPRecipEstimate()/RecipEstimate
570 /* See RecipEstimate()
572 * input is a 9 bit fixed point number
573 * input range 256 .. 511 for a number from 0.5 <= x < 1.0.
574 * result range 256 .. 511 for a number from 1.0 to 511/256.
577 static int recip_estimate(int input)
579 int a, b, r;
580 assert(256 <= input && input < 512);
581 a = (input * 2) + 1;
582 b = (1 << 19) / a;
583 r = (b + 1) >> 1;
584 assert(256 <= r && r < 512);
585 return r;
589 * Common wrapper to call recip_estimate
591 * The parameters are exponent and 64 bit fraction (without implicit
592 * bit) where the binary point is nominally at bit 52. Returns a
593 * float64 which can then be rounded to the appropriate size by the
594 * callee.
597 static uint64_t call_recip_estimate(int *exp, int exp_off, uint64_t frac)
599 uint32_t scaled, estimate;
600 uint64_t result_frac;
601 int result_exp;
603 /* Handle sub-normals */
604 if (*exp == 0) {
605 if (extract64(frac, 51, 1) == 0) {
606 *exp = -1;
607 frac <<= 2;
608 } else {
609 frac <<= 1;
613 /* scaled = UInt('1':fraction<51:44>) */
614 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8));
615 estimate = recip_estimate(scaled);
617 result_exp = exp_off - *exp;
618 result_frac = deposit64(0, 44, 8, estimate);
619 if (result_exp == 0) {
620 result_frac = deposit64(result_frac >> 1, 51, 1, 1);
621 } else if (result_exp == -1) {
622 result_frac = deposit64(result_frac >> 2, 50, 2, 1);
623 result_exp = 0;
626 *exp = result_exp;
628 return result_frac;
631 static bool round_to_inf(float_status *fpst, bool sign_bit)
633 switch (fpst->float_rounding_mode) {
634 case float_round_nearest_even: /* Round to Nearest */
635 return true;
636 case float_round_up: /* Round to +Inf */
637 return !sign_bit;
638 case float_round_down: /* Round to -Inf */
639 return sign_bit;
640 case float_round_to_zero: /* Round to Zero */
641 return false;
642 default:
643 g_assert_not_reached();
647 uint32_t HELPER(recpe_f16)(uint32_t input, void *fpstp)
649 float_status *fpst = fpstp;
650 float16 f16 = float16_squash_input_denormal(input, fpst);
651 uint32_t f16_val = float16_val(f16);
652 uint32_t f16_sign = float16_is_neg(f16);
653 int f16_exp = extract32(f16_val, 10, 5);
654 uint32_t f16_frac = extract32(f16_val, 0, 10);
655 uint64_t f64_frac;
657 if (float16_is_any_nan(f16)) {
658 float16 nan = f16;
659 if (float16_is_signaling_nan(f16, fpst)) {
660 float_raise(float_flag_invalid, fpst);
661 nan = float16_silence_nan(f16, fpst);
663 if (fpst->default_nan_mode) {
664 nan = float16_default_nan(fpst);
666 return nan;
667 } else if (float16_is_infinity(f16)) {
668 return float16_set_sign(float16_zero, float16_is_neg(f16));
669 } else if (float16_is_zero(f16)) {
670 float_raise(float_flag_divbyzero, fpst);
671 return float16_set_sign(float16_infinity, float16_is_neg(f16));
672 } else if (float16_abs(f16) < (1 << 8)) {
673 /* Abs(value) < 2.0^-16 */
674 float_raise(float_flag_overflow | float_flag_inexact, fpst);
675 if (round_to_inf(fpst, f16_sign)) {
676 return float16_set_sign(float16_infinity, f16_sign);
677 } else {
678 return float16_set_sign(float16_maxnorm, f16_sign);
680 } else if (f16_exp >= 29 && fpst->flush_to_zero) {
681 float_raise(float_flag_underflow, fpst);
682 return float16_set_sign(float16_zero, float16_is_neg(f16));
685 f64_frac = call_recip_estimate(&f16_exp, 29,
686 ((uint64_t) f16_frac) << (52 - 10));
688 /* result = sign : result_exp<4:0> : fraction<51:42> */
689 f16_val = deposit32(0, 15, 1, f16_sign);
690 f16_val = deposit32(f16_val, 10, 5, f16_exp);
691 f16_val = deposit32(f16_val, 0, 10, extract64(f64_frac, 52 - 10, 10));
692 return make_float16(f16_val);
695 float32 HELPER(recpe_f32)(float32 input, void *fpstp)
697 float_status *fpst = fpstp;
698 float32 f32 = float32_squash_input_denormal(input, fpst);
699 uint32_t f32_val = float32_val(f32);
700 bool f32_sign = float32_is_neg(f32);
701 int f32_exp = extract32(f32_val, 23, 8);
702 uint32_t f32_frac = extract32(f32_val, 0, 23);
703 uint64_t f64_frac;
705 if (float32_is_any_nan(f32)) {
706 float32 nan = f32;
707 if (float32_is_signaling_nan(f32, fpst)) {
708 float_raise(float_flag_invalid, fpst);
709 nan = float32_silence_nan(f32, fpst);
711 if (fpst->default_nan_mode) {
712 nan = float32_default_nan(fpst);
714 return nan;
715 } else if (float32_is_infinity(f32)) {
716 return float32_set_sign(float32_zero, float32_is_neg(f32));
717 } else if (float32_is_zero(f32)) {
718 float_raise(float_flag_divbyzero, fpst);
719 return float32_set_sign(float32_infinity, float32_is_neg(f32));
720 } else if (float32_abs(f32) < (1ULL << 21)) {
721 /* Abs(value) < 2.0^-128 */
722 float_raise(float_flag_overflow | float_flag_inexact, fpst);
723 if (round_to_inf(fpst, f32_sign)) {
724 return float32_set_sign(float32_infinity, f32_sign);
725 } else {
726 return float32_set_sign(float32_maxnorm, f32_sign);
728 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
729 float_raise(float_flag_underflow, fpst);
730 return float32_set_sign(float32_zero, float32_is_neg(f32));
733 f64_frac = call_recip_estimate(&f32_exp, 253,
734 ((uint64_t) f32_frac) << (52 - 23));
736 /* result = sign : result_exp<7:0> : fraction<51:29> */
737 f32_val = deposit32(0, 31, 1, f32_sign);
738 f32_val = deposit32(f32_val, 23, 8, f32_exp);
739 f32_val = deposit32(f32_val, 0, 23, extract64(f64_frac, 52 - 23, 23));
740 return make_float32(f32_val);
743 float64 HELPER(recpe_f64)(float64 input, void *fpstp)
745 float_status *fpst = fpstp;
746 float64 f64 = float64_squash_input_denormal(input, fpst);
747 uint64_t f64_val = float64_val(f64);
748 bool f64_sign = float64_is_neg(f64);
749 int f64_exp = extract64(f64_val, 52, 11);
750 uint64_t f64_frac = extract64(f64_val, 0, 52);
752 /* Deal with any special cases */
753 if (float64_is_any_nan(f64)) {
754 float64 nan = f64;
755 if (float64_is_signaling_nan(f64, fpst)) {
756 float_raise(float_flag_invalid, fpst);
757 nan = float64_silence_nan(f64, fpst);
759 if (fpst->default_nan_mode) {
760 nan = float64_default_nan(fpst);
762 return nan;
763 } else if (float64_is_infinity(f64)) {
764 return float64_set_sign(float64_zero, float64_is_neg(f64));
765 } else if (float64_is_zero(f64)) {
766 float_raise(float_flag_divbyzero, fpst);
767 return float64_set_sign(float64_infinity, float64_is_neg(f64));
768 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
769 /* Abs(value) < 2.0^-1024 */
770 float_raise(float_flag_overflow | float_flag_inexact, fpst);
771 if (round_to_inf(fpst, f64_sign)) {
772 return float64_set_sign(float64_infinity, f64_sign);
773 } else {
774 return float64_set_sign(float64_maxnorm, f64_sign);
776 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
777 float_raise(float_flag_underflow, fpst);
778 return float64_set_sign(float64_zero, float64_is_neg(f64));
781 f64_frac = call_recip_estimate(&f64_exp, 2045, f64_frac);
783 /* result = sign : result_exp<10:0> : fraction<51:0>; */
784 f64_val = deposit64(0, 63, 1, f64_sign);
785 f64_val = deposit64(f64_val, 52, 11, f64_exp);
786 f64_val = deposit64(f64_val, 0, 52, f64_frac);
787 return make_float64(f64_val);
790 /* The algorithm that must be used to calculate the estimate
791 * is specified by the ARM ARM.
794 static int do_recip_sqrt_estimate(int a)
796 int b, estimate;
798 assert(128 <= a && a < 512);
799 if (a < 256) {
800 a = a * 2 + 1;
801 } else {
802 a = (a >> 1) << 1;
803 a = (a + 1) * 2;
805 b = 512;
806 while (a * (b + 1) * (b + 1) < (1 << 28)) {
807 b += 1;
809 estimate = (b + 1) / 2;
810 assert(256 <= estimate && estimate < 512);
812 return estimate;
816 static uint64_t recip_sqrt_estimate(int *exp , int exp_off, uint64_t frac)
818 int estimate;
819 uint32_t scaled;
821 if (*exp == 0) {
822 while (extract64(frac, 51, 1) == 0) {
823 frac = frac << 1;
824 *exp -= 1;
826 frac = extract64(frac, 0, 51) << 1;
829 if (*exp & 1) {
830 /* scaled = UInt('01':fraction<51:45>) */
831 scaled = deposit32(1 << 7, 0, 7, extract64(frac, 45, 7));
832 } else {
833 /* scaled = UInt('1':fraction<51:44>) */
834 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8));
836 estimate = do_recip_sqrt_estimate(scaled);
838 *exp = (exp_off - *exp) / 2;
839 return extract64(estimate, 0, 8) << 44;
842 uint32_t HELPER(rsqrte_f16)(uint32_t input, void *fpstp)
844 float_status *s = fpstp;
845 float16 f16 = float16_squash_input_denormal(input, s);
846 uint16_t val = float16_val(f16);
847 bool f16_sign = float16_is_neg(f16);
848 int f16_exp = extract32(val, 10, 5);
849 uint16_t f16_frac = extract32(val, 0, 10);
850 uint64_t f64_frac;
852 if (float16_is_any_nan(f16)) {
853 float16 nan = f16;
854 if (float16_is_signaling_nan(f16, s)) {
855 float_raise(float_flag_invalid, s);
856 nan = float16_silence_nan(f16, s);
858 if (s->default_nan_mode) {
859 nan = float16_default_nan(s);
861 return nan;
862 } else if (float16_is_zero(f16)) {
863 float_raise(float_flag_divbyzero, s);
864 return float16_set_sign(float16_infinity, f16_sign);
865 } else if (f16_sign) {
866 float_raise(float_flag_invalid, s);
867 return float16_default_nan(s);
868 } else if (float16_is_infinity(f16)) {
869 return float16_zero;
872 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
873 * preserving the parity of the exponent. */
875 f64_frac = ((uint64_t) f16_frac) << (52 - 10);
877 f64_frac = recip_sqrt_estimate(&f16_exp, 44, f64_frac);
879 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(2) */
880 val = deposit32(0, 15, 1, f16_sign);
881 val = deposit32(val, 10, 5, f16_exp);
882 val = deposit32(val, 2, 8, extract64(f64_frac, 52 - 8, 8));
883 return make_float16(val);
886 float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
888 float_status *s = fpstp;
889 float32 f32 = float32_squash_input_denormal(input, s);
890 uint32_t val = float32_val(f32);
891 uint32_t f32_sign = float32_is_neg(f32);
892 int f32_exp = extract32(val, 23, 8);
893 uint32_t f32_frac = extract32(val, 0, 23);
894 uint64_t f64_frac;
896 if (float32_is_any_nan(f32)) {
897 float32 nan = f32;
898 if (float32_is_signaling_nan(f32, s)) {
899 float_raise(float_flag_invalid, s);
900 nan = float32_silence_nan(f32, s);
902 if (s->default_nan_mode) {
903 nan = float32_default_nan(s);
905 return nan;
906 } else if (float32_is_zero(f32)) {
907 float_raise(float_flag_divbyzero, s);
908 return float32_set_sign(float32_infinity, float32_is_neg(f32));
909 } else if (float32_is_neg(f32)) {
910 float_raise(float_flag_invalid, s);
911 return float32_default_nan(s);
912 } else if (float32_is_infinity(f32)) {
913 return float32_zero;
916 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
917 * preserving the parity of the exponent. */
919 f64_frac = ((uint64_t) f32_frac) << 29;
921 f64_frac = recip_sqrt_estimate(&f32_exp, 380, f64_frac);
923 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(15) */
924 val = deposit32(0, 31, 1, f32_sign);
925 val = deposit32(val, 23, 8, f32_exp);
926 val = deposit32(val, 15, 8, extract64(f64_frac, 52 - 8, 8));
927 return make_float32(val);
930 float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
932 float_status *s = fpstp;
933 float64 f64 = float64_squash_input_denormal(input, s);
934 uint64_t val = float64_val(f64);
935 bool f64_sign = float64_is_neg(f64);
936 int f64_exp = extract64(val, 52, 11);
937 uint64_t f64_frac = extract64(val, 0, 52);
939 if (float64_is_any_nan(f64)) {
940 float64 nan = f64;
941 if (float64_is_signaling_nan(f64, s)) {
942 float_raise(float_flag_invalid, s);
943 nan = float64_silence_nan(f64, s);
945 if (s->default_nan_mode) {
946 nan = float64_default_nan(s);
948 return nan;
949 } else if (float64_is_zero(f64)) {
950 float_raise(float_flag_divbyzero, s);
951 return float64_set_sign(float64_infinity, float64_is_neg(f64));
952 } else if (float64_is_neg(f64)) {
953 float_raise(float_flag_invalid, s);
954 return float64_default_nan(s);
955 } else if (float64_is_infinity(f64)) {
956 return float64_zero;
959 f64_frac = recip_sqrt_estimate(&f64_exp, 3068, f64_frac);
961 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(44) */
962 val = deposit64(0, 61, 1, f64_sign);
963 val = deposit64(val, 52, 11, f64_exp);
964 val = deposit64(val, 44, 8, extract64(f64_frac, 52 - 8, 8));
965 return make_float64(val);
968 uint32_t HELPER(recpe_u32)(uint32_t a)
970 int input, estimate;
972 if ((a & 0x80000000) == 0) {
973 return 0xffffffff;
976 input = extract32(a, 23, 9);
977 estimate = recip_estimate(input);
979 return deposit32(0, (32 - 9), 9, estimate);
982 uint32_t HELPER(rsqrte_u32)(uint32_t a)
984 int estimate;
986 if ((a & 0xc0000000) == 0) {
987 return 0xffffffff;
990 estimate = do_recip_sqrt_estimate(extract32(a, 23, 9));
992 return deposit32(0, 23, 9, estimate);
995 /* VFPv4 fused multiply-accumulate */
996 dh_ctype_f16 VFP_HELPER(muladd, h)(dh_ctype_f16 a, dh_ctype_f16 b,
997 dh_ctype_f16 c, void *fpstp)
999 float_status *fpst = fpstp;
1000 return float16_muladd(a, b, c, 0, fpst);
1003 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
1005 float_status *fpst = fpstp;
1006 return float32_muladd(a, b, c, 0, fpst);
1009 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
1011 float_status *fpst = fpstp;
1012 return float64_muladd(a, b, c, 0, fpst);
1015 /* ARMv8 round to integral */
1016 dh_ctype_f16 HELPER(rinth_exact)(dh_ctype_f16 x, void *fp_status)
1018 return float16_round_to_int(x, fp_status);
1021 float32 HELPER(rints_exact)(float32 x, void *fp_status)
1023 return float32_round_to_int(x, fp_status);
1026 float64 HELPER(rintd_exact)(float64 x, void *fp_status)
1028 return float64_round_to_int(x, fp_status);
1031 dh_ctype_f16 HELPER(rinth)(dh_ctype_f16 x, void *fp_status)
1033 int old_flags = get_float_exception_flags(fp_status), new_flags;
1034 float16 ret;
1036 ret = float16_round_to_int(x, fp_status);
1038 /* Suppress any inexact exceptions the conversion produced */
1039 if (!(old_flags & float_flag_inexact)) {
1040 new_flags = get_float_exception_flags(fp_status);
1041 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
1044 return ret;
1047 float32 HELPER(rints)(float32 x, void *fp_status)
1049 int old_flags = get_float_exception_flags(fp_status), new_flags;
1050 float32 ret;
1052 ret = float32_round_to_int(x, fp_status);
1054 /* Suppress any inexact exceptions the conversion produced */
1055 if (!(old_flags & float_flag_inexact)) {
1056 new_flags = get_float_exception_flags(fp_status);
1057 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
1060 return ret;
1063 float64 HELPER(rintd)(float64 x, void *fp_status)
1065 int old_flags = get_float_exception_flags(fp_status), new_flags;
1066 float64 ret;
1068 ret = float64_round_to_int(x, fp_status);
1070 new_flags = get_float_exception_flags(fp_status);
1072 /* Suppress any inexact exceptions the conversion produced */
1073 if (!(old_flags & float_flag_inexact)) {
1074 new_flags = get_float_exception_flags(fp_status);
1075 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
1078 return ret;
1081 /* Convert ARM rounding mode to softfloat */
1082 int arm_rmode_to_sf(int rmode)
1084 switch (rmode) {
1085 case FPROUNDING_TIEAWAY:
1086 rmode = float_round_ties_away;
1087 break;
1088 case FPROUNDING_ODD:
1089 /* FIXME: add support for TIEAWAY and ODD */
1090 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
1091 rmode);
1092 /* fall through for now */
1093 case FPROUNDING_TIEEVEN:
1094 default:
1095 rmode = float_round_nearest_even;
1096 break;
1097 case FPROUNDING_POSINF:
1098 rmode = float_round_up;
1099 break;
1100 case FPROUNDING_NEGINF:
1101 rmode = float_round_down;
1102 break;
1103 case FPROUNDING_ZERO:
1104 rmode = float_round_to_zero;
1105 break;
1107 return rmode;
1111 * Implement float64 to int32_t conversion without saturation;
1112 * the result is supplied modulo 2^32.
1114 uint64_t HELPER(fjcvtzs)(float64 value, void *vstatus)
1116 float_status *status = vstatus;
1117 uint32_t exp, sign;
1118 uint64_t frac;
1119 uint32_t inexact = 1; /* !Z */
1121 sign = extract64(value, 63, 1);
1122 exp = extract64(value, 52, 11);
1123 frac = extract64(value, 0, 52);
1125 if (exp == 0) {
1126 /* While not inexact for IEEE FP, -0.0 is inexact for JavaScript. */
1127 inexact = sign;
1128 if (frac != 0) {
1129 if (status->flush_inputs_to_zero) {
1130 float_raise(float_flag_input_denormal, status);
1131 } else {
1132 float_raise(float_flag_inexact, status);
1133 inexact = 1;
1136 frac = 0;
1137 } else if (exp == 0x7ff) {
1138 /* This operation raises Invalid for both NaN and overflow (Inf). */
1139 float_raise(float_flag_invalid, status);
1140 frac = 0;
1141 } else {
1142 int true_exp = exp - 1023;
1143 int shift = true_exp - 52;
1145 /* Restore implicit bit. */
1146 frac |= 1ull << 52;
1148 /* Shift the fraction into place. */
1149 if (shift >= 0) {
1150 /* The number is so large we must shift the fraction left. */
1151 if (shift >= 64) {
1152 /* The fraction is shifted out entirely. */
1153 frac = 0;
1154 } else {
1155 frac <<= shift;
1157 } else if (shift > -64) {
1158 /* Normal case -- shift right and notice if bits shift out. */
1159 inexact = (frac << (64 + shift)) != 0;
1160 frac >>= -shift;
1161 } else {
1162 /* The fraction is shifted out entirely. */
1163 frac = 0;
1166 /* Notice overflow or inexact exceptions. */
1167 if (true_exp > 31 || frac > (sign ? 0x80000000ull : 0x7fffffff)) {
1168 /* Overflow, for which this operation raises invalid. */
1169 float_raise(float_flag_invalid, status);
1170 inexact = 1;
1171 } else if (inexact) {
1172 float_raise(float_flag_inexact, status);
1175 /* Honor the sign. */
1176 if (sign) {
1177 frac = -frac;
1181 /* Pack the result and the env->ZF representation of Z together. */
1182 return deposit64(frac, 32, 32, inexact);
1185 uint32_t HELPER(vjcvt)(float64 value, CPUARMState *env)
1187 uint64_t pair = HELPER(fjcvtzs)(value, &env->vfp.fp_status);
1188 uint32_t result = pair;
1189 uint32_t z = (pair >> 32) == 0;
1191 /* Store Z, clear NCV, in FPSCR.NZCV. */
1192 env->vfp.xregs[ARM_VFP_FPSCR]
1193 = (env->vfp.xregs[ARM_VFP_FPSCR] & ~CPSR_NZCV) | (z * CPSR_Z);
1195 return result;
1198 /* Round a float32 to an integer that fits in int32_t or int64_t. */
1199 static float32 frint_s(float32 f, float_status *fpst, int intsize)
1201 int old_flags = get_float_exception_flags(fpst);
1202 uint32_t exp = extract32(f, 23, 8);
1204 if (unlikely(exp == 0xff)) {
1205 /* NaN or Inf. */
1206 goto overflow;
1209 /* Round and re-extract the exponent. */
1210 f = float32_round_to_int(f, fpst);
1211 exp = extract32(f, 23, 8);
1213 /* Validate the range of the result. */
1214 if (exp < 126 + intsize) {
1215 /* abs(F) <= INT{N}_MAX */
1216 return f;
1218 if (exp == 126 + intsize) {
1219 uint32_t sign = extract32(f, 31, 1);
1220 uint32_t frac = extract32(f, 0, 23);
1221 if (sign && frac == 0) {
1222 /* F == INT{N}_MIN */
1223 return f;
1227 overflow:
1229 * Raise Invalid and return INT{N}_MIN as a float. Revert any
1230 * inexact exception float32_round_to_int may have raised.
1232 set_float_exception_flags(old_flags | float_flag_invalid, fpst);
1233 return (0x100u + 126u + intsize) << 23;
1236 float32 HELPER(frint32_s)(float32 f, void *fpst)
1238 return frint_s(f, fpst, 32);
1241 float32 HELPER(frint64_s)(float32 f, void *fpst)
1243 return frint_s(f, fpst, 64);
1246 /* Round a float64 to an integer that fits in int32_t or int64_t. */
1247 static float64 frint_d(float64 f, float_status *fpst, int intsize)
1249 int old_flags = get_float_exception_flags(fpst);
1250 uint32_t exp = extract64(f, 52, 11);
1252 if (unlikely(exp == 0x7ff)) {
1253 /* NaN or Inf. */
1254 goto overflow;
1257 /* Round and re-extract the exponent. */
1258 f = float64_round_to_int(f, fpst);
1259 exp = extract64(f, 52, 11);
1261 /* Validate the range of the result. */
1262 if (exp < 1022 + intsize) {
1263 /* abs(F) <= INT{N}_MAX */
1264 return f;
1266 if (exp == 1022 + intsize) {
1267 uint64_t sign = extract64(f, 63, 1);
1268 uint64_t frac = extract64(f, 0, 52);
1269 if (sign && frac == 0) {
1270 /* F == INT{N}_MIN */
1271 return f;
1275 overflow:
1277 * Raise Invalid and return INT{N}_MIN as a float. Revert any
1278 * inexact exception float64_round_to_int may have raised.
1280 set_float_exception_flags(old_flags | float_flag_invalid, fpst);
1281 return (uint64_t)(0x800 + 1022 + intsize) << 52;
1284 float64 HELPER(frint32_d)(float64 f, void *fpst)
1286 return frint_d(f, fpst, 32);
1289 float64 HELPER(frint64_d)(float64 f, void *fpst)
1291 return frint_d(f, fpst, 64);
1294 void HELPER(check_hcr_el2_trap)(CPUARMState *env, uint32_t rt, uint32_t reg)
1296 uint32_t syndrome;
1298 switch (reg) {
1299 case ARM_VFP_MVFR0:
1300 case ARM_VFP_MVFR1:
1301 case ARM_VFP_MVFR2:
1302 if (!(arm_hcr_el2_eff(env) & HCR_TID3)) {
1303 return;
1305 break;
1306 case ARM_VFP_FPSID:
1307 if (!(arm_hcr_el2_eff(env) & HCR_TID0)) {
1308 return;
1310 break;
1311 default:
1312 g_assert_not_reached();
1315 syndrome = ((EC_FPIDTRAP << ARM_EL_EC_SHIFT)
1316 | ARM_EL_IL
1317 | (1 << 24) | (0xe << 20) | (7 << 14)
1318 | (reg << 10) | (rt << 5) | 1);
1320 raise_exception(env, EXCP_HYP_TRAP, syndrome, 2);
1323 #endif