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_csqrtf.c 275819 2014-12-16 09:21:56Z ed $"); 29a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 30a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include <complex.h> 31a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include <math.h> 32a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 33a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#include "math_private.h" 34a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 35a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes/* 36a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * gcc doesn't implement complex multiplication or division correctly, 37a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * so we need to handle infinities specially. We turn on this pragma to 38a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * notify conforming c99 compilers that the fast-but-incorrect code that 39a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * gcc generates is acceptable, since the special cases have already been 40a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * handled. 41a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes */ 42a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes#pragma STDC CX_LIMITED_RANGE ON 43a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 44a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughesfloat complex 45a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughescsqrtf(float complex z) 46a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes{ 47a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes float a = crealf(z), b = cimagf(z); 48a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes double t; 49a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 50a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes /* Handle special cases. */ 51a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes if (z == 0) 528cff2f95d8673b4b9002292d50ce8caa6efb98b6Elliott Hughes return (CMPLXF(0, b)); 53a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes if (isinf(b)) 548cff2f95d8673b4b9002292d50ce8caa6efb98b6Elliott Hughes return (CMPLXF(INFINITY, b)); 55a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes if (isnan(a)) { 56a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes t = (b - b) / (b - b); /* raise invalid if b is not a NaN */ 578cff2f95d8673b4b9002292d50ce8caa6efb98b6Elliott Hughes return (CMPLXF(a, t)); /* return NaN + NaN i */ 58a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes } 59a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes if (isinf(a)) { 60a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes /* 61a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * csqrtf(inf + NaN i) = inf + NaN i 62a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * csqrtf(inf + y i) = inf + 0 i 63a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * csqrtf(-inf + NaN i) = NaN +- inf i 64a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * csqrtf(-inf + y i) = 0 + inf i 65a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes */ 66a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes if (signbit(a)) 678cff2f95d8673b4b9002292d50ce8caa6efb98b6Elliott Hughes return (CMPLXF(fabsf(b - b), copysignf(a, b))); 68a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes else 698cff2f95d8673b4b9002292d50ce8caa6efb98b6Elliott Hughes return (CMPLXF(a, copysignf(b - b, b))); 70a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes } 71a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes /* 72a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * The remaining special case (b is NaN) is handled just fine by 73a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * the normal code path below. 74a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes */ 75a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes 76a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes /* 77a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * We compute t in double precision to avoid overflow and to 78a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * provide correct rounding in nearly all cases. 79a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes * This is Algorithm 312, CACM vol 10, Oct 1967. 80a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes */ 81a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes if (a >= 0) { 82a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes t = sqrt((a + hypot(a, b)) * 0.5); 838cff2f95d8673b4b9002292d50ce8caa6efb98b6Elliott Hughes return (CMPLXF(t, b / (2.0 * t))); 84a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes } else { 85a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes t = sqrt((-a + hypot(a, b)) * 0.5); 868cff2f95d8673b4b9002292d50ce8caa6efb98b6Elliott Hughes return (CMPLXF(fabsf(b) / (2.0 * t), copysignf(t, b))); 87a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes } 88a0ee07829a9ba7e99ef68e8c12551301cc797f0fElliott Hughes} 89