-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
RFC: Ability to change rounding mode for all floats (cf. #2976) #3149
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
95dbacb
Add rounding modes to normal floating point arithmetic
andrioni 823b017
Add Float32 rounding mode tests
andrioni 1a13239
Integrate the new rounding modes with BigFloats
andrioni 451f21e
Export RoundAwayZero too (even though it only works for BigFloats)
andrioni 4f6ee0f
Stop hardcoding the `fenv.h` constants
andrioni 454f61a
Fix bug while parsing fenv_constants.h
andrioni 5a064b7
Renaming of the rounding modes
andrioni 54a4055
Fixes post-rebase
andrioni e314eba
Remove fenv_constants.jl, as it is auto-generated
andrioni 285b238
Documentation for the rounding modes (manual + stdlib)
andrioni 773b60e
Fix issue with fenv.h and Ubuntu 12.04
andrioni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const JL_FE_UNDERFLOW = 0x0010 | ||
const JL_FE_OVERFLOW = 0x0008 | ||
const JL_FE_DIVBYZERO = 0x0004 | ||
const JL_FE_INVALID = 0x0001 | ||
const JL_FE_TONEAREST = 0x0000 | ||
const JL_FE_UPWARD = 0x0800 | ||
const JL_FE_DOWNWARD = 0x0400 | ||
const JL_FE_TOWARDZERO = 0x0c00 |
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
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
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,89 @@ | ||
# Small sanity tests to ensure changing the rounding of float functions work | ||
using Base.Test | ||
|
||
## Float64 checks | ||
# a + b returns a number exactly between prevfloat(1.) and 1., so its | ||
# final result depends strongly on the utilized rounding direction. | ||
a = prevfloat(0.5) | ||
b = 0.5 | ||
c = 0x1p-54 | ||
d = prevfloat(1.) | ||
|
||
# Default rounding direction, RoundNearest | ||
@test a + b === 1. | ||
@test - a - b === -1. | ||
@test a - b === -c | ||
@test b - a === c | ||
|
||
# RoundToZero | ||
with_rounding(RoundToZero) do | ||
@test a + b === d | ||
@test - a - b === -d | ||
@test a - b === -c | ||
@test b - a === c | ||
end | ||
|
||
# Sanity check to see if we have returned to RoundNearest | ||
@test a + b === 1. | ||
@test - a - b === -1. | ||
@test a - b == -c | ||
@test b - a == c | ||
|
||
# RoundUp | ||
with_rounding(RoundUp) do | ||
@test a + b === 1. | ||
@test - a - b === -d | ||
@test a - b === -c | ||
@test b - a === c | ||
end | ||
|
||
# RoundDown | ||
with_rounding(RoundDown) do | ||
@test a + b === d | ||
@test - a - b === -1. | ||
@test a - b === -c | ||
@test b - a === c | ||
end | ||
|
||
## Float32 checks | ||
|
||
a32 = prevfloat(0.5f0) | ||
b32 = 0.5f0 | ||
c32 = (1.f0 - prevfloat(1.f0))/2 | ||
d32 = prevfloat(1.0f0) | ||
|
||
# Default rounding direction, RoundNearest | ||
@test a32 + b32 === 1.0f0 | ||
@test - a32 - b32 === -1.0f0 | ||
@test a32 - b32 === -c32 | ||
@test b32 - a32 === c32 | ||
|
||
# RoundToZero | ||
with_rounding(RoundToZero) do | ||
@test a32 + b32 === d32 | ||
@test - a32 - b32 === -d32 | ||
@test a32 - b32 === -c32 | ||
@test b32 - a32 === c32 | ||
end | ||
|
||
# Sanity check to see if we have returned to RoundNearest | ||
@test a32 + b32 === 1.0f0 | ||
@test - a32 - b32 === -1.0f0 | ||
@test a32 - b32 == -c32 | ||
@test b32 - a32 == c32 | ||
|
||
# RoundUp | ||
with_rounding(RoundUp) do | ||
@test a32 + b32 === 1.0f0 | ||
@test - a32 - b32 === -d32 | ||
@test a32 - b32 === -c32 | ||
@test b32 - a32 === c32 | ||
end | ||
|
||
# RoundDown | ||
with_rounding(RoundDown) do | ||
@test a32 + b32 === d32 | ||
@test - a32 - b32 === -1.0f0 | ||
@test a32 - b32 === -c32 | ||
@test b32 - a32 === c32 | ||
end |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this file shouldn't be part of the commit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, I thought I removed it, but now I see I just added the file to
.gitignore
. Done, thanks.