xref: /illumos-gate/usr/src/lib/libmvec/common/__vexpf.c (revision 8c69cc8fbe729fa7b091e901c4b50508ccc6bb33)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
24  */
25 /*
26  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27  * Use is subject to license terms.
28  */
29 
30 #ifdef __RESTRICT
31 #define restrict _Restrict
32 #else
33 #define restrict
34 #endif
35 
36 /* float expf(float x)
37  *
38  * Method :
39  *	1. Special cases:
40  *		for x > 88.722839355...(0x42B17218) => Inf + overflow;
41  *		for x < -103.97207642..(0xc2CFF1B4) => 0 + underflow;
42  *		for x = Inf			    => Inf;
43  *		for x = -Inf			    => 0;
44  *		for x = +-NaN			    => QNaN.
45  *	2. Computes exponential from:
46  *		exp(x) = 2**a  *  2**(k/256)  *  2**(y/256)
47  *	Where:
48  *		a    =    int  ( 256 * log2(e) * x ) >> 8;
49  *		k    =    int  ( 256 * log2(e) * x ) & 0xFF;
50  *		y    =    frac ( 256 * x * log2(e)).
51  *	Note that:
52  *		k = 0, 1, ..., 255;
53  *		y = (-1, 1).
54  *	Then:
55  *		2**(k/256) is looked up in a table of 2**0, 2**1/256, ...
56  *		2**(y/256) is computed using approximation:
57  *			2**(y/256) =  a0 + a1 * y + a2 * y**2
58  *		Multiplication by 2**a is done by adding "a" to
59  *		the biased exponent.
60  * Accuracy:
61  *	The maximum relative error for the approximating
62  *	polynomial is 2**(-29.18).  All calculations are of
63  *	double precision.
64  *	Maximum error observed: less than 0.528 ulp for the whole
65  *	float type range.
66  *
67  * NOTE: This implementation has been modified for SPARC to deliver
68  * zero instead of a subnormal result whenever the argument is less
69  * than log(2^-126).  Therefore the worst case relative error is 1.
70  */
71 
72 static const double __TBL_exp2f[] = {
73 	/* 2^(i/256) - (((i & 0xff) << 44), i = [0, 255] */
74 1.000000000000000000e+00, 9.994025125251012609e-01, 9.988087005564013632e-01,
75 9.982185740592087742e-01, 9.976321430258502376e-01, 9.970494174757447148e-01,
76 9.964704074554765478e-01, 9.958951230388689568e-01, 9.953235743270583136e-01,
77 9.947557714485678604e-01, 9.941917245593818730e-01, 9.936314438430204898e-01,
78 9.930749395106142074e-01, 9.925222218009785990e-01, 9.919733009806893653e-01,
79 9.914281873441580517e-01, 9.908868912137068774e-01, 9.903494229396448967e-01,
80 9.898157929003436051e-01, 9.892860115023132117e-01, 9.887600891802785785e-01,
81 9.882380363972563808e-01, 9.877198636446310465e-01, 9.872055814422322495e-01,
82 9.866952003384118486e-01, 9.861887309101209365e-01, 9.856861837629877776e-01,
83 9.851875695313955239e-01, 9.846928988785599302e-01, 9.842021824966076249e-01,
84 9.837154311066546031e-01, 9.832326554588848300e-01, 9.827538663326288448e-01,
85 9.822790745364429199e-01, 9.818082909081884413e-01, 9.813415263151109569e-01,
86 9.808787916539204454e-01, 9.804200978508705866e-01, 9.799654558618393629e-01,
87 9.795148766724087741e-01, 9.790683712979462161e-01, 9.786259507836846394e-01,
88 9.781876262048033732e-01, 9.777534086665099489e-01, 9.773233093041209241e-01,
89 9.768973392831440394e-01, 9.764755097993595978e-01, 9.760578320789027318e-01,
90 9.756443173783457823e-01, 9.752349769847807881e-01, 9.748298222159020865e-01,
91 9.744288644200894689e-01, 9.740321149764913367e-01, 9.736395852951079677e-01,
92 9.732512868168755604e-01, 9.728672310137493895e-01, 9.724874293887887378e-01,
93 9.721118934762408292e-01, 9.717406348416250950e-01, 9.713736650818186602e-01,
94 9.710109958251406104e-01, 9.706526387314379223e-01, 9.702986054921705072e-01,
95 9.699489078304969203e-01, 9.696035575013605134e-01, 9.692625662915755891e-01,
96 9.689259460199136642e-01, 9.685937085371902899e-01, 9.682658657263515378e-01,
97 9.679424295025619296e-01, 9.676234118132908124e-01, 9.673088246384006217e-01,
98 9.669986799902344776e-01, 9.666929899137042259e-01, 9.663917664863788115e-01,
99 9.660950218185727634e-01, 9.658027680534350123e-01, 9.655150173670379310e-01,
100 9.652317819684667066e-01, 9.649530740999082701e-01, 9.646789060367420010e-01,
101 9.644092900876289898e-01, 9.641442385946024096e-01, 9.638837639331581109e-01,
102 9.636278785123455481e-01, 9.633765947748582636e-01, 9.631299251971253694e-01,
103 9.628878822894031408e-01, 9.626504785958666099e-01, 9.624177266947013809e-01,
104 9.621896391981960006e-01, 9.619662287528346623e-01, 9.617475080393891318e-01,
105 9.615334897730127839e-01, 9.613241867033328614e-01, 9.611196116145447332e-01,
106 9.609197773255048203e-01, 9.607246966898252971e-01, 9.605343825959679060e-01,
107 9.603488479673386591e-01, 9.601681057623822069e-01, 9.599921689746773179e-01,
108 9.598210506330320246e-01, 9.596547638015787696e-01, 9.594933215798706616e-01,
109 9.593367371029771773e-01, 9.591850235415807502e-01, 9.590381941020729162e-01,
110 9.588962620266514580e-01, 9.587592405934176609e-01, 9.586271431164729018e-01,
111 9.584999829460172371e-01, 9.583777734684463256e-01, 9.582605281064505709e-01,
112 9.581482603191123770e-01, 9.580409836020059577e-01, 9.579387114872952580e-01,
113 9.578414575438342071e-01, 9.577492353772650846e-01, 9.576620586301189952e-01,
114 9.575799409819160113e-01, 9.575028961492645374e-01, 9.574309378859631181e-01,
115 9.573640799831001358e-01, 9.573023362691556182e-01, 9.572457206101023797e-01,
116 9.571942469095077177e-01, 9.571479291086353314e-01, 9.571067811865475727e-01,
117 9.570708171602075875e-01, 9.570400510845827879e-01, 9.570144970527471040e-01,
118 9.569941691959850116e-01, 9.569790816838944503e-01, 9.569692487244911838e-01,
119 9.569646845643128286e-01, 9.569654034885233251e-01, 9.569714198210175216e-01,
120 9.569827479245263113e-01, 9.569994022007218826e-01, 9.570213970903235223e-01,
121 9.570487470732028656e-01, 9.570814666684909211e-01, 9.571195704346837640e-01,
122 9.571630729697496731e-01, 9.572119889112359337e-01, 9.572663329363761964e-01,
123 9.573261197621985019e-01, 9.573913641456324175e-01, 9.574620808836177277e-01,
124 9.575382848132127922e-01, 9.576199908117032367e-01, 9.577072137967114207e-01,
125 9.577999687263049067e-01, 9.578982705991073709e-01, 9.580021344544072948e-01,
126 9.581115753722692086e-01, 9.582266084736434930e-01, 9.583472489204779565e-01,
127 9.584735119158284133e-01, 9.586054127039703721e-01, 9.587429665705107240e-01,
128 9.588861888424999869e-01, 9.590350948885443261e-01, 9.591897001189184646e-01,
129 9.593500199856788146e-01, 9.595160699827764983e-01, 9.596878656461707013e-01,
130 9.598654225539432483e-01, 9.600487563264122892e-01, 9.602378826262468747e-01,
131 9.604328171585819751e-01, 9.606335756711334994e-01, 9.608401739543135367e-01,
132 9.610526278413467072e-01, 9.612709532083855146e-01, 9.614951659746271417e-01,
133 9.617252821024303566e-01, 9.619613175974318642e-01, 9.622032885086644338e-01,
134 9.624512109286739170e-01, 9.627051009936374859e-01, 9.629649748834822054e-01,
135 9.632308488220031606e-01, 9.635027390769824729e-01, 9.637806619603088709e-01,
136 9.640646338280971506e-01, 9.643546710808080791e-01, 9.646507901633681881e-01,
137 9.649530075652912320e-01, 9.652613398207983142e-01, 9.655758035089392344e-01,
138 9.658964152537145020e-01, 9.662231917241966839e-01, 9.665561496346526393e-01,
139 9.668953057446663113e-01, 9.672406768592617388e-01, 9.675922798290256255e-01,
140 9.679501315502314629e-01, 9.683142489649629869e-01, 9.686846490612389671e-01,
141 9.690613488731369962e-01, 9.694443654809188349e-01, 9.698337160111555333e-01,
142 9.702294176368531087e-01, 9.706314875775782225e-01, 9.710399430995845238e-01,
143 9.714548015159391037e-01, 9.718760801866497268e-01, 9.723037965187919518e-01,
144 9.727379679666363632e-01, 9.731786120317773570e-01, 9.736257462632605941e-01,
145 9.740793882577122309e-01, 9.745395556594674824e-01, 9.750062661607005188e-01,
146 9.754795375015535841e-01, 9.759593874702675587e-01, 9.764458339033119660e-01,
147 9.769388946855159794e-01, 9.774385877501994280e-01, 9.779449310793042471e-01,
148 9.784579427035267063e-01, 9.789776407024486371e-01, 9.795040432046712153e-01,
149 9.800371683879468554e-01, 9.805770344793129922e-01, 9.811236597552254191e-01,
150 9.816770625416927354e-01, 9.822372612144102400e-01, 9.828042741988944897e-01,
151 9.833781199706193021e-01, 9.839588170551499813e-01, 9.845463840282800971e-01,
152 9.851408395161672660e-01, 9.857422021954695968e-01, 9.863504907934828037e-01,
153 9.869657240882776517e-01, 9.875879209088370692e-01, 9.882171001351949258e-01,
154 9.888532806985737000e-01, 9.894964815815237014e-01, 9.901467218180625141e-01,
155 9.908040204938135531e-01, 9.914683967461471736e-01, 9.921398697643202258e-01,
156 9.928184587896166091e-01, 9.935041831154891590e-01, 9.941970620877000897e-01,
157 9.948971151044636585e-01, 9.956043616165879406e-01, 9.963188211276171602e-01,
158 9.970405131939754639e-01, 9.977694574251096959e-01, 9.985056734836331715e-01,
159 9.992491810854701173e-01
160 };
161 
162 static const double
163 	K256ONLN2 = 369.3299304675746271,
164 	KA2 = 3.66556671660783833261e-06,
165 	KA1 = 2.70760782821392980564e-03,
166 	KA0 = 1.0;
167 
168 static const float extreme[2] = { 1.0e30f, 1.0e-30f };
169 
170 #define PROCESS(N)						\
171 	x##N *= K256ONLN2;					\
172 	k##N = (int) x##N;					\
173 	x##N -= (double) k##N;					\
174 	x##N = (KA2 * x##N + KA1) * x##N + KA0;			\
175 	lres##N = ((long long *)__TBL_exp2f)[k##N & 0xff];	\
176 	lres##N += (long long)k##N << 44;			\
177 	*y = (float) (x##N * *(double *)&lres##N);		\
178 	y += stridey
179 
180 #ifdef __sparc
181 
182 #define PREPROCESS(N, index, label)				\
183 	xi = *(int *)x;						\
184 	ax = xi & ~0x80000000;					\
185 	fx = *x;						\
186 	x += stridex;						\
187 	if (ax >= 0x42aeac50)	/* log(2^126) = 87.3365... */	\
188 	{							\
189 		sign = (unsigned)xi >> 31;			\
190 		if (ax >= 0x7f800000)	/* |x| = inf or nan */	\
191 		{						\
192 			if (ax > 0x7f800000)	/* nan */	\
193 			{					\
194 				y[index] = fx * fx;		\
195 				goto label;			\
196 			}					\
197 			y[index] = (sign) ? 0.0f : fx;		\
198 			goto label;				\
199 		}						\
200 		if (sign || ax > 0x42b17218) {		\
201 			fx = extreme[sign];			\
202 			y[index] = fx * fx;			\
203 			goto label;				\
204 		}						\
205 	}							\
206 	x##N = fx
207 
208 #else
209 
210 #define PREPROCESS(N, index, label)				\
211 	xi = *(int *)x;						\
212 	ax = xi & ~0x80000000;					\
213 	fx = *x;						\
214 	x += stridex;						\
215 	if (ax > 0x42cff1b4)	/* 103.972076f */		\
216 	{							\
217 		sign = (unsigned)xi >> 31;			\
218 		if (ax >= 0x7f800000)	/* |x| = inf or nan */	\
219 		{						\
220 			if (ax > 0x7f800000)	/* nan */	\
221 			{					\
222 				y[index] = fx * fx;		\
223 				goto label;			\
224 			}					\
225 			y[index] = (sign) ? 0.0f : fx;		\
226 			goto label;				\
227 		}						\
228 		fx = extreme[sign];				\
229 		y[index] = fx * fx;				\
230 		goto label;					\
231 	}							\
232 	x##N = fx
233 
234 #endif
235 
236 void
237 __vexpf(int n, float * restrict x, int stridex, float * restrict y,
238 	int stridey)
239 {
240 	double		x0, x1, x2, x3, x4;
241 	double		res0, res1, res2, res3, res4;
242 	float		fx;
243 	long long	lres0, lres1, lres2, lres3, lres4;
244 	int		k0, k1, k2, k3, k4;
245 	int		xi, ax, sign;
246 
247 	y -= stridey;
248 
249 	for (; ;)
250 	{
251 begin:
252 		if (--n < 0)
253 			break;
254 		y += stridey;
255 
256 		PREPROCESS(0, 0, begin);
257 
258 		if (--n < 0)
259 			goto process1;
260 
261 		PREPROCESS(1, stridey, process1);
262 
263 		if (--n < 0)
264 			goto process2;
265 
266 		PREPROCESS(2, stridey << 1, process2);
267 
268 		if (--n < 0)
269 			goto process3;
270 
271 		PREPROCESS(3, (stridey << 1) + stridey, process3);
272 
273 		if (--n < 0)
274 			goto process4;
275 
276 		PREPROCESS(4, (stridey << 2), process4);
277 
278 		x0 *= K256ONLN2;
279 		x1 *= K256ONLN2;
280 		x2 *= K256ONLN2;
281 		x3 *= K256ONLN2;
282 		x4 *= K256ONLN2;
283 
284 		k0 = (int)x0;
285 		k1 = (int)x1;
286 		k2 = (int)x2;
287 		k3 = (int)x3;
288 		k4 = (int)x4;
289 
290 		x0 -= (double)k0;
291 		x1 -= (double)k1;
292 		x2 -= (double)k2;
293 		x3 -= (double)k3;
294 		x4 -= (double)k4;
295 
296 		x0 = (KA2 * x0 + KA1) * x0 + KA0;
297 		x1 = (KA2 * x1 + KA1) * x1 + KA0;
298 		x2 = (KA2 * x2 + KA1) * x2 + KA0;
299 		x3 = (KA2 * x3 + KA1) * x3 + KA0;
300 		x4 = (KA2 * x4 + KA1) * x4 + KA0;
301 
302 		lres0 = ((long long *)__TBL_exp2f)[k0 & 255];
303 		lres1 = ((long long *)__TBL_exp2f)[k1 & 255];
304 		lres2 = ((long long *)__TBL_exp2f)[k2 & 255];
305 		lres3 = ((long long *)__TBL_exp2f)[k3 & 255];
306 		lres4 = ((long long *)__TBL_exp2f)[k4 & 255];
307 
308 		lres0 += (long long)k0 << 44;
309 		res0 = *(double *)&lres0;
310 		lres1 += (long long)k1 << 44;
311 		res1 = *(double *)&lres1;
312 		lres2 += (long long)k2 << 44;
313 		res2 = *(double *)&lres2;
314 		lres3 += (long long)k3 << 44;
315 		res3 = *(double *)&lres3;
316 		lres4 += (long long)k4 << 44;
317 		res4 = *(double *)&lres4;
318 
319 		*y = (float)(res0 * x0);
320 		y += stridey;
321 		*y = (float)(res1 * x1);
322 		y += stridey;
323 		*y = (float)(res2 * x2);
324 		y += stridey;
325 		*y = (float)(res3 * x3);
326 		y += stridey;
327 		*y = (float)(res4 * x4);
328 		continue;
329 
330 process1:
331 		PROCESS(0);
332 		continue;
333 
334 process2:
335 		PROCESS(0);
336 		PROCESS(1);
337 		continue;
338 
339 process3:
340 		PROCESS(0);
341 		PROCESS(1);
342 		PROCESS(2);
343 		continue;
344 
345 process4:
346 		PROCESS(0);
347 		PROCESS(1);
348 		PROCESS(2);
349 		PROCESS(3);
350 	}
351 }
352