1  /* LoongArch softfloat definitions
       2     Copyright (C) 2022-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5  
       6     The GNU C 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.
      10  
      11     The GNU C 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.
      15  
      16     You should have received a copy of the GNU Lesser General Public
      17     License along with the GNU C Library.  If not, see
      18     <https://www.gnu.org/licenses/>.  */
      19  
      20  #include <fenv.h>
      21  #include <fpu_control.h>
      22  
      23  #define _FP_W_TYPE_SIZE 64
      24  #define _FP_W_TYPE unsigned long long
      25  #define _FP_WS_TYPE signed long long
      26  #define _FP_I_TYPE long long
      27  
      28  #define _FP_MUL_MEAT_S(R, X, Y) _FP_MUL_MEAT_1_imm (_FP_WFRACBITS_S, R, X, Y)
      29  #define _FP_MUL_MEAT_D(R, X, Y) \
      30    _FP_MUL_MEAT_1_wide (_FP_WFRACBITS_D, R, X, Y, umul_ppmm)
      31  #define _FP_MUL_MEAT_Q(R, X, Y) \
      32    _FP_MUL_MEAT_2_wide_3mul (_FP_WFRACBITS_Q, R, X, Y, umul_ppmm)
      33  
      34  #define _FP_MUL_MEAT_DW_S(R, X, Y) \
      35    _FP_MUL_MEAT_DW_1_imm (_FP_WFRACBITS_S, R, X, Y)
      36  #define _FP_MUL_MEAT_DW_D(R, X, Y) \
      37    _FP_MUL_MEAT_DW_1_wide (_FP_WFRACBITS_D, R, X, Y, umul_ppmm)
      38  #define _FP_MUL_MEAT_DW_Q(R, X, Y) \
      39    _FP_MUL_MEAT_DW_2_wide_3mul (_FP_WFRACBITS_Q, R, X, Y, umul_ppmm)
      40  
      41  #define _FP_DIV_MEAT_S(R, X, Y) \
      42    _FP_DIV_MEAT_1_imm (S, R, X, Y, _FP_DIV_HELP_imm)
      43  #define _FP_DIV_MEAT_D(R, X, Y) _FP_DIV_MEAT_1_udiv_norm (D, R, X, Y)
      44  #define _FP_DIV_MEAT_Q(R, X, Y) _FP_DIV_MEAT_2_udiv (Q, R, X, Y)
      45  
      46  #define _FP_NANFRAC_S _FP_QNANBIT_S
      47  #define _FP_NANFRAC_D _FP_QNANBIT_D
      48  #define _FP_NANFRAC_Q _FP_QNANBIT_Q, 0
      49  
      50  #define _FP_NANSIGN_S 0
      51  #define _FP_NANSIGN_D 0
      52  #define _FP_NANSIGN_Q 0
      53  
      54  #define _FP_KEEPNANFRACP 1
      55  #define _FP_QNANNEGATEDP 0
      56  
      57  /* NaN payloads should be preserved for NAN2008.  */
      58  #define _FP_CHOOSENAN(fs, wc, R, X, Y, OP) \
      59    do \
      60      { \
      61        R##_s = X##_s; \
      62        _FP_FRAC_COPY_##wc (R, X); \
      63        R##_c = FP_CLS_NAN; \
      64      } \
      65    while (0)
      66  
      67  #define _FP_DECL_EX fpu_control_t _fcw
      68  
      69  #define FP_ROUNDMODE (_fcw & 0x300)
      70  
      71  #define FP_RND_NEAREST FE_TONEAREST
      72  #define FP_RND_ZERO FE_TOWARDZERO
      73  #define FP_RND_PINF FE_UPWARD
      74  #define FP_RND_MINF FE_DOWNWARD
      75  
      76  #define FP_EX_INVALID FE_INVALID
      77  #define FP_EX_OVERFLOW FE_OVERFLOW
      78  #define FP_EX_UNDERFLOW FE_UNDERFLOW
      79  #define FP_EX_DIVZERO FE_DIVBYZERO
      80  #define FP_EX_INEXACT FE_INEXACT
      81  
      82  #define _FP_TININESS_AFTER_ROUNDING 1
      83  
      84  #ifdef __loongarch_hard_float
      85  #define FP_INIT_ROUNDMODE \
      86    do \
      87      { \
      88        _FPU_GETCW (_fcw); \
      89      } \
      90    while (0)
      91  
      92  #define FP_HANDLE_EXCEPTIONS \
      93    do \
      94      { \
      95        if (__glibc_unlikely (_fex)) \
      96  	_FPU_SETCW (_fcw | _fex | (_fex << 8)); \
      97      } \
      98    while (0)
      99  #define FP_TRAPPING_EXCEPTIONS ((_fcw << 16) & 0x1f0000)
     100  #else
     101  #define FP_INIT_ROUNDMODE _fcw = FP_RND_NEAREST
     102  #endif