xref: /freebsd/lib/msun/src/k_expf.c (revision 0dd5a5603e7a33d976f8e6015620bbc79839c609)
112188b77SDavid Schultz /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
412188b77SDavid Schultz  * Copyright (c) 2011 David Schultz <das@FreeBSD.ORG>
512188b77SDavid Schultz  * All rights reserved.
612188b77SDavid Schultz  *
712188b77SDavid Schultz  * Redistribution and use in source and binary forms, with or without
812188b77SDavid Schultz  * modification, are permitted provided that the following conditions
912188b77SDavid Schultz  * are met:
1012188b77SDavid Schultz  * 1. Redistributions of source code must retain the above copyright
1112188b77SDavid Schultz  *    notice, this list of conditions and the following disclaimer.
1212188b77SDavid Schultz  * 2. Redistributions in binary form must reproduce the above copyright
1312188b77SDavid Schultz  *    notice, this list of conditions and the following disclaimer in the
1412188b77SDavid Schultz  *    documentation and/or other materials provided with the distribution.
1512188b77SDavid Schultz  *
1612188b77SDavid Schultz  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1712188b77SDavid Schultz  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1812188b77SDavid Schultz  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1912188b77SDavid Schultz  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2012188b77SDavid Schultz  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2112188b77SDavid Schultz  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2212188b77SDavid Schultz  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2312188b77SDavid Schultz  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2412188b77SDavid Schultz  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2512188b77SDavid Schultz  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2612188b77SDavid Schultz  * SUCH DAMAGE.
2712188b77SDavid Schultz  */
2812188b77SDavid Schultz 
2912188b77SDavid Schultz #include <complex.h>
3012188b77SDavid Schultz 
3112188b77SDavid Schultz #include "math.h"
3212188b77SDavid Schultz #include "math_private.h"
3312188b77SDavid Schultz 
3412188b77SDavid Schultz static const uint32_t k = 235;			/* constant for reduction */
3512188b77SDavid Schultz static const float kln2 =  162.88958740F;	/* k * ln2 */
3612188b77SDavid Schultz 
3712188b77SDavid Schultz /*
3812188b77SDavid Schultz  * See k_exp.c for details.
3912188b77SDavid Schultz  *
4012188b77SDavid Schultz  * Input:  ln(FLT_MAX) <= x < ln(2 * FLT_MAX / FLT_MIN_DENORM) ~= 192.7
4112188b77SDavid Schultz  * Output: 2**127 <= y < 2**128
4212188b77SDavid Schultz  */
4312188b77SDavid Schultz static float
__frexp_expf(float x,int * expt)4412188b77SDavid Schultz __frexp_expf(float x, int *expt)
4512188b77SDavid Schultz {
466e3ab4e1SDavid Schultz 	float exp_x;
4712188b77SDavid Schultz 	uint32_t hx;
4812188b77SDavid Schultz 
4912188b77SDavid Schultz 	exp_x = expf(x - kln2);
5012188b77SDavid Schultz 	GET_FLOAT_WORD(hx, exp_x);
5112188b77SDavid Schultz 	*expt = (hx >> 23) - (0x7f + 127) + k;
5212188b77SDavid Schultz 	SET_FLOAT_WORD(exp_x, (hx & 0x7fffff) | ((0x7f + 127) << 23));
5312188b77SDavid Schultz 	return (exp_x);
5412188b77SDavid Schultz }
5512188b77SDavid Schultz 
5612188b77SDavid Schultz float
__ldexp_expf(float x,int expt)5712188b77SDavid Schultz __ldexp_expf(float x, int expt)
5812188b77SDavid Schultz {
5912188b77SDavid Schultz 	float exp_x, scale;
6012188b77SDavid Schultz 	int ex_expt;
6112188b77SDavid Schultz 
6212188b77SDavid Schultz 	exp_x = __frexp_expf(x, &ex_expt);
6312188b77SDavid Schultz 	expt += ex_expt;
6412188b77SDavid Schultz 	SET_FLOAT_WORD(scale, (0x7f + expt) << 23);
6512188b77SDavid Schultz 	return (exp_x * scale);
6612188b77SDavid Schultz }
6712188b77SDavid Schultz 
6812188b77SDavid Schultz float complex
__ldexp_cexpf(float complex z,int expt)6912188b77SDavid Schultz __ldexp_cexpf(float complex z, int expt)
7012188b77SDavid Schultz {
71fcce470cSStefan Eßer 	float c, exp_x, s, scale1, scale2, x, y;
7212188b77SDavid Schultz 	int ex_expt, half_expt;
7312188b77SDavid Schultz 
7412188b77SDavid Schultz 	x = crealf(z);
7512188b77SDavid Schultz 	y = cimagf(z);
7612188b77SDavid Schultz 	exp_x = __frexp_expf(x, &ex_expt);
7712188b77SDavid Schultz 	expt += ex_expt;
7812188b77SDavid Schultz 
7912188b77SDavid Schultz 	half_expt = expt / 2;
8012188b77SDavid Schultz 	SET_FLOAT_WORD(scale1, (0x7f + half_expt) << 23);
8112188b77SDavid Schultz 	half_expt = expt - half_expt;
8212188b77SDavid Schultz 	SET_FLOAT_WORD(scale2, (0x7f + half_expt) << 23);
8312188b77SDavid Schultz 
84fcce470cSStefan Eßer 	sincosf(y, &s, &c);
85fcce470cSStefan Eßer 	return (CMPLXF(c * exp_x * scale1 * scale2,
86fcce470cSStefan Eßer 	    s * exp_x * scale1 * scale2));
8712188b77SDavid Schultz }
88