1177761Sdas/*-
2177761Sdas * Copyright (c) 2007-2008 David Schultz <das@FreeBSD.ORG>
3177761Sdas * All rights reserved.
4177761Sdas *
5177761Sdas * Redistribution and use in source and binary forms, with or without
6177761Sdas * modification, are permitted provided that the following conditions
7177761Sdas * are met:
8177761Sdas * 1. Redistributions of source code must retain the above copyright
9177761Sdas *    notice, this list of conditions and the following disclaimer.
10177761Sdas * 2. Redistributions in binary form must reproduce the above copyright
11177761Sdas *    notice, this list of conditions and the following disclaimer in the
12177761Sdas *    documentation and/or other materials provided with the distribution.
13177761Sdas *
14177761Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15177761Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16177761Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17177761Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18177761Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19177761Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20177761Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21177761Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22177761Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23177761Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24177761Sdas * SUCH DAMAGE.
25177761Sdas */
26177761Sdas
27177761Sdas#include <sys/cdefs.h>
28177761Sdas__FBSDID("$FreeBSD$");
29177761Sdas
30177761Sdas#include <complex.h>
31177761Sdas#include <float.h>
32177761Sdas#include <math.h>
33177761Sdas
34177761Sdas#include "math_private.h"
35177761Sdas
36177761Sdas/*
37177761Sdas * gcc doesn't implement complex multiplication or division correctly,
38177761Sdas * so we need to handle infinities specially. We turn on this pragma to
39177761Sdas * notify conforming c99 compilers that the fast-but-incorrect code that
40177761Sdas * gcc generates is acceptable, since the special cases have already been
41177761Sdas * handled.
42177761Sdas */
43181402Sdas#pragma	STDC CX_LIMITED_RANGE	ON
44177761Sdas
45177761Sdas/* We risk spurious overflow for components >= LDBL_MAX / (1 + sqrt(2)). */
46177761Sdas#define	THRESH	(LDBL_MAX / 2.414213562373095048801688724209698L)
47177761Sdas
48177761Sdaslong double complex
49177761Sdascsqrtl(long double complex z)
50177761Sdas{
51177761Sdas	long double complex result;
52177761Sdas	long double a, b;
53177761Sdas	long double t;
54177761Sdas	int scale;
55177761Sdas
56177761Sdas	a = creall(z);
57177761Sdas	b = cimagl(z);
58177761Sdas
59177761Sdas	/* Handle special cases. */
60177761Sdas	if (z == 0)
61177761Sdas		return (cpackl(0, b));
62177761Sdas	if (isinf(b))
63177761Sdas		return (cpackl(INFINITY, b));
64177761Sdas	if (isnan(a)) {
65177761Sdas		t = (b - b) / (b - b);	/* raise invalid if b is not a NaN */
66177761Sdas		return (cpackl(a, t));	/* return NaN + NaN i */
67177761Sdas	}
68177761Sdas	if (isinf(a)) {
69177761Sdas		/*
70177761Sdas		 * csqrt(inf + NaN i)  = inf +  NaN i
71177761Sdas		 * csqrt(inf + y i)    = inf +  0 i
72177761Sdas		 * csqrt(-inf + NaN i) = NaN +- inf i
73177761Sdas		 * csqrt(-inf + y i)   = 0   +  inf i
74177761Sdas		 */
75177761Sdas		if (signbit(a))
76177761Sdas			return (cpackl(fabsl(b - b), copysignl(a, b)));
77177761Sdas		else
78177761Sdas			return (cpackl(a, copysignl(b - b, b)));
79177761Sdas	}
80177761Sdas	/*
81177761Sdas	 * The remaining special case (b is NaN) is handled just fine by
82177761Sdas	 * the normal code path below.
83177761Sdas	 */
84177761Sdas
85177761Sdas	/* Scale to avoid overflow. */
86177761Sdas	if (fabsl(a) >= THRESH || fabsl(b) >= THRESH) {
87177761Sdas		a *= 0.25;
88177761Sdas		b *= 0.25;
89177761Sdas		scale = 1;
90177761Sdas	} else {
91177761Sdas		scale = 0;
92177761Sdas	}
93177761Sdas
94177761Sdas	/* Algorithm 312, CACM vol 10, Oct 1967. */
95177761Sdas	if (a >= 0) {
96177761Sdas		t = sqrtl((a + hypotl(a, b)) * 0.5);
97177761Sdas		result = cpackl(t, b / (2 * t));
98177761Sdas	} else {
99177761Sdas		t = sqrtl((-a + hypotl(a, b)) * 0.5);
100177761Sdas		result = cpackl(fabsl(b) / (2 * t), copysignl(t, b));
101177761Sdas	}
102177761Sdas
103177761Sdas	/* Rescale. */
104177761Sdas	if (scale)
105177761Sdas		return (result * 2);
106177761Sdas	else
107177761Sdas		return (result);
108177761Sdas}
109