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