2021-08-16 22:26:31 +00:00
|
|
|
|
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
|
|
|
|
|
│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│
|
|
|
|
|
╚──────────────────────────────────────────────────────────────────────────────╝
|
2021-02-28 09:06:00 +00:00
|
|
|
|
│ │
|
2023-05-14 16:32:15 +00:00
|
|
|
|
│ FreeBSD lib/msun/src/s_tanhf.c │
|
|
|
|
|
│ Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. │
|
2021-08-16 22:26:31 +00:00
|
|
|
|
│ │
|
2023-05-15 08:51:29 +00:00
|
|
|
|
│ Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. │
|
|
|
|
|
│ │
|
|
|
|
|
│ Developed at SunPro, a Sun Microsystems, Inc. business. │
|
|
|
|
|
│ Permission to use, copy, modify, and distribute this │
|
|
|
|
|
│ software is freely granted, provided that this notice │
|
|
|
|
|
│ is preserved. │
|
|
|
|
|
│ │
|
2023-05-14 16:32:15 +00:00
|
|
|
|
│ Copyright (c) 1992-2023 The FreeBSD Project. │
|
2021-08-16 22:26:31 +00:00
|
|
|
|
│ │
|
2023-05-14 16:32:15 +00:00
|
|
|
|
│ Redistribution and use in source and binary forms, with or without │
|
|
|
|
|
│ modification, are permitted provided that the following conditions │
|
|
|
|
|
│ are met: │
|
|
|
|
|
│ 1. Redistributions of source code must retain the above copyright │
|
|
|
|
|
│ notice, this list of conditions and the following disclaimer. │
|
|
|
|
|
│ 2. Redistributions in binary form must reproduce the above copyright │
|
|
|
|
|
│ notice, this list of conditions and the following disclaimer in the │
|
|
|
|
|
│ documentation and/or other materials provided with the distribution. │
|
2021-08-16 22:26:31 +00:00
|
|
|
|
│ │
|
2023-05-14 16:32:15 +00:00
|
|
|
|
│ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND │
|
|
|
|
|
│ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE │
|
|
|
|
|
│ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE │
|
|
|
|
|
│ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE │
|
|
|
|
|
│ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL │
|
|
|
|
|
│ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS │
|
|
|
|
|
│ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) │
|
|
|
|
|
│ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT │
|
|
|
|
|
│ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY │
|
|
|
|
|
│ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF │
|
|
|
|
|
│ SUCH DAMAGE. │
|
|
|
|
|
│ │
|
2021-02-28 09:06:00 +00:00
|
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
|
#include "libc/math.h"
|
2023-05-14 16:32:15 +00:00
|
|
|
|
#include "libc/tinymath/freebsd.internal.h"
|
2021-08-16 22:26:31 +00:00
|
|
|
|
|
|
|
|
|
asm(".ident\t\"\\n\\n\
|
2023-05-14 16:32:15 +00:00
|
|
|
|
FreeBSD libm (BSD-2 License)\\n\
|
|
|
|
|
Copyright (c) 2005-2011, Bruce D. Evans, Steven G. Kargl, David Schultz.\"");
|
|
|
|
|
asm(".ident\t\"\\n\\n\
|
|
|
|
|
fdlibm (fdlibm license)\\n\
|
|
|
|
|
Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.\"");
|
2021-08-16 22:26:31 +00:00
|
|
|
|
asm(".include \"libc/disclaimer.inc\"");
|
2023-05-14 16:32:15 +00:00
|
|
|
|
// clang-format off
|
|
|
|
|
|
|
|
|
|
static const volatile float tiny = 1.0e-30;
|
|
|
|
|
static const float one=1.0, two=2.0, huge = 1.0e30;
|
2021-02-28 09:06:00 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns hyperbolic tangent of 𝑥.
|
|
|
|
|
*/
|
2023-05-14 16:32:15 +00:00
|
|
|
|
float
|
|
|
|
|
tanhf(float x)
|
2021-08-16 22:26:31 +00:00
|
|
|
|
{
|
2023-05-14 16:32:15 +00:00
|
|
|
|
float t,z;
|
|
|
|
|
int32_t jx,ix;
|
2021-08-16 22:26:31 +00:00
|
|
|
|
|
2023-05-14 16:32:15 +00:00
|
|
|
|
GET_FLOAT_WORD(jx,x);
|
|
|
|
|
ix = jx&0x7fffffff;
|
|
|
|
|
|
|
|
|
|
/* x is INF or NaN */
|
|
|
|
|
if(ix>=0x7f800000) {
|
|
|
|
|
if (jx>=0) return one/x+one; /* tanh(+-inf)=+-1 */
|
|
|
|
|
else return one/x-one; /* tanh(NaN) = NaN */
|
|
|
|
|
}
|
2021-08-16 22:26:31 +00:00
|
|
|
|
|
2023-05-14 16:32:15 +00:00
|
|
|
|
/* |x| < 9 */
|
|
|
|
|
if (ix < 0x41100000) { /* |x|<9 */
|
|
|
|
|
if (ix<0x39800000) { /* |x|<2**-12 */
|
|
|
|
|
if(huge+x>one) return x; /* tanh(tiny) = tiny with inexact */
|
|
|
|
|
}
|
|
|
|
|
if (ix>=0x3f800000) { /* |x|>=1 */
|
|
|
|
|
t = expm1f(two*fabsf(x));
|
|
|
|
|
z = one - two/(t+two);
|
|
|
|
|
} else {
|
|
|
|
|
t = expm1f(-two*fabsf(x));
|
|
|
|
|
z= -t/(t+two);
|
|
|
|
|
}
|
|
|
|
|
/* |x| >= 9, return +-1 */
|
2021-08-16 22:26:31 +00:00
|
|
|
|
} else {
|
2023-05-14 16:32:15 +00:00
|
|
|
|
z = one - tiny; /* raise inexact flag */
|
2021-08-16 22:26:31 +00:00
|
|
|
|
}
|
2023-05-14 16:32:15 +00:00
|
|
|
|
return (jx>=0)? z: -z;
|
2021-02-28 09:06:00 +00:00
|
|
|
|
}
|