1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  24  */
  25 
  26 /*
  27  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  28  * Use is subject to license terms.
  29  */
  30 
  31 #pragma weak __csqrtf = csqrtf
  32 
  33 #include "libm.h"                       /* sqrt/fabsf/sqrtf */
  34 #include "complex_wrapper.h"
  35 
  36 static const float zero = 0.0F;
  37 
  38 
  39 fcomplex
  40 csqrtf(fcomplex z)
  41 {
  42         fcomplex ans;
  43         double dt, dx, dy;
  44         float x, y, t, ax, ay, w;
  45         int ix, iy, hx, hy;
  46 
  47         x = F_RE(z);
  48         y = F_IM(z);
  49         hx = THE_WORD(x);
  50         hy = THE_WORD(y);
  51         ix = hx & 0x7fffffff;
  52         iy = hy & 0x7fffffff;
  53         ay = fabsf(y);
  54         ax = fabsf(x);
  55 
  56         if (ix >= 0x7f800000 || iy >= 0x7f800000) {
  57                 /* x or y is Inf or NaN */
  58                 if (iy == 0x7f800000) {
  59                         F_IM(ans) = F_RE(ans) = ay;
  60                 } else if (ix == 0x7f800000) {
  61                         if (hx > 0) {
  62                                 F_RE(ans) = ax;
  63                                 F_IM(ans) = ay * zero;
  64                         } else {
  65                                 F_RE(ans) = ay * zero;
  66                                 F_IM(ans) = ax;
  67                         }
  68                 } else {
  69                         F_IM(ans) = F_RE(ans) = ax + ay;
  70                 }
  71         } else if (iy == 0) {
  72                 if (hx >= 0) {
  73                         F_RE(ans) = sqrtf(ax);
  74                         F_IM(ans) = zero;
  75                 } else {
  76                         F_IM(ans) = sqrtf(ax);
  77                         F_RE(ans) = zero;
  78                 }
  79         } else {
  80                 dx = (double)ax;
  81                 dy = (double)ay;
  82                 dt = sqrt(0.5 * (sqrt(dx * dx + dy * dy) + dx));
  83                 t = (float)dt;
  84                 w = (float)(dy / (dt + dt));
  85 
  86                 if (hx >= 0) {
  87                         F_RE(ans) = t;
  88                         F_IM(ans) = w;
  89                 } else {
  90                         F_IM(ans) = t;
  91                         F_RE(ans) = w;
  92                 }
  93         }
  94 
  95         if (hy < 0)
  96                 F_IM(ans) = -F_IM(ans);
  97 
  98         return (ans);
  99 }