├── REQUIRE ├── src ├── FastDirectedRounding.jl ├── eftRound.jl └── directRounding.jl ├── LICENSE └── README.md /REQUIRE: -------------------------------------------------------------------------------- 1 | julia 0.5- 2 | AdjacentFloat 3 | -------------------------------------------------------------------------------- /src/FastDirectedRounding.jl: -------------------------------------------------------------------------------- 1 | module FastDirectedRounding 2 | 3 | import Base: (+), (-), (*), (/), (\), sqrt, hypot, inv 4 | 5 | using AdjacentFloat 6 | 7 | import ErrorfreeArithmetic: eftAdd, eftSub, eftMul, eftSqr, accDivForSign, accInv, accSqrtForSign 8 | 9 | export sqr 10 | 11 | 12 | include("eftRound.jl") 13 | include("directRounding.jl") 14 | 15 | end # module 16 | -------------------------------------------------------------------------------- /src/eftRound.jl: -------------------------------------------------------------------------------- 1 | #= 2 | using the result of an errorfree transformation 3 | to drive highly performant multi-directed roundings 4 | =# 5 | 6 | @inline eftRound{T<:AbstractFloat}(hi::T, lo::T, ::RoundingMode{:ToZero}) = 7 | (((signbit(hi)==signbit(lo)) | (lo==zero(T))) ? hi : nextNearerToZero(hi)) 8 | 9 | @inline eftRound{T<:AbstractFloat}(hi::T, lo::T, ::RoundingMode{:FromZero}) = 10 | (((signbit(hi)!=signbit(lo)) | (lo == zero(T))) ? hi : nextAwayFromZero(hi)) 11 | 12 | @inline eftRound{T<:AbstractFloat}(hi::T, lo::T, ::RoundingMode{:Up}) = 13 | (((signbit(lo) | (lo == zero(T)))) ? hi : nextFloat(hi)) 14 | 15 | @inline eftRound{T<:AbstractFloat}(hi::T, lo::T, ::RoundingMode{:Down}) = 16 | (((signbit(lo) & (lo != zero(T)))) ? prevFloat(hi) : hi) 17 | 18 | @inline eftRound{T<:AbstractFloat}(hi::T, lo::T, ::RoundingMode{:Nearest}) = 19 | (hi) 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Jeffrey Sarnoff 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/directRounding.jl: -------------------------------------------------------------------------------- 1 | function (inv){T<:AbstractFloat, R<:RoundingMode}(a::T, rounding::R) 2 | hi,lo = accInv(a) 3 | eftRound(hi, lo, rounding) 4 | end 5 | 6 | function (+){T<:AbstractFloat, R<:RoundingMode}(a::T, b::T, rounding::R) 7 | hi,lo = eftAdd(a,b) 8 | eftRound(hi, lo, rounding) 9 | end 10 | 11 | function (-){T<:AbstractFloat, R<:RoundingMode}(a::T, b::T, rounding::R) 12 | hi,lo = eftSub(a,b) 13 | eftRound(hi, lo, rounding) 14 | end 15 | 16 | function (*){T<:AbstractFloat, R<:RoundingMode}(a::T, b::T, rounding::R) 17 | hi,lo = eftMul(a,b) 18 | eftRound(hi, lo, rounding) 19 | end 20 | 21 | function (/){T<:AbstractFloat, R<:RoundingMode}(a::T, b::T, rounding::R) 22 | hi,lo = accDiv(a,b) 23 | eftRound(hi, lo, rounding) 24 | end 25 | 26 | function (\){T<:AbstractFloat, R<:RoundingMode}(a::T, b::T, rounding::R) 27 | hi,lo = accDiv(b,a) 28 | eftRound(hi, lo, rounding) 29 | end 30 | 31 | function (sqr){T<:AbstractFloat, R<:RoundingMode}(a::T, rounding::R) 32 | hi,lo = eftSqr(a) 33 | eftRound(hi, lo, rounding) 34 | end 35 | 36 | function (sqrt){T<:AbstractFloat, R<:RoundingMode}(a::T, rounding::R) 37 | hi,lo = accSqrt(a) 38 | eftRound(hi, lo, rounding) 39 | end 40 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## FastDirectedRounding 2 | ###### faster rounding up and down and nearest without switching the fpu's rounding mode 3 | ```ruby 4 | Jeffrey Sarnoff © 2016-Mar-12 in New York, NY 5 | 2016-Jun-25 at v"0.1.0" 6 | ``` 7 | 8 | #### Use 9 | 10 | with a,b floating point values, op in {+,-,*,/,sqrt,square} and rounding a rounding mode 11 | (the rounding modes are RoundNearest, RoundUp, RoundDown, RoundToZero, RoundFromZero) 12 | 13 | value = (op)(a, b, rounding ) 14 | c = ( +)(a, b, RoundDown) 15 | 16 | (this package offers RoundFromZero for Floats, while Julia v0.5 does not) 17 | 18 | ##### Rounding made faster (q.v. src dir, AdjacentFloat.jl) 19 | ``` 20 | # hi, lo are the two parts of a precision-doubled floating point type 21 | # given a rounding mode, the signs of hi and lo select the rounding logic 22 | 23 | RoundDown 24 | 25 | hi lo rounding fast rounding 26 | -------------------------------------------------------------------- 27 | + + hi hi 28 | + - prevfloat(hi) nextNearerToZero(hi) == prevFloat(hi) 29 | - + hi hi 30 | - - prevfloat(hi) nextAwayFromZero(hi) == prevFloat(hi) 31 | 32 | RoundUp 33 | 34 | hi lo rounding fast rounding 35 | -------------------------------------------------------------------- 36 | + + nextfloat(hi) nextAwayFromZero(hi) == nextFloat(hi) 37 | + - hi hi 38 | - + nextfloat(hi) nextNearerToZero(hi) == nextFloat(hi) 39 | - - hi hi 40 | 41 | RoundFromZero 42 | 43 | hi lo rounding fast rounding 44 | -------------------------------------------------------------------- 45 | + + nextfloat(hi) nextAwayFromZero(hi) == nextFloat(hi) 46 | + - hi hi 47 | - + hi hi 48 | - - prevfloat(hi) nextAwayFromZero(hi) == prevFloat(hi) 49 | 50 | RoundToZero 51 | 52 | hi lo rounding fast rounding 53 | -------------------------------------------------------------------- 54 | + + hi hi 55 | + - prevfloat(hi) nextNearerToZero(hi) == prevFloat(hi) 56 | - + nextfloat(hi) nextNearerToZero(hi) == prevFloat(hi) 57 | - - hi hi 58 | 59 | RoundNearest 60 | 61 | hi lo rounding fast rounding 62 | -------------------------------------------------------------------- 63 | + + hi hi 64 | + - hi hi 65 | - + hi hi 66 | - - hi hi 67 | ``` 68 | --------------------------------------------------------------------------------