Skip to content

Commit

Permalink
ArbCall: Add method for setting error_on_failure for fpwrap methods
Browse files Browse the repository at this point in the history
Previously the error_on_failure argument for fpwrap methods would
default to false. Now it still defaults to false but this can be
changed using the fpwrap_error_on_fauilure_default_set method. This
allows switching this on or off globally for easier debugging whens
searching for what produces a NaN. The current implementation leads to
zero overhead, it only incurs a compile time cost.

See comments in #138 and
#150 for some context.
  • Loading branch information
Joel-Dahne committed May 3, 2022
1 parent a42dcc6 commit 923a423
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 8 deletions.
34 changes: 32 additions & 2 deletions src/ArbCall/ArbFPWrapFunction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@ end

jlfname(af::ArbFPWrapFunction) = Symbol(:fpwrap_, split(arbfname(af), "_", limit = 4)[4])

# See https://github.com/kalmarek/Arblib.jl/issues/138 for some
# discussions related to this approach for default arguments
"""
fpwrap_error_on_failure_default()
Function giving the default value for the argument `error_on_failure`.
See also [`fpwrap_error_on_failure_default_set`](@ref).
"""
fpwrap_error_on_failure_default() = false

"""
fpwrap_error_on_failure_default_set(value::Bool)
Set the default value for the argument `error_on_failure` for `fpwrap`
methods. See also [`fpwrap_error_on_failure_default`](@ref).
"""
function fpwrap_error_on_failure_default_set(value::Bool)
if fpwrap_error_on_failure_default() != value
@eval fpwrap_error_on_failure_default() = $value
end
end

# TODO: Improve support for vector arguments
function jlargs(af::ArbFPWrapFunction)
cargs = arguments(af)
Expand All @@ -83,13 +105,21 @@ function jlargs(af::ArbFPWrapFunction)

if basetype(af) == Float64
kwargs = [
Expr(:kw, :(error_on_failure::Bool), :(false)),
Expr(
:kw,
:(error_on_failure::Bool),
:(Arblib.ArbCall.fpwrap_error_on_failure_default()),
),
Expr(:kw, :(correct_rounding::Bool), :(false)),
Expr(:kw, :(work_limit::Integer), :(8)),
]
else
kwargs = [
Expr(:kw, :(error_on_failure::Bool), :(false)),
Expr(
:kw,
:(error_on_failure::Bool),
:(Arblib.ArbCall.fpwrap_error_on_failure_default()),
),
Expr(:kw, :(accurate_parts::Bool), :(false)),
Expr(:kw, :(correct_rounding::Bool), :(false)),
Expr(:kw, :(work_limit::Integer), :(8)),
Expand Down
48 changes: 42 additions & 6 deletions test/ArbCall/ArbFPWrapFunction-test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,29 @@
end
end

@testset "fpwrap_error_on_failure" begin
@test !Arblib.ArbCall.fpwrap_error_on_failure_default()
Arblib.ArbCall.fpwrap_error_on_failure_default_set(true)
@test Arblib.ArbCall.fpwrap_error_on_failure_default()
Arblib.ArbCall.fpwrap_error_on_failure_default_set(true)
@test Arblib.ArbCall.fpwrap_error_on_failure_default()
Arblib.ArbCall.fpwrap_error_on_failure_default_set(false)
@test !Arblib.ArbCall.fpwrap_error_on_failure_default()
Arblib.ArbCall.fpwrap_error_on_failure_default_set(false)
@test !Arblib.ArbCall.fpwrap_error_on_failure_default()
end

@testset "jlargs" begin
for (str, args, kwargs) in (
(
"int arb_fpwrap_double_exp(double * res, double x, int flags)",
[:(x::$(Union{Float16,Float32,Float64}))],
[
Expr(:kw, :(error_on_failure::Bool), :(false)),
Expr(
:kw,
:(error_on_failure::Bool),
:(Arblib.ArbCall.fpwrap_error_on_failure_default()),
),
Expr(:kw, :(correct_rounding::Bool), :(false)),
Expr(:kw, :(work_limit::Integer), :(8)),
],
Expand All @@ -86,7 +102,11 @@
"int arb_fpwrap_cdouble_exp(complex_double * res, complex_double x, int flags)",
[:(x::$(Union{ComplexF16,ComplexF32,ComplexF64}))],
[
Expr(:kw, :(error_on_failure::Bool), :(false)),
Expr(
:kw,
:(error_on_failure::Bool),
:(Arblib.ArbCall.fpwrap_error_on_failure_default()),
),
Expr(:kw, :(accurate_parts::Bool), :(false)),
Expr(:kw, :(correct_rounding::Bool), :(false)),
Expr(:kw, :(work_limit::Integer), :(8)),
Expand All @@ -96,7 +116,11 @@
"int arb_fpwrap_double_lambertw(double * res, double x, slong branch, int flags)",
[:(x::$(Union{Float16,Float32,Float64})), :(branch::$Integer)],
[
Expr(:kw, :(error_on_failure::Bool), :(false)),
Expr(
:kw,
:(error_on_failure::Bool),
:(Arblib.ArbCall.fpwrap_error_on_failure_default()),
),
Expr(:kw, :(correct_rounding::Bool), :(false)),
Expr(:kw, :(work_limit::Integer), :(8)),
],
Expand All @@ -105,7 +129,11 @@
"int arb_fpwrap_cdouble_lambertw(complex_double * res, complex_double x, slong branch, int flags)",
[:(x::$(Union{ComplexF16,ComplexF32,ComplexF64})), :(branch::$Integer)],
[
Expr(:kw, :(error_on_failure::Bool), :(false)),
Expr(
:kw,
:(error_on_failure::Bool),
:(Arblib.ArbCall.fpwrap_error_on_failure_default()),
),
Expr(:kw, :(accurate_parts::Bool), :(false)),
Expr(:kw, :(correct_rounding::Bool), :(false)),
Expr(:kw, :(work_limit::Integer), :(8)),
Expand All @@ -115,7 +143,11 @@
"int arb_fpwrap_double_legendre_root(double * res1, double * res2, ulong n, ulong k, int flags)",
[:(n::$Unsigned), :(k::$Unsigned)],
[
Expr(:kw, :(error_on_failure::Bool), :(false)),
Expr(
:kw,
:(error_on_failure::Bool),
:(Arblib.ArbCall.fpwrap_error_on_failure_default()),
),
Expr(:kw, :(correct_rounding::Bool), :(false)),
Expr(:kw, :(work_limit::Integer), :(8)),
],
Expand All @@ -131,7 +163,11 @@
:(regularized::$Integer),
],
[
Expr(:kw, :(error_on_failure::Bool), :(false)),
Expr(
:kw,
:(error_on_failure::Bool),
:(Arblib.ArbCall.fpwrap_error_on_failure_default()),
),
Expr(:kw, :(correct_rounding::Bool), :(false)),
Expr(:kw, :(work_limit::Integer), :(8)),
],
Expand Down

0 comments on commit 923a423

Please sign in to comment.