1570af302Sopenharmony_ci/* origin: FreeBSD /usr/src/lib/msun/src/s_csqrtf.c */ 2570af302Sopenharmony_ci/*- 3570af302Sopenharmony_ci * Copyright (c) 2007 David Schultz <das@FreeBSD.ORG> 4570af302Sopenharmony_ci * All rights reserved. 5570af302Sopenharmony_ci * 6570af302Sopenharmony_ci * Redistribution and use in source and binary forms, with or without 7570af302Sopenharmony_ci * modification, are permitted provided that the following conditions 8570af302Sopenharmony_ci * are met: 9570af302Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright 10570af302Sopenharmony_ci * notice, this list of conditions and the following disclaimer. 11570af302Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright 12570af302Sopenharmony_ci * notice, this list of conditions and the following disclaimer in the 13570af302Sopenharmony_ci * documentation and/or other materials provided with the distribution. 14570af302Sopenharmony_ci * 15570af302Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16570af302Sopenharmony_ci * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17570af302Sopenharmony_ci * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18570af302Sopenharmony_ci * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19570af302Sopenharmony_ci * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20570af302Sopenharmony_ci * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21570af302Sopenharmony_ci * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22570af302Sopenharmony_ci * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23570af302Sopenharmony_ci * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24570af302Sopenharmony_ci * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25570af302Sopenharmony_ci * SUCH DAMAGE. 26570af302Sopenharmony_ci */ 27570af302Sopenharmony_ci 28570af302Sopenharmony_ci#include "complex_impl.h" 29570af302Sopenharmony_ci 30570af302Sopenharmony_ci/* 31570af302Sopenharmony_ci * gcc doesn't implement complex multiplication or division correctly, 32570af302Sopenharmony_ci * so we need to handle infinities specially. We turn on this pragma to 33570af302Sopenharmony_ci * notify conforming c99 compilers that the fast-but-incorrect code that 34570af302Sopenharmony_ci * gcc generates is acceptable, since the special cases have already been 35570af302Sopenharmony_ci * handled. 36570af302Sopenharmony_ci */ 37570af302Sopenharmony_ci#pragma STDC CX_LIMITED_RANGE ON 38570af302Sopenharmony_ci 39570af302Sopenharmony_cifloat complex csqrtf(float complex z) 40570af302Sopenharmony_ci{ 41570af302Sopenharmony_ci float a = crealf(z), b = cimagf(z); 42570af302Sopenharmony_ci double t; 43570af302Sopenharmony_ci 44570af302Sopenharmony_ci /* Handle special cases. */ 45570af302Sopenharmony_ci if (z == 0) 46570af302Sopenharmony_ci return CMPLXF(0, b); 47570af302Sopenharmony_ci if (isinf(b)) 48570af302Sopenharmony_ci return CMPLXF(INFINITY, b); 49570af302Sopenharmony_ci if (isnan(a)) { 50570af302Sopenharmony_ci t = (b - b) / (b - b); /* raise invalid if b is not a NaN */ 51570af302Sopenharmony_ci return CMPLXF(a, t); /* return NaN + NaN i */ 52570af302Sopenharmony_ci } 53570af302Sopenharmony_ci if (isinf(a)) { 54570af302Sopenharmony_ci /* 55570af302Sopenharmony_ci * csqrtf(inf + NaN i) = inf + NaN i 56570af302Sopenharmony_ci * csqrtf(inf + y i) = inf + 0 i 57570af302Sopenharmony_ci * csqrtf(-inf + NaN i) = NaN +- inf i 58570af302Sopenharmony_ci * csqrtf(-inf + y i) = 0 + inf i 59570af302Sopenharmony_ci */ 60570af302Sopenharmony_ci if (signbit(a)) 61570af302Sopenharmony_ci return CMPLXF(fabsf(b - b), copysignf(a, b)); 62570af302Sopenharmony_ci else 63570af302Sopenharmony_ci return CMPLXF(a, copysignf(b - b, b)); 64570af302Sopenharmony_ci } 65570af302Sopenharmony_ci /* 66570af302Sopenharmony_ci * The remaining special case (b is NaN) is handled just fine by 67570af302Sopenharmony_ci * the normal code path below. 68570af302Sopenharmony_ci */ 69570af302Sopenharmony_ci 70570af302Sopenharmony_ci /* 71570af302Sopenharmony_ci * We compute t in double precision to avoid overflow and to 72570af302Sopenharmony_ci * provide correct rounding in nearly all cases. 73570af302Sopenharmony_ci * This is Algorithm 312, CACM vol 10, Oct 1967. 74570af302Sopenharmony_ci */ 75570af302Sopenharmony_ci if (a >= 0) { 76570af302Sopenharmony_ci t = sqrt((a + hypot(a, b)) * 0.5); 77570af302Sopenharmony_ci return CMPLXF(t, b / (2.0 * t)); 78570af302Sopenharmony_ci } else { 79570af302Sopenharmony_ci t = sqrt((-a + hypot(a, b)) * 0.5); 80570af302Sopenharmony_ci return CMPLXF(fabsf(b) / (2.0 * t), copysignf(t, b)); 81570af302Sopenharmony_ci } 82570af302Sopenharmony_ci} 83