about summary refs log tree commit diff
path: root/library/compiler-builtins/libm/src/math/k_expo2f.rs
diff options
context:
space:
mode:
authorTrevor Gross <tmgross@umich.edu>2025-04-19 21:09:49 +0000
committerTrevor Gross <t.gross35@gmail.com>2025-04-19 17:20:24 -0400
commit8b8bd8a0fd75e43a9b282284b849e651828ceec2 (patch)
treecc6eba464cba2cb43a51c912a97029a01572a7e0 /library/compiler-builtins/libm/src/math/k_expo2f.rs
parent911a70381a9e7c84400b156e3cbcd805f3e64034 (diff)
downloadrust-8b8bd8a0fd75e43a9b282284b849e651828ceec2.tar.gz
rust-8b8bd8a0fd75e43a9b282284b849e651828ceec2.zip
libm: Flatten the `libm/libm` directory
Diffstat (limited to 'library/compiler-builtins/libm/src/math/k_expo2f.rs')
-rw-r--r--library/compiler-builtins/libm/src/math/k_expo2f.rs14
1 files changed, 14 insertions, 0 deletions
diff --git a/library/compiler-builtins/libm/src/math/k_expo2f.rs b/library/compiler-builtins/libm/src/math/k_expo2f.rs
new file mode 100644
index 00000000000..fbd7b27d583
--- /dev/null
+++ b/library/compiler-builtins/libm/src/math/k_expo2f.rs
@@ -0,0 +1,14 @@
+use super::expf;
+
+/* k is such that k*ln2 has minimal relative error and x - kln2 > log(FLT_MIN) */
+const K: i32 = 235;
+
+/* expf(x)/2 for x >= log(FLT_MAX), slightly better than 0.5f*expf(x/2)*expf(x/2) */
+#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
+pub(crate) fn k_expo2f(x: f32) -> f32 {
+    let k_ln2 = f32::from_bits(0x4322e3bc);
+    /* note that k is odd and scale*scale overflows */
+    let scale = f32::from_bits(((0x7f + K / 2) as u32) << 23);
+    /* exp(x - k ln2) * 2**(k-1) */
+    expf(x - k_ln2) * scale * scale
+}