blob: e56813adfdc2ce46794475c5ac8413ab4067100f [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* s_tanhf.c -- float version of s_tanh.c.
2 * Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com.
3 */
4
5/*
6 * ====================================================
7 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
8 *
9 * Developed at SunPro, a Sun Microsystems, Inc. business.
10 * Permission to use, copy, modify, and distribute this
11 * software is freely granted, provided that this notice
12 * is preserved.
13 * ====================================================
14 */
15
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080016#include "math.h"
17#include "math_private.h"
18
Elliott Hughes460ad742014-09-12 14:00:02 -070019static const volatile float tiny = 1.0e-30;
20static const float one=1.0, two=2.0, huge = 1.0e30;
21
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080022float
23tanhf(float x)
24{
25 float t,z;
26 int32_t jx,ix;
27
28 GET_FLOAT_WORD(jx,x);
29 ix = jx&0x7fffffff;
30
31 /* x is INF or NaN */
32 if(ix>=0x7f800000) {
33 if (jx>=0) return one/x+one; /* tanh(+-inf)=+-1 */
34 else return one/x-one; /* tanh(NaN) = NaN */
35 }
36
37 /* |x| < 9 */
38 if (ix < 0x41100000) { /* |x|<9 */
39 if (ix<0x39800000) { /* |x|<2**-12 */
40 if(huge+x>one) return x; /* tanh(tiny) = tiny with inexact */
41 }
42 if (ix>=0x3f800000) { /* |x|>=1 */
43 t = expm1f(two*fabsf(x));
44 z = one - two/(t+two);
45 } else {
46 t = expm1f(-two*fabsf(x));
47 z= -t/(t+two);
48 }
49 /* |x| >= 9, return +-1 */
50 } else {
51 z = one - tiny; /* raise inexact flag */
52 }
53 return (jx>=0)? z: -z;
54}