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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  27  * Use is subject to license terms.
  28  */
  29 
  30 #pragma weak __clogl = clogl
  31 
  32 #include "libm.h"       /* atan2l/fabsl/isinfl/log1pl/logl/__k_clog_rl */
  33 #include "complex_wrapper.h"
  34 #include "longdouble.h"
  35 
  36 #if defined(__sparc)
  37 #define SIGP7   120
  38 #define HSIGP7  60
  39 #elif defined(__x86)
  40 #define SIGP7   70
  41 #define HSIGP7  35
  42 #endif
  43 
  44 /* INDENT OFF */
  45 static const long double zero = 0.0L, half = 0.5L, one = 1.0L;
  46 /* INDENT ON */
  47 
  48 ldcomplex
  49 clogl(ldcomplex z) {
  50         ldcomplex ans;
  51         long double x, y, t, ax, ay;
  52         int n, ix, iy, hx, hy;
  53 
  54         x = LD_RE(z);
  55         y = LD_IM(z);
  56         hx = HI_XWORD(x);
  57         hy = HI_XWORD(y);
  58         ix = hx & 0x7fffffff;
  59         iy = hy & 0x7fffffff;
  60         ay = fabsl(y);
  61         ax = fabsl(x);
  62         LD_IM(ans) = atan2l(y, x);
  63         if (ix < iy || (ix == iy && ix < 0x7fff0000 && ax < ay)) {
  64                         /* swap x and y to force ax>=ay */
  65                 t = ax;
  66                 ax = ay;
  67                 ay = t;
  68                 n = ix, ix = iy;
  69                 iy = n;
  70         }
  71         n = (ix - iy) >> 16;
  72         if (ix >= 0x7fff0000) {      /* x or y is Inf or NaN */
  73                 if (isinfl(ax))
  74                         LD_RE(ans) = ax;
  75                 else if (isinfl(ay))
  76                         LD_RE(ans) = ay;
  77                 else
  78                         LD_RE(ans) = ax + ay;
  79         } else if (ay == zero)
  80                 LD_RE(ans) = logl(ax);
  81         else if (((0x3fffffff - ix) ^ (ix - 0x3ffe0000)) >= 0) {
  82                                                         /* 0.5 <= x < 2 */
  83                 if (ix >= 0x3fff0000) {
  84                         if (ax == one)
  85                                 LD_RE(ans) = half * log1pl(ay * ay);
  86                         else if (n >= SIGP7)
  87                                 LD_RE(ans) = logl(ax);
  88                         else
  89                                 LD_RE(ans) = half * (log1pl(ay * ay + (ax -
  90                                         one) * (ax + one)));
  91                 } else if (n >= SIGP7)
  92                         LD_RE(ans) = logl(ax);
  93                 else
  94                         LD_RE(ans) = __k_clog_rl(x, y, &t);
  95         } else if (n >= HSIGP7)
  96                 LD_RE(ans) = logl(ax);
  97         else if (ix < 0x5f3f0000 && iy >= 0x20bf0000)
  98                 /* 2**-8000 < y < x < 2**8000 */
  99                 LD_RE(ans) = half * logl(ax * ax + ay * ay);
 100         else {
 101                 t = ay / ax;
 102                 LD_RE(ans) = logl(ax) + half * log1pl(t * t);
 103         }
 104         return (ans);
 105 }