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