-
Notifications
You must be signed in to change notification settings - Fork 10
add BigFloat fma and muladd #211
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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,92 @@ | ||
| # Copyright (c) 2023 MutableArithmetics.jl contributors | ||
| # | ||
| # This Source Code Form is subject to the terms of the Mozilla Public License, | ||
| # v.2.0. If a copy of the MPL was not distributed with this file, You can obtain | ||
| # one at http://mozilla.org/MPL/2.0/. | ||
|
|
||
| function test_fma_output_values(x::F, y::F, z::F) where {F<:BigFloat} | ||
| two_roundings_reference = x * y + z | ||
| one_rounding_reference = fma(x, y, z) | ||
| @test one_rounding_reference != two_roundings_reference | ||
|
|
||
| @testset "fma $op output values" for op in (MA.operate!, MA.operate!!) | ||
| (a, b, c) = map(t -> t + zero(F), (x, y, z)) # copy | ||
| @inferred op(fma, a, b, c) | ||
| @test one_rounding_reference == a | ||
| @test y == b | ||
| @test z == c | ||
| end | ||
|
|
||
| @testset "fma $op output values" for op in (MA.operate_to!, MA.operate_to!!) | ||
| (a, b, c) = map(t -> t + zero(F), (x, y, z)) # copy | ||
| out = F() | ||
| @inferred op(out, fma, a, b, c) | ||
| @test one_rounding_reference == out | ||
| @test x == a | ||
| @test y == b | ||
| @test z == c | ||
| end | ||
|
|
||
| return nothing | ||
| end | ||
|
|
||
| function test_fma_output_values(x::F, y::F, z::F) where {F<:Float64} | ||
| return test_fma_output_values(map(BigFloat, (x, y, z))...) | ||
| end | ||
|
|
||
| function test_fma_output_values_func(x::F, y::F, z::F) where {F<:Float64} | ||
| return let x = x, y = y, z = z | ||
| () -> test_fma_output_values(x, y, z) | ||
| end | ||
| end | ||
|
|
||
| @testset "fma output values: $exp_x $exp_y $sign_x $sign_y" for exp_x in (-3):3, | ||
| exp_y in (-3):3, | ||
| sign_x in (-1, 1), | ||
| sign_y in (-1, 1) | ||
|
|
||
| # Assuming a two-bit mantissa | ||
| significand_length = 2 | ||
|
|
||
| sign_z = -sign_x * sign_y | ||
| exp_z = exp_x + exp_y | ||
|
|
||
| base = 2.0 | ||
| bit_0 = 2.0^0 | ||
| bit_1 = 2.0^-1 | ||
|
|
||
| x = sign_x * base^exp_x * (bit_0 + bit_1) | ||
| y = sign_y * base^exp_y * (bit_0 + bit_1) | ||
| z = sign_z * base^exp_z * (bit_0 + bit_1) | ||
|
|
||
| setprecision( | ||
| test_fma_output_values_func(x, y, z), | ||
| BigFloat, | ||
| significand_length, | ||
| ) | ||
| end | ||
|
|
||
| @testset "muladd operate_to!! type inferred" begin | ||
| m1 = BigFloat(-1.0) | ||
| out = BigFloat() | ||
| @test iszero(@inferred MA.operate_to!!(out, Base.muladd, m1, m1, m1)) | ||
| end | ||
|
|
||
| @testset "muladd operate!! type inferred" begin | ||
| x = BigFloat(-1.0) | ||
| y = BigFloat(-1.0) | ||
| z = BigFloat(-1.0) | ||
| @test iszero(@inferred MA.operate!!(Base.muladd, x, y, z)) | ||
| end | ||
|
|
||
| @testset "fma $op doesn't allocate" for op in (MA.operate_to!, MA.operate_to!!) | ||
| alloc_test(let op = op, o = big"1.3", x = big"1.3" | ||
| () -> op(o, Base.fma, x, x, x) | ||
| end, 0) | ||
| end | ||
|
|
||
| @testset "fma $op doesn't allocate" for op in (MA.operate!, MA.operate!!) | ||
| alloc_test(let op = op, x = big"1.3", y = big"1.3", z = big"1.3" | ||
| () -> op(Base.fma, x, y, z) | ||
| end, 0) | ||
| end |
This file contains hidden or 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.
I'm not sure we want to do this because then
muladd(x, y, z)would give a different result thatoperate!(muladd, x, y, z)wouldn't it ?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.
Julia
Baseis being changed in the same direction, using the fusedmpfr_fmaformuladd: JuliaLang/julia#49401There 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.
Looks good to go then