1*0a6a1f1dSLionel Sambuc /*-
2*0a6a1f1dSLionel Sambuc * Copyright (c) 2007-2008 David Schultz <das@FreeBSD.ORG>
3*0a6a1f1dSLionel Sambuc * All rights reserved.
4*0a6a1f1dSLionel Sambuc *
5*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
6*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
7*0a6a1f1dSLionel Sambuc * are met:
8*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
9*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
10*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
11*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
12*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
13*0a6a1f1dSLionel Sambuc *
14*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*0a6a1f1dSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*0a6a1f1dSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*0a6a1f1dSLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*0a6a1f1dSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*0a6a1f1dSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*0a6a1f1dSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*0a6a1f1dSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*0a6a1f1dSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*0a6a1f1dSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*0a6a1f1dSLionel Sambuc * SUCH DAMAGE.
25*0a6a1f1dSLionel Sambuc */
26*0a6a1f1dSLionel Sambuc
27*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
28*0a6a1f1dSLionel Sambuc #if 0
29*0a6a1f1dSLionel Sambuc __FBSDID("$FreeBSD: head/lib/msun/src/s_csqrtl.c 181402 2008-08-08 00:15:16Z das $");
30*0a6a1f1dSLionel Sambuc #else
31*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: csqrtl.c,v 1.2 2014/10/11 00:43:51 christos Exp $");
32*0a6a1f1dSLionel Sambuc #endif
33*0a6a1f1dSLionel Sambuc
34*0a6a1f1dSLionel Sambuc #include "../src/namespace.h"
35*0a6a1f1dSLionel Sambuc #include <complex.h>
36*0a6a1f1dSLionel Sambuc #include <float.h>
37*0a6a1f1dSLionel Sambuc #include <math.h>
38*0a6a1f1dSLionel Sambuc #include <stdbool.h>
39*0a6a1f1dSLionel Sambuc
40*0a6a1f1dSLionel Sambuc /*
41*0a6a1f1dSLionel Sambuc * gcc doesn't implement complex multiplication or division correctly,
42*0a6a1f1dSLionel Sambuc * so we need to handle infinities specially. We turn on this pragma to
43*0a6a1f1dSLionel Sambuc * notify conforming c99 compilers that the fast-but-incorrect code that
44*0a6a1f1dSLionel Sambuc * gcc generates is acceptable, since the special cases have already been
45*0a6a1f1dSLionel Sambuc * handled.
46*0a6a1f1dSLionel Sambuc */
47*0a6a1f1dSLionel Sambuc // #pragma STDC CX_LIMITED_RANGE ON
48*0a6a1f1dSLionel Sambuc
49*0a6a1f1dSLionel Sambuc /* We risk spurious overflow for components >= LDBL_MAX / (1 + sqrt(2)). */
50*0a6a1f1dSLionel Sambuc #define THRESH (LDBL_MAX / 2.414213562373095048801688724209698L)
51*0a6a1f1dSLionel Sambuc
52*0a6a1f1dSLionel Sambuc #define cpackl(r, i) ((r) + (i) * I)
53*0a6a1f1dSLionel Sambuc
54*0a6a1f1dSLionel Sambuc long double complex
csqrtl(long double complex z)55*0a6a1f1dSLionel Sambuc csqrtl(long double complex z)
56*0a6a1f1dSLionel Sambuc {
57*0a6a1f1dSLionel Sambuc long double complex result;
58*0a6a1f1dSLionel Sambuc long double a, b;
59*0a6a1f1dSLionel Sambuc long double t;
60*0a6a1f1dSLionel Sambuc bool scale;
61*0a6a1f1dSLionel Sambuc
62*0a6a1f1dSLionel Sambuc a = creall(z);
63*0a6a1f1dSLionel Sambuc b = cimagl(z);
64*0a6a1f1dSLionel Sambuc
65*0a6a1f1dSLionel Sambuc /* Handle special cases. */
66*0a6a1f1dSLionel Sambuc if (z == 0.0L)
67*0a6a1f1dSLionel Sambuc return (cpackl(0.0L, b));
68*0a6a1f1dSLionel Sambuc if (isinf(b))
69*0a6a1f1dSLionel Sambuc return (cpackl(INFINITY, b));
70*0a6a1f1dSLionel Sambuc if (isnan(a)) {
71*0a6a1f1dSLionel Sambuc t = (b - b) / (b - b); /* raise invalid if b is not a NaN */
72*0a6a1f1dSLionel Sambuc return (cpackl(a, t)); /* return NaN + NaN i */
73*0a6a1f1dSLionel Sambuc }
74*0a6a1f1dSLionel Sambuc if (isinf(a)) {
75*0a6a1f1dSLionel Sambuc /*
76*0a6a1f1dSLionel Sambuc * csqrt(inf + NaN i) = inf + NaN i
77*0a6a1f1dSLionel Sambuc * csqrt(inf + y i) = inf + 0 i
78*0a6a1f1dSLionel Sambuc * csqrt(-inf + NaN i) = NaN +- inf i
79*0a6a1f1dSLionel Sambuc * csqrt(-inf + y i) = 0 + inf i
80*0a6a1f1dSLionel Sambuc */
81*0a6a1f1dSLionel Sambuc if (signbit(a))
82*0a6a1f1dSLionel Sambuc return (cpackl(fabsl(b - b), copysignl(a, b)));
83*0a6a1f1dSLionel Sambuc else
84*0a6a1f1dSLionel Sambuc return (cpackl(a, copysignl(b - b, b)));
85*0a6a1f1dSLionel Sambuc }
86*0a6a1f1dSLionel Sambuc /*
87*0a6a1f1dSLionel Sambuc * The remaining special case (b is NaN) is handled just fine by
88*0a6a1f1dSLionel Sambuc * the normal code path below.
89*0a6a1f1dSLionel Sambuc */
90*0a6a1f1dSLionel Sambuc
91*0a6a1f1dSLionel Sambuc /* Scale to avoid overflow. */
92*0a6a1f1dSLionel Sambuc if (fabsl(a) >= THRESH || fabsl(b) >= THRESH) {
93*0a6a1f1dSLionel Sambuc a *= 0.25L;
94*0a6a1f1dSLionel Sambuc b *= 0.25L;
95*0a6a1f1dSLionel Sambuc scale = true;
96*0a6a1f1dSLionel Sambuc } else {
97*0a6a1f1dSLionel Sambuc scale = false;
98*0a6a1f1dSLionel Sambuc }
99*0a6a1f1dSLionel Sambuc
100*0a6a1f1dSLionel Sambuc /* Algorithm 312, CACM vol 10, Oct 1967. */
101*0a6a1f1dSLionel Sambuc if (a >= 0L) {
102*0a6a1f1dSLionel Sambuc t = sqrtl((a + hypotl(a, b)) * 0.5L);
103*0a6a1f1dSLionel Sambuc result = cpackl(t, b / (2.0L * t));
104*0a6a1f1dSLionel Sambuc } else {
105*0a6a1f1dSLionel Sambuc t = sqrtl((-a + hypotl(a, b)) * 0.5L);
106*0a6a1f1dSLionel Sambuc result = cpackl(fabsl(b) / (2.0L * t), copysignl(t, b));
107*0a6a1f1dSLionel Sambuc }
108*0a6a1f1dSLionel Sambuc
109*0a6a1f1dSLionel Sambuc /* Rescale. */
110*0a6a1f1dSLionel Sambuc if (scale)
111*0a6a1f1dSLionel Sambuc return (result * 2.0L);
112*0a6a1f1dSLionel Sambuc else
113*0a6a1f1dSLionel Sambuc return (result);
114*0a6a1f1dSLionel Sambuc }
115