csqrtl.c revision ddc0e0b53c661f6e439e3b7072b3ef353eadb4af
2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
2N/A */
2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma weak __csqrtl = csqrtl
2N/A
2N/A#include "libm.h" /* fabsl/isinfl/sqrtl */
2N/A#include "complex_wrapper.h"
2N/A#include "longdouble.h"
2N/A
2N/A/* INDENT OFF */
2N/Astatic const long double
2N/A twom9001 = 2.6854002716003034957421765100615693043656e-2710L,
2N/A twom4500 = 2.3174987687592429423263242862381544149252e-1355L,
2N/A two8999 = 9.3095991180122343502582347372163290310934e+2708L,
2N/A two4500 = 4.3149968987270974283777803545571722250806e+1354L,
2N/A zero = 0.0L,
2N/A half = 0.5L,
2N/A two = 2.0L;
2N/A/* INDENT ON */
2N/A
2N/Aldcomplex
2N/Acsqrtl(ldcomplex z) {
2N/A ldcomplex ans;
2N/A long double x, y, t, ax, ay;
2N/A int n, ix, iy, hx, hy;
2N/A
2N/A x = LD_RE(z);
2N/A y = LD_IM(z);
2N/A hx = HI_XWORD(x);
2N/A hy = HI_XWORD(y);
2N/A ix = hx & 0x7fffffff;
2N/A iy = hy & 0x7fffffff;
2N/A ay = fabsl(y);
2N/A ax = fabsl(x);
2N/A if (ix >= 0x7fff0000 || iy >= 0x7fff0000) {
2N/A /* x or y is Inf or NaN */
2N/A if (isinfl(y))
2N/A LD_IM(ans) = LD_RE(ans) = ay;
2N/A else if (isinfl(x)) {
2N/A if (hx > 0) {
2N/A LD_RE(ans) = ax;
2N/A LD_IM(ans) = ay * zero;
2N/A } else {
2N/A LD_RE(ans) = ay * zero;
2N/A LD_IM(ans) = ax;
2N/A }
2N/A } else
2N/A LD_IM(ans) = LD_RE(ans) = ax + ay;
2N/A } else if (y == zero) {
2N/A if (hx >= 0) {
2N/A LD_RE(ans) = sqrtl(ax);
2N/A LD_IM(ans) = zero;
2N/A } else {
2N/A LD_IM(ans) = sqrtl(ax);
2N/A LD_RE(ans) = zero;
2N/A }
2N/A } else if (ix >= iy) {
2N/A n = (ix - iy) >> 16;
2N/A#if defined(__x86) /* 64 significant bits */
2N/A if (n >= 35)
2N/A#else /* 113 significant bits */
2N/A if (n >= 60)
2N/A#endif
2N/A t = sqrtl(ax);
2N/A else if (ix >= 0x5f3f0000) { /* x > 2**8000 */
2N/A ax *= twom9001;
2N/A y *= twom9001;
2N/A t = two4500 * sqrtl(ax + sqrtl(ax * ax + y * y));
2N/A } else if (iy <= 0x20bf0000) { /* y < 2**-8000 */
2N/A ax *= two8999;
2N/A y *= two8999;
2N/A t = twom4500 * sqrtl(ax + sqrtl(ax * ax + y * y));
2N/A } else
2N/A t = sqrtl(half * (ax + sqrtl(ax * ax + y * y)));
2N/A
2N/A if (hx >= 0) {
2N/A LD_RE(ans) = t;
2N/A LD_IM(ans) = ay / (t + t);
2N/A } else {
2N/A LD_IM(ans) = t;
2N/A LD_RE(ans) = ay / (t + t);
2N/A }
2N/A } else {
2N/A n = (iy - ix) >> 16;
2N/A#if defined(__x86) /* 64 significant bits */
2N/A if (n >= 35) { /* } */
2N/A#else /* 113 significant bits */
2N/A if (n >= 60) {
2N/A#endif
2N/A if (n >= 120)
2N/A t = sqrtl(half * ay);
2N/A else if (iy >= 0x7ffe0000)
2N/A t = sqrtl(half * ay + half * ax);
2N/A else if (ix <= 0x00010000)
2N/A t = half * (sqrtl(two * (ax + ay)));
2N/A else
2N/A t = sqrtl(half * (ax + ay));
2N/A } else if (iy >= 0x5f3f0000) { /* y > 2**8000 */
2N/A ax *= twom9001;
2N/A y *= twom9001;
2N/A t = two4500 * sqrtl(ax + sqrtl(ax * ax + y * y));
2N/A } else if (ix <= 0x20bf0000) {
2N/A ax *= two8999;
2N/A y *= two8999;
2N/A t = twom4500 * sqrtl(ax + sqrtl(ax * ax + y * y));
2N/A } else
2N/A t = sqrtl(half * (ax + sqrtl(ax * ax + y * y)));
2N/A
2N/A if (hx >= 0) {
2N/A LD_RE(ans) = t;
2N/A LD_IM(ans) = ay / (t + t);
2N/A } else {
2N/A LD_IM(ans) = t;
2N/A LD_RE(ans) = ay / (t + t);
2N/A }
2N/A }
2N/A if (hy < 0)
2N/A LD_IM(ans) = -LD_IM(ans);
2N/A return (ans);
2N/A}
2N/A