_divkc3.c revision 1.1.1.2
1/* Copyright (C) 1989-2017 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
26typedef float KFtype __attribute__ ((mode (KF)));
27typedef __complex float KCtype __attribute__ ((mode (KC)));
28
29#define COPYSIGN(x,y) __builtin_copysignq (x, y)
30#define INFINITY __builtin_infq ()
31#define FABS __builtin_fabsq
32#define isnan __builtin_isnan
33#define isinf __builtin_isinf
34#define isfinite __builtin_isfinite
35
36KCtype
37__divkc3 (KFtype a, KFtype b, KFtype c, KFtype d)
38{
39  KFtype denom, ratio, x, y;
40  KCtype res;
41
42  /* ??? We can get better behavior from logarithmic scaling instead of
43     the division.  But that would mean starting to link libgcc against
44     libm.  We could implement something akin to ldexp/frexp as gcc builtins
45     fairly easily...  */
46  if (FABS (c) < FABS (d))
47    {
48      ratio = c / d;
49      denom = (c * ratio) + d;
50      x = ((a * ratio) + b) / denom;
51      y = ((b * ratio) - a) / denom;
52    }
53  else
54    {
55      ratio = d / c;
56      denom = (d * ratio) + c;
57      x = ((b * ratio) + a) / denom;
58      y = (b - (a * ratio)) / denom;
59    }
60
61  /* Recover infinities and zeros that computed as NaN+iNaN; the only cases
62     are nonzero/zero, infinite/finite, and finite/infinite.  */
63  if (isnan (x) && isnan (y))
64    {
65      if (c == 0.0 && d == 0.0 && (!isnan (a) || !isnan (b)))
66	{
67	  x = COPYSIGN (INFINITY, c) * a;
68	  y = COPYSIGN (INFINITY, c) * b;
69	}
70      else if ((isinf (a) || isinf (b)) && isfinite (c) && isfinite (d))
71	{
72	  a = COPYSIGN (isinf (a) ? 1 : 0, a);
73	  b = COPYSIGN (isinf (b) ? 1 : 0, b);
74	  x = INFINITY * (a * c + b * d);
75	  y = INFINITY * (b * c - a * d);
76	}
77      else if ((isinf (c) || isinf (d)) && isfinite (a) && isfinite (b))
78	{
79	  c = COPYSIGN (isinf (c) ? 1 : 0, c);
80	  d = COPYSIGN (isinf (d) ? 1 : 0, d);
81	  x = 0.0 * (a * c + b * d);
82	  y = 0.0 * (b * c - a * d);
83	}
84    }
85
86  __real__ res = x;
87  __imag__ res = y;
88  return res;
89}
90