aboutsummaryrefslogtreecommitdiff
path: root/target/riscv/fpu_helper.c
blob: 91b1a56d10a7ab78cbde6951dd7e05d55d3b2b56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/*
 * RISC-V FPU Emulation Helpers for QEMU.
 *
 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2 or later, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "qemu/osdep.h"
#include "cpu.h"
#include "qemu/host-utils.h"
#include "exec/exec-all.h"
#include "exec/helper-proto.h"
#include "fpu/softfloat.h"
#include "internals.h"

target_ulong riscv_cpu_get_fflags(CPURISCVState *env)
{
    int soft = get_float_exception_flags(&env->fp_status);
    target_ulong hard = 0;

    hard |= (soft & float_flag_inexact) ? FPEXC_NX : 0;
    hard |= (soft & float_flag_underflow) ? FPEXC_UF : 0;
    hard |= (soft & float_flag_overflow) ? FPEXC_OF : 0;
    hard |= (soft & float_flag_divbyzero) ? FPEXC_DZ : 0;
    hard |= (soft & float_flag_invalid) ? FPEXC_NV : 0;

    return hard;
}

void riscv_cpu_set_fflags(CPURISCVState *env, target_ulong hard)
{
    int soft = 0;

    soft |= (hard & FPEXC_NX) ? float_flag_inexact : 0;
    soft |= (hard & FPEXC_UF) ? float_flag_underflow : 0;
    soft |= (hard & FPEXC_OF) ? float_flag_overflow : 0;
    soft |= (hard & FPEXC_DZ) ? float_flag_divbyzero : 0;
    soft |= (hard & FPEXC_NV) ? float_flag_invalid : 0;

    set_float_exception_flags(soft, &env->fp_status);
}

void helper_set_rounding_mode(CPURISCVState *env, uint32_t rm)
{
    int softrm;

    if (rm == RISCV_FRM_DYN) {
        rm = env->frm;
    }
    switch (rm) {
    case RISCV_FRM_RNE:
        softrm = float_round_nearest_even;
        break;
    case RISCV_FRM_RTZ:
        softrm = float_round_to_zero;
        break;
    case RISCV_FRM_RDN:
        softrm = float_round_down;
        break;
    case RISCV_FRM_RUP:
        softrm = float_round_up;
        break;
    case RISCV_FRM_RMM:
        softrm = float_round_ties_away;
        break;
    default:
        riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
    }

    set_float_rounding_mode(softrm, &env->fp_status);
}

void helper_set_rounding_mode_chkfrm(CPURISCVState *env, uint32_t rm)
{
    int softrm;

    /* Always validate frm, even if rm != DYN. */
    if (unlikely(env->frm >= 5)) {
        riscv_raise_exception(env, RISCV_EXCP_ILLEGAL_INST, GETPC());
    }
    if (rm == RISCV_FRM_DYN) {
        rm = env->frm;
    }
    switch (rm) {
    case RISCV_FRM_RNE:
        softrm = float_round_nearest_even;
        break;
    case RISCV_FRM_RTZ:
        softrm = float_round_to_zero;
        break;
    case RISCV_FRM_RDN:
        softrm = float_round_down;
        break;
    case RISCV_FRM_RUP:
        softrm = float_round_up;
        break;
    case RISCV_FRM_RMM:
        softrm = float_round_ties_away;
        break;
    case RISCV_FRM_ROD:
        softrm = float_round_to_odd;
        break;
    default:
        g_assert_not_reached();
    }

    set_float_rounding_mode(softrm, &env->fp_status);
}

static uint64_t do_fmadd_h(CPURISCVState *env, uint64_t rs1, uint64_t rs2,
                           uint64_t rs3, int flags)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    float16 frs2 = check_nanbox_h(env, rs2);
    float16 frs3 = check_nanbox_h(env, rs3);
    return nanbox_h(env, float16_muladd(frs1, frs2, frs3, flags,
                                        &env->fp_status));
}

static uint64_t do_fmadd_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2,
                           uint64_t rs3, int flags)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    float32 frs2 = check_nanbox_s(env, rs2);
    float32 frs3 = check_nanbox_s(env, rs3);
    return nanbox_s(env, float32_muladd(frs1, frs2, frs3, flags,
                                        &env->fp_status));
}

uint64_t helper_fmadd_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2,
                        uint64_t frs3)
{
    return do_fmadd_s(env, frs1, frs2, frs3, 0);
}

uint64_t helper_fmadd_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2,
                        uint64_t frs3)
{
    return float64_muladd(frs1, frs2, frs3, 0, &env->fp_status);
}

uint64_t helper_fmadd_h(CPURISCVState *env, uint64_t frs1, uint64_t frs2,
                        uint64_t frs3)
{
    return do_fmadd_h(env, frs1, frs2, frs3, 0);
}

uint64_t helper_fmsub_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2,
                        uint64_t frs3)
{
    return do_fmadd_s(env, frs1, frs2, frs3, float_muladd_negate_c);
}

uint64_t helper_fmsub_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2,
                        uint64_t frs3)
{
    return float64_muladd(frs1, frs2, frs3, float_muladd_negate_c,
                          &env->fp_status);
}

uint64_t helper_fmsub_h(CPURISCVState *env, uint64_t frs1, uint64_t frs2,
                        uint64_t frs3)
{
    return do_fmadd_h(env, frs1, frs2, frs3, float_muladd_negate_c);
}

uint64_t helper_fnmsub_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2,
                         uint64_t frs3)
{
    return do_fmadd_s(env, frs1, frs2, frs3, float_muladd_negate_product);
}

uint64_t helper_fnmsub_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2,
                         uint64_t frs3)
{
    return float64_muladd(frs1, frs2, frs3, float_muladd_negate_product,
                          &env->fp_status);
}

uint64_t helper_fnmsub_h(CPURISCVState *env, uint64_t frs1, uint64_t frs2,
                         uint64_t frs3)
{
    return do_fmadd_h(env, frs1, frs2, frs3, float_muladd_negate_product);
}

uint64_t helper_fnmadd_s(CPURISCVState *env, uint64_t frs1, uint64_t frs2,
                         uint64_t frs3)
{
    return do_fmadd_s(env, frs1, frs2, frs3,
                      float_muladd_negate_c | float_muladd_negate_product);
}

uint64_t helper_fnmadd_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2,
                         uint64_t frs3)
{
    return float64_muladd(frs1, frs2, frs3, float_muladd_negate_c |
                          float_muladd_negate_product, &env->fp_status);
}

uint64_t helper_fnmadd_h(CPURISCVState *env, uint64_t frs1, uint64_t frs2,
                         uint64_t frs3)
{
    return do_fmadd_h(env, frs1, frs2, frs3,
                      float_muladd_negate_c | float_muladd_negate_product);
}

uint64_t helper_fadd_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    float32 frs2 = check_nanbox_s(env, rs2);
    return nanbox_s(env, float32_add(frs1, frs2, &env->fp_status));
}

uint64_t helper_fsub_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    float32 frs2 = check_nanbox_s(env, rs2);
    return nanbox_s(env, float32_sub(frs1, frs2, &env->fp_status));
}

uint64_t helper_fmul_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    float32 frs2 = check_nanbox_s(env, rs2);
    return nanbox_s(env, float32_mul(frs1, frs2, &env->fp_status));
}

uint64_t helper_fdiv_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    float32 frs2 = check_nanbox_s(env, rs2);
    return nanbox_s(env, float32_div(frs1, frs2, &env->fp_status));
}

uint64_t helper_fmin_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    float32 frs2 = check_nanbox_s(env, rs2);
    return nanbox_s(env, env->priv_ver < PRIV_VERSION_1_11_0 ?
                         float32_minnum(frs1, frs2, &env->fp_status) :
                         float32_minimum_number(frs1, frs2, &env->fp_status));
}

uint64_t helper_fminm_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    float32 frs2 = check_nanbox_s(env, rs2);
    float32 ret = float32_min(frs1, frs2, &env->fp_status);
    return nanbox_s(env, ret);
}

uint64_t helper_fmax_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    float32 frs2 = check_nanbox_s(env, rs2);
    return nanbox_s(env, env->priv_ver < PRIV_VERSION_1_11_0 ?
                         float32_maxnum(frs1, frs2, &env->fp_status) :
                         float32_maximum_number(frs1, frs2, &env->fp_status));
}

uint64_t helper_fmaxm_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    float32 frs2 = check_nanbox_s(env, rs2);
    float32 ret = float32_max(frs1, frs2, &env->fp_status);
    return nanbox_s(env, ret);
}

uint64_t helper_fsqrt_s(CPURISCVState *env, uint64_t rs1)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    return nanbox_s(env, float32_sqrt(frs1, &env->fp_status));
}

target_ulong helper_fle_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    float32 frs2 = check_nanbox_s(env, rs2);
    return float32_le(frs1, frs2, &env->fp_status);
}

target_ulong helper_fleq_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    float32 frs2 = check_nanbox_s(env, rs2);
    return float32_le_quiet(frs1, frs2, &env->fp_status);
}

target_ulong helper_flt_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    float32 frs2 = check_nanbox_s(env, rs2);
    return float32_lt(frs1, frs2, &env->fp_status);
}

target_ulong helper_fltq_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    float32 frs2 = check_nanbox_s(env, rs2);
    return float32_lt_quiet(frs1, frs2, &env->fp_status);
}

target_ulong helper_feq_s(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    float32 frs2 = check_nanbox_s(env, rs2);
    return float32_eq_quiet(frs1, frs2, &env->fp_status);
}

target_ulong helper_fcvt_w_s(CPURISCVState *env, uint64_t rs1)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    return float32_to_int32(frs1, &env->fp_status);
}

target_ulong helper_fcvt_wu_s(CPURISCVState *env, uint64_t rs1)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    return (int32_t)float32_to_uint32(frs1, &env->fp_status);
}

target_ulong helper_fcvt_l_s(CPURISCVState *env, uint64_t rs1)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    return float32_to_int64(frs1, &env->fp_status);
}

target_ulong helper_fcvt_lu_s(CPURISCVState *env, uint64_t rs1)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    return float32_to_uint64(frs1, &env->fp_status);
}

uint64_t helper_fcvt_s_w(CPURISCVState *env, target_ulong rs1)
{
    return nanbox_s(env, int32_to_float32((int32_t)rs1, &env->fp_status));
}

uint64_t helper_fcvt_s_wu(CPURISCVState *env, target_ulong rs1)
{
    return nanbox_s(env, uint32_to_float32((uint32_t)rs1, &env->fp_status));
}

uint64_t helper_fcvt_s_l(CPURISCVState *env, target_ulong rs1)
{
    return nanbox_s(env, int64_to_float32(rs1, &env->fp_status));
}

uint64_t helper_fcvt_s_lu(CPURISCVState *env, target_ulong rs1)
{
    return nanbox_s(env, uint64_to_float32(rs1, &env->fp_status));
}

target_ulong helper_fclass_s(CPURISCVState *env, uint64_t rs1)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    return fclass_s(frs1);
}

uint64_t helper_fround_s(CPURISCVState *env, uint64_t rs1)
{
    float_status *fs = &env->fp_status;
    uint16_t nx_old = get_float_exception_flags(fs) & float_flag_inexact;
    float32 frs1 = check_nanbox_s(env, rs1);

    frs1 = float32_round_to_int(frs1, fs);

    /* Restore the original NX flag. */
    uint16_t flags = get_float_exception_flags(fs);
    flags &= ~float_flag_inexact;
    flags |= nx_old;
    set_float_exception_flags(flags, fs);

    return nanbox_s(env, frs1);
}

uint64_t helper_froundnx_s(CPURISCVState *env, uint64_t rs1)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    frs1 = float32_round_to_int(frs1, &env->fp_status);
    return nanbox_s(env, frs1);
}

uint64_t helper_fadd_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2)
{
    return float64_add(frs1, frs2, &env->fp_status);
}

uint64_t helper_fsub_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2)
{
    return float64_sub(frs1, frs2, &env->fp_status);
}

uint64_t helper_fmul_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2)
{
    return float64_mul(frs1, frs2, &env->fp_status);
}

uint64_t helper_fdiv_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2)
{
    return float64_div(frs1, frs2, &env->fp_status);
}

uint64_t helper_fmin_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2)
{
    return env->priv_ver < PRIV_VERSION_1_11_0 ?
           float64_minnum(frs1, frs2, &env->fp_status) :
           float64_minimum_number(frs1, frs2, &env->fp_status);
}

uint64_t helper_fminm_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2)
{
    return float64_min(frs1, frs2, &env->fp_status);
}

uint64_t helper_fmax_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2)
{
    return env->priv_ver < PRIV_VERSION_1_11_0 ?
           float64_maxnum(frs1, frs2, &env->fp_status) :
           float64_maximum_number(frs1, frs2, &env->fp_status);
}

uint64_t helper_fmaxm_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2)
{
    return float64_max(frs1, frs2, &env->fp_status);
}

uint64_t helper_fcvt_s_d(CPURISCVState *env, uint64_t rs1)
{
    return nanbox_s(env, float64_to_float32(rs1, &env->fp_status));
}

uint64_t helper_fcvt_d_s(CPURISCVState *env, uint64_t rs1)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    return float32_to_float64(frs1, &env->fp_status);
}

uint64_t helper_fsqrt_d(CPURISCVState *env, uint64_t frs1)
{
    return float64_sqrt(frs1, &env->fp_status);
}

target_ulong helper_fle_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2)
{
    return float64_le(frs1, frs2, &env->fp_status);
}

target_ulong helper_fleq_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2)
{
    return float64_le_quiet(frs1, frs2, &env->fp_status);
}

target_ulong helper_flt_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2)
{
    return float64_lt(frs1, frs2, &env->fp_status);
}

target_ulong helper_fltq_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2)
{
    return float64_lt_quiet(frs1, frs2, &env->fp_status);
}

target_ulong helper_feq_d(CPURISCVState *env, uint64_t frs1, uint64_t frs2)
{
    return float64_eq_quiet(frs1, frs2, &env->fp_status);
}

target_ulong helper_fcvt_w_d(CPURISCVState *env, uint64_t frs1)
{
    return float64_to_int32(frs1, &env->fp_status);
}

uint64_t helper_fcvtmod_w_d(CPURISCVState *env, uint64_t value)
{
    return float64_to_int32_modulo(value, float_round_to_zero, &env->fp_status);
}

target_ulong helper_fcvt_wu_d(CPURISCVState *env, uint64_t frs1)
{
    return (int32_t)float64_to_uint32(frs1, &env->fp_status);
}

target_ulong helper_fcvt_l_d(CPURISCVState *env, uint64_t frs1)
{
    return float64_to_int64(frs1, &env->fp_status);
}

target_ulong helper_fcvt_lu_d(CPURISCVState *env, uint64_t frs1)
{
    return float64_to_uint64(frs1, &env->fp_status);
}

uint64_t helper_fcvt_d_w(CPURISCVState *env, target_ulong rs1)
{
    return int32_to_float64((int32_t)rs1, &env->fp_status);
}

uint64_t helper_fcvt_d_wu(CPURISCVState *env, target_ulong rs1)
{
    return uint32_to_float64((uint32_t)rs1, &env->fp_status);
}

uint64_t helper_fcvt_d_l(CPURISCVState *env, target_ulong rs1)
{
    return int64_to_float64(rs1, &env->fp_status);
}

uint64_t helper_fcvt_d_lu(CPURISCVState *env, target_ulong rs1)
{
    return uint64_to_float64(rs1, &env->fp_status);
}

target_ulong helper_fclass_d(uint64_t frs1)
{
    return fclass_d(frs1);
}

uint64_t helper_fround_d(CPURISCVState *env, uint64_t frs1)
{
    float_status *fs = &env->fp_status;
    uint16_t nx_old = get_float_exception_flags(fs) & float_flag_inexact;

    frs1 = float64_round_to_int(frs1, fs);

    /* Restore the original NX flag. */
    uint16_t flags = get_float_exception_flags(fs);
    flags &= ~float_flag_inexact;
    flags |= nx_old;
    set_float_exception_flags(flags, fs);

    return frs1;
}

uint64_t helper_froundnx_d(CPURISCVState *env, uint64_t frs1)
{
    return float64_round_to_int(frs1, &env->fp_status);
}

uint64_t helper_fadd_h(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    float16 frs2 = check_nanbox_h(env, rs2);
    return nanbox_h(env, float16_add(frs1, frs2, &env->fp_status));
}

uint64_t helper_fsub_h(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    float16 frs2 = check_nanbox_h(env, rs2);
    return nanbox_h(env, float16_sub(frs1, frs2, &env->fp_status));
}

uint64_t helper_fmul_h(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    float16 frs2 = check_nanbox_h(env, rs2);
    return nanbox_h(env, float16_mul(frs1, frs2, &env->fp_status));
}

uint64_t helper_fdiv_h(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    float16 frs2 = check_nanbox_h(env, rs2);
    return nanbox_h(env, float16_div(frs1, frs2, &env->fp_status));
}

uint64_t helper_fmin_h(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    float16 frs2 = check_nanbox_h(env, rs2);
    return nanbox_h(env, env->priv_ver < PRIV_VERSION_1_11_0 ?
                         float16_minnum(frs1, frs2, &env->fp_status) :
                         float16_minimum_number(frs1, frs2, &env->fp_status));
}

uint64_t helper_fminm_h(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    float16 frs2 = check_nanbox_h(env, rs2);
    float16 ret = float16_min(frs1, frs2, &env->fp_status);
    return nanbox_h(env, ret);
}

uint64_t helper_fmax_h(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    float16 frs2 = check_nanbox_h(env, rs2);
    return nanbox_h(env, env->priv_ver < PRIV_VERSION_1_11_0 ?
                         float16_maxnum(frs1, frs2, &env->fp_status) :
                         float16_maximum_number(frs1, frs2, &env->fp_status));
}

uint64_t helper_fmaxm_h(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    float16 frs2 = check_nanbox_h(env, rs2);
    float16 ret = float16_max(frs1, frs2, &env->fp_status);
    return nanbox_h(env, ret);
}

uint64_t helper_fsqrt_h(CPURISCVState *env, uint64_t rs1)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    return nanbox_h(env, float16_sqrt(frs1, &env->fp_status));
}

target_ulong helper_fle_h(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    float16 frs2 = check_nanbox_h(env, rs2);
    return float16_le(frs1, frs2, &env->fp_status);
}

target_ulong helper_fleq_h(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    float16 frs2 = check_nanbox_h(env, rs2);
    return float16_le_quiet(frs1, frs2, &env->fp_status);
}

target_ulong helper_flt_h(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    float16 frs2 = check_nanbox_h(env, rs2);
    return float16_lt(frs1, frs2, &env->fp_status);
}

target_ulong helper_fltq_h(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    float16 frs2 = check_nanbox_h(env, rs2);
    return float16_lt_quiet(frs1, frs2, &env->fp_status);
}

target_ulong helper_feq_h(CPURISCVState *env, uint64_t rs1, uint64_t rs2)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    float16 frs2 = check_nanbox_h(env, rs2);
    return float16_eq_quiet(frs1, frs2, &env->fp_status);
}

target_ulong helper_fclass_h(CPURISCVState *env, uint64_t rs1)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    return fclass_h(frs1);
}

uint64_t helper_fround_h(CPURISCVState *env, uint64_t rs1)
{
    float_status *fs = &env->fp_status;
    uint16_t nx_old = get_float_exception_flags(fs) & float_flag_inexact;
    float16 frs1 = check_nanbox_h(env, rs1);

    frs1 = float16_round_to_int(frs1, fs);

    /* Restore the original NX flag. */
    uint16_t flags = get_float_exception_flags(fs);
    flags &= ~float_flag_inexact;
    flags |= nx_old;
    set_float_exception_flags(flags, fs);

    return nanbox_h(env, frs1);
}

uint64_t helper_froundnx_h(CPURISCVState *env, uint64_t rs1)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    frs1 = float16_round_to_int(frs1, &env->fp_status);
    return nanbox_h(env, frs1);
}

target_ulong helper_fcvt_w_h(CPURISCVState *env, uint64_t rs1)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    return float16_to_int32(frs1, &env->fp_status);
}

target_ulong helper_fcvt_wu_h(CPURISCVState *env, uint64_t rs1)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    return (int32_t)float16_to_uint32(frs1, &env->fp_status);
}

target_ulong helper_fcvt_l_h(CPURISCVState *env, uint64_t rs1)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    return float16_to_int64(frs1, &env->fp_status);
}

target_ulong helper_fcvt_lu_h(CPURISCVState *env, uint64_t rs1)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    return float16_to_uint64(frs1, &env->fp_status);
}

uint64_t helper_fcvt_h_w(CPURISCVState *env, target_ulong rs1)
{
    return nanbox_h(env, int32_to_float16((int32_t)rs1, &env->fp_status));
}

uint64_t helper_fcvt_h_wu(CPURISCVState *env, target_ulong rs1)
{
    return nanbox_h(env, uint32_to_float16((uint32_t)rs1, &env->fp_status));
}

uint64_t helper_fcvt_h_l(CPURISCVState *env, target_ulong rs1)
{
    return nanbox_h(env, int64_to_float16(rs1, &env->fp_status));
}

uint64_t helper_fcvt_h_lu(CPURISCVState *env, target_ulong rs1)
{
    return nanbox_h(env, uint64_to_float16(rs1, &env->fp_status));
}

uint64_t helper_fcvt_h_s(CPURISCVState *env, uint64_t rs1)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    return nanbox_h(env, float32_to_float16(frs1, true, &env->fp_status));
}

uint64_t helper_fcvt_s_h(CPURISCVState *env, uint64_t rs1)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    return nanbox_s(env, float16_to_float32(frs1, true, &env->fp_status));
}

uint64_t helper_fcvt_h_d(CPURISCVState *env, uint64_t rs1)
{
    return nanbox_h(env, float64_to_float16(rs1, true, &env->fp_status));
}

uint64_t helper_fcvt_d_h(CPURISCVState *env, uint64_t rs1)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    return float16_to_float64(frs1, true, &env->fp_status);
}

uint64_t helper_fcvt_bf16_s(CPURISCVState *env, uint64_t rs1)
{
    float32 frs1 = check_nanbox_s(env, rs1);
    return nanbox_h(env, float32_to_bfloat16(frs1, &env->fp_status));
}

uint64_t helper_fcvt_s_bf16(CPURISCVState *env, uint64_t rs1)
{
    float16 frs1 = check_nanbox_h(env, rs1);
    return nanbox_s(env, bfloat16_to_float32(frs1, &env->fp_status));
}