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