/* origin: FreeBSD /usr/src/lib/msun/src/k_exp.c */1/*-2* Copyright (c) 2011 David Schultz <[email protected]>3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include "complex_impl.h"2829static const uint32_t k = 1799; /* constant for reduction */30static const double kln2 = 1246.97177782734161156; /* k * ln2 */3132/*33* Compute exp(x), scaled to avoid spurious overflow. An exponent is34* returned separately in 'expt'.35*36* Input: ln(DBL_MAX) <= x < ln(2 * DBL_MAX / DBL_MIN_DENORM) ~= 1454.9137* Output: 2**1023 <= y < 2**102438*/39static double __frexp_exp(double x, int *expt)40{41double exp_x;42uint32_t hx;4344/*45* We use exp(x) = exp(x - kln2) * 2**k, carefully chosen to46* minimize |exp(kln2) - 2**k|. We also scale the exponent of47* exp_x to MAX_EXP so that the result can be multiplied by48* a tiny number without losing accuracy due to denormalization.49*/50exp_x = exp(x - kln2);51GET_HIGH_WORD(hx, exp_x);52*expt = (hx >> 20) - (0x3ff + 1023) + k;53SET_HIGH_WORD(exp_x, (hx & 0xfffff) | ((0x3ff + 1023) << 20));54return exp_x;55}5657/*58* __ldexp_cexp(x, expt) compute exp(x) * 2**expt.59* It is intended for large arguments (real part >= ln(DBL_MAX))60* where care is needed to avoid overflow.61*62* The present implementation is narrowly tailored for our hyperbolic and63* exponential functions. We assume expt is small (0 or -1), and the caller64* has filtered out very large x, for which overflow would be inevitable.65*/66_Dcomplex __ldexp_cexp(_Dcomplex z, int expt)67{68double x, y, exp_x, scale1, scale2;69int ex_expt, half_expt;7071x = creal(z);72y = cimag(z);73exp_x = __frexp_exp(x, &ex_expt);74expt += ex_expt;7576/*77* Arrange so that scale1 * scale2 == 2**expt. We use this to78* compensate for scalbn being horrendously slow.79*/80half_expt = expt / 2;81INSERT_WORDS(scale1, (0x3ff + half_expt) << 20, 0);82half_expt = expt - half_expt;83INSERT_WORDS(scale2, (0x3ff + half_expt) << 20, 0);8485return CMPLX(cos(y) * exp_x * scale1 * scale2, sin(y) * exp_x * scale1 * scale2);86}878889