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