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 __asinh = asinh
  32 
  33 
  34 /*
  35  * asinh(x)
  36  * Method :
  37  *      Based on
  38  *              asinh(x) = sign(x) * log [ |x| + sqrt(x*x+1) ]
  39  *      we have
  40  *      asinh(x) := x  if  1+x*x == 1,
  41  *               := sign(x)*(log(x)+ln2)) for large |x|, else
  42  *               := sign(x)*log(2|x|+1/(|x|+sqrt(x*x+1))) if|x| > 2, else
  43  *               := sign(x)*log1p(|x|+x^2/(1+sqrt(1+x^2)))
  44  */
  45 
  46 #include "libm_macros.h"
  47 #include <math.h>
  48 
  49 static const double xxx[] = {
  50 /* one */
  51         1.00000000000000000000e+00,     /* 3FF00000, 00000000 */
  52 /* ln2 */ 6.93147180559945286227e-01,   /* 3FE62E42, FEFA39EF */
  53 /* huge */ 1.00000000000000000000e+300
  54 };
  55 
  56 #define one             xxx[0]
  57 #define ln2             xxx[1]
  58 #define huge            xxx[2]
  59 
  60 double
  61 asinh(double x)
  62 {
  63         double t, w;
  64         int hx, ix;
  65 
  66         hx = ((int *)&x)[HIWORD];
  67         ix = hx & 0x7fffffff;
  68 
  69         if (ix >= 0x7ff00000)
  70 #if defined(FPADD_TRAPS_INCOMPLETE_ON_NAN)
  71                 return (ix >= 0x7ff80000 ? x : x + x);
  72 
  73         /* assumes sparc-like QNaN */
  74 #else
  75                 return (x + x);         /* x is inf or NaN */
  76 #endif
  77 
  78         if (ix < 0x3e300000) {               /* |x|<2**-28 */
  79                 if (huge + x > one)
  80                         return (x);     /* return x inexact except 0 */
  81         }
  82 
  83         if (ix > 0x41b00000) {               /* |x| > 2**28 */
  84                 w = log(fabs(x)) + ln2;
  85         } else if (ix > 0x40000000) {
  86                 /* 2**28 > |x| > 2.0 */
  87                 t = fabs(x);
  88                 w = log(2.0 * t + one / (sqrt(x * x + one) + t));
  89         } else {
  90                 /* 2.0 > |x| > 2**-28 */
  91                 t = x * x;
  92                 w = log1p(fabs(x) + t / (one + sqrt(one + t)));
  93         }
  94 
  95         return (hx > 0 ? w : -w);
  96 }