From 7feac387a1ee54ea9c63a98437c390907a630aa9 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Tue, 5 Apr 2022 14:41:51 -0400 Subject: [PATCH 1/4] make `exp(::Float32)` and friends vectorize --- base/special/exp.jl | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/base/special/exp.jl b/base/special/exp.jl index c2bbb47902360..1c98aaaec40bf 100644 --- a/base/special/exp.jl +++ b/base/special/exp.jl @@ -267,25 +267,21 @@ end twopk = Int64(k) << 52 return reinterpret(T, twopk + reinterpret(Int64, small_part)) end - + @inline function exp_impl(x::Float32, base) T = Float32 + bad = (abs(x) <= SUBNORM_EXP(base, T)) N_float = round(x*LogBINV(base, T)) N = unsafe_trunc(Int32, N_float) r = muladd(N_float, LogBU(base, T), x) r = muladd(N_float, LogBL(base, T), r) small_part = expb_kernel(base, r) - if !(abs(x) <= SUBNORM_EXP(base, T)) - x > MAX_EXP(base, T) && return Inf32 - x < MIN_EXP(base, T) && return 0.0f0 - if N <= Int32(-24) - twopk = reinterpret(T, (N+Int32(151)) << Int32(23)) - return (twopk*small_part)*(2f0^(-24)) - end - N == 128 && return small_part * T(2.0) * T(2.0)^127 - end - twopk = reinterpret(T, (N+Int32(127)) << Int32(23)) - return twopk*small_part + power = (N+Int32(127)) + x > MAX_EXP(base, T) && return Inf32 + x < MIN_EXP(base, T) && return 0.0f0 + x <= -SUBNORM_EXP(base, T) && (power+=Int32(24); small_part*=Float32(0x1p-24)) + N == 128 && (power-=Int32(1); small_part*=2f0) + return small_part * reinterpret(T, power << Int32(23)) end @inline function exp_impl_fast(x::Float32, base) From 047234b711d17776f53014b45fcfabd61e22bc90 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Tue, 5 Apr 2022 15:07:53 -0400 Subject: [PATCH 2/4] fix formatting. --- base/special/exp.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/base/special/exp.jl b/base/special/exp.jl index 1c98aaaec40bf..36b1cd1fa53a2 100644 --- a/base/special/exp.jl +++ b/base/special/exp.jl @@ -267,10 +267,9 @@ end twopk = Int64(k) << 52 return reinterpret(T, twopk + reinterpret(Int64, small_part)) end - + @inline function exp_impl(x::Float32, base) T = Float32 - bad = (abs(x) <= SUBNORM_EXP(base, T)) N_float = round(x*LogBINV(base, T)) N = unsafe_trunc(Int32, N_float) r = muladd(N_float, LogBU(base, T), x) From 1c67ec5e11efb1280f235760db5409d3daef6602 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Tue, 5 Apr 2022 16:52:07 -0400 Subject: [PATCH 3/4] respond to review. --- base/special/exp.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/base/special/exp.jl b/base/special/exp.jl index 36b1cd1fa53a2..a0c5af4f603b3 100644 --- a/base/special/exp.jl +++ b/base/special/exp.jl @@ -278,7 +278,10 @@ end power = (N+Int32(127)) x > MAX_EXP(base, T) && return Inf32 x < MIN_EXP(base, T) && return 0.0f0 - x <= -SUBNORM_EXP(base, T) && (power+=Int32(24); small_part*=Float32(0x1p-24)) + if x <= -SUBNORM_EXP(base, T) + power += Int32(24) + small_part *= Float32(0x1p-24) + end N == 128 && (power-=Int32(1); small_part*=2f0) return small_part * reinterpret(T, power << Int32(23)) end From e30db34b2c0a5a958d7f9d60be197fb8dcd1e6e3 Mon Sep 17 00:00:00 2001 From: Oscar Smith Date: Tue, 5 Apr 2022 22:43:23 -0500 Subject: [PATCH 4/4] more formatting fixes --- base/special/exp.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/base/special/exp.jl b/base/special/exp.jl index a0c5af4f603b3..837310bc7ed19 100644 --- a/base/special/exp.jl +++ b/base/special/exp.jl @@ -282,7 +282,10 @@ end power += Int32(24) small_part *= Float32(0x1p-24) end - N == 128 && (power-=Int32(1); small_part*=2f0) + if N == 128 + power -= Int32(1) + small_part *= 2f0 + end return small_part * reinterpret(T, power << Int32(23)) end