Skip to content

Commit 1743a8b

Browse files
devmotiongiordano
andauthored
Fix JET analysis (#502) (#503)
* Fix JET analysis (#502) * Add regression tests * Run tests also on prerelease version of Julia * Update version number * Add missing newline character Co-authored-by: Mosè Giordano <765740+giordano@users.noreply.github.com> --------- Co-authored-by: Mosè Giordano <765740+giordano@users.noreply.github.com>
1 parent fddf5e0 commit 1743a8b

File tree

6 files changed

+51
-35
lines changed

6 files changed

+51
-35
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ jobs:
1717
- 'min'
1818
- 'lts'
1919
- '1'
20+
- 'pre'
2021
os:
2122
- ubuntu-latest
2223
- macOS-latest

Project.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "SpecialFunctions"
22
uuid = "276daf66-3868-5448-9aa4-cd146d93841b"
3-
version = "2.6.0"
3+
version = "2.6.1"
44

55
[deps]
66
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
@@ -19,6 +19,7 @@ SpecialFunctionsChainRulesCoreExt = "ChainRulesCore"
1919
ChainRulesCore = "0.9.44, 0.10, 1"
2020
ChainRulesTestUtils = "0.6.8, 0.7, 1"
2121
IrrationalConstants = "0.1, 0.2"
22+
JET = "0.9, 0.10"
2223
LogExpFunctions = "0.3.2"
2324
OpenLibm_jll = "0.7, 0.8"
2425
OpenSpecFun_jll = "0.5"
@@ -27,8 +28,9 @@ julia = "1.10"
2728
[extras]
2829
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
2930
ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a"
31+
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
3032
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
3133
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
3234

3335
[targets]
34-
test = ["ChainRulesCore", "ChainRulesTestUtils", "Random", "Test"]
36+
test = ["ChainRulesCore", "ChainRulesTestUtils", "JET", "Random", "Test"]

src/logabsgamma/e_lgamma_r.jl

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,32 +92,37 @@ function _logabsgamma(x::Float64)
9292
lx = ux % UInt32
9393

9494
#= purge off +-inf, NaN, +-0, tiny and negative arguments =#
95-
signgam = 1
96-
isneg = hx < Int32(0)
9795
ix = hx & 0x7fffffff
98-
ix 0x7ff00000 && return x * x, signgam
99-
ix | lx == 0x00000000 && return Inf, signgam
96+
ix 0x7ff00000 && return x * x, 1
97+
ix | lx == 0x00000000 && return Inf, 1
98+
isneg = hx < Int32(0)
10099
if ix < 0x3b900000 #= |x|<2**-70, return -log(|x|) =#
101100
if isneg
102-
signgam = -1
103-
return -log(-x), signgam
101+
return -log(-x), -1
104102
else
105-
return -log(x), signgam
103+
return -log(x), 1
106104
end
107105
end
106+
107+
# remaining cases
108108
if isneg
109109
# ix ≥ 0x43300000 && return Inf, signgam #= |x|>=2**52, must be -integer =#
110110
t = sinpi(x)
111-
iszero(t) && return Inf, signgam #= -integer =#
112-
nadj = logπ - log(abs(t * x))
113-
if t < 0.0; signgam = -1; end
114-
x = -x
111+
iszero(t) && return Inf, 1 #= -integer =#
112+
r = logπ - log(abs(t * x)) - _logabsgamma_pos(-x, ix)
113+
signgam = t < 0.0 ? -1 : 1
114+
else
115+
r = _logabsgamma_pos(x, ix)
116+
signgam = 1
115117
end
118+
return r, signgam
119+
end
120+
function _logabsgamma_pos(x::Float64, ix::UInt32)
116121
if ix < 0x40000000 #= x < 2.0 =#
117122
i = round(x, RoundToZero)
118123
f = x - i
119124
if f == 0.0 #= purge off 1; 2 handled by x < 8.0 branch =#
120-
return 0.0, signgam
125+
return 0.0
121126
elseif i == 1.0
122127
r = 0.0
123128
c = 1.0
@@ -169,8 +174,5 @@ function _logabsgamma(x::Float64)
169174
else #= 2^58 ≤ x ≤ Inf =#
170175
r = muladd(x, log(x), -x)
171176
end
172-
if isneg
173-
r = nadj - r
174-
end
175-
return r, signgam
177+
return r
176178
end

src/logabsgamma/e_lgammaf_r.jl

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,37 @@ function _logabsgamma(x::Float32)
2020
hx = reinterpret(Int32, x)
2121

2222
#= purge off +-inf, NaN, +-0, tiny and negative arguments =#
23-
signgam = 1
24-
isneg = hx < Int32(0)
2523
ix = hx & 0x7fffffff
26-
ix 0x7f800000 && return x * x, signgam
27-
ix == 0x00000000 && return Inf32, signgam
24+
ix 0x7f800000 && return x * x, 1
25+
ix == 0x00000000 && return Inf32, 1
26+
isneg = hx < Int32(0)
2827
if ix < 0x35000000 #= |x|<2**-21, return -log(|x|) =#
2928
if isneg
30-
signgam = -1
31-
return -log(-x), signgam
29+
return -log(-x), -1
3230
else
33-
return -log(x), signgam
31+
return -log(x), 1
3432
end
3533
end
34+
35+
# remaining cases
3636
if isneg
3737
# ix ≥ 0x4b000000 && return Inf32, signgam #= |x|>=2**23, must be -integer =#
3838
t = sinpi(x)
39-
t == 0.0f0 && return Inf32, signgam #= -integer =#
40-
nadj = logπ - log(abs(t * x))
41-
if t < 0.0f0; signgam = -1; end
42-
x = -x
39+
t == 0.0f0 && return Inf32, 1 #= -integer =#
40+
r = logπ - log(abs(t * x)) - _logabsgamma_pos(-x, ix)
41+
signgam = copysign(1, t)
42+
else
43+
r = _logabsgamma_pos(x, ix)
44+
signgam = 1
4345
end
44-
46+
return r, signgam
47+
end
48+
function _logabsgamma_pos(x::Float32, ix::UInt32)
4549
if ix < 0x40000000 #= x < 2.0 =#
4650
i = round(x, RoundToZero)
4751
f = x - i
4852
if f == 0.0f0 #= purge off 1; 2 handled by x < 8.0 branch =#
49-
return 0.0f0, signgam
53+
return 0.0f0
5054
elseif i == 1.0f0
5155
r = 0.0f0
5256
c = 1.0f0
@@ -99,8 +103,5 @@ function _logabsgamma(x::Float32)
99103
#= 2^58 ≤ x ≤ Inf =#
100104
r = muladd(x, log(x), -x)
101105
end
102-
if isneg
103-
r = nadj - r
104-
end
105-
return r, signgam
106+
return r
106107
end

test/logabsgamma.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,11 @@ x = 8.000001f0
156156
# (i.e. prevfloat(8.000001f0) == 8.0f0)
157157
# We still check appropriate behavior at 8.0f0
158158
@test ulp(8.0f0) < 0.4006594736129046
159+
160+
@testset "JET" begin
161+
# issue #502
162+
JET.@test_call logabsgamma(1.0)
163+
JET.@test_opt logabsgamma(1.0)
164+
JET.@test_call logabsgamma(1f0)
165+
JET.@test_opt logabsgamma(1f0)
166+
end

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ using Random
77
using Test
88
using Base.MathConstants: γ
99

10+
using JET: JET
11+
1012
using SpecialFunctions: AmosException, f64
1113

1214
# useful test functions for relative error, which differ from isapprox

0 commit comments

Comments
 (0)