1/* Copyright (C) 1989-2020 Free Software Foundation, Inc.
2
3This file is part of GCC.
4
5GCC is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License as published by the Free
7Software Foundation; either version 3, or (at your option) any later
8version.
9
10GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11WARRANTY; without even the implied warranty of MERCHANTABILITY or
12FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13for more details.
14
15Under Section 7 of GPL version 3, you are granted additional
16permissions described in the GCC Runtime Library Exception, version
173.1, as published by the Free Software Foundation.
18
19You should have received a copy of the GNU General Public License and
20a copy of the GCC Runtime Library Exception along with this program;
21see 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#define COPYSIGN(x,y) __builtin_copysignf128 (x, y)
30#define INFINITY __builtin_inff128 ()
31#define FABS __builtin_fabsf128
32#define isnan __builtin_isnan
33#define isinf __builtin_isinf
34#define isfinite __builtin_isfinite
35
36#if defined(FLOAT128_HW_INSNS) && !defined(__divkc3)
37#define __divkc3 __divkc3_sw
38#endif
39
40TCtype
41__divkc3 (TFtype a, TFtype b, TFtype c, TFtype d)
42{
43  TFtype denom, ratio, x, y;
44  TCtype res;
45
46  /* ??? We can get better behavior from logarithmic scaling instead of
47     the division.  But that would mean starting to link libgcc against
48     libm.  We could implement something akin to ldexp/frexp as gcc builtins
49     fairly easily...  */
50  if (FABS (c) < FABS (d))
51    {
52      ratio = c / d;
53      denom = (c * ratio) + d;
54      x = ((a * ratio) + b) / denom;
55      y = ((b * ratio) - a) / denom;
56    }
57  else
58    {
59      ratio = d / c;
60      denom = (d * ratio) + c;
61      x = ((b * ratio) + a) / denom;
62      y = (b - (a * ratio)) / denom;
63    }
64
65  /* Recover infinities and zeros that computed as NaN+iNaN; the only cases
66     are nonzero/zero, infinite/finite, and finite/infinite.  */
67  if (isnan (x) && isnan (y))
68    {
69      if (c == 0.0 && d == 0.0 && (!isnan (a) || !isnan (b)))
70	{
71	  x = COPYSIGN (INFINITY, c) * a;
72	  y = COPYSIGN (INFINITY, c) * b;
73	}
74      else if ((isinf (a) || isinf (b)) && isfinite (c) && isfinite (d))
75	{
76	  a = COPYSIGN (isinf (a) ? 1 : 0, a);
77	  b = COPYSIGN (isinf (b) ? 1 : 0, b);
78	  x = INFINITY * (a * c + b * d);
79	  y = INFINITY * (b * c - a * d);
80	}
81      else if ((isinf (c) || isinf (d)) && isfinite (a) && isfinite (b))
82	{
83	  c = COPYSIGN (isinf (c) ? 1 : 0, c);
84	  d = COPYSIGN (isinf (d) ? 1 : 0, d);
85	  x = 0.0 * (a * c + b * d);
86	  y = 0.0 * (b * c - a * d);
87	}
88    }
89
90  __real__ res = x;
91  __imag__ res = y;
92  return res;
93}
94