Skip to content
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

Add support for floating point exceptions #47930

Draft
wants to merge 12 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
add some tests
  • Loading branch information
simonbyrne committed Dec 20, 2022
commit 7436f340c8d84cd334baaff162e7b88590ab0335
4 changes: 2 additions & 2 deletions src/jlapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,9 @@ JL_DLLEXPORT int jl_get_fenv_except(void)
fenv_t env;
fegetenv(&env);
#if defined(_CPU_AARCH64_)
return (env.__fpcr & FE_ALL_EXCEPT << 8);
return (env.__fpcr >> 8 & FE_ALL_EXCEPT);
#elif defined(_CPU_X86_64_)
return (~env.__mxcsr & FE_ALL_EXCEPT << 7);
return (~env.__mxcsr >> 7 & FE_ALL_EXCEPT);
#else
return -1;
#endif
Expand Down
39 changes: 39 additions & 0 deletions test/rounding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ using Base.MathConstants

using Test


@testset "Float64 checks" begin
# a + b returns a number exactly between prevfloat(1.) and 1., so its
# final result depends strongly on the utilized rounding direction.
Expand Down Expand Up @@ -351,3 +352,41 @@ end
Base.Rounding.setrounding_raw(T, Base.Rounding.to_fenv(old))
end
end


try
Base.Rounding.get_exceptions()
fp_exceptions = true
catch e
fp_exceptions = false
end

if fp_exceptions

@testset "floating point exceptions" begin
@test Base.Rounding.get_exceptions() == (invalid = false, inexact = false, underflow = false, overflow = false, dividebyzero = false)

@test isnan(0/0)
@test 1/0 == Inf

Base.Rounding.set_exceptions(invalid=true)
@test Base.Rounding.get_exceptions() == (invalid = true, inexact = false, underflow = false, overflow = false, dividebyzero = false)

@test_throws InvalidFloatingPointException 0/0
@test 1/0 == Inf

Base.Rounding.set_exceptions(dividebyzero=true)
@test Base.Rounding.get_exceptions() == (invalid = true, inexact = false, underflow = false, overflow = false, dividebyzero = true)

@test_throws InvalidFloatingPointException 0/0
@test_throws DivideByZeroFloatingPointException 1/0

Base.Rounding.set_exceptions(invalid=false, dividebyzero=false)

@test Base.Rounding.get_exceptions() == (invalid = false, inexact = false, underflow = false, overflow = false, dividebyzero = false)

@test isnan(0/0)
@test 1/0 == Inf

end
end