| 1 |
|
|---|
| 2 | /* @(#)e_log.c 5.1 93/09/24 */
|
|---|
| 3 | /*
|
|---|
| 4 | * ====================================================
|
|---|
| 5 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
|---|
| 6 | *
|
|---|
| 7 | * Developed at SunPro, a Sun Microsystems, Inc. business.
|
|---|
| 8 | * Permission to use, copy, modify, and distribute this
|
|---|
| 9 | * software is freely granted, provided that this notice
|
|---|
| 10 | * is preserved.
|
|---|
| 11 | * ====================================================
|
|---|
| 12 | */
|
|---|
| 13 |
|
|---|
| 14 | /* __ieee754_log(x)
|
|---|
| 15 | * Return the logrithm of x
|
|---|
| 16 | *
|
|---|
| 17 | * Method :
|
|---|
| 18 | * 1. Argument Reduction: find k and f such that
|
|---|
| 19 | * x = 2^k * (1+f),
|
|---|
| 20 | * where sqrt(2)/2 < 1+f < sqrt(2) .
|
|---|
| 21 | *
|
|---|
| 22 | * 2. Approximation of log(1+f).
|
|---|
| 23 | * Let s = f/(2+f) ; based on log(1+f) = log(1+s) - log(1-s)
|
|---|
| 24 | * = 2s + 2/3 s**3 + 2/5 s**5 + .....,
|
|---|
| 25 | * = 2s + s*R
|
|---|
| 26 | * We use a special Reme algorithm on [0,0.1716] to generate
|
|---|
| 27 | * a polynomial of degree 14 to approximate R The maximum error
|
|---|
| 28 | * of this polynomial approximation is bounded by 2**-58.45. In
|
|---|
| 29 | * other words,
|
|---|
| 30 | * 2 4 6 8 10 12 14
|
|---|
| 31 | * R(z) ~ Lg1*s +Lg2*s +Lg3*s +Lg4*s +Lg5*s +Lg6*s +Lg7*s
|
|---|
| 32 | * (the values of Lg1 to Lg7 are listed in the program)
|
|---|
| 33 | * and
|
|---|
| 34 | * | 2 14 | -58.45
|
|---|
| 35 | * | Lg1*s +...+Lg7*s - R(z) | <= 2
|
|---|
| 36 | * | |
|
|---|
| 37 | * Note that 2s = f - s*f = f - hfsq + s*hfsq, where hfsq = f*f/2.
|
|---|
| 38 | * In order to guarantee error in log below 1ulp, we compute log
|
|---|
| 39 | * by
|
|---|
| 40 | * log(1+f) = f - s*(f - R) (if f is not too large)
|
|---|
| 41 | * log(1+f) = f - (hfsq - s*(hfsq+R)). (better accuracy)
|
|---|
| 42 | *
|
|---|
| 43 | * 3. Finally, log(x) = k*ln2 + log(1+f).
|
|---|
| 44 | * = k*ln2_hi+(f-(hfsq-(s*(hfsq+R)+k*ln2_lo)))
|
|---|
| 45 | * Here ln2 is split into two floating point number:
|
|---|
| 46 | * ln2_hi + ln2_lo,
|
|---|
| 47 | * where n*ln2_hi is always exact for |n| < 2000.
|
|---|
| 48 | *
|
|---|
| 49 | * Special cases:
|
|---|
| 50 | * log(x) is NaN with signal if x < 0 (including -INF) ;
|
|---|
| 51 | * log(+INF) is +INF; log(0) is -INF with signal;
|
|---|
| 52 | * log(NaN) is that NaN with no signal.
|
|---|
| 53 | *
|
|---|
| 54 | * Accuracy:
|
|---|
| 55 | * according to an error analysis, the error is always less than
|
|---|
| 56 | * 1 ulp (unit in the last place).
|
|---|
| 57 | *
|
|---|
| 58 | * Constants:
|
|---|
| 59 | * The hexadecimal values are the intended ones for the following
|
|---|
| 60 | * constants. The decimal values may be used, provided that the
|
|---|
|
|---|