Print this page
11210 libm should be cstyle(1ONBLD) clean
   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  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  23  */

  24 /*
  25  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 
  29 #pragma weak clog = __clog
  30 
  31 /* INDENT OFF */
  32 /*
  33  * dcomplex clog(dcomplex z);
  34  *
  35  *                    _________
  36  *                   / 2    2            -1   y
  37  * log(x+iy) = log(\/ x  + y    ) + i tan   (---)
  38  *                                            x
  39  *
  40  *              1       2    2         -1   y
  41  *           = --- log(x  + y ) + i tan   (---)
  42  *              2                           x
  43  *
  44  * Note that the arctangent ranges from -PI to +PI, thus the imaginary
  45  * part of clog is atan2(y,x).
  46  *
  47  * EXCEPTION CASES (conform to ISO/IEC 9899:1999(E)):
  48  *    clog(-0 + i 0   ) =  -inf + i pi
  49  *    clog( 0 + i 0   ) =  -inf + i 0
  50  *    clog( x + i inf ) =  -inf + i pi/2, for finite x
  51  *    clog( x + i NaN ) =  NaN  + i NaN with invalid for finite x
  52  *    clog(-inf + iy   )=  +inf + i pi, for finite positive-signed y
  53  *    clog(+inf + iy   )=  +inf + i 0 , for finite positive-signed y
  54  *    clog(-inf + i inf)=  inf  + i 3pi/4
  55  *    clog(+inf + i inf)=  inf  + i pi/4
  56  *    clog(+-inf+ i NaN)=  inf  + i NaN
  57  *    clog(NaN  + i y  )=  NaN  + i NaN for finite y
  58  *    clog(NaN  + i inf)=  inf  + i NaN
  59  *    clog(NaN  + i NaN)=  NaN  + i NaN
  60  */
  61 /* INDENT ON */
  62 
  63 #include <math.h>         /* atan2/fabs/log/log1p */
  64 #include "complex_wrapper.h"
  65 #include "libm_protos.h"        /* __k_clog_r */
  66 
  67 
  68 static const double half = 0.5, one = 1.0;
  69 
  70 dcomplex
  71 __clog(dcomplex z) {

  72         dcomplex        ans;
  73         double          x, y, t, ax, ay, w;
  74         int             n, ix, iy, hx, hy;
  75         unsigned        lx, ly;
  76 
  77         x = D_RE(z);
  78         y = D_IM(z);
  79         hx = HI_WORD(x);
  80         lx = LO_WORD(x);
  81         hy = HI_WORD(y);
  82         ly = LO_WORD(y);
  83         ix = hx & 0x7fffffff;
  84         iy = hy & 0x7fffffff;
  85         ay = fabs(y);
  86         ax = fabs(x);
  87         D_IM(ans) = carg(z);

  88         if (ix < iy || (ix == iy && lx < ly)) {
  89                 /* swap x and y to force ax >= ay */
  90                 t = ax;
  91                 ax = ay;
  92                 ay = t;
  93                 n = ix, ix = iy;
  94                 iy = n;
  95                 n = lx, lx = ly;
  96                 ly = n;
  97         }

  98         n = (ix - iy) >> 20;

  99         if (ix >= 0x7ff00000) {      /* x or y is Inf or NaN */
 100                 if (ISINF(ix, lx))
 101                         D_RE(ans) = ax;
 102                 else if (ISINF(iy, ly))
 103                         D_RE(ans) = ay;
 104                 else
 105                         D_RE(ans) = ax * ay;
 106         } else if ((iy | ly) == 0) {
 107                 D_RE(ans) = ((ix | lx) == 0)? -one / ax : log(ax);
 108         } else if (((0x3fffffff - ix) ^ (ix - 0x3fe00000)) >= 0) {
 109                 /* 0.5 <= x < 2 */
 110                 if (ix >= 0x3ff00000) {
 111                         if (((ix - 0x3ff00000) | lx) == 0)
 112                                 D_RE(ans) = half * log1p(ay * ay);
 113                         else if (n >= 60)
 114                                 D_RE(ans) = log(ax);
 115                         else
 116                                 D_RE(ans) = half * (log1p(ay * ay + (ax -
 117                                     one) * (ax + one)));
 118                 } else if (n >= 60) {
 119                         D_RE(ans) = log(ax);
 120                 } else {
 121                         D_RE(ans) = __k_clog_r(ax, ay, &w);
 122                 }
 123         } else if (n >= 30) {
 124                 D_RE(ans) = log(ax);
 125         } else if (ix < 0x5f300000 && iy >= 0x20b00000) {
 126                 /* 2**-500< y < x < 2**500 */
 127                 D_RE(ans) = half * log(ax * ax + ay * ay);
 128         } else {
 129                 t = ay / ax;
 130                 D_RE(ans) = log(ax) + half * log1p(t * t);
 131         }

 132         return (ans);
 133 }
   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 2005 Sun Microsystems, Inc.  All rights reserved.
  28  * Use is subject to license terms.
  29  */
  30 
  31 #pragma weak clog = __clog
  32 
  33 
  34 /*
  35  * dcomplex clog(dcomplex z);
  36  *
  37  *                    _________
  38  *                   / 2    2            -1   y
  39  * log(x+iy) = log(\/ x  + y    ) + i tan   (---)
  40  *                                            x
  41  *
  42  *              1       2    2         -1   y
  43  *           = --- log(x  + y ) + i tan   (---)
  44  *              2                           x
  45  *
  46  * Note that the arctangent ranges from -PI to +PI, thus the imaginary
  47  * part of clog is atan2(y,x).
  48  *
  49  * EXCEPTION CASES (conform to ISO/IEC 9899:1999(E)):
  50  *    clog(-0 + i 0   ) =  -inf + i pi
  51  *    clog( 0 + i 0   ) =  -inf + i 0
  52  *    clog( x + i inf ) =  -inf + i pi/2, for finite x
  53  *    clog( x + i NaN ) =  NaN  + i NaN with invalid for finite x
  54  *    clog(-inf + iy   )=  +inf + i pi, for finite positive-signed y
  55  *    clog(+inf + iy   )=  +inf + i 0 , for finite positive-signed y
  56  *    clog(-inf + i inf)=  inf  + i 3pi/4
  57  *    clog(+inf + i inf)=  inf  + i pi/4
  58  *    clog(+-inf+ i NaN)=  inf  + i NaN
  59  *    clog(NaN  + i y  )=  NaN  + i NaN for finite y
  60  *    clog(NaN  + i inf)=  inf  + i NaN
  61  *    clog(NaN  + i NaN)=  NaN  + i NaN
  62  */

  63 
  64 #include <math.h>                 /* atan2/fabs/log/log1p */
  65 #include "complex_wrapper.h"
  66 #include "libm_protos.h"                /* __k_clog_r */
  67 

  68 static const double half = 0.5, one = 1.0;
  69 
  70 dcomplex
  71 __clog(dcomplex z)
  72 {
  73         dcomplex ans;
  74         double x, y, t, ax, ay, w;
  75         int n, ix, iy, hx, hy;
  76         unsigned lx, ly;
  77 
  78         x = D_RE(z);
  79         y = D_IM(z);
  80         hx = HI_WORD(x);
  81         lx = LO_WORD(x);
  82         hy = HI_WORD(y);
  83         ly = LO_WORD(y);
  84         ix = hx & 0x7fffffff;
  85         iy = hy & 0x7fffffff;
  86         ay = fabs(y);
  87         ax = fabs(x);
  88         D_IM(ans) = carg(z);
  89 
  90         if (ix < iy || (ix == iy && lx < ly)) {
  91                 /* swap x and y to force ax >= ay */
  92                 t = ax;
  93                 ax = ay;
  94                 ay = t;
  95                 n = ix, ix = iy;
  96                 iy = n;
  97                 n = lx, lx = ly;
  98                 ly = n;
  99         }
 100 
 101         n = (ix - iy) >> 20;
 102 
 103         if (ix >= 0x7ff00000) {              /* x or y is Inf or NaN */
 104                 if (ISINF(ix, lx))
 105                         D_RE(ans) = ax;
 106                 else if (ISINF(iy, ly))
 107                         D_RE(ans) = ay;
 108                 else
 109                         D_RE(ans) = ax * ay;
 110         } else if ((iy | ly) == 0) {
 111                 D_RE(ans) = ((ix | lx) == 0) ? -one / ax : log(ax);
 112         } else if (((0x3fffffff - ix) ^ (ix - 0x3fe00000)) >= 0) {
 113                 /* 0.5 <= x < 2 */
 114                 if (ix >= 0x3ff00000) {
 115                         if (((ix - 0x3ff00000) | lx) == 0)
 116                                 D_RE(ans) = half * log1p(ay * ay);
 117                         else if (n >= 60)
 118                                 D_RE(ans) = log(ax);
 119                         else
 120                                 D_RE(ans) = half * (log1p(ay * ay + (ax - one) *
 121                                     (ax + one)));
 122                 } else if (n >= 60) {
 123                         D_RE(ans) = log(ax);
 124                 } else {
 125                         D_RE(ans) = __k_clog_r(ax, ay, &w);
 126                 }
 127         } else if (n >= 30) {
 128                 D_RE(ans) = log(ax);
 129         } else if (ix < 0x5f300000 && iy >= 0x20b00000) {
 130                 /* 2**-500< y < x < 2**500 */
 131                 D_RE(ans) = half * log(ax * ax + ay * ay);
 132         } else {
 133                 t = ay / ax;
 134                 D_RE(ans) = log(ax) + half * log1p(t * t);
 135         }
 136 
 137         return (ans);
 138 }