-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3149 from andrioni/floatrounding
Ability to change rounding mode for all floats (cf. #2976)
- Loading branch information
Showing
13 changed files
with
258 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/pcre_h.jl | ||
/errno_h.jl | ||
/build_h.jl | ||
/fenv_constants.jl | ||
/file_constants.jl | ||
/uv_constants.jl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module Rounding | ||
include("fenv_constants.jl") | ||
|
||
export | ||
RoundingMode, RoundNearest, RoundToZero, RoundUp, RoundDown, | ||
get_rounding, set_rounding, with_rounding | ||
|
||
## rounding modes ## | ||
abstract RoundingMode | ||
type RoundNearest <: RoundingMode end | ||
type RoundToZero <: RoundingMode end | ||
type RoundUp <: RoundingMode end | ||
type RoundDown <: RoundingMode end | ||
|
||
set_rounding(::Type{RoundNearest}) = ccall(:fesetround, Cint, (Cint, ), JL_FE_TONEAREST) | ||
set_rounding(::Type{RoundToZero}) = ccall(:fesetround, Cint, (Cint, ), JL_FE_TOWARDZERO) | ||
set_rounding(::Type{RoundUp}) = ccall(:fesetround, Cint, (Cint, ), JL_FE_UPWARD) | ||
set_rounding(::Type{RoundDown}) = ccall(:fesetround, Cint, (Cint, ), JL_FE_DOWNWARD) | ||
|
||
function get_rounding() | ||
r = ccall(:fegetround, Cint, ()) | ||
if r == JL_FE_TONEAREST | ||
return RoundNearest | ||
elseif r == JL_FE_DOWNWARD | ||
return RoundDown | ||
elseif r == JL_FE_UPWARD | ||
return RoundUp | ||
elseif r == JL_FE_TOWARDZERO | ||
return RoundToZero | ||
else | ||
error() | ||
end | ||
end | ||
|
||
function with_rounding{T<:RoundingMode}(f::Function, rounding::Type{T}) | ||
old_rounding = get_rounding() | ||
set_rounding(rounding) | ||
try | ||
return f() | ||
finally | ||
set_rounding(old_rounding) | ||
end | ||
end | ||
|
||
end #module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include <fenv.h> | ||
const JL_FE_INEXACT = FE_INEXACT | ||
const JL_FE_UNDERFLOW = FE_UNDERFLOW | ||
const JL_FE_OVERFLOW = FE_OVERFLOW | ||
const JL_FE_DIVBYZERO = FE_DIVBYZERO | ||
const JL_FE_INVALID = FE_INVALID | ||
const JL_FE_TONEAREST = FE_TONEAREST | ||
const JL_FE_UPWARD = FE_UPWARD | ||
const JL_FE_DOWNWARD = FE_DOWNWARD | ||
const JL_FE_TOWARDZERO = FE_TOWARDZERO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.