aboutsummaryrefslogtreecommitdiff
path: root/target/loongarch/fpu_helper.c
blob: 81466678eb7a652ef8babe741540e59ba22fcb80 (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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
 * LoongArch float point emulation helpers for QEMU
 *
 * Copyright (c) 2021 Loongson Technology Corporation Limited
 */

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

#define FLOAT_TO_INT32_OVERFLOW 0x7fffffff
#define FLOAT_TO_INT64_OVERFLOW 0x7fffffffffffffffULL

static inline uint64_t nanbox_s(float32 fp)
{
    return fp | MAKE_64BIT_MASK(32, 32);
}

/* Convert loongarch rounding mode in fcsr0 to IEEE library */
static const FloatRoundMode ieee_rm[4] = {
    float_round_nearest_even,
    float_round_to_zero,
    float_round_up,
    float_round_down
};

void restore_fp_status(CPULoongArchState *env)
{
    set_float_rounding_mode(ieee_rm[(env->fcsr0 >> FCSR0_RM) & 0x3],
                            &env->fp_status);
    set_flush_to_zero(0, &env->fp_status);
}

static int ieee_ex_to_loongarch(int xcpt)
{
    int ret = 0;
    if (xcpt & float_flag_invalid) {
        ret |= FP_INVALID;
    }
    if (xcpt & float_flag_overflow) {
        ret |= FP_OVERFLOW;
    }
    if (xcpt & float_flag_underflow) {
        ret |= FP_UNDERFLOW;
    }
    if (xcpt & float_flag_divbyzero) {
        ret |= FP_DIV0;
    }
    if (xcpt & float_flag_inexact) {
        ret |= FP_INEXACT;
    }
    return ret;
}

static void update_fcsr0_mask(CPULoongArchState *env, uintptr_t pc, int mask)
{
    int flags = get_float_exception_flags(&env->fp_status);

    set_float_exception_flags(0, &env->fp_status);

    flags &= ~mask;

    if (!flags) {
        SET_FP_CAUSE(env->fcsr0, flags);
        return;
    } else {
        flags = ieee_ex_to_loongarch(flags);
        SET_FP_CAUSE(env->fcsr0, flags);
    }

    if (GET_FP_ENABLES(env->fcsr0) & flags) {
        do_raise_exception(env, EXCCODE_FPE, pc);
    } else {
        UPDATE_FP_FLAGS(env->fcsr0, flags);
    }
}

static void update_fcsr0(CPULoongArchState *env, uintptr_t pc)
{
    update_fcsr0_mask(env, pc, 0);
}

uint64_t helper_fadd_s(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;

    fd = nanbox_s(float32_add((uint32_t)fj, (uint32_t)fk, &env->fp_status));
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fadd_d(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;

    fd = float64_add(fj, fk, &env->fp_status);
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fsub_s(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;

    fd = nanbox_s(float32_sub((uint32_t)fj, (uint32_t)fk, &env->fp_status));
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fsub_d(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;

    fd = float64_sub(fj, fk, &env->fp_status);
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fmul_s(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;

    fd = nanbox_s(float32_mul((uint32_t)fj, (uint32_t)fk, &env->fp_status));
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fmul_d(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;

    fd = float64_mul(fj, fk, &env->fp_status);
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fdiv_s(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;

    fd = nanbox_s(float32_div((uint32_t)fj, (uint32_t)fk, &env->fp_status));
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fdiv_d(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;

    fd = float64_div(fj, fk, &env->fp_status);
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fmax_s(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;

    fd = nanbox_s(float32_maxnum((uint32_t)fj, (uint32_t)fk, &env->fp_status));
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fmax_d(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;

    fd = float64_maxnum(fj, fk, &env->fp_status);
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fmin_s(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;

    fd = nanbox_s(float32_minnum((uint32_t)fj, (uint32_t)fk, &env->fp_status));
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fmin_d(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;

    fd = float64_minnum(fj, fk, &env->fp_status);
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fmaxa_s(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;

    fd = nanbox_s(float32_maxnummag((uint32_t)fj,
                                    (uint32_t)fk, &env->fp_status));
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fmaxa_d(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;

    fd = float64_maxnummag(fj, fk, &env->fp_status);
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fmina_s(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;

    fd = nanbox_s(float32_minnummag((uint32_t)fj,
                                    (uint32_t)fk, &env->fp_status));
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fmina_d(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;

    fd = float64_minnummag(fj, fk, &env->fp_status);
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fscaleb_s(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;
    int32_t n = (int32_t)fk;

    fd = nanbox_s(float32_scalbn((uint32_t)fj,
                                 n >  0x200 ?  0x200 :
                                 n < -0x200 ? -0x200 : n,
                                 &env->fp_status));
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fscaleb_d(CPULoongArchState *env, uint64_t fj, uint64_t fk)
{
    uint64_t fd;
    int64_t n = (int64_t)fk;

    fd = float64_scalbn(fj,
                        n >  0x1000 ?  0x1000 :
                        n < -0x1000 ? -0x1000 : n,
                        &env->fp_status);
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fsqrt_s(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;

    fd = nanbox_s(float32_sqrt((uint32_t)fj, &env->fp_status));
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fsqrt_d(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;

    fd = float64_sqrt(fj, &env->fp_status);
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_frecip_s(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;

    fd = nanbox_s(float32_div(float32_one, (uint32_t)fj, &env->fp_status));
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_frecip_d(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;

    fd = float64_div(float64_one, fj, &env->fp_status);
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_frsqrt_s(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;
    uint32_t fp;

    fp = float32_sqrt((uint32_t)fj, &env->fp_status);
    fd = nanbox_s(float32_div(float32_one, fp, &env->fp_status));
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_frsqrt_d(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fp, fd;

    fp = float64_sqrt(fj, &env->fp_status);
    fd = float64_div(float64_one, fp, &env->fp_status);
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_flogb_s(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;
    uint32_t fp;
    float_status *status = &env->fp_status;
    FloatRoundMode old_mode = get_float_rounding_mode(status);

    set_float_rounding_mode(float_round_down, status);
    fp = float32_log2((uint32_t)fj, status);
    fd = nanbox_s(float32_round_to_int(fp, status));
    set_float_rounding_mode(old_mode, status);
    update_fcsr0_mask(env, GETPC(), float_flag_inexact);
    return fd;
}

uint64_t helper_flogb_d(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;
    float_status *status = &env->fp_status;
    FloatRoundMode old_mode = get_float_rounding_mode(status);

    set_float_rounding_mode(float_round_down, status);
    fd = float64_log2(fj, status);
    fd = float64_round_to_int(fd, status);
    set_float_rounding_mode(old_mode, status);
    update_fcsr0_mask(env, GETPC(), float_flag_inexact);
    return fd;
}

uint64_t helper_fclass_s(CPULoongArchState *env, uint64_t fj)
{
    float32 f = fj;
    bool sign = float32_is_neg(f);

    if (float32_is_infinity(f)) {
        return sign ? 1 << 2 : 1 << 6;
    } else if (float32_is_zero(f)) {
        return sign ? 1 << 5 : 1 << 9;
    } else if (float32_is_zero_or_denormal(f)) {
        return sign ? 1 << 4 : 1 << 8;
    } else if (float32_is_any_nan(f)) {
        float_status s = { }; /* for snan_bit_is_one */
        return float32_is_quiet_nan(f, &s) ? 1 << 1 : 1 << 0;
    } else {
        return sign ? 1 << 3 : 1 << 7;
    }
}

uint64_t helper_fclass_d(CPULoongArchState *env, uint64_t fj)
{
    float64 f = fj;
    bool sign = float64_is_neg(f);

    if (float64_is_infinity(f)) {
        return sign ? 1 << 2 : 1 << 6;
    } else if (float64_is_zero(f)) {
        return sign ? 1 << 5 : 1 << 9;
    } else if (float64_is_zero_or_denormal(f)) {
        return sign ? 1 << 4 : 1 << 8;
    } else if (float64_is_any_nan(f)) {
        float_status s = { }; /* for snan_bit_is_one */
        return float64_is_quiet_nan(f, &s) ? 1 << 1 : 1 << 0;
    } else {
        return sign ? 1 << 3 : 1 << 7;
    }
}

uint64_t helper_fmuladd_s(CPULoongArchState *env, uint64_t fj,
                          uint64_t fk, uint64_t fa, uint32_t flag)
{
    uint64_t fd;

    fd = nanbox_s(float32_muladd((uint32_t)fj, (uint32_t)fk,
                                 (uint32_t)fa, flag, &env->fp_status));
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fmuladd_d(CPULoongArchState *env, uint64_t fj,
                          uint64_t fk, uint64_t fa, uint32_t flag)
{
    uint64_t fd;

    fd = float64_muladd(fj, fk, fa, flag, &env->fp_status);
    update_fcsr0(env, GETPC());
    return fd;
}

static uint64_t fcmp_common(CPULoongArchState *env, FloatRelation cmp,
                            uint32_t flags)
{
    bool ret;

    switch (cmp) {
    case float_relation_less:
        ret = (flags & FCMP_LT);
        break;
    case float_relation_equal:
        ret = (flags & FCMP_EQ);
        break;
    case float_relation_greater:
        ret = (flags & FCMP_GT);
        break;
    case float_relation_unordered:
        ret = (flags & FCMP_UN);
        break;
    default:
        g_assert_not_reached();
    }
    update_fcsr0(env, GETPC());

    return ret;
}

/* fcmp_cXXX_s */
uint64_t helper_fcmp_c_s(CPULoongArchState *env, uint64_t fj,
                         uint64_t fk, uint32_t flags)
{
    FloatRelation cmp = float32_compare_quiet((uint32_t)fj,
                                              (uint32_t)fk, &env->fp_status);
    return fcmp_common(env, cmp, flags);
}

/* fcmp_sXXX_s */
uint64_t helper_fcmp_s_s(CPULoongArchState *env, uint64_t fj,
                         uint64_t fk, uint32_t flags)
{
    FloatRelation cmp = float32_compare((uint32_t)fj,
                                        (uint32_t)fk, &env->fp_status);
    return fcmp_common(env, cmp, flags);
}

/* fcmp_cXXX_d */
uint64_t helper_fcmp_c_d(CPULoongArchState *env, uint64_t fj,
                         uint64_t fk, uint32_t flags)
{
    FloatRelation cmp = float64_compare_quiet(fj, fk, &env->fp_status);
    return fcmp_common(env, cmp, flags);
}

/* fcmp_sXXX_d */
uint64_t helper_fcmp_s_d(CPULoongArchState *env, uint64_t fj,
                         uint64_t fk, uint32_t flags)
{
    FloatRelation cmp = float64_compare(fj, fk, &env->fp_status);
    return fcmp_common(env, cmp, flags);
}

/* floating point conversion */
uint64_t helper_fcvt_s_d(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;

    fd = nanbox_s(float64_to_float32(fj, &env->fp_status));
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_fcvt_d_s(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;

    fd = float32_to_float64((uint32_t)fj, &env->fp_status);
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ffint_s_w(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;

    fd = nanbox_s(int32_to_float32((int32_t)fj, &env->fp_status));
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ffint_s_l(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;

    fd = nanbox_s(int64_to_float32(fj, &env->fp_status));
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ffint_d_w(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;

    fd = int32_to_float64((int32_t)fj, &env->fp_status);
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ffint_d_l(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;

    fd = int64_to_float64(fj, &env->fp_status);
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_frint_s(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;

    fd = (uint64_t)(float32_round_to_int((uint32_t)fj, &env->fp_status));
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_frint_d(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;

    fd = float64_round_to_int(fj, &env->fp_status);
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftintrm_l_d(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;
    FloatRoundMode old_mode = get_float_rounding_mode(&env->fp_status);

    set_float_rounding_mode(float_round_down, &env->fp_status);
    fd = float64_to_int64(fj, &env->fp_status);
    set_float_rounding_mode(old_mode, &env->fp_status);

    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT64_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftintrm_l_s(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;
    FloatRoundMode old_mode = get_float_rounding_mode(&env->fp_status);

    set_float_rounding_mode(float_round_down, &env->fp_status);
    fd = float32_to_int64((uint32_t)fj, &env->fp_status);
    set_float_rounding_mode(old_mode, &env->fp_status);

    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT64_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftintrm_w_d(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;
    FloatRoundMode old_mode = get_float_rounding_mode(&env->fp_status);

    set_float_rounding_mode(float_round_down, &env->fp_status);
    fd = (uint64_t)float64_to_int32(fj, &env->fp_status);
    set_float_rounding_mode(old_mode, &env->fp_status);

    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT32_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftintrm_w_s(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;
    FloatRoundMode old_mode = get_float_rounding_mode(&env->fp_status);

    set_float_rounding_mode(float_round_down, &env->fp_status);
    fd = (uint64_t)float32_to_int32((uint32_t)fj, &env->fp_status);
    set_float_rounding_mode(old_mode, &env->fp_status);

    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT32_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftintrp_l_d(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;
    FloatRoundMode old_mode = get_float_rounding_mode(&env->fp_status);

    set_float_rounding_mode(float_round_up, &env->fp_status);
    fd = float64_to_int64(fj, &env->fp_status);
    set_float_rounding_mode(old_mode, &env->fp_status);

    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT64_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftintrp_l_s(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;
    FloatRoundMode old_mode = get_float_rounding_mode(&env->fp_status);

    set_float_rounding_mode(float_round_up, &env->fp_status);
    fd = float32_to_int64((uint32_t)fj, &env->fp_status);
    set_float_rounding_mode(old_mode, &env->fp_status);

    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT64_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftintrp_w_d(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;
    FloatRoundMode old_mode = get_float_rounding_mode(&env->fp_status);

    set_float_rounding_mode(float_round_up, &env->fp_status);
    fd = (uint64_t)float64_to_int32(fj, &env->fp_status);
    set_float_rounding_mode(old_mode, &env->fp_status);

    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT32_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftintrp_w_s(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;
    FloatRoundMode old_mode = get_float_rounding_mode(&env->fp_status);

    set_float_rounding_mode(float_round_up, &env->fp_status);
    fd = (uint64_t)float32_to_int32((uint32_t)fj, &env->fp_status);
    set_float_rounding_mode(old_mode, &env->fp_status);

    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT32_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftintrz_l_d(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;
    FloatRoundMode old_mode = get_float_rounding_mode(&env->fp_status);

    fd = float64_to_int64_round_to_zero(fj, &env->fp_status);
    set_float_rounding_mode(old_mode, &env->fp_status);

    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT64_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftintrz_l_s(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;
    FloatRoundMode old_mode = get_float_rounding_mode(&env->fp_status);

    fd = float32_to_int64_round_to_zero((uint32_t)fj, &env->fp_status);
    set_float_rounding_mode(old_mode, &env->fp_status);

    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT64_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftintrz_w_d(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;
    FloatRoundMode old_mode = get_float_rounding_mode(&env->fp_status);

    fd = (uint64_t)float64_to_int32_round_to_zero(fj, &env->fp_status);
    set_float_rounding_mode(old_mode, &env->fp_status);

    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT32_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftintrz_w_s(CPULoongArchState *env, uint64_t fj)
{
    uint32_t fd;
    FloatRoundMode old_mode = get_float_rounding_mode(&env->fp_status);

    fd = float32_to_int32_round_to_zero((uint32_t)fj, &env->fp_status);
    set_float_rounding_mode(old_mode, &env->fp_status);

    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT32_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return (uint64_t)fd;
}

uint64_t helper_ftintrne_l_d(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;
    FloatRoundMode old_mode = get_float_rounding_mode(&env->fp_status);

    set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
    fd = float64_to_int64(fj, &env->fp_status);
    set_float_rounding_mode(old_mode, &env->fp_status);

    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT64_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftintrne_l_s(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;
    FloatRoundMode old_mode = get_float_rounding_mode(&env->fp_status);

    set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
    fd = float32_to_int64((uint32_t)fj, &env->fp_status);
    set_float_rounding_mode(old_mode, &env->fp_status);

    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT64_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftintrne_w_d(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;
    FloatRoundMode old_mode = get_float_rounding_mode(&env->fp_status);

    set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
    fd = (uint64_t)float64_to_int32(fj, &env->fp_status);
    set_float_rounding_mode(old_mode, &env->fp_status);

    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT32_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftintrne_w_s(CPULoongArchState *env, uint64_t fj)
{
    uint32_t fd;
    FloatRoundMode old_mode = get_float_rounding_mode(&env->fp_status);

    set_float_rounding_mode(float_round_nearest_even, &env->fp_status);
    fd = float32_to_int32((uint32_t)fj, &env->fp_status);
    set_float_rounding_mode(old_mode, &env->fp_status);

    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT32_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return (uint64_t)fd;
}

uint64_t helper_ftint_l_d(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;

    fd = float64_to_int64(fj, &env->fp_status);
    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT64_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftint_l_s(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;

    fd = float32_to_int64((uint32_t)fj, &env->fp_status);
    if (get_float_exception_flags(&env->fp_status) &
        (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT64_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftint_w_s(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;

    fd = (uint64_t)float32_to_int32((uint32_t)fj, &env->fp_status);
    if (get_float_exception_flags(&env->fp_status)
        & (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT32_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}

uint64_t helper_ftint_w_d(CPULoongArchState *env, uint64_t fj)
{
    uint64_t fd;

    fd = (uint64_t)float64_to_int32(fj, &env->fp_status);
    if (get_float_exception_flags(&env->fp_status)
        & (float_flag_invalid | float_flag_overflow)) {
        fd = FLOAT_TO_INT32_OVERFLOW;
    }
    update_fcsr0(env, GETPC());
    return fd;
}