Print this page
11210 libm should be cstyle(1ONBLD) clean

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/libm/common/complex/catanl.c
          +++ new/usr/src/lib/libm/common/complex/catanl.c
↓ open down ↓ 14 lines elided ↑ open up ↑
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  24   24   */
       25 +
  25   26  /*
  26   27   * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  27   28   * Use is subject to license terms.
  28   29   */
  29   30  
  30   31  #pragma weak __catanl = catanl
  31   32  
  32      -/* INDENT OFF */
       33 +
  33   34  /*
  34   35   * ldcomplex catanl(ldcomplex z);
  35   36   *
  36   37   * Atan(z) return A + Bi where,
  37   38   *            1
  38   39   *      A = --- * atan2(2x, 1-x*x-y*y)
  39   40   *            2
  40   41   *
  41   42   *            1      [ x*x + (y+1)*(y+1) ]   1               4y
  42   43   *       B = --- log [ ----------------- ] = - log (1+ -----------------)
↓ open down ↓ 8 lines elided ↑ open up ↑
  51   52   * Let p = exp(iw), then z = tan(w) = ((p-1/p)/(p+1/p))/i, or
  52   53   * iz = (p*p-1)/(p*p+1), or, after simplification,
  53   54   *      p*p = (1+iz)/(1-iz)                                 ... (1)
  54   55   * LHS of (1) = exp(2iw) = exp(2i(A+Bi)) = exp(-2B)*exp(2iA)
  55   56   *            = exp(-2B)*(cos(2A)+i*sin(2A))                ... (2)
  56   57   *              1-y+ix   (1-y+ix)*(1+y+ix)   1-x*x-y*y + 2xi
  57   58   * RHS of (1) = ------ = ----------------- = --------------- ... (3)
  58   59   *              1+y-ix    (1+y)**2 + x**2    (1+y)**2 + x**2
  59   60   *
  60   61   * Comparing the real and imaginary parts of (2) and (3), we have:
  61      - *      cos(2A) : 1-x*x-y*y = sin(2A) : 2x
       62 + *      cos(2A) : 1-x*x-y*y = sin(2A) : 2x
  62   63   * and hence
  63   64   *      tan(2A) = 2x/(1-x*x-y*y), or
  64   65   *      A = 0.5 * atan2(2x, 1-x*x-y*y)                      ... (4)
  65   66   *
  66   67   * For the imaginary part B, Note that |p*p| = exp(-2B), and
  67   68   *      |1+iz|   |i-z|   hypot(x,(y-1))
  68   69   *       |----| = |---| = --------------
  69   70   *      |1-iz|   |i+z|   hypot(x,(y+1))
  70   71   * Thus
  71   72   *                 x*x + (y+1)*(y+1)
↓ open down ↓ 20 lines elided ↑ open up ↑
  92   93   *    catan( NaN, 0   ) =  (NaN  ,  0   )
  93   94   *    catan( 0  , 1   ) =  (0    ,  +inf) with divide-by-zero
  94   95   *    catan( inf, y   ) =  (pi/2 ,  0   ) for finite +y
  95   96   *    catan( NaN, y   ) =  (NaN  ,  NaN ) with invalid for finite y != 0
  96   97   *    catan( x  , inf ) =  (pi/2 ,  0   ) for finite +x
  97   98   *    catan( inf, inf ) =  (pi/2 ,  0   )
  98   99   *    catan( NaN, inf ) =  (NaN  ,  0   )
  99  100   *    catan( x  , NaN ) =  (NaN  ,  NaN ) with invalid for finite x
 100  101   *    catan( inf, NaN ) =  (pi/2 ,  +-0 )
 101  102   */
 102      -/* INDENT ON */
 103  103  
 104  104  #include "libm.h"       /* atan2l/atanl/fabsl/isinfl/iszerol/log1pl/logl */
 105  105  #include "complex_wrapper.h"
 106  106  #include "longdouble.h"
 107  107  
 108      -/* INDENT OFF */
 109      -static const long double
 110      -zero = 0.0L,
 111      -one = 1.0L,
 112      -two = 2.0L,
 113      -half = 0.5L,
 114      -ln2 = 6.931471805599453094172321214581765680755e-0001L,
 115      -pi_2 = 1.570796326794896619231321691639751442098584699687552910487472L,
      108 +/* BEGIN CSTYLED */
      109 +static const long double zero = 0.0L,
      110 +        one = 1.0L,
      111 +        two = 2.0L,
      112 +        half = 0.5L,
      113 +        ln2 = 6.931471805599453094172321214581765680755e-0001L,
      114 +        pi_2 = 1.570796326794896619231321691639751442098584699687552910487472L,
 116  115  #if defined(__x86)
 117      -E = 2.910383045673370361328125000000000000000e-11L,     /* 2**-35 */
 118      -Einv = 3.435973836800000000000000000000000000000e+10L;  /* 2**+35 */
      116 +        E = 2.910383045673370361328125000000000000000e-11L,             /* 2**-35 */
      117 +        Einv = 3.435973836800000000000000000000000000000e+10L;  /* 2**+35 */
 119  118  #else
 120      -E = 8.673617379884035472059622406959533691406e-19L,     /* 2**-60 */
 121      -Einv = 1.152921504606846976000000000000000000000e18L;   /* 2**+60 */
      119 +        E = 8.673617379884035472059622406959533691406e-19L,             /* 2**-60 */
      120 +        Einv = 1.152921504606846976000000000000000000000e18L;           /* 2**+60 */
 122  121  #endif
 123      -/* INDENT ON */
      122 +/* END CSTYLED */
 124  123  
 125  124  ldcomplex
 126      -catanl(ldcomplex z) {
      125 +catanl(ldcomplex z)
      126 +{
 127  127          ldcomplex ans;
 128  128          long double x, y, t1, ax, ay, t;
 129  129          int hx, hy, ix, iy;
 130  130  
 131  131          x = LD_RE(z);
 132  132          y = LD_IM(z);
 133  133          ax = fabsl(x);
 134  134          ay = fabsl(y);
 135  135          hx = HI_XWORD(x);
 136  136          hy = HI_XWORD(y);
 137  137          ix = hx & 0x7fffffff;
 138  138          iy = hy & 0x7fffffff;
 139  139  
 140  140          /* x is inf or NaN */
 141  141          if (ix >= 0x7fff0000) {
 142  142                  if (isinfl(x)) {
 143  143                          LD_RE(ans) = pi_2;
 144  144                          LD_IM(ans) = zero;
 145  145                  } else {
 146  146                          LD_RE(ans) = x + x;
      147 +
 147  148                          if (iszerol(y) || (isinfl(y)))
 148  149                                  LD_IM(ans) = zero;
 149  150                          else
 150  151                                  LD_IM(ans) = (fabsl(y) - ay) / (fabsl(y) - ay);
 151  152                  }
 152  153          } else if (iy >= 0x7fff0000) {
 153  154                  /* y is inf or NaN */
 154  155                  if (isinfl(y)) {
 155  156                          LD_RE(ans) = pi_2;
 156  157                          LD_IM(ans) = zero;
 157  158                  } else {
 158  159                          LD_RE(ans) = (fabsl(x) - ax) / (fabsl(x) - ax);
 159  160                          LD_IM(ans) = y;
 160  161                  }
 161  162          } else if (iszerol(x)) {
 162      -                /* INDENT OFF */
      163 +                /* BEGIN CSTYLED */
 163  164                  /*
 164  165                   * x = 0
 165  166                   *      1                            1
 166  167                   * A = --- * atan2(2x, 1-x*x-y*y) = --- atan2(0,1-|y|)
 167  168                   *      2                            2
 168  169                   *
 169  170                   *     1     [ (y+1)*(y+1) ]   1          2      1         2y
 170  171                   * B = - log [ ----------- ] = - log (1+ ---) or - log(1+ ----)
 171  172                   *     4     [ (y-1)*(y-1) ]   2         y-1     2         1-y
 172  173                   */
 173      -                /* INDENT ON */
      174 +                /* END CSTYLED */
 174  175                  t = one - ay;
      176 +
 175  177                  if (ay == one) {
 176  178                          /* y=1: catan(0,1)=(0,+inf) with 1/0 signal */
 177  179                          LD_IM(ans) = ay / ax;
 178  180                          LD_RE(ans) = zero;
 179  181                  } else if (ay > one) {  /* y>1 */
 180  182                          LD_IM(ans) = half * log1pl(two / (-t));
 181  183                          LD_RE(ans) = pi_2;
 182  184                  } else {                /* y<1 */
 183  185                          LD_IM(ans) = half * log1pl((ay + ay) / t);
 184  186                          LD_RE(ans) = zero;
 185  187                  }
 186  188          } else if (ay < E * (one + ax)) {
 187      -                /* INDENT OFF */
      189 +                /* BEGIN CSTYLED */
 188  190                  /*
 189  191                   * Tiny y (relative to 1+|x|)
 190  192                   *     |y| < E*(1+|x|)
 191  193                   * where E=2**-29, -35, -60 for double, extended, quad precision
 192  194                   *
 193  195                   *     1                         [x<=1:   atan(x)
 194  196                   * A = - * atan2(2x,1-x*x-y*y) ~ [      1                 1+x
 195  197                   *     2                         [x>=1: - atan2(2,(1-x)*(-----))
 196  198                   *                                      2                  x
 197  199                   *
 198  200                   *                               y/x
 199  201                   * B ~ t*(1-2t), where t = ----------------- is tiny
 200  202                   *                         x + (y-1)*(y-1)/x
 201  203                   *
 202  204                   *                           y
 203  205                   * (when x < 2**-60, t = ----------- )
 204  206                   *                       (y-1)*(y-1)
 205  207                   */
 206      -                /* INDENT ON */
 207      -                if (ay == zero)
      208 +                /* END CSTYLED */
      209 +                if (ay == zero) {
 208  210                          LD_IM(ans) = ay;
 209      -                else {
      211 +                } else {
 210  212                          t1 = ay - one;
      213 +
 211  214                          if (ix < 0x3fc30000)
 212  215                                  t = ay / (t1 * t1);
 213  216                          else if (ix > 0x403b0000)
 214  217                                  t = (ay / ax) / ax;
 215  218                          else
 216  219                                  t = ay / (ax * ax + t1 * t1);
      220 +
 217  221                          LD_IM(ans) = t * (one - two * t);
 218  222                  }
      223 +
 219  224                  if (ix < 0x3fff0000)
 220  225                          LD_RE(ans) = atanl(ax);
 221  226                  else
 222  227                          LD_RE(ans) = half * atan2l(two, (one - ax) * (one +
 223      -                                one / ax));
 224      -
      228 +                            one / ax));
 225  229          } else if (ay > Einv * (one + ax)) {
 226      -                /* INDENT OFF */
      230 +                /* BEGIN CSTYLED */
 227  231                  /*
 228  232                   * Huge y relative to 1+|x|
 229  233                   *     |y| > Einv*(1+|x|), where Einv~2**(prec/2+3),
 230  234                   *      1
 231  235                   * A ~ --- * atan2(2x, -y*y) ~ pi/2
 232  236                   *      2
 233  237                   *                               y
 234  238                   * B ~ t*(1-2t), where t = --------------- is tiny
 235  239                   *                          (y-1)*(y-1)
 236  240                   */
 237      -                /* INDENT ON */
      241 +                /* END CSTYLED */
 238  242                  LD_RE(ans) = pi_2;
 239  243                  t = (ay / (ay - one)) / (ay - one);
 240  244                  LD_IM(ans) = t * (one - (t + t));
 241  245          } else if (ay == one) {
 242      -                /* INDENT OFF */
      246 +                /* BEGIN CSTYLED */
 243  247                  /*
 244  248                   * y=1
 245  249                   *     1                      1
 246  250                   * A = - * atan2(2x, -x*x) = --- atan2(2,-x)
 247  251                   *     2                      2
 248  252                   *
 249  253                   *     1     [ x*x+4]   1          4     [ 0.5(log2-logx) if
 250  254                   * B = - log [ -----] = - log (1+ ---) = [ |x|<E, else 0.25*
 251  255                   *     4     [  x*x ]   4         x*x    [ log1p((2/x)*(2/x))
 252  256                   */
 253      -                /* INDENT ON */
      257 +                /* END CSTYLED */
 254  258                  LD_RE(ans) = half * atan2l(two, -ax);
 255      -                if (ax < E)
      259 +
      260 +                if (ax < E) {
 256  261                          LD_IM(ans) = half * (ln2 - logl(ax));
 257      -                else {
      262 +                } else {
 258  263                          t = two / ax;
 259  264                          LD_IM(ans) = 0.25L * log1pl(t * t);
 260  265                  }
 261  266          } else if (ax > Einv * Einv) {
 262      -                /* INDENT OFF */
      267 +                /* BEGIN CSTYLED */
 263  268                  /*
 264  269                   * Huge x:
 265  270                   * when |x| > 1/E^2,
 266  271                   *      1                           pi
 267  272                   * A ~ --- * atan2(2x, -x*x-y*y) ~ ---
 268  273                   *      2                           2
 269  274                   *                               y                 y/x
 270  275                   * B ~ t*(1-2t), where t = --------------- = (-------------- )/x
 271  276                   *                         x*x+(y-1)*(y-1)     1+((y-1)/x)^2
 272  277                   */
 273      -                /* INDENT ON */
      278 +                /* END CSTYLED */
 274  279                  LD_RE(ans) = pi_2;
 275  280                  t = ((ay / ax) / (one + ((ay - one) / ax) * ((ay - one) /
 276      -                        ax))) / ax;
      281 +                    ax))) / ax;
 277  282                  LD_IM(ans) = t * (one - (t + t));
 278  283          } else if (ax < E * E * E * E) {
 279      -                /* INDENT OFF */
      284 +                /* BEGIN CSTYLED */
 280  285                  /*
 281  286                   * Tiny x:
 282  287                   * when |x| < E^4,  (note that y != 1)
 283  288                   *      1                            1
 284  289                   * A = --- * atan2(2x, 1-x*x-y*y) ~ --- * atan2(2x,1-y*y)
 285  290                   *      2                            2
 286  291                   *
 287  292                   *     1     [ (y+1)*(y+1) ]   1          2      1         2y
 288  293                   * B = - log [ ----------- ] = - log (1+ ---) or - log(1+ ----)
 289  294                   *     4     [ (y-1)*(y-1) ]   2         y-1     2         1-y
 290  295                   */
 291      -                /* INDENT ON */
      296 +                /* END CSTYLED */
 292  297                  LD_RE(ans) = half * atan2l(ax + ax, (one - ay) * (one + ay));
 293      -                if (ay > one)   /* y>1 */
      298 +
      299 +                if (ay > one)           /* y>1 */
 294  300                          LD_IM(ans) = half * log1pl(two / (ay - one));
 295      -                else            /* y<1 */
      301 +                else                    /* y<1 */
 296  302                          LD_IM(ans) = half * log1pl((ay + ay) / (one - ay));
 297  303          } else {
 298      -                /* INDENT OFF */
      304 +                /* BEGIN CSTYLED */
 299  305                  /*
 300  306                   * normal x,y
 301  307                   *      1
 302  308                   * A = --- * atan2(2x, 1-x*x-y*y)
 303  309                   *      2
 304  310                   *
 305  311                   *     1     [ x*x+(y+1)*(y+1) ]   1               4y
 306  312                   * B = - log [ --------------- ] = - log (1+ -----------------)
 307  313                   *     4     [ x*x+(y-1)*(y-1) ]   4         x*x + (y-1)*(y-1)
 308  314                   */
 309      -                /* INDENT ON */
      315 +                /* END CSTYLED */
 310  316                  t = one - ay;
      317 +
 311  318                  if (iy >= 0x3ffe0000 && iy < 0x40000000) {
 312  319                          /* y close to 1 */
 313      -                        LD_RE(ans) = half * (atan2l((ax + ax), (t * (one +
 314      -                                ay) - ax * ax)));
      320 +                        LD_RE(ans) = half * (atan2l((ax + ax), (t * (one + ay) -
      321 +                            ax * ax)));
 315  322                  } else if (ix >= 0x3ffe0000 && ix < 0x40000000) {
 316  323                          /* x close to 1 */
 317  324                          LD_RE(ans) = half * atan2l((ax + ax), ((one - ax) *
 318      -                                (one + ax) - ay * ay));
 319      -                } else
 320      -                        LD_RE(ans) = half * atan2l((ax + ax), ((one - ax *
 321      -                                ax) - ay * ay));
      325 +                            (one + ax) - ay * ay));
      326 +                } else {
      327 +                        LD_RE(ans) = half * atan2l((ax + ax), ((one - ax * ax) -
      328 +                            ay * ay));
      329 +                }
      330 +
 322  331                  LD_IM(ans) = 0.25L * log1pl((4.0L * ay) / (ax * ax + t * t));
 323  332          }
      333 +
 324  334          if (hx < 0)
 325  335                  LD_RE(ans) = -LD_RE(ans);
      336 +
 326  337          if (hy < 0)
 327  338                  LD_IM(ans) = -LD_IM(ans);
      339 +
 328  340          return (ans);
 329  341  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX