Merge commit '7d815089a43a963b49eaddf97e514194ec29805b'
[unleashed.git] / usr / src / lib / libmvec / common / __vexpf.c
blob7474c2b29bb225afe903a4077f4d433891effcce
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 #ifdef __RESTRICT
31 #define restrict _Restrict
32 #else
33 #define restrict
34 #endif
36 /* float expf(float x)
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.
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.
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
162 static const double
163 K256ONLN2 = 369.3299304675746271,
164 KA2 = 3.66556671660783833261e-06,
165 KA1 = 2.70760782821392980564e-03,
166 KA0 = 1.0;
168 static const float extreme[2] = { 1.0e30f, 1.0e-30f };
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
181 #define PREPROCESS(N, index, label) \
182 xi = *(int *)x; \
183 ax = xi & ~0x80000000; \
184 fx = *x; \
185 x += stridex; \
186 if (ax > 0x42cff1b4) /* 103.972076f */ \
188 sign = (unsigned)xi >> 31; \
189 if (ax >= 0x7f800000) /* |x| = inf or nan */ \
191 if (ax > 0x7f800000) /* nan */ \
193 y[index] = fx * fx; \
194 goto label; \
196 y[index] = (sign) ? 0.0f : fx; \
197 goto label; \
199 fx = extreme[sign]; \
200 y[index] = fx * fx; \
201 goto label; \
203 x##N = fx
206 void
207 __vexpf(int n, float * restrict x, int stridex, float * restrict y,
208 int stridey)
210 double x0, x1, x2, x3, x4;
211 double res0, res1, res2, res3, res4;
212 float fx;
213 long long lres0, lres1, lres2, lres3, lres4;
214 int k0, k1, k2, k3, k4;
215 int xi, ax, sign;
217 y -= stridey;
219 for (; ;)
221 begin:
222 if (--n < 0)
223 break;
224 y += stridey;
226 PREPROCESS(0, 0, begin);
228 if (--n < 0)
229 goto process1;
231 PREPROCESS(1, stridey, process1);
233 if (--n < 0)
234 goto process2;
236 PREPROCESS(2, stridey << 1, process2);
238 if (--n < 0)
239 goto process3;
241 PREPROCESS(3, (stridey << 1) + stridey, process3);
243 if (--n < 0)
244 goto process4;
246 PREPROCESS(4, (stridey << 2), process4);
248 x0 *= K256ONLN2;
249 x1 *= K256ONLN2;
250 x2 *= K256ONLN2;
251 x3 *= K256ONLN2;
252 x4 *= K256ONLN2;
254 k0 = (int)x0;
255 k1 = (int)x1;
256 k2 = (int)x2;
257 k3 = (int)x3;
258 k4 = (int)x4;
260 x0 -= (double)k0;
261 x1 -= (double)k1;
262 x2 -= (double)k2;
263 x3 -= (double)k3;
264 x4 -= (double)k4;
266 x0 = (KA2 * x0 + KA1) * x0 + KA0;
267 x1 = (KA2 * x1 + KA1) * x1 + KA0;
268 x2 = (KA2 * x2 + KA1) * x2 + KA0;
269 x3 = (KA2 * x3 + KA1) * x3 + KA0;
270 x4 = (KA2 * x4 + KA1) * x4 + KA0;
272 lres0 = ((long long *)__TBL_exp2f)[k0 & 255];
273 lres1 = ((long long *)__TBL_exp2f)[k1 & 255];
274 lres2 = ((long long *)__TBL_exp2f)[k2 & 255];
275 lres3 = ((long long *)__TBL_exp2f)[k3 & 255];
276 lres4 = ((long long *)__TBL_exp2f)[k4 & 255];
278 lres0 += (long long)k0 << 44;
279 res0 = *(double *)&lres0;
280 lres1 += (long long)k1 << 44;
281 res1 = *(double *)&lres1;
282 lres2 += (long long)k2 << 44;
283 res2 = *(double *)&lres2;
284 lres3 += (long long)k3 << 44;
285 res3 = *(double *)&lres3;
286 lres4 += (long long)k4 << 44;
287 res4 = *(double *)&lres4;
289 *y = (float)(res0 * x0);
290 y += stridey;
291 *y = (float)(res1 * x1);
292 y += stridey;
293 *y = (float)(res2 * x2);
294 y += stridey;
295 *y = (float)(res3 * x3);
296 y += stridey;
297 *y = (float)(res4 * x4);
298 continue;
300 process1:
301 PROCESS(0);
302 continue;
304 process2:
305 PROCESS(0);
306 PROCESS(1);
307 continue;
309 process3:
310 PROCESS(0);
311 PROCESS(1);
312 PROCESS(2);
313 continue;
315 process4:
316 PROCESS(0);
317 PROCESS(1);
318 PROCESS(2);
319 PROCESS(3);