s_csqrtf.c revision 181402
1174617Sdas/*- 2174617Sdas * Copyright (c) 2007 David Schultz <das@FreeBSD.ORG> 3174617Sdas * All rights reserved. 4174617Sdas * 5174617Sdas * Redistribution and use in source and binary forms, with or without 6174617Sdas * modification, are permitted provided that the following conditions 7174617Sdas * are met: 8174617Sdas * 1. Redistributions of source code must retain the above copyright 9174617Sdas * notice, this list of conditions and the following disclaimer. 10174617Sdas * 2. Redistributions in binary form must reproduce the above copyright 11174617Sdas * notice, this list of conditions and the following disclaimer in the 12174617Sdas * documentation and/or other materials provided with the distribution. 13174617Sdas * 14174617Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15174617Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16174617Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17174617Sdas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18174617Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19174617Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20174617Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21174617Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22174617Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23174617Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24174617Sdas * SUCH DAMAGE. 25174617Sdas */ 26174617Sdas 27174617Sdas#include <sys/cdefs.h> 28174617Sdas__FBSDID("$FreeBSD: head/lib/msun/src/s_csqrtf.c 181402 2008-08-08 00:15:16Z das $"); 29174617Sdas 30174617Sdas#include <complex.h> 31174617Sdas#include <math.h> 32174617Sdas 33174617Sdas#include "math_private.h" 34174617Sdas 35174617Sdas/* 36174617Sdas * gcc doesn't implement complex multiplication or division correctly, 37174617Sdas * so we need to handle infinities specially. We turn on this pragma to 38174617Sdas * notify conforming c99 compilers that the fast-but-incorrect code that 39174617Sdas * gcc generates is acceptable, since the special cases have already been 40174617Sdas * handled. 41174617Sdas */ 42181402Sdas#pragma STDC CX_LIMITED_RANGE ON 43174617Sdas 44174617Sdasfloat complex 45174617Sdascsqrtf(float complex z) 46174617Sdas{ 47174617Sdas float a = crealf(z), b = cimagf(z); 48174617Sdas double t; 49174617Sdas 50174617Sdas /* Handle special cases. */ 51174617Sdas if (z == 0) 52174617Sdas return (cpackf(0, b)); 53174617Sdas if (isinf(b)) 54174617Sdas return (cpackf(INFINITY, b)); 55174617Sdas if (isnan(a)) { 56174617Sdas t = (b - b) / (b - b); /* raise invalid if b is not a NaN */ 57175225Sdas return (cpackf(a, t)); /* return NaN + NaN i */ 58174617Sdas } 59174617Sdas if (isinf(a)) { 60174617Sdas /* 61175225Sdas * csqrtf(inf + NaN i) = inf + NaN i 62174617Sdas * csqrtf(inf + y i) = inf + 0 i 63175225Sdas * csqrtf(-inf + NaN i) = NaN +- inf i 64174617Sdas * csqrtf(-inf + y i) = 0 + inf i 65174617Sdas */ 66174617Sdas if (signbit(a)) 67174617Sdas return (cpackf(fabsf(b - b), copysignf(a, b))); 68174617Sdas else 69174617Sdas return (cpackf(a, copysignf(b - b, b))); 70174617Sdas } 71174617Sdas /* 72174617Sdas * The remaining special case (b is NaN) is handled just fine by 73174617Sdas * the normal code path below. 74174617Sdas */ 75174617Sdas 76174617Sdas /* 77174617Sdas * We compute t in double precision to avoid overflow and to 78174617Sdas * provide correct rounding in nearly all cases. 79174617Sdas * This is Algorithm 312, CACM vol 10, Oct 1967. 80174617Sdas */ 81174617Sdas if (a >= 0) { 82174617Sdas t = sqrt((a + hypot(a, b)) * 0.5); 83174617Sdas return (cpackf(t, b / (2.0 * t))); 84174617Sdas } else { 85174617Sdas t = sqrt((-a + hypot(a, b)) * 0.5); 86174617Sdas return (cpackf(fabsf(b) / (2.0 * t), copysignf(t, b))); 87174617Sdas } 88174617Sdas} 89