1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
28
29
30
31
32
33#ifdef CONFIG_TCG
34
35
36static inline int vfp_exceptbits_from_host(int host_bits)
37{
38 int target_bits = 0;
39
40 if (host_bits & float_flag_invalid) {
41 target_bits |= 1;
42 }
43 if (host_bits & float_flag_divbyzero) {
44 target_bits |= 2;
45 }
46 if (host_bits & float_flag_overflow) {
47 target_bits |= 4;
48 }
49 if (host_bits & (float_flag_underflow | float_flag_output_denormal)) {
50 target_bits |= 8;
51 }
52 if (host_bits & float_flag_inexact) {
53 target_bits |= 0x10;
54 }
55 if (host_bits & float_flag_input_denormal) {
56 target_bits |= 0x80;
57 }
58 return target_bits;
59}
60
61
62static inline int vfp_exceptbits_to_host(int target_bits)
63{
64 int host_bits = 0;
65
66 if (target_bits & 1) {
67 host_bits |= float_flag_invalid;
68 }
69 if (target_bits & 2) {
70 host_bits |= float_flag_divbyzero;
71 }
72 if (target_bits & 4) {
73 host_bits |= float_flag_overflow;
74 }
75 if (target_bits & 8) {
76 host_bits |= float_flag_underflow;
77 }
78 if (target_bits & 0x10) {
79 host_bits |= float_flag_inexact;
80 }
81 if (target_bits & 0x80) {
82 host_bits |= float_flag_input_denormal;
83 }
84 return host_bits;
85}
86
87static uint32_t vfp_get_fpscr_from_host(CPUARMState *env)
88{
89 uint32_t i;
90
91 i = get_float_exception_flags(&env->vfp.fp_status);
92 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
93
94 i |= (get_float_exception_flags(&env->vfp.fp_status_f16)
95 & ~float_flag_input_denormal);
96 return vfp_exceptbits_from_host(i);
97}
98
99static void vfp_set_fpscr_to_host(CPUARMState *env, uint32_t val)
100{
101 int i;
102 uint32_t changed = env->vfp.xregs[ARM_VFP_FPSCR];
103
104 changed ^= val;
105 if (changed & (3 << 22)) {
106 i = (val >> 22) & 3;
107 switch (i) {
108 case FPROUNDING_TIEEVEN:
109 i = float_round_nearest_even;
110 break;
111 case FPROUNDING_POSINF:
112 i = float_round_up;
113 break;
114 case FPROUNDING_NEGINF:
115 i = float_round_down;
116 break;
117 case FPROUNDING_ZERO:
118 i = float_round_to_zero;
119 break;
120 }
121 set_float_rounding_mode(i, &env->vfp.fp_status);
122 set_float_rounding_mode(i, &env->vfp.fp_status_f16);
123 }
124 if (changed & FPCR_FZ16) {
125 bool ftz_enabled = val & FPCR_FZ16;
126 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
127 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status_f16);
128 }
129 if (changed & FPCR_FZ) {
130 bool ftz_enabled = val & FPCR_FZ;
131 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status);
132 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status);
133 }
134 if (changed & FPCR_DN) {
135 bool dnan_enabled = val & FPCR_DN;
136 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status);
137 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status_f16);
138 }
139
140
141
142
143
144
145 i = vfp_exceptbits_to_host(val);
146 set_float_exception_flags(i, &env->vfp.fp_status);
147 set_float_exception_flags(0, &env->vfp.fp_status_f16);
148 set_float_exception_flags(0, &env->vfp.standard_fp_status);
149}
150
151#else
152
153static uint32_t vfp_get_fpscr_from_host(CPUARMState *env)
154{
155 return 0;
156}
157
158static void vfp_set_fpscr_to_host(CPUARMState *env, uint32_t val)
159{
160}
161
162#endif
163
164uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
165{
166 uint32_t i, fpscr;
167
168 fpscr = env->vfp.xregs[ARM_VFP_FPSCR]
169 | (env->vfp.vec_len << 16)
170 | (env->vfp.vec_stride << 20);
171
172 fpscr |= vfp_get_fpscr_from_host(env);
173
174 i = env->vfp.qc[0] | env->vfp.qc[1] | env->vfp.qc[2] | env->vfp.qc[3];
175 fpscr |= i ? FPCR_QC : 0;
176
177 return fpscr;
178}
179
180uint32_t vfp_get_fpscr(CPUARMState *env)
181{
182 return HELPER(vfp_get_fpscr)(env);
183}
184
185void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
186{
187
188 if (!cpu_isar_feature(aa64_fp16, env_archcpu(env))) {
189 val &= ~FPCR_FZ16;
190 }
191
192 if (arm_feature(env, ARM_FEATURE_M)) {
193
194
195
196
197 val &= 0xf7c0009f;
198 }
199
200 vfp_set_fpscr_to_host(env, val);
201
202
203
204
205
206
207
208
209
210 env->vfp.xregs[ARM_VFP_FPSCR] = val & 0xf7c80000;
211 env->vfp.vec_len = (val >> 16) & 7;
212 env->vfp.vec_stride = (val >> 20) & 3;
213
214
215
216
217
218 env->vfp.qc[0] = val & FPCR_QC;
219 env->vfp.qc[1] = 0;
220 env->vfp.qc[2] = 0;
221 env->vfp.qc[3] = 0;
222}
223
224void vfp_set_fpscr(CPUARMState *env, uint32_t val)
225{
226 HELPER(vfp_set_fpscr)(env, val);
227}
228
229#ifdef CONFIG_TCG
230
231#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
232
233#define VFP_BINOP(name) \
234float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
235{ \
236 float_status *fpst = fpstp; \
237 return float32_ ## name(a, b, fpst); \
238} \
239float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
240{ \
241 float_status *fpst = fpstp; \
242 return float64_ ## name(a, b, fpst); \
243}
244VFP_BINOP(add)
245VFP_BINOP(sub)
246VFP_BINOP(mul)
247VFP_BINOP(div)
248VFP_BINOP(min)
249VFP_BINOP(max)
250VFP_BINOP(minnum)
251VFP_BINOP(maxnum)
252#undef VFP_BINOP
253
254float32 VFP_HELPER(neg, s)(float32 a)
255{
256 return float32_chs(a);
257}
258
259float64 VFP_HELPER(neg, d)(float64 a)
260{
261 return float64_chs(a);
262}
263
264float32 VFP_HELPER(abs, s)(float32 a)
265{
266 return float32_abs(a);
267}
268
269float64 VFP_HELPER(abs, d)(float64 a)
270{
271 return float64_abs(a);
272}
273
274float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
275{
276 return float32_sqrt(a, &env->vfp.fp_status);
277}
278
279float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
280{
281 return float64_sqrt(a, &env->vfp.fp_status);
282}
283
284static void softfloat_to_vfp_compare(CPUARMState *env, int cmp)
285{
286 uint32_t flags;
287 switch (cmp) {
288 case float_relation_equal:
289 flags = 0x6;
290 break;
291 case float_relation_less:
292 flags = 0x8;
293 break;
294 case float_relation_greater:
295 flags = 0x2;
296 break;
297 case float_relation_unordered:
298 flags = 0x3;
299 break;
300 default:
301 g_assert_not_reached();
302 }
303 env->vfp.xregs[ARM_VFP_FPSCR] =
304 deposit32(env->vfp.xregs[ARM_VFP_FPSCR], 28, 4, flags);
305}
306
307
308#define DO_VFP_cmp(p, type) \
309void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
310{ \
311 softfloat_to_vfp_compare(env, \
312 type ## _compare_quiet(a, b, &env->vfp.fp_status)); \
313} \
314void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
315{ \
316 softfloat_to_vfp_compare(env, \
317 type ## _compare(a, b, &env->vfp.fp_status)); \
318}
319DO_VFP_cmp(s, float32)
320DO_VFP_cmp(d, float64)
321#undef DO_VFP_cmp
322
323
324
325#define CONV_ITOF(name, ftype, fsz, sign) \
326ftype HELPER(name)(uint32_t x, void *fpstp) \
327{ \
328 float_status *fpst = fpstp; \
329 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
330}
331
332#define CONV_FTOI(name, ftype, fsz, sign, round) \
333sign##int32_t HELPER(name)(ftype x, void *fpstp) \
334{ \
335 float_status *fpst = fpstp; \
336 if (float##fsz##_is_any_nan(x)) { \
337 float_raise(float_flag_invalid, fpst); \
338 return 0; \
339 } \
340 return float##fsz##_to_##sign##int32##round(x, fpst); \
341}
342
343#define FLOAT_CONVS(name, p, ftype, fsz, sign) \
344 CONV_ITOF(vfp_##name##to##p, ftype, fsz, sign) \
345 CONV_FTOI(vfp_to##name##p, ftype, fsz, sign, ) \
346 CONV_FTOI(vfp_to##name##z##p, ftype, fsz, sign, _round_to_zero)
347
348FLOAT_CONVS(si, h, uint32_t, 16, )
349FLOAT_CONVS(si, s, float32, 32, )
350FLOAT_CONVS(si, d, float64, 64, )
351FLOAT_CONVS(ui, h, uint32_t, 16, u)
352FLOAT_CONVS(ui, s, float32, 32, u)
353FLOAT_CONVS(ui, d, float64, 64, u)
354
355#undef CONV_ITOF
356#undef CONV_FTOI
357#undef FLOAT_CONVS
358
359
360float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
361{
362 return float32_to_float64(x, &env->vfp.fp_status);
363}
364
365float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
366{
367 return float64_to_float32(x, &env->vfp.fp_status);
368}
369
370
371#define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
372float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
373 void *fpstp) \
374{ return itype##_to_##float##fsz##_scalbn(x, -shift, fpstp); }
375
376#define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, ROUND, suff) \
377uint##isz##_t HELPER(vfp_to##name##p##suff)(float##fsz x, uint32_t shift, \
378 void *fpst) \
379{ \
380 if (unlikely(float##fsz##_is_any_nan(x))) { \
381 float_raise(float_flag_invalid, fpst); \
382 return 0; \
383 } \
384 return float##fsz##_to_##itype##_scalbn(x, ROUND, shift, fpst); \
385}
386
387#define VFP_CONV_FIX(name, p, fsz, isz, itype) \
388VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
389VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, \
390 float_round_to_zero, _round_to_zero) \
391VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, \
392 get_float_rounding_mode(fpst), )
393
394#define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
395VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
396VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, \
397 get_float_rounding_mode(fpst), )
398
399VFP_CONV_FIX(sh, d, 64, 64, int16)
400VFP_CONV_FIX(sl, d, 64, 64, int32)
401VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
402VFP_CONV_FIX(uh, d, 64, 64, uint16)
403VFP_CONV_FIX(ul, d, 64, 64, uint32)
404VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
405VFP_CONV_FIX(sh, s, 32, 32, int16)
406VFP_CONV_FIX(sl, s, 32, 32, int32)
407VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
408VFP_CONV_FIX(uh, s, 32, 32, uint16)
409VFP_CONV_FIX(ul, s, 32, 32, uint32)
410VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
411
412#undef VFP_CONV_FIX
413#undef VFP_CONV_FIX_FLOAT
414#undef VFP_CONV_FLOAT_FIX_ROUND
415#undef VFP_CONV_FIX_A64
416
417uint32_t HELPER(vfp_sltoh)(uint32_t x, uint32_t shift, void *fpst)
418{
419 return int32_to_float16_scalbn(x, -shift, fpst);
420}
421
422uint32_t HELPER(vfp_ultoh)(uint32_t x, uint32_t shift, void *fpst)
423{
424 return uint32_to_float16_scalbn(x, -shift, fpst);
425}
426
427uint32_t HELPER(vfp_sqtoh)(uint64_t x, uint32_t shift, void *fpst)
428{
429 return int64_to_float16_scalbn(x, -shift, fpst);
430}
431
432uint32_t HELPER(vfp_uqtoh)(uint64_t x, uint32_t shift, void *fpst)
433{
434 return uint64_to_float16_scalbn(x, -shift, fpst);
435}
436
437uint32_t HELPER(vfp_toshh)(uint32_t x, uint32_t shift, void *fpst)
438{
439 if (unlikely(float16_is_any_nan(x))) {
440 float_raise(float_flag_invalid, fpst);
441 return 0;
442 }
443 return float16_to_int16_scalbn(x, get_float_rounding_mode(fpst),
444 shift, fpst);
445}
446
447uint32_t HELPER(vfp_touhh)(uint32_t x, uint32_t shift, void *fpst)
448{
449 if (unlikely(float16_is_any_nan(x))) {
450 float_raise(float_flag_invalid, fpst);
451 return 0;
452 }
453 return float16_to_uint16_scalbn(x, get_float_rounding_mode(fpst),
454 shift, fpst);
455}
456
457uint32_t HELPER(vfp_toslh)(uint32_t x, uint32_t shift, void *fpst)
458{
459 if (unlikely(float16_is_any_nan(x))) {
460 float_raise(float_flag_invalid, fpst);
461 return 0;
462 }
463 return float16_to_int32_scalbn(x, get_float_rounding_mode(fpst),
464 shift, fpst);
465}
466
467uint32_t HELPER(vfp_toulh)(uint32_t x, uint32_t shift, void *fpst)
468{
469 if (unlikely(float16_is_any_nan(x))) {
470 float_raise(float_flag_invalid, fpst);
471 return 0;
472 }
473 return float16_to_uint32_scalbn(x, get_float_rounding_mode(fpst),
474 shift, fpst);
475}
476
477uint64_t HELPER(vfp_tosqh)(uint32_t x, uint32_t shift, void *fpst)
478{
479 if (unlikely(float16_is_any_nan(x))) {
480 float_raise(float_flag_invalid, fpst);
481 return 0;
482 }
483 return float16_to_int64_scalbn(x, get_float_rounding_mode(fpst),
484 shift, fpst);
485}
486
487uint64_t HELPER(vfp_touqh)(uint32_t x, uint32_t shift, void *fpst)
488{
489 if (unlikely(float16_is_any_nan(x))) {
490 float_raise(float_flag_invalid, fpst);
491 return 0;
492 }
493 return float16_to_uint64_scalbn(x, get_float_rounding_mode(fpst),
494 shift, fpst);
495}
496
497
498
499
500uint32_t HELPER(set_rmode)(uint32_t rmode, void *fpstp)
501{
502 float_status *fp_status = fpstp;
503
504 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
505 set_float_rounding_mode(rmode, fp_status);
506
507 return prev_rmode;
508}
509
510
511
512
513
514
515
516
517uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
518{
519 float_status *fp_status = &env->vfp.standard_fp_status;
520
521 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
522 set_float_rounding_mode(rmode, fp_status);
523
524 return prev_rmode;
525}
526
527
528float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, void *fpstp, uint32_t ahp_mode)
529{
530
531
532
533 float_status *fpst = fpstp;
534 flag save = get_flush_inputs_to_zero(fpst);
535 set_flush_inputs_to_zero(false, fpst);
536 float32 r = float16_to_float32(a, !ahp_mode, fpst);
537 set_flush_inputs_to_zero(save, fpst);
538 return r;
539}
540
541uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, void *fpstp, uint32_t ahp_mode)
542{
543
544
545
546 float_status *fpst = fpstp;
547 flag save = get_flush_to_zero(fpst);
548 set_flush_to_zero(false, fpst);
549 float16 r = float32_to_float16(a, !ahp_mode, fpst);
550 set_flush_to_zero(save, fpst);
551 return r;
552}
553
554float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, void *fpstp, uint32_t ahp_mode)
555{
556
557
558
559 float_status *fpst = fpstp;
560 flag save = get_flush_inputs_to_zero(fpst);
561 set_flush_inputs_to_zero(false, fpst);
562 float64 r = float16_to_float64(a, !ahp_mode, fpst);
563 set_flush_inputs_to_zero(save, fpst);
564 return r;
565}
566
567uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, void *fpstp, uint32_t ahp_mode)
568{
569
570
571
572 float_status *fpst = fpstp;
573 flag save = get_flush_to_zero(fpst);
574 set_flush_to_zero(false, fpst);
575 float16 r = float64_to_float16(a, !ahp_mode, fpst);
576 set_flush_to_zero(save, fpst);
577 return r;
578}
579
580#define float32_two make_float32(0x40000000)
581#define float32_three make_float32(0x40400000)
582#define float32_one_point_five make_float32(0x3fc00000)
583
584float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
585{
586 float_status *s = &env->vfp.standard_fp_status;
587 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
588 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
589 if (!(float32_is_zero(a) || float32_is_zero(b))) {
590 float_raise(float_flag_input_denormal, s);
591 }
592 return float32_two;
593 }
594 return float32_sub(float32_two, float32_mul(a, b, s), s);
595}
596
597float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
598{
599 float_status *s = &env->vfp.standard_fp_status;
600 float32 product;
601 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
602 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
603 if (!(float32_is_zero(a) || float32_is_zero(b))) {
604 float_raise(float_flag_input_denormal, s);
605 }
606 return float32_one_point_five;
607 }
608 product = float32_mul(a, b, s);
609 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
610}
611
612
613
614
615
616#define float64_256 make_float64(0x4070000000000000LL)
617#define float64_512 make_float64(0x4080000000000000LL)
618#define float16_maxnorm make_float16(0x7bff)
619#define float32_maxnorm make_float32(0x7f7fffff)
620#define float64_maxnorm make_float64(0x7fefffffffffffffLL)
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635static int recip_estimate(int input)
636{
637 int a, b, r;
638 assert(256 <= input && input < 512);
639 a = (input * 2) + 1;
640 b = (1 << 19) / a;
641 r = (b + 1) >> 1;
642 assert(256 <= r && r < 512);
643 return r;
644}
645
646
647
648
649
650
651
652
653
654
655static uint64_t call_recip_estimate(int *exp, int exp_off, uint64_t frac)
656{
657 uint32_t scaled, estimate;
658 uint64_t result_frac;
659 int result_exp;
660
661
662 if (*exp == 0) {
663 if (extract64(frac, 51, 1) == 0) {
664 *exp = -1;
665 frac <<= 2;
666 } else {
667 frac <<= 1;
668 }
669 }
670
671
672 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8));
673 estimate = recip_estimate(scaled);
674
675 result_exp = exp_off - *exp;
676 result_frac = deposit64(0, 44, 8, estimate);
677 if (result_exp == 0) {
678 result_frac = deposit64(result_frac >> 1, 51, 1, 1);
679 } else if (result_exp == -1) {
680 result_frac = deposit64(result_frac >> 2, 50, 2, 1);
681 result_exp = 0;
682 }
683
684 *exp = result_exp;
685
686 return result_frac;
687}
688
689static bool round_to_inf(float_status *fpst, bool sign_bit)
690{
691 switch (fpst->float_rounding_mode) {
692 case float_round_nearest_even:
693 return true;
694 case float_round_up:
695 return !sign_bit;
696 case float_round_down:
697 return sign_bit;
698 case float_round_to_zero:
699 return false;
700 }
701
702 g_assert_not_reached();
703}
704
705uint32_t HELPER(recpe_f16)(uint32_t input, void *fpstp)
706{
707 float_status *fpst = fpstp;
708 float16 f16 = float16_squash_input_denormal(input, fpst);
709 uint32_t f16_val = float16_val(f16);
710 uint32_t f16_sign = float16_is_neg(f16);
711 int f16_exp = extract32(f16_val, 10, 5);
712 uint32_t f16_frac = extract32(f16_val, 0, 10);
713 uint64_t f64_frac;
714
715 if (float16_is_any_nan(f16)) {
716 float16 nan = f16;
717 if (float16_is_signaling_nan(f16, fpst)) {
718 float_raise(float_flag_invalid, fpst);
719 nan = float16_silence_nan(f16, fpst);
720 }
721 if (fpst->default_nan_mode) {
722 nan = float16_default_nan(fpst);
723 }
724 return nan;
725 } else if (float16_is_infinity(f16)) {
726 return float16_set_sign(float16_zero, float16_is_neg(f16));
727 } else if (float16_is_zero(f16)) {
728 float_raise(float_flag_divbyzero, fpst);
729 return float16_set_sign(float16_infinity, float16_is_neg(f16));
730 } else if (float16_abs(f16) < (1 << 8)) {
731
732 float_raise(float_flag_overflow | float_flag_inexact, fpst);
733 if (round_to_inf(fpst, f16_sign)) {
734 return float16_set_sign(float16_infinity, f16_sign);
735 } else {
736 return float16_set_sign(float16_maxnorm, f16_sign);
737 }
738 } else if (f16_exp >= 29 && fpst->flush_to_zero) {
739 float_raise(float_flag_underflow, fpst);
740 return float16_set_sign(float16_zero, float16_is_neg(f16));
741 }
742
743 f64_frac = call_recip_estimate(&f16_exp, 29,
744 ((uint64_t) f16_frac) << (52 - 10));
745
746
747 f16_val = deposit32(0, 15, 1, f16_sign);
748 f16_val = deposit32(f16_val, 10, 5, f16_exp);
749 f16_val = deposit32(f16_val, 0, 10, extract64(f64_frac, 52 - 10, 10));
750 return make_float16(f16_val);
751}
752
753float32 HELPER(recpe_f32)(float32 input, void *fpstp)
754{
755 float_status *fpst = fpstp;
756 float32 f32 = float32_squash_input_denormal(input, fpst);
757 uint32_t f32_val = float32_val(f32);
758 bool f32_sign = float32_is_neg(f32);
759 int f32_exp = extract32(f32_val, 23, 8);
760 uint32_t f32_frac = extract32(f32_val, 0, 23);
761 uint64_t f64_frac;
762
763 if (float32_is_any_nan(f32)) {
764 float32 nan = f32;
765 if (float32_is_signaling_nan(f32, fpst)) {
766 float_raise(float_flag_invalid, fpst);
767 nan = float32_silence_nan(f32, fpst);
768 }
769 if (fpst->default_nan_mode) {
770 nan = float32_default_nan(fpst);
771 }
772 return nan;
773 } else if (float32_is_infinity(f32)) {
774 return float32_set_sign(float32_zero, float32_is_neg(f32));
775 } else if (float32_is_zero(f32)) {
776 float_raise(float_flag_divbyzero, fpst);
777 return float32_set_sign(float32_infinity, float32_is_neg(f32));
778 } else if (float32_abs(f32) < (1ULL << 21)) {
779
780 float_raise(float_flag_overflow | float_flag_inexact, fpst);
781 if (round_to_inf(fpst, f32_sign)) {
782 return float32_set_sign(float32_infinity, f32_sign);
783 } else {
784 return float32_set_sign(float32_maxnorm, f32_sign);
785 }
786 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
787 float_raise(float_flag_underflow, fpst);
788 return float32_set_sign(float32_zero, float32_is_neg(f32));
789 }
790
791 f64_frac = call_recip_estimate(&f32_exp, 253,
792 ((uint64_t) f32_frac) << (52 - 23));
793
794
795 f32_val = deposit32(0, 31, 1, f32_sign);
796 f32_val = deposit32(f32_val, 23, 8, f32_exp);
797 f32_val = deposit32(f32_val, 0, 23, extract64(f64_frac, 52 - 23, 23));
798 return make_float32(f32_val);
799}
800
801float64 HELPER(recpe_f64)(float64 input, void *fpstp)
802{
803 float_status *fpst = fpstp;
804 float64 f64 = float64_squash_input_denormal(input, fpst);
805 uint64_t f64_val = float64_val(f64);
806 bool f64_sign = float64_is_neg(f64);
807 int f64_exp = extract64(f64_val, 52, 11);
808 uint64_t f64_frac = extract64(f64_val, 0, 52);
809
810
811 if (float64_is_any_nan(f64)) {
812 float64 nan = f64;
813 if (float64_is_signaling_nan(f64, fpst)) {
814 float_raise(float_flag_invalid, fpst);
815 nan = float64_silence_nan(f64, fpst);
816 }
817 if (fpst->default_nan_mode) {
818 nan = float64_default_nan(fpst);
819 }
820 return nan;
821 } else if (float64_is_infinity(f64)) {
822 return float64_set_sign(float64_zero, float64_is_neg(f64));
823 } else if (float64_is_zero(f64)) {
824 float_raise(float_flag_divbyzero, fpst);
825 return float64_set_sign(float64_infinity, float64_is_neg(f64));
826 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
827
828 float_raise(float_flag_overflow | float_flag_inexact, fpst);
829 if (round_to_inf(fpst, f64_sign)) {
830 return float64_set_sign(float64_infinity, f64_sign);
831 } else {
832 return float64_set_sign(float64_maxnorm, f64_sign);
833 }
834 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
835 float_raise(float_flag_underflow, fpst);
836 return float64_set_sign(float64_zero, float64_is_neg(f64));
837 }
838
839 f64_frac = call_recip_estimate(&f64_exp, 2045, f64_frac);
840
841
842 f64_val = deposit64(0, 63, 1, f64_sign);
843 f64_val = deposit64(f64_val, 52, 11, f64_exp);
844 f64_val = deposit64(f64_val, 0, 52, f64_frac);
845 return make_float64(f64_val);
846}
847
848
849
850
851
852static int do_recip_sqrt_estimate(int a)
853{
854 int b, estimate;
855
856 assert(128 <= a && a < 512);
857 if (a < 256) {
858 a = a * 2 + 1;
859 } else {
860 a = (a >> 1) << 1;
861 a = (a + 1) * 2;
862 }
863 b = 512;
864 while (a * (b + 1) * (b + 1) < (1 << 28)) {
865 b += 1;
866 }
867 estimate = (b + 1) / 2;
868 assert(256 <= estimate && estimate < 512);
869
870 return estimate;
871}
872
873
874static uint64_t recip_sqrt_estimate(int *exp , int exp_off, uint64_t frac)
875{
876 int estimate;
877 uint32_t scaled;
878
879 if (*exp == 0) {
880 while (extract64(frac, 51, 1) == 0) {
881 frac = frac << 1;
882 *exp -= 1;
883 }
884 frac = extract64(frac, 0, 51) << 1;
885 }
886
887 if (*exp & 1) {
888
889 scaled = deposit32(1 << 7, 0, 7, extract64(frac, 45, 7));
890 } else {
891
892 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8));
893 }
894 estimate = do_recip_sqrt_estimate(scaled);
895
896 *exp = (exp_off - *exp) / 2;
897 return extract64(estimate, 0, 8) << 44;
898}
899
900uint32_t HELPER(rsqrte_f16)(uint32_t input, void *fpstp)
901{
902 float_status *s = fpstp;
903 float16 f16 = float16_squash_input_denormal(input, s);
904 uint16_t val = float16_val(f16);
905 bool f16_sign = float16_is_neg(f16);
906 int f16_exp = extract32(val, 10, 5);
907 uint16_t f16_frac = extract32(val, 0, 10);
908 uint64_t f64_frac;
909
910 if (float16_is_any_nan(f16)) {
911 float16 nan = f16;
912 if (float16_is_signaling_nan(f16, s)) {
913 float_raise(float_flag_invalid, s);
914 nan = float16_silence_nan(f16, s);
915 }
916 if (s->default_nan_mode) {
917 nan = float16_default_nan(s);
918 }
919 return nan;
920 } else if (float16_is_zero(f16)) {
921 float_raise(float_flag_divbyzero, s);
922 return float16_set_sign(float16_infinity, f16_sign);
923 } else if (f16_sign) {
924 float_raise(float_flag_invalid, s);
925 return float16_default_nan(s);
926 } else if (float16_is_infinity(f16)) {
927 return float16_zero;
928 }
929
930
931
932
933 f64_frac = ((uint64_t) f16_frac) << (52 - 10);
934
935 f64_frac = recip_sqrt_estimate(&f16_exp, 44, f64_frac);
936
937
938 val = deposit32(0, 15, 1, f16_sign);
939 val = deposit32(val, 10, 5, f16_exp);
940 val = deposit32(val, 2, 8, extract64(f64_frac, 52 - 8, 8));
941 return make_float16(val);
942}
943
944float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
945{
946 float_status *s = fpstp;
947 float32 f32 = float32_squash_input_denormal(input, s);
948 uint32_t val = float32_val(f32);
949 uint32_t f32_sign = float32_is_neg(f32);
950 int f32_exp = extract32(val, 23, 8);
951 uint32_t f32_frac = extract32(val, 0, 23);
952 uint64_t f64_frac;
953
954 if (float32_is_any_nan(f32)) {
955 float32 nan = f32;
956 if (float32_is_signaling_nan(f32, s)) {
957 float_raise(float_flag_invalid, s);
958 nan = float32_silence_nan(f32, s);
959 }
960 if (s->default_nan_mode) {
961 nan = float32_default_nan(s);
962 }
963 return nan;
964 } else if (float32_is_zero(f32)) {
965 float_raise(float_flag_divbyzero, s);
966 return float32_set_sign(float32_infinity, float32_is_neg(f32));
967 } else if (float32_is_neg(f32)) {
968 float_raise(float_flag_invalid, s);
969 return float32_default_nan(s);
970 } else if (float32_is_infinity(f32)) {
971 return float32_zero;
972 }
973
974
975
976
977 f64_frac = ((uint64_t) f32_frac) << 29;
978
979 f64_frac = recip_sqrt_estimate(&f32_exp, 380, f64_frac);
980
981
982 val = deposit32(0, 31, 1, f32_sign);
983 val = deposit32(val, 23, 8, f32_exp);
984 val = deposit32(val, 15, 8, extract64(f64_frac, 52 - 8, 8));
985 return make_float32(val);
986}
987
988float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
989{
990 float_status *s = fpstp;
991 float64 f64 = float64_squash_input_denormal(input, s);
992 uint64_t val = float64_val(f64);
993 bool f64_sign = float64_is_neg(f64);
994 int f64_exp = extract64(val, 52, 11);
995 uint64_t f64_frac = extract64(val, 0, 52);
996
997 if (float64_is_any_nan(f64)) {
998 float64 nan = f64;
999 if (float64_is_signaling_nan(f64, s)) {
1000 float_raise(float_flag_invalid, s);
1001 nan = float64_silence_nan(f64, s);
1002 }
1003 if (s->default_nan_mode) {
1004 nan = float64_default_nan(s);
1005 }
1006 return nan;
1007 } else if (float64_is_zero(f64)) {
1008 float_raise(float_flag_divbyzero, s);
1009 return float64_set_sign(float64_infinity, float64_is_neg(f64));
1010 } else if (float64_is_neg(f64)) {
1011 float_raise(float_flag_invalid, s);
1012 return float64_default_nan(s);
1013 } else if (float64_is_infinity(f64)) {
1014 return float64_zero;
1015 }
1016
1017 f64_frac = recip_sqrt_estimate(&f64_exp, 3068, f64_frac);
1018
1019
1020 val = deposit64(0, 61, 1, f64_sign);
1021 val = deposit64(val, 52, 11, f64_exp);
1022 val = deposit64(val, 44, 8, extract64(f64_frac, 52 - 8, 8));
1023 return make_float64(val);
1024}
1025
1026uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
1027{
1028
1029 int input, estimate;
1030
1031 if ((a & 0x80000000) == 0) {
1032 return 0xffffffff;
1033 }
1034
1035 input = extract32(a, 23, 9);
1036 estimate = recip_estimate(input);
1037
1038 return deposit32(0, (32 - 9), 9, estimate);
1039}
1040
1041uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
1042{
1043 int estimate;
1044
1045 if ((a & 0xc0000000) == 0) {
1046 return 0xffffffff;
1047 }
1048
1049 estimate = do_recip_sqrt_estimate(extract32(a, 23, 9));
1050
1051 return deposit32(0, 23, 9, estimate);
1052}
1053
1054
1055float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
1056{
1057 float_status *fpst = fpstp;
1058 return float32_muladd(a, b, c, 0, fpst);
1059}
1060
1061float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
1062{
1063 float_status *fpst = fpstp;
1064 return float64_muladd(a, b, c, 0, fpst);
1065}
1066
1067
1068float32 HELPER(rints_exact)(float32 x, void *fp_status)
1069{
1070 return float32_round_to_int(x, fp_status);
1071}
1072
1073float64 HELPER(rintd_exact)(float64 x, void *fp_status)
1074{
1075 return float64_round_to_int(x, fp_status);
1076}
1077
1078float32 HELPER(rints)(float32 x, void *fp_status)
1079{
1080 int old_flags = get_float_exception_flags(fp_status), new_flags;
1081 float32 ret;
1082
1083 ret = float32_round_to_int(x, fp_status);
1084
1085
1086 if (!(old_flags & float_flag_inexact)) {
1087 new_flags = get_float_exception_flags(fp_status);
1088 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
1089 }
1090
1091 return ret;
1092}
1093
1094float64 HELPER(rintd)(float64 x, void *fp_status)
1095{
1096 int old_flags = get_float_exception_flags(fp_status), new_flags;
1097 float64 ret;
1098
1099 ret = float64_round_to_int(x, fp_status);
1100
1101 new_flags = get_float_exception_flags(fp_status);
1102
1103
1104 if (!(old_flags & float_flag_inexact)) {
1105 new_flags = get_float_exception_flags(fp_status);
1106 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
1107 }
1108
1109 return ret;
1110}
1111
1112
1113int arm_rmode_to_sf(int rmode)
1114{
1115 switch (rmode) {
1116 case FPROUNDING_TIEAWAY:
1117 rmode = float_round_ties_away;
1118 break;
1119 case FPROUNDING_ODD:
1120
1121 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
1122 rmode);
1123
1124 case FPROUNDING_TIEEVEN:
1125 default:
1126 rmode = float_round_nearest_even;
1127 break;
1128 case FPROUNDING_POSINF:
1129 rmode = float_round_up;
1130 break;
1131 case FPROUNDING_NEGINF:
1132 rmode = float_round_down;
1133 break;
1134 case FPROUNDING_ZERO:
1135 rmode = float_round_to_zero;
1136 break;
1137 }
1138 return rmode;
1139}
1140
1141
1142
1143
1144
1145uint64_t HELPER(fjcvtzs)(float64 value, void *vstatus)
1146{
1147 float_status *status = vstatus;
1148 uint32_t exp, sign;
1149 uint64_t frac;
1150 uint32_t inexact = 1;
1151
1152 sign = extract64(value, 63, 1);
1153 exp = extract64(value, 52, 11);
1154 frac = extract64(value, 0, 52);
1155
1156 if (exp == 0) {
1157
1158 inexact = sign;
1159 if (frac != 0) {
1160 if (status->flush_inputs_to_zero) {
1161 float_raise(float_flag_input_denormal, status);
1162 } else {
1163 float_raise(float_flag_inexact, status);
1164 inexact = 1;
1165 }
1166 }
1167 frac = 0;
1168 } else if (exp == 0x7ff) {
1169
1170 float_raise(float_flag_invalid, status);
1171 frac = 0;
1172 } else {
1173 int true_exp = exp - 1023;
1174 int shift = true_exp - 52;
1175
1176
1177 frac |= 1ull << 52;
1178
1179
1180 if (shift >= 0) {
1181
1182 if (shift >= 64) {
1183
1184 frac = 0;
1185 } else {
1186 frac <<= shift;
1187 }
1188 } else if (shift > -64) {
1189
1190 inexact = (frac << (64 + shift)) != 0;
1191 frac >>= -shift;
1192 } else {
1193
1194 frac = 0;
1195 }
1196
1197
1198 if (true_exp > 31 || frac > (sign ? 0x80000000ull : 0x7fffffff)) {
1199
1200 float_raise(float_flag_invalid, status);
1201 inexact = 1;
1202 } else if (inexact) {
1203 float_raise(float_flag_inexact, status);
1204 }
1205
1206
1207 if (sign) {
1208 frac = -frac;
1209 }
1210 }
1211
1212
1213 return deposit64(frac, 32, 32, inexact);
1214}
1215
1216uint32_t HELPER(vjcvt)(float64 value, CPUARMState *env)
1217{
1218 uint64_t pair = HELPER(fjcvtzs)(value, &env->vfp.fp_status);
1219 uint32_t result = pair;
1220 uint32_t z = (pair >> 32) == 0;
1221
1222
1223 env->vfp.xregs[ARM_VFP_FPSCR]
1224 = (env->vfp.xregs[ARM_VFP_FPSCR] & ~CPSR_NZCV) | (z * CPSR_Z);
1225
1226 return result;
1227}
1228
1229
1230static float32 frint_s(float32 f, float_status *fpst, int intsize)
1231{
1232 int old_flags = get_float_exception_flags(fpst);
1233 uint32_t exp = extract32(f, 23, 8);
1234
1235 if (unlikely(exp == 0xff)) {
1236
1237 goto overflow;
1238 }
1239
1240
1241 f = float32_round_to_int(f, fpst);
1242 exp = extract32(f, 23, 8);
1243
1244
1245 if (exp < 126 + intsize) {
1246
1247 return f;
1248 }
1249 if (exp == 126 + intsize) {
1250 uint32_t sign = extract32(f, 31, 1);
1251 uint32_t frac = extract32(f, 0, 23);
1252 if (sign && frac == 0) {
1253
1254 return f;
1255 }
1256 }
1257
1258 overflow:
1259
1260
1261
1262
1263 set_float_exception_flags(old_flags | float_flag_invalid, fpst);
1264 return (0x100u + 126u + intsize) << 23;
1265}
1266
1267float32 HELPER(frint32_s)(float32 f, void *fpst)
1268{
1269 return frint_s(f, fpst, 32);
1270}
1271
1272float32 HELPER(frint64_s)(float32 f, void *fpst)
1273{
1274 return frint_s(f, fpst, 64);
1275}
1276
1277
1278static float64 frint_d(float64 f, float_status *fpst, int intsize)
1279{
1280 int old_flags = get_float_exception_flags(fpst);
1281 uint32_t exp = extract64(f, 52, 11);
1282
1283 if (unlikely(exp == 0x7ff)) {
1284
1285 goto overflow;
1286 }
1287
1288
1289 f = float64_round_to_int(f, fpst);
1290 exp = extract64(f, 52, 11);
1291
1292
1293 if (exp < 1022 + intsize) {
1294
1295 return f;
1296 }
1297 if (exp == 1022 + intsize) {
1298 uint64_t sign = extract64(f, 63, 1);
1299 uint64_t frac = extract64(f, 0, 52);
1300 if (sign && frac == 0) {
1301
1302 return f;
1303 }
1304 }
1305
1306 overflow:
1307
1308
1309
1310
1311 set_float_exception_flags(old_flags | float_flag_invalid, fpst);
1312 return (uint64_t)(0x800 + 1022 + intsize) << 52;
1313}
1314
1315float64 HELPER(frint32_d)(float64 f, void *fpst)
1316{
1317 return frint_d(f, fpst, 32);
1318}
1319
1320float64 HELPER(frint64_d)(float64 f, void *fpst)
1321{
1322 return frint_d(f, fpst, 64);
1323}
1324
1325#endif
1326