k_expf.c revision 226597
1226597Sdas/*- 2226597Sdas * Copyright (c) 2011 David Schultz <das@FreeBSD.ORG> 3226597Sdas * All rights reserved. 4226597Sdas * 5226597Sdas * Redistribution and use in source and binary forms, with or without 6226597Sdas * modification, are permitted provided that the following conditions 7226597Sdas * are met: 8226597Sdas * 1. Redistributions of source code must retain the above copyright 9226597Sdas * notice, this list of conditions and the following disclaimer. 10226597Sdas * 2. Redistributions in binary form must reproduce the above copyright 11226597Sdas * notice, this list of conditions and the following disclaimer in the 12226597Sdas * documentation and/or other materials provided with the distribution. 13226597Sdas * 14226597Sdas * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15226597Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16226597Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17226597Sdas * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18226597Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19226597Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20226597Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21226597Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22226597Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23226597Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24226597Sdas * SUCH DAMAGE. 25226597Sdas */ 26226597Sdas 27226597Sdas#include <sys/cdefs.h> 28226597Sdas__FBSDID("$FreeBSD: head/lib/msun/src/k_expf.c 226597 2011-10-21 06:27:56Z das $"); 29226597Sdas 30226597Sdas#include <complex.h> 31226597Sdas 32226597Sdas#include "math.h" 33226597Sdas#include "math_private.h" 34226597Sdas 35226597Sdasstatic const uint32_t k = 235; /* constant for reduction */ 36226597Sdasstatic const float kln2 = 162.88958740F; /* k * ln2 */ 37226597Sdas 38226597Sdas/* 39226597Sdas * See k_exp.c for details. 40226597Sdas * 41226597Sdas * Input: ln(FLT_MAX) <= x < ln(2 * FLT_MAX / FLT_MIN_DENORM) ~= 192.7 42226597Sdas * Output: 2**127 <= y < 2**128 43226597Sdas */ 44226597Sdasstatic float 45226597Sdas__frexp_expf(float x, int *expt) 46226597Sdas{ 47226597Sdas double exp_x; 48226597Sdas uint32_t hx; 49226597Sdas 50226597Sdas exp_x = expf(x - kln2); 51226597Sdas GET_FLOAT_WORD(hx, exp_x); 52226597Sdas *expt = (hx >> 23) - (0x7f + 127) + k; 53226597Sdas SET_FLOAT_WORD(exp_x, (hx & 0x7fffff) | ((0x7f + 127) << 23)); 54226597Sdas return (exp_x); 55226597Sdas} 56226597Sdas 57226597Sdasfloat 58226597Sdas__ldexp_expf(float x, int expt) 59226597Sdas{ 60226597Sdas float exp_x, scale; 61226597Sdas int ex_expt; 62226597Sdas 63226597Sdas exp_x = __frexp_expf(x, &ex_expt); 64226597Sdas expt += ex_expt; 65226597Sdas SET_FLOAT_WORD(scale, (0x7f + expt) << 23); 66226597Sdas return (exp_x * scale); 67226597Sdas} 68226597Sdas 69226597Sdasfloat complex 70226597Sdas__ldexp_cexpf(float complex z, int expt) 71226597Sdas{ 72226597Sdas float x, y, exp_x, scale1, scale2; 73226597Sdas int ex_expt, half_expt; 74226597Sdas 75226597Sdas x = crealf(z); 76226597Sdas y = cimagf(z); 77226597Sdas exp_x = __frexp_expf(x, &ex_expt); 78226597Sdas expt += ex_expt; 79226597Sdas 80226597Sdas half_expt = expt / 2; 81226597Sdas SET_FLOAT_WORD(scale1, (0x7f + half_expt) << 23); 82226597Sdas half_expt = expt - half_expt; 83226597Sdas SET_FLOAT_WORD(scale2, (0x7f + half_expt) << 23); 84226597Sdas 85226597Sdas return (cpackf(cosf(y) * exp_x * scale1 * scale2, 86226597Sdas sinf(y) * exp_x * scale1 * scale2)); 87226597Sdas} 88