csqrtf.c revision ddc0e0b53c661f6e439e3b7072b3ef353eadb4af
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/*
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * CDDL HEADER START
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * The contents of this file are subject to the terms of the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Common Development and Distribution License (the "License").
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * You may not use this file except in compliance with the License.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * or http://www.opensolaris.org/os/licensing.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * See the License for the specific language governing permissions
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * and limitations under the License.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner]
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * CDDL HEADER END
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/*
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/*
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Use is subject to license terms.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
ddc0e0b53c661f6e439e3b7072b3ef353eadb4afRichard Lowe#pragma weak __csqrtf = csqrtf
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#include "libm.h" /* sqrt/fabsf/sqrtf */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#include "complex_wrapper.h"
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* INDENT OFF */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtisstatic const float zero = 0.0F;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* INDENT ON */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtisfcomplex
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtiscsqrtf(fcomplex z) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis fcomplex ans;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis double dt, dx, dy;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis float x, y, t, ax, ay, w;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis int ix, iy, hx, hy;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis x = F_RE(z);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis y = F_IM(z);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis hx = THE_WORD(x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis hy = THE_WORD(y);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis ix = hx & 0x7fffffff;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis iy = hy & 0x7fffffff;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis ay = fabsf(y);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis ax = fabsf(x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (ix >= 0x7f800000 || iy >= 0x7f800000) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* x or y is Inf or NaN */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (iy == 0x7f800000)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_IM(ans) = F_RE(ans) = ay;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else if (ix == 0x7f800000) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hx > 0) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_RE(ans) = ax;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_IM(ans) = ay * zero;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_RE(ans) = ay * zero;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_IM(ans) = ax;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_IM(ans) = F_RE(ans) = ax + ay;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else if (iy == 0) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hx >= 0) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_RE(ans) = sqrtf(ax);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_IM(ans) = zero;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_IM(ans) = sqrtf(ax);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_RE(ans) = zero;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis dx = (double) ax;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis dy = (double) ay;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis dt = sqrt(0.5 * (sqrt(dx * dx + dy * dy) + dx));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis t = (float) dt;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis w = (float) (dy / (dt + dt));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hx >= 0) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_RE(ans) = t;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_IM(ans) = w;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_IM(ans) = t;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_RE(ans) = w;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hy < 0)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_IM(ans) = -F_IM(ans);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return (ans);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis}