Home | History | Annotate | Line # | Download | only in complex
csqrtl.c revision 1.1
      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.1  christos __RCSID("$NetBSD: csqrtl.c,v 1.1 2014/10/10 00:48:18 christos Exp $");
     32  1.1  christos #endif
     33  1.1  christos 
     34  1.1  christos #include <complex.h>
     35  1.1  christos #include <float.h>
     36  1.1  christos #include <math.h>
     37  1.1  christos #include <stdbool.h>
     38  1.1  christos 
     39  1.1  christos /*
     40  1.1  christos  * gcc doesn't implement complex multiplication or division correctly,
     41  1.1  christos  * so we need to handle infinities specially. We turn on this pragma to
     42  1.1  christos  * notify conforming c99 compilers that the fast-but-incorrect code that
     43  1.1  christos  * gcc generates is acceptable, since the special cases have already been
     44  1.1  christos  * handled.
     45  1.1  christos  */
     46  1.1  christos // #pragma	STDC CX_LIMITED_RANGE	ON
     47  1.1  christos 
     48  1.1  christos /* We risk spurious overflow for components >= LDBL_MAX / (1 + sqrt(2)). */
     49  1.1  christos #define	THRESH	(LDBL_MAX / 2.414213562373095048801688724209698L)
     50  1.1  christos 
     51  1.1  christos #define cpackl(r, i) ((r) + (i) * I)
     52  1.1  christos 
     53  1.1  christos long double complex
     54  1.1  christos csqrtl(long double complex z)
     55  1.1  christos {
     56  1.1  christos 	long double complex result;
     57  1.1  christos 	long double a, b;
     58  1.1  christos 	long double t;
     59  1.1  christos 	bool scale;
     60  1.1  christos 
     61  1.1  christos 	a = creall(z);
     62  1.1  christos 	b = cimagl(z);
     63  1.1  christos 
     64  1.1  christos 	/* Handle special cases. */
     65  1.1  christos 	if (z == 0.0L)
     66  1.1  christos 		return (cpackl(0.0L, b));
     67  1.1  christos 	if (isinf(b))
     68  1.1  christos 		return (cpackl(INFINITY, b));
     69  1.1  christos 	if (isnan(a)) {
     70  1.1  christos 		t = (b - b) / (b - b);	/* raise invalid if b is not a NaN */
     71  1.1  christos 		return (cpackl(a, t));	/* return NaN + NaN i */
     72  1.1  christos 	}
     73  1.1  christos 	if (isinf(a)) {
     74  1.1  christos 		/*
     75  1.1  christos 		 * csqrt(inf + NaN i)  = inf +  NaN i
     76  1.1  christos 		 * csqrt(inf + y i)    = inf +  0 i
     77  1.1  christos 		 * csqrt(-inf + NaN i) = NaN +- inf i
     78  1.1  christos 		 * csqrt(-inf + y i)   = 0   +  inf i
     79  1.1  christos 		 */
     80  1.1  christos 		if (signbit(a))
     81  1.1  christos 			return (cpackl(fabsl(b - b), copysignl(a, b)));
     82  1.1  christos 		else
     83  1.1  christos 			return (cpackl(a, copysignl(b - b, b)));
     84  1.1  christos 	}
     85  1.1  christos 	/*
     86  1.1  christos 	 * The remaining special case (b is NaN) is handled just fine by
     87  1.1  christos 	 * the normal code path below.
     88  1.1  christos 	 */
     89  1.1  christos 
     90  1.1  christos 	/* Scale to avoid overflow. */
     91  1.1  christos 	if (fabsl(a) >= THRESH || fabsl(b) >= THRESH) {
     92  1.1  christos 		a *= 0.25L;
     93  1.1  christos 		b *= 0.25L;
     94  1.1  christos 		scale = true;
     95  1.1  christos 	} else {
     96  1.1  christos 		scale = false;
     97  1.1  christos 	}
     98  1.1  christos 
     99  1.1  christos 	/* Algorithm 312, CACM vol 10, Oct 1967. */
    100  1.1  christos 	if (a >= 0L) {
    101  1.1  christos 		t = sqrtl((a + hypotl(a, b)) * 0.5L);
    102  1.1  christos 		result = cpackl(t, b / (2.0L * t));
    103  1.1  christos 	} else {
    104  1.1  christos 		t = sqrtl((-a + hypotl(a, b)) * 0.5L);
    105  1.1  christos 		result = cpackl(fabsl(b) / (2.0L * t), copysignl(t, b));
    106  1.1  christos 	}
    107  1.1  christos 
    108  1.1  christos 	/* Rescale. */
    109  1.1  christos 	if (scale)
    110  1.1  christos 		return (result * 2.0L);
    111  1.1  christos 	else
    112  1.1  christos 		return (result);
    113  1.1  christos }
    114