(root)/
gcc-13.2.0/
libgcc/
config/
rs6000/
_divkc3.c
       1  /* Copyright (C) 1989-2023 Free Software Foundation, Inc.
       2  
       3  This file is part of GCC.
       4  
       5  GCC is free software; you can redistribute it and/or modify it under
       6  the terms of the GNU General Public License as published by the Free
       7  Software Foundation; either version 3, or (at your option) any later
       8  version.
       9  
      10  GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11  WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13  for more details.
      14  
      15  Under Section 7 of GPL version 3, you are granted additional
      16  permissions described in the GCC Runtime Library Exception, version
      17  3.1, as published by the Free Software Foundation.
      18  
      19  You should have received a copy of the GNU General Public License and
      20  a copy of the GCC Runtime Library Exception along with this program;
      21  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      22  <http://www.gnu.org/licenses/>.  */
      23  
      24  /* This is a temporary specialization of code from libgcc/libgcc2.c.  */
      25  
      26  #include "soft-fp.h"
      27  #include "quad-float128.h"
      28  
      29  /* Use the correct built-in function based on whether TFmode is _Float128 or
      30     long double.  See quad-float128.h for more details.  */
      31  #ifndef __LONG_DOUBLE_IEEE128__
      32  #define COPYSIGN(x,y) __builtin_copysignf128 (x, y)
      33  #define INFINITY __builtin_inff128 ()
      34  #define FABS __builtin_fabsf128
      35  
      36  #else
      37  #define COPYSIGN(x,y) __builtin_copysignl (x, y)
      38  #define INFINITY __builtin_infl ()
      39  #define FABS __builtin_fabsl
      40  #endif
      41  
      42  #define isnan __builtin_isnan
      43  #define isinf __builtin_isinf
      44  #define isfinite __builtin_isfinite
      45  
      46  #if defined(FLOAT128_HW_INSNS) && !defined(__divkc3)
      47  #define __divkc3 __divkc3_sw
      48  #endif
      49  
      50  #ifndef __LONG_DOUBLE_IEEE128__
      51  #define RBIG   (__LIBGCC_KF_MAX__ / 2)
      52  #define RMIN   (__LIBGCC_KF_MIN__)
      53  #define RMIN2  (__LIBGCC_KF_EPSILON__)
      54  #define RMINSCAL (1 / __LIBGCC_KF_EPSILON__)
      55  #define RMAX2  (RBIG * RMIN2)
      56  #else
      57  #define RBIG   (__LIBGCC_TF_MAX__ / 2)
      58  #define RMIN   (__LIBGCC_TF_MIN__)
      59  #define RMIN2  (__LIBGCC_TF_EPSILON__)
      60  #define RMINSCAL (1 / __LIBGCC_TF_EPSILON__)
      61  #define RMAX2  (RBIG * RMIN2)
      62  #endif
      63  
      64  TCtype
      65  __divkc3 (TFtype a, TFtype b, TFtype c, TFtype d)
      66  {
      67    TFtype denom, ratio, x, y;
      68    TCtype res;
      69  
      70    /* long double has significant potential underflow/overflow errors that
      71       can be greatly reduced with a limited number of tests and adjustments.
      72    */
      73  
      74    /* Scale by max(c,d) to reduce chances of denominator overflowing.  */
      75    if (FABS (c) < FABS (d))
      76      {
      77        /* Prevent underflow when denominator is near max representable.  */
      78        if (FABS (d) >= RBIG)
      79  	{
      80  	  a = a / 2;
      81  	  b = b / 2;
      82  	  c = c / 2;
      83  	  d = d / 2;
      84  	}
      85        /* Avoid overflow/underflow issues when c and d are small.
      86  	 Scaling up helps avoid some underflows.
      87  	 No new overflow possible since c&d < RMIN2.  */
      88        if (FABS (d) < RMIN2)
      89  	{
      90  	  a = a * RMINSCAL;
      91  	  b = b * RMINSCAL;
      92  	  c = c * RMINSCAL;
      93  	  d = d * RMINSCAL;
      94  	}
      95        else
      96  	{
      97  	  if (((FABS (a) < RMIN) && (FABS (b) < RMAX2) && (FABS (d) < RMAX2))
      98  	      || ((FABS (b) < RMIN) && (FABS (a) < RMAX2)
      99  		  && (FABS (d) < RMAX2)))
     100  	    {
     101  	      a = a * RMINSCAL;
     102  	      b = b * RMINSCAL;
     103  	      c = c * RMINSCAL;
     104  	      d = d * RMINSCAL;
     105  	    }
     106  	}
     107        ratio = c / d;
     108        denom = (c * ratio) + d;
     109        /* Choose alternate order of computation if ratio is subnormal.  */
     110        if (FABS (ratio) > RMIN)
     111  	{
     112  	  x = ((a * ratio) + b) / denom;
     113  	  y = ((b * ratio) - a) / denom;
     114  	}
     115        else
     116  	{
     117  	  x = ((c * (a / d)) + b) / denom;
     118  	  y = ((c * (b / d)) - a) / denom;
     119  	}
     120      }
     121    else
     122      {
     123        /* Prevent underflow when denominator is near max representable.  */
     124        if (FABS (c) >= RBIG)
     125  	{
     126  	  a = a / 2;
     127  	  b = b / 2;
     128  	  c = c / 2;
     129  	  d = d / 2;
     130  	}
     131        /* Avoid overflow/underflow issues when both c and d are small.
     132  	 Scaling up helps avoid some underflows.
     133  	 No new overflow possible since both c&d are less than RMIN2.  */
     134        if (FABS (c) < RMIN2)
     135  	{
     136  	  a = a * RMINSCAL;
     137  	  b = b * RMINSCAL;
     138  	  c = c * RMINSCAL;
     139  	  d = d * RMINSCAL;
     140  	}
     141        else
     142  	{
     143  	  if (((FABS (a) < RMIN) && (FABS (b) < RMAX2) && (FABS (c) < RMAX2))
     144  	      || ((FABS (b) < RMIN) && (FABS (a) < RMAX2)
     145  		  && (FABS (c) < RMAX2)))
     146  	    {
     147  	      a = a * RMINSCAL;
     148  	      b = b * RMINSCAL;
     149  	      c = c * RMINSCAL;
     150  	      d = d * RMINSCAL;
     151  	    }
     152  	}
     153        ratio = d / c;
     154        denom = (d * ratio) + c;
     155        /* Choose alternate order of computation if ratio is subnormal.  */
     156        if (FABS (ratio) > RMIN)
     157  	{
     158  	  x = ((b * ratio) + a) / denom;
     159  	  y = (b - (a * ratio)) / denom;
     160  	}
     161        else
     162  	{
     163  	  x = (a + (d * (b / c))) / denom;
     164  	  y = (b - (d * (a / c))) / denom;
     165  	}
     166      }
     167  
     168    /* Recover infinities and zeros that computed as NaN+iNaN; the only cases
     169       are nonzero/zero, infinite/finite, and finite/infinite.  */
     170    if (isnan (x) && isnan (y))
     171      {
     172        if (c == 0.0 && d == 0.0 && (!isnan (a) || !isnan (b)))
     173  	{
     174  	  x = COPYSIGN (INFINITY, c) * a;
     175  	  y = COPYSIGN (INFINITY, c) * b;
     176  	}
     177        else if ((isinf (a) || isinf (b)) && isfinite (c) && isfinite (d))
     178  	{
     179  	  a = COPYSIGN (isinf (a) ? 1 : 0, a);
     180  	  b = COPYSIGN (isinf (b) ? 1 : 0, b);
     181  	  x = INFINITY * (a * c + b * d);
     182  	  y = INFINITY * (b * c - a * d);
     183  	}
     184        else if ((isinf (c) || isinf (d)) && isfinite (a) && isfinite (b))
     185  	{
     186  	  c = COPYSIGN (isinf (c) ? 1 : 0, c);
     187  	  d = COPYSIGN (isinf (d) ? 1 : 0, d);
     188  	  x = 0.0 * (a * c + b * d);
     189  	  y = 0.0 * (b * c - a * d);
     190  	}
     191      }
     192  
     193    __real__ res = x;
     194    __imag__ res = y;
     195    return res;
     196  }