1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
|
/******************************************************************************
Copyright (c) 2007-2024, Intel Corp.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
#ifndef BASE_NAME
# define BASE_NAME EXPM1_BASE_NAME
#endif
#if !defined F_ENTRY_NAME
# define F_ENTRY_NAME F_EXPM1_NAME
#endif
/*
** Get build file name. Since the constant table is shared, alway define
** MAKE_COMMON
*/
#define MAKE_COMMON
#if !defined(BUILD_FILE_NAME)
# define BUILD_FILE_NAME F_POW_BUILD_FILE_NAME
#endif
/*
** Pick up the latest default DPML definitions. however, don't let
** dpml_private.h default TABLE_NAME. This has already been done or over-
** ridden when the constant file was generated.
*/
#define DONT_DEFAULT_TABLE_NAME 1
#define NEW_DPML_MACROS 1
#include "dpml_private.h"
#if defined(UNDEF_TABLE_NAME)
# undef TABLE_NAME
#endif
/*
** Pick up common build time constants and definitions from the generated
** power constant table file
*/
#define DEFINE_SYMBOLIC_CONSTANTS 1
#include STR( BUILD_FILE_NAME )
/*
** Pick up common compile time constants and definitions
*/
#include "dpml_pow.h"
/*
** Design Overview:
** ---------------
**
** The implementation of expm1 is based on the implementation of exp(x).
** Specifically, exp is computed as follows:
**
** o Let fm be the value of x/ln2 rounded to POW2_K bits, where POW2_K
** is a small positive integer value.
** o Let I and j be the integer and fraction bits of fm and z = x - ln2*fm
**
** Then
**
** e^x = 2^(I + j/2^POW2_K)*e^z
** = 2^I * 2^(j/2^POW2_K) * [1 + z*p(z)]
**
** where p(z) is a polynomial approximation to (e^x - 1)/x. The value of
** 2^(j/2^POW2_K) is obtained from a table in hi and lo pieces: T(j) the
** correctly rounded value of 2^(j/2^POW2_K) and R(j) =
** [2^(j/2^POW2_K) - T(j)]/T(j). Then
**
** e^x = 2^I * [ T(j) + T(j)* R(j)] * [ 1 + z*p(z) ]
** = 2^I * T(j) * [ 1 + R(j)] * [ 1 + z*p(z) ]
** = 2^I * T(j) * [ 1 + R(j) + z*p(z) + R(j)*z*p(z) ]
**
** If we denote by V(I,j) the product 2^I*T(j) we have
**
** e^x = V(I,j) + V(I,j)*{ R(j) + [1 + R(j)]*z*p(z) }
**
** Note that V(I,j) is exact and there is an alignment shift of at least
** POW2_K + 1 bits between V(I,j) and V(I,j)*{ R(j) + [1 + R(j)]*z*p(z) },
** there by allowing for a very accurate final result.
**
** Based on the above, e^x - 1 is simply
**
** expm1(x) = V(I,j) + V(I,j)*{ R(j) + [1 + R(j)]*z*p(z) } - 1. (1)
**
** Note that the polynomial p(z) has the form p(z) = 1 + z*q(z). If we
** define U(j,z) = R(j)*(1 + z) + z^2*q(z) and W(j,z) = U(j,z) + z, then
**
** expm1(x) = V(I,j) + V(I,j)*{ R(j) + [1 + R(j)]*z*p(z) } - 1
** = V(I,j) + V(I,j)*{ R(j) + [1 + R(j)]*z*[1 + z*q(z)] } - 1
** = V(I,j) + V(I,j)*{ R(j) + [1 + R(j)]*z*[1 + z*q(z)] } - 1
** = V(I,j) + V(I,j)*{ R(j)*(1 + z) + z^2*q(z) + z +
** R(j)*z^2*q(z) } - 1
** = V(I,j) + V(I,j)*{ U(j,z) + z + R(j)*z^2*q(z) } - 1
** = V(I,j) + V(I,j)*{ W(j,z) + R(j)*z^2*q(z) } - 1.
**
** Finally, we note that for machine precision arithmetic, the term
** R(j)*z^2*q(z) is insignificant relative to W(j,z) so that
**
** expm1(x) = V(I,j) + V(I,j)*W(j,z)) - 1 (2)
**
** The trick in evaluating (2) is determining where to add the -1 term so
** that no accuracy is lost. The tack chosen in this routine is to divide
** the domain of expm1 into several subdomains based on the value of I.
**
** NOTE: When backup precision is available, only one of the subdomains,
** the polynomial range need be implemented.
**
**
** Constant Range:
**
** If I < -(F_PRECISION + 1), all of the terms except -1 are insignificant
** in the current precision, so just return -1
**
** Hi/lo Range:
**
** If -(F_PRECISION + 1) <= I <= -2, -1 is the dominate term, but
** the sum V(I,j) - 1 will lose some precision. In this case, we
** break V(I,j) - 1 into hi and lo pieces as follows:
**
** hi = V(I,j) - 1
** lo = V(I,j) - (hi + 1)
**
** and then write (2) as
**
** expm1(x) = hi + [ lo + V(I,j)*W(j,z)]
**
** Problem Range:
**
** When I = 0 or -1, V(I,j) is very close to 1, so that V(I,j) - 1 can be
** very small (zero in fact), which effectively eliminates the alignment
** shift required to maintain accuracy. In this case, we need to reorder
** the sum in (1) in order to restore the original overhang. Recalling
** that W(j,z) = U(j,z) + z, and denoting V(I,j) - 1 by V1(I,j),
**
** expm1(x) = V(I,j) + V(I,j)*W(j,z) - 1
** = [V(I,j) - 1] + V(I,j)*[ U(j,z) + z ]
** = V1(I,j) + [ V1(I,j) + 1 ]*[ U(j,z) + z ]
** = V1(I,j) + U(j,z) + z + V1(I,j)*[ U(j,z) + z ]
** = V1(I,j) + z + U(j,z) + V1(I,j)*W(j,z)
**
** Now, suppose that z, the reduced argument is computed in hi and lo
** pieces. Then the above equation becomes:
**
** expm1(x) = V1(I,j) + z + U(j,z) + V1(I,j)*W(j,z)
** = [V1(I,j) + z_hi] + [z_lo + U(j,z) + V1(I,j)*W(j,z)]
**
** Note that [ V1(I,j) + z_hi ]/[ z_lo + U(j,z) + V1(I,j)*W(j,z) ] ~
** [ V1(I,j) + z ]/[ z^2/2 + V1(I,j)*z ] so that the first term overhangs
** the second by at least POW2_K bits. With all of the above in mind, the
** final calculation looks like:
**
** hi = V1(I,j) + z_hi
** lo = (z_hi - (hi - V1(I,j))) + z_lo
** expm1(x) = hi + { lo + [ U(j,z) + V1(I,j)*W(j,z) ] }
**
** Normal Range:
**
** When 1 <= I <= F_PRECISION - 1, V(I,j) - 1 is exact and loses at most
** one bit of alignment shift. In this case we can compute expm1 as
**
** expm1(x) = [V(I,j) - 1] + V(I,j)*W(j,z)
**
** Big range:
**
** When F_PRECISION <= I, then 1 is insignificant relative to V(I,j).
** In this case we can compute expm1 as
**
** expm1(x) = V(I,j) + [ V(I,j)*W(j,z) - 1 ]
**
** Note that on this range, overflow might occur.
**
** Polynomial Range:
**
** When |x| is relatively small, then I and j are both zero and no
** alignment shift is available using (2). In this situation, we
** base are approximation on the Taylor series expansion:
**
** expm1(x) = sum { x^n/n! | n = 1, ... }
**
**
** The following diagram summerizes the evaluation ranges for expm1
**
** -inf 0 +inf
** +----------+---------+--------+---+---+--------+-----------+--------+
** | | | | | | | |
** constant hi/lo problem poly problem normal big
*/
/* Select data type function dependent table values */
#define SUFFIX BACKUP_SELECT(R, F)
#define EXPM1_HI_CHECK PASTE( EXPM1_HI_CHECK_, SUFFIX )
#define EXPM1_LO_CHECK PASTE( EXPM1_LO_CHECK_, SUFFIX )
#define POLY_CHECK PASTE( EXPM1_POLY_CHECK_, SUFFIX )
#define POW2_MAX_SCALE PASTE( POW2_MAX_SCALE_, SUFFIX )
#define POLY(x) BACKUP_SELECT( EXPM1_POLY_R(x), EXPM1_POLY_F(x) )
#define BIG ACC_BIG /* 'big' for accurate pow */
/* Miscellaneous local definitions */
#define ALIGN_WITH_I(n) ((WORD) (n) << POW2_K)
F_TYPE
F_ENTRY_NAME( F_TYPE x )
{
EXCEPTION_RECORD_DECLARATION
B_TYPE fm, z, w, t, v, one;
B_UNION stack_tmp_u;
F_UNION stack_tmp_v;
U_WORD status_word;
WORD m, i, j;
# if !USE_BACKUP
B_TYPE z_hi, z_lo, u, v1;
# endif
/*
** Weed out: near overflow, certain -1 cases, NaN's, Inf's, denorms and
** polynomial cases.
*/
stack_tmp_v.f = x;
i = stack_tmp_v.F_HI_WORD;
m = i & MAKE_MASK(F_SIGN_BIT_POS, 0);
IF_VAX(i &= MAKE_MASK(F_SIGN_BIT_POS + 1, 0);)
/*
** The product x*(1/ln2) is on the critical path of this routine.
** Because the code is structured with a branch prior to the multiply,
** it is difficult for some compilers to schedule the load of
** the constant 1/ln2 early enough to avoid delaying the reduced argument
** computation. To avoid this delay, we preload (1/ln2)
*/
t = RECIP_LN2;
one = ONE;
/*
** Screen out cases where expm1(x) = -1, or overflow as well as x = NaN
** or infinity. We also need to screen out denorms and small argument
** (polynomial range). In order to avoid code schedule issues, pre-compute
** the check for the polynomial range.
*/
j = (m <= POLY_CHECK);
if (((U_WORD) i >= EXPM1_HI_CHECK) &&
((U_WORD) (i - F_SIGN_BIT_MASK) >= EXPM1_LO_CHECK))
goto possible_problems;
if (j)
goto poly_range;
/*
** compute the reduced argument, z.
*/
w = ((B_TYPE) x) * t;
INIT_FPU_STATE_AND_ROUND_TO_NEAREST(status_word);
t = BIG + w; /* Save for getting m later on */
fm = t - BIG;
BACKUP_SELECT(
z = w - fm;, z_hi = x - fm*LN2_HI;
z_lo = fm*LN2_LO;
z = z_hi - z_lo;
)
/*
** Now get the bits of m as a integer and break it up into I and j
*/
stack_tmp_u.f = t;
GET_LOW_32_BITS(m, stack_tmp_u);
j = (m & POW2_INDEX_MASK) << POW2_INDEX_POS;
i = m & (~POW2_INDEX_MASK);
BACKUP_SELECT(
w = EXPM1_RED_POLY_R(z);, u = POW2_LO_OV_POW2_HI(j)*(one + z);
u = EXPM1_RED_POLY_F( u, z );
w = u + z;
)
/* Scale 2^(j/2^POW2_K) by 2^I, so that only one multiply is done */
IF_SMALL_WORD(IPOW2_LO(stack_tmp_u, j);)
m = IPOW2(j);
m = W_ADD_TO_EXP_FIELD(m, ALIGN_SCALE_WITH_EXP(i));
stack_tmp_u.B_HI_WORD = m;
IF_VAX( m &= MAKE_MASK(F_SIGN_BIT_POS + 1, 0); )
v = stack_tmp_u.f;
/* We no longer care about the rounding mode, so restore it. */
RESTORE_FPU_STATE(status_word);
# if USE_BACKUP
/* Scale polynomial result and check for possible overflow */
z = (v - one) + v*w;
if (m > POW2_MAX_SCALE)
goto boundary_check;
# else
/*
** Strip out the big case since once thats done, its safe to use
** V(i,j) in arithmetic expressions
*/
if ( i >= ALIGN_WITH_I(F_PRECISION))
goto big_region;
/*
* Normal, problem and hi/lo ranges get here
*/
v1 = v - one;
if (i >= ALIGN_WITH_I(1))
goto normal_region;
if (i <= ALIGN_WITH_I(-2))
goto hi_lo_region;
/* problem_region: */
t = v1 + z;
z_lo = (z_hi - (t - v1)) - z_lo;
z = t + (z_lo + (u + v1*w));
goto return_z;
hi_lo_region:
z = v1 + ((v - (v1 + one)) + w*v);
goto return_z;
normal_region:
z = v1 + w*v;
goto return_z;
big_region:
/*
** Screen for overflows with moderate care. If no overflow possible,
** just go ahead and compute the result. At this point, m is the
** hi bits of V(I,j) = 2^I*T(j)
*/
if (m > POW2_MAX_SCALE)
goto boundary_check;
z = v + (v*w - one);
/* Fall through */
return_z:
# endif
return (F_TYPE) z;
poly_range:
/*
** At this point x is small and m is the high word of |x|. If x is
** really tiny (including denorms), we can just return x. Otherwise
** compute the series evaluation for expm1.
*/
if (m > ALIGN_W_EXP_FIELD(F_EXP_BIAS - F_PRECISION - F_NORM))
x = POLY(x);
return x;
boundary_check:
/*
** Do the "final" multiple. However, when no backup is available
** the final multiply might involve a NaN or dirty zero, so we need to
** do this scaling carefully
*/
IF_NO_BACKUP(
/* Multiply by table entry */
t = POW2_HI(j);
z = t + t*w;
)
stack_tmp_u.f = z;
m = stack_tmp_u.B_HI_WORD;
IF_NO_BACKUP(
/* "Multiply" by 2^i */
m = W_ADD_TO_EXP_FIELD(m, ALIGN_SCALE_WITH_EXP(i));
stack_tmp_u.B_HI_WORD = m;
z = stack_tmp_u.f;
)
/* Isolate exponent field and check for overflow */
j = EXPM1_OVERFLOW;
IF_VAX( m &= B_SIGN_EXP_MASK; )
if ((U_WORD) m >= ALIGN_WITH_B_TYPE_EXP(F_MAX_BIN_EXP + B_EXP_BIAS + 1))
goto do_exception;
return (F_TYPE) z;
NaN_or_Inf:
/*
* If we get here, i and m are the high words of x and |x|
* respectively. If m doesn't contain all of the bits of x,
* check for non-zero bits in the low word
*/
m <<= (BITS_PER_WORD - F_EXP_POS);
IF_SMALL_WORD( m = m OR_LOW_BITS_SET(stack_tmp_v); )
if ( m == 0 )
{ /* x was +/- infinity */
j = (i & F_SIGN_BIT_MASK) ? EXPM1_OF_NEG_INF : EXPM1_OF_INF;
goto do_exception;
}
return x;
return_minus_1:
return (F_TYPE) -one;
possible_problems:
/*
** If we get here, x is: large or an IEEE special case (NaN or Inf).
** Start by weeding out NaN and infinities
*/
IF_IEEE(
/* Screen out NaN's and Inf's */
if (m >= F_EXP_MASK) goto NaN_or_Inf;
)
/*
** If x is negative, expm1(x) = -1 to machine precision. Otherwise,
** expm1(x) overflows.
**
** NOTE: at this point i is the high word of x and m
** is i &= ~F_SIGN_BIT_MASK
*/
if (i & F_SIGN_BIT_MASK)
goto return_minus_1;
j = EXP_OVERFLOW;
do_exception:
GET_EXCEPTION_RESULT_1(j, x, x);
return x;
}
|