-
Notifications
You must be signed in to change notification settings - Fork 20
root: add F32::exp and F32::ln #554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
52ac8cb
root: implement exp and ln for F32
nilscrm 384bdae
verify float ops
nilscrm b2f5587
cspell
nilscrm 86e6e3b
snapshot verify script diagnostics
nilscrm 69c4966
refactors
nilscrm d2691c9
fix exp precision bug
nilscrm f95ab47
test float ops with random floats
nilscrm 509033c
improve verification error messages
tjjfvi d0c5d50
increase number of randomly tested floats
tjjfvi f122cc3
allow 1ulp error for F64::ln
nilscrm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
|
|
||
| const num_mantissa_bits: N32 = Float::num_mantissa_bits[F32]; | ||
| const max_exponent: I32 = Float::max_exponent[F32]; | ||
| const exp_mask: N32 = (1[N32] << Float::num_exponent_bits[F32]) - 1; | ||
|
|
||
| /// normalize returns a normal number y and exponent exp | ||
| /// satisfying x == y × 2**exp. It assumes x is finite and non-zero. | ||
| pub fn .normalize(x: F32) -> (F32, I32) { | ||
| if x.to_bits() & (exp_mask << num_mantissa_bits) == 0 { | ||
| return (x * (1 << num_mantissa_bits) as F32, -num_mantissa_bits); | ||
| } | ||
| return (x, +0); | ||
| } | ||
|
|
||
| /// Calculates frac * 2 ^ exp. | ||
| pub fn apply_exponent(frac: F32, exp: I32) -> F32 { | ||
| // special cases | ||
| if frac == 0.0 or frac == F32::inf or frac == F32::neg_inf or frac.is_nan() { | ||
| return frac; | ||
| } | ||
| let (frac, e) = frac.normalize(); | ||
| exp += e; | ||
| let x = frac.to_bits(); | ||
| exp += ((x >> num_mantissa_bits) as N32 & exp_mask) as I32 - max_exponent; | ||
| if exp < -149 { | ||
| // underflow | ||
| return if frac < 0.0 { | ||
| -0.0 | ||
| } else { | ||
| 0.0 | ||
| }; | ||
| } | ||
| if exp > +127 { | ||
| // overflow | ||
| return if frac < 0.0 { | ||
| F32::neg_inf | ||
| } else { | ||
| F32::inf | ||
| }; | ||
| } | ||
| let m = 1.0; | ||
| if exp < -126 { | ||
| // denormalize | ||
| exp += +24 | ||
| // 2**-24 | ||
| m = 1.0 / (1 << 24) as F32 | ||
| } | ||
| x &= !(exp_mask << num_mantissa_bits); | ||
| x |= (exp + max_exponent) as N32 << num_mantissa_bits; | ||
| return m * F32::from_bits(x); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
|
|
||
| // Adapted from https://github.com/rust-lang/compiler-builtins/blob/libm-v0.2.16/libm/src/math/exp.rs | ||
| // | ||
| // origin: FreeBSD /usr/src/lib/msun/src/e_expf.c | ||
| // | ||
| // Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. | ||
| // | ||
| // ==================================================== | ||
| // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. | ||
| // | ||
| // Developed at SunPro, a Sun Microsystems, Inc. business. | ||
| // Permission to use, copy, modify, and distribute this | ||
| // software is freely granted, provided that this notice | ||
| // is preserved. | ||
| // ==================================================== | ||
|
|
||
| const ln2_hi: F32 = 6.9314575195e-01; | ||
| const ln2_lo: F32 = 1.4286067653e-06; | ||
| const inv_ln2: F32 = 1.4426950216e+00; | ||
|
|
||
| const P1: F32 = 1.6666625440e-1; | ||
| const P2: F32 = -2.7667332906e-3; | ||
|
|
||
| const overflow: F32 = 88.7228394[F32]; | ||
| const underflow: F32 = -103.972084[F32]; | ||
|
|
||
| /// Exponential, base *e* (F32) | ||
| /// | ||
| /// Calculate the exponential of `f`, that is, *e* raised to the power `f` | ||
| /// (where *e* is the base of the natural system of logarithms, approximately 2.71828). | ||
| pub fn .exp(f: F32) -> F32 { | ||
| when { | ||
| f.is_nan() { f } | ||
| f >= overflow { F32::inf } | ||
| f <= underflow { 0.0 } | ||
| _ { | ||
| let hf = f.to_bits(); | ||
| let is_negative = (hf >> 31) == 1; | ||
| // high word of |f| | ||
| hf &= 0x7fffffff; | ||
|
|
||
| // argument reduction | ||
| when { | ||
| hf > 0x3eb17218 { | ||
| // if |f| > 0.5 ln2 | ||
| let k = when { | ||
| hf > 0x3f851592 { | ||
| if is_negative { | ||
| // if f < 1.5 ln2 | ||
| (inv_ln2 * f - 0.5[F32]) as I32 | ||
| } else { | ||
| // if f > 1.5 ln2 | ||
| (inv_ln2 * f + 0.5[F32]) as I32 | ||
| } | ||
| } | ||
| is_negative { -1 } | ||
| _ { +1 } | ||
| }; | ||
| let kf = k as F32; | ||
| let hi = f - kf * ln2_hi; | ||
| // k*ln2hi is exact here | ||
| let lo = kf * ln2_lo; | ||
| f = hi - lo; | ||
| exp_poly_approximation(f, hi, lo, k) | ||
| } | ||
| hf > 0x39000000 { exp_poly_approximation(f, f, 0.0, +0) } | ||
| _ { 1.0 + f } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn exp_poly_approximation(f: F32, hi: F32, lo: F32, k: I32) -> F32 { | ||
| let ff = f * f; | ||
| let c = f - ff * (P1 + ff * P2); | ||
| let y = 1.0 + (f * c / (2.0 - c) - lo + hi); | ||
| if k == +0 { | ||
| y | ||
| } else { | ||
| common::apply_exponent(y, k) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
|
|
||
| // Adapted from https://github.com/rust-lang/compiler-builtins/blob/libm-v0.2.16/libm/src/math/logf.rs | ||
| // | ||
| // origin: FreeBSD /usr/src/lib/msun/src/e_logf.c */ | ||
| // | ||
| // Conversion to float by Ian Lance Taylor, Cygnus Support, ian@cygnus.com. | ||
| // | ||
| // ==================================================== | ||
| // Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. | ||
| // | ||
| // Developed at SunPro, a Sun Microsystems, Inc. business. | ||
| // Permission to use, copy, modify, and distribute this | ||
| // software is freely granted, provided that this notice | ||
| // is preserved. | ||
| // ==================================================== | ||
|
|
||
| const ln2_hi: F32 = 6.9313812256e-01; | ||
| const ln2_lo: F32 = 9.0580006145e-06; | ||
| // |(log(1+s)-log(1-s))/s - Lg(s)| < 2**-34.24 (~[-4.95e-11, 4.97e-11]). | ||
| const lg1: F32 = 0.66666662693; | ||
| const lg2: F32 = 0.40000972152; | ||
| const lg3: F32 = 0.28498786688; | ||
| const lg4: F32 = 0.24279078841; | ||
|
|
||
| /// The natural logarithm of `f` (F32). | ||
| pub fn .ln(f: F32) -> F32 { | ||
| // 0x1p25f === 2 ^ 25 | ||
| let x1p25 = F32::from_bits(0x4c000000); | ||
| let bits = f.to_bits(); | ||
| let k = +0; | ||
|
|
||
| when { | ||
| bits < 0x00800000 or (bits >> 31) != 0 { | ||
| // f < 2**-126 (f small or negative) | ||
| if bits << 1 == 0 { | ||
| // log(+-0)=-inf | ||
| return -1.0 / (f * f); | ||
| } | ||
| if bits as I32 < +0 { | ||
| return F32::nan; | ||
| } | ||
| // subnormal number, scale up f | ||
| k -= +25; | ||
| f *= x1p25; | ||
| bits = f.to_bits(); | ||
| } | ||
| bits >= 0x7f800000 { | ||
| // f is infinite or NaN | ||
| return f; | ||
| } | ||
| bits == 0x3f800000 { | ||
| // f == 1.0 | ||
| return 0.0; | ||
| } | ||
| } | ||
|
|
||
| // reduce f into [sqrt(2)/2, sqrt(2)] | ||
| bits += (0x3f800000 - 0x3f3504f3); | ||
| k += ((bits >> 23) as I32) - +0x7f; | ||
| bits = (bits & 0x007fffff) + 0x3f3504f3; | ||
| f = F32::from_bits(bits); | ||
|
|
||
| f -= 1.0; | ||
| let s = f / (2.0 + f); | ||
| let z = s * s; | ||
| let w = z * z; | ||
| let t1 = w * (lg2 + w * lg4); | ||
| let t2 = z * (lg1 + w * lg3); | ||
| let r = t2 + t1; | ||
| let hfsq = 0.5 * f * f; | ||
| let dk = k as F32; | ||
| s * (hfsq + r) + dk * ln2_lo - hfsq + f + dk * ln2_hi | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.